CS 3240: Languages and Computation - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

CS 3240: Languages and Computation

Description:

Compilers typically use 'intermediate code' and 'assembly code' ... Problem: When generating codes, the destination being jumped to may have not been generated yet ... – PowerPoint PPT presentation

Number of Views:47
Avg rating:3.0/5.0
Slides: 21
Provided by: ble87
Category:

less

Transcript and Presenter's Notes

Title: CS 3240: Languages and Computation


1
CS 3240 Languages and Computation
  • Basic Code Generation Techniques

2
Overview of Code Generation
  • Main task of back-end of compiler
  • Most complex phase of compilation
  • Interact with source language, target
    architecture, runtime environment, operating
    system, etc.
  • Involve optimization of code to improve
    performance
  • This course focuses on front-end, so we will give
    only brief overview of code generation

3
Intermediate Code
  • Compilers typically use intermediate code and
    assembly code
  • Intermediate code represents linearization of
    syntax tree
  • It contains more information than target code, so
    is particularly useful for code optimization
  • Also useful for code optimization of different
    source languages and different architectures
  • Common-types three-address code and P-code

4
Memory Organization
  • Code memory
  • Different procedures are stored separately
  • All addresses known at compile (link) time
  • Data memory
  • Global and static variables
  • Stack variables for local variables
  • Heap variables for dynamically allocated arrays
  • Note In stack machine, all data are on stack

5
Three-Address Code
  • Motivated by arithmetic operations X y op z
  • Its syntax tree are typically binary. Internal
    nodes may require temporary variables
  • E.g., c 2a(b-3) can be translated into
  • t1 2 a
  • t2 b 3
  • c t1 t2
  • Temporary variables can be in register or memory
  • Exception unary operations require only two

6
Implementation of 3-Address Code
  • Implement using records with several fields
  • How many fields do we need?
  • Answer Four (quadruples).
  • Sequence of instructions can be implemented using
    linked list or arrays

7
P-Code
  • Designed for P-machine (a stack machine) for
    Pascal compilers
  • P-machine has
  • Code memory
  • Unspecified data memory for named variables
  • Stack for temporary data
  • Registers for program counter and stack pointer
  • Operands are always at top of stack, so no
    address is needed (but constant may be needed as
    operand)
  • Similar to our Stack Machine in the project

8
Examples of P-Code
  • Note actually for its variance of Stack Machine
  • Arithmetic operations x y - 1
  • push 1
  • push ltaddr of ygt
  • load
  • subi
  • push ltaddr of xgt
  • store
  • Its implementation can also use linked lists of
    records as 3-address code

9
Comp. of 3-Addr Code and P-Code
  • P-code requires one or zero address
  • 3-Address code is more compact
  • Major advantage of P-code is that there is no
    need to name temporary variables

10
Basic Code-Gen Techniques
  • Code is essentially synthesized attribute
  • Example
  • exp1 id exp2 exp1.code exp2.code
  • load ltidgt store
  • exp aexp exp.code aexp.code
  • axep1 aexp2 fac aexp1.code
    fac.code aexp2.code addi
  • fac num fac.code push ltnumgt
  • fac id fac.code push ltidgt load

11
Address Calculations
  • Global variables addresses can be calculated
    statically and stored in symbol table
  • Address of components of array and struct require
    arithmetic operations to add base and offset
  • E.g., to access asi
  • push ltaddr of asgt
  • push ltigt
  • addi
  • load
  • To access pointers, use pointer dereference
  • E.g., to access p
  • push ltpgt
  • load
  • load
  • Addresses of local variables require special care

12
Code Gen for If Statements
  • Code generation for if statements
  • code for if test
  • jnz ltoffset to start_of_if_codegt
  • code for else part
  • jmp ltoffset to end_of_ifgt
  • code for if part
  • code after if

code before if
code for if test
T
conditional jump
F
code for else part
uncond. jump
code for if part
code after if
13
Code Gen for While Statements
  • code for while test
  • jnz 2
  • jmp ltoffset to end of while loopgt
  • code for the while loop
  • jmp ltoffset to start of while conditiongt
  • code after while

code before while
code for while test
F
conditional jump
T
body of while
uncond. jump
code after while
14
Code Gen for Logical Expression
  • Logical expressions require short-circuit
    evaluation
  • a and b is equivalent to if a then b else
    false
  • push a
  • load
  • jnz 3
  • push 0
  • jmp 3
  • push b
  • load
  • code after
  • a or b is equivalent to if a then true else b

15
Code Generation for Jumps
  • Problem When generating codes, the destination
    being jumped to may have not been generated yet
  • Solution Using multiple passes
  • First pass generates statements with blanks for
    jumps
  • Second pass fills in the addresses

16
Function Call Record
  • Stack frame
  • Space for arguments
  • Space for bookkeeping
  • E.g., save register contents
  • Space for local variables
  • Space for local temporaries
  • Frame pointer (FP) and/or argument pointer (AP)
  • In stack machine, we dont have register for
    FP/AP, usebottom of stack instead

AP
Example stack frame for stack machine
17
Call and Return Sequences
  • Register contents need be saved/changed
  • Stack frame need to be created
  • Arguments needs to be filled in
  • How to divide the calling sequence between caller
    and callee?

18
Callers Task
  • reserve space for return value
  • spi 1
  • store return address
  • push ltaddress of statement after function callgt
  • save frame pointer
  • push 0
  • load
  • load arguments
  • push ltarg 1gt
  • load
  • ...
  • invoke function
  • push ltaddress of functiongt
  • rpc
  • statement after function call
  • Bookkeeping for registers
  • Return address
  • Frame pointer
  • Compute arguments
  • Invoke function

19
Callees Tasks
  • compute new frame pointer
  • push ltsize of parametersgt
  • psp
  • subi
  • push 0
  • store
  • reserve space for local variables
  • spi ltsize of local variablesgt
  • body of function
  • with the return value on top of the stack, we
    do
  • push 3
  • push 0
  • load
  • subi
  • store
  • pop local variables and parameters values
  • push 0
  • load
  • lsp
  • Update frame pointer
  • Allocate space for local variables
  • Within function body, save return value
  • Recover registers (e.g., frame pointer and
    program counter)

20
Notes on Code Optimization
  • Code optimization is active subject of research
    in compiler community
  • Example optimization techniques
  • register allocation, common subexpression
    elimination, dead code elimination, jump
    optimization, constant folding, reduction in
    strength, loop unrolling, inlining, etc
  • Covered in more advanced compiler courses (CS4240
    CS6240)
  • For the project, code optimization is not required
Write a Comment
User Comments (0)
About PowerShow.com