#include "emit.h" const int NDIGITS = 10; // for an array long enough to hold a number string // for sprintf #include string ind(int offset, const string ®) { static char offsetStr[10]; sprintf(offsetStr, "%d", offset); return string(offsetStr) + "(" + reg + ")"; } string imm(int num) { static char numString[NDIGITS]; sprintf(numString, "%d", num); return string(numString); } void emit(ostream & o, const string &opcode, const string &operand) { o << "\t" << opcode << "\t" << operand << endl; } void emit(ostream & o, const string &opcode, const string &op1, const string &op2) { o << "\t" << opcode << "\t" << op1 << " " << op2 << endl; } void emit(ostream & o, const string &opcode, const string &op1, const string &op2, const string &op3) { o << "\t" << opcode << "\t" << op1 << " " << op2 << " " << op3 << endl; } void emitLabelDef(ostream & o, const string &label) { o << label << ":" << endl; } NameGenerator::NameGenerator(const string &prefix) : pre(prefix), next(0) { } string NameGenerator::gen() { // update next available number next++; static char numString[NDIGITS]; // convert number to a string sprintf(numString, "%d", next-1); return pre + numString; }