2. Target program
The code generator's output is the target program. The result could be:
- Assembly language: It allows subprograms to be separately compiled.
- Relocatable machine language: It simplifies the code generating process.
- Absolute machine language: It can be stored in a specific position in memory and run immediately.
3. Memory management
In the memory management design, the source program's frontend and code generator map names address data items in run-time memory. It utilizes a symbol table. In a three-address statement, a name refers to the name's symbol-table entry. Labels in three-address statements must be transformed into instruction addresses.
For example,
j: goto i generates the following jump instruction:
if i < j, A backward jump instruction is generated with a target address equal to the quadruple i code location.
If i > j, It's a forward jump. The position of the first quadruple j machine instruction must be saved on a list for quadruple i. When i is processed, the machine locations for all instructions that forward hop to i are populated.
4. Instruction selection
In the Instruction selection, the design issues in the code generator program's efficiency will be improved by selecting the optimum instructions. It contains all of the instructions, which should be thorough and consistent. Regarding efficiency, instruction speeds and machine idioms have a big effect. Instruction selection is simple if we don't care about the target program's efficiency.
The relevant three-address statements, for example, would be translated into the following code sequence:
P:=Q+R
S:=P+T
MOV Q, R0
ADD R, R0
MOV R0, P
MOV P, R0
ADD T, R0
MOV R0, S
The fourth sentence is unnecessary since the P value is loaded again in that statement already stored. It results in an inefficient code sequence. A given intermediate representation can be translated into several distinct code sequences, each with considerable cost differences. Previous knowledge of instruction cost is required to build good sequences, yet reliable cost information is difficult to forecast.
5. Register allocation
In the Register allocation, design issues in the code generator can be accessed faster than memory. The instructions involving operands in the register are shorter and faster than those involved in memory operands.
The following sub-problems arise when we use registers:
- Register allocation: In register allocation, we select the set of variables that will reside in the register.
- Register assignment: In the Register assignment, we pick the register that contains a variable.
Certain machines require even-odd pairs of registers for some operands and results.
Example
Consider the following division instruction of the form:
D x, y
Where,
x is the dividend even register in even/odd register pair
y is the divisor
An old register is used to hold the quotient.
6. Evaluation order
The code generator determines the order in which the instructions are executed. The target code's efficiency is influenced by order of computations. Many computational orders will only require a few registers to store interim results. However, choosing the best order is a completely challenging task in the general case.
Frequently Asked Questions
What are the different issues in the design of a code generator?
The code generator transforms the intermediate representation of source code into a machine-readable format. During the code generation step, a number of problems may occur: supplying data to the code generator, target program, memory management, Instruction choice, allocation of Register, and Evaluation sequence.
What is the role of code generator in compiler design?
A code generator is a compiler that converts the source program's intermediate representation into the destination program. In other words, a code generator converts an abstract syntax tree into executable code that is specific to a certain machine. It is expected that a code generator will produce accurate code.
What properties a good code generator should have?
While using a code generator successfully in your projects requires seven essential elements. Be sure you have a firm grasp of these principles. A good code generator should have the following properties: Code performance, Code quality, Open to custom code, Development tool integration, Generation speed, and Technology evolution support.
What is the potential problem with automatic code generation?
There are risks with automated code generators. It creates small alterations that could have big effects. As a result, a team will be required to manage, test, and document the metaprogramming tools, which include IDE and compile-time code generation tools and dynamic program tools that produce the actual code.
Conclusion
In this article, we have extensively discussed the concepts of design issues in designing a code generator and frequently asked questions.
After reading about the design issues of code generators, are you not feeling excited to read/explore more articles on the topic of compilers? Don't worry; Coding Ninjas has you covered.
Recommended Reading:
Also check out some of the Guided Paths on topics such as Competitive Programming, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.