CS-536 Class Notes April 29, 1998
Charles Fisher Discussion Covers: Code Generation of If-Then-Else statements, nested If's and the Translation of operators, complex Statements, Unary Ops and Type Casting. Misc: Handed out Part III of Code Generation.
If Statements: IF STMT //Evaluate Boolean Expression To Do
GOTO L2 //Exit Point, Executed the THEN and is leaving block. L1: {Code for to execute when the IF Stmt true} L2: Code Generation of IF-THEN The Code: CG () { condition.cg(); // Generates the code to determine if true or false and puts it on Stack String elseLab = genLab (); // This never gets used in CSX Lite, becuase no else stmts. if (! elsePart.isNull()) { // Do not need to branch if there is no else part. branchZ (elseLab);} ThenPart.cg(); // Generates code to do in the then part. String endLab = genLab(); // Gets a new label for the end of IF-THEN branch (endLab); if (! elsePart.isNull()) { // Do not need to generate code if there is no else part. elsePart.cg();} // Generate code for else part. defineLab(endLab); } Question: What happens with if-then without an else? answer: The else part is Null so its acceptable to ask a Null Node to generate Nothing Question: Do nested if
statments work?
Solution: Walk through
the Algorithm to show it works of code Generation.
//This is the code generated and it does handle the nested if's. Question: Is there a problem here? answer: A small one. We have Jump Chaining which is a jump to a jump. Some compilers will optimize this, but it is not required. Another issue of translating operators
Relational Opeators IF_ICMPEQ, IF_ICMPNE, IF_ICMPLT, For example: if(a<b) { //This is handled by code for genRelationalOp on page 5 of Part II z=0;} else {} This is a specialized if statment. So what we need to do to anaylize we need to push the values of 'a' and 'b' on to the stack. Then we can use: IF_ICMPLT True // This takes the top two values on the stack and branchs if Less than. LDC 0 // If it is not true it is False so put 0 on Stack. GOTO Skip True: LDC 1 // The expression is true so put 1 on stack. Skip: // This code generated by genRelationalOp() on Page 5 of Part II // Then we need to add on the if part since we have the value we want is on stack IFEQ Else LDC 0 putStatic c/z$ I Else: // That part takes care of the if Question: What happens in context
of if-then with complex statments
Question: Will pop ever
get used?
Type Casting Is just part of the if-Then code again. What we will do is consider the only types we can type cast are Bool, Int, Char and look at the 9 possiblites we have to account for. Type of Operand
Code for (Bool) Int and
(Bool) Char
Code fir (Char) Int
Unary Operator (not) You do not want to use a compliment for Not you want to use xor. A true boolean = 000000..001 and a false = 00000..000 so the compliment would be wrong because it would make true = 11111..110 which is not 0. So what you wnat to do is us XOR with 000...001. push bool //push the bool onto the stack ldc 1 //pushes the mask on stack XOR //will leave the real value on stack.
David Toal |
|||||||||||||||||