目录
Code.h 生成mips中间代码
在之前实验三的基础上改的,有冗余代码,我也不想删了
#ifndef __CODE_H__
#define __CODE_H__
#include <ConstantAndVariable.h>
#include <Function.h>
#include <KV.h>
#include <algorithm>
#include <ctype.h>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <string.h>
#include <utility>
#include <vector>
using namespace std;
class Code {
private:
public:
// 全局变量表
vector<ConstantAndVariable*> ConstantsdAndvariables;
// 全局函数表
vector<Function*> Functions;
//树的根节点
KV* root;
//当前的作用域
string build_scope = "GLOBAL";
// mips .data输出结果,先存一个临时的字符串进去
string mips_data_out = ".data\n\tendline : .asciiz \"\\n\"\n";
// mips .text输出结果
string mips_text_out = ".text\n";
Code() { }
Code(KV* tree_root)
{
this->root = tree_root;
}
~Code() { }
// 对树进行遍历
void mips_code_generate()
{
vector<KV*> stacks;
stacks.push_back(this->root);
while (stacks.size() > 0) {
KV* current = stacks.back();
stacks.pop_back();
if (!current->isleaf()) {
//主函数
if (judge_main_function(current)) {
}
//常量定义
else if (judge_constant_definition(current)) {
// do nothing
}
//变量定义无初始化
else if (judge_Variable_definition_with_no_initialization(current)) {
for (int i = current->get_children().size() - 1; i >= 0; i--) {
stacks.push_back(current->get_children()[i]);
}
//变量定义及初始化
} else if (judge_Variable_definition_with_initialization(current)) {
for (int i = current->get_children().size() - 1; i >= 0; i--) {
stacks.push_back(current->get_children()[i]);
}
} else {
for (int i = current->get_children().size() - 1; i >= 0; i--) {
stacks.push_back(current->get_children()[i]);
}
}
}
else {
// do nothing
}
}
}
//主函数
bool judge_main_function(KV* root)
{
if (root->category_code == "<主函数>") {
string temp_name = root->children[1]->value;
string temp_type = root->children[0]->category_code;
build_scope = "void_main";
KV* compound_statement = root->children[5];
check_compound_sentence_in_main(compound_statement);
return true;
} else {
return false;
}
}
// <常量定义> judge_constant_definition(),用于建立变量表
bool judge_constant_definition(KV* root)
{
if (root->category_code == "<常量定义>") {
string temp_name = "\0";
string temp_type = root->children[0]->category_code;
int temp_index = 1;
while (temp_index < root->get_children().size()) {
if (match_IDENFR(root->children[temp_index])) {
temp_name = root->children[temp_index]->value;
// 生成常量的mips .data代码
if (temp_type == "INTTK") {
mips_data_out += "\t" + temp_name + " : .word ";
} else if (temp_type == "CHARTK") {
mips_data_out += "\t" + temp_name + " : .asciiz ";
} else {
// do nothing
}
ConstantAndVariable* temp_constant = new ConstantAndVariable(temp_name, temp_type, true, 0, true, build_scope);
ConstantsdAndvariables.push_back(temp_constant);
temp_index++;
temp_index++;
// 生成常量的mips .data代码,我就不考虑字符串了
if (root->children[temp_index]->category_code == "<整数>") {
KV* temp_int = root->children[temp_index];
for (KV* temp_int_member : temp_int->children) {
if (temp_int_member->category_code == "<无符号整数>") {
mips_data_out += temp_int_member->children[0]->value + "\n";
} else {
mips_data_out += temp_int_member->value;
}
}
}
temp_index++;
}
if (temp_index == root->get_children().size()) {
break;
} else if (match_COMMA(root->children[temp_index])) {
temp_index++;
continue;
}
}
return true;
} else {
return false;
}
}
// <变量定义无初始化> judge_Variable_definition_with_no_initialization(),用于建立变量表,只需要记录维度就好
bool judge_Variable_definition_with_no_initialization(KV* root)
{
if (root->category_code == "<变量定义无初始化>") {
string temp_name = "\0";
string temp_type = root->children[0]->category_code;
int temp_index = 1;
while (temp_index < root->get_children().size()) {
if (match_IDENFR(root->children[temp_index])) {
temp_name = root->children[temp_index]->value;
// 生成变量定义无初始化的mips .data代码
if (temp_type == "INTTK") {
mips_data_out += "\t" + temp_name + " : .word 0\n";
} else if (temp_type == "CHARTK") {
mips_data_out += "\t" + temp_name + " : .asciiz \"init\"\n";
} else {
// do nothing
}
temp_index++;
if (temp_index == root->get_children().size()) {
ConstantAndVariable* temp_constant = new ConstantAndVariable(temp_name, temp_type, false, 0, false, build_scope);
ConstantsdAndvariables.push_back(temp_constant);
break;
} else if (match_COMMA(root->children[temp_index])) {
ConstantAndVariable* temp_constant = new ConstantAndVariable(temp_name, temp_type, false, 0, false, build_scope);
ConstantsdAndvariables.push_back(temp_constant);
temp_index++;
continue;
}
//<类型标识符> <标识符>'['<无符号整数>']'
else if (match_LBRACK(root->children[temp_index])) {
temp_index++;
temp_index++;
temp_index++;
if (temp_index == root->get_children().size()) {
ConstantAndVariable* temp_constant = new ConstantAndVariable(temp_name, temp_type, false, 1, false, build_scope);
ConstantsdAndvariables.push_back(temp_constant);
break;