// Integer arithmetic compiler value (avalue) // declaration. // // File: cavalue.h // Author: course // Version: 2 // // This file declares the arithmetic compiler value // type, or avalue. // // This version of the file declares avalues to be // register numbers that are allocated and deallocated // by operations on avalues, and provides that these // operation have the side effect of outputing assembly // language code to evaluate the operation. // // Other versions of this file and its companion // avalue.cc file may be written without changing the // rest of the compiler program. #ifndef _AVALUE_H_ #define _AVALUE_H_ #include "basic.h" // Coursewide basic declarations. // Lexeme_class are declared later, in lexeme.h, but // used here. A lexeme is a pointer to a lexeme_class: // class lexeme_class; typedef lexeme_class * lexeme; const int MAX_TEMPORARY_REGS = 8; const int MAX_PERMANENT_REGS = 8; class avalue { public: // Registers are temporaries $t0, ... $t7, zero $0, // and permanent registers $s0, ..., $s7. // enum type { TEMPORARY, ZERO, PERMANENT }; type reg_type; int reg_number; // For temporary and permanent registers these // arrays are true if register in use and false // otherwise. // static bool temporary_reg_usage [MAX_TEMPORARY_REGS]; static bool permanent_reg_usage [MAX_PERMANENT_REGS]; // Default Initialization. Must set to zero // register. // avalue () { reg_type = ZERO; reg_number = 0; } }; // Output: s << x where s is typically cout. // ostream& operator << (ostream& s, avalue x); // Input: s >> x where s is a istream. // istream& operator >> (istream& s, avalue& x); // `Zero' avalue, which becomes value of empty program. // Here this is the zero register. // extern avalue zero_avalue; // Internal function to perform arithmetic operations. // avalue avalue_operate (char * opcode, avalue src1, avalue src2); // Arithmetic functions: // Arithmetic negation: - x // inline avalue operator - (avalue x) { return avalue_operate ("sub", zero_avalue, x); } // Arithmetic addition: x + y // inline avalue operator + (avalue x, avalue y) { return avalue_operate ("add", x, y); } // Arithmetic subtraction: x - y // inline avalue operator - (avalue x, avalue y) { return avalue_operate ("sub", x, y); } // Arithmetic multiplication: x * y // inline avalue operator * (avalue x, avalue y) { return avalue_operate ("mul", x, y); } // Arithmetic division: x / y // inline avalue operator / (avalue x, avalue y) { return avalue_operate ("div", x, y); } // Evaluate a lexeme to produce a value. Lexeme must // be number or symbol. // avalue evaluate (lexeme l); // Assign to a lexeme. Returns the value assigned. // avalue assign (lexeme l, avalue v); // Initialize avalues. // void init_avalues (void); #endif /* _AVALUE_H_ */