-
Notifications
You must be signed in to change notification settings - Fork 77
/
vmir_value.c
1074 lines (918 loc) · 24.5 KB
/
vmir_value.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
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
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2016 Lonelycoder AB
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
typedef enum {
IR_VC_UNDEF,
IR_VC_FUNCTION,
IR_VC_GLOBALVAR,
IR_VC_CONSTANT,
IR_VC_TEMPORARY,
IR_VC_REGFRAME,
IR_VC_MACHINEREG,
IR_VC_DATA,
IR_VC_CE, // Const expression
IR_VC_AGGREGATE,
IR_VC_ALIAS,
IR_VC_ZERO_INITIALIZER,
IR_VC_DEAD, // Value is not used
} ir_value_class_t;
typedef struct ir_constexpr {
int ic_code;
union {
struct {
int op;
int src;
} ic_cast;
struct {
int num_values;
int *values;
} ic_gep;
struct {
int op;
int lhs;
int rhs;
} ic_binop;
struct {
int opty;
int lhs;
int rhs;
int pred;
} ic_cmp;
};
} ir_constexpr_t;
/**
* Relation between instruction and temporaries
*/
typedef struct ir_value_instr {
LIST_ENTRY(ir_value_instr) ivi_value_link;
LIST_ENTRY(ir_value_instr) ivi_instr_link;
struct ir_value *ivi_value;
struct ir_instr *ivi_instr;
enum {
IVI_INPUT,
IVI_OUTPUT,
} ivi_relation;
} ir_value_instr_t;
/**
*
*/
typedef struct ir_value {
ir_value_class_t iv_class;
struct ir_value_instr_list iv_instructions;
char *iv_name;
void *iv_data;
int iv_id;
int iv_type;
int iv_edges;
union {
// These guys must be in a union cause how we initialize float/double
// constants
uint32_t iv_u32;
uint64_t iv_u64;
float iv_float;
double iv_double;
ir_function_t *iv_func;
ir_globalvar_t *iv_gvar;
ir_constexpr_t *iv_ce;
int iv_reg;
int iv_num_values; // For aggregate types
int iv_jit; // For IR_VC_TEMPORARY that only exist in JITed code (Machine registers)
};
int iv_precolored;
SLIST_ENTRY(ir_value) iv_tmp_link;
} ir_value_t;
static const char *value_str(ir_unit_t *iu, const ir_value_t *iv);
static const char *value_str_id(ir_unit_t *iu, int id);
static const char *value_str_vt(ir_unit_t *iu, const ir_valuetype_t vt);
/**
*
*/
static void
value_bind_instr(ir_value_t *iv, ir_instr_t *ii, int relation)
{
ir_value_instr_t *ivi = malloc(sizeof(ir_value_instr_t));
ivi->ivi_instr = ii;
ivi->ivi_value = iv;
ivi->ivi_relation = relation;
LIST_INSERT_HEAD(&ii->ii_values, ivi, ivi_instr_link);
LIST_INSERT_HEAD(&iv->iv_instructions, ivi, ivi_value_link);
}
/**
*
*/
static ir_instr_t *
value_get_assigning_instr(ir_unit_t *iu, ir_value_t *iv)
{
ir_value_instr_t *ivi;
LIST_FOREACH(ivi, &iv->iv_instructions, ivi_value_link)
if(ivi->ivi_relation == IVI_OUTPUT)
return ivi->ivi_instr;
return NULL;
}
/**
*
*/
static void
ivi_destroy(ir_value_instr_t *ivi)
{
LIST_REMOVE(ivi, ivi_instr_link);
LIST_REMOVE(ivi, ivi_value_link);
free(ivi);
}
/**
*
*/
static void
instr_bind_clear(ir_instr_t *ii)
{
ir_value_instr_t *ivi;
while((ivi = LIST_FIRST(&ii->ii_values)) != NULL)
ivi_destroy(ivi);
}
/**
*
*/
static void
instr_bind_clear_inputs(ir_instr_t *ii)
{
ir_value_instr_t *ivi, *next;
for(ivi = LIST_FIRST(&ii->ii_values); ivi != NULL; ivi = next) {
next = LIST_NEXT(ivi, ivi_instr_link);
if(ivi->ivi_relation == IVI_INPUT)
ivi_destroy(ivi);
}
}
/**
*
*/
static void
value_clear(ir_value_t *iv)
{
free(iv->iv_name);
iv->iv_name = NULL;
free(iv->iv_data);
iv->iv_data = NULL;
ir_value_instr_t *ivi;
while((ivi = LIST_FIRST(&iv->iv_instructions)) != NULL)
ivi_destroy(ivi);
switch(iv->iv_class) {
case IR_VC_GLOBALVAR:
free(iv->iv_gvar->ig_name);
free(iv->iv_gvar);
break;
default:
break;
}
iv->iv_class = IR_VC_UNDEF;
}
/**
*
*/
static void
value_resize(ir_unit_t *iu, int newsize)
{
assert(newsize <= VECTOR_LEN(&iu->iu_values));
for(int i = newsize; i < VECTOR_LEN(&iu->iu_values); i++) {
ir_value_t *iv = VECTOR_ITEM(&iu->iu_values, i);
if(iv == NULL)
continue;
VECTOR_ITEM(&iu->iu_values, i) = NULL;
value_clear(iv);
free(iv);
}
iu->iu_next_value = newsize;
}
/**
*
*/
static int
value_append(ir_unit_t *iu)
{
ir_value_t *iv;
if(iu->iu_next_value == VECTOR_LEN(&iu->iu_values)) {
iv = calloc(1, sizeof(ir_value_t));
VECTOR_PUSH_BACK(&iu->iu_values, iv);
} else {
assert(iu->iu_next_value < VECTOR_LEN(&iu->iu_values));
if(VECTOR_ITEM(&iu->iu_values, iu->iu_next_value) == NULL) {
iv = calloc(1, sizeof(ir_value_t));
VECTOR_ITEM(&iu->iu_values, iu->iu_next_value) = iv;
} else {
iv = VECTOR_ITEM(&iu->iu_values, iu->iu_next_value);
}
}
iv->iv_id = iu->iu_next_value;
return iu->iu_next_value++;
}
/**
*
*/
static unsigned int __attribute__((unused))
value_reg(const ir_value_t *iv)
{
assert(iv->iv_class == IR_VC_REGFRAME);
return iv->iv_reg;
}
/**
*
*/
static ir_function_t *__attribute__((unused))
value_function(ir_unit_t *iu, int id)
{
if(id >= VECTOR_LEN(&iu->iu_values))
return NULL;
const ir_value_t *iv = VECTOR_ITEM(&iu->iu_values, id);
if(iv->iv_class != IR_VC_FUNCTION)
return NULL;
return iv->iv_func;
}
/**
*
*/
static uint32_t __attribute__((unused))
value_function_addr(const ir_value_t *iv)
{
assert(iv->iv_class == IR_VC_FUNCTION);
return iv->iv_func->if_gfid;
}
/**
*
*/
static ir_value_t *
value_append_and_get(ir_unit_t *iu)
{
const int val = value_append(iu);
return VECTOR_ITEM(&iu->iu_values, val);
}
/**
*
*/
static ir_value_t *
value_get(ir_unit_t *iu, unsigned int index)
{
if(index >= VECTOR_LEN(&iu->iu_values))
parser_error(iu, "Bad value index %d", index);
return VECTOR_ITEM(&iu->iu_values, index);
}
/**
*
*/
static void
value_bind_return_value(ir_unit_t *iu, ir_instr_t *ii)
{
value_bind_instr(value_get(iu, ii->ii_ret.value), ii, IVI_OUTPUT);
}
/**
*
*/
static void
value_alloc_instr_ret(ir_unit_t *iu, int type, struct ir_instr *ii)
{
int val = value_append(iu);
ir_value_t *iv = VECTOR_ITEM(&iu->iu_values, val);
iv->iv_class = IR_VC_TEMPORARY;
iv->iv_type = type;
iv->iv_precolored = -1;
ii->ii_ret.value = val;
ii->ii_ret.type = type;
value_bind_instr(iv, ii, IVI_OUTPUT);
}
/**
*
*/
static ir_valuetype_t
value_alloc_temporary(ir_unit_t *iu, int type)
{
int val = value_append(iu);
ir_value_t *iv = VECTOR_ITEM(&iu->iu_values, val);
iv->iv_class = IR_VC_TEMPORARY;
iv->iv_precolored = -1;
iv->iv_type = type;
return (ir_valuetype_t) {.value = val, .type = type};
}
/**
*
*/
static int
value_regframe_slots(ir_unit_t *iu, int type)
{
ir_type_t *it = type_get(iu, type);
switch(it->it_code) {
case IR_TYPE_INT1:
case IR_TYPE_INT8:
case IR_TYPE_INT16:
case IR_TYPE_INT32:
case IR_TYPE_FLOAT:
case IR_TYPE_POINTER:
case IR_TYPE_FUNCTION:
return 1;
case IR_TYPE_INT64:
case IR_TYPE_DOUBLE:
return 2;
case IR_TYPE_INTx:
if(it->it_bits <= 32)
return 1;
else
return 2;
default:
parser_error(iu, "Can't determine regframe slots for type %s",
type_str(iu, it));
}
}
/**
*
*/
static int
value_regframe_size(ir_unit_t *iu, int type)
{
return value_regframe_slots(iu, type) * 4;
}
/**
*
*/
static void
value_alloc_function_arg(ir_unit_t *iu, int type)
{
int val = value_append(iu);
ir_value_t *iv = VECTOR_ITEM(&iu->iu_values, val);
iv->iv_class = IR_VC_REGFRAME;
iv->iv_type = type;
ir_function_t *f = iu->iu_current_function;
int size = value_regframe_size(iu, type);
f->if_callarg_size -= size;
iv->iv_reg = f->if_callarg_size;
}
/**
*
*/
static uint32_t __attribute__((unused))
value_get_const32(ir_unit_t *iu, const ir_value_t *iv)
{
ir_type_t *it = type_get(iu, iv->iv_type);
switch(iv->iv_class) {
case IR_VC_GLOBALVAR:
return iv->iv_gvar->ig_addr;
case IR_VC_CONSTANT:
switch(it->it_code) {
case IR_TYPE_INT1:
return !!iv->iv_u32;
case IR_TYPE_INT8:
case IR_TYPE_INT16:
case IR_TYPE_INT32:
case IR_TYPE_POINTER:
case IR_TYPE_FLOAT:
return iv->iv_u32;
case IR_TYPE_INTx:
case IR_TYPE_INT64:
case IR_TYPE_DOUBLE:
return iv->iv_u64;
default:
break;
}
break;
default:
break;
}
parser_error(iu, "Unable to value_get_const for value %s",
value_str(iu, iv));
}
/**
*
*/
static uint32_t __attribute__((unused))
value_get_const(ir_unit_t *iu, const ir_value_t *iv)
{
ir_type_t *it = type_get(iu, iv->iv_type);
switch(iv->iv_class) {
case IR_VC_GLOBALVAR:
return iv->iv_gvar->ig_addr;
case IR_VC_CONSTANT:
switch(it->it_code) {
case IR_TYPE_INT1:
return !!iv->iv_u32;
case IR_TYPE_INT8:
case IR_TYPE_INT16:
case IR_TYPE_INT32:
case IR_TYPE_POINTER:
case IR_TYPE_FLOAT:
return iv->iv_u32 & type_code_mask(it->it_code);
case IR_TYPE_INTx:
case IR_TYPE_INT64:
case IR_TYPE_DOUBLE:
return iv->iv_u64;
default:
break;
}
break;
default:
break;
}
parser_error(iu, "Unable to value_get_const for value %s",
value_str(iu, iv));
}
/**
*
*/
static uint64_t __attribute__((unused))
value_get_const64(ir_unit_t *iu, const ir_value_t *iv)
{
assert(iv->iv_class == IR_VC_CONSTANT);
ir_type_t *it = type_get(iu, iv->iv_type);
switch(it->it_code) {
case IR_TYPE_INT1:
return !!iv->iv_u32;
case IR_TYPE_INT8:
case IR_TYPE_INT16:
case IR_TYPE_INT32:
case IR_TYPE_POINTER:
return iv->iv_u32;
case IR_TYPE_INTx:
case IR_TYPE_INT64:
case IR_TYPE_DOUBLE:
return iv->iv_u64;
default:
parser_error(iu, "Unable to get_constant for type %s",
type_str(iu, it));
}
}
/**
*
*/
static ir_valuetype_t
value_create_const32(ir_unit_t *iu, int v, ir_type_code_t code)
{
int type = type_find_by_code(iu, code);
int ret = value_append(iu);
ir_value_t *iv = VECTOR_ITEM(&iu->iu_values, ret);
iv->iv_class = IR_VC_CONSTANT;
iv->iv_type = type;
iv->iv_u32 = v;
return (ir_valuetype_t) {.value = ret, .type = type};
}
/**
*
*/
__attribute__((unused))
static ir_valuetype_t
value_create_const64(ir_unit_t *iu, uint64_t v, ir_type_code_t code)
{
int type = type_find_by_code(iu, code);
int ret = value_append(iu);
ir_value_t *iv = VECTOR_ITEM(&iu->iu_values, ret);
iv->iv_class = IR_VC_CONSTANT;
iv->iv_type = type;
iv->iv_u64 = v;
return (ir_valuetype_t) {.value = ret, .type = type};
}
/**
*
*/
static ir_valuetype_t
value_create_zero(ir_unit_t *iu, int type)
{
int ret = value_append(iu);
ir_value_t *iv = VECTOR_ITEM(&iu->iu_values, ret);
iv->iv_class = IR_VC_CONSTANT;
iv->iv_type = type;
iv->iv_u64 = 0;
return (ir_valuetype_t) {.value = ret, .type = type};
}
static int value_print_id(char **dstp, ir_unit_t *iu, int id);
static int value_print_vt(char **dstp, ir_unit_t *iu, ir_valuetype_t vt);
/**
*
*/
static int
value_print(char **dstp, ir_unit_t *iu, const ir_value_t *iv,
const ir_type_t *it)
{
int value = iv->iv_id;
int len = 0;
char tmpbuf[64];
if(it == NULL) {
if(iv->iv_type < VECTOR_LEN(&iu->iu_types))
it = &VECTOR_ITEM(&iu->iu_types, iv->iv_type);
}
switch(iv->iv_class) {
case IR_VC_UNDEF:
len += addstr(dstp, "<undefined>");
break;
case IR_VC_CE:
len += addstr(dstp, "<constexpr>");
break;
case IR_VC_AGGREGATE:
len += type_print(dstp, iu, it);
len += addstr(dstp, " = {");
ir_valuetype_t *values = iv->iv_data;
for(int i = 0; i < iv->iv_num_values; i++) {
if(i != 0)
len += addstr(dstp, ", ");
len += value_print_vt(dstp, iu, values[i]);
}
len += addstr(dstp, "}");
break;
case IR_VC_DEAD:
snprintf(tmpbuf, sizeof(tmpbuf), "<dead>%%%d", value);
len += addstr(dstp, tmpbuf);
break;
case IR_VC_ALIAS:
len += addstr(dstp, "alias -> ");
len += value_print_id(dstp, iu, iv->iv_reg);
break;
case IR_VC_FUNCTION:
len += addstr(dstp, "function ");
if(iv->iv_func->if_name != NULL) {
len += addstr(dstp, iv->iv_func->if_name);
len += addstr(dstp, "() ");
}
len += type_print_id(dstp, iu, iv->iv_func->if_type);
break;
case IR_VC_GLOBALVAR:
len += addstr(dstp, "global ");
if(iv->iv_gvar->ig_name != NULL) {
len += addstr(dstp, iv->iv_gvar->ig_name);
len += addstr(dstp, " ");
}
len += type_print(dstp, iu, it);
snprintf(tmpbuf, sizeof(tmpbuf), " @ 0x%x", iv->iv_gvar->ig_addr);
len += addstr(dstp, tmpbuf);
break;
case IR_VC_TEMPORARY:
len += addstr(dstp, "(");
len += type_print(dstp, iu, it);
snprintf(tmpbuf, sizeof(tmpbuf), ")%%%d", value);
len += addstr(dstp, tmpbuf);
break;
case IR_VC_CONSTANT:
len += addstr(dstp, "(");
len += type_print(dstp, iu, it);
snprintf(tmpbuf, sizeof(tmpbuf), ")%%%d", value);
len += addstr(dstp, tmpbuf);
if(it == NULL) {
len += addstr(dstp, "badtype");
break;
}
switch(it->it_code) {
case IR_TYPE_INT1:
snprintf(tmpbuf, sizeof(tmpbuf), "#0x%x", !!iv->iv_u32);
break;
case IR_TYPE_INT8:
case IR_TYPE_INT16:
case IR_TYPE_INT32:
case IR_TYPE_POINTER:
snprintf(tmpbuf, sizeof(tmpbuf), "#0x%x", iv->iv_u32);
break;
case IR_TYPE_INT64:
snprintf(tmpbuf, sizeof(tmpbuf), "#0x%"PRIx64, iv->iv_u64);
break;
case IR_TYPE_FLOAT:
snprintf(tmpbuf, sizeof(tmpbuf), "#%f", iv->iv_float);
break;
case IR_TYPE_DOUBLE:
snprintf(tmpbuf, sizeof(tmpbuf), "#%f", iv->iv_double);
break;
default:
snprintf(tmpbuf, sizeof(tmpbuf), "#?(code-%d)", it->it_code);
break;
}
len += addstr(dstp, tmpbuf);
break;
case IR_VC_ZERO_INITIALIZER:
len += addstr(dstp, "zeroinitializer");
break;
case IR_VC_REGFRAME:
len += addstr(dstp, "(");
len += type_print(dstp, iu, it);
snprintf(tmpbuf, sizeof(tmpbuf), ")%%%d{0x%x}", value, iv->iv_reg);
len += addstr(dstp, tmpbuf);
break;
case IR_VC_MACHINEREG:
len += addstr(dstp, "(");
len += type_print(dstp, iu, it);
snprintf(tmpbuf, sizeof(tmpbuf), ")%%%d{r%d}", value, iv->iv_reg);
len += addstr(dstp, tmpbuf);
break;
case IR_VC_DATA:
{
len += addstr(dstp, "data [");
int size = type_sizeof(iu, iv->iv_type);
for(int i = 0; i < size; i++) {
if(i)
len += addstr(dstp, ".");
snprintf(tmpbuf, sizeof(tmpbuf), "%02x", ((uint8_t *)iv->iv_data)[i]);
len += addstr(dstp, tmpbuf);
}
len += addstr(dstp, "]");
}
break;
}
if(iv->iv_name != NULL) {
len += addstr(dstp, "\"");
len += addstr(dstp, iv->iv_name);
len += addstr(dstp, "\"");
}
return len;
}
/**
*
*/
static int
value_print_id(char **dstp, ir_unit_t *iu, int value)
{
if(value >= VECTOR_LEN(&iu->iu_values)) {
char tmpbuf[64];
snprintf(tmpbuf, sizeof(tmpbuf), ">>BADVALUE:%d<<", value);
return addstr(dstp, tmpbuf);
}
ir_value_t *iv = VECTOR_ITEM(&iu->iu_values, value);
return value_print(dstp, iu, iv, NULL);
}
/**
*
*/
static int
value_print_vt(char **dstp, ir_unit_t *iu, ir_valuetype_t vt)
{
if(vt.value >= VECTOR_LEN(&iu->iu_values)) {
char tmpbuf[64];
snprintf(tmpbuf, sizeof(tmpbuf), ">>BADVALUE:%d<<", vt.value);
return addstr(dstp, tmpbuf);
}
if(vt.type >= VECTOR_LEN(&iu->iu_types)) {
char tmpbuf[64];
snprintf(tmpbuf, sizeof(tmpbuf), ">>BADTYPE:%d<<", vt.type);
return addstr(dstp, tmpbuf);
}
ir_value_t *iv = VECTOR_ITEM(&iu->iu_values, vt.value);
const ir_type_t *it = &VECTOR_ITEM(&iu->iu_types, vt.type);
return value_print(dstp, iu, iv, it);
}
/**
*
*/
static const char *
value_str(ir_unit_t *iu, const ir_value_t *iv)
{
int len = value_print(NULL, iu, iv, NULL);
char *dst = tmpstr(iu, len);
const char *ret = dst;
value_print(&dst, iu, iv, NULL);
return ret;
}
/**
*
*/
static const char *
value_str_id(ir_unit_t *iu, int id)
{
int len = value_print_id(NULL, iu, id);
char *dst = tmpstr(iu, len);
const char *ret = dst;
value_print_id(&dst, iu, id);
return ret;
}
/**
*
*/
__attribute__((unused)) static const char *
value_str_vt(ir_unit_t *iu, ir_valuetype_t vt)
{
int len = value_print_vt(NULL, iu, vt);
char *dst = tmpstr(iu, len);
const char *ret = dst;
value_print_vt(&dst, iu, vt);
return ret;
}
/**
*
*/
static void __attribute__((unused))
value_print_list(ir_unit_t *iu)
{
for(int i = 0; i < iu->iu_next_value; i++) {
printf("i=%d %p\n", i, value_get(iu, i));
printf("[%5d]: %s\n", i, value_str_id(iu, i));
}
}
/**
*
*/
static void
eval_constexpr(ir_unit_t *iu, ir_value_t *iv, ir_constexpr_t *ic)
{
switch(ic->ic_code) {
case CST_CODE_CE_CAST:
{
ir_type_code_t tc = type_get(iu, iv->iv_type)->it_code;
ir_value_t *src = value_get(iu, ic->ic_cast.src);
if(src->iv_class == IR_VC_CE)
eval_constexpr(iu, src, src->iv_ce);
if(src->iv_class == IR_VC_ALIAS) {
src = value_get(iu, src->iv_reg);
}
iv->iv_class = IR_VC_CONSTANT;
switch(COMBINE3(tc, ic->ic_cast.op, src->iv_class)) {
case COMBINE3(IR_TYPE_POINTER, CAST_INTTOPTR, IR_VC_CONSTANT):
iv->iv_u32 = src->iv_u32;
break;
case COMBINE3(IR_TYPE_POINTER, CAST_BITCAST, IR_VC_GLOBALVAR):
iv->iv_u32 = value_get_const32(iu, src);
break;
case COMBINE3(IR_TYPE_POINTER, CAST_BITCAST, IR_VC_CONSTANT):
iv->iv_u32 = value_get_const32(iu, src);
break;
case COMBINE3(IR_TYPE_POINTER, CAST_BITCAST, IR_VC_FUNCTION):
iv->iv_class = IR_VC_FUNCTION;
iv->iv_func = src->iv_func;
iv->iv_type = src->iv_type;
break;
case COMBINE3(IR_TYPE_INT32, CAST_PTRTOINT, IR_VC_CONSTANT):
case COMBINE3(IR_TYPE_INT32, CAST_PTRTOINT, IR_VC_GLOBALVAR):
case COMBINE3(IR_TYPE_INT8, CAST_PTRTOINT, IR_VC_CONSTANT):
case COMBINE3(IR_TYPE_INT8, CAST_PTRTOINT, IR_VC_GLOBALVAR):
iv->iv_u32 = value_get_const32(iu, src);
break;
case COMBINE3(IR_TYPE_INT32, CAST_PTRTOINT, IR_VC_FUNCTION):
iv->iv_u32 = value_function_addr(src);
break;
default:
parser_error(iu,
"Unable to constant-cast %s from %s using op %d class=%d",
type_str_index(iu, iv->iv_type),
type_str_index(iu, src->iv_type),
ic->ic_cast.op, src->iv_class);
}
}
break;
case CST_CODE_CE_BINOP:
{
ir_value_t *lhs = value_get(iu, ic->ic_binop.lhs);
ir_value_t *rhs = value_get(iu, ic->ic_binop.rhs);
ir_type_code_t tc = type_get(iu, iv->iv_type)->it_code;
switch(tc) {
case IR_TYPE_INT8:
case IR_TYPE_INT32:
case IR_TYPE_POINTER:
{
const uint32_t l = value_get_const32(iu, lhs);
const uint32_t r = value_get_const32(iu, rhs);
switch(ic->ic_binop.op) {
case BINOP_ADD: iv->iv_u32 = l + r; break;
case BINOP_SUB: iv->iv_u32 = l - r; break;
case BINOP_MUL: iv->iv_u32 = l * r; break;
case BINOP_UDIV: iv->iv_u32 = l / r; break;
case BINOP_SDIV: iv->iv_u32 = (int32_t)l / (int32_t)r; break;
case BINOP_UREM: iv->iv_u32 = l % r; break;
case BINOP_SREM: iv->iv_u32 = (int32_t)l % (int32_t)r; break;
case BINOP_SHL: iv->iv_u32 = l << r; break;
case BINOP_LSHR: iv->iv_u32 = l >> r; break;
case BINOP_ASHR: iv->iv_u32 = (int32_t)l >> r; break;
case BINOP_AND: iv->iv_u32 = l & r; break;
case BINOP_OR: iv->iv_u32 = l | r; break;
case BINOP_XOR: iv->iv_u32 = l ^ r; break;
default:
parser_error(iu, "Unable to handle opcode %d in constant binop",
ic->ic_binop.op);
}
}
break;
default:
parser_error(iu, "Unable to handle constant binop for typecode %d",
tc);
}
iv->iv_class = IR_VC_CONSTANT;
}
break;
case CST_CODE_CE_CMP:
{
ir_value_t *lhs = value_get(iu, ic->ic_cmp.lhs);
ir_value_t *rhs = value_get(iu, ic->ic_cmp.rhs);
ir_type_code_t opty = type_get(iu, ic->ic_cmp.opty)->it_code;
switch(opty) {
case IR_TYPE_INT32:
case IR_TYPE_POINTER:
{
const uint32_t l = value_get_const32(iu, lhs);
const uint32_t r = value_get_const32(iu, rhs);
switch(ic->ic_cmp.pred) {
case ICMP_EQ: iv->iv_u32 = l == r; break;
case ICMP_NE: iv->iv_u32 = l != r; break;
case ICMP_UGT: iv->iv_u32 = l > r; break;
case ICMP_UGE: iv->iv_u32 = l >= r; break;
case ICMP_ULT: iv->iv_u32 = l < r; break;
case ICMP_ULE: iv->iv_u32 = l <= r; break;
case ICMP_SGT: iv->iv_u32 = (int32_t)l > (int32_t)r; break;
case ICMP_SGE: iv->iv_u32 = (int32_t)l >= (int32_t)r; break;
case ICMP_SLT: iv->iv_u32 = (int32_t)l < (int32_t)r; break;
case ICMP_SLE: iv->iv_u32 = (int32_t)l <= (int32_t)r; break;
default:
parser_error(iu, "Unable to handle opcode %d in constant cmp",
ic->ic_binop.op);
}
}
break;
default:
parser_error(iu, "Unable to handle constant cmp for type %s",
type_str_index(iu, ic->ic_cmp.opty));
}
iv->iv_class = IR_VC_CONSTANT;
}
break;
case CST_CODE_CE_GEP:
case CST_CODE_CE_INBOUNDS_GEP:
{
const int *vv = ic->ic_gep.values;
ir_value_t *curval = value_get(iu, vv[0]);
if(curval->iv_class == IR_VC_CE)
eval_constexpr(iu, curval, curval->iv_ce);
uint32_t addr = value_get_const32(iu, curval);
int type_index = curval->iv_type;
for(int i = 1; i < ic->ic_gep.num_values; i++) {
ir_type_t *curtype = type_get(iu, type_index);
curval = value_get(iu, vv[i]);
int x = value_get_const32(iu, curval);
switch(curtype->it_code) {