The article Changing the code of a program during its execution, using the example of Common Lisp, shows this wonderful feature, but unfortunately only for CL. Tell me please, is it really problematic for C? After all, there is for example the Tiny C Compiler , which can work in interpreter mode, and the presence of the libtcc library allows for the dynamic generation of code.

In general, the possibility of "changing the program code during its execution" for C is of interest.

  • There are no methods out of the box. Possible options: * a compiler is available on Unix, so the program can generate source code, compile it on the fly and run * you can implement an interpreter of any language (at least the same C), and pass through the necessary parts of the source code. - VladD 2:57 pm
  • 2
    Better so now dynamically create a shared library and load. Only there is a sense in this, if the code in it is rarely modified, and is used often or performs very massive calculations, for example. Compiling is a rather slow thing. - skegg
  • one
    Purely theoretically, you can search for how “crackers” do it by writing the code to the stack (buffer overflow (this is a search hint)) and calling it. By the way @dreq, the statement that this is a great opportunity is very controversial. Try to understand someone else's program, which is so written. - avp
  • @avp, in Linux, the stack section is prohibited for execution. - skegg
  • Yes, but the technique will not fundamentally change if, instead of a stack, you take a completely “mmap” memory area with recording and execution resolution. - avp

3 answers 3

For such purposes, it is best to use embedded interpreted language. Lua or Python are very convenient in this respect. You can, of course, use tcc, but this thing is rather primitive and does not go on with self-indulgence. If you need something serious - then these powerful languages ​​or others like them.

And C is a compiled language. Therefore, such a dynamic code change is more than problematic for it.

    Embeddable Common Lisp - ECL can also be easily integrated into applications written in other languages, such as a scripting language, but with more advanced features: Common Lisp, compiling into bytecode or machine code (if a C compiler is available).

      1. Write your C interpreter
      2. Supplement the base language with metaprogramming tools (something from the pattern matching / replace field on the AST tree of the program, can someone throw a link on the implementation principles)
      3. Implement dynamic compilation (does JIT from LLVM complete?)

      The theory of metaprogramming (in one of the options - the school of Turchin) can be tried here:

      Who is in the subject? Please send links to lectures / articles on other approaches. Metaprogramming with the use of Minsky frames is of particular interest, was it used and how.

      • PS: who is interested in this nonsense about interpreted / compiled languages ? such a feeling that the classics did not see any classics on CS, and cite some tatty training manual - Dmitry Ponyatov