200 likes | 300 Views
Compiler Model Front-End − language dependant part Back-End − machine dependant part. 9.1 Introduction [1/3]. Introduction [2/3]. IL 의 필요성 Modular Construction Automatic Construction Easy Translation Portability Optimization Bootstrapping IL 의 분류
E N D
Intermediate Language Compiler Model Front-End− language dependant part Back-End− machine dependant part 9.1 Introduction [1/3]
Intermediate Language Introduction [2/3] • IL의 필요성 • Modular Construction • Automatic Construction • Easy Translation • Portability • Optimization • Bootstrapping • IL의 분류 • Polish Notation --- Postfix, IR • Three Address Code --- Quadruple, Triple, Indirect triple • Tree Structured Code --- PT, AST, TCOL • Abstract Machine Code --- P-code, EM-code, U-code,Byte-code
Intermediate Language Two level Code Generation ILS 소스로부터 자동화에 의해 얻을 수 있는 형태 소스 언어에 의존적이며 high level이다. ILT 후단부의 자동화에 의해 목적기계로의 번역이 매우 쉬운 형태 목적기계에 의존적이며 low level이다. ILS to ILT ILS에서 ILT로의 번역이 주된 작업임. Introduction [3/3]
Intermediate Language 9.2 Polish Notation [1/2] • Polish mathematician Lucasiewiezinvented the parenthesis-free notation. • Postfix(Suffix) Polish Notation • earliest IL • popular for interpreted language - SNOBOL, BASIC • general form : e1 e2 ... ekOP (k ≥ 1) where, OP : k_ary operator ei : any postfix expression (1 ≤ i ≤ k)
Intermediate Language example : if a then if c-d then a+c else a*c else a+b 〓〉a L1 BZ c d - L2 BZ a c + L3 BR L2: a c * L3 BR L1: a b + L3: note 1) high level: source to IL - fast & easy translation IL to target - difficulty 2) easy evaluation - operand stack 3) optimization 부적당- 다른 IL로의 translation 필요 4) parentheses free notation - arithmetic expression interpretive language에 적합 Polish Notation [2/2]
Intermediate Language 9.3 Three Address Code [1/3] • most popular IL, optimizing compiler • General form: where, A : result address B, C : operand addresses op : operator (1) Quadruple - 4-tuple notation <operator>,<operand1>,<operand2>,<result> (2) Triple - 3-tuple notation <operator>,<operand1>,<operand2> (3) Indirect triple - execution order table & triples A := B op C
Intermediate Language example a = b + c * d / e; f = c * d; Three Address Code [2/3]
Intermediate Language Three Address Code [3/3] • Note • Quadruple vs. Triple • quadruple - optimization 용이 • triple - removal of temporary addresses ⇒ Indirect Triple • extensive code optimization 용이 • IL rearrange 가능 (triple 제외) • easy translation - source to IL • difficult to generate good code • quadruple to two-address machine • triple to three-address machine
Intermediate Language Abstract Syntax Tree parse tree에서 redundant한 information 제거. Leaf node -- variable name, constant Internal node -- operator [예제 9.8] { x = 0; y = z + 2 * y; while ((x<n) && (v[x] != z)) x = x+1; return x; } 9.4 Tree Structured Code [1/4]
Intermediate Language Tree Structured Code [2/4] • Tree Structured Common Language(TCOL) • Variants of AST - containing the result of semantic analysis. • TCOL operator - type & context specific operator • Context ┌ value --- rhs of assignment statement ├ location --- lhs of assignment statement ├ boolean --- conditional control statement └ statement --- statement ex) . : operand - location result - value while : operand- boolean, statement result - statement
Intermediate Language Tree Structured Code [3/4] Example)int a; float b; ... b = a + 1; • Representation ---- graph orientation internal notation ----- efficient external notation ---- debug, interface linear graph notation
Intermediate Language Pascal-P Code [1/2] • Pascal P Compiler --- portable compiler producing P_CODE for an abstract machine(P_Machine). • P_Machine ----- hypothetical stack machine designed for Pascal language. (1) Instruction --- closely related to the PASCAL language. (2) Registers PC --- program counter NP --- new pointer SP --- stack pointer MP --- mark pointer (3) Memory CODE --- instruction part STORE --- data part(constant area, stack, heap)
Intermediate Language Pascal-P Code [2/2]
Intermediate Language Ucode [1/8] • Ucode the intermediate form used by the Stanford Portable Pascal compiler. stack-based and is defined in terms of a hypothetical stack machine. Ucode Interpreter : Appendix B. • Addressing stack addressing ===> a tuple : (B, O) B : the block number containing the address O : the offset in words from the beginning of the block, offsets start at 1. label to label any Ucode instruction with a label field. All targets of jumps and procedures must be labeled. All labels must be unique for the entire program.
Intermediate Language Ucode [2/8] • Example : • Consider the following skeleton : int x; void main() { int i; int j; // ..... } • block number - 전역변수 : 1 - 함수 내 지역변수 : 2 • variable addressing - x : (1,1) - i : (2,1) - j : (2,2)
Intermediate Language Ucode [3/8] • Ucode Operations(39개) • Unary --- notop, neg, inc, dec, dup • Binary --- add, sub, mult, div, mod, swp and, or, gt, lt, ge, le, eq, ne • Stack Operations --- lod, str, ldc, lda • Control Flow --- ujp, tjp, fjp • Range Checking --- chkh, chkl • Indirect Addressing --- ldi (load indirect), sti (store indirect) • Procedure --- cal, ret, retv, ldp, proc, end • Etc. --- nop, bgn, sym
Intermediate Language Ucode [4/8] • Example : • x = a + b * c; lod 1 1 /* a */ lod 1 2 /* b */ lod 1 3 /* c */ mult add str 1 4 /* x */ • if (a>b) a = a + b; lod 1 1 /* a */ lod 1 2 /* b */ gt fjp next lod 1 1 /* a */ lod 1 2 /* b */ add str 1 1 /* a */ next ...
Intermediate Language Ucode [5/8] • Indirect Addressing • is used to access the array elements. • ldi --- indirect load • replace stacktop by the value of the item at location stacktop. • to retrieve A[i] : lod i // actually (Bi, Oi)) lda A // also (block number, offset) add // effective address ldi // indirect load gets contents of A[i]
Intermediate Language Ucode [6/8] • sti --- indirect store • sti stores stacktop into the address at stack[stacktop-1], both items are popped. • A[i] = j; lod i lda A add lod j Sti
Intermediate Language Ucode [7/8] • Procedure Calling Sequence • function definition : • void func(int x, int array[]) { } • function call : • func(a, list); • calling sequence : ldp // load parameter lod a // load the value of actual parameter lda list // load the address of actual parameter call func // call func