03-06icodeGeneration

03-06icodeGeneration - Project # 4 Code Generation Part 2...

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

View Full Document Right Arrow Icon
1 CSE 450: Compilers K. Stirewalt Code Generation Part 2 Topics: – Quick intro to project 4 – Example implementation of an SDD using syntax trees – Array types – Data storage/layout Readings: Sections 7.1 - 7.3 of the Dragon Book CSE 450: Compilers K. Stirewalt Project # 4 Aims to build an intermediate code generator to support incremental generation of 3-address instructions Goals: – Provide a platform for implementing the ‘gen’ commands we’ve been using in SDDs for expression translation – Store 3-address instructions efficiently Mostly independent of the design of your existing compiler CSE 450: Compilers K. Stirewalt Recall: SDT for incremental generation of assignments S id = E ; E E1 + E2 | id { gen ( top.get(id.lexeme) ‘=‘ E.addr ); } { E.addr = new Temp(); gen (E.addr ‘=‘ E 1 .addr ‘+’ E 2 .addr); } { E.addr = top.get(id.lexeme) } CSE 450: Compilers K. Stirewalt Suggested design class ICodeGenerator { public: ICodeGenerator& genBinary( BINARY_OP, Address*, Address*, Address* ); ICodeGenerator& genLabel( LABEL ); ICodeGenerator& genIfGoto( Address*, RELATIONAL_OP, Address*, LABEL ); }; CSE 450: Compilers K. Stirewalt Suggested design class ICodeGenerator { public: genBinary( BINARY_OP, Address*, Address*, Address* ); genLabel( LABEL ); genIfGoto( Address*, RELATIONAL_OP, Address*, LABEL ); }; Design facilitates “chaining” of calls to generate various instructions CSE 450: Compilers K. Stirewalt Example use int main(void) { ICodeGenerator cgen; Symbol* x = …; Symbol* y = …; Temp* t1 = …; cgen.genBinary( OP_PLUS, x, y, t1) .genIfGoto( x, OP_GREATER, y, L0 ) …; }
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 CSE 450: Compilers K. Stirewalt Example use int main(void) { ICodeGenerator cgen; Symbol* x = …; Symbol* y = …; Temp* t1 = …; cgen.genBinary( OP_PLUS, x, y, t1) .genIfGoto( x, OP_GREATER, y, L0 ) …; } Notice: The chaining of calls to generate instructions CSE 450: Compilers K. Stirewalt Representing 3-address instructions 3-address instructions represented using a number of different structures – Quadruples – Triples – Indirect triples Among these, quadruples are the easiest to use in later phases CSE 450: Compilers K. Stirewalt Quadruple data structure Essentially a 4-tuple: – First component is an opcode – Second through fourth components are either addresses, goto labels, or are unused Intermediate code generated for a program is thus a sequence of quads Individual quads (actually indices into the sequence) may be labeled CSE 450: Compilers K. Stirewalt Example quadruple rep t1 = - c t2 = b * t1 t3 = - c t4 = b * t3 t5 = t2 + t4 a = t5 minus c t1 * b t1 t2 minus c t3 * b t3 t4 + t2 t4 t5 = t2 a CSE 450: Compilers K. Stirewalt Exercise (from last time) Translate the C code: if ( x > 0 || z == 7 ) x = 0; else y = 0; into 3-address form CSE 450: Compilers K. Stirewalt General method Implement the SDD by traversing the syntax tree representation of the program
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the 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 / 10

03-06icodeGeneration - Project # 4 Code Generation Part 2...

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