[Date Prev] [Date Next] [Thread Prev] [Thread Next] Date Index Thread Index Search archive:
Date:Mon Dec 7 18:45:33 1993 
Subject:Re:languages for self-modifying code 
From:Robin Popplestone 
Volume-ID:931209.01 

> A second issue is whether the generated program is to run in the same
> process as the generator. In this case the generated program must be
> in the same language as the generator or in a language (tcl, for
> example) for which the generating language has an interpreter.

Or -compiler- in the case of POPLOG which generates native code for a range of
languages.  It will also support linking, relinking and loading of external
functions into itself. A simple example is:

/*
cfn is a POP-11 procedure which takes as arguments -f-, the name of an
function in the C language, and -string-, its definition. It compiles
the C code, links it into POPLOG and defines a POP-11 procedure
with the same name which calls the external code.
*/


define cfn(f,string);
  lvars string,i,
    rep = discout('temp.c'),              ;;; Make output file
    ;
    for i from 1 to datalength(string) do ;;; Copy string to file
      rep(string(i));
    endfor;
    rep(termin);                          ;;; Close file
    sysobey('gcc -c temp.c');             ;;; Call GNU-C compiler
    lvars Exptr = "Exptr_"<>f;            ;;; Name of pointer to external code
    popval([exload  ^f ['temp.o']         ;;; Link in the compiled code
             (language C)
             ^Exptr(1):int <- ^f;         ;;; With -f- code in Exptr
            endexload
           ]);
    popval([define ^f(x); exacc ^Exptr(x); ;;; Define POP-11 procedure
            enddefine]);

enddefine;

;;; Define a C-function "twice", compile it and incrementally link it to
;;; POPLOG

cfn("twice", 'int twice(int x) {return x*2;}');

;;; twice is now a POP-11 procedure which uses the compiled C - call it and
;;; print out the answer

twice(23)=>

** 46