diff --git a/project/src/transpiler/CMakeLists.txt b/project/src/transpiler/CMakeLists.txt index 2e2b2704..cd6442c9 100755 --- a/project/src/transpiler/CMakeLists.txt +++ b/project/src/transpiler/CMakeLists.txt @@ -54,7 +54,7 @@ set(SOURCE_FILES record_manager.cpp tracer.cpp - ) + process_variables.cpp process_variables.h) # add_executable(recvisitor main.cpp) add_executable(c2eo ${SOURCE_FILES}) diff --git a/project/src/transpiler/aliases.h b/project/src/transpiler/aliases.h index a9b77fd0..0402445b 100644 --- a/project/src/transpiler/aliases.h +++ b/project/src/transpiler/aliases.h @@ -12,12 +12,13 @@ std::set known_types = { "memory", "seq", "$", - "while-loop-label.backward", - "while-loop-label.forward TRUE", "do-while-loop-label-1.forward TRUE", "do-while-loop-label-2.forward TRUE", + "end.forward TRUE", "for-loop-label-1.forward TRUE", "for-loop-label-2.forward TRUE", + "while-loop-label.backward", + "while-loop-label.forward TRUE", }; std::map known_aliases = { diff --git a/project/src/transpiler/memory_manager.cpp b/project/src/transpiler/memory_manager.cpp index 6cc77eec..4b94bf63 100644 --- a/project/src/transpiler/memory_manager.cpp +++ b/project/src/transpiler/memory_manager.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -86,13 +87,21 @@ std::vector::const_iterator MemoryManager::end() const { return variables_.end(); } +template +std::string int_to_hex(T i) { + std::stringstream stream; + stream << "0x" << std::setfill('0') << std::setw(sizeof(T) * 2) << std::hex + << i; + return stream.str(); +} + const Variable &MemoryManager::GetVarById(const VarDecl *id) const { auto res = find_if(variables_.begin(), variables_.end(), [id](const Variable &x) { return x.id == id; }); if (res == variables_.end()) { throw invalid_argument( "exception: element with id " + - to_string(reinterpret_cast( + int_to_hex(reinterpret_cast( id)) // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) + " not found"); } diff --git a/project/src/transpiler/process_variables.cpp b/project/src/transpiler/process_variables.cpp new file mode 100644 index 00000000..2939ecd4 --- /dev/null +++ b/project/src/transpiler/process_variables.cpp @@ -0,0 +1,204 @@ +#include "process_variables.h" + +#include "vardecl.h" + +using namespace std; +using namespace clang; +using namespace llvm; + +extern UnitTranspiler transpiler; + +void ProcessDeclStmt(size_t shift, vector &all_local, + DeclStmt *decl_stmt); + +void ProcessForStmtLocalVariables(vector &all_local, size_t shift, + ForStmt *for_stmt); +void ProcessWhileStmtLocalVariables(vector &all_local, size_t shift, + WhileStmt *while_stmt); +void ProcessCaseStmtLocalVariables(vector &all_local, size_t shift, + CaseStmt *case_stmt); +void ProcessIfStmtLocalVariables(vector &all_local, size_t shift, + IfStmt *if_stmt); +void ProcessDefaultStmtLocalVariables(vector &all_local, size_t shift, + DefaultStmt *default_stmt); +void ProcessSwitchStmtLocalVariables(vector &all_local, size_t shift, + SwitchStmt *switch_stmt); +void ProcessDoStmtLocalVariables(vector &all_local, size_t shift, + DoStmt *do_stmt); +void ProcessStmtLocalVariables(vector &all_local, size_t shift, + Stmt *stmt) { + Stmt::StmtClass stmt_class = stmt->getStmtClass(); + if (stmt_class == Stmt::DeclStmtClass) { + auto *decl_stmt = dyn_cast(stmt); + ProcessDeclStmt(shift, all_local, decl_stmt); + } else if (stmt_class == Stmt::ForStmtClass) { + auto *for_stmt = dyn_cast(stmt); + ProcessForStmtLocalVariables(all_local, shift, for_stmt); + } else if (stmt_class == Stmt::CompoundStmtClass) { + auto *compound_stmt = dyn_cast(stmt); + ProcessFunctionLocalVariables(compound_stmt, all_local, shift); + } else if (stmt_class == Stmt::WhileStmtClass) { + auto *while_stmt = dyn_cast(stmt); + ProcessWhileStmtLocalVariables(all_local, shift, while_stmt); + } else if (stmt_class == Stmt::SwitchStmtClass) { + auto *switch_stmt = dyn_cast(stmt); + ProcessSwitchStmtLocalVariables(all_local, shift, switch_stmt); + } else if (stmt_class == Stmt::DoStmtClass) { + auto *do_stmt = dyn_cast(stmt); + ProcessDoStmtLocalVariables(all_local, shift, do_stmt); + } else if (stmt_class == Stmt::CaseStmtClass) { + auto *case_stmt = dyn_cast(stmt); + ProcessCaseStmtLocalVariables(all_local, shift, case_stmt); + } else if (stmt_class == Stmt::DefaultStmtClass) { + auto *default_stmt = dyn_cast(stmt); + ProcessDefaultStmtLocalVariables(all_local, shift, default_stmt); + } else if (stmt_class == Stmt::IfStmtClass) { + auto *if_stmt = dyn_cast(stmt); + ProcessIfStmtLocalVariables(all_local, shift, if_stmt); + } +} + +void ProcessFunctionLocalVariables(const clang::CompoundStmt *CS, + std::vector &all_local, + size_t shift) { + if (CS == nullptr) { + return; + } + for (auto *stmt : CS->body()) { + ProcessStmtLocalVariables(all_local, shift, stmt); + } +} +void ProcessDoStmtLocalVariables(vector &all_local, size_t shift, + DoStmt *do_stmt) { + if (do_stmt == nullptr) { + return; + } + if (do_stmt->getBody() != nullptr && + do_stmt->getBody()->getStmtClass() == Stmt::CompoundStmtClass) { + auto *compound_stmt = dyn_cast(do_stmt->getBody()); + ProcessFunctionLocalVariables(compound_stmt, all_local, shift); + } +} +void ProcessSwitchStmtLocalVariables(vector &all_local, size_t shift, + SwitchStmt *switch_stmt) { + if (switch_stmt == nullptr) { + return; + } + if (switch_stmt->getBody() != nullptr && + switch_stmt->getBody()->getStmtClass() == Stmt::CompoundStmtClass) { + auto *compound_stmt = dyn_cast(switch_stmt->getBody()); + ProcessFunctionLocalVariables(compound_stmt, all_local, shift); + } +} +void ProcessDefaultStmtLocalVariables(vector &all_local, size_t shift, + DefaultStmt *default_stmt) { + if (default_stmt == nullptr) { + return; + } + if (default_stmt->getSubStmt() != nullptr && + default_stmt->getSubStmt()->getStmtClass() == Stmt::CompoundStmtClass) { + auto *compound_stmt = dyn_cast(default_stmt->getSubStmt()); + ProcessFunctionLocalVariables(compound_stmt, all_local, shift); + } else if (default_stmt->getSubStmt() != nullptr && + default_stmt->getSubStmt()->getStmtClass() == + Stmt::DeclStmtClass) { + auto *decl_stmt = dyn_cast(default_stmt->getSubStmt()); + ProcessDeclStmt(shift, all_local, decl_stmt); + } +} +void ProcessIfStmtLocalVariables(vector &all_local, size_t shift, + IfStmt *if_stmt) { + if (if_stmt == nullptr) { + return; + } + if (if_stmt->getThen() != nullptr) { + ProcessStmtLocalVariables(all_local, shift, if_stmt->getThen()); + } + if (if_stmt->getElse() != nullptr) { + ProcessStmtLocalVariables(all_local, shift, if_stmt->getElse()); + } +} +void ProcessCaseStmtLocalVariables(vector &all_local, size_t shift, + CaseStmt *case_stmt) { + if (case_stmt == nullptr || case_stmt->getSubStmt() == nullptr) { + return; + } + ProcessStmtLocalVariables(all_local, shift, case_stmt->getSubStmt()); +} +void ProcessWhileStmtLocalVariables(vector &all_local, size_t shift, + WhileStmt *while_stmt) { + if (while_stmt == nullptr || while_stmt->getBody() == nullptr) { + return; + } + ProcessStmtLocalVariables(all_local, shift, while_stmt->getBody()); +} +void ProcessForStmtLocalVariables(vector &all_local, size_t shift, + ForStmt *for_stmt) { + if (for_stmt == nullptr) { + return; + } + if (for_stmt->getInit() != nullptr && + for_stmt->getInit()->getStmtClass() == Stmt::DeclStmtClass) { + auto *decl_stmt = dyn_cast(for_stmt->getInit()); + ProcessDeclStmt(shift, all_local, decl_stmt); + } + if (for_stmt->getBody() != nullptr) { + ProcessStmtLocalVariables(all_local, shift, for_stmt->getBody()); + } +} + +void ProcessCompoundStatementLocalVariables(const clang::CompoundStmt *CS, + vector &all_local) { + if (CS == nullptr) { + return; + } + for (auto *stmt : CS->body()) { + Stmt::StmtClass stmt_class = stmt->getStmtClass(); + if (stmt_class == Stmt::DeclStmtClass) { + auto *decl_stmt = dyn_cast(stmt); + if (decl_stmt == nullptr) { + continue; + } + for (auto *decl : decl_stmt->decls()) { + Decl::Kind decl_kind = decl->getKind(); + if (decl_kind == Decl::Var) { + auto *var_decl = dyn_cast(decl); + all_local.push_back(transpiler.glob_.GetVarById(var_decl)); + } + } + } else if (stmt_class == Stmt::ForStmtClass) { + auto *for_stmt = dyn_cast(stmt); + if (for_stmt == nullptr) { + continue; + } + if (for_stmt->getInit() != nullptr && + for_stmt->getInit()->getStmtClass() == Stmt::DeclStmtClass) { + auto *decl_stmt = dyn_cast(for_stmt->getInit()); + if (decl_stmt == nullptr) { + continue; + } + for (auto *decl : decl_stmt->decls()) { + Decl::Kind decl_kind = decl->getKind(); + if (decl_kind == Decl::Var) { + auto *var_decl = dyn_cast(decl); + all_local.push_back(transpiler.glob_.GetVarById(var_decl)); + } + } + } + } + } +} + +void ProcessDeclStmt(size_t shift, vector &all_local, + DeclStmt *decl_stmt) { + if (decl_stmt == nullptr) { + return; + } + for (auto *decl : decl_stmt->decls()) { + Decl::Kind decl_kind = decl->getKind(); + if (decl_kind == Decl::Var) { + auto *var_decl = dyn_cast(decl); + all_local.push_back(ProcessVariable(var_decl, "local-start", shift)); + } + } +} diff --git a/project/src/transpiler/process_variables.h b/project/src/transpiler/process_variables.h new file mode 100644 index 00000000..94e2c289 --- /dev/null +++ b/project/src/transpiler/process_variables.h @@ -0,0 +1,12 @@ +#ifndef C2EO_SRC_TRANSPILER_PROCESS_VARIABLES_H_ +#define C2EO_SRC_TRANSPILER_PROCESS_VARIABLES_H_ +#include "memory_manager.h" +#include "unit_transpiler.h" + +void ProcessFunctionLocalVariables(const clang::CompoundStmt* CS, + std::vector& all_local, + size_t shift); + +void ProcessCompoundStatementLocalVariables(const clang::CompoundStmt* CS, + std::vector& all_local); +#endif // C2EO_SRC_TRANSPILER_PROCESS_VARIABLES_H_ diff --git a/project/src/transpiler/transpile_helper.cpp b/project/src/transpiler/transpile_helper.cpp index 75121e99..72ac2bc6 100644 --- a/project/src/transpiler/transpile_helper.cpp +++ b/project/src/transpiler/transpile_helper.cpp @@ -5,6 +5,7 @@ #include #include "memory_manager.h" +#include "process_variables.h" #include "recorddecl.h" #include "unit_transpiler.h" #include "vardecl.h" @@ -13,9 +14,6 @@ using namespace clang; using namespace llvm; using namespace std; -vector ProcessFunctionLocalVariables(const clang::CompoundStmt *CS, - size_t shift); - EOObject GetBinaryStmtEOObject(const BinaryOperator *p_operator); EOObject GetAssignmentOperationOperatorEOObject( @@ -69,16 +67,10 @@ uint64_t GetTypeSize(QualType qual_type); EOObject GetCastEOObject(const CastExpr *op); -EOObject GetGotoForWhileEO(const EOObject &while_eo_object); - -void ProcessDeclStmt(size_t shift, vector &all_local, - DeclStmt *decl_stmt); - -vector ProcessCompoundStatementLocalVariables( - const clang::CompoundStmt *CS); - +EOObject GetSwitchEOObject(const SwitchStmt *p_stmt); +EOObject GetCaseCondEOObject(const vector &all_cases, + const EOObject &switch_exp, size_t i); extern UnitTranspiler transpiler; -int loop_level = 0; EOObject GetFunctionBody(const clang::FunctionDecl *FD) { if (!FD->hasBody()) { @@ -92,8 +84,9 @@ EOObject GetFunctionBody(const clang::FunctionDecl *FD) { size_t param_memory_size = GetParamMemorySize(FD->parameters()); vector all_param = ProcessFunctionParams(FD->parameters(), shift); vector all_types = PrecessRecordTypes(func_body); - vector all_local = - ProcessFunctionLocalVariables(func_body, shift + param_memory_size); + vector all_local; + ProcessFunctionLocalVariables(func_body, all_local, + shift + param_memory_size); EOObject func_body_eo = EOObject(EOObjectType::EO_EMPTY); EOObject local_start("add", "local-start"); local_start.nested.emplace_back("param-start"); @@ -173,122 +166,6 @@ vector ProcessFunctionParams(ArrayRef params, return all_params; } -vector ProcessFunctionLocalVariables(const clang::CompoundStmt *CS, - size_t shift) { - vector all_local; - if (CS == nullptr) { - return all_local; - } - for (auto *stmt : CS->body()) { - Stmt::StmtClass stmt_class = stmt->getStmtClass(); - if (stmt_class == Stmt::DeclStmtClass) { - auto *decl_stmt = dyn_cast(stmt); - ProcessDeclStmt(shift, all_local, decl_stmt); - } else if (stmt_class == Stmt::ForStmtClass) { - auto *for_stmt = dyn_cast(stmt); - if (for_stmt == nullptr) { - continue; - } - if (for_stmt->getInit() != nullptr && - for_stmt->getInit()->getStmtClass() == Stmt::DeclStmtClass) { - auto *decl_stmt = dyn_cast(for_stmt->getInit()); - ProcessDeclStmt(shift, all_local, decl_stmt); - } - if (for_stmt->getBody() != nullptr && - for_stmt->getBody()->getStmtClass() == Stmt::CompoundStmtClass) { - auto *compound_stmt = dyn_cast(for_stmt->getBody()); - auto res = ProcessFunctionLocalVariables(compound_stmt, shift); - all_local.insert(all_local.end(), res.begin(), res.end()); - } - } else if (stmt_class == Stmt::CompoundStmtClass) { - auto *compound_stmt = dyn_cast(stmt); - auto res = ProcessFunctionLocalVariables(compound_stmt, shift); - all_local.insert(all_local.end(), res.begin(), res.end()); - } else if (stmt_class == Stmt::WhileStmtClass) { - auto *while_stmt = dyn_cast(stmt); - if (while_stmt == nullptr) { - continue; - } - if (while_stmt->getBody() != nullptr && - while_stmt->getBody()->getStmtClass() == Stmt::CompoundStmtClass) { - auto *compound_stmt = dyn_cast(while_stmt->getBody()); - auto res = ProcessFunctionLocalVariables(compound_stmt, shift); - all_local.insert(all_local.end(), res.begin(), res.end()); - } - } else if (stmt_class == Stmt::DoStmtClass) { - auto *do_stmt = dyn_cast(stmt); - if (do_stmt == nullptr) { - continue; - } - if (do_stmt->getBody() != nullptr && - do_stmt->getBody()->getStmtClass() == Stmt::CompoundStmtClass) { - auto *compound_stmt = dyn_cast(do_stmt->getBody()); - auto res = ProcessFunctionLocalVariables(compound_stmt, shift); - all_local.insert(all_local.end(), res.begin(), res.end()); - } - } - } - return all_local; -} - -vector ProcessCompoundStatementLocalVariables( - const clang::CompoundStmt *CS) { - vector all_local; - if (CS == nullptr) { - return all_local; - } - for (auto *stmt : CS->body()) { - Stmt::StmtClass stmt_class = stmt->getStmtClass(); - if (stmt_class == Stmt::DeclStmtClass) { - auto *decl_stmt = dyn_cast(stmt); - if (decl_stmt == nullptr) { - continue; - } - for (auto *decl : decl_stmt->decls()) { - Decl::Kind decl_kind = decl->getKind(); - if (decl_kind == Decl::Var) { - auto *var_decl = dyn_cast(decl); - all_local.push_back(transpiler.glob_.GetVarById(var_decl)); - } - } - } else if (stmt_class == Stmt::ForStmtClass) { - auto *for_stmt = dyn_cast(stmt); - if (for_stmt == nullptr) { - continue; - } - if (for_stmt->getInit() != nullptr && - for_stmt->getInit()->getStmtClass() == Stmt::DeclStmtClass) { - auto *decl_stmt = dyn_cast(for_stmt->getInit()); - if (decl_stmt == nullptr) { - continue; - } - for (auto *decl : decl_stmt->decls()) { - Decl::Kind decl_kind = decl->getKind(); - if (decl_kind == Decl::Var) { - auto *var_decl = dyn_cast(decl); - all_local.push_back(transpiler.glob_.GetVarById(var_decl)); - } - } - } - } - } - return all_local; -} - -void ProcessDeclStmt(size_t shift, vector &all_local, - DeclStmt *decl_stmt) { - if (decl_stmt == nullptr) { - return; - } - for (auto *decl : decl_stmt->decls()) { - Decl::Kind decl_kind = decl->getKind(); - if (decl_kind == Decl::Var) { - auto *var_decl = dyn_cast(decl); - all_local.push_back(ProcessVariable(var_decl, "local-start", shift)); - } - } -} - // Function to get eo representation of CompoundStmt EOObject GetCompoundStmt(const clang::CompoundStmt *CS, bool is_decorator = false) { @@ -296,7 +173,8 @@ EOObject GetCompoundStmt(const clang::CompoundStmt *CS, if (is_decorator) { res.postfix = "@"; } - auto all_local_in_block = ProcessCompoundStatementLocalVariables(CS); + vector all_local_in_block; + ProcessCompoundStatementLocalVariables(CS, all_local_in_block); auto pos_it = res.nested.begin(); for (const auto &var : all_local_in_block) { if (var.is_initialized) { @@ -443,12 +321,118 @@ EOObject GetStmtEOObject(const Stmt *stmt) { if (stmt_class == Stmt::ContinueStmtClass) { return EOObject{"continue"}; } + if (stmt_class == Stmt::SwitchStmtClass) { + const auto *op = dyn_cast(stmt); + return GetSwitchEOObject(op); + } + if (stmt_class == Stmt::ConstantExprClass) { + const auto *op = dyn_cast(stmt); + return GetStmtEOObject(op->getSubExpr()); + } llvm::errs() << "Warning: Unknown statement " << stmt->getStmtClassName() << "\n"; return EOObject(EOObjectType::EO_PLUG); } +EOObject GetSwitchEOObject(const SwitchStmt *p_stmt) { + EOObject goto_object{"goto"}; + EOObject return_label{EOObjectType::EO_ABSTRACT}; + return_label.arguments.emplace_back("end"); + + return_label.nested.emplace_back("end.forward TRUE", "break"); + return_label.nested.emplace_back("memory", "flag"); + + EOObject seq_object{"seq", "@"}; + EOObject init_flag_object{"write"}; + init_flag_object.nested.emplace_back("flag"); + init_flag_object.nested.emplace_back("0", EOObjectType::EO_LITERAL); + EOObject set_flag_object{"write"}; + set_flag_object.nested.emplace_back("flag"); + set_flag_object.nested.emplace_back("1", EOObjectType::EO_LITERAL); + seq_object.nested.push_back(init_flag_object); + const auto *switch_init = p_stmt->getInit(); + if (switch_init != nullptr) { + seq_object.nested.push_back(GetStmtEOObject(switch_init)); + } + + EOObject switch_expr_object = GetStmtEOObject(p_stmt->getCond()); + + // TODO if get body return null... + auto end = p_stmt->getBody()->child_end(); + for (auto stmt = p_stmt->getBody()->child_begin(); stmt != end; ++stmt) { + if ((*stmt)->getStmtClass() == Stmt::CaseStmtClass) { + const auto *case_stmt = dyn_cast(*stmt); + EOObject if_obj{"if"}; + vector all_cases{case_stmt->getLHS()}; + const auto *nested = case_stmt->getSubStmt(); + while (nested != nullptr && + nested->getStmtClass() == Stmt::CaseStmtClass) { + const auto *nested_case = dyn_cast(nested); + all_cases.push_back(nested_case->getLHS()); + nested = nested_case->getSubStmt(); + } + EOObject cond_obj{"or"}; + EOObject eq_obj{"eq"}; + cond_obj.nested.emplace_back("flag"); + eq_obj.nested.push_back(switch_expr_object); + eq_obj.nested.push_back(GetStmtEOObject(case_stmt->getLHS())); + cond_obj.nested.push_back( + GetCaseCondEOObject(all_cases, switch_expr_object, 0)); + if_obj.nested.push_back(cond_obj); + EOObject buffer_obj{"seq"}; + if (nested != nullptr) { + buffer_obj.nested.push_back(GetStmtEOObject(nested)); + } + auto tmp = stmt; + tmp++; + while (tmp != end && (*tmp)->getStmtClass() != Stmt::CaseStmtClass && + (*tmp)->getStmtClass() != Stmt::DefaultStmtClass) { + buffer_obj.nested.push_back(GetStmtEOObject(*tmp)); + tmp++; + } + buffer_obj.nested.push_back(set_flag_object); + buffer_obj.nested.emplace_back("TRUE", EOObjectType::EO_LITERAL); + if_obj.nested.push_back(buffer_obj); + seq_object.nested.push_back(if_obj); + } else if ((*stmt)->getStmtClass() == Stmt::DefaultStmtClass) { + const auto *default_stmt = dyn_cast(*stmt); + EOObject buffer_obj{"seq"}; + if (default_stmt->getSubStmt() != nullptr) { + buffer_obj.nested.push_back( + GetStmtEOObject(default_stmt->getSubStmt())); + } + auto tmp = stmt; + tmp++; + while (tmp != end && (*tmp)->getStmtClass() != Stmt::CaseStmtClass && + (*tmp)->getStmtClass() != Stmt::DefaultStmtClass) { + buffer_obj.nested.push_back(GetStmtEOObject(*tmp)); + tmp++; + } + buffer_obj.nested.push_back(set_flag_object); + buffer_obj.nested.emplace_back("TRUE", EOObjectType::EO_LITERAL); + seq_object.nested.push_back(buffer_obj); + } + } + return_label.nested.push_back(seq_object); + goto_object.nested.push_back(return_label); + + return goto_object; +} +EOObject GetCaseCondEOObject(const vector &all_cases, + const EOObject &switch_exp, size_t i) { + EOObject eq_object{"eq"}; + eq_object.nested.push_back(switch_exp); + eq_object.nested.push_back(GetStmtEOObject(all_cases[i])); + if (i + 1 == all_cases.size()) { + return eq_object; + } + EOObject or_object{"or"}; + or_object.nested.push_back(eq_object); + or_object.nested.push_back(GetCaseCondEOObject(all_cases, switch_exp, i + 1)); + return or_object; +} + EOObject GetCastEOObject(const CastExpr *op) { if (op == nullptr) { return EOObject{EOObjectType::EO_PLUG}; @@ -673,7 +657,11 @@ EOObject GetMemberExprEOObject(const MemberExpr *op) { EOObject GetFunctionCallEOObject(const CallExpr *op) { EOObject call("seq"); vector var_sizes; - if (op != nullptr) { + if (op->getDirectCallee()->isCXXClassMember() || + op->getDirectCallee()->isCXXInstanceMember()) { + return EOObject{EOObjectType::EO_PLUG}; + } + if (op != nullptr && op->getDirectCallee() != nullptr) { for (auto *VD : op->getDirectCallee()->parameters()) { TypeInfo type_info = VD->getASTContext().getTypeInfo(VD->getType()); size_t type_size = type_info.Width / byte_size; @@ -682,7 +670,7 @@ EOObject GetFunctionCallEOObject(const CallExpr *op) { } size_t shift = 0; int i = 0; - if (op != nullptr) { + if (op != nullptr && op->getNumArgs() <= var_sizes.size()) { for (const auto *arg : op->arguments()) { EOObject param{"write"}; EOObject address{"address"}; @@ -1058,17 +1046,6 @@ EOObject GetIfStmtEOObject(const IfStmt *p_stmt) { return EOObject{EOObjectType::EO_PLUG}; } -EOObject GetGotoForWhileEO(const EOObject &while_eo_object) { - EOObject goto_object{"goto"}; - EOObject return_label{EOObjectType::EO_ABSTRACT}; - return_label.arguments.emplace_back("goto-loop-label" + - to_string(loop_level)); - return_label.nested.push_back(while_eo_object); - goto_object.nested.push_back(return_label); - loop_level--; - return goto_object; -} - EOObject GetWhileStmtEOObject(const WhileStmt *p_stmt) { if (p_stmt == nullptr) { return EOObject{EOObjectType::EO_PLUG}; diff --git a/project/src/transpiler/vardecl.cpp b/project/src/transpiler/vardecl.cpp index fd74e429..39a88034 100755 --- a/project/src/transpiler/vardecl.cpp +++ b/project/src/transpiler/vardecl.cpp @@ -95,6 +95,9 @@ EOObject InitValueAnalysis(const VarDecl *VD) { auto float_value = init_val->getFloat().convertToFloat(); str = std::to_string(float_value); } + if (str.empty()) { + return EOObject{EOObjectType::EO_PLUG}; + } return {str, EOObjectType::EO_LITERAL}; } diff --git a/project/tests/main/switch/switch01.c b/project/tests/main/switch/switch01.c new file mode 100644 index 00000000..bea9568c --- /dev/null +++ b/project/tests/main/switch/switch01.c @@ -0,0 +1,21 @@ +//clang -Xclang -ast-dump -fsyntax-only test07.c +#include "stdio.h" +long long n = 5; +int main() { + + long long x; + long long test = 42; + switch (n) { + case 1: + x = 1; + break; + case 5: + x = 5; + break; + default: + x = 13; + break ; + } + printf("%lld",x); + return 0; +} \ No newline at end of file diff --git a/project/tests/main/switch/switch02.c b/project/tests/main/switch/switch02.c new file mode 100644 index 00000000..75844734 --- /dev/null +++ b/project/tests/main/switch/switch02.c @@ -0,0 +1,28 @@ +//clang -Xclang -ast-dump -fsyntax-only test07.c +#include "stdio.h" +long long n = 5; +int main() { + for (long long i = 0; i < 10; ++i) { + long long x=0; + switch (i) { + case 1: + x += 1; + break; + case 2: + case 3: + x += 2; + break; + case 4: + x+= 4; + case 5: + x+= 8; + break; + default: + x +=100; + break ; + } + printf("%lld\n",x); + } + + return 0; +} \ No newline at end of file diff --git a/project/tests/main/switch/switch03.c b/project/tests/main/switch/switch03.c new file mode 100644 index 00000000..6189cbe7 --- /dev/null +++ b/project/tests/main/switch/switch03.c @@ -0,0 +1,20 @@ +//clang -Xclang -ast-dump -fsyntax-only test07.c +#include "stdio.h" +long long n = 4; +int main() { + long long x; + switch (n) { + case 1: + x = 1; + break; + case 4: + case 5: + x = 5; + break; + default: + x = 13; + break ; + } + printf("%lld",x); + return 0; +} \ No newline at end of file diff --git a/project/tests/main/switch/switch04.c b/project/tests/main/switch/switch04.c new file mode 100644 index 00000000..c4386cb5 --- /dev/null +++ b/project/tests/main/switch/switch04.c @@ -0,0 +1,21 @@ +//clang -Xclang -ast-dump -fsyntax-only test07.c +#include "stdio.h" +long long n = 4; +int main() { + long long x; + switch (n) { + case 1: + x = 1; + break; + case 4: + x = 1; + case 5: + x += 5; + break; + default: + x = 13; + break ; + } + printf("%lld",x); + return 0; +} \ No newline at end of file diff --git a/project/tests/main/switch/switch05.c b/project/tests/main/switch/switch05.c new file mode 100644 index 00000000..f8b62251 --- /dev/null +++ b/project/tests/main/switch/switch05.c @@ -0,0 +1,19 @@ +//clang -Xclang -ast-dump -fsyntax-only test07.c +#include "stdio.h" +long long n = 5; +int main() { + long long x; + switch (n) { + case 1: + x = 1; + break; + case 4: + case 5: + x = 5; + default: + x += 13; + break ; + } + printf("%lld",x); + return 0; +} \ No newline at end of file diff --git a/project/tests/parsing/gcc.c-torture/compile/limits-caselabels.c b/project/tests/parsing/gcc.c-torture/compile/limits-caselabels.c index 08e8195b..ef22c4f5 100755 --- a/project/tests/parsing/gcc.c-torture/compile/limits-caselabels.c +++ b/project/tests/parsing/gcc.c-torture/compile/limits-caselabels.c @@ -15,7 +15,7 @@ void q19_func (long i) { switch (i) { - LIM5 (case 1) + LIM3 (case 1) break; } } diff --git a/project/tests/parsing/gcc.c-torture/execute/20030714-1.c b/project/tests/parsing/gcc.c-torture/execute/20030714-1.c deleted file mode 100755 index 719baede..00000000 --- a/project/tests/parsing/gcc.c-torture/execute/20030714-1.c +++ /dev/null @@ -1,193 +0,0 @@ -/* derived from PR optimization/11440 */ - -extern void abort (void); -extern void exit (int); - -typedef _Bool bool; -const bool false = 0; -const bool true = 1; - -enum EPosition -{ - STATIC, RELATIVE, ABSOLUTE, FIXED -}; -typedef enum EPosition EPosition; - -enum EFloat -{ - FNONE = 0, FLEFT, FRIGHT -}; -typedef enum EFloat EFloat; - -struct RenderBox -{ - int unused[6]; - short m_verticalPosition; - - bool m_layouted : 1; - bool m_unused : 1; - bool m_minMaxKnown : 1; - bool m_floating : 1; - - bool m_positioned : 1; - bool m_overhangingContents : 1; - bool m_relPositioned : 1; - bool m_paintSpecial : 1; - - bool m_isAnonymous : 1; - bool m_recalcMinMax : 1; - bool m_isText : 1; - bool m_inline : 1; - - bool m_replaced : 1; - bool m_mouseInside : 1; - bool m_hasFirstLine : 1; - bool m_isSelectionBorder : 1; - - bool (*isTableCell) (struct RenderBox *this); -}; - -typedef struct RenderBox RenderBox; - -struct RenderStyle -{ - struct NonInheritedFlags - { - union - { - struct - { - unsigned int _display : 4; - unsigned int _bg_repeat : 2; - bool _bg_attachment : 1; - unsigned int _overflow : 4 ; - unsigned int _vertical_align : 4; - unsigned int _clear : 2; - EPosition _position : 2; - EFloat _floating : 2; - unsigned int _table_layout : 1; - bool _flowAroundFloats :1; - - unsigned int _styleType : 3; - bool _hasHover : 1; - bool _hasActive : 1; - bool _clipSpecified : 1; - unsigned int _unicodeBidi : 2; - int _unused : 1; - } f; - int _niflags; - }; - } noninherited_flags; -}; - -typedef struct RenderStyle RenderStyle; - -extern void RenderObject_setStyle(RenderBox *this, RenderStyle *_style); -extern void removeFromSpecialObjects(RenderBox *this); - - - -void RenderBox_setStyle(RenderBox *thisin, RenderStyle *_style) -{ - RenderBox *this = thisin; - bool oldpos, tmp; - EPosition tmppo; - - tmp = this->m_positioned; - - oldpos = tmp; - - RenderObject_setStyle(this, _style); - - tmppo = _style->noninherited_flags.f._position; - - switch(tmppo) - { - case ABSOLUTE: - case FIXED: - { - bool ltrue = true; - this->m_positioned = ltrue; - break; - } - - default: - { - EFloat tmpf; - EPosition tmpp; - if (oldpos) - { - bool ltrue = true; - this->m_positioned = ltrue; - removeFromSpecialObjects(this); - } - { - bool lfalse = false; - this->m_positioned = lfalse; - } - - tmpf = _style->noninherited_flags.f._floating; - - if(!this->isTableCell (this) && !(tmpf == FNONE)) - { - bool ltrue = true; - this->m_floating = ltrue; - } - else - { - tmpp = _style->noninherited_flags.f._position; - if (tmpp == RELATIVE) - { - bool ltrue = true; - this->m_relPositioned = ltrue; - } - } - } - } -} - - - - -RenderBox g_this; -RenderStyle g__style; - -void RenderObject_setStyle(RenderBox *this, RenderStyle *_style) -{ - (void) this; - (void) _style; -} - -void removeFromSpecialObjects(RenderBox *this) -{ - (void) this; -} - -bool RenderBox_isTableCell (RenderBox *this) -{ - (void) this; - return false; -} - -int main (void) -{ - - g_this.m_relPositioned = false; - g_this.m_positioned = false; - g_this.m_floating = false; - g_this.isTableCell = RenderBox_isTableCell; - - g__style.noninherited_flags.f._position = FIXED; - g__style.noninherited_flags.f._floating = FNONE; - - RenderBox_setStyle (&g_this, &g__style); - - if (g_this.m_positioned != true) - abort (); - if (g_this.m_relPositioned != false) - abort (); - if (g_this.m_floating != false) - abort (); - - exit (0); -}