Code Generator

This unit processes intermediate code and generates executable code and reference records for Linker.

Intermediate Code

Most compilers use some kind of intermediate code which is a program representation somewhere betwen the high-level source code and executable code for target processor. This means that the compiler does not generate the target code directly but it generates some intermediate pseudo instructions which are converted into target code after the program is compiled. 

Generating Instructions

Turbo Pascal uses few procedures to generate any x86 instruction needed. In general two intermediate code records are used: one for actual inctruction opcodes and imemdiate data and one for references.

Creating and Destroying Stack Frame

Programs created with Turbo Pascal use stack frame. This is part of the processor's stack space which can be accessed using a dedicated base pointer (BP) register.

Module Entry and Exit Code

Each module (program or unit) needs some initialization and finalization: entry and exit code.

Interrupt Entry and Exit Code

Interrupt procedures also need some initialization and finalization.

Procedure Entry and Exit Code

Procedures and functions (including methods) also need initialization and finalization code.

Jumps and Labels

Turbo Pascal generates jumps in an interesting way. Since most jumps will jump to the same place, Turbo Pascal keeps them in a linked list.

Creating References

Whenever Turbo Pascal needs to reference anything that doesn't have fixed location, i.e. its address is not known yet like variables, typed constants or procedures, the compiler generates intermediate code reference record which contains all information to later resolve the reference.

ES:DI Address Loading

Turbo Pascal tries to optimize loading of addresses to ES:DI register pair. For this purpose it uses few intermediate code instructions that track addresses that need to be loaded.

Optimizing Jumps

Turbo Pascal tries to convert all near jumps to short. This is only possible if jump displacement can be stored in a byte.
<< Start < Prev 1 2 Next > End >>

© 2021 Turbo Pascal | Privacy Policy