Skip to content

Commit

Permalink
Merge pull request #21 from Shaikh-Ubaid/subroutine_and_function_call
Browse files Browse the repository at this point in the history
Support `SubroutineCall` and `FunctionCall`
  • Loading branch information
Shaikh-Ubaid authored Dec 18, 2023
2 parents d336bcf + e3c5164 commit 1151444
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ endmacro(RUN)
# wasm --- compile to WASM binary directly

RUN(NAME expr1.c LABELS gcc c wasm llvm NOFAST)
RUN(NAME expr2.c LABELS gcc c wasm llvm NOFAST)

# arrays
RUN(NAME array_01.cpp LABELS gcc llvm NOFAST)
Expand Down
17 changes: 17 additions & 0 deletions integration_tests/expr2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <stdio.h>

void subrout(int x) {
printf("%s %d\n", "x is", x);
}

int func(int x) {
return x + 2;
}

int main() {
int p = 26;
subrout(p);
p = func(p);
printf("%s %d\n", "p is", p);
return 0;
}
34 changes: 23 additions & 11 deletions src/lc/clang_ast_to_asr.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,9 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
const clang::Qualifiers qualifiers = split_qual_type.asPair().second;
Location l; l.first = 1, l.last = 1;
ASR::ttype_t* type = nullptr;
if( clang_type->isCharType() ) {
if (clang_type->isVoidType() ) {
// do nothing
} else if( clang_type->isCharType() ) {
type = ASRUtils::TYPE(ASR::make_Character_t(al, l, 1, -1, nullptr));
} else if( clang_type->isIntegerType() ) {
type = ASRUtils::TYPE(ASR::make_Integer_t(al, l, 4));
Expand Down Expand Up @@ -406,14 +408,18 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit

ASR::Var_t* callee_Var = ASR::down_cast<ASR::Var_t>(callee);
ASR::symbol_t* callee_sym = callee_Var->m_v;
if( x->getCallReturnType(*Context).split().asPair().first->isVoidType() ) {
throw std::runtime_error("Void return type not yet supported.");
const clang::QualType& qual_type = x->getCallReturnType(*Context);
ASR::ttype_t* return_type = ClangTypeToASRType(qual_type);
if( return_type == nullptr ) {
tmp = ASRUtils::make_SubroutineCall_t_util(al, Lloc(x), callee_sym,
callee_sym, call_args.p, call_args.size(), nullptr,
nullptr, false);
} else {
const clang::QualType& qual_type = x->getCallReturnType(*Context);
tmp = ASRUtils::make_FunctionCall_t_util(al, Lloc(x), callee_sym,
callee_sym, call_args.p, call_args.size(), ClangTypeToASRType(qual_type),
callee_sym, call_args.p, call_args.size(), return_type,
nullptr, nullptr);
}
is_stmt_created = true;
return true;
}

Expand All @@ -432,11 +438,15 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
}

ASR::ttype_t* return_type = ClangTypeToASRType(x->getReturnType());
ASR::symbol_t* return_sym = ASR::down_cast<ASR::symbol_t>(ASR::make_Variable_t(al, Lloc(x),
current_scope, s2c(al, "__return_var"), nullptr, 0, ASR::intentType::ReturnVar, nullptr, nullptr,
ASR::storage_typeType::Default, return_type, nullptr, ASR::abiType::Source, ASR::accessType::Public,
ASR::presenceType::Required, false));
current_scope->add_symbol("__return_var", return_sym);
ASR::symbol_t* return_sym = nullptr;
ASR::expr_t* return_var = nullptr;
if (return_type != nullptr) {
return_sym = ASR::down_cast<ASR::symbol_t>(ASR::make_Variable_t(al, Lloc(x),
current_scope, s2c(al, "__return_var"), nullptr, 0, ASR::intentType::ReturnVar, nullptr, nullptr,
ASR::storage_typeType::Default, return_type, nullptr, ASR::abiType::Source, ASR::accessType::Public,
ASR::presenceType::Required, false));
current_scope->add_symbol("__return_var", return_sym);
}

Vec<ASR::stmt_t*>* current_body_copy = current_body;
Vec<ASR::stmt_t*> body; body.reserve(al, 1);
Expand All @@ -446,7 +456,9 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
}
current_body = current_body_copy;

ASR::expr_t* return_var = ASRUtils::EXPR(ASR::make_Var_t(al, return_sym->base.loc, return_sym));
if (return_type != nullptr) {
return_var = ASRUtils::EXPR(ASR::make_Var_t(al, return_sym->base.loc, return_sym));
}
tmp = ASRUtils::make_Function_t_util(al, Lloc(x), current_scope, s2c(al, name), nullptr, 0,
args.p, args.size(), body.p, body.size(), return_var, ASR::abiType::Source, ASR::accessType::Public,
ASR::deftypeType::Implementation, nullptr, false, false, false, false, false, nullptr, 0,
Expand Down

0 comments on commit 1151444

Please sign in to comment.