Changeset c6b4432 for src/Common
- Timestamp:
- Nov 8, 2023, 2:01:11 PM (7 months ago)
- Branches:
- master
- Children:
- 3e4bf0d, f5ec35a
- Parents:
- 790d835
- Location:
- src/Common
- Files:
-
- 4 deleted
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Common/Eval.cc
r790d835 rc6b4432 19 19 20 20 #include "AST/Inspect.hpp" 21 #include "Common/PassVisitor.h"22 21 #include "CodeGen/OperatorTable.h" // access: OperatorInfo 23 22 #include "AST/Pass.hpp" 24 23 #include "InitTweak/InitTweak.h" 25 #include "SynTree/Expression.h" 26 27 //------------------------------------------------------------- 28 // Old AST 29 struct EvalOld : public WithShortCircuiting { 30 long long int value = 0; // compose the result of the constant expression 31 bool valid = true; // true => constant expression and value is the result 32 // false => not constant expression, e.g., ++i 33 bool cfavalid = true; // true => constant expression and value computable 34 // false => constant expression but value not computable, e.g., sizeof(int) 35 36 void previsit( const BaseSyntaxNode * ) { visit_children = false; } 37 void postvisit( const BaseSyntaxNode * ) { valid = false; } 38 39 void postvisit( const SizeofExpr * ) { 40 } 41 42 void postvisit( const ConstantExpr * expr ) { 43 value = expr->intValue(); 44 } 45 46 void postvisit( const CastExpr * expr ) { 47 auto arg = eval(expr->arg); 48 valid = arg.second; 49 value = arg.first; 50 // TODO: perform type conversion on value if valid 51 } 52 53 void postvisit( const VariableExpr * const expr ) { 54 if ( EnumInstType * inst = dynamic_cast<EnumInstType *>(expr->result) ) { 55 if ( EnumDecl * decl = inst->baseEnum ) { 56 if ( decl->valueOf( expr->var, value ) ) { // value filled by valueOf 57 return; 58 } 59 } 60 } 61 valid = false; 62 } 63 64 void postvisit( const ApplicationExpr * expr ) { 65 DeclarationWithType * function = InitTweak::getFunction(const_cast<ApplicationExpr *>(expr)); 66 if ( ! function || function->linkage != LinkageSpec::Intrinsic ) { valid = false; return; } 67 const std::string & fname = function->name; 68 assertf( expr->args.size() == 1 || expr->args.size() == 2, "Intrinsic function with %zd arguments: %s", expr->args.size(), fname.c_str() ); 69 std::pair<long long int, bool> arg1, arg2; 70 arg1 = eval(expr->args.front()); 71 valid = valid && arg1.second; 72 if ( ! valid ) return; 73 if ( expr->args.size() == 2 ) { 74 arg2 = eval(expr->args.back()); 75 valid = valid && arg2.second; 76 if ( ! valid ) return; 77 } 78 if (fname == "?+?") { 79 value = arg1.first + arg2.first; 80 } else if (fname == "?-?") { 81 value = arg1.first - arg2.first; 82 } else if (fname == "?*?") { 83 value = arg1.first * arg2.first; 84 } else if (fname == "?/?") { 85 value = arg1.first / arg2.first; 86 } else if (fname == "?%?") { 87 value = arg1.first % arg2.first; 88 } else { 89 valid = false; 90 } 91 // TODO: implement other intrinsic functions 92 } 93 }; 94 95 //------------------------------------------------------------- 96 // New AST 24 97 25 struct EvalNew : public ast::WithShortCircuiting { 98 26 Evaluation result = { 0, true, true }; … … 270 198 }; 271 199 272 std::pair<long long int, bool> eval( const Expression * expr ) {273 PassVisitor<EvalOld> ev;274 if ( expr ) {275 expr->accept( ev );276 return std::make_pair( ev.pass.value, ev.pass.valid );277 } else {278 return std::make_pair( 0, false );279 }280 }281 282 200 Evaluation eval( const ast::Expr * expr ) { 283 201 if ( expr ) { -
src/Common/Eval.h
r790d835 rc6b4432 30 30 31 31 /// Evaluates expr as a long long int. 32 /// If second is false, expr could not be evaluated.33 std::pair<long long int, bool> eval(const Expression * expr);34 32 Evaluation eval(const ast::Expr * expr); 35 33 -
src/Common/Examine.cc
r790d835 rc6b4432 19 19 #include "CodeGen/OperatorTable.h" 20 20 #include "InitTweak/InitTweak.h" 21 22 DeclarationWithType * isMainFor( FunctionDecl * func, AggregateDecl::Aggregate kind ) {23 if (func->name != "main") return nullptr;24 if (func->type->parameters.size() != 1) return nullptr;25 26 auto param = func->type->parameters.front();27 28 auto type = dynamic_cast<ReferenceType * >(param->get_type());29 if (!type) return nullptr;30 31 auto obj = dynamic_cast<StructInstType *>(type->base);32 if (!obj) return nullptr;33 34 if (kind != obj->baseStruct->kind) return nullptr;35 36 return param;37 }38 21 39 22 namespace { … … 69 52 70 53 namespace { 71 Type * getDestructorParam( FunctionDecl * func ) {72 if ( !CodeGen::isDestructor( func->name ) ) return nullptr;73 74 auto params = func->type->parameters;75 if ( 1 != params.size() ) return nullptr;76 77 auto ref = dynamic_cast<ReferenceType *>( params.front()->get_type() );78 if ( ref ) {79 return ref->base;80 }81 return nullptr;82 }83 54 84 55 const ast::Type * getDestructorParam( const ast::FunctionDecl * func ) { … … 88 59 } 89 60 90 }91 92 bool isDestructorFor( FunctionDecl * func, StructDecl * type_decl ) {93 if ( Type * type = getDestructorParam( func ) ) {94 auto stype = dynamic_cast<StructInstType *>( type );95 return stype && stype->baseStruct == type_decl;96 }97 return false;98 61 } 99 62 -
src/Common/Examine.h
r790d835 rc6b4432 15 15 16 16 #include "AST/Decl.hpp" 17 #include "SynTree/Declaration.h"18 17 19 18 /// Check if this is a main function for a type of an aggregate kind. 20 DeclarationWithType * isMainFor( FunctionDecl * func, AggregateDecl::Aggregate kind );21 19 const ast::DeclWithType * isMainFor( 22 20 const ast::FunctionDecl * func, ast::AggregateDecl::Aggregate kind ); … … 24 22 25 23 /// Check if this function is a destructor for the given structure. 26 bool isDestructorFor( FunctionDecl * func, StructDecl * type_decl );27 24 bool isDestructorFor( 28 25 const ast::FunctionDecl * func, const ast::StructDecl * type ); -
src/Common/module.mk
r790d835 rc6b4432 31 31 Common/Indenter.cc \ 32 32 Common/Iterate.hpp \ 33 Common/PassVisitor.cc \34 Common/PassVisitor.h \35 Common/PassVisitor.impl.h \36 Common/PassVisitor.proto.h \37 33 Common/PersistentMap.h \ 38 34 Common/ResolvProtoDump.hpp \
Note: See TracChangeset
for help on using the changeset viewer.