CSC362 2004S, Class 35: Translating Loops Admin: * New project assigned. Relatively random group assignment. * Seniors: Participate in the senior challenge. * Sam's goal: Homework graded this weekend. (Not projects.) * Final Thursday (of finals week) at 9:00 a.m. * Puzzling convo yesterday * Essentialism: Things have pre-given meaning * Cool convo next week. * A final chance to embarrass Ananta next week. * Interesting idea for observing the ends of eboards tail -f 35.txt Overview: * An alternative mechanism for translating Booleans. * Translating case statements. * Translating while loops. * Translating repeat loops. * Translating for loops. Review: * Goal is to build a "code" attribute for each node in the parse tree * The code of the root is the code for the program * Like type checking, this requires recursive traversal of the tre * Monday: Translation of arithmetic expressions * Observation: In addition to a "code" attribute, you should also have a "location" attribute (where the result of the expression is stored) * Wednesday: Translation of Booleans and conditionals * Represent true as 1 and 0 as false * Idea: Translate Boolean expressions the same way we translate regular expressions * To translate if then _0 else _1 * Translate (giving code_e and loc_e), translate statements * Generate code_e JZ loc_e FALSE JMP TRUE TRUE: code_0 JMP ENDIF FALSE: code_1 JMP ENDIF ENDIF: * Yesterday: See it in action; obseve alternate translation of Booleans and conditionals * Idea: False is in location 0, True is in location 1 * Idea: When you translate a Boolean, you use two additional pieces of information: Where the code should continue if the Boolean is true and where the code should contine if the Boolean is false Explore that idea in pseudocode * Observation: * Have different destinations if the Boolean is being translated for a conditional * Also have them for short-circuit evaluation, while loops, repeat loops, etc. * Possible kinds of Booleans * TRUE * FALSE * ID * AND * OR * NOT * COMPARE translate(bool, truelabel, falselabel) Symbol sym = bool.getSymbol() if (sym == PascalTokens.TTRUE) return new JUMP(truelabel); else if (sym == PascalTokens.TFALSE) return new JUMP(falselabel); else if (sym instance Identifier) // Assume typechecked already return new InstructionSequence( JZ symbols.getLocation(sym) falselabel JUMP truelabel ); else if (sym == PascalTokens.TNOT) // Look! More efficient code return translate(bool.getChild(0), falselabel, truelabel); else if (sym == PascalTokens.TAND) // Warning! In the real parse tree, you get ANDs, but they are not represented this way // We need to translate the left Boolean expression // We need to translate the right Boolean expression // We need to return code based on those two return new InstructionSequence( translate(bool.getChild(0), SECOND_HALF_OF_AND, falselabel) new Label("SECOND_HALF_OF_AND") translate(bool.getChild(1), truelabel, falselabel) ) else if (sym == PascalTokens.TOR) // Warning! In the real parse tree, you get ORs, but they are not represented this way return new InstructionSequence( translate(bool.getChild(0), truelable, SECOND_HALF_OF_OR) new Label("SECOND_HALF_OF_OR") translate(bool.getChild(1), truelabel, falselabel) ) Saving with NOT if (not x) then A else B Old style SUB 1 x tmp0 JZ tmp0 CODE_FOR_B JUMP CODE_FOR_A New style JZ x CODE_FOR_A JUMP CODE_FOR_B Problem: What if we want a strict AND? return new InstructionSequence( translate(bool.getChild(0), SECOND_HALF_OF_AND_TRUE, SECOND_HALF_OF_AND_FALSE); new Label("SECOND_HALF_OF_AND_TRUE"); translate(bool.getChild(1), truelabel, falselabel); new Label("SECOND_HALF_OF_AND_FALSE"); translate(bool.getChild(1), falselabel, falselabel); )