I am reading about Goedel machine http://people.idsia.ch/~juergen/goedelmachine.html and especially the article about possible implementation (in the Scheme language) of this machine http://people.idsia.ch/~juergen/selfreflection.pdf . The idea is that there is Scheme environment, then there is special virtual machine (written in Scheme) and at laste there is program (written in the special language for this virtual machine - L3 in the article) for this virtual machine. The virtual machine is quite unusual - it not only loads the program but it also provides extension point through which the program can modify the value of any variable of the executing program and also modify the instruction tree that sits in the memory of the virtual machine. That is the self-modifying progam.

I have 2 questions regarding self-modifying programs:

  1. Is there virtual machine (e.g. for Java or JavaScript), that exposes such facilities for modifying the values of the variables of the running program or for modifying the loaded program structure?
  2. I can not grasp the difference between run-time modification of the self-modifying program (let us call it the scenario S1) and between the following process (that can implement the self-modifying program scenario alternatively) (let us call it the scenario S2):
    1. Program L3 (continuing the notation to name the upper-most program) computes: 1) the updated program - new code; 2) the new values of all the variables of the old program and the initial values of the additional variables introduced anew by the updated program; 3) the codepoint of the updated program at which the execution should be started
    2. Program L3 exits and instructs the execution environment about the next steps that should be taken;
    3. Execution environment loads the updated program and assigns the values of the variables and the starting point/point of resume/point of load according to the point 1. and execution environment boosts the updated program.
    4. Updated program executes and computes the next version of the update in parallel.

Of course, there is third option (S3) - to rewrite JavaScript function pointers of use Perl/Python eval construction - it keeps the current code running, but it spans the new code. So - is there difference between S1, S2, S3? The drawback of S3 is that the memory used by the previous versions of the code is never recovered and so - the program can not be life-long running.

So - what is the difference between S1 and S2? If we consider the conventional computing architecture where the program is executed deterministically (assuming we can control the time slices between tread execution and so on) then I can not see the difference between S1 and S2? In both cases the currently running program should decide which variables and how should be changed, which instruction should be executed next and so on, so on. Restart with update should be the same as the run-time modification of in-memory running program. Or - maybe there is any difference?

Just curious - are there any other applications, research efforts of self-modifying code (especially for Java and JavaScript) which can guide me in my efforts to understand how to implement the self-modifying programs?

There is http://commons.apache.org/proper/commons-bcel/index.html for Java, but I guess, that it does not allow to modify the values of the variables of the code itself or already loaded (for execution) Java class file. There is also culture of meta-circular interpreters https://en.wikipedia.org/wiki/Meta-circular_evaluator - but they are more the intellectual curiosities and the opening up of the running program (variables and code) is not the feature they offer, so, no use for self-modifying programs.

  • 1
    $\begingroup$ Machine language is an example of a fully self-modifiable programming language. The answer to your second question is tricky, because you did not specify with precision what it means to compare two programming languages. If e.g. time and memory usage are observable then the two approaches are different. $\endgroup$ – Martin Berger May 22 '19 at 11:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.