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 Announcements Weekly team meetings with me: - Doodle poll link in Piazza Wednesday (4/ 4) will be a workshop Wednesday - Post questions you'd like addressed in Piazza


slide-1
SLIDE 1

CSE443 Compilers

  • Dr. Carl Alphonce

alphonce@buffalo.edu 343 Davis Hall

slide-2
SLIDE 2

Weekly team meetings with me:

  • Doodle poll link in Piazza

Wednesday (4/ 4) will be a workshop Wednesday

  • Post questions you'd like addressed

in Piazza by Sunday (4/1) evening

Announcements

slide-3
SLIDE 3

Phases of a compiler

Figure 1.6, page 5 of text

Intermediate Representation (IR): specification and generation

slide-4
SLIDE 4

Intermediate Representations

slide-5
SLIDE 5

Sizes of types

type string: 1 -> character 8 bytes + length of string * size

  • f character (= 1 byte)

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

https:/ / en.wikipedia.org/wiki/VAX Q: Since the number of dimensions is part of the type, do we need to store it at runtime? A: No.

slide-6
SLIDE 6

Sizes of types

type string: 1 -> character 4 bytes + length of string * size

  • f character (= 1 byte)

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

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

slide-7
SLIDE 7

Sizes of types

What is the size of a multi- dimensional array of type T? sizes of dimensions (Si): X*4 bytes data: (āˆi∈X Si) * sizeOf(T)

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-8
SLIDE 8

Figure 6.16, p 375

T B C C šœ C int [2] [3]

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

For the purposes of type checking the number of dimensions is relevant, but the size of each dimension is not. Q: if a and b are compatible array types, what are the semantics of a := b ?

slide-9
SLIDE 9

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

What if type info comes after dimensions?

slide-10
SLIDE 10

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

slide-11
SLIDE 11

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

We can specialize due to the structure of our grammar: see next slide!

slide-12
SLIDE 12

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(); }

{ ( integer : x , y ) { ( real : x , z ) … … } { ( Boolean : y ; character : z ) … … } }

dblocks (6.3.5 and 6.3.6)

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

integer: x integer: y

Since declarations must be gathered together at the start of an sblock, and cannot themselves be directly nested, we can do better:

  • ffset = 0
  • ffset = 4
  • ffset = 8
  • ffset = 8

push offset = 8 onto stack

  • ffset = 16
  • ffset = 24

pop offset = 8 from stack push offset = 8 onto stack

  • ffset = 9
  • ffset = 10

pop offset = 8 from stack integer: x integer: y real: x real: z integer: x integer: y

Boolean: y character: z
  • ffset = 8
slide-13
SLIDE 13

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(); }

{ ( integer : x , y ) { ( real : x , z ) … … } { ( Boolean : y ; character : z ) … … } }

dblocks (6.3.5 and 6.3.6)

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

integer: x integer: y

Since declarations must be gathered together at the start of an sblock, and cannot themselves be directly nested, we can do better:

  • ffset = 0
  • ffset = 4
  • ffset = 8
  • ffset = 8

push offset = 8 onto stack

  • ffset = 16
  • ffset = 24

pop offset = 8 from stack push offset = 8 onto stack

  • ffset = 9
  • ffset = 10

pop offset = 8 from stack

AT RUNTIME

  • ffset = 8
slide-14
SLIDE 14

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(); }

{ ( integer : x , y ) { ( real : x , z ) … … } { ( Boolean : y ; character : z ) … … } }

dblocks (6.3.5 and 6.3.6)

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

integer: x integer: y

Since declarations must be gathered together at the start of an sblock, and cannot themselves be directly nested, we can do better:

  • ffset = 0
  • ffset = 4
  • ffset = 8
  • ffset = 8

push offset = 8 onto stack

  • ffset = 16
  • ffset = 24

pop offset = 8 from stack push offset = 8 onto stack

  • ffset = 9
  • ffset = 10

pop offset = 8 from stack integer: x integer: y real: x real: z

AT RUNTIME

  • ffset = 8
slide-15
SLIDE 15

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(); }

{ ( integer : x , y ) { ( real : x , z ) … … } { ( Boolean : y ; character : z ) … … } }

dblocks (6.3.5 and 6.3.6)

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

integer: x integer: y

Since declarations must be gathered together at the start of an sblock, and cannot themselves be directly nested, we can do better:

  • ffset = 0
  • ffset = 4
  • ffset = 8
  • ffset = 8

push offset = 8 onto stack

  • ffset = 16
  • ffset = 24

pop offset = 8 from stack push offset = 8 onto stack

  • ffset = 9
  • ffset = 10

pop offset = 8 from stack

AT RUNTIME

  • ffset = 8
slide-16
SLIDE 16

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(); }

{ ( integer : x , y ) { ( real : x , z ) … … } { ( Boolean : y ; character : z ) … … } }

dblocks (6.3.5 and 6.3.6)

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

integer: x integer: y

Since declarations must be gathered together at the start of an sblock, and cannot themselves be directly nested, we can do better:

  • ffset = 0
  • ffset = 4
  • ffset = 8
  • ffset = 8

push offset = 8 onto stack

  • ffset = 16
  • ffset = 24

pop offset = 8 from stack push offset = 8 onto stack

  • ffset = 9
  • ffset = 10

pop offset = 8 from stack integer: x integer: y

Boolean: y character: z

AT RUNTIME

  • ffset = 8
slide-17
SLIDE 17

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(); }

{ ( integer : x , y ) { ( real : x , z ) … … } { ( Boolean : y ; character : z ) … … } }

dblocks (6.3.5 and 6.3.6)

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

integer: x integer: y

Since declarations must be gathered together at the start of an sblock, and cannot themselves be directly nested, we can do better:

  • ffset = 0
  • ffset = 4
  • ffset = 8
  • ffset = 8

push offset = 8 onto stack

  • ffset = 16
  • ffset = 24

pop offset = 8 from stack push offset = 8 onto stack

  • ffset = 9
  • ffset = 10

pop offset = 8 from stack

AT RUNTIME

  • ffset = 8