Goose  Diff

Differences From Artifact [f7c31a22f7]:

  • File bs/builtins/statements/while.cpp — part of check-in [af650a9e95] at 2019-09-22 14:37:55 on branch trunk — Project renaming. (user: achavasse size: 7358)

To Artifact [6f657eba62]:

  • File bs/builtins/statements/while.cpp — part of check-in [f7c8245cb2] at 2019-11-16 16:27:32 on branch trunk —
    • while: remove code attempting to detect infinite loops, this will be best left to the verifier.
    • verifier: loop unrolling now works.
    (user: achavasse size: 5358)

67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184

                return false;
            }

            if( get< Value >( converted ).isPoison() )
                p.cfg()->poison();

            // We need to know if the condition is constant true and this is an infinite loop:
            // it means that no return is required after the while. In practice, that means
            // we don't need to create a successor block and therefore won't leave it unterminated,
            // which will prevent the higher level parsing code to complain about a missing return.
            bool infiniteLoop = false;

            // Try to eagerly evaluate the condition, if it happens to be constant true
            // we need to know about it.
            if( llr::CanValueBeEagerlyEvaluated( get< Value >( converted ) ) )
            {
                execute::VM vm;
                auto cond = Evaluate( get< Value >( converted ), vm );
                if( cond.isPoison() )
                    p.cfg()->poison();
                else if( cond.isConstant() && *FromValue< bool >( cond ) )
                    infiniteLoop = true;
            }

            ptr< llr::BasicBlock > pHeaderBB;

            // If this is an infinite loop, we don't need a header block: we can jump straight to the
            // body.
            if( !infiniteLoop )
                pHeaderBB = p.cfg()->createBB();

            Parser::BreakableScopeGuard bsg( p );
            Parser::ContinuableScopeGuard csg( p );

            auto pBodyBB = ParseSubStatement( p, precedence::IfStmt );
            if( !pBodyBB )
            {
                dm.emitSyntaxErrorMessage( p.resolver()->getCurrentLocation(), "expected a statement after the while condition.", 0 );
                return false;
            }

            if( !pPrecBB )
                return true;

            // If this is an infinite loop, we don't need a header block: we can jump straight to the
            // body.
            if( infiniteLoop )
                pHeaderBB = pBodyBB;

            // Retrieve the final block of the loop body, if any
            if( auto pBodySuccBB = p.cfg()->currentBB(); pBodySuccBB && !pBodySuccBB->terminator() )
            {
                // Jump from the end of the body BB back to the loop header
                pBodySuccBB->setTerminator( llr::Branch( pHeaderBB ) );
            }

            // Jump unconditionally from the pred block to the loop header.
            pPrecBB->setTerminator( llr::Branch( pHeaderBB ) );

            ptr< llr::BasicBlock > pSuccBB;
            if( !infiniteLoop )
                pSuccBB = p.cfg()->createBB();

            auto breakLevel = p.breakableScopeLevels();
            auto continueLevel = p.continuableScopeLevels();

            // Go through all basic blocks, find all break and continue terminators
            // for our scope level and replace them with branches respectively to
            // the successor BB or to the loop header BB.
            p.cfg()->ForEachBB( [&]( auto&& bb )
            {
                const auto& t = bb->terminator();
                if( !t )
                    return;

                if( const auto* pBreak = get_if< llr::Break >( &t->content() ) )
                {
                    if( pBreak->level() == breakLevel )
                    {
                        // If there is a break somewhere, we are not an infinite loop after all.
                        // this doesn't affect that we don't need a header, but it means that we do need
                        // a successor block.
                        if( !pSuccBB )
                            pSuccBB = p.cfg()->createBB();

                        bb->setTerminator( llr::Branch( pSuccBB ) );
                    }
                    return;
                }

                if( const auto* pCont = get_if< llr::Continue >( &t->content() ) )
                {
                    if( pCont->level() == continueLevel )
                        bb->setTerminator( llr::Branch( pHeaderBB ) );
                    return;
                }
            } );

            if( pSuccBB && !infiniteLoop )
            {
                // Emit the conditional branch that will either run an iteration of the loop or exit to the succ bb.
                pHeaderBB->setTerminator( llr::CondBranch(
                    get< Value >( converted ),
                    pBodyBB, pSuccBB ) );
            }

            // If we have no successor BB, this will intetionally set the current BB to null.
            p.cfg()->setCurrentBB( pSuccBB );
            return true;
        };

        Rule r( handleWhile );
        auto ruleVal = ToValue( move( r ) );
        auto ruleTerm = ValueToIRExpr( ruleVal );
        e.storeValue( AppendToVectorTerm( RootIdentity(), TSID( while ) ), ANYTERM( _ ), ruleTerm );
    }
}







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|














<
<
<
<
<










<
<
|
















<
<
<
<
<
<
<

<











<
<
|
|
|
|
|
<
<










67
68
69
70
71
72
73























74
75
76
77
78
79
80
81
82
83
84
85
86
87
88





89
90
91
92
93
94
95
96
97
98


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115







116

117
118
119
120
121
122
123
124
125
126
127


128
129
130
131
132


133
134
135
136
137
138
139
140
141
142

                return false;
            }

            if( get< Value >( converted ).isPoison() )
                p.cfg()->poison();
























            auto pHeaderBB = p.cfg()->createBB();

            Parser::BreakableScopeGuard bsg( p );
            Parser::ContinuableScopeGuard csg( p );

            auto pBodyBB = ParseSubStatement( p, precedence::IfStmt );
            if( !pBodyBB )
            {
                dm.emitSyntaxErrorMessage( p.resolver()->getCurrentLocation(), "expected a statement after the while condition.", 0 );
                return false;
            }

            if( !pPrecBB )
                return true;






            // Retrieve the final block of the loop body, if any
            if( auto pBodySuccBB = p.cfg()->currentBB(); pBodySuccBB && !pBodySuccBB->terminator() )
            {
                // Jump from the end of the body BB back to the loop header
                pBodySuccBB->setTerminator( llr::Branch( pHeaderBB ) );
            }

            // Jump unconditionally from the pred block to the loop header.
            pPrecBB->setTerminator( llr::Branch( pHeaderBB ) );



            auto pSuccBB = p.cfg()->createBB();

            auto breakLevel = p.breakableScopeLevels();
            auto continueLevel = p.continuableScopeLevels();

            // Go through all basic blocks, find all break and continue terminators
            // for our scope level and replace them with branches respectively to
            // the successor BB or to the loop header BB.
            p.cfg()->ForEachBB( [&]( auto&& bb )
            {
                const auto& t = bb->terminator();
                if( !t )
                    return;

                if( const auto* pBreak = get_if< llr::Break >( &t->content() ) )
                {
                    if( pBreak->level() == breakLevel )







                        bb->setTerminator( llr::Branch( pSuccBB ) );

                    return;
                }

                if( const auto* pCont = get_if< llr::Continue >( &t->content() ) )
                {
                    if( pCont->level() == continueLevel )
                        bb->setTerminator( llr::Branch( pHeaderBB ) );
                    return;
                }
            } );



            // Emit the conditional branch that will either run an iteration of the loop or exit to the succ bb.
            pHeaderBB->setTerminator( llr::CondBranch(
                get< Value >( converted ),
                pBodyBB, pSuccBB ) );



            p.cfg()->setCurrentBB( pSuccBB );
            return true;
        };

        Rule r( handleWhile );
        auto ruleVal = ToValue( move( r ) );
        auto ruleTerm = ValueToIRExpr( ruleVal );
        e.storeValue( AppendToVectorTerm( RootIdentity(), TSID( while ) ), ANYTERM( _ ), ruleTerm );
    }
}