Skip to content

Commit 08dd2b9

Browse files
authored
Merge pull request #22 from Shaikh-Ubaid/exit
Support `exit()`
2 parents 1151444 + 17068a6 commit 08dd2b9

File tree

4 files changed

+70
-25
lines changed

4 files changed

+70
-25
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ endmacro(RUN)
178178

179179
RUN(NAME expr1.c LABELS gcc c wasm llvm NOFAST)
180180
RUN(NAME expr2.c LABELS gcc c wasm llvm NOFAST)
181+
RUN(NAME expr3.c FAIL LABELS gcc c wasm llvm NOFAST)
181182

182183
# arrays
183184
RUN(NAME array_01.cpp LABELS gcc llvm NOFAST)

integration_tests/expr3.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
int main() {
5+
int x;
6+
x = (2+3)*5;
7+
exit(1);
8+
printf("%d\n", x);
9+
return 0;
10+
}

src/lc/clang_ast_to_asr.h

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@
3131

3232
namespace LCompilers {
3333

34+
enum SpecialFunc {
35+
Printf,
36+
Exit
37+
};
38+
39+
std::map<std::string, SpecialFunc> special_function_map = {
40+
{"printf", SpecialFunc::Printf},
41+
{"exit", SpecialFunc::Exit}
42+
};
43+
3444
class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisitor> {
3545

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

153+
void remove_special_functions() {
154+
ASR::TranslationUnit_t* tu = (ASR::TranslationUnit_t*)this->tu;
155+
if (tu->m_symtab->resolve_symbol("printf")) {
156+
tu->m_symtab->erase_symbol("printf");
157+
}
158+
if (tu->m_symtab->resolve_symbol("exit")) {
159+
tu->m_symtab->erase_symbol("exit");
160+
}
161+
}
162+
143163
bool TraverseTranslationUnitDecl(clang::TranslationUnitDecl *x) {
144164
SymbolTable *parent_scope = al.make_new<SymbolTable>(nullptr);
145165
current_scope = parent_scope;
@@ -151,8 +171,7 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
151171
}
152172

153173
construct_program();
154-
// remove printf definition
155-
((ASR::TranslationUnit_t*)tu)->m_symtab->erase_symbol("printf");
174+
remove_special_functions();
156175

157176
return true;
158177
}
@@ -339,27 +358,6 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
339358
return true;
340359
}
341360

342-
void handle_printf(clang::CallExpr *x) {
343-
Vec<ASR::expr_t*> args;
344-
args.reserve(al, 1);
345-
bool skip_format_str = true;
346-
for (auto *p : x->arguments()) {
347-
TraverseStmt(p);
348-
if (skip_format_str) {
349-
skip_format_str = false;
350-
continue;
351-
}
352-
args.push_back(al, ASRUtils::EXPR(tmp));
353-
}
354-
tmp = ASR::make_Print_t(al, Lloc(x), args.p, args.size(), nullptr, nullptr);
355-
is_stmt_created = true;
356-
}
357-
358-
bool check_printf(ASR::expr_t* callee) {
359-
ASR::Var_t* callee_Var = ASR::down_cast<ASR::Var_t>(callee);
360-
return std::string(ASRUtils::symbol_name(callee_Var->m_v)) == "printf";
361-
}
362-
363361
bool TraverseCXXOperatorCallExpr(clang::CXXOperatorCallExpr* x) {
364362
clang::Expr* callee = x->getCallee();
365363
TraverseStmt(callee);
@@ -385,11 +383,41 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
385383
return true;
386384
}
387385

386+
bool check_and_handle_special_function(
387+
clang::CallExpr *x, ASR::expr_t* callee) {
388+
ASR::Var_t* callee_Var = ASR::down_cast<ASR::Var_t>(callee);
389+
std::string func_name = std::string(ASRUtils::symbol_name(callee_Var->m_v));
390+
if (special_function_map.find(func_name) == special_function_map.end()) {
391+
return false;
392+
}
393+
SpecialFunc sf = special_function_map[func_name];
394+
Vec<ASR::expr_t*> args;
395+
args.reserve(al, 1);
396+
bool skip_format_str = true;
397+
for (auto *p : x->arguments()) {
398+
TraverseStmt(p);
399+
if (sf == SpecialFunc::Printf && skip_format_str) {
400+
skip_format_str = false;
401+
continue;
402+
}
403+
args.push_back(al, ASRUtils::EXPR(tmp));
404+
}
405+
if (sf == SpecialFunc::Printf) {
406+
tmp = ASR::make_Print_t(al, Lloc(x), args.p, args.size(), nullptr, nullptr);
407+
} else if (sf == SpecialFunc::Exit) {
408+
LCOMPILERS_ASSERT(args.size() == 1);
409+
tmp = ASR::make_Stop_t(al, Lloc(x), args[0]);
410+
} else {
411+
throw std::runtime_error("Only printf and exit special functions supported");
412+
}
413+
is_stmt_created = true;
414+
return true;
415+
}
416+
388417
bool TraverseCallExpr(clang::CallExpr *x) {
389418
TraverseStmt(x->getCallee());
390419
ASR::expr_t* callee = ASRUtils::EXPR(tmp);
391-
if( check_printf(callee) ) {
392-
handle_printf(x);
420+
if( check_and_handle_special_function(x, callee) ) {
393421
return true;
394422
}
395423

src/runtime/include/stdlib.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef STDLIB_H
2+
#define STDLIB_H
3+
4+
void exit(int exit_code);
5+
6+
#endif // STDLIB_H

0 commit comments

Comments
 (0)