Here are bits and pieces of the operator processing: case OpK: // process lhs codegen_expression(currnode->child[0]); // process rhs if binary operator if (currnode->child[1]) { emitRM("ST", AC, toff--, FP, "Save left side"); codegen_expression(currnode->child[1]); emitRM("LD", AC1, ++toff, FP, "Load left into ac1"); } // do operator switch (currnode->attr.op) { case '+': emitRO("ADD", AC, AC1, AC, "Op +"); break; ---------------------------------------------------------------------- Here is doing a while-statement: case WhileK: emitComment("WHILE"); currloc = emitSkip(0); // return to here to do the test codegen_expression(currnode->child[0]); emitRM("LDC", AC1, 1, 6, "Load constant 1"); emitRO("SUB", AC, AC, AC1, "While cond check"); emitRM("JGE", AC, 1, PC, "Jump to while part"); emitComment("DO"); skiploc = emitSkip(1); // addr of instr that jumps to end of loop skiploc2 = breakloc; // save for break statement breakloc = skiploc; codegen_next(currnode->child[1]); breakloc = skiploc2; emitRMAbs("LDA", PC, currloc, "go to beginning of loop"); currloc = emitSkip(0); emitBackup(skiploc); emitRMAbs("LDA", PC, currloc, "No more loop"); // backpatch jump to end of loop emitBackup(currloc); emitComment("ENDWHILE"); break; ---------------------------------------------------------------------- Here is the return statement: case ReturnK: emitComment("RETURN"); if (currnode->child[0]) { codegen_expression(currnode->child[0]); } emitRM("LDA", RT, 0, AC, "Copy result to rt register"); emitRM("LD", AC, RETURN_OFFSET, FP, "Load return address"); emitRM("LD", FP, OFPOFF, FP, "Adjust fp"); emitRM("LDA", PC, 0, AC, "Return"); break; ---------------------------------------------------------------------- int backPatchJumpToHere(int addr, char *comment) { int currloc; currloc = emitSkip(0); emitBackup(addr); emitRMAbs("LDA", PC, currloc, "Jump around the THEN"); emitBackup(currloc); } ---------------------------------------------------------------------- void codegen(FILE *codeFile, // where the code should be written char *srcFile, // name of file compiled TreeNode *syntaxTree, // the internal representation of the program SymTab *globalsIn, // globals so function info can be found int globalOffset) // size of the global frame { int init_jump; code = codeFile; globals = globalsIn; breakloc = 0; init_jump = emitSkip(1); // save a place for the jump to init codegen_header(srcFile); // nice comments describing what is compiled codegen_general(syntaxTree); // general code generation including I/O library codegen_init(init_jump, globalOffset); // generation of initialization for run }