{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

c09 - CS421 COMPILERS AND INTERPRETERS CS421 Intermediate...

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full Document Right Arrow Icon
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 itree Expressions itree expressions stand for the computation of some value, possiblly with side-effects: CONST( i ) the integer constant i CONSTF( x ) the real constant x NAME( n ) the symbolic constant n (i.e., the assembly lang. label) TEMP( t ) content of temporary t ; like registers (unlimited number) BINOP( o , e
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.

{[ snackBarMessage ]}