C09 - C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E R S Copyright 1994 2010 Zhong Shao Yale University Intermediate Code Generation Page 1

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: C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E R S Copyright 1994 - 2010 Zhong Shao, Yale University Intermediate Code Generation: Page 1 of 25 Intermediate Code Generation • Translating the abstract syntax into the intermediate representation . • What should Intermediate Representation ( IR ) be like ? not too low-level (machine independent) but also not too high-level (so that we can do optimizations) • How to convert abstract syntax into IR ? lexer & parser Tiger source program absyn inter. code semantic analysis report all lexical & syntactic errors report all semantic errors inter. codegen correct absyn compiler backend C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E R S Copyright 1994 - 2010 Zhong Shao, Yale University Intermediate Code Generation: Page 2 of 25 Intermediate Representations (IR) • What makes a good IR ?--- easy to convert from the absyn; easy to convert into the machine code; must be clear and simple; must support various machine-independent optimizing transformations; • Some modern compilers use several IRs ( e.g., k=3 in SML/NJ ) --- each IR in later phase is a little closer (to the machine code) than the previous phase. Absyn ==> IR 1 ==> IR 2 ... ==> IR k ==> machine code pros : make the compiler cleaner, simpler, and easier to maintain cons : multiple passes of code-traversal --- compilation may be slow • The Tiger compiler uses one IR only --- the Intermediate Tree (itree) Absyn => itree frags => assembly => machine code How to design itree ? stay in the middle of absyn and assembly! C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E R S Copyright 1994 - 2010 Zhong Shao, Yale University Intermediate Code Generation: Page 3 of 25 Case Study : itree • Here is one example, defined using ML datatype definition: structure Tree : TREE = struct type label = string type size = int type temp = int datatype stm = SEQ of stm * stm | ...... and exp = BINOP of binop * exp * exp | ...... and test = TEST of relop * exp * exp and binop = FPLUS | FMINUS | FDIV | FMUL | PLUS | MINUS | MUL | DIV | AND | OR | LSHIFT | RSHIFT | ARSHIFT | XOR and relop = EQ | NE | LT | GT | LE | GE | ULT | ULE | UGT | UGE | FEQ | FNE | FLT | FLE | FGT | FGE and cvtop = CVTSU | CVTSS | CVTSF | CVTUU | CVTUS | CVTFS | CVTFF end C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E R S Copyright 1994 - 2010 Zhong Shao, Yale University Intermediate Code Generation: Page 4 of 25 itree Statements and Expressions • Here is the detail of itree statements stm and itree expressions exp datatype stm = SEQ of stm * stm | LABEL of label | JUMP of exp | CJUMP of test * label * label | MOVE of exp * exp | EXP of exp and exp = BINOP of binop * exp * exp | CVTOP of cvtop * exp * size * size | MEM of exp * size | TEMP of temp | ESEQ of stm * exp | NAME of label | CONST of int | CONSTF of real | CALL of exp * exp list C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E R S Copyright 1994 - 2010 Zhong Shao, Yale University Intermediate Code Generation: Page 5 of 25...
View Full Document

This document was uploaded on 02/20/2012.

Page1 / 7

C09 - C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E R S Copyright 1994 2010 Zhong Shao Yale University Intermediate Code Generation Page 1

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