CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 13– Compilers 2
Chapter 5 - Compilers Optimization (optional) Source program Intermediate code generator (semantic analyzer) Code generator Lexical analizer Syntax analizer Intermediate code Lexical units (Tokens) Machine language Parse trees Computer Symbol table
Code Generation Add S(id) to LIST and LISTCOUNT++
Code Generation (cont’d.)
Code Generation (cont’d.)
Code Generation (cont’d.) Save to Register A 1. <term>2 already in Register A, or 2. <factor> already in Register A, or 3. <term>2 and <factor>both are not in Register A
Code Generation (cont’d.) Node Specifier S(<term>1) save to rA
Code Generation (cont’d.)
Code Generation (cont’d.) 9 and 10 S(<exp>) := S(<term>) S(<exp>) <> rA =S(MEAN)
Code Generation (cont’d.) 10
Code Generation (cont’d.) 10 S(<exp>2) <> rA Call GETA(<exp>2) Generate [SUB S(MEAN)=T2] S(<exp>1) := rA REGA := <exp>1
Code Generation (cont’d.) 11 S(<term>) := S(SUMSQ) S(<term>) <> rA S(<term>) := S(MEAN)
Code Generation (cont’d.) 11 S(<term>2) <> rA S(<factor>) <> rA Call GETA(MEAN) Generate [MUL MEAN] S(MEAN) := rA REGA:= MEAN
Code Generation (cont’d.) 11 S(<term>2) <> rA Call GETA(SUMSQ) Generate [DIV #100] S(<term>1) := rA REGA := <term>1
Code Generation (cont’d.) 12 S(<factor>) := S(SUMSQ) S(<factor>) := S(MEAN) S(<factor>) := S(#100)
Code Generation (cont’d.) REGA <> NULL = SUMSQ S(MEAN) <> rA = S(SUMSQ) Generate [STA T1] S(SUMSQ) = T1 Generate [LDA MEAN] S(MEAN) := rA REGA := MEAN REGA = NULL Generate [LDA SUMSQ] S(SUMSQ) := rA REGA := SUMSQ REGA <> NULL = MEAN S(SUMSQ) <> rA = S(MEAN) Generate [STA T2] S(MEAN) = T2 Generate [LDA S(SUMSQ)=T1] S(SUMSQ) := rA REGA := SUMSQ
Code Generation (cont’d.)
Code Generation (cont’d.) 1, 2, and 3
Code Generation (cont’d.) 4, 5, and 7
8, 14, 15
Code Generation (cont’d.) 16 and 17
Code Generation (cont’d.)
Code Generation (cont’d.)