@Override public void emitCode(GluonOutput code) { code.comment("Function: " + funcName.getValue()); String funcLabel = GluonFunction.getLabel(funcName.getValue()); // Setup variables passed in on stack for use int offset = 4; // + 4 * parameters.size(); int used = 4; for (Variable parameter : parameters) { int stackOffset = offset + used; used += 4; code.code(parameter.getLabel() + " EQU EBP + " + stackOffset); } // Function code.label(funcLabel); // Push BP code.code("PUSH EBP"); // Mov BP, SP code.code("MOV EBP, ESP"); // Local variables scope.emitCreateScope(code); // Push all reg code.code("PUSH EAX"); code.code("PUSH EBX"); code.code("PUSH ECX"); code.code("PUSH EDX"); // Actual function logic code.comment("Function code start"); logic.emitCode(code); code.comment("Function code end"); // Pop all reg code.code("POP EDX"); code.code("POP ECX"); code.code("POP EBX"); code.code("POP EAX"); // Local variables scope.emitDistroyScope(code); // Pop BP code.code("POP EBP"); // Ret paramaters code.code("RET"); scope.buildRestore(code, parameters, true); code.comment("Function end: " + funcName.getValue()); }
@Override public void emitCode(GluonOutput code) { // Creae labels String labelStart = GluonLabels.createLabel(first, "start"); String labelEnd = GluonLabels.createLabel(first, "end"); GluonLabels.addEndLabel(labelEnd); code.comment("While Statement"); code.label(labelStart); testExp.emitCode(code); code.code("TEST AL, AL ;EAX, EAX"); code.code("JZ " + labelEnd); statements.emitCode(code); code.code("JMP " + labelStart); code.label(labelEnd); code.comment("End While"); GluonLabels.removeEndLabel(labelEnd); }