📄 tac.h
字号:
/* File: tac.h
* -----------
* The Tac class (and its various subclasses) define very simple
* objects used for storing Tac instructions. Each is mostly just
* little struct with a few fields, but each responds polymorphically
* to the methods Print and Emit, the first is used to print
* out the TAC form of the instruction (helpful when debugging)
* and the second to convert to the appropriate MIPS assembly. You
* may need to make changes/extensions to these classes depending on
* how you do optimization.
*/
#ifndef _H_tac
#define _H_tac
#include "decllist.h"
class Declaration;
class Mips;
class Ex86asm;
class Tac {
public:
typedef enum {ToNothing,ToX86,ToMips}TacToWhat;
static TacToWhat toWhat;
typedef enum {LoadConstant, LoadStringConstant, LoadLabel, Assign,
Load, Store, Add, Sub, Mul, Div, Mod, Eq, Less, And, Or,
Label, Goto, IfZ, BeginFunc, EndFunc, Return,
PushParam, ACall, LCall, VTable, GVar,NumOps } OpCode;
OpCode code;
char printed[128];
Tac(OpCode code);
OpCode GetOpCode() { return code; }
virtual void Print();
virtual void EmitSpecific(Mips *mips) = 0;
virtual void Emit(Mips *mips);
virtual void Emit(Ex86asm * x86asm);
virtual void EmitSpecific(Ex86asm * x86asm)=0;
static OpCode OpCodeForName(const char *name);
static const char *NameForOpCode(OpCode code);
static void InitOpNames();
static const char *opName[NumOps];
};
class LoadConstant : public Tac {
Declaration *dst;
int val;
public:
LoadConstant(Declaration *dst, int val);
void EmitSpecific(Mips *mips);
void EmitSpecific(Ex86asm * x86asm);
};
class LoadStringConstant : public Tac {
Declaration *dst;
char *str;
public:
LoadStringConstant(Declaration *dst, const char *s);
void EmitSpecific(Mips *mips);
void EmitSpecific(Ex86asm * x86asm);
};
class LoadLabel : public Tac {
Declaration *dst;
const char *label;
public:
LoadLabel(Declaration *dst, const char *label);
void EmitSpecific(Mips *mips);
void EmitSpecific(Ex86asm * x86asm);
};
class Assign : public Tac {
Declaration *dst, *src;
public:
Assign(Declaration *dst, Declaration *src);
void EmitSpecific(Mips *mips);
void EmitSpecific(Ex86asm * x86asm);
};
class Load : public Tac {
Declaration *dst, *src;
int offset;
public:
Load(Declaration *dst, Declaration *src, int offset = 0);
void EmitSpecific(Mips *mips);
void EmitSpecific(Ex86asm * x86asm);
};
class Store : public Tac {
Declaration *dst, *src;
int offset;
public:
Store(Declaration *d, Declaration *s, int offset = 0);
void EmitSpecific(Mips *mips);
void EmitSpecific(Ex86asm * x86asm);
};
class BinaryOp : public Tac {
Declaration *dst, *op1, *op2;
public:
BinaryOp(OpCode c, Declaration *dst, Declaration *op1, Declaration *op2);
void EmitSpecific(Mips *mips);
void EmitSpecific(Ex86asm * x86asm);
};
class Label : public Tac {
const char *label;
public:
bool isFunLabel;
Label(const char *label);
void Print();
void EmitSpecific(Mips *mips);
void EmitSpecific(Ex86asm * x86asm);
};
class Goto : public Tac {
const char *label;
public:
Goto(const char *label);
void EmitSpecific(Mips *mips);
void EmitSpecific(Ex86asm * x86asm);
};
class IfZ : public Tac {
Declaration *test;
const char *label;
public:
IfZ(Declaration *test, const char *label);
void EmitSpecific(Mips *mips);
void EmitSpecific(Ex86asm * x86asm);
};
class BeginFunc : public Tac {
Declaration *fn;
public:
BeginFunc(Declaration *fn);
void EmitSpecific(Mips *mips);
void EmitSpecific(Ex86asm * x86asm);
};
class EndFunc : public Tac {
Declaration *fn;
public:
EndFunc(Declaration *fn);
void EmitSpecific(Mips *mips);
void EmitSpecific(Ex86asm * x86asm);
};
class Return : public Tac {
Declaration *val;
public:
Return(Declaration *val);
void EmitSpecific(Mips *mips);
void EmitSpecific(Ex86asm * x86asm);
};
class PushParam : public Tac {
Declaration *param;
public:
PushParam(Declaration *param);
void EmitSpecific(Mips *mips);
void EmitSpecific(Ex86asm * x86asm);
};
class LCall : public Tac {
const char *label;
Declaration *dst;
int numBytesOfParameters;
public:
LCall(const char *label, int numBytesOfParameters, Declaration *result);
void EmitSpecific(Mips *mips);
void EmitSpecific(Ex86asm * x86asm);
};
class ACall : public Tac {
Declaration *dst, *methodAddr;
int numBytesOfParameters;
public:
ACall(Declaration *meth, int numBytesOfParameters, Declaration *result);
void EmitSpecific(Mips *mips);
void EmitSpecific(Ex86asm * x86asm);
};
class VTable : public Tac {
DeclList *methods;
const char *label;
public:
VTable(const char *label, DeclList *methods);
void Print();
void EmitSpecific(Mips *mips);
void EmitSpecific(Ex86asm * x86asm);
};
class GVar :public Tac
{
Declaration * decl;
public:
GVar(Declaration * d):Tac(Tac::GVar)
{
decl =d;
printed[0]='\0';
}
void Print(){};
void EmitSpecific(Mips *mips){};
void EmitSpecific(Ex86asm * x86asm);
};
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -