source: src/Parser/StatementNode.cc @ 01865fb

ADTast-experimentalpthread-emulation
Last change on this file since 01865fb was f135b50, checked in by JiadaL <j82liang@…>, 3 years ago

The compiler is now trying to pass the value of enum const to code gen; but it won't work cause we must be able to evaluate the const_expr in compiler time. It is not currently passed as a Expression but won't be able to evaluate at compile time

  • Property mode set to 100644
File size: 14.9 KB
RevLine 
[b87a5ed]1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
[4e7171f]7// StatementNode.cc -- Transform from parse data-structures to AST data-structures, usually deleting the parse
8//     data-structure after the transformation.
[b87a5ed]9//
10// Author           : Rodolfo G. Esteves
11// Created On       : Sat May 16 14:59:41 2015
[a67b60e]12// Last Modified By : Peter A. Buhr
[6180274]13// Last Modified On : Wed Feb  2 20:29:30 2022
14// Update Count     : 425
[b87a5ed]15//
16
[e3e16bc]17#include <cassert>                 // for assert, strict_dynamic_cast, assertf
[d180746]18#include <list>                    // for list
19#include <memory>                  // for unique_ptr
20#include <string>                  // for string
[51b7345]21
[d180746]22#include "Common/SemanticError.h"  // for SemanticError
23#include "Common/utility.h"        // for maybeMoveBuild, maybeBuild
24#include "ParseNode.h"             // for StatementNode, ExpressionNode, bui...
25#include "SynTree/Expression.h"    // for Expression, ConstantExpr
26#include "SynTree/Label.h"         // for Label, noLabels
[6d49ea3]27#include "SynTree/Declaration.h"
[d180746]28#include "SynTree/Statement.h"     // for Statement, BranchStmt, CaseStmt
29#include "parserutility.h"         // for notZeroExpr
30
31class Declaration;
[51b7345]32
33using namespace std;
34
[b87a5ed]35
[61fc4f6]36StatementNode::StatementNode( DeclarationNode * decl ) {
[c0aa336]37        assert( decl );
[61fc4f6]38        DeclarationNode * agg = decl->extractAggregate();
[c0aa336]39        if ( agg ) {
[61fc4f6]40                StatementNode * nextStmt = new StatementNode( new DeclStmt( maybeBuild< Declaration >( decl ) ) );
[c0aa336]41                set_next( nextStmt );
42                if ( decl->get_next() ) {
43                        get_next()->set_next( new StatementNode( dynamic_cast< DeclarationNode * >(decl->get_next()) ) );
44                        decl->set_next( 0 );
[1d4580a]45                } // if
46        } else {
[c0aa336]47                if ( decl->get_next() ) {
48                        set_next( new StatementNode( dynamic_cast< DeclarationNode * >( decl->get_next() ) ) );
49                        decl->set_next( 0 );
50                } // if
51                agg = decl;
[1d4580a]52        } // if
[ba3706f]53        stmt.reset( new DeclStmt( maybeMoveBuild< Declaration >(agg) ) );
[c0aa336]54} // StatementNode::StatementNode
[1d4580a]55
[61fc4f6]56StatementNode * StatementNode::append_last_case( StatementNode * stmt ) {
57        StatementNode * prev = this;
[1d4580a]58        // find end of list and maintain previous pointer
59        for ( StatementNode * curr = prev; curr != nullptr; curr = (StatementNode *)curr->get_next() ) {
[61fc4f6]60                StatementNode * node = strict_dynamic_cast< StatementNode * >(curr);
[ac71a86]61                assert( dynamic_cast< CaseStmt * >(node->stmt.get()) );
[8cc5cb0]62                prev = curr;
63        } // for
[e82aa9df]64        // convert from StatementNode list to Statement list
[61fc4f6]65        StatementNode * node = dynamic_cast< StatementNode * >(prev);
[436bbe5]66        list< Statement * > stmts;
[7ecbb7e]67        buildMoveList( stmt, stmts );
[e82aa9df]68        // splice any new Statements to end of current Statements
[ac71a86]69        CaseStmt * caseStmt = dynamic_cast< CaseStmt * >(node->stmt.get());
[8cc5cb0]70        caseStmt->get_statements().splice( caseStmt->get_statements().end(), stmts );
71        return this;
[401e61f]72} // StatementNode::append_last_case
[8cc5cb0]73
[61fc4f6]74Statement * build_expr( ExpressionNode * ctl ) {
75        Expression * e = maybeMoveBuild< Expression >( ctl );
[8cc5cb0]76
[401e61f]77        if ( e ) return new ExprStmt( e );
78        else return new NullStmt();
79} // build_expr
[8cc5cb0]80
[436bbe5]81Expression * build_if_control( CondCtl * ctl, list< Statement * > & init ) {
[936e9f4]82        if ( ctl->init != 0 ) {
83                buildMoveList( ctl->init, init );
84        } // if
85
[a01f7c94]86        Expression * cond = nullptr;
87        if ( ctl->condition ) {
88                // compare the provided condition against 0
[ee3c93d]89                cond = notZeroExpr( maybeMoveBuild< Expression >(ctl->condition) );
[a01f7c94]90        } else {
91                for ( Statement * stmt : init ) {
92                        // build the && of all of the declared variables compared against 0
[e3e16bc]93                        DeclStmt * declStmt = strict_dynamic_cast< DeclStmt * >( stmt );
94                        DeclarationWithType * dwt = strict_dynamic_cast< DeclarationWithType * >( declStmt->decl );
[a01f7c94]95                        Expression * nze = notZeroExpr( new VariableExpr( dwt ) );
96                        cond = cond ? new LogicalExpr( cond, nze, true ) : nze;
97                }
98        }
[6d49ea3]99        delete ctl;
[ee3c93d]100        return cond;
[401e61f]101} // build_if_control
[ee3c93d]102
[436bbe5]103Statement * build_if( CondCtl * ctl, StatementNode * then, StatementNode * else_ ) {
104        list< Statement * > astinit;                                            // maybe empty
105        Expression * astcond = build_if_control( ctl, astinit ); // ctl deleted, cond/init set
106
107        Statement * astthen, * astelse = nullptr;
108        list< Statement * > aststmt;
109        buildMoveList< Statement, StatementNode >( then, aststmt );
110        assert( aststmt.size() == 1 );
111        astthen = aststmt.front();
112
113        if ( else_ ) {
114                list< Statement * > aststmt;
115                buildMoveList< Statement, StatementNode >( else_, aststmt );
116                assert( aststmt.size() == 1 );
117                astelse = aststmt.front();
[ee3c93d]118        } // if
119
[436bbe5]120        return new IfStmt( astcond, astthen, astelse, astinit );
[401e61f]121} // build_if
[2f22cc4]122
[61fc4f6]123Statement * build_switch( bool isSwitch, ExpressionNode * ctl, StatementNode * stmt ) {
[436bbe5]124        list< Statement * > aststmt;
125        buildMoveList< Statement, StatementNode >( stmt, aststmt );
126        if ( ! isSwitch ) {                                                                     // choose statement
127                for ( Statement * stmt : aststmt ) {
[6a276a0]128                        CaseStmt * caseStmt = strict_dynamic_cast< CaseStmt * >( stmt );
129                        if ( ! caseStmt->stmts.empty() ) {                      // code after "case" => end of case list
130                                CompoundStmt * block = strict_dynamic_cast< CompoundStmt * >( caseStmt->stmts.front() );
131                                block->kids.push_back( new BranchStmt( "", BranchStmt::Break ) );
132                        } // if
133                } // for
134        } // if
[436bbe5]135        // aststmt.size() == 0 for switch (...) {}, i.e., no declaration or statements
136        return new SwitchStmt( maybeMoveBuild< Expression >(ctl), aststmt );
[401e61f]137} // build_switch
138
[61fc4f6]139Statement * build_case( ExpressionNode * ctl ) {
[6180274]140        return new CaseStmt( maybeMoveBuild< Expression >(ctl), {} ); // stmt starts empty and then added to
[401e61f]141} // build_case
142
[61fc4f6]143Statement * build_default() {
[6180274]144        return new CaseStmt( nullptr, {}, true );                       // stmt starts empty and then added to
[401e61f]145} // build_default
146
[3b0bc16]147Statement * build_while( CondCtl * ctl, StatementNode * stmt, StatementNode * else_ ) {
[436bbe5]148        list< Statement * > astinit;                                            // maybe empty
149        Expression * astcond = build_if_control( ctl, astinit ); // ctl deleted, cond/init set
[3b0bc16]150
[436bbe5]151        list< Statement * > aststmt;                                            // loop body, compound created if empty
[3b0bc16]152        buildMoveList< Statement, StatementNode >( stmt, aststmt );
153        assert( aststmt.size() == 1 );
154
[436bbe5]155        list< Statement * > astelse;                                            // else clause, maybe empty
[3b0bc16]156        buildMoveList< Statement, StatementNode >( else_, astelse );
157
[436bbe5]158        return new WhileDoStmt( astcond, aststmt.front(), astelse.front(), astinit, false );
[401e61f]159} // build_while
[2f22cc4]160
[3b0bc16]161Statement * build_do_while( ExpressionNode * ctl, StatementNode * stmt, StatementNode * else_ ) {
[436bbe5]162        list< Statement * > aststmt;                                            // loop body, compound created if empty
[3b0bc16]163        buildMoveList< Statement, StatementNode >( stmt, aststmt );
[436bbe5]164        assert( aststmt.size() == 1 );                                          // compound created if empty
[3b0bc16]165
[436bbe5]166        list< Statement * > astelse;                                            // else clause, maybe empty
[3b0bc16]167        buildMoveList< Statement, StatementNode >( else_, astelse );
[ee3c93d]168
[4e7171f]169        // do-while cannot have declarations in the contitional, so init is always empty
170        return new WhileDoStmt( notZeroExpr( maybeMoveBuild< Expression >(ctl) ), aststmt.front(), astelse.front(), {}, true );
[401e61f]171} // build_do_while
[2f22cc4]172
[3b0bc16]173Statement * build_for( ForCtrl * forctl, StatementNode * stmt, StatementNode * else_ ) {
[436bbe5]174        list< Statement * > astinit;                                            // maybe empty
175        buildMoveList( forctl->init, astinit );
[2f22cc4]176
[436bbe5]177        Expression * astcond = nullptr;                                         // maybe empty
178        astcond = notZeroExpr( maybeMoveBuild< Expression >(forctl->condition) );
[2f22cc4]179
[436bbe5]180        Expression * astincr = nullptr;                                         // maybe empty
181        astincr = maybeMoveBuild< Expression >(forctl->change);
182        delete forctl;
[2f22cc4]183
[436bbe5]184        list< Statement * > aststmt;                                            // loop body, compound created if empty
[3b0bc16]185        buildMoveList< Statement, StatementNode >( stmt, aststmt );
186        assert( aststmt.size() == 1 );
187
[436bbe5]188        list< Statement * > astelse;                                            // else clause, maybe empty
[3b0bc16]189        buildMoveList< Statement, StatementNode >( else_, astelse );
190
[436bbe5]191        return new ForStmt( astinit, astcond, astincr, aststmt.front(), astelse.front() );
[401e61f]192} // build_for
[2f22cc4]193
[61fc4f6]194Statement * build_branch( BranchStmt::Type kind ) {
[ba3706f]195        Statement * ret = new BranchStmt( "", kind );
[ab57786]196        return ret;
[401e61f]197} // build_branch
198
[436bbe5]199Statement * build_branch( string * identifier, BranchStmt::Type kind ) {
[61fc4f6]200        Statement * ret = new BranchStmt( * identifier, kind );
[ab57786]201        delete identifier;                                                                      // allocated by lexer
202        return ret;
[401e61f]203} // build_branch
204
[61fc4f6]205Statement * build_computedgoto( ExpressionNode * ctl ) {
[ba3706f]206        return new BranchStmt( maybeMoveBuild< Expression >(ctl), BranchStmt::Goto );
[401e61f]207} // build_computedgoto
[8cc5cb0]208
[61fc4f6]209Statement * build_return( ExpressionNode * ctl ) {
[436bbe5]210        list< Expression * > exps;
[7ecbb7e]211        buildMoveList( ctl, exps );
[ba3706f]212        return new ReturnStmt( exps.size() > 0 ? exps.back() : nullptr );
[401e61f]213} // build_return
[daf1af8]214
[61fc4f6]215Statement * build_throw( ExpressionNode * ctl ) {
[436bbe5]216        list< Expression * > exps;
[7ecbb7e]217        buildMoveList( ctl, exps );
[436bbe5]218        assertf( exps.size() < 2, "CFA internal error: leaking memory" );
[ba3706f]219        return new ThrowStmt( ThrowStmt::Terminate, !exps.empty() ? exps.back() : nullptr );
[401e61f]220} // build_throw
[daf1af8]221
[61fc4f6]222Statement * build_resume( ExpressionNode * ctl ) {
[436bbe5]223        list< Expression * > exps;
[daf1af8]224        buildMoveList( ctl, exps );
[436bbe5]225        assertf( exps.size() < 2, "CFA internal error: leaking memory" );
[ba3706f]226        return new ThrowStmt( ThrowStmt::Resume, !exps.empty() ? exps.back() : nullptr );
[401e61f]227} // build_resume
[daf1af8]228
[61fc4f6]229Statement * build_resume_at( ExpressionNode * ctl, ExpressionNode * target ) {
[794c15b]230        (void)ctl;
231        (void)target;
232        assertf( false, "resume at (non-local throw) is not yet supported," );
[401e61f]233} // build_resume_at
[321f55d]234
[436bbe5]235Statement * build_try( StatementNode * try_, StatementNode * catch_, StatementNode * finally_ ) {
236        list< CatchStmt * > aststmt;
237        buildMoveList< CatchStmt, StatementNode >( catch_, aststmt );
238        CompoundStmt * tryBlock = strict_dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(try_));
239        FinallyStmt * finallyBlock = dynamic_cast< FinallyStmt * >(maybeMoveBuild< Statement >(finally_) );
240        return new TryStmt( tryBlock, aststmt, finallyBlock );
[401e61f]241} // build_try
242
[61fc4f6]243Statement * build_catch( CatchStmt::Kind kind, DeclarationNode * decl, ExpressionNode * cond, StatementNode * body ) {
[436bbe5]244        list< Statement * > aststmt;
245        buildMoveList< Statement, StatementNode >( body, aststmt );
246        assert( aststmt.size() == 1 );
247        return new CatchStmt( kind, maybeMoveBuild< Declaration >(decl), maybeMoveBuild< Expression >(cond), aststmt.front() );
[401e61f]248} // build_catch
249
[61fc4f6]250Statement * build_finally( StatementNode * stmt ) {
[436bbe5]251        list< Statement * > aststmt;
252        buildMoveList< Statement, StatementNode >( stmt, aststmt );
253        assert( aststmt.size() == 1 );
254        return new FinallyStmt( dynamic_cast< CompoundStmt * >( aststmt.front() ) );
[401e61f]255} // build_finally
[1d4580a]256
[427854b]257SuspendStmt * build_suspend( StatementNode * then, SuspendStmt::Type type ) {
258        auto node = new SuspendStmt();
259
260        node->type = type;
261
[436bbe5]262        list< Statement * > stmts;
[427854b]263        buildMoveList< Statement, StatementNode >( then, stmts );
264        if(!stmts.empty()) {
265                assert( stmts.size() == 1 );
266                node->then = dynamic_cast< CompoundStmt * >( stmts.front() );
267        }
268
269        return node;
270}
271
[135b431]272WaitForStmt * build_waitfor( ExpressionNode * targetExpr, StatementNode * stmt, ExpressionNode * when ) {
273        auto node = new WaitForStmt();
274
275        WaitForStmt::Target target;
276        target.function = maybeBuild<Expression>( targetExpr );
[2065609]277
278        ExpressionNode * next = dynamic_cast<ExpressionNode *>( targetExpr->get_next() );
279        targetExpr->set_next( nullptr );
280        buildMoveList< Expression >( next, target.arguments );
281
[135b431]282        delete targetExpr;
283
284        node->clauses.push_back( WaitForStmt::Clause{
285                target,
286                maybeMoveBuild<Statement >( stmt ),
[1dcd9554]287                notZeroExpr( maybeMoveBuild<Expression>( when ) )
[135b431]288        });
289
290        return node;
[401e61f]291} // build_waitfor
[135b431]292
293WaitForStmt * build_waitfor( ExpressionNode * targetExpr, StatementNode * stmt, ExpressionNode * when, WaitForStmt * node ) {
294        WaitForStmt::Target target;
295        target.function = maybeBuild<Expression>( targetExpr );
[2065609]296
297        ExpressionNode * next = dynamic_cast<ExpressionNode *>( targetExpr->get_next() );
298        targetExpr->set_next( nullptr );
299        buildMoveList< Expression >( next, target.arguments );
300
[135b431]301        delete targetExpr;
302
[310e5b7]303        node->clauses.insert( node->clauses.begin(), WaitForStmt::Clause{
[135b431]304                std::move( target ),
305                maybeMoveBuild<Statement >( stmt ),
[1dcd9554]306                notZeroExpr( maybeMoveBuild<Expression>( when ) )
[135b431]307        });
308
309        return node;
[401e61f]310} // build_waitfor
[135b431]311
312WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when ) {
313        auto node = new WaitForStmt();
314
315        if( timeout ) {
316                node->timeout.time      = maybeMoveBuild<Expression>( timeout );
317                node->timeout.statement = maybeMoveBuild<Statement >( stmt    );
[1dcd9554]318                node->timeout.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) );
[401e61f]319        } else {
[1dcd9554]320                node->orelse.statement  = maybeMoveBuild<Statement >( stmt );
321                node->orelse.condition  = notZeroExpr( maybeMoveBuild<Expression>( when ) );
[401e61f]322        } // if
[135b431]323
324        return node;
[401e61f]325} // build_waitfor_timeout
[135b431]326
[436bbe5]327WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when,  StatementNode * else_, ExpressionNode * else_when ) {
[135b431]328        auto node = new WaitForStmt();
329
330        node->timeout.time      = maybeMoveBuild<Expression>( timeout );
331        node->timeout.statement = maybeMoveBuild<Statement >( stmt    );
[1dcd9554]332        node->timeout.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) );
[135b431]333
[436bbe5]334        node->orelse.statement  = maybeMoveBuild<Statement >( else_ );
[1dcd9554]335        node->orelse.condition  = notZeroExpr( maybeMoveBuild<Expression>( else_when ) );
[135b431]336
337        return node;
[401e61f]338} // build_waitfor_timeout
[135b431]339
[e67991f]340Statement * build_with( ExpressionNode * exprs, StatementNode * stmt ) {
[436bbe5]341        list< Expression * > e;
[a378ca7]342        buildMoveList( exprs, e );
343        Statement * s = maybeMoveBuild<Statement>( stmt );
[e67991f]344        return new DeclStmt( new WithStmt( e, s ) );
[401e61f]345} // build_with
[a378ca7]346
[61fc4f6]347Statement * build_compound( StatementNode * first ) {
348        CompoundStmt * cs = new CompoundStmt();
[7ecbb7e]349        buildMoveList( first, cs->get_kids() );
[b87a5ed]350        return cs;
[401e61f]351} // build_compound
[51b7345]352
[a025ea8]353// A single statement in a control structure is always converted to a compound statement so subsequent generated code
354// can be placed within this compound statement. Otherwise, code generation has to constantly check for a single
355// statement and wrap it into a compound statement to insert additional code. Hence, all control structures have a
356// conical form for code generation.
357StatementNode * maybe_build_compound( StatementNode * first ) {
358        // Optimization: if the control-structure statement is a compound statement, do not wrap it.
359        // e.g., if (...) {...} do not wrap the existing compound statement.
360        if ( ! dynamic_cast<CompoundStmt *>( first->stmt.get() ) ) { // unique_ptr
361                CompoundStmt * cs = new CompoundStmt();
362                buildMoveList( first, cs->get_kids() );
363                return new StatementNode( cs );
364        } // if
365        return first;
366} // maybe_build_compound
367
[f135b50]368// Question
[6d539f83]369Statement * build_asm( bool voltile, Expression * instruction, ExpressionNode * output, ExpressionNode * input, ExpressionNode * clobber, LabelNode * gotolabels ) {
[436bbe5]370        list< Expression * > out, in;
371        list< ConstantExpr * > clob;
[e82aa9df]372
[7ecbb7e]373        buildMoveList( output, out );
374        buildMoveList( input, in );
375        buildMoveList( clobber, clob );
[ba3706f]376        return new AsmStmt( voltile, instruction, out, in, clob, gotolabels ? gotolabels->labels : noLabels );
[401e61f]377} // build_asm
[7f5566b]378
[6d539f83]379Statement * build_directive( string * directive ) {
[cc32d83]380        return new DirectiveStmt( *directive );
[401e61f]381} // build_directive
[61fc4f6]382
[de52331]383Statement * build_mutex( ExpressionNode * exprs, StatementNode * stmt ) {
[436bbe5]384        list< Expression * > expList;
[de52331]385        buildMoveList( exprs, expList );
386        Statement * body = maybeMoveBuild<Statement>( stmt );
387        return new MutexStmt( body, expList );
388} // build_mutex
389
[51b7345]390// Local Variables: //
[b87a5ed]391// tab-width: 4 //
392// mode: c++ //
393// compile-command: "make install" //
[51b7345]394// End: //
Note: See TracBrowser for help on using the repository browser.