Code generation, Errors and Recovery in Compiler Model, Lecture notes of Compiler Construction

Code generation, Errors and Errors Recovery techniques in Compiler design

Typology: Lecture notes

2017/2018

Uploaded on 01/16/2018

momina.idrees
momina.idrees 🇵🇰

4.5

(2)

4 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Submitted by: Momina Idrees
Roll # 1430303
CODE GENERATION, ERRORS &
RECOVERY
Assignment (Finals) of Compiler Construction
pf3
pf4
pf5

Partial preview of the text

Download Code generation, Errors and Recovery in Compiler Model and more Lecture notes Compiler Construction in PDF only on Docsity!

Submitted by: Momina Idrees

Roll # 1430303

CODE GENERATION, ERRORS &

RECOVERY

Assignment (Finals) of Compiler Construction

CODE GENERATOR

The final phase of our compiler model is code generator. It takes input from the intermediate representation with supplementary information in symbol table of the source program and produces as output an equivalent target program.

Issues in the design of a code generator

  • Input to the code generator: The intermediate representation of the source program produced by the front end is input to code generator.
  • Memory management: Mapping names in the source program to addresses of data objects in run-time memory. A name in a three- address statement refers to a symbol table entry for the name. A relative address can be determined.
  • Target programs: ➢ Absolute machine language ➢ Relocatable machine language ➢ Assembly language
  • Selection of instruction : The code generator takes Intermediate Representation as input and converts (maps) it into target machine’s instruction set. One representation can have many

Issues a “Code Generator” tackles:

The Code generation module has to tackle a number of issues.

  • Memory management: Mapping names to data objects in the run-time system.
  • Instruction selection: The assembly language instructions to choose to encode intermediate code statements.
  • Instruction scheduling: Instruction chosen must utilize the CPU resources effectively. Hardware stalls must be avoided.
  • Register allocation: Operands are placed in registers before executing machine operation such as ADD, MULTIPLY etc. Most processors have a limited set of registers available. The code generator has to make efficient use of this limited resource.

Common Errors

Program submitted to a compiler often have errors of various kinds. So, good compiler should be able to detect as many errors as possible in various ways and also recover from them. Even in the presence of errors, the compiler should scan the program and try to compile all of it (error recovery). Errors may be encountered at various stages of the compilation process. A program may have the following kinds of errors at various stages:

  • Lexical : Name of some identifier typed incorrectly.
  • Syntactical : Missing semicolon or unbalanced parenthesis.
  • Semantical : Incompatible value assignment.
  • Logical : Code not reachable, infinite loop.

Error-recovery strategies

  • Panic mode When a parser encounters an error anywhere in the statement, it ignores the rest of the statement by not processing input from erroneous input to

delimiter, such as semi-colon. This is the easiest way of error-recovery and also, it prevents the parser from developing infinite loops.

  • Statement mode When a parser encounters an error, it tries to take corrective measures so that the rest of inputs of statement allow the parser to parse ahead. For example, inserting a missing semicolon, replacing comma with a semicolon etc. Parser designers have to be careful here because one wrong correction may lead to an infinite loop.
  • Error productions Some common errors are known to the compiler designers that may occur in the code. In addition, the designers can create augmented grammar to be used, as productions that generate erroneous constructs when these errors are encountered.
  • Global correction The parser considers the program in hand as a whole and tries to figure out what the program is intended to do and tries to find out a closest match for it, which is error-free. When an erroneous input (statement) X is fed, it creates a parse tree for some closest error-free statement Y. This may allow the parser to make minimal changes in the source code, but due to the complexity (time and space) of this strategy, it has not been implemented in practice yet.