Reputation: 2293
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.
Upvotes: 0
Views: 381
Reputation: 735
HOW A SOURCE CODE TRANSLATED TO EXECUTABLE CODE ? We provide Source Code to the compiler and it gives us Executable code .But this is not a single step operation .This follow some predefined steps to convert the Source Code to Executable Code.
steps followed for conversion from source code to executable code
1.Preprocessor It is very useful part of the compiler as it does lots of job before translated to machine code. It is a text processor which dose the below text editing operation
It removes the comment lines in source code which are written in the Source code for more readability/easy understanding . It add the content of header files to the source code. Header files always contains function prototypes and declarations.(Header files never contain any executable code ) A very important property of Preprocessor is Conditional Compile. It is very required for scalable design. This property also remove the unnecessary burden from compiler. Macros are replaced by this preprocessor.
The final output of this stage is known as pure C code.
2.Translator This part of complier is responsible for converting of pure C code to assembly language code. Step by step mapping of C language code to assembly language code done here. The prototypes of the functions and declarations are used by this part for translation of C code. The out put of this stage known as assembly code.
3.Assembler It generate Object code from assembly language code.It converts the assembly language codes to machine language code(i.e in 0's and 1's format).It is not directly run as we take the help of OS to execute our code in processor. The out put of this stage known as object code.
4.Linker It give the final executable code which is going to be run in our machine. The output of this stage is known as executable code. Which is a combination of object code and supporting files. The supporting files may be user defined function definitions ,predefined library function definitions ...etc.
Upvotes: 1
Reputation: 14467
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
Source file ->
AST ->
RTL representation ->
machine codes (with _optional_ text output of the ASM code) ->
Executable (produced by linker)
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.
Upvotes: 2
Reputation:
Quite vague a question, and I don't think I fully understood what your exact problem is, but here you are an answer anyway: assembly is not put in the executable. Assembly is written to an intermediate assembly file, from which the assembler generates true binary machine code (called an object file), then the linker merges them (along with the needed libraries) to the final executable. When the application is run, the executable is loaded directly into the RAM by the OS and executed natively by the processor.
Upvotes: 2