main
Odeinjul 12 months ago
parent 96175111c4
commit e5754f92b9
No known key found for this signature in database
GPG Key ID: E384228B2B38FFBB

@ -27,7 +27,6 @@ Ptr<GlobalVariable> GlobalVariable::create(std::string name, Ptr<Module> m, Ptr<
std::string GlobalVariable::print() std::string GlobalVariable::print()
{ {
std::cout << "globalval0" <<std::endl;
std::string global_val_ir; std::string global_val_ir;
global_val_ir += print_as_op(shared_from_this(), false); global_val_ir += print_as_op(shared_from_this(), false);
global_val_ir += " = "; global_val_ir += " = ";
@ -36,7 +35,6 @@ std::string GlobalVariable::print()
global_val_ir += " "; global_val_ir += " ";
global_val_ir += this->get_init()->print(); global_val_ir += this->get_init()->print();
return global_val_ir; return global_val_ir;
std::cout << "globalval0" <<std::endl;
} }
} }

@ -129,14 +129,11 @@ std::string Module::print()
std::string module_ir; std::string module_ir;
for ( auto global_val : this->global_list_) for ( auto global_val : this->global_list_)
{ {
std::cout << "global0" <<std::endl;
module_ir += global_val->print(); module_ir += global_val->print();
module_ir += "\n"; module_ir += "\n";
std::cout << "global1" <<std::endl;
} }
for ( auto func : this->function_list_) for ( auto func : this->function_list_)
{ {
std::cout << "func" <<std::endl;
module_ir += func->print(); module_ir += func->print();
module_ir += "\n"; module_ir += "\n";
} }

@ -115,11 +115,9 @@ void IRBuilder::BinaryExprGen(Ptr<Value> lhs, Ptr<Value> rhs, SyntaxTree::BinOp
} }
} else { } else {
if (dynamic_pointer_cast<Constant>(lhs) == nullptr && lhs->get_type()->is_pointer_type()) { if (dynamic_pointer_cast<Constant>(lhs) == nullptr && lhs->get_type()->is_pointer_type()) {
std::cout << 2<<std::endl;
lhs = builder->create_load(lhs); lhs = builder->create_load(lhs);
} }
if (dynamic_pointer_cast<Constant>(rhs) == nullptr && rhs->get_type()->is_pointer_type()) { if (dynamic_pointer_cast<Constant>(rhs) == nullptr && rhs->get_type()->is_pointer_type()) {
std::cout << 3<<std::endl;
rhs = builder->create_load(rhs); rhs = builder->create_load(rhs);
} }
auto lhsFloat = (dynamic_pointer_cast<Constant>(lhs) == nullptr && lhs->get_type()->is_float_type()) || dynamic_pointer_cast<ConstantFloat>(lhs); auto lhsFloat = (dynamic_pointer_cast<Constant>(lhs) == nullptr && lhs->get_type()->is_float_type()) || dynamic_pointer_cast<ConstantFloat>(lhs);
@ -134,9 +132,7 @@ void IRBuilder::BinaryExprGen(Ptr<Value> lhs, Ptr<Value> rhs, SyntaxTree::BinOp
switch (op) { switch (op) {
case SyntaxTree::BinOp::PLUS: case SyntaxTree::BinOp::PLUS:
if (isFloat) { if (isFloat) {
std::cout << 1<<std::endl;
tmpInst = builder->create_fadd(lhs, rhs); tmpInst = builder->create_fadd(lhs, rhs);
std::cout << 2 <<std::endl;
} else { } else {
tmpInst = builder->create_iadd(lhs, rhs); tmpInst = builder->create_iadd(lhs, rhs);
} }
@ -241,12 +237,9 @@ void IRBuilder::BinaryCondExprGen(Ptr<Value> lhs, Ptr<Value> rhs, SyntaxTree::Bi
if (dynamic_pointer_cast<Constant>(lhs) == nullptr &&lhs->get_type()->is_pointer_type()) { if (dynamic_pointer_cast<Constant>(lhs) == nullptr &&lhs->get_type()->is_pointer_type()) {
lhs = builder->create_load(lhs); lhs = builder->create_load(lhs);
} }
std::cout << (dynamic_pointer_cast<Constant>(rhs)) <<std::endl;
std::cout << 2<<std::endl;
if (dynamic_pointer_cast<Constant>(rhs) == nullptr && rhs->get_type()->is_pointer_type()) { if (dynamic_pointer_cast<Constant>(rhs) == nullptr && rhs->get_type()->is_pointer_type()) {
rhs = builder->create_load(rhs); rhs = builder->create_load(rhs);
} }
std::cout << 2<<std::endl;
auto lhsFloat = (dynamic_pointer_cast<Constant>(lhs) == nullptr && lhs->get_type()->is_float_type()) || dynamic_pointer_cast<ConstantFloat>(lhs); auto lhsFloat = (dynamic_pointer_cast<Constant>(lhs) == nullptr && lhs->get_type()->is_float_type()) || dynamic_pointer_cast<ConstantFloat>(lhs);
auto rhsFloat = (dynamic_pointer_cast<Constant>(rhs) == nullptr && rhs->get_type()->is_float_type()) || dynamic_pointer_cast<ConstantFloat>(rhs); auto rhsFloat = (dynamic_pointer_cast<Constant>(rhs) == nullptr && rhs->get_type()->is_float_type()) || dynamic_pointer_cast<ConstantFloat>(rhs);
if (lhsFloat && !rhsFloat) { if (lhsFloat && !rhsFloat) {
@ -442,7 +435,7 @@ void IRBuilder::visit(SyntaxTree::FuncFParamList &node) {
// FINISH // FINISH
void IRBuilder::visit(SyntaxTree::FuncParam &node) { void IRBuilder::visit(SyntaxTree::FuncParam &node) {
auto tmpType = GetParamType(node.param_type, node.array_index.empty()); auto tmpType = GetParamType(node.param_type, !node.array_index.empty());
funcFParam.push_back(tmpType); funcFParam.push_back(tmpType);
} }
@ -475,19 +468,17 @@ void IRBuilder::visit(SyntaxTree::VarDef &node) {
TypeConvert(tmpInst, varType); TypeConvert(tmpInst, varType);
varInit.push_back(dynamic_pointer_cast<Constant>(tmpInst)); varInit.push_back(dynamic_pointer_cast<Constant>(tmpInst));
} }
auto otherLen = arrayType->get_num_of_elements() - node.initializers->elementList.size(); auto arrayLen = arrayType->get_num_of_elements();
auto otherLen = arrayLen - node.initializers->elementList.size();
auto tmpZero = CONST_INT(0); auto tmpZero = CONST_INT(0);
TypeConvert(tmpZero, varType); TypeConvert(tmpZero, varType);
for (int i = 0; i < otherLen; i++) { for (int i = 0; i < otherLen; i++) {
varInit.push_back(dynamic_pointer_cast<Constant>(tmpInst)); varInit.push_back(dynamic_pointer_cast<Constant>(tmpInst));
} }
auto zeroInit = ConstantZero::create(varType, module); auto arrayInit = ConstantArray::create(varType->get_array_type(varType, arrayLen), varInit);
auto tmpInit = ConstantArray::create(static_pointer_cast<ArrayType>(varType), varInit); identAlloca = GlobalVariable::create(node.name, module, arrayType, node.is_constant, arrayInit);
identAlloca = GlobalVariable::create(node.name, module, varType, node.is_constant, tmpInit);
//std::cout <<"VarDef1"<<std::endl;
} else { } else {
//std::cout <<"VarDef2"<<std::endl;
if (node.is_constant) { if (node.is_constant) {
node.initializers->expr->accept(*this); node.initializers->expr->accept(*this);
TypeConvert(tmpInst, varType); TypeConvert(tmpInst, varType);
@ -551,7 +542,6 @@ void IRBuilder::visit(SyntaxTree::LVal &node) {
auto constIndex = dynamic_pointer_cast<ConstantInt>(tmpInst); auto constIndex = dynamic_pointer_cast<ConstantInt>(tmpInst);
auto globalIdent = dynamic_pointer_cast<GlobalVariable>(ident); auto globalIdent = dynamic_pointer_cast<GlobalVariable>(ident);
if(globalIdent != nullptr && globalIdent->is_const() && constIndex != nullptr) { if(globalIdent != nullptr && globalIdent->is_const() && constIndex != nullptr) {
std::cout << "Const LVal "<<std::endl;
auto arrayInit = dynamic_pointer_cast<ConstantArray>(globalIdent->get_init()); auto arrayInit = dynamic_pointer_cast<ConstantArray>(globalIdent->get_init());
tmpInst = arrayInit->get_element_value(constIndex->get_value()); tmpInst = arrayInit->get_element_value(constIndex->get_value());
} else { } else {
@ -601,9 +591,7 @@ void IRBuilder::visit(SyntaxTree::ReturnStmt &node) {
auto expectRetType = auto expectRetType =
builder->get_insert_block()->get_parent()->get_return_type(); builder->get_insert_block()->get_parent()->get_return_type();
TypeConvert(tmpInst, expectRetType); TypeConvert(tmpInst, expectRetType);
std::cout << "RET" <<std::endl;
builder->create_store(tmpInst, retAlloca); builder->create_store(tmpInst, retAlloca);
std::cout << "RET" <<std::endl;
} }
// every ret stmt only store the value and jump to the retBB // every ret stmt only store the value and jump to the retBB
builder->create_br(retBB); builder->create_br(retBB);
@ -614,7 +602,7 @@ void IRBuilder::visit(SyntaxTree::ReturnStmt &node) {
void IRBuilder::visit(SyntaxTree::BlockStmt &node) { void IRBuilder::visit(SyntaxTree::BlockStmt &node) {
scope.enter(); scope.enter();
for (const auto &stmt : node.body) { for (const auto &stmt : node.body) {
std::cout << "Block" <<std::endl; //std::cout << "Block" <<std::endl;
stmt->accept(*this); stmt->accept(*this);
} }
scope.exit(); scope.exit();

@ -54,7 +54,6 @@ int main(int argc, char *argv[])
m->set_file_name(filename); m->set_file_name(filename);
m->set_print_name(); m->set_print_name();
auto IR = m->print(); auto IR = m->print();
std::cout << "END OUT" <<std::endl;
std::ofstream output_stream; std::ofstream output_stream;
output_stream.open(output_llvm_file, std::ios::out); output_stream.open(output_llvm_file, std::ios::out);
output_stream << IR; output_stream << IR;

Loading…
Cancel
Save