I’m very interesting in compilation and I’ve got a question about gcc.
I know that a tree is generated from the code to compile, then ASM code is
generated and I need some explanations about this point.
ASM code is added in a file and executed later or ASM code is directly loaded in memory with asm functions ? I’m working on a small compiler and I don’t know how to execute the tree generated, and I didn’t find any documentation about that.
GCC’s front-end parses the source files in different languages (C, C++, Fortran, ObjectiveC, Java etc.). Then the code (AST) is translated to internal representation, the RTL (register transfer language). This is a close-to-assembly representation.
Then this RTL code is transformed to target machine’s assembly and written to .o (object) file.
The linker then combines generated .o-files to the executable.
The “inline” assembly snippets are also supported by GCC in C/C++.
The workflow is
For the interpreter you may directly interpret the AST or produce you own opcodes for the virtual machine since such an interpreter (virtual machine) would be simpler than the AST interpreter.
If you want all the details you should look at LCC (with a book by Chris Fraser and David Hanson). All the details of code generation for real-world architectures are provided in the accompanying book.
And to know what can be done with the generated code you should read the Linkers and Loaders by John Levine book.
Finally, to avoid asking everything about scripting/interpreters, refer to Game Scripting Mastery by Alex Varanese.