Repository navigation
Expand file tree
/
Copy pathir_check.c
More file actions
682 lines (650 loc) · 21.1 KB
/
Copy pathir_check.c
File metadata and controls
682 lines (650 loc) · 21.1 KB
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
/*
* IR - Lightweight JIT Compilation Framework
* (IR verification)
* This file is part of the IR Project distributed under the MIT-style LICENSE.
* Authors: Dmitry Stogov <dmitry@php.net>
*/
#include "ir.h"
#include "ir_private.h"
void ir_consistency_check(void)
{
IR_ASSERT(IR_UNUSED == 0);
IR_ASSERT(IR_NOP == 0);
IR_ASSERT((int)IR_BOOL == (int)IR_C_BOOL);
IR_ASSERT((int)IR_U8 == (int)IR_C_U8);
IR_ASSERT((int)IR_U16 == (int)IR_C_U16);
IR_ASSERT((int)IR_U32 == (int)IR_C_U32);
IR_ASSERT((int)IR_U64 == (int)IR_C_U64);
IR_ASSERT((int)IR_ADDR == (int)IR_C_ADDR);
IR_ASSERT((int)IR_CHAR == (int)IR_C_CHAR);
IR_ASSERT((int)IR_I8 == (int)IR_C_I8);
IR_ASSERT((int)IR_I16 == (int)IR_C_I16);
IR_ASSERT((int)IR_I32 == (int)IR_C_I32);
IR_ASSERT((int)IR_I64 == (int)IR_C_I64);
IR_ASSERT((int)IR_DOUBLE == (int)IR_C_DOUBLE);
IR_ASSERT((int)IR_FLOAT == (int)IR_C_FLOAT);
IR_ASSERT((IR_EQ ^ 1) == IR_NE);
IR_ASSERT((IR_LT ^ 3) == IR_GT);
IR_ASSERT((IR_GT ^ 3) == IR_LT);
IR_ASSERT((IR_LE ^ 3) == IR_GE);
IR_ASSERT((IR_GE ^ 3) == IR_LE);
IR_ASSERT((IR_ULT ^ 3) == IR_UGT);
IR_ASSERT((IR_UGT ^ 3) == IR_ULT);
IR_ASSERT((IR_ULE ^ 3) == IR_UGE);
IR_ASSERT((IR_UGE ^ 3) == IR_ULE);
IR_ASSERT((IR_ORDERED ^ 1) == IR_UNORDERED);
IR_ASSERT(IR_ADD + 1 == IR_SUB);
}
typedef struct {
ir_arena *arena;
ir_bitset *use_set;
ir_bitset *input_set;
} ir_check_ctx;
static bool ir_check_use_list(ir_check_ctx *check_ctx, const ir_ctx *ctx, ir_ref from, ir_ref to)
{
ir_ref n, *p;
ir_use_list *use_list = &ctx->use_lists[from];
n = use_list->count;
if (n > 16) {
/* Avoid quadratic complexity by maintaining a temporary bit-set */
ir_bitset set;
if (!check_ctx->use_set || !(set = check_ctx->use_set[from])) {
if (!check_ctx->arena) {
check_ctx->arena = ir_arena_create(sizeof(ir_arena) +
ctx->insns_count * sizeof(ir_bitset) +
ir_bitset_len(ctx->insns_count) * sizeof(ir_bitset_base_t));
}
if (!check_ctx->use_set) {
check_ctx->use_set = ir_arena_alloc(&check_ctx->arena, ctx->insns_count * sizeof(ir_bitset));
memset(check_ctx->use_set, 0, ctx->insns_count * sizeof(ir_bitset));
}
check_ctx->use_set[from] = set = (ir_bitset)ir_arena_alloc(&check_ctx->arena,
ir_bitset_len(ctx->insns_count) * sizeof(ir_bitset_base_t));
memset(set, 0, ir_bitset_len(ctx->insns_count) * sizeof(ir_bitset_base_t));
for (p = &ctx->use_edges[use_list->refs]; n > 0; p++, n--) {
ir_bitset_incl(set, *p);
}
}
return ir_bitset_in(set, to);
}
for (p = &ctx->use_edges[use_list->refs]; n > 0; p++, n--) {
if (*p == to) {
return 1;
}
}
return 0;
}
static bool ir_check_input_list(ir_check_ctx *check_ctx, const ir_ctx *ctx, ir_ref from, ir_ref to)
{
ir_insn *insn = &ctx->ir_base[to];
ir_ref n, j, *p;
n = ir_input_edges_count(ctx, insn);
if (n > 16) {
/* Avoid quadratic complexity by maintaining a temporary bit-set */
ir_bitset set;
if (!check_ctx->input_set || !(set = check_ctx->input_set[to])) {
if (!check_ctx->arena) {
check_ctx->arena = ir_arena_create(sizeof(ir_arena) +
ctx->insns_count * sizeof(ir_bitset) +
ir_bitset_len(ctx->insns_count) * sizeof(ir_bitset_base_t));
}
if (!check_ctx->input_set) {
check_ctx->input_set = ir_arena_alloc(&check_ctx->arena, ctx->insns_count * sizeof(ir_bitset));
memset(check_ctx->input_set, 0, ctx->insns_count * sizeof(ir_bitset));
}
check_ctx->input_set[to] = set = (ir_bitset)ir_arena_alloc(&check_ctx->arena,
ir_bitset_len(ctx->insns_count) * sizeof(ir_bitset_base_t));
memset(set, 0, ir_bitset_len(ctx->insns_count) * sizeof(ir_bitset_base_t));
for (j = 1, p = insn->ops + 1; j <= n; j++, p++) {
if (*p > 0) ir_bitset_incl(set, *p);
}
}
return ir_bitset_in(set, from);
}
for (j = 1, p = insn->ops + 1; j <= n; j++, p++) {
if (*p == from) {
return 1;
}
}
return 0;
}
static bool ir_check_domination(const ir_ctx *ctx, ir_ref def, ir_ref use)
{
uint32_t b1 = ctx->cfg_map[def];
uint32_t b2 = ctx->cfg_map[use];
ir_block *blocks = ctx->cfg_blocks;
uint32_t b1_depth = blocks[b1].dom_depth;
const ir_block *bb2 = &blocks[b2];
if (b1 == b2) {
return def < use;
}
while (bb2->dom_depth > b1_depth) {
b2 = bb2->dom_parent;
bb2 = &blocks[b2];
}
return b1 == b2;
}
bool ir_check(const ir_ctx *ctx)
{
ir_ref i, j, n, *p, use;
ir_insn *insn, *use_insn;
ir_type type;
uint32_t flags;
bool ok = 1;
ir_check_ctx check_ctx;
if (ctx->insns_count < 1 || ctx->ir_base[1].op != IR_START) {
fprintf(stderr, "ir_base[1].op invalid opcode (%d)\n",
(ctx->insns_count < 1) ? IR_NOP : ctx->ir_base[0].op);
ok = 0;
}
check_ctx.arena = NULL;
check_ctx.use_set = NULL;
check_ctx.input_set = NULL;
for (i = IR_UNUSED + 1, insn = ctx->ir_base + i; i < ctx->insns_count;) {
if (insn->op >= IR_LAST_OP) {
fprintf(stderr, "ir_base[%d].op invalid opcode (%d)\n", i, insn->op);
ok = 0;
break;
}
flags = ir_op_flags[insn->op];
if (flags & IR_OP_FLAG_DATA) {
IR_ASSERT((flags & (IR_OP_FLAG_MEM|IR_OP_FLAG_CONTROL)) == 0);
if (insn->type == IR_VOID) {
fprintf(stderr, "ir_base[%d].type must be set (instruction %s)\n", i, ir_op_name[insn->op]);
ok = 0;
} else {
switch (insn->op) {
case IR_EQ:
case IR_NE:
case IR_LT:
case IR_GE:
case IR_LE:
case IR_GT:
case IR_ULT:
case IR_UGE:
case IR_ULE:
case IR_UGT:
case IR_ORDERED:
case IR_UNORDERED:
if (insn->type != IR_BOOL && !IR_IS_TYPE_VECTOR(insn->type)) {
fprintf(stderr, "ir_base[%d].type must be bool or vector (instruction %s)\n", i, ir_op_name[insn->op]);
ok = 0;
}
break;
case IR_SEXT:
case IR_ZEXT:
case IR_TRUNC:
case IR_FP2INT:
if (!IR_IS_TYPE_INT(insn->type)
&& (!IR_IS_TYPE_VECTOR(insn->type) || !IR_IS_TYPE_INT(IR_VECTOR_BASE_TYPE(insn->type)))) {
fprintf(stderr, "ir_base[%d].type must be integer or vector (instruction %s)\n", i, ir_op_name[insn->op]);
ok = 0;
}
break;
case IR_INT2FP:
case IR_FP2FP:
if (!IR_IS_TYPE_FP(insn->type)
&& (!IR_IS_TYPE_VECTOR(insn->type) || !IR_IS_TYPE_FP(IR_VECTOR_BASE_TYPE(insn->type)))) {
fprintf(stderr, "ir_base[%d].type must be real number or vector (instruction %s)\n", i, ir_op_name[insn->op]);
ok = 0;
}
break;
case IR_PROTO:
case IR_VADDR:
case IR_FRAME_ADDR:
case IR_ARGVAL:
if (insn->type != IR_ADDR) {
fprintf(stderr, "ir_base[%d].type must be uintptr_t (instruction %s)\n", i, ir_op_name[insn->op]);
ok = 0;
}
break;
case IR_ADD_OV:
case IR_SUB_OV:
case IR_MUL_OV:
if (!IR_IS_TYPE_INT(insn->type)) {
fprintf(stderr, "ir_base[%d].type must be integer (instruction %s)\n", i, ir_op_name[insn->op]);
ok = 0;
}
break;
case IR_OVERFLOW:
if (insn->type != IR_BOOL) {
fprintf(stderr, "ir_base[%d].type must be bool (instruction %s)\n", i, ir_op_name[insn->op]);
ok = 0;
}
break;
case IR_NOT:
if (insn->type != IR_BOOL && !IR_IS_TYPE_INT(insn->type) && !IR_IS_TYPE_VECTOR(insn->type)) {
fprintf(stderr, "ir_base[%d].type must be bool, integer or vector (instruction %s)\n", i, ir_op_name[insn->op]);
ok = 0;
}
break;
case IR_OR:
case IR_AND:
case IR_XOR:
case IR_SHL:
case IR_SHR:
case IR_SAR:
if (!IR_IS_TYPE_INT(insn->type)
&& (!IR_IS_TYPE_VECTOR(insn->type) || !IR_IS_TYPE_INT(IR_VECTOR_BASE_TYPE(insn->type)))) {
fprintf(stderr, "ir_base[%d].type must be integer or vector (instruction %s)\n", i, ir_op_name[insn->op]);
ok = 0;
}
break;
case IR_ROL:
case IR_ROR:
case IR_BSWAP:
if (!IR_IS_TYPE_INT(insn->type)) {
fprintf(stderr, "ir_base[%d].type must be integer (instruction %s)\n", i, ir_op_name[insn->op]);
ok = 0;
}
break;
case IR_CTPOP:
case IR_CTLZ:
case IR_CTTZ:
if (!IR_IS_TYPE_INT(insn->type)) {
fprintf(stderr, "ir_base[%d].type must be integer (instruction %s)\n", i, ir_op_name[insn->op]);
ok = 0;
} else if (ir_type_size[insn->type] > sizeof(void*)) {
fprintf(stderr, "ir_base[%d].type size must fit into a %d-bit integer (instruction %s)\n", i, (int)sizeof(void*) * 8, ir_op_name[insn->op]);
ok = 0;
}
break;
case IR_EXTRACT:
if (!IR_IS_TYPE_INT(insn->type) && !IR_IS_TYPE_FP(insn->type)) {
fprintf(stderr, "ir_base[%d].type must be integer or real (instruction %s)\n", i, ir_op_name[insn->op]);
ok = 0;
}
break;
case IR_REPLACE:
case IR_SPLAT:
case IR_SHUFFLE:
if (!IR_IS_TYPE_VECTOR(insn->type)) {
fprintf(stderr, "ir_base[%d].type must be vector (instruction %s)\n", i, ir_op_name[insn->op]);
ok = 0;
}
break;
default:
break;
}
}
} else if (flags & IR_OP_FLAG_MEM) {
IR_ASSERT(flags & IR_OP_FLAG_CONTROL);
if ((flags & IR_OP_FLAG_MEM_MASK) == IR_OP_FLAG_MEM_LOAD) {
if (insn->type == IR_VOID) {
fprintf(stderr, "ir_base[%d].type must be set (instruction %s)\n", i, ir_op_name[insn->op]);
ok = 0;
}
} else if ((flags & IR_OP_FLAG_MEM_MASK) == IR_OP_FLAG_MEM_ALLOC) {
if (insn->type == IR_VOID) {
fprintf(stderr, "ir_base[%d].type must be set (instruction %s)\n", i, ir_op_name[insn->op]);
ok = 0;
} else if (insn->type != IR_ADDR) {
fprintf(stderr, "ir_base[%d].type must be uintptr_t (instruction %s)\n", i, ir_op_name[insn->op]);
ok = 0;
}
} else if ((flags & IR_OP_FLAG_MEM_MASK) == IR_OP_FLAG_MEM_STORE) {
if (insn->type != IR_VOID) {
fprintf(stderr, "ir_base[%d].type must be empty (instruction %s)\n", i, ir_op_name[insn->op]);
ok = 0;
}
} else {
IR_ASSERT((flags & IR_OP_FLAG_MEM_MASK) == IR_OP_FLAG_MEM_CALL);
}
} else if (flags & IR_OP_FLAG_CONTROL) {
if (insn->type != IR_VOID) {
fprintf(stderr, "ir_base[%d].type must be empty (instruction %s)\n", i, ir_op_name[insn->op]);
ok = 0;
}
}
n = ir_input_edges_count(ctx, insn);
for (j = 1, p = insn->ops + 1; j <= n; j++, p++) {
use = *p;
if (use != IR_UNUSED) {
if (IR_IS_CONST_REF(use)) {
if (IR_OPND_KIND(flags, j) != IR_OPND_DATA) {
fprintf(stderr, "ir_base[%d].ops[%d] reference (%d) must not be constant\n", i, j, use);
ok = 0;
} else if (-use >= ctx->consts_count) {
fprintf(stderr, "ir_base[%d].ops[%d] constant reference (%d) is out of range\n", i, j, use);
ok = 0;
}
} else {
if (use >= ctx->insns_count) {
fprintf(stderr, "ir_base[%d].ops[%d] insn reference (%d) is out of range\n", i, j, use);
ok = 0;
continue;
}
use_insn = &ctx->ir_base[use];
switch (IR_OPND_KIND(flags, j)) {
case IR_OPND_DATA:
if (!(ir_op_flags[use_insn->op] & IR_OP_FLAG_DATA)) {
if (!(ir_op_flags[use_insn->op] & IR_OP_FLAG_MEM)
|| use_insn->type == IR_VOID) {
fprintf(stderr, "ir_base[%d].ops[%d] reference (%d) must be DATA\n", i, j, use);
ok = 0;
}
}
if ((ctx->flags2 & IR_LINEAR)
&& use >= i
&& !(insn->op == IR_PHI && ctx->ir_base[insn->op1].op == IR_LOOP_BEGIN)) {
fprintf(stderr, "ir_base[%d].ops[%d] invalid forward reference (%d)\n", i, j, use);
ok = 0;
}
if (flags & IR_OP_FLAG_DATA) {
switch (insn->op) {
case IR_COND:
if (j == 1) {
break;
}
IR_FALLTHROUGH;
case IR_ADD:
case IR_SUB:
case IR_MUL:
case IR_DIV:
case IR_MOD:
case IR_NEG:
case IR_ABS:
case IR_ADD_OV:
case IR_SUB_OV:
case IR_MUL_OV:
case IR_NOT:
case IR_OR:
case IR_AND:
case IR_XOR:
case IR_SHL:
case IR_SHR:
case IR_SAR:
case IR_ROL:
case IR_ROR:
case IR_BSWAP:
case IR_MIN:
case IR_MAX:
case IR_PHI:
case IR_COPY:
case IR_PI:
if (insn->type != use_insn->type) {
if (j == 2
&& (insn->op == IR_SHL
|| insn->op == IR_SHR
|| insn->op == IR_SAR
|| insn->op == IR_ROL
|| insn->op == IR_ROR)
&& IR_IS_TYPE_INT(use_insn->type)
&& (IR_IS_TYPE_INT(insn->type) || IR_IS_TYPE_VECTOR(insn->type))) {
/* second argument of SHIFT may be incompatible with result */
break;
}
if (insn->op == IR_NOT && insn->type == IR_BOOL) {
/* boolean not */
break;
}
if (insn->type == IR_ADDR && (use_insn->type == IR_UINTPTR_T || use_insn->type == IR_INTPTR_T)) {
break;
} else if (use_insn->type == IR_ADDR && (insn->type == IR_UINTPTR_T || insn->type == IR_INTPTR_T)) {
break;
}
fprintf(stderr, "ir_base[%d].ops[%d] (%d) type is incompatible with result type (%d != %d)\n",
i, j, use, use_insn->type, insn->type);
ok = 0;
}
break;
}
}
if ((ctx->flags2 & IR_LINEAR)
&& ctx->cfg_map
&& insn->op != IR_PHI
&& !ir_check_domination(ctx, use, i)) {
fprintf(stderr, "ir_base[%d].ops[%d] -> %d, %d doesn't dominate %d\n", i, j, use, use, i);
ok = 0;
}
break;
case IR_OPND_CONTROL:
if (flags & IR_OP_FLAG_BB_START) {
if (!(ir_op_flags[use_insn->op] & IR_OP_FLAG_BB_END)) {
fprintf(stderr, "ir_base[%d].ops[%d] reference (%d) must be BB_END\n", i, j, use);
ok = 0;
}
} else {
if (ir_op_flags[use_insn->op] & IR_OP_FLAG_BB_END) {
fprintf(stderr, "ir_base[%d].ops[%d] reference (%d) must not be BB_END\n", i, j, use);
ok = 0;
}
}
if ((ctx->flags2 & IR_LINEAR)
&& use >= i
&& !(insn->op == IR_LOOP_BEGIN)) {
fprintf(stderr, "ir_base[%d].ops[%d] invalid forward reference (%d)\n", i, j, use);
ok = 0;
}
break;
case IR_OPND_CONTROL_DEP:
if ((ctx->flags2 & IR_LINEAR)
&& use >= i) {
fprintf(stderr, "ir_base[%d].ops[%d] invalid forward reference (%d)\n", i, j, use);
ok = 0;
} else if (insn->op == IR_PHI) {
ir_insn *merge_insn = &ctx->ir_base[insn->op1];
if (merge_insn->op != IR_MERGE && merge_insn->op != IR_LOOP_BEGIN) {
fprintf(stderr, "ir_base[%d].ops[%d] reference (%d) must be MERGE or LOOP_BEGIN\n", i, j, use);
ok = 0;
}
}
break;
case IR_OPND_CONTROL_REF:
if (!(ir_op_flags[use_insn->op] & IR_OP_FLAG_CONTROL)) {
fprintf(stderr, "ir_base[%d].ops[%d] reference (%d) must be CONTROL\n", i, j, use);
ok = 0;
}
break;
case IR_OPND_CONTROL_GUARD:
if (!(ir_op_flags[use_insn->op] & IR_OP_FLAG_BB_START)
&& use_insn->op != IR_GUARD
&& use_insn->op != IR_GUARD_NOT) {
fprintf(stderr, "ir_base[%d].ops[%d] reference (%d) must be BB_START or GUARD\n", i, j, use);
ok = 0;
}
break;
default:
fprintf(stderr, "ir_base[%d].ops[%d] reference (%d) of unsupported kind\n", i, j, use);
ok = 0;
}
}
} else if ((insn->op == IR_RETURN || insn->op == IR_UNREACHABLE) && j == 2) {
/* pass (function returns void) */
} else if (insn->op == IR_BEGIN && j == 1) {
/* pass (start of unreachable basic block) */
} else if (IR_OPND_KIND(flags, j) == IR_OPND_CONTROL_GUARD) {
/* reference to control guard is optional */
} else if (IR_OPND_KIND(flags, j) != IR_OPND_CONTROL_REF
&& (insn->op != IR_SNAPSHOT || j == 1)) {
fprintf(stderr, "ir_base[%d].ops[%d] missing reference (%d)\n", i, j, use);
ok = 0;
}
if (ctx->use_lists
&& use > 0
&& !ir_check_use_list(&check_ctx, ctx, use, i)) {
fprintf(stderr, "ir_base[%d].ops[%d] is not in use list (%d)\n", i, j, use);
ok = 0;
}
}
switch (insn->op) {
case IR_PHI:
if (insn->inputs_count != ctx->ir_base[insn->op1].inputs_count + 1) {
fprintf(stderr, "ir_base[%d] inconsistent PHI inputs_count (%d != %d)\n",
i, insn->inputs_count, ctx->ir_base[insn->op1].inputs_count + 1);
ok = 0;
}
break;
case IR_LOAD:
case IR_LOAD_v:
case IR_STORE:
case IR_STORE_v:
type = ctx->ir_base[insn->op2].type;
if (type != IR_ADDR
&& (!IR_IS_TYPE_INT(type) || ir_type_size[type] != ir_type_size[IR_ADDR])) {
fprintf(stderr, "ir_base[%d].op2 must have ADDR type (%s)\n",
i, IR_IS_TYPE_VECTOR(type) ? "VECTOR" : ir_type_name[type]);
ok = 0;
}
break;
case IR_VLOAD:
case IR_VLOAD_v:
case IR_VSTORE:
case IR_VSTORE_v:
if (ctx->ir_base[insn->op2].op != IR_VAR) {
fprintf(stderr, "ir_base[%d].op2 must be 'VAR' (%s)\n",
i, ir_op_name[ctx->ir_base[insn->op2].op]);
ok = 0;
}
break;
case IR_RETURN:
if (ctx->ret_type != (insn->op2 ? ctx->ir_base[insn->op2].type : IR_VOID)) {
fprintf(stderr, "ir_base[%d].type incompatible return type\n", i);
ok = 0;
}
break;
case IR_TAILCALL:
if (ctx->ret_type != insn->type) {
fprintf(stderr, "ir_base[%d].type incompatible return type\n", i);
ok = 0;
}
break;
case IR_PARAM:
if (i > 2 && ctx->ir_base[i - 1].op != IR_PARAM) {
fprintf(stderr, "ir_base[%d].op PARAMs must be used only right after START\n", i);
ok = 0;
}
break;
}
if (ctx->use_lists) {
ir_use_list *use_list = &ctx->use_lists[i];
ir_ref count, n = use_list->count;
for (p = &ctx->use_edges[use_list->refs]; n > 0; p++, n--) {
use = *p;
if (!ir_check_input_list(&check_ctx, ctx, i, use)) {
fprintf(stderr, "ir_base[%d] is in use list of ir_base[%d]\n", use, i);
ok = 0;
}
}
if ((flags & IR_OP_FLAG_CONTROL) && !(flags & IR_OP_FLAG_MEM)) {
switch (insn->op) {
case IR_SWITCH:
/* may have many successors */
if (use_list->count < 1) {
fprintf(stderr, "ir_base[%d].op (SWITCH) must have at least 1 successor (%d)\n", i, use_list->count);
ok = 0;
}
break;
case IR_IF:
if (use_list->count != 2) {
fprintf(stderr, "ir_base[%d].op (IF) must have 2 successors (%d)\n", i, use_list->count);
ok = 0;
}
break;
case IR_UNREACHABLE:
case IR_RETURN:
if (use_list->count == 1) {
/* UNREACHABLE and RETURN may be linked with the following ENTRY by a fake edge */
if (ctx->ir_base[ctx->use_edges[use_list->refs]].op == IR_ENTRY) {
break;
}
}
IR_FALLTHROUGH;
case IR_IJMP:
if (use_list->count != 0) {
fprintf(stderr, "ir_base[%d].op (%s) must not have successors (%d)\n",
i, ir_op_name[insn->op], use_list->count);
ok = 0;
}
break;
case IR_IGOTO:
case IR_ASM_GOTO:
break;
default:
/* skip data references */
count = n = use_list->count;
for (p = &ctx->use_edges[use_list->refs]; n > 0; p++, n--) {
use = *p;
if (!(ir_op_flags[ctx->ir_base[use].op] & IR_OP_FLAG_CONTROL)) {
count--;
}
}
if (count != 1) {
if (insn->op == IR_CALL && count == 2) {
/* result of CALL may be used as data in control instruction */
break;
}
if ((insn->op == IR_LOOP_END || insn->op == IR_END) && count == 2) {
/* LOOP_END/END may be linked with the following ENTRY by a fake edge */
if (ctx->ir_base[ctx->use_edges[use_list->refs]].op == IR_ENTRY) {
count--;
}
if (ctx->ir_base[ctx->use_edges[use_list->refs + 1]].op == IR_ENTRY) {
count--;
}
if (count == 1) {
break;
}
}
if (count == 0 && (insn->op == IR_END || insn->op == IR_LOOP_END)) {
/* Dead block */
break;
}
fprintf(stderr, "ir_base[%d].op (%s) must have 1 successor (%d)\n",
i, ir_op_name[insn->op], count);
ok = 0;
}
break;
}
}
}
n = ir_insn_inputs_to_len(n);
i += n;
insn += n;
}
if (check_ctx.arena) {
ir_arena_free(check_ctx.arena);
}
// if (!ok) {
// ir_dump_codegen(ctx, stderr);
// }
#ifndef IR_CHECK_NO_ABORT
IR_ASSERT(ok);
#endif
return ok;
}
bool ir_check_prototype(const ir_ctx *ctx, uint32_t flags, uint8_t ret_type, uint32_t params_count, uint8_t *param_types)
{
bool ok = 1;
ir_ref ref = 2;
uint32_t n = 0;
while (ref < ctx->insns_count && ctx->ir_base[ref].op == IR_PARAM) {
if (n >= params_count) {
fprintf(stderr, "parameter count doesn't match function signature\n");
ok = 0;
break;
} else if (ctx->ir_base[ref].type != param_types[n]) {
fprintf(stderr, "parameter %d type doesn't match function signature\n", n);
ok = 0;
}
ref++;
n++;
}
if (n < params_count) {
fprintf(stderr, "parameter count doesn't match function signature\n");
ok = 0;
}
if ((flags & IR_VARARG_FUNC) != (ctx->flags & IR_VARARG_FUNC)) {
fprintf(stderr, "IR_VARARG_FUNC flag doesn't match function signature\n");
ok = 0;
}
if (ret_type != ctx->ret_type) {
fprintf(stderr, "return type doesn't match function signature\n");
ok = 0;
}
if ((flags & IR_CALL_CONV_MASK) != (ctx->flags & IR_CALL_CONV_MASK)) {
fprintf(stderr, "calling convention doesn't match function signature\n");
ok = 0;
}
return ok;
}