Skip to content

Commit

Permalink
Merge pull request #22 from Shaikh-Ubaid/exit
Browse files Browse the repository at this point in the history
Support `exit()`
  • Loading branch information
Shaikh-Ubaid authored Dec 18, 2023
2 parents 1151444 + 17068a6 commit 08dd2b9
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 25 deletions.
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ endmacro(RUN)

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

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

int main() {
int x;
x = (2+3)*5;
exit(1);
printf("%d\n", x);
return 0;
}
78 changes: 53 additions & 25 deletions src/lc/clang_ast_to_asr.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@

namespace LCompilers {

enum SpecialFunc {
Printf,
Exit
};

std::map<std::string, SpecialFunc> special_function_map = {
{"printf", SpecialFunc::Printf},
{"exit", SpecialFunc::Exit}
};

class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisitor> {

public:
Expand Down Expand Up @@ -140,6 +150,16 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
tu->m_symtab->add_symbol(prog_name, ASR::down_cast<ASR::symbol_t>(prog));
}

void remove_special_functions() {
ASR::TranslationUnit_t* tu = (ASR::TranslationUnit_t*)this->tu;
if (tu->m_symtab->resolve_symbol("printf")) {
tu->m_symtab->erase_symbol("printf");
}
if (tu->m_symtab->resolve_symbol("exit")) {
tu->m_symtab->erase_symbol("exit");
}
}

bool TraverseTranslationUnitDecl(clang::TranslationUnitDecl *x) {
SymbolTable *parent_scope = al.make_new<SymbolTable>(nullptr);
current_scope = parent_scope;
Expand All @@ -151,8 +171,7 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
}

construct_program();
// remove printf definition
((ASR::TranslationUnit_t*)tu)->m_symtab->erase_symbol("printf");
remove_special_functions();

return true;
}
Expand Down Expand Up @@ -339,27 +358,6 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
return true;
}

void handle_printf(clang::CallExpr *x) {
Vec<ASR::expr_t*> args;
args.reserve(al, 1);
bool skip_format_str = true;
for (auto *p : x->arguments()) {
TraverseStmt(p);
if (skip_format_str) {
skip_format_str = false;
continue;
}
args.push_back(al, ASRUtils::EXPR(tmp));
}
tmp = ASR::make_Print_t(al, Lloc(x), args.p, args.size(), nullptr, nullptr);
is_stmt_created = true;
}

bool check_printf(ASR::expr_t* callee) {
ASR::Var_t* callee_Var = ASR::down_cast<ASR::Var_t>(callee);
return std::string(ASRUtils::symbol_name(callee_Var->m_v)) == "printf";
}

bool TraverseCXXOperatorCallExpr(clang::CXXOperatorCallExpr* x) {
clang::Expr* callee = x->getCallee();
TraverseStmt(callee);
Expand All @@ -385,11 +383,41 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
return true;
}

bool check_and_handle_special_function(
clang::CallExpr *x, ASR::expr_t* callee) {
ASR::Var_t* callee_Var = ASR::down_cast<ASR::Var_t>(callee);
std::string func_name = std::string(ASRUtils::symbol_name(callee_Var->m_v));
if (special_function_map.find(func_name) == special_function_map.end()) {
return false;
}
SpecialFunc sf = special_function_map[func_name];
Vec<ASR::expr_t*> args;
args.reserve(al, 1);
bool skip_format_str = true;
for (auto *p : x->arguments()) {
TraverseStmt(p);
if (sf == SpecialFunc::Printf && skip_format_str) {
skip_format_str = false;
continue;
}
args.push_back(al, ASRUtils::EXPR(tmp));
}
if (sf == SpecialFunc::Printf) {
tmp = ASR::make_Print_t(al, Lloc(x), args.p, args.size(), nullptr, nullptr);
} else if (sf == SpecialFunc::Exit) {
LCOMPILERS_ASSERT(args.size() == 1);
tmp = ASR::make_Stop_t(al, Lloc(x), args[0]);
} else {
throw std::runtime_error("Only printf and exit special functions supported");
}
is_stmt_created = true;
return true;
}

bool TraverseCallExpr(clang::CallExpr *x) {
TraverseStmt(x->getCallee());
ASR::expr_t* callee = ASRUtils::EXPR(tmp);
if( check_printf(callee) ) {
handle_printf(x);
if( check_and_handle_special_function(x, callee) ) {
return true;
}

Expand Down
6 changes: 6 additions & 0 deletions src/runtime/include/stdlib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef STDLIB_H
#define STDLIB_H

void exit(int exit_code);

#endif // STDLIB_H

0 comments on commit 08dd2b9

Please sign in to comment.