Changeset d7aa12c


Ignore:
Timestamp:
May 23, 2019, 11:55:32 AM (5 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
b5fed34
Parents:
9d6e7fa9
Message:

Implemented eval for new AST

Location:
src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/Common/Eval.cc

    r9d6e7fa9 rd7aa12c  
    1717
    1818#include "Common/PassVisitor.h"
     19#include "AST/Pass.hpp"
    1920#include "InitTweak/InitTweak.h"
    2021#include "SynTree/Expression.h"
    2122
    22 struct Eval : public WithShortCircuiting {
     23//-------------------------------------------------------------
     24// Old AST
     25struct EvalOld : public WithShortCircuiting {
    2326        long long int value = 0;
    2427        bool valid = true;
     
    8083};
    8184
     85//-------------------------------------------------------------
     86// New AST
     87struct EvalNew : public ast::WithShortCircuiting {
     88        long long int value = 0;
     89        bool valid = true;
     90
     91        void previsit( const ast::Node * ) { visit_children = false; }
     92        void postvisit( const ast::Node * ) { valid = false; }
     93
     94        void postvisit( const ast::ConstantExpr * expr ) {
     95                value = expr->intValue();
     96        }
     97
     98        void postvisit( const ast::CastExpr * expr ) {
     99                auto arg = eval(expr->arg);
     100                valid = arg.second;
     101                value = arg.first;
     102                // TODO: perform type conversion on value if valid
     103        }
     104
     105        void postvisit( const ast::VariableExpr * expr ) {
     106                if ( const ast::EnumInstType * inst = dynamic_cast<const ast::EnumInstType *>(expr->result.get()) ) {
     107                        if ( const ast::EnumDecl * decl = inst->base ) {
     108                                if ( decl->valueOf( expr->var, value ) ) { // value filled by valueOf
     109                                        return;
     110                                }
     111                        }
     112                }
     113                valid = false;
     114        }
     115
     116        void postvisit( const ast::ApplicationExpr * expr ) {
     117                const ast::DeclWithType * function = InitTweak::getFunction(expr);
     118                if ( ! function || function->linkage != ast::Linkage::Intrinsic ) { valid = false; return; }
     119                const std::string & fname = function->name;
     120                assertf( expr->args.size() == 1 || expr->args.size() == 2, "Intrinsic function with %zd arguments: %s", expr->args.size(), fname.c_str() );
     121                std::pair<long long int, bool> arg1, arg2;
     122                arg1 = eval(expr->args.front());
     123                valid = valid && arg1.second;
     124                if ( ! valid ) return;
     125                if ( expr->args.size() == 2 ) {
     126                        arg2 = eval(expr->args.back());
     127                        valid = valid && arg2.second;
     128                        if ( ! valid ) return;
     129                }
     130                if (fname == "?+?") {
     131                        value = arg1.first + arg2.first;
     132                } else if (fname == "?-?") {
     133                        value = arg1.first - arg2.first;
     134                } else if (fname == "?*?") {
     135                        value = arg1.first * arg2.first;
     136                } else if (fname == "?/?") {
     137                        value = arg1.first / arg2.first;
     138                } else if (fname == "?%?") {
     139                        value = arg1.first % arg2.first;
     140                } else {
     141                        valid = false;
     142                }
     143                // TODO: implement other intrinsic functions
     144        }
     145};
     146
    82147std::pair<long long int, bool> eval(Expression * expr) {
    83         PassVisitor<Eval> ev;
     148        PassVisitor<EvalOld> ev;
    84149        if (expr) {
    85150                expr->accept(ev);
     
    91156
    92157std::pair<long long int, bool> eval(const ast::Expr * expr) {
    93         #warning not implemented
    94         return { 0, false };
     158        ast::Pass<EvalNew> ev;
     159        if (expr) {
     160                expr->accept(ev);
     161                return std::make_pair(ev.pass.value, ev.pass.valid);
     162        } else {
     163                return std::make_pair(0, false);
     164        }
    95165}
    96166
  • src/InitTweak/InitTweak.cc

    r9d6e7fa9 rd7aa12c  
    346346        namespace {
    347347                DeclarationWithType * getCalledFunction( Expression * expr );
     348                const ast::DeclWithType * getCalledFunction( const ast::Expr * expr );
    348349
    349350                template<typename CallExpr>
     
    355356                        return getCalledFunction( expr->get_args().front() );
    356357                }
     358
     359                template<typename CallExpr>
     360                const ast::DeclWithType * handleDerefCalledFunction( const CallExpr * expr ) {
     361                        // (*f)(x) => should get "f"
     362                        std::string name = getFunctionName( expr );
     363                        assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() );
     364                        assertf( ! expr->args.empty(), "Cannot get called function from dereference with no arguments" );
     365                        return getCalledFunction( expr->args.front() );
     366                }
     367
    357368
    358369                DeclarationWithType * getCalledFunction( Expression * expr ) {
     
    375386                        return nullptr;
    376387                }
     388
     389                const ast::DeclWithType * getCalledFunction( const ast::Expr * expr ) {
     390                        assert( expr );
     391                        if ( const ast::VariableExpr * varExpr = dynamic_cast< const ast::VariableExpr * >( expr ) ) {
     392                                return varExpr->var;
     393                        } else if ( const ast::MemberExpr * memberExpr = dynamic_cast< const ast::MemberExpr * >( expr ) ) {
     394                                return memberExpr->member;
     395                        } else if ( const ast::CastExpr * castExpr = dynamic_cast< const ast::CastExpr * >( expr ) ) {
     396                                return getCalledFunction( castExpr->arg );
     397                        } else if ( const ast::UntypedExpr * untypedExpr = dynamic_cast< const ast::UntypedExpr * >( expr ) ) {
     398                                return handleDerefCalledFunction( untypedExpr );
     399                        } else if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * > ( expr ) ) {
     400                                return handleDerefCalledFunction( appExpr );
     401                        } else if ( const ast::AddressExpr * addrExpr = dynamic_cast< const ast::AddressExpr * >( expr ) ) {
     402                                return getCalledFunction( addrExpr->arg );
     403                        } else if ( const ast::CommaExpr * commaExpr = dynamic_cast< const ast::CommaExpr * >( expr ) ) {
     404                                return getCalledFunction( commaExpr->arg2 );
     405                        }
     406                        return nullptr;
     407                }
    377408        }
    378409
     
    382413                } else if ( UntypedExpr * untyped = dynamic_cast< UntypedExpr * > ( expr ) ) {
    383414                        return getCalledFunction( untyped->get_function() );
     415                }
     416                assertf( false, "getFunction received unknown expression: %s", toString( expr ).c_str() );
     417        }
     418
     419        const ast::DeclWithType * getFunction( const ast::Expr * expr ) {
     420                if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * >( expr ) ) {
     421                        return getCalledFunction( appExpr->func );
     422                } else if ( const ast::UntypedExpr * untyped = dynamic_cast< const ast::UntypedExpr * > ( expr ) ) {
     423                        return getCalledFunction( untyped->func );
    384424                }
    385425                assertf( false, "getFunction received unknown expression: %s", toString( expr ).c_str() );
     
    492532        namespace {
    493533                std::string funcName( Expression * func );
     534                std::string funcName( const ast::Expr * func );
    494535
    495536                template<typename CallExpr>
     
    500541                        assertf( ! expr->get_args().empty(), "Cannot get function name from dereference with no arguments" );
    501542                        return funcName( expr->get_args().front() );
     543                }
     544
     545                template<typename CallExpr>
     546                std::string handleDerefName( const CallExpr * expr ) {
     547                        // (*f)(x) => should get name "f"
     548                        std::string name = getFunctionName( expr );
     549                        assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() );
     550                        assertf( ! expr->args.empty(), "Cannot get function name from dereference with no arguments" );
     551                        return funcName( expr->args.front() );
    502552                }
    503553
     
    523573                        }
    524574                }
     575
     576                std::string funcName( const ast::Expr * func ) {
     577                        if ( const ast::NameExpr * nameExpr = dynamic_cast< const ast::NameExpr * >( func ) ) {
     578                                return nameExpr->name;
     579                        } else if ( const ast::VariableExpr * varExpr = dynamic_cast< const ast::VariableExpr * >( func ) ) {
     580                                return varExpr->var->name;
     581                        }       else if ( const ast::CastExpr * castExpr = dynamic_cast< const ast::CastExpr * >( func ) ) {
     582                                return funcName( castExpr->arg );
     583                        } else if ( const ast::MemberExpr * memberExpr = dynamic_cast< const ast::MemberExpr * >( func ) ) {
     584                                return memberExpr->member->name;
     585                        } else if ( const ast::UntypedMemberExpr * memberExpr = dynamic_cast< const ast::UntypedMemberExpr * > ( func ) ) {
     586                                return funcName( memberExpr->member );
     587                        } else if ( const ast::UntypedExpr * untypedExpr = dynamic_cast< const ast::UntypedExpr * >( func ) ) {
     588                                return handleDerefName( untypedExpr );
     589                        } else if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * >( func ) ) {
     590                                return handleDerefName( appExpr );
     591                        } else if ( const ast::ConstructorExpr * ctorExpr = dynamic_cast< const ast::ConstructorExpr * >( func ) ) {
     592                                return funcName( getCallArg( ctorExpr->callExpr, 0 ) );
     593                        } else {
     594                                assertf( false, "Unexpected expression type being called as a function in call expression: %s", toString( func ).c_str() );
     595                        }
     596                }
    525597        }
    526598
     
    533605                } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * > ( expr ) ) {
    534606                        return funcName( untypedExpr->get_function() );
     607                } else {
     608                        std::cerr << expr << std::endl;
     609                        assertf( false, "Unexpected expression type passed to getFunctionName" );
     610                }
     611        }
     612
     613        std::string getFunctionName( const ast::Expr * expr ) {
     614                // there's some unforunate overlap here with getCalledFunction. Ideally this would be able to use getCalledFunction and
     615                // return the name of the DeclarationWithType, but this needs to work for NameExpr and UntypedMemberExpr, where getCalledFunction
     616                // can't possibly do anything reasonable.
     617                if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * >( expr ) ) {
     618                        return funcName( appExpr->func );
     619                } else if ( const ast::UntypedExpr * untypedExpr = dynamic_cast< const ast::UntypedExpr * > ( expr ) ) {
     620                        return funcName( untypedExpr->func );
    535621                } else {
    536622                        std::cerr << expr << std::endl;
  • src/InitTweak/InitTweak.h

    r9d6e7fa9 rd7aa12c  
    7979        /// returns the name of the function being called
    8080        std::string getFunctionName( Expression * expr );
     81        std::string getFunctionName( const ast::Expr * expr );
    8182
    8283        /// returns the argument to a call expression in position N indexed from 0
Note: See TracChangeset for help on using the changeset viewer.