Ignore:
Timestamp:
Apr 19, 2022, 3:00:04 PM (3 years ago)
Author:
m3zulfiq <m3zulfiq@…>
Branches:
ADT, ast-experimental, master, pthread-emulation, qualifiedEnum
Children:
5b84a321
Parents:
ba897d21 (diff), bb7c77d (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

added benchmark and evaluations chapter to thesis

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/InitTweak/FixInitNew.cpp

    rba897d21 r2e9b59b  
    1616#include "CodeGen/GenType.h"           // for genPrettyType
    1717#include "CodeGen/OperatorTable.h"
     18#include "Common/CodeLocationTools.hpp"
    1819#include "Common/PassVisitor.h"        // for PassVisitor, WithStmtsToAdd
    1920#include "Common/SemanticError.h"      // for SemanticError
     
    8586        /// generate/resolve copy construction expressions for each, and generate/resolve destructors for both
    8687        /// arguments and return value temporaries
    87         struct ResolveCopyCtors final : public ast::WithGuards, public ast::WithStmtsToAdd<>, public ast::WithSymbolTable, public ast::WithShortCircuiting, public ast::WithVisitorRef<ResolveCopyCtors> {
     88        struct ResolveCopyCtors final : public ast::WithGuards, public ast::WithStmtsToAdd<>, public ast::WithSymbolTable, public ast::WithShortCircuiting, public ast::WithVisitorRef<ResolveCopyCtors>, public ast::WithConstTranslationUnit {
    8889                const ast::Expr * postvisit( const ast::ImplicitCopyCtorExpr * impCpCtorExpr );
    8990                const ast::StmtExpr * previsit( const ast::StmtExpr * stmtExpr );
     
    189190        /// for any member that is missing a corresponding ctor/dtor call.
    190191        /// error if a member is used before constructed
    191         struct GenStructMemberCalls final : public ast::WithGuards, public ast::WithShortCircuiting, public ast::WithSymbolTable, public ast::WithVisitorRef<GenStructMemberCalls> {
     192        struct GenStructMemberCalls final : public ast::WithGuards, public ast::WithShortCircuiting, public ast::WithSymbolTable, public ast::WithVisitorRef<GenStructMemberCalls>, public ast::WithConstTranslationUnit {
    192193                void previsit( const ast::FunctionDecl * funcDecl );
    193194                const ast::DeclWithType * postvisit( const ast::FunctionDecl * funcDecl );
     
    214215
    215216        /// expands ConstructorExpr nodes into comma expressions, using a temporary for the first argument
    216         struct FixCtorExprs final : public ast::WithDeclsToAdd<>, public ast::WithSymbolTable, public ast::WithShortCircuiting {
     217        struct FixCtorExprs final : public ast::WithDeclsToAdd<>, public ast::WithSymbolTable, public ast::WithShortCircuiting, public ast::WithConstTranslationUnit {
    217218                const ast::Expr * postvisit( const ast::ConstructorExpr * ctorExpr );
    218219        };
     
    509510                // (VariableExpr and already resolved expression)
    510511                CP_CTOR_PRINT( std::cerr << "ResolvingCtorDtor " << untyped << std::endl; )
    511                 ast::ptr<ast::Expr> resolved = ResolvExpr::findVoidExpression(untyped, symtab);
     512                ast::ptr<ast::Expr> resolved = ResolvExpr::findVoidExpression(untyped, { symtab, transUnit().global } );
    512513                assert( resolved );
    513514                if ( resolved->env ) {
     
    553554                ast::ptr<ast::Expr> guard = mutArg;
    554555
    555                 ast::ptr<ast::ObjectDecl> tmp = new ast::ObjectDecl({}, "__tmp", mutResult, nullptr );
     556                ast::ptr<ast::ObjectDecl> tmp = new ast::ObjectDecl(loc, "__tmp", mutResult, nullptr );
    556557
    557558                // create and resolve copy constructor
     
    587588
    588589        ast::Expr * ResolveCopyCtors::destructRet( const ast::ObjectDecl * ret, const ast::Expr * arg ) {
     590                auto global = transUnit().global;
    589591                // TODO: refactor code for generating cleanup attribute, since it's common and reused in ~3-4 places
    590592                // check for existing cleanup attribute before adding another(?)
    591593                // need to add __Destructor for _tmp_cp variables as well
    592594
    593                 assertf( ast::dtorStruct, "Destructor generation requires __Destructor definition." );
    594                 assertf( ast::dtorStruct->members.size() == 2, "__Destructor definition does not have expected fields." );
    595                 assertf( ast::dtorStructDestroy, "Destructor generation requires __destroy_Destructor." );
     595                assertf( global.dtorStruct, "Destructor generation requires __Destructor definition." );
     596                assertf( global.dtorStruct->members.size() == 2, "__Destructor definition does not have expected fields." );
     597                assertf( global.dtorDestroy, "Destructor generation requires __destroy_Destructor." );
    596598
    597599                const CodeLocation loc = ret->location;
     
    610612                auto dtorFunc = getDtorFunc( ret, new ast::ExprStmt(loc, dtor ), stmtsToAddBefore );
    611613
    612                 auto dtorStructType = new ast::StructInstType(ast::dtorStruct);
     614                auto dtorStructType = new ast::StructInstType( global.dtorStruct );
    613615
    614616                // what does this do???
     
    622624                static UniqueName namer( "_ret_dtor" );
    623625                auto retDtor = new ast::ObjectDecl(loc, namer.newName(), dtorStructType, new ast::ListInit(loc, { new ast::SingleInit(loc, ast::ConstantExpr::null(loc) ), new ast::SingleInit(loc, new ast::CastExpr( new ast::VariableExpr(loc, dtorFunc ), dtorType ) ) } ) );
    624                 retDtor->attributes.push_back( new ast::Attribute( "cleanup", { new ast::VariableExpr(loc, ast::dtorStructDestroy ) } ) );
     626                retDtor->attributes.push_back( new ast::Attribute( "cleanup", { new ast::VariableExpr(loc, global.dtorDestroy ) } ) );
    625627                stmtsToAddBefore.push_back( new ast::DeclStmt(loc, retDtor ) );
    626628
    627629                if ( arg ) {
    628                         auto member = new ast::MemberExpr(loc, ast::dtorStruct->members.front().strict_as<ast::DeclWithType>(), new ast::VariableExpr(loc, retDtor ) );
     630                        auto member = new ast::MemberExpr(loc, global.dtorStruct->members.front().strict_as<ast::DeclWithType>(), new ast::VariableExpr(loc, retDtor ) );
    629631                        auto object = new ast::CastExpr( new ast::AddressExpr( new ast::VariableExpr(loc, ret ) ), new ast::PointerType(new ast::VoidType() ) );
    630632                        ast::Expr * assign = createBitwiseAssignment( member, object );
     
    799801        // to prevent warnings ('_unq0' may be used uninitialized in this function),
    800802        // insert an appropriate zero initializer for UniqueExpr temporaries.
    801         ast::Init * makeInit( const ast::Type * t ) {
     803        ast::Init * makeInit( const ast::Type * t, CodeLocation const & loc ) {
    802804                if ( auto inst = dynamic_cast< const ast::StructInstType * >( t ) ) {
    803805                        // initizer for empty struct must be empty
    804                         if ( inst->base->members.empty() ) return new ast::ListInit({}, {});
     806                        if ( inst->base->members.empty() ) {
     807                                return new ast::ListInit( loc, {} );
     808                        }
    805809                } else if ( auto inst = dynamic_cast< const ast::UnionInstType * >( t ) ) {
    806810                        // initizer for empty union must be empty
    807                         if ( inst->base->members.empty() ) return new ast::ListInit({}, {});
    808                 }
    809 
    810                 return new ast::ListInit( {}, { new ast::SingleInit( {}, ast::ConstantExpr::from_int({}, 0) ) } );
     811                        if ( inst->base->members.empty() ) {
     812                                return new ast::ListInit( loc, {} );
     813                        }
     814                }
     815
     816                return new ast::ListInit( loc, {
     817                        new ast::SingleInit( loc, ast::ConstantExpr::from_int( loc, 0 ) )
     818                } );
    811819        }
    812820
     
    832840                        } else {
    833841                                // expr isn't a call expr, so create a new temporary variable to use to hold the value of the unique expression
    834                                 mutExpr->object = new ast::ObjectDecl( mutExpr->location, toString("_unq", mutExpr->id), mutExpr->result, makeInit( mutExpr->result ) );
     842                                mutExpr->object = new ast::ObjectDecl( mutExpr->location, toString("_unq", mutExpr->id), mutExpr->result, makeInit( mutExpr->result, mutExpr->location ) );
    835843                                mutExpr->var = new ast::VariableExpr( mutExpr->location, mutExpr->object );
    836844                        }
     
    11721180                        auto guard = makeFuncGuard( [this]() { symtab.enterScope(); }, [this]() { symtab.leaveScope(); } );
    11731181                        symtab.addFunction( function );
     1182                        auto global = transUnit().global;
    11741183
    11751184                        // need to iterate through members in reverse in order for
     
    12171226
    12181227                                                        static UniqueName memberDtorNamer = { "__memberDtor" };
    1219                                                         assertf( ast::dtorStruct, "builtin __Destructor not found." );
    1220                                                         assertf( ast::dtorStructDestroy, "builtin __destroy_Destructor not found." );
     1228                                                        assertf( global.dtorStruct, "builtin __Destructor not found." );
     1229                                                        assertf( global.dtorDestroy, "builtin __destroy_Destructor not found." );
    12211230
    12221231                                                        ast::Expr * thisExpr = new ast::CastExpr( new ast::AddressExpr( new ast::VariableExpr(loc, thisParam ) ), new ast::PointerType( new ast::VoidType(), ast::CV::Qualifiers() ) );
     
    12281237                                                        auto dtorType = new ast::PointerType( dtorFtype );
    12291238
    1230                                                         auto destructor = new ast::ObjectDecl(loc, memberDtorNamer.newName(), new ast::StructInstType( ast::dtorStruct ), new ast::ListInit(loc, { new ast::SingleInit(loc, thisExpr ), new ast::SingleInit(loc, new ast::CastExpr( dtorExpr, dtorType ) ) } ) );
    1231                                                         destructor->attributes.push_back( new ast::Attribute( "cleanup", { new ast::VariableExpr({}, ast::dtorStructDestroy ) } ) );
     1239                                                        auto destructor = new ast::ObjectDecl(loc, memberDtorNamer.newName(), new ast::StructInstType( global.dtorStruct ), new ast::ListInit(loc, { new ast::SingleInit(loc, thisExpr ), new ast::SingleInit(loc, new ast::CastExpr( dtorExpr, dtorType ) ) } ) );
     1240                                                        destructor->attributes.push_back( new ast::Attribute( "cleanup", { new ast::VariableExpr( loc, global.dtorDestroy ) } ) );
    12321241                                                        mutStmts->push_front( new ast::DeclStmt(loc, destructor ) );
    12331242                                                        mutStmts->kids.splice( mutStmts->kids.begin(), stmtsToAdd );
     
    13231332
    13241333        const ast::Expr * GenStructMemberCalls::postvisit( const ast::UntypedExpr * untypedExpr ) {
    1325                 // Expression * newExpr = untypedExpr;
    13261334                // xxx - functions returning ast::ptr seems wrong...
    1327                 auto res = ResolvExpr::findVoidExpression( untypedExpr, symtab );
    1328                 return res.release();
    1329                 // return newExpr;
     1335                auto res = ResolvExpr::findVoidExpression( untypedExpr, { symtab, transUnit().global } );
     1336                // Fix CodeLocation (at least until resolver is fixed).
     1337                auto fix = localFillCodeLocations( untypedExpr->location, res.release() );
     1338                return strict_dynamic_cast<const ast::Expr *>( fix );
    13301339        }
    13311340
     
    13611370
    13621371                // resolve assignment and dispose of new env
    1363                 auto resolved = ResolvExpr::findVoidExpression( assign, symtab );
     1372                auto resolved = ResolvExpr::findVoidExpression( assign, { symtab, transUnit().global } );
    13641373                auto mut = resolved.get_and_mutate();
    13651374                assertf(resolved.get() == mut, "newly resolved expression must be unique");
Note: See TracChangeset for help on using the changeset viewer.