Intermediate language, array and subprograms Lecture 11 Formal - - PowerPoint PPT Presentation

intermediate language array and
SMART_READER_LITE
LIVE PREVIEW

Intermediate language, array and subprograms Lecture 11 Formal - - PowerPoint PPT Presentation

1 Intermediate language, array and subprograms Lecture 11 Formal Languages and Compilers 2011 Nataliia Bielova 2 Compiler for crme CAraMeL Compiler syntax IC tree tokens Source Parser IC generator C Lexer C generator code


slide-1
SLIDE 1

Intermediate language, array and subprograms

Lecture 11 Formal Languages and Compilers 2011 Nataliia Bielova

1

slide-2
SLIDE 2

Compiler for crème CAraMeL

Formal languages and compilers 2011

2

Source code

Lexer Parser

tokens

IC generator syntax tree IC C generator C

Front-end Compiler Back-end

slide-3
SLIDE 3

Intermediate language

ADD val1 val2 dest

  • sum

CPY src NULL dest

  • copy

CGE val1 val2 dest

  • copy greater or equal

GOTO label NULL NULL

  • unconditional jump

JNE val1 val2 label - conditional jump OUT val NULL NUL

  • print

AGET addr idx dest

  • read array

ASET addr idx src

  • write array

PARAM val NULL NULL

  • add a parameter on the stack

CALL id NULL NULL

  • call a procedure

CALL id NULL dest

  • call a function

Formal languages and compilers 2011

3

slide-4
SLIDE 4

Implementation details

 Memory cells: union of int and float  Two different vectors: stack and “registers”  Allocation of variables: assignment of offset in the stack  Allocation of temporal values: assignment of a new register

Formal languages and compilers 2011

4

slide-5
SLIDE 5

Example

Formal languages and compilers 2011

5

CPY Val INT: 1 NULL

  • ffset 0

CPY Val INT: 5 NULL

  • ffset 2

CPY Val INT: 1 NULL

  • ffset 1

Label2: CGE

  • ffset 2
  • ffset 1

reg[1].i JNE reg[1].i Val INT: 1 Label nr. 1 OUT

  • ffset 1

NULL NULL MUL

  • ffset 0
  • ffset 1

reg[2].i CPY reg[2].i NULL

  • ffset 0

ADD

  • ffset 1

Val INT: 1 reg[3].i CPY reg[3].i NULL

  • ffset 1

NOP NULL NULL NULL GOTO Label nr. 2 NULL NULL Label1: OUT

  • ffset 0

NULL NULL NOP NULL NULL NULL HALT NULL NULL NULL

slide-6
SLIDE 6

Intermediate.ml

 Define the instructions of intermediate code and all types of

  • perands:

 inst_type: ADD, MUL, CPY,…  label, offset for variables, register for temporal values

 class intermediateCode  dec_table - declaration table binds ide with (int, int, element)

Formal languages and compilers 2011

6

slide-7
SLIDE 7

Exercise

 Note: we do not allow arithmetical operations of mixed types  But so far write(…) command can contain any expression: write(2 + 5.2) which should not be allowed  How to fix it?

Formal languages and compilers 2011

7

slide-8
SLIDE 8

Vectors and matrices in crème CAraMeL: compilation

Formal languages and compilers 2011

8

var var m : array array[5] of int

  • f int;

var var v : array array[3,2] of int

  • f int

... for for i := 0 to to 2 do begin do begin for for j := 0 to to 1 do begin do begin v[i,j] := i + j end end end end

slide-9
SLIDE 9

Vectors and matrices in the compiler

 Declaration in style of C: var var v : array array[4,2] of int

  • f int

 Access like before: v[2,1] := 45;  No V.O. (or better, V.O.= α)  Simplifies the multiplies

Formal languages and compilers 2011

9

slide-10
SLIDE 10

Vectors and matrices in the compiler

 Declaration: add dimensions to the declaration table  Semantic control: v[i,j] is OK  i and j are integers and within the bounds  Evaluate expression: calculate the position + AGET  Assignment: calculate the position + ASET

Formal languages and compilers 2011

10

slide-11
SLIDE 11

Subprograms in crème CAraMeL: compilation

Formal languages and compilers 2011

11

program program var var x : int int function function fact(a: int): int int): int var var b : int int begin begin if if (a = 0) then then fact := 1 else begin else begin b := call call fact(a - 1); fact := a * b end end end end begin begin x := call call fact(12); write write(x) end end

479001600

slide-12
SLIDE 12

Subprograms in the compiler

 Syntax: the same as in the interpreter  Table of subprograms  Managing stack pointer and base pointer  Call: push on the stack (param) + call  Using one register for the return of the functions

Formal languages and compilers 2011

12

slide-13
SLIDE 13

Subprograms in the compiler

 Declaration: Building and Subroutine (return type of the functions)  Generation of the code: subroutines.ml  Parameters and local variables: stack!  Call: commands.ml and expressions.ml

Formal languages and compilers 2011

13