~eda-qa/leaflang/misc

« back to all changes in this revision

Viewing changes to include/ir/builder.hpp

  • Committer: edA-qa mort-ora-y
  • Date: 2017-07-22 19:48:13 UTC
  • mfrom: (99.1.41 error)
  • Revision ID: eda-qa@disemia.com-20170722194813-h3yqkhe1j2ouwga4
merging error handling

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
        function_builder create_function( std::string const & name, bool can_fail );
39
39
        function_builder create_function( std::string const & name, shared_ptr<type const> func_type,
40
40
                shared_ptr<value> use_value = shared_ptr<value>() );
 
41
                
 
42
protected:
 
43
        type_manager & get_types() {
 
44
                return cctx->get_types();
 
45
        }
41
46
};
42
47
 
43
48
struct function_builder : public module_builder
109
114
        }
110
115
        
111
116
        //TODO: a generic "inst" base to handle this
112
 
        void add()
113
 
        { where->push_back( inst, block_flow::sequential ); }
114
 
        void defer()
115
 
        { where->push_back( inst, block_flow::defer ); }
116
 
        void defer_error()
117
 
        { where->push_back( inst, block_flow::defer_error ); }
118
 
        void push_back( block_flow flow )
119
 
        { where->push_back( inst, flow ); }
 
117
        void add() { 
 
118
                where->push_back( inst, block_flow::sequential ); 
 
119
        }
 
120
        void defer() { 
 
121
                where->push_back( inst, block_flow::defer ); 
 
122
        }
 
123
        void defer_fail() { 
 
124
                where->push_back( inst, block_flow::defer_fail ); 
 
125
        }
 
126
        void push_back( block_flow flow ) { 
 
127
                where->push_back( inst, flow ); 
 
128
        }
120
129
};
121
130
 
122
131
/**
133
142
        //special block where defer's should put placed
134
143
        block * split_defer;
135
144
        
136
 
        function * owner_function;
 
145
        function & get_owner_function() {
 
146
                return where->get_owner_function();
 
147
        }
137
148
        
138
 
        explicit builder( block & where )
139
 
                : module_builder( where.owner_function.cctx )
140
 
                , where( &where )
141
 
                , split_defer( nullptr )
142
 
                , owner_function( &where.owner_function )
143
 
        { }
144
 
        explicit builder( function & func )
145
 
                : module_builder( func.cctx )
146
 
                , where( func.entry_block.get() )
147
 
                , split_defer( nullptr )
148
 
                , owner_function( &func )
149
 
        { }
150
 
        explicit builder( function_builder & fb )
151
 
                : module_builder( *fb.cctx )
152
 
                , where( fb.owner_function->entry_block.get() )
153
 
                , split_defer( nullptr )
154
 
                , owner_function( fb.owner_function )
155
 
        { }
 
149
        explicit builder( block & where ) : 
 
150
                module_builder( where.get_owner_function().cctx ), 
 
151
                where( &where ), 
 
152
                split_defer( nullptr ) { 
 
153
        }
 
154
        explicit builder( function & func ) : 
 
155
                module_builder( func.cctx ), 
 
156
                where( func.entry_block.get() ), 
 
157
                split_defer( nullptr ) { 
 
158
        }
 
159
        explicit builder( function_builder & fb ) : 
 
160
                module_builder( *fb.cctx ), 
 
161
                where( fb.owner_function->entry_block.get() ), 
 
162
                split_defer( nullptr ) { 
 
163
        }
156
164
 
157
165
        template<typename T>
158
166
        void push_back( T const & i, block_flow flow ) { 
171
179
                push_back(i, block_flow::defer );
172
180
        }
173
181
        template<typename T>
174
 
        void defer_error( T const & i ) { 
175
 
                push_back(i, block_flow::defer_error ); 
 
182
        void defer_fail( T const & i ) { 
 
183
                push_back(i, block_flow::defer_fail ); 
176
184
        }
177
185
 
178
186
        shared_ptr<store_inst> add_store_inst( value_ref left, value_ref right )
184
192
                return si;
185
193
        }
186
194
        
187
 
        block & get_block()
188
 
        { return *where; }
189
 
 
 
195
        block & get_block() { 
 
196
                return *where; 
 
197
        }
190
198
        
191
 
        shared_ptr<value> make_temp( shared_ptr<type const> dt )
192
 
        { return where->make_temp( dt ); }
 
199
        shared_ptr<value> make_temp( shared_ptr<type const> dt ) { 
 
200
                return where->make_temp( dt ); 
 
201
        }
193
202
 
194
203
        //TODO: should return block builders...
195
 
        shared_ptr<block> create_block()
196
 
        { return make_shared<block>( *owner_function ); }
 
204
        shared_ptr<block> create_block() { 
 
205
                return where->create_child_block();
 
206
        }
197
207
        
198
208
        shared_ptr<block> insert_block()
199
209
        { return where->insert_block(); }
203
213
                auto l = make_shared<value>( value::f_local );
204
214
                l->name = name;
205
215
                l->direct_type = direct_type;
206
 
                owner_function->locals.push_back( l );
 
216
                get_owner_function().locals.push_back( l );
207
217
                return value_defn(l);
208
218
        }
209
219
        
267
277
                auto ci = make_shared<bind_func_inst>();
268
278
                ci->ctx_func = ctx_func;
269
279
                ci->ctx_data.direct_type = data_type;
270
 
                ci->ctx_data.result = where->make_temp( type::get_raw_pointer( ci->ctx_data.direct_type ) );
 
280
                ci->ctx_data.result = where->make_temp( get_types().get_raw_pointer( ci->ctx_data.direct_type ) );
271
281
                
272
282
                auto bound_func = ctx_func.result_type()->clone();
273
283
                bound_func->access = type::a_bound;
326
336
        
327
337
        value_defn const_integer_normal( int val ) //of native integer type
328
338
        {
329
 
                auto t = make_shared<type>( type::f_integer );
 
339
                auto t = make_shared<type>( get_types().shared_from_this(), type::f_integer );
330
340
                t->trivial_bitsize = platform::natural_integer_size;
331
341
                return const_integer( t, val );
332
342
        }
343
353
        {
344
354
                auto v = make_shared<value>( value::f_const_boolean );
345
355
                v->boolean_value = val;
346
 
                v->direct_type = cctx->get_types().get_boolean();
 
356
                v->direct_type = get_types().get_boolean();
347
357
                return v;
348
358
        }
349
359
        
358
368
                return sf->result;
359
369
        }
360
370
        
361
 
        value_defn add_binary( binary_inst::op_t op, value_ref a, value_ref b, shared_ptr<type const> ret ) 
362
 
        {
 
371
        value_defn add_binary( binary_inst::op_t op, value_ref a, value_ref b, shared_ptr<type const> ret ) {
363
372
                auto bin = make_shared<binary_inst>();
364
373
                bin->op = op;
365
374
                bin->a = a;
369
378
                return bin->result;
370
379
        }
371
380
 
372
 
        value_defn add_unary( unary_inst::op_t op, value_ref arg, shared_ptr<type const> ret )
373
 
        {
 
381
        value_defn add_unary( unary_inst::op_t op, value_ref arg, shared_ptr<type const> ret ) {
374
382
                auto un = make_shared<unary_inst>();
375
383
                un->op = op;
376
384
                un->arg = arg;
380
388
        }
381
389
        
382
390
        value_ref builtin_function_unary( std::string const & name,
383
 
                shared_ptr<type const> a, shared_ptr<type const> ret= shared_ptr<type const>() )
384
 
        {
 
391
                shared_ptr<type const> a, shared_ptr<type const> ret= shared_ptr<type const>() ) {
385
392
                auto f = make_shared<value>( value::f_import_code );
386
393
                f->name = name;
387
 
                auto dt = make_shared<type>( type::a_raw );
 
394
                auto dt = make_shared<type>( get_types().shared_from_this(), type::a_raw );
388
395
                f->direct_type = dt;
389
396
                if( ret )
390
397
                        dt->sub.push_back( ret );
393
400
        }
394
401
        
395
402
        value_ref call_builtin_unary( std::string const & name,
396
 
                value_ref a, shared_ptr<type const> ret = shared_ptr<type const>() )
397
 
        {
 
403
                value_ref a, shared_ptr<type const> ret = shared_ptr<type const>() ) {
398
404
                auto f = builtin_function_unary( name, a.result_type(), ret );
399
405
                auto c = create_funccall_inst( f );
400
406
                c.add_arg( a );
412
418
                auto arr_type = array.result_type();
413
419
                if( arr_type->is_raw_pointer()) {
414
420
                        //assume pointer to array
415
 
                        ao->result = where->make_temp( type::get_raw_pointer( arr_type->item_type()->item_type() ) );
 
421
                        ao->result = where->make_temp( get_types().get_raw_pointer( arr_type->item_type()->item_type() ) );
416
422
                } else {
417
423
                        ao->result = where->make_temp( arr_type->item_type() );
418
424
                }
427
433
                ao->offset = offset;
428
434
                ao->lvalue = lvalue;
429
435
//              if( lvalue ) {
430
 
//                      ao->result = where->make_temp( type::get_raw_pointer( array.result_type()->item_type() ) );
 
436
//                      ao->result = where->make_temp( get_types().get_raw_pointer( array.result_type()->item_type() ) );
431
437
//              } else {
432
438
                        ao->result = where->make_temp( array.result_type()->item_type() );
433
439
//              }
482
488
                auto ci = make_shared<clone_inst>();
483
489
                ci->source_value = true;
484
490
                ci->source = source;
485
 
                ci->result = where->make_temp( type::get_raw_pointer( ci->source.result_type() ) );
 
491
                ci->result = where->make_temp( get_types().get_raw_pointer( ci->source.result_type() ) );
486
492
                add( ci );
487
493
                return ci->result;
488
494
        }
640
646
inline function_builder module_builder::create_function( std::string const & name, bool can_fail )
641
647
{
642
648
        auto f = cctx->get_cur_module().create_function( name );
643
 
        auto mt = make_shared<type>( type::f_function_ptr );
 
649
        auto mt = make_shared<type>( get_types().shared_from_this(), type::f_function_ptr );
644
650
        mt->access = type::a_raw;
645
651
        mt->can_fail = can_fail;
646
652
        f->defn_value->direct_type = mt;