03-07storage - 1 CSE 450: Compilers K. Stirewalt Code...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 CSE 450: Compilers K. Stirewalt Code Generation Part 3 Topics: More on translating array types Data storage/layout Readings: Sections 7.1 - 7.3 of the Dragon Book CSE 450: Compilers K. Stirewalt Array type expressions Specified using new type constructor: array : Integer x Type Type For instance, the declaration: int a[30]; is of type array( 30, integer ) float f [ 20] [ 30 ] is of type array( 20, array( 30, float ) ) CSE 450: Compilers K. Stirewalt Translating array references Chief problem in generating array references is to relate the address-calculation formulas to a grammar for array references Assume grammar is: L L [E] | id [E] Then non-terminal L has 3 synthesized attributes: L.addr is a temporary used to compute the offset L.array is a pointer to symbol table entry for array L.array.base holds actual l-value of array ref after all index expressions are analyzed L.array.type holds the type of the array L.type is of the subarray generated by L CSE 450: Compilers K. Stirewalt Semantics of array references S id = E ; S L = E ; E id E L { S.code = E.code || gen( top.get(id.lexeme), = E.addr ) } { S.code = E.code || L.code || gen( L.array.base [ L.addr ] = E.addr ) } { E.addr = top.get( id.lexeme ) E.code = } { E.addr = newTemp() E.code = L.code || gen( E.addr = L.array.base [ L.addr ] ) } CSE 450: Compilers K. Stirewalt Sems of array refs (continued) L id [E] L L 1 [E] { L.array = top.get(id.lexeme) L.type = L.array.type.elem L.addr = newTemp() L.code = E.code || gen( L.addr = E.addr * L.type.width ) } { L.array = L 1 .array L.type = L 1 .type.elem t = newTemp() L.addr = newTemp() L.code = L 1 .code || E.code || gen(t = E.addr * L.type.width) || gen(L.addr = L 1 .addr + t) } CSE 450: Compilers K. Stirewalt Exercise Given the declarations: int i, j, k, n, a [20] [30] ; Translate the array reference: n = a [i+j] [k] ; into 3-address intermediate code 2 CSE 450: Compilers K. Stirewalt Symbol table for this exercise Symbol table contents: [ a ( , base ... , type array (20, array(30, integer )) ), i ( , type integer ), j ( , type integer ), ] CSE 450: Compilers K. Stirewalt For brevity in the example... ... symbol-table entry for lexemes i, j, k, and n are abbreviated by the addresses i, j, k, n ... base address of array a is a [ a ( , base a , type array (20, array(30, integer )) ), i i , j j , ] CSE 450: Compilers K. Stirewalt Syntax tree ( n = a[i+j][k]; ) [ ] a + [ ] i j k n = CSE 450: Compilers K. Stirewalt Syntax tree ( k = a[i+1][3]; ) [ ] a + [ ] i j k n = First, we will compute attributes for this subtree CSE 450: Compilers K. Stirewalt Syntax tree ( a[i+j] ) [ ] a + i j addr array code addr addr type addr code code code CSE 450: Compilers K. StirewaltK....
View Full Document

This note was uploaded on 07/25/2008 for the course CSE 450 taught by Professor Stirewalt during the Spring '08 term at Michigan State University.

Page1 / 9

03-07storage - 1 CSE 450: Compilers K. Stirewalt Code...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online