Docsity

Compiler Construction: Introduction and Intermediate Code Generation, Slides of Compilers

Partial preview of the text

Download Compiler Construction: Introduction and Intermediate Code Generation and more Slides Compilers in PDF only on Docsity!

CSC434 - Compiler Construction Department of Mathematics and Computer Science, SLU 2020/

A Compiler

Is a program which translates the source form of a program (written in HLL) into a semantically equivalent target form (MLL).

  • (^) Traditionally, this was machine code, but nowadays the target form may be a virtual machine (e.g. JVM) or indeed another language such as C.
  • (^) An important part of a compiler is ‘error showing’ to the programmer
  • (^) Can appear a very hard program to write
  • (^) How can one even start?
  • (^) It’s just like juggling too many balls (picking instructions while determining whether this ‘+’ is part of ‘++’ or whether its right operand is just a variable or an expression... )

Compilation sub-processes

A multi-pass compiler does one ‘simple’ thins at once and passes its output to the next stage

Overview

lex (lexical analysis) Converts a stream of characters into a stream of tokens syn (syntax analysis) Also called parsing. Converts a stream of tokens into a parse tree – a.k.a. (abstract) syntax tree trans (translation/linearization) Or intermediate code generation, converts a tree into simple (linear) intermediate code cg (target code generation) Translates intermediate code into target machine program. At this stage, optimization is done to reduce length of machine language program.

In other words

  • (^) Intermediate representation (IR)
  • (^) Front end maps legal code into IR
  • (^) Back end maps IR to target machine
  • (^) Simplify retargeting
  • (^) Allows multiple front ends
  • (^) Multiple passes better code

Intermediate code generation

INTERMEDIATE LANGUAGES Three approaches to intermediate representation:

  • (^) Syntax tree
  • (^) Postfix notation
  • (^) Three address code Semantic rules for three-address code from common programming language construct are similar to those for constructing syntax trees or postfix notation

Graphical representation

Syntax tree: Depicts the natural hierarchical structure of a source program. A dag (Directed Acyclic Graph) gives similar information in a more compact way because common subexpressions are identified. Example: The following is a syntax tree and dag for assignment statement a := b * -c + b -c*

Three-Address Code: Is a sequence of statements of a general form x := y op z

  • (^) Where x, y, and z are names, constants, or compiler-generated temporaries
  • (^) op stands for any operator, e.g. arithmetic operator, or a logical operator on boolean valued data Example : source language x+y*z may be translated into a sequence t 1 := y * z t 2 := x +t 1 Where t 1 and t 2 are compiler generated temporary names

Advantages of three-address code

  • (^) The unraveling of complicated arithmetic expressions and of nested flow- of-control statements makes three-address code desirable for target code generation and optimization
  • (^) The use of names for the intermediate values computed by a program allows three-address code to be easily rearranged unlike postfix notation
  • (^) Variable names can appear directly in three-address statements

“three-address code” reason: because each statement usually contains three addresses, two for the operands and one for the result Types of three-address statements:

  • (^) Assignment statements of the form x := y op z , where op is a binary arithmetic or logical operation
  • Assignment instructions of the form x := op y, where op is unary operation
  • (^) Copy statements of the form x := y where the value of y is assigned to x
  • (^) The unconditional jump goto L. The three-address is the next to be executed
  • (^) Conditional jumps such as if x relop y goto L. This instruction applies a relational operator (<,=,>=, etc.) to x and y, and executes the statement with label L
  • (^) Param x and call p , n for the procedure calls and return y , where y representing a returned value is optional. E.g.
  • (^) Indexed assignments of the form x := y[i] and x[i] :=y
  • (^) Address and pointer assignments of the form x:= &y, x:=*y, and *x:=y

Here, each node is represented as a record with a field for its operator and additional fields for pointers to its children

Syntax-directed translation into three-address code: When three-address code is generated, temporary names are made up for the interior nodes of a syntax tree. E.g. id:=E consists of code to evaluate E into temporary t , then assignment id .place := t