I do not understand how a program such as an interpreter can run and create code "on the fly" to run. Can someone help me understand what such a program would have to do in order to function?

I'm familiar with the process that a compiler takes the input file, creates the AST, produces the machine code, then the result is an .exe, .elf, etc. file to be run. The OS loader then takes the .exe (or .elf) file and loads that code into a memory chunk and starts execution. I understand that that process can fork and create a copy of it's process space, or create a new thread with system calls to the OS. I just don't understand how a program can run code itself. Does it just get extra space in its address space to write new code to? Does the typical CPU segmentation model even allow write access to the code segment? Is there a built in way to tell the OS Loader that the program will create and run new code on it's own?

  • $\begingroup$ It just allocates memory, writes code there, modifies permissions so the page can be executed (see msdn.microsoft.com/en-us/library/windows/desktop/… for Windows) and jumps there. Also, it is not Interpreter which runs code (as name suggests, Interpreter only interprets the code), but it is a definition nit picking. $\endgroup$ – user1543037 Jan 24 '18 at 0:44
  • $\begingroup$ So how would a basic interpreter work? Could it be done as a single process in a loop that reads a line of code, translates to machine code, and writes it into it's own process virtual memory space, execute it, then jump back to the interpreter section? Can you point me towards any basic literature on this? $\endgroup$ – bwjohns4 Jan 24 '18 at 2:03
  • $\begingroup$ Typically interpreter simply reads the code and executes it using code prepared beforehand (imagine a bunch of ifs and a logic "if opcode is this and parameters are those then I do this"). This approach is slow so there are optimizations based on generating machine code on the fly (e.g., see template interpreter in JVM Hot Spot) but usually you can't avoid adding logical "if" for every opcode. What you want to achieve is Just in time compiler which generates code and then just executes it. You might read Dragon Book but I don't recall if it goes deep in technical details. $\endgroup$ – user1543037 Jan 24 '18 at 16:39
  • $\begingroup$ @bwjohns4: "So how would a basic interpreter work? Could it be done as […] a loop that reads a line of code, translates to machine code, […]". No. If it translates (to machine code or any other language), then it's a compiler, not an interpreter. Compiler is just another word for translator. A compiler translates, an interpreter runs. An interpreter reads the code, "understands" it and runs it. A compiler reads the code, "understands" it and outputs an equivalent program in another language. You may find this interesting: softwareengineering.stackexchange.com/a/269878/1352 $\endgroup$ – Jörg W Mittag Jan 28 '18 at 20:56

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.