Notes for April 28, 1999 (lecture)
Project 5 Due Date changed
Due date: Friday May 7th
Last accepted day: Monday May 10th
No class next Monday
No office hours next Tuesday
For proper spelling capitalization and non-psudocode please refer to the handout entitled CSX Code Generation Routines (part I, page 6)
DeclField() {
call toString(varName.idVal) + "$"; //$ added to prevent clash with system variables, etc
DelGlobalInt(varLabel, initvalue);
varname.idInfo.Label = varLabel;
varname.idInfo.adr = global;
}
sometimes variable delarations need calculation and subsequent assignment as follows:
Float Pi=3.14;
Float Pi2=Pi/2.0; //here you generate the assignment after the field is declared
cg () {
if (!(initval.isnul()) &&
!(initval Instance of IntLitNode {
initvalue.cg();
storeId(varname);
};
}
Refer to Part II of the same packet
already declared members of class CSXLib:
int readInt();
char readChar();
void printInt(int);
void printBool(bool);
void printChar(char);
void printString(string);
void printCharArray(char[]);
cg () { //readNode
computeAdr(targetVar);
if (targetvar.varName.idInfo.type == Types.Integer)
genCall("CSXLib/readInt() I");
else genCall("CSXLib/readChar() C");
storeName(targetVar);
moreReads.cg();
};
top of page 3
cg () { //printNode
outputValue.cg();
if ((outputValue.kind.val == Kinds.Array) || (outputValue.kind.val == Kinds.RefArray))
genCall("CSXLib/printCharArray([C) V");
else switch (outputValue.type.val) {
case Types.Integer:
genCall("CSXLib/printInt(I) V");
break;
.
.
.
}
}
Control Structures
while (expr) {
stmts // which can contain/execute other loops/blocks
}
now how can we translate this?
we need to set up unique labels followed by numbers
L1:
{Generate code for conditional expression}
IFEQ L2 //if the top of the stack=0 (false) goto L2
{Generate code for body of loop}
GOTO L1
L2:
A break means ==> L2
continue means ==> L1
top of page 4
cg () { //whileLoop
String Top = genLab(); //generates a unique label name
String Bottom = genLab();
if (! Label.isNull()) {
Label.idInfo.topLabel = Top;
Label.idInfo.bottomLabel = Bottom
};
defineLab(top); //puts label in file
Condition.cg();
BranchZ(Bottom)
loopBody.cg();
Branch(Top);
defineLab(Bottom)
}
test it. you will see that it works for nested loops too!
cg () { //breakNode
Branch(Label.idInfo.bottomLabel)
};
Compliments, large checks drawn on US accounts, please contact Matt Pagel
(death threats, letters of unfettered devotion from males not encouraged)