📄 tacprintsupport.cpp
字号:
#ifndef __TACPRINTSUPPORT_CPP__
#define __TACPRINTSUPPORT_CPP__
#include "TACsupport.cpp"
#include "IDHash.cpp"
#include "TACConst.cpp"
#include <fstream>
using namespace std;
int __inlist(const list <int>& lst, int key);
void __chk_print(ofstream& fout, const list <int>& arglist, list <int>& tmplist, const TOperand& oprd);
void TACPrintNamedVariable(ofstream& fout, TSymbolListVariableEntry& Ventry);
void TACPrintVariable(ofstream& fout, int id);
void TACPrintOPName(ofstream& fout, int op);
void TACPrintOperand(ofstream& fout, const TOperand& oprd);
void TACPrintStmt(ofstream& fout, int id);
void TACPrintOut(char* filename);
string __vartype2str(int var_type);
void TACPrintSymTable(char* filename, char* source_filename);
int __inlist(const list <int>& lst, int key)
{
for (list <int> :: const_iterator p = lst.begin(); p != lst.end(); p++)
{
if (*p == key)
return 1;
}
return 0;
}
string type2str(int var_type)
{
if (var_type == var_type_int || var_type == var_type_char ||
SymbolListType[var_type].type_number == type_number_pointer ||
SymbolListType[var_type].type_number == type_number_function)
{
return string("u16");
}
else
{
char buf[10];
sprintf(buf, "w%d", SymbolListType[var_type].total_size);
return string(buf);
}
}
void TACPrintNamedVariable(ofstream& fout, TSymbolListVariableEntry& Ventry)
{
fout << "\t" << type2str(Ventry.var_type) << "\t";
fout << "v" << Ventry.var_name_TAC << endl;
}
void __chk_print(ofstream& fout, const list <int>& arglist, list <int>& tmplist, const TOperand& oprd)
{
if (oprd.oper_type != TAC_oper_type_variable) return;
if (oprd.data.id < 0) return;
if (TempVar_TAC_Allocate[oprd.data.id].global_sign) return;
if (__inlist(arglist, oprd.data.id)) return;
if (__inlist(tmplist, oprd.data.id)) return;
tmplist.push_back(oprd.data.id);
fout << "\t" << type2str(TempVar_TAC_Allocate[oprd.data.id].var_type) << "\t";
fout << "v" << oprd.data.id << endl;
}
void TACPrintVariable(ofstream& fout, int id)
{
fout << "Parameter" << endl;
list <int> arglist;
for (list <TSymbolListVariableEntry> :: iterator p = SymbolListFunction[id].arg_list.begin();
p != SymbolListFunction[id].arg_list.end(); p++)
{
arglist.push_back(p->var_name_TAC);
TACPrintNamedVariable(fout, *p);
}
list <int> tmplist;
fout << "Var" << endl;
for (TInstruction* p = TAC_Program_Instruction[id].begin; p != NULL; p = p->next)
{
if (p->op != TAC_OP_CALL)
{
__chk_print(fout, arglist, tmplist, p->a);
}
if (p->op != TAC_OP_RCALL)
{
__chk_print(fout, arglist, tmplist, p->b);
}
__chk_print(fout, arglist, tmplist, p->c);
}
}
void TACPrintOPName(ofstream& fout, int op)
{
if (op == TAC_OP_ADD) fout << "ADD";
if (op == TAC_OP_SUB) fout << "SUB";
if (op == TAC_OP_MUL) fout << "MUL";
if (op == TAC_OP_DIV) fout << "DIV";
if (op == TAC_OP_MOD) fout << "MOD";
if (op == TAC_OP_LET) fout << "LET";
if (op == TAC_OP_LABEL) fout << "LABEL";
if (op == TAC_OP_JE) fout << "JE";
if (op == TAC_OP_JNE) fout << "JNE";
if (op == TAC_OP_JL) fout << "JL";
if (op == TAC_OP_JLE) fout << "JLE";
if (op == TAC_OP_JG) fout << "JG";
if (op == TAC_OP_JGE) fout << "JGE";
if (op == TAC_OP_JMP) fout << "JMP";
if (op == TAC_OP_AND) fout << "AND";
if (op == TAC_OP_OR) fout << "OR";
if (op == TAC_OP_NOT) fout << "NOT";
if (op == TAC_OP_LEA) fout << "LEA";
if (op == TAC_OP_LOAD) fout << "LOAD";
if (op == TAC_OP_STORE) fout << "STORE";
if (op == TAC_OP_PUSH) fout << "PUSH";
if (op == TAC_OP_POP) fout << "POP";
if (op == TAC_OP_CALL) fout << "CALL";
if (op == TAC_OP_RCALL) fout << "RCALL";
if (op == TAC_OP_ICALL) fout << "ICALL";
if (op == TAC_OP_RICALL) fout << "RICALL";
if (op == TAC_OP_RET) fout << "RET";
if (op == TAC_OP_EXIT) fout << "EXIT";
if (op == TAC_OP_HALT) fout << "HALT";
if (op == TAC_OP_XOR) fout << "XOR";
if (op == TAC_OP_SHL) fout << "SHL";
if (op == TAC_OP_SHR) fout << "SHR";
if (op == TAC_OP_RREAD) fout << "RREAD";
if (op == TAC_OP_RWRITE) fout << "RWRITE";
if (op == TAC_OP_ASM) fout << "ASM";
if (op == TAC_OP_MEMCPY) fout << "MEMCPY";
if (op == TAC_OP_LEAFUNC) fout << "LEAFUNC";
}
void TACPrintOperand(ofstream& fout, const TOperand& oprd)
{
if (oprd.oper_type == TAC_oper_type_NULL)
{
fout << "\t(null)";
return;
}
fout << " ";
if (oprd.oper_type == TAC_oper_type_constint)
{
fout << oprd.data.cint;
}
else if (oprd.oper_type == TAC_oper_type_label)
{
fout << "L" << oprd.data.id;
}
else if (oprd.oper_type == TAC_oper_type_variable)
{
fout << "v" << oprd.data.id;
}
}
void TACPrintFuncName(ofstream& fout, const TOperand& oprd)
{
fout << " ";
if (oprd.data.id >= 0)
{
fout << IDHash_int2ID(oprd.data.id);
}
else
{
if (oprd.data.id == TAC_FUNC_PRINTINT) fout << "PrintInteger";
if (oprd.data.id == TAC_FUNC_PRINTDOUBLE) fout << "PrintDouble";
if (oprd.data.id == TAC_FUNC_READINT) fout << "ReadInteger";
if (oprd.data.id == TAC_FUNC_READDOUBLE) fout << "ReadDouble";
if (oprd.data.id == TAC_FUNC_ALLOCATEMEM) fout << "Alloc";
if (oprd.data.id == TAC_FUNC_PRINTSTR) fout << "PrintString";
if (oprd.data.id == TAC_FUNC_READLINE) fout << "ReadLine";
}
}
void TACPrintStmt(ofstream& fout, int id)
{
for (TInstruction* p = TAC_Program_Instruction[id].begin; p != NULL; p = p->next)
{
fout << "\t";
TACPrintOPName(fout, p->op);
if (p->op != TAC_OP_CALL)
{
TACPrintOperand(fout, p->a);
}
else
{
TACPrintFuncName(fout, p->a);
}
if (p->op != TAC_OP_RCALL && p->op != TAC_OP_LEAFUNC)
{
TACPrintOperand(fout, p->b);
}
else
{
TACPrintFuncName(fout, p->b);
}
TACPrintOperand(fout, p->c);
fout << endl;
}
}
void TACPrintOut(char* filename)
{
ofstream fout(filename);
fout.precision(20);
fout << "GlobalData" << endl;
for (int i = 0; i < SymbolListConstStringCNT; i ++)
{
fout << "\tv" << SymbolListConstString[i].Ventry.var_name_TAC << "\t";
for (list <int> :: iterator p = SymbolListConstString[i].data.begin(); p != SymbolListConstString[i].data.end(); p ++)
{
fout << *p << " ";
}
TempVar_TAC_Allocate[SymbolListConstString[i].Ventry.var_name_TAC].global_sign = 1;
fout << "#" << endl;
}
fout << "EndGlobalData" << endl << endl;
fout << "Global" << endl;
for (list <int> :: iterator p = SymbolListLevelDefined[0].begin(); p!= SymbolListLevelDefined[0].end(); p++)
{
int id = *p;
if (SymbolListVariable[id].empty()) continue;
if (SymbolListVariable[id].begin()->const_sign) continue;
if (!TempVar_TAC_Allocate[SymbolListVariable[id].begin()->var_name_TAC].global_sign)
{
TACPrintNamedVariable(fout, *(SymbolListVariable[id].begin()));
}
TempVar_TAC_Allocate[SymbolListVariable[id].begin()->var_name_TAC].global_sign = 1;
}
fout << "EndGlobal" << endl;
fout << endl;
for (list <int> :: iterator p = SymbolListFunctionList.begin(); p != SymbolListFunctionList.end(); p++)
{
int id = *p;
fout << IDHash_int2ID(id) << " : " << type2str(SymbolListType[SymbolListFunction[id].func_type].ret_type_id) << endl;
fout << "BeginFunction" << endl;
TACPrintVariable(fout, id);
fout << "Stmt" << endl;
TACPrintStmt(fout, id);
fout << "EndFunction" << endl;
fout << endl;
}
}
#endif // __TACPRINTSUPPORT_CPP__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -