CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis - - PowerPoint PPT Presentation

cse443 compilers
SMART_READER_LITE
LIVE PREVIEW

CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis - - PowerPoint PPT Presentation

CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall http:/ /www.cse.buffalo.edu/faculty/alphonce/SP17 /CSE443/index.php https:/ /piazza.com/class/iybn4ndqa1s3ei WW @ 4/5 handling of type checking in: 'assignable


slide-1
SLIDE 1

CSE443 Compilers

  • Dr. Carl Alphonce

alphonce@buffalo.edu 343 Davis Hall

http:/ /www.cse.buffalo.edu/faculty/alphonce/SP17 /CSE443/index.php https:/ /piazza.com/class/iybn4ndqa1s3ei

slide-2
SLIDE 2

WW @ 4/5

handling of type checking in: 'assignable ablock' 'assignable recOp identifier' handling of records/arrays recap project 3 intermediate code expectations for pr04

slide-3
SLIDE 3

Assignment status

PR04 - due M 4/17 @5 HW4 - out W 4/5 @5, due T 4/11 @5

slide-4
SLIDE 4

Phases of a compiler

Figure 1.6, page 5 of text

Intermediate Representation (IR): specification and generation

slide-5
SLIDE 5

Three address code instructions (see 6.2.1, pages 364-5)

1. x = y op z 2. x = op y (treat i2r and r2i as unary ops) 3. x = y

  • 4. goto L

5. if x goto L / ifFalse x goto L 6. if x relop y goto L 7. function calls:

  • param x
  • call p, n
  • y = call p
  • return y

8. x = y[i] and x[i] = y 9. x = &y, x = *y, *x = y

slide-6
SLIDE 6

Our language

primitive types: integer, real, Boolean, character built-in type: string (array of character) user-defined types: record types have names type rec : (real x := -4.79, y := 1.3) array types have names type arr : (5,10) -> string : ( "" ) function types have names type bar : ( real : x ) -> rec function f : bar { … }

slide-7
SLIDE 7

Our language

primitive types: integer, real, Boolean, character built-in type: string (array of character) user-defined types: record types have names type rec : (real x := -4.79, y := 1.3) array types have names type arr : (5,10) -> string : ( "" ) function types have names type bar : ( real : x ) -> rec function f : bar { … }

Record field initialization now done with declaration Record field initialization now done with declaration

slide-8
SLIDE 8

Our language

primitive types: integer, real, Boolean, character built-in type: string (array of character) user-defined types: record types have names type rec : (real x := -4.79, y := 1.3) array types have names type arr : (5,10) -> string : ( "" ) function types have names type bar : ( real : x ) -> rec function f : bar { … }

We now have allow multi- dimensional arrays

slide-9
SLIDE 9

Our language

primitive types: integer, real, Boolean, character built-in type: string (array of character) user-defined types: record types have names type rec : (real x := -4.79, y := 1.3) array types have names type arr : (5,10) -> string : ( "" ) function types have names type bar : ( real : x ) -> rec function f : bar { … }

Function types must now be named before use Function types must now be named before use

slide-10
SLIDE 10

Recusive records Recursive functions

A record type must allow a component to be

  • f the same type as the type itself:

type Node: ( integer datum:=0 ; Node rest:=null )

slide-11
SLIDE 11

type information

type indicates size type indicates storage location primitives: in current environment records: on heap arrays: on heap functions: code in static, locals on stack need to determine how to lay out records, arrays, invocation records in memory

slide-12
SLIDE 12

Sizes of types

int: 32 bits (2's complement) real: 64 bits (IEEE 754) Boolean: 8 bits (TBD) character: 8 bit (ASCII)

slide-13
SLIDE 13

Sizes of types

type string: ? -> character 5 bytes + length of string * size of character (= 1 byte)

# dims size of dimension 1 (integer) (0) (1) (2) (3) (5) 1 5 V A X E S

https:/ / en.wikipedia.org/wiki/VAX

slide-14
SLIDE 14

Sizes of types

What is the size of a multi- dimensional array of type T? # of dimensions (X): 1 byte sizes of dimensions (Si): X*4 bytes data: (∏i∈X Si) * sizeOf(T)

2 # dims size of first dimension 2 size of second dimension 3 a(0,0) first row a(0,1) a(0,2) a(1,0) second row a(1,1) a(1,2)

slide-15
SLIDE 15

Figure 6.16, p 375

T B C C 𝜁 C int [2] [3]

w = 4 int w = 4 array(3,int) w = 12 array(2,array(3,int)) w = 24 array(2,array(3,int)) w = 24

slide-16
SLIDE 16

Figure 6.16, p 375

T B C C 𝜁 C

character w = 1

2 3

w = 6 w = 3 9+array(2,arr(3,character)) w = 9 + 6 * 1 = 15 w = 2 * 3

slide-17
SLIDE 17

dblocks (6.3.5 and 6.3.6)

records (in separate symbol table), sequence of declarations at start of sblock

definition —> type identifier ':' dblock { st.put(identifier.lexeme, TYPE, dblock.type, dblock.width } dblock —> '(' { Env.push(st); st = new Env(); Stack.push(offset); offset = 0; } declaration-list ')' { dblock.type=record(st); dblock.width=offset; st=Env.pop(); offset=Stack.pop(); } declaration-list —> declaration ';' declaration-list declaration-list —> declaration declaration —> identifier ':' { id-list.type = identifier; } <— however you store types identifier-list (maybe pointer into st?) identifier-list —> identifier ( sBinOp constant ) ',' { st.put(identifier.lexeme, VAR, identifier-list.type, offset);

  • ffset = offset + identifier-list.type.width; }

identifier-list identifier-list —> identifier ( sBinOp constant ) { st.put(identifier.lexeme, VAR, identifier-list.type, offset);

  • ffset = offset + identifier-list.type.width; }

Just suggestions, not to be taken literally Just suggestions, not to be taken literally