Today in CS362: Translating Conditionals Admin How are your parsers going? [It would help me if you told me what's taking so long.] Symbol table coming soon Stop me if I move too fast Overview Translating array references Translating conditionals; Basics + Kinds of Boolean expressions + Strict and short-circuit evaluation Translating if's with strict Booleans Translating if's with short-circuit evaluation Array references: ID0 := ID1[] Simple exapmles: x: array[0..10] of integer; x[5] is at "base of x" + 5 * sizeof(integer) y: array[1..10] of real y[5] is at "base of y" + 4 * sizeof(real) ID[num] is at "base of ID" + (num-firstindex) * sizeof(cell) Moral: Need to store firstindex and cellsize Observation: You might want to error-check, so array references probably need error checking code for lower-bound and upper-bound if then if then else case () of val1: val2: ... end; Detour: Kinds of Boolean expressions Arithmetic comparisons: Constant: true, false Function calls coolFunctionThatReturnsBoolean(xxxxx) Compound Booleans and or not Boolean variables ID Note: Need to represent Boolean as a value in the underlying architecture. Traditionally, use integers. 0 is false. 1 (or any nonzero number) is true. if (test1 or test2) then If the first test holds, do we evaluate the second test? Two options: * Strict: Yes, you evaluate all "arguments" to or + If there are side-effects in test2, they won't be executed if you short-circuit * Short-Circuit: No, you stop as soon as you know the answer + Seems more efficient if (p != null) && (p->foo == 3) Translation of strict is pretty easy. + Translation of " and " is the translation of " * " + Translation of or is almost the trnalsation of " + " + Need to make sure that both are 0-1 + Translation of "not " the translation of "1 - " translation of "if then else " code for JUMP_IF_ZERO .result-location FALSE_PART JUMP TRUE_PART TRUE_PART: code for JUMP END_IF FALSE_PART: code for JUMP END_IF END_IF Harder problem: Short-circuit evaluation if then else Translation technique: When translating a boolean expression, pass along the label for the true part and the label for the false part translate(" or ", truelabel, falselabel) (0) aaron = newlabel(); (1) code1 = translate("", truelabel, aaron) (2) code2 = translate("", truelable, falselabel) (3) return code1 + aaron + code2; return concatCode(code1, concatCode(new Label(aaron), code2)); translate("true", truelabel, falselabel) (0) return JUMP truelabel translate(ID, truelabel, falselabel) (0) return JUMP_IF_ZERO ID.memloc falselabel JUMP truelabel translate("not ", truelabel, falselabel) return translate("", falselabel, truelabel) translate(" and ", truelabel, falselabel) (0) aaron = newlabel(); (1) code1 = translate("", aaron, falselabel) (2) code2 = translate("", truelable, falselabel) (3) return code1 + aaron + code2; translate("if then else ") label1 = newlabel(); label2 = newlabel(); label3 = newlabel(); return translate(, label1, label2) + label1 + translate() + "JUMP label3" + label2 + translate() + "JUMP label3" + label3 case () of : ; : ; : ; : ; end case translate as if ( = ) then else if ( = ) then ... Whoops! could have side effects tmp = if (tmp = ) then else if (tmp = ) then ... Suppose every value from 1 .. 10 were listed and that's it. jump-dest = { LABEL1,LABEL2.... LABEL10} JUMP jump-dest[value] LABEL1: translate() JUMP ESAC LABEL2: translate() JUMP ESAC ESAC: Compromise solution when there are selected values entered: Build a binary search tree