/
rir_block.c
507 lines (463 loc) · 16 KB
/
rir_block.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
#include <ir/rir_block.h>
#include <rfbase/string/core.h>
#include <rfbase/string/manipulationx.h>
#include <ir/rir.h>
#include <ir/rir_object.h>
#include <ir/rir_expression.h>
#include <ir/rir_value.h>
#include <ir/rir_function.h>
#include <ir/rir_binaryop.h>
#include <ir/rir_constant.h>
#include <ir/rir_process.h>
#include <ast/block.h>
#include <ast/matchexpr.h>
#include <utils/common_strings.h>
/* -- functions for rir_block_exit -- */
static const struct RFstring rir_blockexit_type_strings[] = {
[RIR_BLOCK_EXIT_INVALID] = RF_STRING_STATIC_INIT("invalid"),
[RIR_BLOCK_EXIT_BRANCH] = RF_STRING_STATIC_INIT("branch"),
[RIR_BLOCK_EXIT_CONDBRANCH] = RF_STRING_STATIC_INIT("condbranch"),
[RIR_BLOCK_EXIT_RETURN] = RF_STRING_STATIC_INIT("return"),
};
const struct RFstring *rir_blockexit_type_str(enum rir_block_exit_type type)
{
return &rir_blockexit_type_strings[type];
}
i_INLINE_INS const struct RFstring *rir_block_exit_type_str(const struct rir_block_exit *b);
bool rir_block_exit_init_branch(struct rir_block_exit *exit,
struct rir_value *branch_dst)
{
exit->type = RIR_BLOCK_EXIT_BRANCH;
return rir_branch_init(&exit->branch, branch_dst);
}
bool rir_block_exit_init_condbranch(struct rir_block_exit *exit,
const struct rir_value *cond,
struct rir_value *taken,
struct rir_value *fallthrough)
{
exit->type = RIR_BLOCK_EXIT_CONDBRANCH;
return rir_condbranch_init(&exit->condbranch, cond, taken, fallthrough);
}
static inline void rir_block_exit_deinit(struct rir_block_exit *exit)
{
switch (exit->type) {
case RIR_BLOCK_EXIT_BRANCH:
rir_branch_deinit(&exit->branch);
break;
case RIR_BLOCK_EXIT_CONDBRANCH:
rir_condbranch_deinit(&exit->condbranch);
break;
case RIR_BLOCK_EXIT_INVALID:
// if we come here during EXIT_INVALID, it means parsing failed
case RIR_BLOCK_EXIT_RETURN:
// the return stmt should be a global rir object so is freed from global rir objects list
break;
}
}
void rir_block_exit_return_init(struct rir_block_exit *exit,
const struct rir_value *val)
{
exit->type = RIR_BLOCK_EXIT_RETURN;
rir_return_init(&exit->retstmt, val);
}
static bool rir_blockexit_tostring(struct rirtostr_ctx *ctx, const struct rir_block_exit *exitb)
{
bool ret = false;
RFS_PUSH();
switch (exitb->type) {
case RIR_BLOCK_EXIT_BRANCH:
if (exitb->branch.dst) {
if (!rir_branch_tostring(ctx, &exitb->branch)) {
goto end;
}
} else {
rf_stringx_append_cstr(ctx->rir->buff, "branch(NODESTINATION-FIXME)\n");
}
break;
case RIR_BLOCK_EXIT_CONDBRANCH:
if (!rir_condbranch_tostring(ctx, &exitb->condbranch)) {
goto end;
}
break;
case RIR_BLOCK_EXIT_RETURN:
if (exitb->retstmt.val) {
if (!rf_stringx_append(
ctx->rir->buff,
RFS(RIRTOSTR_INDENT"return("RFS_PF")\n",
RFS_PA(rir_value_string(exitb->retstmt.val)))
)) {
goto end;
}
} else {
if (!rf_stringx_append_cstr(ctx->rir->buff, RIRTOSTR_INDENT"return()\n")) {
goto end;
}
}
break;
case RIR_BLOCK_EXIT_INVALID:
RF_CRITICAL_FAIL("Should never happen");
goto end;
}
// success
ret = true;
end:
RFS_POP();
return ret;
}
static struct rir_object *rir_block_functionend_create_obj(bool has_return, struct rir_ctx *ctx)
{
const struct RFstring fend_label = RF_STRING_STATIC_INIT("function_end");
struct rir_object *ret = rir_object_create(RIR_OBJ_BLOCK, ctx->common.rir);
if (!ret) {
goto fail_free_ret;
}
struct rir_block *b = &ret->block;
RF_STRUCT_ZERO(b);
ctx->common.current_block = b;
rf_ilist_head_init(&b->expressions);
if (!rir_value_label_init_string(&ret->block.label, ret, &fend_label, &ctx->common)) {
goto fail_free_ret;
}
// current block's exit should be the return
struct rir_value *return_val = NULL;
if (has_return) {
if (rir_type_is_composite(rir_ctx_curr_fn(ctx)->retslot_val->type)) {
// if returning a user type, return directly
return_val = rir_ctx_curr_fn(ctx)->retslot_val;
} else { // else read the value from the pointer
struct rir_expression *e = rir_read_create(
rir_ctx_curr_fn(ctx)->retslot_val,
RIRPOS_AST,
ctx
);
if (!e) {
RF_ERROR("Could not create a read from a function's return slot");
goto fail_free_ret;
}
rir_common_block_add(&ctx->common, e);
return_val = &e->val;
}
}
rir_block_exit_return_init(&ret->block.exit, return_val);
b->st = rir_ctx_curr_st(ctx);
RF_ASSERT(b->st, "Symbol table should not be NULL");
return ret;
fail_free_ret:
free (ret);
return NULL;
}
struct rir_block *rir_block_functionend_create(bool has_return, struct rir_ctx *ctx)
{
struct rir_object *obj = rir_block_functionend_create_obj(has_return, ctx);
return obj ? &obj->block : NULL;
}
static bool rir_block_init(
struct rir_object *obj,
const struct RFstring *name,
enum rir_pos pos,
rir_data data
)
{
struct rir_block *b = &obj->block;
RF_STRUCT_ZERO(b);
rf_ilist_head_init(&b->expressions);
rir_data_curr_block(data) = b;
if (!rir_value_label_init_string(&b->label, obj, name, rir_data_common(data))) {
return false;
}
return true;
}
// @warning: wrap in RFS_PUSH() and RFS_POP()
static inline const struct RFstring *rir_block_from_ast_new_labelname(
enum block_position pos,
struct rir_ctx *ctx
)
{
if (pos == BLOCK_POSITION_FUNCTIONSTART) {
return &g_str_fnstart;
} else {
return RFS("label_%d", ctx->label_idx++);
}
}
static inline bool rir_block_init_from_ast_common(
struct rir_object *obj,
enum block_position pos,
struct rir_ctx *ctx
)
{
bool ret = false;
RFS_PUSH();
const struct RFstring *name = rir_block_from_ast_new_labelname(pos, ctx);
if (!name) {
goto end;
}
if (!rir_block_init(obj, name, RIRPOS_AST, ctx)) {
goto end;
}
// success
ret = true;
end:
RFS_POP();
return ret;
}
/**
* Initialize a rir block from an ast node
*
* @param obj The rir object of the block to get initialized
* @param n An ast node expression. Can be:
* - ast_block:
* in which case the ast_block creates an
* equivalent rir block, or
* - ast_expression
* case a rir block with that expression is created.
* - NULL
* in which case an empty rir block is created
* @param position Where the block is located. @see enum block_position
* for possible values
* @param ctx The rir context
*/
static bool rir_block_init_from_ast(
struct rir_object *obj,
const struct ast_node *n,
enum block_position pos,
struct rir_ctx *ctx)
{
struct rir_block *b = &obj->block;
if (!rir_block_init_from_ast_common(obj, pos, ctx)) {
return false;
}
if (n) {
// add basic block to the current function
rir_fndef_add_block(rir_ctx_curr_fn(ctx), b);
if (n->type == AST_BLOCK) {
ctx->current_ast_block = n;
struct symbol_table *block_st = ast_block_symbol_table_get((struct ast_node*)n);
struct rir_expression *curridx;
if (pos == BLOCK_POSITION_LOOP) {
rir_ctx_push_st(ctx, block_st->parent);
// create allocas for block's symbols and populate the symbol table with rir objects
rir_ctx_st_create_and_add_allocas(ctx);
// now get the loopvariable symbol table record object
struct symbol_table_record *rec = symbol_table_lookup_record(
block_st->parent,
ctx->loops.loopvar_str,
NULL
);
// read the current index
if (!(curridx = rir_read_create(
rir_object_value(ctx->loops.indexobj),
RIRPOS_AST,
ctx))) {
return false;
}
rir_common_block_add(&ctx->common, curridx);
// if it's a normal iteration of a collection
if (ctx->loops.itervalue) {
// create a comparison of current index to iterable's size
struct rir_object *idxaccessobj = rir_objidx_create_obj(
ctx->loops.itervalue,
&curridx->val,
RIRPOS_AST,
ctx
);
if (!idxaccessobj) {
return false;
}
rec->rirobj = idxaccessobj;
rir_common_block_add(&ctx->common, &idxaccessobj->expr);
} else { // it can only be a range iteration
// so get the index object directly
rec->rirobj = ctx->loops.indexobj;
}
}
// set current symbol table
rir_ctx_push_st(ctx, block_st);
// create allocas for block's symbols and populate the symbol table with rir objects
rir_ctx_st_create_and_add_allocas(ctx);
// for each expression of the block create a rir expression and add it to the block
struct ast_node **child;
darray_foreach(child, n->children) {
if (!rir_process_ast_node(*child, ctx)) {
return false;
}
}
b->st = rir_ctx_curr_st(ctx);
rir_ctx_pop_st(ctx);
if (pos == BLOCK_POSITION_LOOP) {
// add step to the index
struct rir_expression *add_step = rir_binaryop_create_nonast(
RIR_EXPRESSION_ADD,
&curridx->val,
ctx->loops.iterstep,
RIRPOS_AST,
ctx
);
rir_common_block_add(&ctx->common, add_step);
// write the new index to the index object
struct rir_expression *writenewidx = rir_write_create(
rir_object_value(ctx->loops.indexobj),
&add_step->val,
RIRPOS_AST,
ctx
);
if (!writenewidx) {
return false;
}
rir_common_block_add(&ctx->common, writenewidx);
rir_ctx_pop_st(ctx);
rir_ctx_reset_loopvars(ctx);
}
} else if (n->type == AST_MATCH_EXPRESSION) {
// process match expression as body
b->st = rir_ctx_curr_st(ctx);
RF_ASSERT(b->st, "Symbol table should not be NULL");
return rir_process_matchexpr((struct ast_node*)n, ctx);
} else {
RF_CRITICAL_FAIL("Should never get here");
return false;
}
} else {
b->st = rir_ctx_curr_st(ctx);
}
// success
RF_ASSERT(b->st, "Symbol table should not be NULL");
return true;
}
struct rir_object *rir_block_create_obj_from_ast(
const struct ast_node *n,
enum block_position pos,
struct rir_ctx *ctx
)
{
struct rir_object *ret = rir_object_create(RIR_OBJ_BLOCK, rir_ctx_rir(ctx));
if (!ret) {
return NULL;
}
if (!rir_block_init_from_ast(ret, n, pos, ctx)) {
RF_ERROR("Failed to initialize a rir block");
rir_object_listrem_destroy(ret, rir_ctx_rir(ctx), rir_ctx_curr_fn(ctx));
ret = NULL;
}
return ret;
}
struct rir_block *rir_block_create_from_ast(
const struct ast_node *n,
enum block_position pos,
struct rir_ctx *ctx
)
{
struct rir_object *obj = rir_block_create_obj_from_ast(n, pos, ctx);
return obj ? &obj->block : NULL;
}
static struct rir_object *rir_block_matchcase_create_obj(const struct ast_node *mcase,
struct rir_object *matched_rir_obj,
struct rir_ctx *ctx)
{
struct rir_object *ret = rir_object_create(RIR_OBJ_BLOCK, rir_ctx_rir(ctx));
if (!ret) {
goto fail;
}
struct rir_block *b = &ret->block;
if (!rir_block_init_from_ast_common(ret, BLOCK_POSITION_NORMAL, ctx)) {
goto fail;
}
AST_NODE_ASSERT_TYPE(mcase, AST_MATCH_CASE);
// add basic block to the current function
rir_fndef_add_block(rir_ctx_curr_fn(ctx), b);
// populate the match case allocas
if (!rir_match_st_populate_allocas(mcase, matched_rir_obj, ctx)) {
RF_ERROR("Failed to populate a match case's alloca in the RIR");
goto fail;
}
// finally process the contents of the match expression
if (!rir_process_ast_node(ast_matchcase_expression(mcase), ctx)) {
RF_ERROR("Failed to process a match case's expression in the RIR");
goto fail;
}
b->st = mcase->matchcase.st;
RF_ASSERT(b->st, "Symbol table should not be NULL");
return ret;
fail:
free(ret);
return NULL;
}
struct rir_block *rir_block_matchcase_create(const struct ast_node *mcase,
struct rir_object *matched_rir_obj,
struct rir_ctx *ctx)
{
struct rir_object *obj = rir_block_matchcase_create_obj(mcase, matched_rir_obj, ctx);
return obj ? &obj->block : NULL;
}
struct rir_object *rir_block_create_obj(
const struct RFstring *name,
enum rir_pos pos,
rir_data data
)
{
RF_ASSERT(pos == RIRPOS_PARSE, "At least for now only through parsing we get here");
struct rir_object *ret = rir_object_create(RIR_OBJ_BLOCK, rir_data_rir(data));
if (!ret) {
return NULL;
}
if (!rir_block_init(ret, name, pos, data)) {
RF_ERROR("Failed to initialize a rir block");
rir_object_listrem_destroy(ret, rir_data_rir(data), rir_data_curr_fn(data));
ret = NULL;
}
return ret;
}
struct rir_block *rir_block_create(
const struct RFstring *name,
enum rir_pos pos,
rir_data data
)
{
struct rir_object *obj = rir_block_create_obj(name, pos, data);
return obj ? &obj->block : NULL;
}
void rir_block_deinit(struct rir_block* b)
{
// block expressions are part of global oject list and will be destroyed there
rir_block_exit_deinit(&b->exit);
rir_value_deinit(&b->label);
}
void rir_block_destroy(struct rir_block* b)
{
rir_block_deinit(b);
free(b);
}
bool rir_block_tostring(struct rirtostr_ctx *ctx, const struct rir_block *b)
{
struct rir_expression *expr;
rirtostr_ctx_visit_block(ctx, b);
if (b->label.category == RIR_VALUE_LABEL) {
if (!rir_value_tostring(ctx->rir, &b->label)) {
return false;
}
if (!rf_stringx_append_cstr(ctx->rir->buff, "\n")) {
return false;
}
}
rf_ilist_for_each(&b->expressions, expr, ln) {
if (!rir_expression_tostring(ctx, expr)) {
return false;
}
}
if (!rir_blockexit_tostring(ctx, &b->exit)) {
return false;
}
return true;
}
const struct RFstring *rir_block_label_str(const struct rir_block *b)
{
if (b->label.category == RIR_VALUE_LABEL) {
return rir_value_string(&b->label);
}
return RFS("");
}
i_INLINE_INS bool rir_block_exit_initialized(const struct rir_block *b);
bool rir_block_is_first(const struct rir_block *b)
{
return rf_string_equal(&b->label.id, &g_str_fnstart);
}
i_INLINE_INS void rir_block_add_expr(struct rir_block *b, struct rir_expression *e);