Skip to content

Commit

Permalink
Refactor into convert_call_args()
Browse files Browse the repository at this point in the history
  • Loading branch information
certik committed Aug 24, 2020
1 parent 0a7fb00 commit 105ae28
Showing 1 changed file with 15 additions and 24 deletions.
39 changes: 15 additions & 24 deletions src/lfortran/codegen/asr_to_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -672,13 +672,8 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
exit(context, *module, *builder, exit_code);
}

void visit_SubroutineCall(const ASR::SubroutineCall_t &x) {
ASR::Subroutine_t *s = SUBROUTINE((ASR::asr_t*)x.m_name);
llvm::Function *fn = module->getFunction(s->m_name);
if (!fn) {
throw CodeGenError("Subroutine code not generated for '"
+ std::string(s->m_name) + "'");
}
template <typename T>
std::vector<llvm::Value*> convert_call_args(const T &x) {
std::vector<llvm::Value *> args;
for (size_t i=0; i<x.n_args; i++) {
if (x.m_args[i]->type == ASR::exprType::Var) {
Expand All @@ -695,32 +690,28 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
}
args.push_back(tmp);
}
return args;
}

void visit_SubroutineCall(const ASR::SubroutineCall_t &x) {
ASR::Subroutine_t *s = SUBROUTINE((ASR::asr_t*)x.m_name);
llvm::Function *fn = module->getFunction(s->m_name);
if (!fn) {
throw CodeGenError("Subroutine code not generated for '"
+ std::string(s->m_name) + "'");
}
std::vector<llvm::Value *> args = convert_call_args(x);
builder->CreateCall(fn, args);
}

void visit_FuncCall(const ASR::FuncCall_t &x) {
ASR::Function_t *s = FUNCTION((ASR::asr_t*)x.m_func);
llvm::Function *fn = module->getFunction(s->m_name);
if (!fn) {
throw CodeGenError("Subroutine code not generated for '"
throw CodeGenError("Function code not generated for '"
+ std::string(s->m_name) + "'");
}
std::vector<llvm::Value *> args;
for (size_t i=0; i<x.n_args; i++) {
if (x.m_args[i]->type == ASR::exprType::Var) {
ASR::Variable_t *arg = VARIABLE((ASR::asr_t*)EXPR_VAR((ASR::asr_t*)x.m_args[i])->m_v);
std::string arg_name = arg->m_name;
tmp = llvm_symtab[arg_name];
} else {
this->visit_expr(*x.m_args[i]);
llvm::Value *value=tmp;
llvm::AllocaInst *target = builder->CreateAlloca(
llvm::Type::getInt64Ty(context), nullptr);
builder->CreateStore(value, target);
tmp = target;
}
args.push_back(tmp);
}
std::vector<llvm::Value *> args = convert_call_args(x);
tmp = builder->CreateCall(fn, args);
}

Expand Down

0 comments on commit 105ae28

Please sign in to comment.