更新目录结构,修改IR结构,部分修复IR生成

This commit is contained in:
rain2133
2025-06-20 22:46:04 +08:00
parent 1de8c0e7d7
commit c54543bff3
16 changed files with 1699 additions and 1890 deletions

View File

@@ -834,204 +834,4 @@ std::any SysYIRGenerator::visitConstExp(SysYParser::ConstExpContext* ctx) {
return res;
}
/* begin
std::any SysYIRGenerator::visitConstGlobalDecl(SysYParser::ConstDeclContext *ctx, Type* type) {
std::vector<Value *> values;
for (auto constDef : ctx->constDef()) {
auto name = constDef->Ident()->getText();
// get its dimensions
vector<Value *> dims;
for (auto dim : constDef->constExp())
dims.push_back(any_cast<Value *>(dim->accept(this)));
if (dims.size() == 0) {
auto init = constDef->ASSIGN() ? any_cast<Value *>((constDef->constInitVal()->constExp()->accept(this)))
: nullptr;
if (init && isa<ConstantValue>(init)){
Type *btype = type->as<PointerType>()->getBaseType();
if (btype->isInt() && init->getType()->isFloat())
init = ConstantValue::get((int)dynamic_cast<ConstantValue *>(init)->getFloat());
else if (btype->isFloat() && init->getType()->isInt())
init = ConstantValue::get((float)dynamic_cast<ConstantValue *>(init)->getInt());
}
auto global_value = module->createGlobalValue(name, type, dims, init);
symbols_table.insert(name, global_value);
values.push_back(global_value);
}
else{
auto init = constDef->ASSIGN() ? any_cast<Value *>(dims[0])
: nullptr;
auto global_value = module->createGlobalValue(name, type, dims, init);
if (constDef->ASSIGN()) {
d = 0;
n = 0;
path.clear();
path = vector<int>(dims.size(), 0);
isalloca = false;
current_type = global_value->getType()->as<PointerType>()->getBaseType();
current_global = global_value;
numdims = global_value->getNumDims();
for (auto init : constDef->constInitVal()->constInitVal())
init->accept(this);
// visitConstInitValue(init);
}
symbols_table.insert(name, global_value);
values.push_back(global_value);
}
}
return values;
}
std::any SysYIRGenerator::visitVarGlobalDecl(SysYParser::VarDeclContext *ctx, Type* type){
std::vector<Value *> values;
for (auto varDef : ctx->varDef()) {
auto name = varDef->Ident()->getText();
// get its dimensions
vector<Value *> dims;
for (auto dim : varDef->constExp())
dims.push_back(any_cast<Value *>(dim->accept(this)));
if (dims.size() == 0) {
auto init = varDef->ASSIGN() ? any_cast<Value *>((varDef->initVal()->exp()->accept(this)))
: nullptr;
if (init && isa<ConstantValue>(init)){
Type *btype = type->as<PointerType>()->getBaseType();
if (btype->isInt() && init->getType()->isFloat())
init = ConstantValue::get((int)dynamic_cast<ConstantValue *>(init)->getFloat());
else if (btype->isFloat() && init->getType()->isInt())
init = ConstantValue::get((float)dynamic_cast<ConstantValue *>(init)->getInt());
}
auto global_value = module->createGlobalValue(name, type, dims, init);
symbols_table.insert(name, global_value);
values.push_back(global_value);
}
else{
auto init = varDef->ASSIGN() ? any_cast<Value *>(dims[0])
: nullptr;
auto global_value = module->createGlobalValue(name, type, dims, init);
if (varDef->ASSIGN()) {
d = 0;
n = 0;
path.clear();
path = vector<int>(dims.size(), 0);
isalloca = false;
current_type = global_value->getType()->as<PointerType>()->getBaseType();
current_global = global_value;
numdims = global_value->getNumDims();
for (auto init : varDef->initVal()->initVal())
init->accept(this);
// visitInitValue(init);
}
symbols_table.insert(name, global_value);
values.push_back(global_value);
}
}
return values;
}
std::any SysYIRGenerator::visitConstLocalDecl(SysYParser::ConstDeclContext *ctx, Type* type){
std::vector<Value *> values;
// handle variables
for (auto constDef : ctx->constDef()) {
auto name = constDef->Ident()->getText();
vector<Value *> dims;
for (auto dim : constDef->constExp())
dims.push_back(any_cast<Value *>(dim->accept(this)));
auto alloca = builder.createAllocaInst(type, dims, name);
symbols_table.insert(name, alloca);
if (constDef->ASSIGN()) {
if (alloca->getNumDims() == 0) {
auto value = any_cast<Value *>(constDef->constInitVal()->constExp()->accept(this));
if (isa<ConstantValue>(value)) {
if (ctx->bType()->INT() && dynamic_cast<ConstantValue *>(value)->isFloat())
value = ConstantValue::get((int)dynamic_cast<ConstantValue *>(value)->getFloat());
else if (ctx->bType()->FLOAT() && dynamic_cast<ConstantValue *>(value)->isInt())
value = ConstantValue::get((float)dynamic_cast<ConstantValue *>(value)->getInt());
}
else if (alloca->getType()->as<PointerType>()->getBaseType()->isInt() && value->getType()->isFloat())
value = builder.createFtoIInst(value);
else if (alloca->getType()->as<PointerType>()->getBaseType()->isFloat() && value->getType()->isInt())
value = builder.createIToFInst(value);
auto store = builder.createStoreInst(value, alloca);
}
else{
d = 0;
n = 0;
path.clear();
path = vector<int>(alloca->getNumDims(), 0);
isalloca = true;
current_alloca = alloca;
current_type = alloca->getType()->as<PointerType>()->getBaseType();
numdims = alloca->getNumDims();
for (auto init : constDef->constInitVal()->constInitVal())
init->accept(this);
}
}
values.push_back(alloca);
}
return values;
}
std::any SysYIRGenerator::visitVarLocalDecl(SysYParser::VarDeclContext *ctx, Type* type){
std::vector<Value *> values;
for (auto varDef : ctx->varDef()) {
auto name = varDef->Ident()->getText();
vector<Value *> dims;
for (auto dim : varDef->constExp())
dims.push_back(any_cast<Value *>(dim->accept(this)));
auto alloca = builder.createAllocaInst(type, dims, name);
symbols_table.insert(name, alloca);
if (varDef->ASSIGN()) {
if (alloca->getNumDims() == 0) {
auto value = any_cast<Value *>(varDef->initVal()->exp()->accept(this));
if (isa<ConstantValue>(value)) {
if (ctx->bType()->INT() && dynamic_cast<ConstantValue *>(value)->isFloat())
value = ConstantValue::get((int)dynamic_cast<ConstantValue *>(value)->getFloat());
else if (ctx->bType()->FLOAT() && dynamic_cast<ConstantValue *>(value)->isInt())
value = ConstantValue::get((float)dynamic_cast<ConstantValue *>(value)->getInt());
}
else if (alloca->getType()->as<PointerType>()->getBaseType()->isInt() && value->getType()->isFloat())
value = builder.createFtoIInst(value);
else if (alloca->getType()->as<PointerType>()->getBaseType()->isFloat() && value->getType()->isInt())
value = builder.createIToFInst(value);
auto store = builder.createStoreInst(value, alloca);
}
else{
d = 0;
n = 0;
path.clear();
path = vector<int>(alloca->getNumDims(), 0);
isalloca = true;
current_alloca = alloca;
current_type = alloca->getType()->as<PointerType>()->getBaseType();
numdims = alloca->getNumDims();
for (auto init : varDef->initVal()->initVal())
init->accept(this);
}
}
values.push_back(alloca);
}
return values;
}
end
*/
} // namespace sysy