forked from vnmakarov/mir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mir.c
5453 lines (4969 loc) · 208 KB
/
mir.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
/* This file is a part of MIR project.
Copyright (C) 2018-2020 Vladimir Makarov <[email protected]>.
*/
#include "mir.h"
DEF_VARR (MIR_insn_t);
DEF_VARR (MIR_reg_t);
DEF_VARR (MIR_op_t);
DEF_VARR (MIR_type_t);
DEF_HTAB (MIR_item_t);
DEF_VARR (MIR_module_t);
DEF_VARR (size_t);
DEF_VARR (char);
DEF_VARR (uint8_t);
struct gen_ctx;
struct c2mir_ctx;
struct string_ctx;
struct reg_ctx;
struct simplify_ctx;
struct machine_code_ctx;
struct io_ctx;
struct scan_ctx;
struct interp_ctx;
struct MIR_context {
struct gen_ctx *gen_ctx; /* should be the 1st member */
struct c2mir_ctx *c2mir_ctx; /* should be the 2nd member */
MIR_error_func_t error_func;
VARR (MIR_insn_t) * temp_insns, *temp_insns2;
VARR (MIR_op_t) * temp_insn_ops;
VARR (MIR_var_t) * temp_vars;
VARR (MIR_type_t) * temp_types;
VARR (size_t) * insn_nops;
VARR (char) * temp_string;
VARR (uint8_t) * temp_data;
char temp_buff[30];
HTAB (MIR_item_t) * module_item_tab;
/* Module to keep items potentially used by all modules: */
struct MIR_module environment_module;
VARR (MIR_reg_t) * inline_reg_map;
MIR_module_t curr_module;
MIR_func_t curr_func;
int curr_label_num;
DLIST (MIR_module_t) all_modules;
VARR (MIR_module_t) * modules_to_link;
struct string_ctx *string_ctx;
struct reg_ctx *reg_ctx;
struct simplify_ctx *simplify_ctx;
struct machine_code_ctx *machine_code_ctx;
struct io_ctx *io_ctx;
struct scan_ctx *scan_ctx;
struct interp_ctx *interp_ctx;
size_t inlined_calls, inline_insns_before, inline_insns_after;
};
#define error_func ctx->error_func
#define temp_insns ctx->temp_insns
#define temp_insns2 ctx->temp_insns2
#define temp_insn_ops ctx->temp_insn_ops
#define temp_vars ctx->temp_vars
#define temp_types ctx->temp_types
#define insn_nops ctx->insn_nops
#define temp_string ctx->temp_string
#define temp_data ctx->temp_data
#define temp_buff ctx->temp_buff
#define module_item_tab ctx->module_item_tab
#define environment_module ctx->environment_module
#define inline_reg_map ctx->inline_reg_map
#define curr_module ctx->curr_module
#define curr_func ctx->curr_func
#define curr_label_num ctx->curr_label_num
#define all_modules ctx->all_modules
#define modules_to_link ctx->modules_to_link
#define inlined_calls ctx->inlined_calls
#define inline_insns_before ctx->inline_insns_before
#define inline_insns_after ctx->inline_insns_after
static void util_error (MIR_context_t ctx, const char *message);
#define MIR_VARR_ERROR util_error
#define MIR_HTAB_ERROR MIR_VARR_ERROR
#include "mir-hash.h"
#include "mir-htab.h"
#include "mir-reduce.h"
#include <string.h>
#include <stdarg.h>
#include <inttypes.h>
#include <float.h>
#include <ctype.h>
#include <limits.h>
static void interp_init (MIR_context_t ctx);
static void finish_func_interpretation (MIR_item_t func_item);
static void interp_finish (MIR_context_t ctx);
static void MIR_NO_RETURN default_error (enum MIR_error_type error_type, const char *format, ...) {
va_list ap;
va_start (ap, format);
vfprintf (stderr, format, ap);
fprintf (stderr, "\n");
va_end (ap);
exit (1);
}
static void MIR_NO_RETURN MIR_UNUSED util_error (MIR_context_t ctx, const char *message) {
(*error_func) (MIR_alloc_error, message);
}
#define HARD_REG_NAME_PREFIX "hr"
#define TEMP_REG_NAME_PREFIX "t"
#define TEMP_ITEM_NAME_PREFIX ".lc"
int _MIR_reserved_ref_name_p (MIR_context_t ctx, const char *name) {
return strncmp (name, TEMP_ITEM_NAME_PREFIX, strlen (TEMP_ITEM_NAME_PREFIX)) == 0;
}
/* Reserved names:
fp - frame pointer
hr<number> - a hardware reg
lc<number> - a temp item */
int _MIR_reserved_name_p (MIR_context_t ctx, const char *name) {
size_t i, start;
if (_MIR_reserved_ref_name_p (ctx, name))
return TRUE;
else if (strncmp (name, HARD_REG_NAME_PREFIX, strlen (HARD_REG_NAME_PREFIX)) == 0)
start = strlen (HARD_REG_NAME_PREFIX);
else
return FALSE;
for (i = start; name[i] != '\0'; i++)
if (name[i] < '0' || name[i] > '9') return FALSE;
return TRUE;
}
struct insn_desc {
MIR_insn_code_t code;
const char *name;
unsigned op_modes[4];
};
#define OUTPUT_FLAG (1 << 30)
static const struct insn_desc insn_descs[] = {
{MIR_MOV, "mov", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_FMOV, "fmov", {MIR_OP_FLOAT | OUTPUT_FLAG, MIR_OP_FLOAT, MIR_OP_BOUND}},
{MIR_DMOV, "dmov", {MIR_OP_DOUBLE | OUTPUT_FLAG, MIR_OP_DOUBLE, MIR_OP_BOUND}},
{MIR_LDMOV, "ldmov", {MIR_OP_LDOUBLE | OUTPUT_FLAG, MIR_OP_LDOUBLE, MIR_OP_BOUND}},
{MIR_EXT8, "ext8", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_EXT16, "ext16", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_EXT32, "ext32", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_UEXT8, "uext8", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_UEXT16, "uext16", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_UEXT32, "uext32", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_I2F, "i2f", {MIR_OP_FLOAT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_I2D, "i2d", {MIR_OP_DOUBLE | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_I2LD, "i2ld", {MIR_OP_LDOUBLE | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_UI2F, "ui2f", {MIR_OP_FLOAT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_UI2D, "ui2d", {MIR_OP_DOUBLE | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_UI2LD, "ui2ld", {MIR_OP_LDOUBLE | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_F2I, "f2i", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_FLOAT, MIR_OP_BOUND}},
{MIR_D2I, "d2i", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_DOUBLE, MIR_OP_BOUND}},
{MIR_LD2I, "ld2i", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_LDOUBLE, MIR_OP_BOUND}},
{MIR_F2D, "f2d", {MIR_OP_DOUBLE | OUTPUT_FLAG, MIR_OP_FLOAT, MIR_OP_BOUND}},
{MIR_F2LD, "f2ld", {MIR_OP_LDOUBLE | OUTPUT_FLAG, MIR_OP_FLOAT, MIR_OP_BOUND}},
{MIR_D2F, "d2f", {MIR_OP_FLOAT | OUTPUT_FLAG, MIR_OP_DOUBLE, MIR_OP_BOUND}},
{MIR_D2LD, "d2ld", {MIR_OP_LDOUBLE | OUTPUT_FLAG, MIR_OP_DOUBLE, MIR_OP_BOUND}},
{MIR_LD2F, "ld2f", {MIR_OP_FLOAT | OUTPUT_FLAG, MIR_OP_LDOUBLE, MIR_OP_BOUND}},
{MIR_LD2D, "ld2d", {MIR_OP_DOUBLE | OUTPUT_FLAG, MIR_OP_LDOUBLE, MIR_OP_BOUND}},
{MIR_NEG, "neg", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_NEGS, "negs", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_FNEG, "fneg", {MIR_OP_FLOAT | OUTPUT_FLAG, MIR_OP_FLOAT, MIR_OP_BOUND}},
{MIR_DNEG, "dneg", {MIR_OP_DOUBLE | OUTPUT_FLAG, MIR_OP_DOUBLE, MIR_OP_BOUND}},
{MIR_LDNEG, "ldneg", {MIR_OP_LDOUBLE | OUTPUT_FLAG, MIR_OP_LDOUBLE, MIR_OP_BOUND}},
{MIR_ADD, "add", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_ADDS, "adds", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_FADD, "fadd", {MIR_OP_FLOAT | OUTPUT_FLAG, MIR_OP_FLOAT, MIR_OP_FLOAT, MIR_OP_BOUND}},
{MIR_DADD, "dadd", {MIR_OP_DOUBLE | OUTPUT_FLAG, MIR_OP_DOUBLE, MIR_OP_DOUBLE, MIR_OP_BOUND}},
{MIR_LDADD,
"ldadd",
{MIR_OP_LDOUBLE | OUTPUT_FLAG, MIR_OP_LDOUBLE, MIR_OP_LDOUBLE, MIR_OP_BOUND}},
{MIR_SUB, "sub", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_SUBS, "subs", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_FSUB, "fsub", {MIR_OP_FLOAT | OUTPUT_FLAG, MIR_OP_FLOAT, MIR_OP_FLOAT, MIR_OP_BOUND}},
{MIR_DSUB, "dsub", {MIR_OP_DOUBLE | OUTPUT_FLAG, MIR_OP_DOUBLE, MIR_OP_DOUBLE, MIR_OP_BOUND}},
{MIR_LDSUB,
"ldsub",
{MIR_OP_LDOUBLE | OUTPUT_FLAG, MIR_OP_LDOUBLE, MIR_OP_LDOUBLE, MIR_OP_BOUND}},
{MIR_MUL, "mul", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_MULS, "muls", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_FMUL, "fmul", {MIR_OP_FLOAT | OUTPUT_FLAG, MIR_OP_FLOAT, MIR_OP_FLOAT, MIR_OP_BOUND}},
{MIR_DMUL, "dmul", {MIR_OP_DOUBLE | OUTPUT_FLAG, MIR_OP_DOUBLE, MIR_OP_DOUBLE, MIR_OP_BOUND}},
{MIR_LDMUL,
"ldmul",
{MIR_OP_LDOUBLE | OUTPUT_FLAG, MIR_OP_LDOUBLE, MIR_OP_LDOUBLE, MIR_OP_BOUND}},
{MIR_DIV, "div", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_DIVS, "divs", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_UDIV, "udiv", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_UDIVS, "udivs", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_FDIV, "fdiv", {MIR_OP_FLOAT | OUTPUT_FLAG, MIR_OP_FLOAT, MIR_OP_FLOAT, MIR_OP_BOUND}},
{MIR_DDIV, "ddiv", {MIR_OP_DOUBLE | OUTPUT_FLAG, MIR_OP_DOUBLE, MIR_OP_DOUBLE, MIR_OP_BOUND}},
{MIR_LDDIV,
"lddiv",
{MIR_OP_LDOUBLE | OUTPUT_FLAG, MIR_OP_LDOUBLE, MIR_OP_LDOUBLE, MIR_OP_BOUND}},
{MIR_MOD, "mod", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_MODS, "mods", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_UMOD, "umod", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_UMODS, "umods", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_AND, "and", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_ANDS, "ands", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_OR, "or", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_ORS, "ors", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_XOR, "xor", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_XORS, "xors", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_LSH, "lsh", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_LSHS, "lshs", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_RSH, "rsh", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_RSHS, "rshs", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_URSH, "ursh", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_URSHS, "urshs", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_EQ, "eq", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_EQS, "eqs", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_FEQ, "feq", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_FLOAT, MIR_OP_FLOAT, MIR_OP_BOUND}},
{MIR_DEQ, "deq", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_DOUBLE, MIR_OP_DOUBLE, MIR_OP_BOUND}},
{MIR_LDEQ, "ldeq", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_LDOUBLE, MIR_OP_LDOUBLE, MIR_OP_BOUND}},
{MIR_NE, "ne", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_NES, "nes", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_FNE, "fne", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_FLOAT, MIR_OP_FLOAT, MIR_OP_BOUND}},
{MIR_DNE, "dne", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_DOUBLE, MIR_OP_DOUBLE, MIR_OP_BOUND}},
{MIR_LDNE, "ldne", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_LDOUBLE, MIR_OP_LDOUBLE, MIR_OP_BOUND}},
{MIR_LT, "lt", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_LTS, "lts", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_ULT, "ult", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_ULTS, "ults", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_FLT, "flt", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_FLOAT, MIR_OP_FLOAT, MIR_OP_BOUND}},
{MIR_DLT, "dlt", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_DOUBLE, MIR_OP_DOUBLE, MIR_OP_BOUND}},
{MIR_LDLT, "ldlt", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_LDOUBLE, MIR_OP_LDOUBLE, MIR_OP_BOUND}},
{MIR_LE, "le", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_LES, "les", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_ULE, "ule", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_ULES, "ules", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_FLE, "fle", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_FLOAT, MIR_OP_FLOAT, MIR_OP_BOUND}},
{MIR_DLE, "dle", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_DOUBLE, MIR_OP_DOUBLE, MIR_OP_BOUND}},
{MIR_LDLE, "ldle", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_LDOUBLE, MIR_OP_LDOUBLE, MIR_OP_BOUND}},
{MIR_GT, "gt", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_GTS, "gts", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_UGT, "ugt", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_UGTS, "ugts", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_FGT, "fgt", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_FLOAT, MIR_OP_FLOAT, MIR_OP_BOUND}},
{MIR_DGT, "dgt", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_DOUBLE, MIR_OP_DOUBLE, MIR_OP_BOUND}},
{MIR_LDGT, "ldgt", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_LDOUBLE, MIR_OP_LDOUBLE, MIR_OP_BOUND}},
{MIR_GE, "ge", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_GES, "ges", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_UGE, "uge", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_UGES, "uges", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_FGE, "fge", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_FLOAT, MIR_OP_FLOAT, MIR_OP_BOUND}},
{MIR_DGE, "dge", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_DOUBLE, MIR_OP_DOUBLE, MIR_OP_BOUND}},
{MIR_LDGE, "ldge", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_LDOUBLE, MIR_OP_LDOUBLE, MIR_OP_BOUND}},
{MIR_JMP, "jmp", {MIR_OP_LABEL, MIR_OP_BOUND}},
{MIR_BT, "bt", {MIR_OP_LABEL, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_BTS, "bts", {MIR_OP_LABEL, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_BF, "bf", {MIR_OP_LABEL, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_BFS, "bfs", {MIR_OP_LABEL, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_BEQ, "beq", {MIR_OP_LABEL, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_BEQS, "beqs", {MIR_OP_LABEL, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_FBEQ, "fbeq", {MIR_OP_LABEL, MIR_OP_FLOAT, MIR_OP_FLOAT, MIR_OP_BOUND}},
{MIR_DBEQ, "dbeq", {MIR_OP_LABEL, MIR_OP_DOUBLE, MIR_OP_DOUBLE, MIR_OP_BOUND}},
{MIR_LDBEQ, "ldbeq", {MIR_OP_LABEL, MIR_OP_LDOUBLE, MIR_OP_LDOUBLE, MIR_OP_BOUND}},
{MIR_BNE, "bne", {MIR_OP_LABEL, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_BNES, "bnes", {MIR_OP_LABEL, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_FBNE, "fbne", {MIR_OP_LABEL, MIR_OP_FLOAT, MIR_OP_FLOAT, MIR_OP_BOUND}},
{MIR_DBNE, "dbne", {MIR_OP_LABEL, MIR_OP_DOUBLE, MIR_OP_DOUBLE, MIR_OP_BOUND}},
{MIR_LDBNE, "ldbne", {MIR_OP_LABEL, MIR_OP_LDOUBLE, MIR_OP_LDOUBLE, MIR_OP_BOUND}},
{MIR_BLT, "blt", {MIR_OP_LABEL, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_BLTS, "blts", {MIR_OP_LABEL, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_UBLT, "ublt", {MIR_OP_LABEL, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_UBLTS, "ublts", {MIR_OP_LABEL, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_FBLT, "fblt", {MIR_OP_LABEL, MIR_OP_FLOAT, MIR_OP_FLOAT, MIR_OP_BOUND}},
{MIR_DBLT, "dblt", {MIR_OP_LABEL, MIR_OP_DOUBLE, MIR_OP_DOUBLE, MIR_OP_BOUND}},
{MIR_LDBLT, "ldblt", {MIR_OP_LABEL, MIR_OP_LDOUBLE, MIR_OP_LDOUBLE, MIR_OP_BOUND}},
{MIR_BLE, "ble", {MIR_OP_LABEL, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_BLES, "bles", {MIR_OP_LABEL, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_UBLE, "uble", {MIR_OP_LABEL, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_UBLES, "ubles", {MIR_OP_LABEL, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_FBLE, "fble", {MIR_OP_LABEL, MIR_OP_FLOAT, MIR_OP_FLOAT, MIR_OP_BOUND}},
{MIR_DBLE, "dble", {MIR_OP_LABEL, MIR_OP_DOUBLE, MIR_OP_DOUBLE, MIR_OP_BOUND}},
{MIR_LDBLE, "ldble", {MIR_OP_LABEL, MIR_OP_LDOUBLE, MIR_OP_LDOUBLE, MIR_OP_BOUND}},
{MIR_BGT, "bgt", {MIR_OP_LABEL, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_BGTS, "bgts", {MIR_OP_LABEL, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_UBGT, "ubgt", {MIR_OP_LABEL, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_UBGTS, "ubgts", {MIR_OP_LABEL, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_FBGT, "fbgt", {MIR_OP_LABEL, MIR_OP_FLOAT, MIR_OP_FLOAT, MIR_OP_BOUND}},
{MIR_DBGT, "dbgt", {MIR_OP_LABEL, MIR_OP_DOUBLE, MIR_OP_DOUBLE, MIR_OP_BOUND}},
{MIR_LDBGT, "ldbgt", {MIR_OP_LABEL, MIR_OP_LDOUBLE, MIR_OP_LDOUBLE, MIR_OP_BOUND}},
{MIR_BGE, "bge", {MIR_OP_LABEL, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_BGES, "bges", {MIR_OP_LABEL, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_UBGE, "ubge", {MIR_OP_LABEL, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_UBGES, "ubges", {MIR_OP_LABEL, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_FBGE, "fbge", {MIR_OP_LABEL, MIR_OP_FLOAT, MIR_OP_FLOAT, MIR_OP_BOUND}},
{MIR_DBGE, "dbge", {MIR_OP_LABEL, MIR_OP_DOUBLE, MIR_OP_DOUBLE, MIR_OP_BOUND}},
{MIR_LDBGE, "ldbge", {MIR_OP_LABEL, MIR_OP_LDOUBLE, MIR_OP_LDOUBLE, MIR_OP_BOUND}},
{MIR_CALL, "call", {MIR_OP_BOUND}},
{MIR_INLINE, "inline", {MIR_OP_BOUND}},
{MIR_SWITCH, "switch", {MIR_OP_BOUND}},
{MIR_RET, "ret", {MIR_OP_BOUND}},
{MIR_ALLOCA, "alloca", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_BSTART, "bstart", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_BOUND}},
{MIR_BEND, "bend", {MIR_OP_INT, MIR_OP_BOUND}},
{MIR_VA_ARG, "va_arg", {MIR_OP_INT | OUTPUT_FLAG, MIR_OP_INT, MIR_OP_UNDEF, MIR_OP_BOUND}},
{MIR_VA_START, "va_start", {MIR_OP_INT, MIR_OP_BOUND}},
{MIR_VA_END, "va_end", {MIR_OP_INT, MIR_OP_BOUND}},
{MIR_LABEL, "label", {MIR_OP_BOUND}},
{MIR_INVALID_INSN, "invalid-insn", {MIR_OP_BOUND}},
};
static void check_and_prepare_insn_descs (MIR_context_t ctx) {
size_t i, j;
VARR_CREATE (size_t, insn_nops, 0);
for (i = 0; i < MIR_INSN_BOUND; i++) {
mir_assert (insn_descs[i].code == i);
for (j = 0; insn_descs[i].op_modes[j] != MIR_OP_BOUND; j++)
;
VARR_PUSH (size_t, insn_nops, j);
}
}
static MIR_op_mode_t type2mode (MIR_type_t type) {
return (type == MIR_T_UNDEF
? MIR_OP_UNDEF
: type == MIR_T_F
? MIR_OP_FLOAT
: type == MIR_T_D ? MIR_OP_DOUBLE : type == MIR_T_LD ? MIR_OP_LDOUBLE : MIR_OP_INT);
}
/* New Page */
typedef struct string {
size_t num; /* string number starting with 1 */
MIR_str_t str;
} string_t;
DEF_VARR (string_t);
DEF_HTAB (string_t);
struct string_ctx {
VARR (string_t) * strings;
HTAB (string_t) * string_tab;
};
#define strings ctx->string_ctx->strings
#define string_tab ctx->string_ctx->string_tab
static htab_hash_t str_hash (string_t str, void *arg) {
return mir_hash (str.str.s, str.str.len, 0);
}
static int str_eq (string_t str1, string_t str2, void *arg) {
return str1.str.len == str2.str.len && memcmp (str1.str.s, str2.str.s, str1.str.len) == 0;
}
static void string_init (VARR (string_t) * *strs, HTAB (string_t) * *str_tab) {
string_t string = {0, {0, NULL}};
VARR_CREATE (string_t, *strs, 0);
VARR_PUSH (string_t, *strs, string); /* don't use 0th string */
HTAB_CREATE (string_t, *str_tab, 1000, str_hash, str_eq, NULL);
}
static int string_find (VARR (string_t) * *strs, HTAB (string_t) * *str_tab, MIR_str_t str,
string_t *s) {
string_t string;
string.str = str;
return HTAB_DO (string_t, *str_tab, string, HTAB_FIND, *s);
}
static string_t string_store (MIR_context_t ctx, VARR (string_t) * *strs,
HTAB (string_t) * *str_tab, MIR_str_t str) {
char *heap_str;
string_t el, string;
if (string_find (strs, str_tab, str, &el)) return el;
if ((heap_str = malloc (str.len)) == NULL)
(*error_func) (MIR_alloc_error, "Not enough memory for strings");
memcpy (heap_str, str.s, str.len);
string.str.s = heap_str;
string.str.len = str.len;
string.num = VARR_LENGTH (string_t, *strs);
VARR_PUSH (string_t, *strs, string);
HTAB_DO (string_t, *str_tab, string, HTAB_INSERT, el);
return string;
}
static void string_finish (VARR (string_t) * *strs, HTAB (string_t) * *str_tab) {
size_t i;
for (i = 1; i < VARR_LENGTH (string_t, *strs); i++)
free ((char *) VARR_ADDR (string_t, *strs)[i].str.s);
VARR_DESTROY (string_t, *strs);
HTAB_DESTROY (string_t, *str_tab);
}
/* New Page */
typedef struct reg_desc {
size_t name_num; /* 1st key for the namenum2rdn hash tab */
MIR_func_t func; /* 2nd key for hash the both tabs */
MIR_type_t type;
MIR_reg_t reg; /* 1st key reg2rdn hash tab */
} reg_desc_t;
DEF_VARR (reg_desc_t);
DEF_HTAB (size_t);
struct reg_ctx {
VARR (reg_desc_t) * reg_descs;
HTAB (size_t) * namenum2rdn_tab;
HTAB (size_t) * reg2rdn_tab;
};
#define reg_descs ctx->reg_ctx->reg_descs
#define namenum2rdn_tab ctx->reg_ctx->namenum2rdn_tab
#define reg2rdn_tab ctx->reg_ctx->reg2rdn_tab
static int namenum2rdn_eq (size_t rdn1, size_t rdn2, void *arg) {
MIR_context_t ctx = arg;
reg_desc_t *addr = VARR_ADDR (reg_desc_t, reg_descs);
return (addr[rdn1].name_num == addr[rdn2].name_num && addr[rdn1].func == addr[rdn2].func);
}
static htab_hash_t namenum2rdn_hash (size_t rdn, void *arg) {
MIR_context_t ctx = arg;
reg_desc_t *addr = VARR_ADDR (reg_desc_t, reg_descs);
return mir_hash_finish (
mir_hash_step (mir_hash_step (mir_hash_init (0), (uint64_t) addr[rdn].func),
(uint64_t) addr[rdn].name_num));
}
static int reg2rdn_eq (size_t rdn1, size_t rdn2, void *arg) {
MIR_context_t ctx = arg;
reg_desc_t *addr = VARR_ADDR (reg_desc_t, reg_descs);
return addr[rdn1].reg == addr[rdn2].reg && addr[rdn1].func == addr[rdn2].func;
}
static htab_hash_t reg2rdn_hash (size_t rdn, void *arg) {
MIR_context_t ctx = arg;
reg_desc_t *addr = VARR_ADDR (reg_desc_t, reg_descs);
return mir_hash_finish (
mir_hash_step (mir_hash_step (mir_hash_init (0), (uint64_t) addr[rdn].func), addr[rdn].reg));
}
static void reg_init (MIR_context_t ctx) {
reg_desc_t rd = {0, NULL, MIR_T_I64, 0};
if ((ctx->reg_ctx = malloc (sizeof (struct reg_ctx))) == NULL)
(*error_func) (MIR_alloc_error, "Not enough memory for ctx");
VARR_CREATE (reg_desc_t, reg_descs, 300);
VARR_PUSH (reg_desc_t, reg_descs, rd); /* for 0 reg */
HTAB_CREATE (size_t, namenum2rdn_tab, 300, namenum2rdn_hash, namenum2rdn_eq, ctx);
HTAB_CREATE (size_t, reg2rdn_tab, 300, reg2rdn_hash, reg2rdn_eq, ctx);
}
static MIR_reg_t create_func_reg (MIR_context_t ctx, MIR_func_t func, const char *name,
MIR_reg_t reg, MIR_type_t type, int any_p) {
reg_desc_t rd;
size_t rdn, tab_rdn;
int htab_res;
if (!any_p && _MIR_reserved_name_p (ctx, name))
(*error_func) (MIR_reserved_name_error, "redefining a reserved name %s", name);
rd.name_num = string_store (ctx, &strings, &string_tab, (MIR_str_t){strlen (name) + 1, name}).num;
rd.func = func;
rd.type = type;
rd.reg = reg; /* 0 is reserved */
rdn = VARR_LENGTH (reg_desc_t, reg_descs);
VARR_PUSH (reg_desc_t, reg_descs, rd);
if (HTAB_DO (size_t, namenum2rdn_tab, rdn, HTAB_FIND, tab_rdn)) {
VARR_POP (reg_desc_t, reg_descs);
(*error_func) (MIR_repeated_decl_error, "Repeated reg declaration %s", name);
}
htab_res = HTAB_DO (size_t, namenum2rdn_tab, rdn, HTAB_INSERT, tab_rdn);
mir_assert (!htab_res);
htab_res = HTAB_DO (size_t, reg2rdn_tab, rdn, HTAB_INSERT, tab_rdn);
mir_assert (!htab_res);
return reg;
}
static void reg_finish (MIR_context_t ctx) {
VARR_DESTROY (reg_desc_t, reg_descs);
HTAB_DESTROY (size_t, namenum2rdn_tab);
HTAB_DESTROY (size_t, reg2rdn_tab);
free (ctx->reg_ctx);
ctx->reg_ctx = NULL;
}
/* New Page */
static void push_data (MIR_context_t ctx, uint8_t *els, size_t size) {
for (size_t i = 0; i < size; i++) VARR_PUSH (uint8_t, temp_data, els[i]);
}
const char *MIR_item_name (MIR_context_t ctx, MIR_item_t item) {
mir_assert (item != NULL);
return (item->item_type == MIR_func_item
? item->u.func->name
: item->item_type == MIR_proto_item
? item->u.proto->name
: item->item_type == MIR_import_item
? item->u.import_id
: item->item_type == MIR_export_item
? item->u.export_id
: item->item_type == MIR_forward_item
? item->u.forward_id
: item->item_type == MIR_bss_item
? item->u.bss->name
: item->item_type == MIR_data_item
? item->u.data->name
: item->item_type == MIR_ref_data_item
? item->u.ref_data->name
: item->u.expr_data->name);
}
#if !MIR_NO_IO
static void io_init (MIR_context_t ctx);
static void io_finish (MIR_context_t ctx);
#endif
#if !MIR_NO_SCAN
static void scan_init (MIR_context_t ctx);
static void scan_finish (MIR_context_t ctx);
#endif
static void vn_init (MIR_context_t ctx);
static void vn_finish (MIR_context_t ctx);
MIR_error_func_t MIR_get_error_func (MIR_context_t ctx) { return error_func; }
void MIR_set_error_func (MIR_context_t ctx, MIR_error_func_t func) { error_func = func; }
static htab_hash_t item_hash (MIR_item_t it, void *arg) {
return mir_hash_finish (
mir_hash_step (mir_hash_step (mir_hash_init (28), (uint64_t) MIR_item_name (NULL, it)),
(uint64_t) it->module));
}
static int item_eq (MIR_item_t it1, MIR_item_t it2, void *arg) {
return it1->module == it2->module && MIR_item_name (NULL, it1) == MIR_item_name (NULL, it2);
}
static MIR_item_t find_item (MIR_context_t ctx, const char *name, MIR_module_t module) {
MIR_item_t tab_item;
struct MIR_item item_s;
struct MIR_func func_s;
item_s.item_type = MIR_func_item;
func_s.name = name;
item_s.module = module;
item_s.u.func = &func_s;
if (HTAB_DO (MIR_item_t, module_item_tab, &item_s, HTAB_FIND, tab_item)) return tab_item;
return NULL;
}
static void init_module (MIR_context_t ctx, MIR_module_t m, const char *name) {
m->data = NULL;
m->last_temp_item_num = 0;
m->name = string_store (ctx, &strings, &string_tab, (MIR_str_t){strlen (name) + 1, name}).str.s;
DLIST_INIT (MIR_item_t, m->items);
}
static void code_init (MIR_context_t ctx);
static void code_finish (MIR_context_t ctx);
MIR_context_t MIR_init (void) {
MIR_context_t ctx;
if ((ctx = malloc (sizeof (struct MIR_context))) == NULL)
default_error (MIR_alloc_error, "Not enough memory for ctx");
ctx->string_ctx = NULL;
ctx->reg_ctx = NULL;
ctx->simplify_ctx = NULL;
ctx->machine_code_ctx = NULL;
ctx->io_ctx = NULL;
ctx->scan_ctx = NULL;
ctx->interp_ctx = NULL;
#ifndef NDEBUG
for (MIR_insn_code_t c = 0; c < MIR_INVALID_INSN; c++) mir_assert (c == insn_descs[c].code);
#endif
error_func = default_error;
curr_module = NULL;
curr_func = NULL;
curr_label_num = 0;
if ((ctx->string_ctx = malloc (sizeof (struct string_ctx))) == NULL)
(*error_func) (MIR_alloc_error, "Not enough memory for ctx");
string_init (&strings, &string_tab);
reg_init (ctx);
VARR_CREATE (MIR_insn_t, temp_insns, 0);
VARR_CREATE (MIR_insn_t, temp_insns2, 0);
VARR_CREATE (MIR_op_t, temp_insn_ops, 0);
VARR_CREATE (MIR_var_t, temp_vars, 0);
VARR_CREATE (MIR_type_t, temp_types, 0);
check_and_prepare_insn_descs (ctx);
DLIST_INIT (MIR_module_t, all_modules);
vn_init (ctx);
VARR_CREATE (MIR_reg_t, inline_reg_map, 256);
VARR_CREATE (char, temp_string, 64);
VARR_CREATE (uint8_t, temp_data, 512);
#if !MIR_NO_IO
io_init (ctx);
#endif
#if !MIR_NO_SCAN
scan_init (ctx);
#endif
VARR_CREATE (MIR_module_t, modules_to_link, 0);
init_module (ctx, &environment_module, ".environment");
HTAB_CREATE (MIR_item_t, module_item_tab, 512, item_hash, item_eq, NULL);
code_init (ctx);
interp_init (ctx);
inlined_calls = inline_insns_before = inline_insns_after = 0;
return ctx;
}
void MIR_remove_insn (MIR_context_t ctx, MIR_item_t func_item, MIR_insn_t insn) {
mir_assert (func_item != NULL);
if (func_item->item_type != MIR_func_item)
(*error_func) (MIR_wrong_param_value_error, "MIR_remove_insn: wrong func item");
DLIST_REMOVE (MIR_insn_t, func_item->u.func->insns, insn);
free (insn);
}
static void remove_func_insns (MIR_context_t ctx, MIR_item_t func_item,
DLIST (MIR_insn_t) * insns) {
MIR_insn_t insn;
mir_assert (func_item->item_type == MIR_func_item);
while ((insn = DLIST_HEAD (MIR_insn_t, *insns)) != NULL) {
MIR_remove_insn (ctx, func_item, insn);
}
}
static void remove_item (MIR_context_t ctx, MIR_item_t item) {
MIR_module_t module = item->module;
switch (item->item_type) {
case MIR_func_item:
remove_func_insns (ctx, item, &item->u.func->insns);
remove_func_insns (ctx, item, &item->u.func->original_insns);
VARR_DESTROY (MIR_var_t, item->u.func->vars);
free (item->u.func);
break;
case MIR_proto_item:
VARR_DESTROY (MIR_var_t, item->u.proto->args);
free (item->u.proto);
break;
case MIR_import_item:
case MIR_export_item:
case MIR_forward_item: break;
case MIR_data_item:
if (item->addr != NULL && item->section_head_p) free (item->addr);
free (item->u.data);
break;
case MIR_ref_data_item:
if (item->addr != NULL && item->section_head_p) free (item->addr);
free (item->u.ref_data);
break;
case MIR_expr_data_item:
if (item->addr != NULL && item->section_head_p) free (item->addr);
free (item->u.expr_data);
break;
case MIR_bss_item:
if (item->addr != NULL && item->section_head_p) free (item->addr);
free (item->u.bss);
break;
default: mir_assert (FALSE);
}
if (item->data != NULL) free (item->data);
free (item);
}
static void remove_module (MIR_context_t ctx, MIR_module_t module, int free_module_p) {
MIR_item_t item;
while ((item = DLIST_HEAD (MIR_item_t, module->items)) != NULL) {
DLIST_REMOVE (MIR_item_t, module->items, item);
remove_item (ctx, item);
}
if (module->data != NULL) free (module->data);
if (free_module_p) free (module);
}
static void remove_all_modules (MIR_context_t ctx) {
MIR_module_t module;
while ((module = DLIST_HEAD (MIR_module_t, all_modules)) != NULL) {
DLIST_REMOVE (MIR_module_t, all_modules, module);
remove_module (ctx, module, TRUE);
}
remove_module (ctx, &environment_module, FALSE);
}
void MIR_finish (MIR_context_t ctx) {
interp_finish (ctx);
remove_all_modules (ctx);
HTAB_DESTROY (MIR_item_t, module_item_tab);
VARR_DESTROY (MIR_module_t, modules_to_link);
#if !MIR_NO_SCAN
scan_finish (ctx);
#endif
#if !MIR_NO_IO
io_finish (ctx);
#endif
VARR_DESTROY (uint8_t, temp_data);
VARR_DESTROY (char, temp_string);
VARR_DESTROY (MIR_reg_t, inline_reg_map);
reg_finish (ctx);
string_finish (&strings, &string_tab);
vn_finish (ctx);
VARR_DESTROY (MIR_var_t, temp_vars);
VARR_DESTROY (size_t, insn_nops);
VARR_DESTROY (MIR_op_t, temp_insn_ops);
VARR_DESTROY (MIR_insn_t, temp_insns2);
VARR_DESTROY (MIR_insn_t, temp_insns);
VARR_DESTROY (MIR_type_t, temp_types);
code_finish (ctx);
if (curr_func != NULL)
(*error_func) (MIR_finish_error, "finish when function %s is not finished", curr_func->name);
if (curr_module != NULL)
(*error_func) (MIR_finish_error, "finish when module %s is not finished", curr_module->name);
#if 0
if (inlined_calls != 0)
fprintf (stderr, "inlined calls = %lu, insns before = %lu, insns_after = %lu, ratio = %.2f\n",
inlined_calls, inline_insns_before, inline_insns_after,
(double) inline_insns_after / inline_insns_before);
#endif
free (ctx->string_ctx);
free (ctx);
ctx = NULL;
}
MIR_module_t MIR_new_module (MIR_context_t ctx, const char *name) {
if (curr_module != NULL)
(*error_func) (MIR_nested_module_error,
"Creating module when previous module %s is not finished", curr_module->name);
if ((curr_module = malloc (sizeof (struct MIR_module))) == NULL)
(*error_func) (MIR_alloc_error, "Not enough memory for module %s creation", name);
init_module (ctx, curr_module, name);
DLIST_APPEND (MIR_module_t, all_modules, curr_module);
return curr_module;
}
DLIST (MIR_module_t) * MIR_get_module_list (MIR_context_t ctx) { return &all_modules; }
static const char *type_str (MIR_type_t tp) {
switch (tp) {
case MIR_T_I8: return "i8";
case MIR_T_U8: return "u8";
case MIR_T_I16: return "i16";
case MIR_T_U16: return "u16";
case MIR_T_I32: return "i32";
case MIR_T_U32: return "u32";
case MIR_T_I64: return "i64";
case MIR_T_U64: return "u64";
case MIR_T_F: return "f";
case MIR_T_D: return "d";
case MIR_T_LD: return "ld";
case MIR_T_P: return "p";
case MIR_T_UNDEF: return "undef";
default: return "";
}
}
const char *MIR_type_str (MIR_context_t ctx, MIR_type_t tp) {
const char *str = type_str (tp);
if (strcmp (str, "") == 0)
(*error_func) (MIR_wrong_param_value_error, "MIR_type_str: wrong type");
return str;
}
static const char *mode_str (MIR_op_mode_t mode) {
switch (mode) {
case MIR_OP_REG: return "reg";
case MIR_OP_HARD_REG: return "hard_reg";
case MIR_OP_INT: return "int";
case MIR_OP_UINT: return "uint";
case MIR_OP_FLOAT: return "float";
case MIR_OP_DOUBLE: return "double";
case MIR_OP_LDOUBLE: return "ldouble";
case MIR_OP_REF: return "ref";
case MIR_OP_STR: return "str";
case MIR_OP_MEM: return "mem";
case MIR_OP_HARD_REG_MEM: return "hard_reg_mem";
case MIR_OP_LABEL: return "label";
case MIR_OP_BOUND: return "bound";
case MIR_OP_UNDEF: return "undef";
default: return "";
}
}
static MIR_item_t add_item (MIR_context_t ctx, MIR_item_t item) {
int replace_p;
MIR_item_t tab_item;
if ((tab_item = find_item (ctx, MIR_item_name (ctx, item), item->module)) == NULL) {
DLIST_APPEND (MIR_item_t, curr_module->items, item);
HTAB_DO (MIR_item_t, module_item_tab, item, HTAB_INSERT, item);
return item;
}
switch (tab_item->item_type) {
case MIR_import_item:
if (item->item_type != MIR_import_item)
(*error_func) (MIR_import_export_error,
"existing module definition %s already defined as import",
tab_item->u.import_id);
item = tab_item;
break;
case MIR_export_item:
case MIR_forward_item:
replace_p = FALSE;
if (item->item_type == MIR_import_item) {
(*error_func) (MIR_import_export_error, "export/forward of import %s", item->u.import_id);
} else if (item->item_type != MIR_export_item && item->item_type != MIR_forward_item) {
replace_p = TRUE;
DLIST_APPEND (MIR_item_t, curr_module->items, item);
} else {
if (tab_item->item_type == item->item_type)
item = tab_item;
else
DLIST_APPEND (MIR_item_t, curr_module->items, item);
if (item->item_type == MIR_export_item && tab_item->item_type == MIR_forward_item)
replace_p = TRUE;
}
if (replace_p) { /* replace forward by export or export/forward by its definition: */
tab_item->ref_def = item;
if (tab_item->item_type == MIR_export_item) item->export_p = TRUE;
HTAB_DO (MIR_item_t, module_item_tab, tab_item, HTAB_DELETE, tab_item);
HTAB_DO (MIR_item_t, module_item_tab, item, HTAB_INSERT, tab_item);
mir_assert (item == tab_item);
}
break;
case MIR_proto_item:
(*error_func) (MIR_repeated_decl_error, "item %s was already defined as proto",
tab_item->u.proto->name);
break;
case MIR_bss_item:
case MIR_data_item:
case MIR_ref_data_item:
case MIR_expr_data_item:
case MIR_func_item:
if (item->item_type == MIR_export_item) {
if (tab_item->export_p) {
item = tab_item;
} else { /* just keep one export: */
tab_item->export_p = TRUE;
DLIST_APPEND (MIR_item_t, curr_module->items, item);
item->ref_def = tab_item;
}
} else if (item->item_type == MIR_forward_item) {
DLIST_APPEND (MIR_item_t, curr_module->items, item);
item->ref_def = tab_item;
} else if (item->item_type == MIR_import_item) {
(*error_func) (MIR_import_export_error, "import of local definition %s", item->u.import_id);
} else {
(*error_func) (MIR_repeated_decl_error, "Repeated item declaration %s",
MIR_item_name (ctx, item));
}
break;
default: mir_assert (FALSE);
}
return item;
}
static MIR_item_t create_item (MIR_context_t ctx, MIR_item_type_t item_type,
const char *item_name) {
MIR_item_t item;
if (curr_module == NULL) (*error_func) (MIR_no_module_error, "%s outside module", item_name);
if ((item = malloc (sizeof (struct MIR_item))) == NULL)
(*error_func) (MIR_alloc_error, "Not enough memory for creation of item %s", item_name);
item->data = NULL;
item->module = curr_module;
item->item_type = item_type;
item->ref_def = NULL;
item->export_p = FALSE;
item->section_head_p = FALSE;
item->addr = NULL;
return item;
}
static MIR_item_t new_export_import_forward (MIR_context_t ctx, const char *name,
MIR_item_type_t item_type, const char *item_name,
int create_only_p) {
MIR_item_t item, tab_item;
const char *uniq_name;
item = create_item (ctx, item_type, item_name);
uniq_name = string_store (ctx, &strings, &string_tab, (MIR_str_t){strlen (name) + 1, name}).str.s;
if (item_type == MIR_export_item)
item->u.export_id = uniq_name;
else if (item_type == MIR_import_item)
item->u.import_id = uniq_name;
else
item->u.forward_id = uniq_name;
if (create_only_p) return item;
if ((tab_item = add_item (ctx, item)) != item) {
free (item);
item = tab_item;
}
return item;
}
MIR_item_t MIR_new_export (MIR_context_t ctx, const char *name) {
return new_export_import_forward (ctx, name, MIR_export_item, "export", FALSE);
}
MIR_item_t MIR_new_import (MIR_context_t ctx, const char *name) {
return new_export_import_forward (ctx, name, MIR_import_item, "import", FALSE);
}
MIR_item_t MIR_new_forward (MIR_context_t ctx, const char *name) {
return new_export_import_forward (ctx, name, MIR_forward_item, "forward", FALSE);
}
MIR_item_t MIR_new_bss (MIR_context_t ctx, const char *name, size_t len) {
MIR_item_t tab_item, item = create_item (ctx, MIR_bss_item, "bss");
item->u.bss = malloc (sizeof (struct MIR_bss));
if (item->u.bss == NULL) {
free (item);
(*error_func) (MIR_alloc_error, "Not enough memory for creation of bss %s", name);
}
if (name != NULL)
name = string_store (ctx, &strings, &string_tab, (MIR_str_t){strlen (name) + 1, name}).str.s;
item->u.bss->name = name;
item->u.bss->len = len;
if (name == NULL) {
DLIST_APPEND (MIR_item_t, curr_module->items, item);
} else if ((tab_item = add_item (ctx, item)) != item) {
free (item);
item = tab_item;
}
return item;
}
static MIR_type_t canon_type (MIR_type_t type) {
#if __SIZEOF_LONG_DOUBLE__ == 8
if (type == MIR_T_LD) type = MIR_T_D;
#endif
return type;
}
size_t _MIR_type_size (MIR_context_t ctx, MIR_type_t type) {
switch (type) {
case MIR_T_I8: return sizeof (int8_t);
case MIR_T_U8: return sizeof (uint8_t);
case MIR_T_I16: return sizeof (int16_t);
case MIR_T_U16: return sizeof (uint16_t);
case MIR_T_I32: return sizeof (int32_t);
case MIR_T_U32: return sizeof (uint32_t);
case MIR_T_I64: return sizeof (int64_t);
case MIR_T_U64: return sizeof (uint64_t);
case MIR_T_F: return sizeof (float);
case MIR_T_D: return sizeof (double);
case MIR_T_LD: return sizeof (long double);
case MIR_T_P: return sizeof (void *);
default: mir_assert (FALSE); return 1;
}
}
MIR_item_t MIR_new_data (MIR_context_t ctx, const char *name, MIR_type_t el_type, size_t nel,
const void *els) {
MIR_item_t tab_item, item = create_item (ctx, MIR_data_item, "data");
MIR_data_t data;
size_t el_len = _MIR_type_size (ctx, el_type);
item->u.data = data = malloc (sizeof (struct MIR_data) + el_len * nel);
if (data == NULL) {
free (item);
(*error_func) (MIR_alloc_error, "Not enough memory for creation of data %s",
name == NULL ? "" : name);
}
if (name != NULL)
name = string_store (ctx, &strings, &string_tab, (MIR_str_t){strlen (name) + 1, name}).str.s;
data->name = name;
if (name == NULL) {
DLIST_APPEND (MIR_item_t, curr_module->items, item);
} else if ((tab_item = add_item (ctx, item)) != item) {
free (item);
item = tab_item;
}
data->el_type = canon_type (el_type);
data->nel = nel;
memcpy (data->u.els, els, el_len * nel);
return item;
}