#ifndef _EMIT_H #define _EMIT_H /* emit.h * by CMB 10/04 * Some useful utility functions and class for code generation. * * Detailed documentation appears next to headers and class def. */ #include #include using namespace std; /* emit funcs * Overloaded function to format MIPS instructions * Takes 2, 3, or 4 strings * Puts the instruction all on one line, with a tab before and after * the operation, and ends the instruction with a newline. * */ void emit(ostream & o, const string &opcode, const string &operand); void emit(ostream & o, const string &opcode, const string &op1, const string &op2); void emit(ostream & o, const string &opcode, const string &op1, const string &op2, const string &op3); // emitLabelDef emits a label not indented, followed by ":" // on a line by itself void emitLabelDef(ostream & o, const string &label); // ind(offset,reg) generates the string: offset(reg) // for MIPS indexed addressing. // Normally you would then pass the result of ind to one of the emit functions string ind(int offset, const string ®); // imm(num) generates the string version of num // for MIPS immediate addressing // Normally you would then pass the result of ind to one of the emit functions // Note: since emit functions only take strings, this is an easy way // to pass an int to one of those functions. string imm(int num); /* NameGenerator * * Generates unique names with a given prefix. * Each a call to gen() returns the next unused name. * * E.g., if the prefix is "t", the names generated are "t0", "t1", "t2", etc. * For this example, the code is: * * NameGenerator tgen("t"); * * cout << tgen.gen(); // prints t0 * cout << tgen.gen(); // prints t1 * cout << tgen.gen(); // prints t2 * */ class NameGenerator { public: NameGenerator(const string &prefix); string gen(); private: string pre; int next; }; #endif