Today’s topics Language Translation Generating Code Upcoming Electric Circuits (Hardware) (not in text) Reading Great Ideas, Chapters 9 CompSci 001 27.1 Adding Semantics Need to add semantic components to our rules For every syntax rule, we will add a semantic rule This will show the assembler code generated The code, as interpreted by the machine will provide the meaning Revise R1 Syntax Rule Semantic Rule R1: <n>j -> w M(<n>j) = w M ... Meaning of ... Name ... Memory location In other words, use same identifier/name in both Java and Assembler CompSci 001 27.2 Adding Semantics Revise R2 Syntax Rule R2: <e>i -> <n>j Semantic Rules M(<e>i) = M(<n>j) code(<e>i) = nothing No code is generated! Revise R3 Syntax Rule Semantic Rules R3: <s>k -> <n>j = <e>i; code(<s>k) = code(<e>i) COPY AX, M(<e>i) COPY M(<n>j), AX Says code for statement is code to calculate expression <e>i and code to copy it into memory associated with <n>j CompSci 001 27.3 Generating Code for X = Y; Now have enough to demonstrate simplest case Use syntactic production to control process Associated semantic rules are applied at each step Use rules to generate code for X = Y; Derivation Syntax Rule Semantic Rules s1 R3: s1 -> n2=e3; code(s1) = code(e3) COPY AX, M(e3) COPY M(n2), AX MEANING: code(s1) = code (e3) COPY AX, M(e3) COPY M(n2), AX n2 = e3; R1: n2 -> X M(n2) = X MEANING: code(s1) = code (e3) COPY AX, M(e3) COPY X, AX CompSci 001 27.4 Generating Code for X = Y; Derivation X = e3; Semantic Rules M(e3) = M(n4) code(e3) = nothing MEANING: code(s1) = nothing COPY AX, M(n4) COPY X, AX X = n4; R1: n4 -> Y M(n4) = Y MEANING: code(s1) = COPY AX, Y COPY X, AX X = Y; CompSci 001 Syntax Rule R2: e3 -> n4 27.5 More Rules Revise R4 Syntax Rule R4: <e>i -> (<e>j+<e>k) Semantic Rules M(<e>i) = createname code(<e>i)= code(<e>j) code(<e>k) COPY AX, M(<e>j) ADD AX, M(<e>k) COPY M(<e>i), AX Says code for <e>i is code to calculate expression <e>j followed by code to calculate expression <e>k and code to add them together and store that sum into memory associated with <e>i CompSci 001 27.6 More Rules Revise R5 Syntax Rule R5: <e>i -> (<e>j*<e>k) Semantic Rules M(<e>i) = createname code(<e>i)= code(<e>j) code(<e>k) COPY AX, M(<e>j) MUL AX, M(<e>k) COPY M(<e>i), AX Says code for <e>i is code to calculate expression <e>j followed by code to calculate expression <e>k and code to multiply them together and store that sum into memory associated with <e>i Basically, rules R4 and R5 are identical except that the + and ADD in one are replaced by the * and MUL in the other. CompSci 001 27.7 Code for Z = (X + Y); Derivation s1 Syntax Rule Semantic Rules R3: s1 -> n2=e3; code(s1) = code(e3) COPY AX, M(e3) COPY M(n2), AX MEANING: code(s1) = code (e3) COPY AX, M(e3) COPY M(n2), AX n2 = e3; R1: n2 -> Z M(n2) = Z MEANING: code(s1) = code (e3) COPY AX, M(e3) COPY Z, AX CompSci 001 27.8 Code for Z = (X + Y); .2 Derivation Z = e3; Syntax Rule Semantic Rules R4: e3 -> (e4+e5) M(e3) = CN1 code(e3) = code(e4) code(e5) COPY AX, M(e4) ADD AX, M(e5) COPY M(e3), AX MEANING: code(s1) = code(e4) code(e5) COPY AX, M(e4) ADD AX, M(e5) COPY CN1, AX COPY AX, CN1 COPY Z, AX CompSci 001 27.9 Code for Z = (X + Y); Z = (e4 + e5); .3 M(e4) = M(n6) code(e4) = nothing MEANING: code(s1) = nothing code(e5) COPY AX, M(n6) ADD AX, M(e5) COPY CN1, AX COPY AX, CN1 COPY Z, AX Z = (n6 + e5); R1: n6 -> X M(n6) = X MEANING: code(s1) = code(e5) COPY AX, X ADD AX, M(e5) COPY CN1, AX COPY AX, CN1 COPY Z, AX CompSci 001 R2: e4 -> n6 27.10 Code for Z = (X + Y); Z = (X + e5); .4 M(e5) = M(n7) code(e5) = nothing MEANING: code(s1) = nothing COPY AX, X ADD AX, M(n7) COPY CN1, AX COPY AX, CN1 COPY Z, AX Z = (X + n7); R1: n7 -> Y M(n7) = Y MEANING: code(s1) = COPY AX, X ADD AX, Y COPY CN1, AX COPY AX, CN1 COPY Z, AX Z = (X + Y); CompSci 001 R2: e5 -> n7 27.11 Towards a Real Program More complicated statement: U1 = (X + (Y * Z)); Done on pages 277-279 in text (Note that book uses <i>j where we used <n>j) Rules for Looping Sequence of statements Rules 6 and 7: A sequence of statements Syntax Rule Semantic Rules R6: <q>i -> <s>j code(<q>i) = code(<s>j) <q>k code(<q>k) R7: <q>i -> <s>j code(<q>i) = code(<s>j) Says code for a sequence of statements is the code for the first statement followed by the code for the next statement, etc. Notice the recursive nature of these statements. CompSci 001 27.12 More Complicated Statements Rule 8: Compound Statement Syntax Rule Semantic Rules R8: <c>i -> { <q>j code(<c>i) = code(<q>j) } Rule 9: While Statement Syntax Rule Semantic Rules R9: <s>i -> M(<s>i) = createname while (<n>j < <e>k) M'(<s>i) = createname <c>h code(<s>i) = M(<s>i) code(<e>k) COPY AX, M(<n>j) CMP AX, M(<e>k) JNB M'(<s>i) code(<c>h) JMP M(<s>i) M'(<s>i) NO-OP CompSci 001 27.13 Final Thoughts Clean Up Translation Some code generated can be removed Modern compilers spend a lot of effort optimizing Important: Everything done by simple substitution Everything “adds up” code( { <s>1;<s>2;<s>3 } ) is code(<s>1) code(<s>2) code(<s>3) CompSci 001 27.14