Today in CS362: Translating Expressions Admin * Project, phase 3 assigned * When will I get phase 2? * Des Moines Art Museum * Note: Process vs. Product Overview * Translating Assignments * Translating Expressions ::= ID := What translation do we have to do? What code do we generate? Option A: Assume that each expression has two attributes: (1) The code to evaluate the expression (2) The location in which the result is stored code for ID := concat(.code "COPY FROM .memloc to lookupMemLoc(ID)") Potential problem: How much memory should we copy? It's type dependent. May be more complex for records and arrays. Leave that as a problem for later, but you can simply think of each memory location as the base and copy the appropriate number of bytes. Option B: When translating an expression, provide that expression with a desired result location. This technique can potentially save you a copy. How do we translate expression? Version A: CodePlusMemLoc translateExpression(ParseTree exp) Version B: Code translateExpression(ParseTree exp, MemLoc desiredLoc) * identifier Using technique A: Code is: NOTHING Memory location is: lookupMemLoc(ID) Using technique B: Code is: COPY lookupMemLoc(ID) to desiredLocation * integer-constant Using technique B: Code is: STORE constant(int) into desiredLocation Using technique A: + When we translate constants, we can give them memory locations [SILLY] Pick a new memory location Code is STORE constant(int) into that new memory location Memloc is that new memory location * + Using technique A: memloc = newTemp(); code = concat(.code, .code, "ADD .memloc .memloc memloc") Using technique B: temp0 = newTemp() temp1 = newTemp() concat( translate(,temp0) translate(,temp0) "ADD temp0 temp1 desiredLoc") Comment: Why not just use "the result register"? (3 + 4) + (5 + 6) Comment; Why not just use a hybrid? Technique D: CodeAndMemLoc translateExp(ParseTre Exp, MemLoc desiredLoc) if desiredLoc is non-null, translateExp must put the result there if desiredLoc is null, translateExp must use some other location translateD( + , dest) { if (dest == null) dest = newTemp(); (memloc0,code0) = translateExp(, null); (memloc1,code1) = translateExp(, null); code = concat(code0, code1, "ADD memloc0 memloc1 dest") } translateD(ID, dest) { if (dest == null) return new CodeAndMemLoc(null, lookupMemLoc(ID)); else return new CodeAndmemLoc( "COPY lookupMemLoc(ID) dest" dest); } translateD(constant, dest) { if (dest == null) { Pick a new memory location Code is STORE constant(int) into that new memory location Memloc is that new memory location } else return new CodeAndMemLoc( "STORE constant(constnat) into dest" dest); } translate(id[x])