forked from vnmakarov/mir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mir.c
7022 lines (6437 loc) · 278 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-2024 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);
DEF_VARR (MIR_proto_t);
struct gen_ctx;
struct c2mir_ctx;
struct string_ctx;
struct reg_ctx;
struct alias_ctx;
struct simplify_ctx;
struct machine_code_ctx;
struct io_ctx;
struct scan_ctx;
struct hard_reg_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;
int func_redef_permission_p; /* when true loaded func can be redfined lately */
VARR (size_t) * insn_nops; /* constant after initialization */
VARR (MIR_proto_t) * unspec_protos; /* protos of unspec insns (set only during initialization) */
VARR (char) * temp_string;
VARR (uint8_t) * temp_data, *used_label_p;
HTAB (MIR_item_t) * module_item_tab;
/* Module to keep items potentially used by all modules: */
struct MIR_module environment_module;
MIR_module_t curr_module;
MIR_func_t curr_func;
size_t curr_label_num;
DLIST (MIR_module_t) all_modules;
VARR (MIR_module_t) * modules_to_link;
VARR (MIR_op_t) * temp_ops;
struct string_ctx *string_ctx;
struct reg_ctx *reg_ctx;
struct alias_ctx *alias_ctx;
struct simplify_ctx *simplify_ctx;
struct machine_code_ctx *machine_code_ctx;
struct io_ctx *io_ctx;
struct scan_ctx *scan_ctx;
struct hard_reg_ctx *hard_reg_ctx;
struct interp_ctx *interp_ctx;
void *setjmp_addr; /* used in interpreter to call setjmp directly not from a shim and FFI */
void *wrapper_end_addr; /* used by generator */
};
#define error_func ctx->error_func
#define func_redef_permission_p ctx->func_redef_permission_p
#define unspec_protos ctx->unspec_protos
#define insn_nops ctx->insn_nops
#define temp_string ctx->temp_string
#define temp_data ctx->temp_data
#define used_label_p ctx->used_label_p
#define module_item_tab ctx->module_item_tab
#define environment_module ctx->environment_module
#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 temp_ops ctx->temp_ops
#define setjmp_addr ctx->setjmp_addr
#define wrapper_end_addr ctx->wrapper_end_addr
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 "mir-bitmap.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 MIR_UNUSED,
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) {
MIR_get_error_func (ctx) (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 MIR_UNUSED, 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 char op_modes[5];
};
#define OUT_FLAG (1 << 7)
static const struct insn_desc insn_descs[] = {
{MIR_MOV, "mov", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_FMOV, "fmov", {MIR_OP_FLOAT | OUT_FLAG, MIR_OP_FLOAT, MIR_OP_BOUND}},
{MIR_DMOV, "dmov", {MIR_OP_DOUBLE | OUT_FLAG, MIR_OP_DOUBLE, MIR_OP_BOUND}},
{MIR_LDMOV, "ldmov", {MIR_OP_LDOUBLE | OUT_FLAG, MIR_OP_LDOUBLE, MIR_OP_BOUND}},
{MIR_EXT8, "ext8", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_EXT16, "ext16", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_EXT32, "ext32", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_UEXT8, "uext8", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_UEXT16, "uext16", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_UEXT32, "uext32", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_I2F, "i2f", {MIR_OP_FLOAT | OUT_FLAG, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_I2D, "i2d", {MIR_OP_DOUBLE | OUT_FLAG, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_I2LD, "i2ld", {MIR_OP_LDOUBLE | OUT_FLAG, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_UI2F, "ui2f", {MIR_OP_FLOAT | OUT_FLAG, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_UI2D, "ui2d", {MIR_OP_DOUBLE | OUT_FLAG, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_UI2LD, "ui2ld", {MIR_OP_LDOUBLE | OUT_FLAG, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_F2I, "f2i", {MIR_OP_INT | OUT_FLAG, MIR_OP_FLOAT, MIR_OP_BOUND}},
{MIR_D2I, "d2i", {MIR_OP_INT | OUT_FLAG, MIR_OP_DOUBLE, MIR_OP_BOUND}},
{MIR_LD2I, "ld2i", {MIR_OP_INT | OUT_FLAG, MIR_OP_LDOUBLE, MIR_OP_BOUND}},
{MIR_F2D, "f2d", {MIR_OP_DOUBLE | OUT_FLAG, MIR_OP_FLOAT, MIR_OP_BOUND}},
{MIR_F2LD, "f2ld", {MIR_OP_LDOUBLE | OUT_FLAG, MIR_OP_FLOAT, MIR_OP_BOUND}},
{MIR_D2F, "d2f", {MIR_OP_FLOAT | OUT_FLAG, MIR_OP_DOUBLE, MIR_OP_BOUND}},
{MIR_D2LD, "d2ld", {MIR_OP_LDOUBLE | OUT_FLAG, MIR_OP_DOUBLE, MIR_OP_BOUND}},
{MIR_LD2F, "ld2f", {MIR_OP_FLOAT | OUT_FLAG, MIR_OP_LDOUBLE, MIR_OP_BOUND}},
{MIR_LD2D, "ld2d", {MIR_OP_DOUBLE | OUT_FLAG, MIR_OP_LDOUBLE, MIR_OP_BOUND}},
{MIR_NEG, "neg", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_NEGS, "negs", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_FNEG, "fneg", {MIR_OP_FLOAT | OUT_FLAG, MIR_OP_FLOAT, MIR_OP_BOUND}},
{MIR_DNEG, "dneg", {MIR_OP_DOUBLE | OUT_FLAG, MIR_OP_DOUBLE, MIR_OP_BOUND}},
{MIR_LDNEG, "ldneg", {MIR_OP_LDOUBLE | OUT_FLAG, MIR_OP_LDOUBLE, MIR_OP_BOUND}},
{MIR_ADDR, "addr", {MIR_OP_INT | OUT_FLAG, MIR_OP_REG, MIR_OP_BOUND}}, /* MIR_OP_REG! */
{MIR_ADDR8, "addr8", {MIR_OP_INT | OUT_FLAG, MIR_OP_REG, MIR_OP_BOUND}}, /* MIR_OP_REG! */
{MIR_ADDR16, "addr16", {MIR_OP_INT | OUT_FLAG, MIR_OP_REG, MIR_OP_BOUND}}, /* MIR_OP_REG! */
{MIR_ADDR32, "addr32", {MIR_OP_INT | OUT_FLAG, MIR_OP_REG, MIR_OP_BOUND}}, /* MIR_OP_REG! */
{MIR_ADD, "add", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_ADDS, "adds", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_FADD, "fadd", {MIR_OP_FLOAT | OUT_FLAG, MIR_OP_FLOAT, MIR_OP_FLOAT, MIR_OP_BOUND}},
{MIR_DADD, "dadd", {MIR_OP_DOUBLE | OUT_FLAG, MIR_OP_DOUBLE, MIR_OP_DOUBLE, MIR_OP_BOUND}},
{MIR_LDADD, "ldadd", {MIR_OP_LDOUBLE | OUT_FLAG, MIR_OP_LDOUBLE, MIR_OP_LDOUBLE, MIR_OP_BOUND}},
{MIR_SUB, "sub", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_SUBS, "subs", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_FSUB, "fsub", {MIR_OP_FLOAT | OUT_FLAG, MIR_OP_FLOAT, MIR_OP_FLOAT, MIR_OP_BOUND}},
{MIR_DSUB, "dsub", {MIR_OP_DOUBLE | OUT_FLAG, MIR_OP_DOUBLE, MIR_OP_DOUBLE, MIR_OP_BOUND}},
{MIR_LDSUB, "ldsub", {MIR_OP_LDOUBLE | OUT_FLAG, MIR_OP_LDOUBLE, MIR_OP_LDOUBLE, MIR_OP_BOUND}},
{MIR_MUL, "mul", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_MULS, "muls", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_FMUL, "fmul", {MIR_OP_FLOAT | OUT_FLAG, MIR_OP_FLOAT, MIR_OP_FLOAT, MIR_OP_BOUND}},
{MIR_DMUL, "dmul", {MIR_OP_DOUBLE | OUT_FLAG, MIR_OP_DOUBLE, MIR_OP_DOUBLE, MIR_OP_BOUND}},
{MIR_LDMUL, "ldmul", {MIR_OP_LDOUBLE | OUT_FLAG, MIR_OP_LDOUBLE, MIR_OP_LDOUBLE, MIR_OP_BOUND}},
{MIR_DIV, "div", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_DIVS, "divs", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_UDIV, "udiv", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_UDIVS, "udivs", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_FDIV, "fdiv", {MIR_OP_FLOAT | OUT_FLAG, MIR_OP_FLOAT, MIR_OP_FLOAT, MIR_OP_BOUND}},
{MIR_DDIV, "ddiv", {MIR_OP_DOUBLE | OUT_FLAG, MIR_OP_DOUBLE, MIR_OP_DOUBLE, MIR_OP_BOUND}},
{MIR_LDDIV, "lddiv", {MIR_OP_LDOUBLE | OUT_FLAG, MIR_OP_LDOUBLE, MIR_OP_LDOUBLE, MIR_OP_BOUND}},
{MIR_MOD, "mod", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_MODS, "mods", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_UMOD, "umod", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_UMODS, "umods", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_AND, "and", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_ANDS, "ands", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_OR, "or", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_ORS, "ors", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_XOR, "xor", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_XORS, "xors", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_LSH, "lsh", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_LSHS, "lshs", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_RSH, "rsh", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_RSHS, "rshs", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_URSH, "ursh", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_URSHS, "urshs", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_EQ, "eq", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_EQS, "eqs", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_FEQ, "feq", {MIR_OP_INT | OUT_FLAG, MIR_OP_FLOAT, MIR_OP_FLOAT, MIR_OP_BOUND}},
{MIR_DEQ, "deq", {MIR_OP_INT | OUT_FLAG, MIR_OP_DOUBLE, MIR_OP_DOUBLE, MIR_OP_BOUND}},
{MIR_LDEQ, "ldeq", {MIR_OP_INT | OUT_FLAG, MIR_OP_LDOUBLE, MIR_OP_LDOUBLE, MIR_OP_BOUND}},
{MIR_NE, "ne", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_NES, "nes", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_FNE, "fne", {MIR_OP_INT | OUT_FLAG, MIR_OP_FLOAT, MIR_OP_FLOAT, MIR_OP_BOUND}},
{MIR_DNE, "dne", {MIR_OP_INT | OUT_FLAG, MIR_OP_DOUBLE, MIR_OP_DOUBLE, MIR_OP_BOUND}},
{MIR_LDNE, "ldne", {MIR_OP_INT | OUT_FLAG, MIR_OP_LDOUBLE, MIR_OP_LDOUBLE, MIR_OP_BOUND}},
{MIR_LT, "lt", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_LTS, "lts", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_ULT, "ult", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_ULTS, "ults", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_FLT, "flt", {MIR_OP_INT | OUT_FLAG, MIR_OP_FLOAT, MIR_OP_FLOAT, MIR_OP_BOUND}},
{MIR_DLT, "dlt", {MIR_OP_INT | OUT_FLAG, MIR_OP_DOUBLE, MIR_OP_DOUBLE, MIR_OP_BOUND}},
{MIR_LDLT, "ldlt", {MIR_OP_INT | OUT_FLAG, MIR_OP_LDOUBLE, MIR_OP_LDOUBLE, MIR_OP_BOUND}},
{MIR_LE, "le", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_LES, "les", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_ULE, "ule", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_ULES, "ules", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_FLE, "fle", {MIR_OP_INT | OUT_FLAG, MIR_OP_FLOAT, MIR_OP_FLOAT, MIR_OP_BOUND}},
{MIR_DLE, "dle", {MIR_OP_INT | OUT_FLAG, MIR_OP_DOUBLE, MIR_OP_DOUBLE, MIR_OP_BOUND}},
{MIR_LDLE, "ldle", {MIR_OP_INT | OUT_FLAG, MIR_OP_LDOUBLE, MIR_OP_LDOUBLE, MIR_OP_BOUND}},
{MIR_GT, "gt", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_GTS, "gts", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_UGT, "ugt", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_UGTS, "ugts", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_FGT, "fgt", {MIR_OP_INT | OUT_FLAG, MIR_OP_FLOAT, MIR_OP_FLOAT, MIR_OP_BOUND}},
{MIR_DGT, "dgt", {MIR_OP_INT | OUT_FLAG, MIR_OP_DOUBLE, MIR_OP_DOUBLE, MIR_OP_BOUND}},
{MIR_LDGT, "ldgt", {MIR_OP_INT | OUT_FLAG, MIR_OP_LDOUBLE, MIR_OP_LDOUBLE, MIR_OP_BOUND}},
{MIR_GE, "ge", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_GES, "ges", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_UGE, "uge", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_UGES, "uges", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_FGE, "fge", {MIR_OP_INT | OUT_FLAG, MIR_OP_FLOAT, MIR_OP_FLOAT, MIR_OP_BOUND}},
{MIR_DGE, "dge", {MIR_OP_INT | OUT_FLAG, MIR_OP_DOUBLE, MIR_OP_DOUBLE, MIR_OP_BOUND}},
{MIR_LDGE, "ldge", {MIR_OP_INT | OUT_FLAG, MIR_OP_LDOUBLE, MIR_OP_LDOUBLE, MIR_OP_BOUND}},
{MIR_ADDO, "addo", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_ADDOS, "addos", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_SUBO, "subo", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_SUBOS, "subos", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_MULO, "mulo", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_MULOS, "mulos", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_UMULO, "umulo", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_UMULOS, "umulos", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_INT, 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_BO, "bo", {MIR_OP_LABEL, MIR_OP_BOUND}},
{MIR_UBO, "ubo", {MIR_OP_LABEL, MIR_OP_BOUND}},
{MIR_BNO, "bno", {MIR_OP_LABEL, MIR_OP_BOUND}},
{MIR_UBNO, "ubno", {MIR_OP_LABEL, MIR_OP_BOUND}},
{MIR_LADDR, "laddr", {MIR_OP_INT, MIR_OP_LABEL, MIR_OP_BOUND}},
{MIR_JMPI, "jmpi", {MIR_OP_INT, MIR_OP_BOUND}},
{MIR_CALL, "call", {MIR_OP_BOUND}},
{MIR_INLINE, "inline", {MIR_OP_BOUND}},
{MIR_JCALL, "jcall", {MIR_OP_BOUND}},
{MIR_SWITCH, "switch", {MIR_OP_BOUND}},
{MIR_RET, "ret", {MIR_OP_BOUND}},
{MIR_JRET, "jret", {MIR_OP_INT, MIR_OP_BOUND}},
{MIR_ALLOCA, "alloca", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_BSTART, "bstart", {MIR_OP_INT | OUT_FLAG, MIR_OP_BOUND}},
{MIR_BEND, "bend", {MIR_OP_INT, MIR_OP_BOUND}},
{MIR_VA_ARG, "va_arg", {MIR_OP_INT | OUT_FLAG, MIR_OP_INT, MIR_OP_UNDEF, MIR_OP_BOUND}},
{MIR_VA_BLOCK_ARG,
"va_block_arg",
{MIR_OP_INT, MIR_OP_INT, MIR_OP_INT, MIR_OP_INT, 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_UNSPEC, "unspec", {MIR_OP_BOUND}},
{MIR_PRSET, "prset", {MIR_OP_UNDEF, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_PRBEQ, "prbeq", {MIR_OP_LABEL, MIR_OP_UNDEF, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_PRBNE, "prbne", {MIR_OP_LABEL, MIR_OP_UNDEF, MIR_OP_INT, MIR_OP_BOUND}},
{MIR_USE, "use", {MIR_OP_BOUND}},
{MIR_PHI, "phi", {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);
}
int64_t _MIR_addr_offset (MIR_context_t ctx MIR_UNUSED, MIR_insn_code_t code) {
int v = 1;
if (code == MIR_ADDR || *(char *) &v != 0) return 0;
return code == MIR_ADDR8 ? 7 : code == MIR_ADDR16 ? 6 : 4;
}
/* 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 MIR_UNUSED) {
return (htab_hash_t) mir_hash (str.str.s, str.str.len, 0);
}
static int str_eq (string_t str1, string_t str2, void *arg MIR_UNUSED) {
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 MIR_UNUSED, 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)
MIR_get_error_func (ctx) (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 struct string get_ctx_string (MIR_context_t ctx, MIR_str_t str) {
return string_store (ctx, &strings, &string_tab, str);
}
static const char *get_ctx_str (MIR_context_t ctx, const char *string) {
return get_ctx_string (ctx, (MIR_str_t){strlen (string) + 1, string}).str.s;
}
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);
}
/* Functions to work with aliases. */
struct alias_ctx {
VARR (string_t) * aliases;
HTAB (string_t) * alias_tab;
};
#define aliases ctx->alias_ctx->aliases
#define alias_tab ctx->alias_ctx->alias_tab
MIR_alias_t MIR_alias (MIR_context_t ctx, const char *name) {
return (MIR_alias_t) string_store (ctx, &aliases, &alias_tab,
(MIR_str_t){strlen (name) + 1, name})
.num;
}
const char *MIR_alias_name (MIR_context_t ctx, MIR_alias_t alias) {
if (alias == 0) return "";
if (alias >= VARR_LENGTH (string_t, aliases))
MIR_get_error_func (ctx) (MIR_alloc_error, "Wrong alias number");
return VARR_ADDR (string_t, aliases)[alias].str.s;
}
/* New Page */
/* We attribute global vars to func as func can be inlined from different module. */
typedef struct reg_desc {
MIR_type_t type;
MIR_reg_t reg; /* key reg2rdn hash tab */
char *name; /* key for the name2rdn hash tab */
char *hard_reg_name; /* NULL unless tied global, key for hrn2rdn */
} reg_desc_t;
DEF_VARR (reg_desc_t);
DEF_HTAB (size_t);
typedef struct func_regs {
VARR (reg_desc_t) * reg_descs;
HTAB (size_t) * name2rdn_tab;
HTAB (size_t) * hrn2rdn_tab;
HTAB (size_t) * reg2rdn_tab;
} *func_regs_t;
static int name2rdn_eq (size_t rdn1, size_t rdn2, void *arg) {
func_regs_t func_regs = arg;
reg_desc_t *addr = VARR_ADDR (reg_desc_t, func_regs->reg_descs);
return strcmp (addr[rdn1].name, addr[rdn2].name) == 0;
}
static htab_hash_t name2rdn_hash (size_t rdn, void *arg) {
func_regs_t func_regs = arg;
reg_desc_t *addr = VARR_ADDR (reg_desc_t, func_regs->reg_descs);
return (htab_hash_t) mir_hash (addr[rdn].name, strlen (addr[rdn].name), 0);
}
static int hrn2rdn_eq (size_t rdn1, size_t rdn2, void *arg) {
func_regs_t func_regs = arg;
reg_desc_t *addr = VARR_ADDR (reg_desc_t, func_regs->reg_descs);
return strcmp (addr[rdn1].hard_reg_name, addr[rdn2].hard_reg_name) == 0;
}
static htab_hash_t hrn2rdn_hash (size_t rdn, void *arg) {
func_regs_t func_regs = arg;
reg_desc_t *addr = VARR_ADDR (reg_desc_t, func_regs->reg_descs);
return (htab_hash_t) mir_hash (addr[rdn].hard_reg_name, strlen (addr[rdn].hard_reg_name), 0);
}
static int reg2rdn_eq (size_t rdn1, size_t rdn2, void *arg) {
func_regs_t func_regs = arg;
reg_desc_t *addr = VARR_ADDR (reg_desc_t, func_regs->reg_descs);
return addr[rdn1].reg == addr[rdn2].reg;
}
static htab_hash_t reg2rdn_hash (size_t rdn, void *arg) {
func_regs_t func_regs = arg;
reg_desc_t *addr = VARR_ADDR (reg_desc_t, func_regs->reg_descs);
return (htab_hash_t) mir_hash_finish (mir_hash_step (mir_hash_init (0), addr[rdn].reg));
}
static void func_regs_init (MIR_context_t ctx, MIR_func_t func) {
func_regs_t func_regs;
reg_desc_t rd = {MIR_T_I64, 0, NULL, NULL};
if ((func_regs = func->internal = malloc (sizeof (struct func_regs))) == NULL)
MIR_get_error_func (ctx) (MIR_alloc_error, "Not enough memory for func regs info");
VARR_CREATE (reg_desc_t, func_regs->reg_descs, 50);
VARR_PUSH (reg_desc_t, func_regs->reg_descs, rd); /* for 0 reg */
HTAB_CREATE (size_t, func_regs->name2rdn_tab, 100, name2rdn_hash, name2rdn_eq, func_regs);
HTAB_CREATE (size_t, func_regs->hrn2rdn_tab, 10, hrn2rdn_hash, hrn2rdn_eq, func_regs);
HTAB_CREATE (size_t, func_regs->reg2rdn_tab, 100, reg2rdn_hash, reg2rdn_eq, func_regs);
}
static int target_locs_num (MIR_reg_t loc, MIR_type_t type);
static int target_hard_reg_type_ok_p (MIR_reg_t hard_reg, MIR_type_t type);
static int target_fixed_hard_reg_p (MIR_reg_t hard_reg);
static MIR_reg_t create_func_reg (MIR_context_t ctx, MIR_func_t func, const char *name,
const char *hard_reg_name, MIR_reg_t reg, MIR_type_t type,
int any_p, char **name_ptr) {
func_regs_t func_regs = func->internal;
MIR_module_t func_module;
reg_desc_t rd, *rd_ref;
size_t rdn, tab_rdn;
int htab_res;
MIR_reg_t hr;
if (!any_p && _MIR_reserved_name_p (ctx, name))
MIR_get_error_func (ctx) (MIR_reserved_name_error, "redefining a reserved name %s", name);
rd.name = (char *) get_ctx_str (ctx, name);
if (hard_reg_name != NULL) hard_reg_name = get_ctx_str (ctx, hard_reg_name);
rd.hard_reg_name = (char *) hard_reg_name;
rd.type = type;
rd.reg = reg; /* 0 is reserved */
rdn = VARR_LENGTH (reg_desc_t, func_regs->reg_descs);
VARR_PUSH (reg_desc_t, func_regs->reg_descs, rd);
if (HTAB_DO (size_t, func_regs->name2rdn_tab, rdn, HTAB_FIND, tab_rdn)) {
VARR_POP (reg_desc_t, func_regs->reg_descs);
MIR_get_error_func (ctx) (MIR_repeated_decl_error, "Repeated reg declaration %s", name);
}
if (hard_reg_name != NULL) {
if ((hr = _MIR_get_hard_reg (ctx, hard_reg_name)) == MIR_NON_VAR) {
MIR_get_error_func (ctx) (MIR_hard_reg_error, "unknown hard reg %s", hard_reg_name);
} else if (!target_hard_reg_type_ok_p (hr, type)) {
MIR_get_error_func (ctx) (MIR_hard_reg_error,
"reg %s tied to hard reg %s can not be of type %s", name,
hard_reg_name, MIR_type_str (ctx, type));
} else if (target_fixed_hard_reg_p (hr)) {
MIR_get_error_func (ctx) (MIR_hard_reg_error,
"reg %s can not be tied to reserved hard reg %s", name,
hard_reg_name);
} else if (target_locs_num (hr, type) > 1)
MIR_get_error_func (ctx) (MIR_hard_reg_error, "reg %s tied to %s requires more one hard reg",
name, hard_reg_name);
if (HTAB_DO (size_t, func_regs->hrn2rdn_tab, rdn, HTAB_FIND, tab_rdn)) {
rd_ref = &VARR_ADDR (reg_desc_t, func_regs->reg_descs)[tab_rdn];
if (type != rd_ref->type)
MIR_get_error_func (ctx) (MIR_repeated_decl_error,
"regs %s and %s tied to hard reg %s have different types", name,
rd_ref->name, hard_reg_name);
/* Use always one reg for global vars assigned to hard regs: */
VARR_POP (reg_desc_t, func_regs->reg_descs);
*name_ptr = rd_ref->name;
return rd_ref->reg;
}
func_module = func->func_item->module;
if (func_module->data == NULL) func_module->data = bitmap_create2 (128);
bitmap_set_bit_p (func_module->data, hr); /* hard regs used for globals */
}
*name_ptr = rd.name;
htab_res = HTAB_DO (size_t, func_regs->name2rdn_tab, rdn, HTAB_INSERT, tab_rdn);
mir_assert (!htab_res);
if (hard_reg_name != NULL) {
htab_res = HTAB_DO (size_t, func_regs->hrn2rdn_tab, rdn, HTAB_INSERT, tab_rdn);
mir_assert (!htab_res);
}
htab_res = HTAB_DO (size_t, func_regs->reg2rdn_tab, rdn, HTAB_INSERT, tab_rdn);
mir_assert (!htab_res);
return reg;
}
static void func_regs_finish (MIR_context_t ctx MIR_UNUSED, MIR_func_t func) {
func_regs_t func_regs = func->internal;
VARR_DESTROY (reg_desc_t, func_regs->reg_descs);
HTAB_DESTROY (size_t, func_regs->name2rdn_tab);
HTAB_DESTROY (size_t, func_regs->hrn2rdn_tab);
HTAB_DESTROY (size_t, func_regs->reg2rdn_tab);
free (func->internal);
func->internal = 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_UNUSED, MIR_item_t item) {
mir_assert (item != NULL);
switch (item->item_type) {
case MIR_func_item: return item->u.func->name;
case MIR_proto_item: return item->u.proto->name;
case MIR_import_item: return item->u.import_id;
case MIR_export_item: return item->u.export_id;
case MIR_forward_item: return item->u.forward_id;
case MIR_bss_item: return item->u.bss->name;
case MIR_data_item: return item->u.data->name;
case MIR_ref_data_item: return item->u.ref_data->name;
case MIR_lref_data_item: return item->u.lref_data->name;
case MIR_expr_data_item: return item->u.expr_data->name;
default: mir_assert (FALSE); return NULL;
}
}
MIR_func_t MIR_get_item_func (MIR_context_t ctx MIR_UNUSED, MIR_item_t item) {
mir_assert (item != NULL);
if (item->item_type == MIR_func_item) {
return item->u.func;
} else {
return NULL;
}
}
#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 simplify_init (MIR_context_t ctx);
static void simplify_finish (MIR_context_t ctx);
MIR_error_func_t MIR_get_error_func (MIR_context_t ctx) { return error_func; } // ??? atomic
void MIR_set_error_func (MIR_context_t ctx, MIR_error_func_t func) { // ?? atomic access
error_func = func;
}
int MIR_get_func_redef_permission_p (MIR_context_t ctx) { return func_redef_permission_p; }
void MIR_set_func_redef_permission (MIR_context_t ctx, int enable_p) { // ?? atomic access
func_redef_permission_p = enable_p;
}
static htab_hash_t item_hash (MIR_item_t it, void *arg MIR_UNUSED) {
return (htab_hash_t) 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 MIR_UNUSED) {
return it1->module == it2->module && MIR_item_name (NULL, it1) == MIR_item_name (NULL, it2);
}
static MIR_item_t item_tab_find (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 MIR_item_t item_tab_insert (MIR_context_t ctx, MIR_item_t item) {
MIR_item_t tab_item;
HTAB_DO (MIR_item_t, module_item_tab, item, HTAB_INSERT, tab_item);
return tab_item;
}
static void item_tab_remove (MIR_context_t ctx, MIR_item_t item) {
HTAB_DO (MIR_item_t, module_item_tab, item, HTAB_DELETE, item);
}
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 = get_ctx_str (ctx, name);
DLIST_INIT (MIR_item_t, m->items);
}
static void code_init (MIR_context_t ctx);
static void code_finish (MIR_context_t ctx);
double _MIR_get_api_version (void) { return MIR_API_VERSION; }
static void hard_reg_name_init (MIR_context_t ctx);
static void hard_reg_name_finish (MIR_context_t ctx);
MIR_context_t _MIR_init (void) {
MIR_context_t ctx;
mir_assert (MIR_OP_BOUND < OUT_FLAG);
if ((ctx = malloc (sizeof (struct MIR_context))) == NULL)
default_error (MIR_alloc_error, "Not enough memory for ctx");
ctx->string_ctx = NULL;
ctx->alias_ctx = NULL;
ctx->reg_ctx = NULL;
ctx->simplify_ctx = NULL;
ctx->machine_code_ctx = NULL;
ctx->io_ctx = NULL;
ctx->scan_ctx = NULL;
ctx->hard_reg_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;
func_redef_permission_p = FALSE;
curr_module = NULL;
curr_func = NULL;
curr_label_num = 0;
if ((ctx->string_ctx = malloc (sizeof (struct string_ctx))) == NULL
|| (ctx->alias_ctx = malloc (sizeof (struct string_ctx))) == NULL)
MIR_get_error_func (ctx) (MIR_alloc_error, "Not enough memory for ctx");
string_init (&strings, &string_tab);
string_init (&aliases, &alias_tab);
VARR_CREATE (MIR_proto_t, unspec_protos, 0);
check_and_prepare_insn_descs (ctx);
DLIST_INIT (MIR_module_t, all_modules);
simplify_init (ctx);
VARR_CREATE (char, temp_string, 64);
VARR_CREATE (uint8_t, temp_data, 512);
VARR_CREATE (uint8_t, used_label_p, 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);
VARR_CREATE (MIR_op_t, temp_ops, 0);
init_module (ctx, &environment_module, ".environment");
HTAB_CREATE (MIR_item_t, module_item_tab, 512, item_hash, item_eq, NULL);
setjmp_addr = NULL;
code_init (ctx);
wrapper_end_addr = _MIR_get_wrapper_end (ctx); /* should be after code_init */
hard_reg_name_init (ctx);
interp_init (ctx);
return ctx;
}
static void remove_insn (MIR_context_t ctx, MIR_item_t func_item, MIR_insn_t insn,
DLIST (MIR_insn_t) * insns) {
mir_assert (func_item != NULL);
if (func_item->item_type != MIR_func_item)
MIR_get_error_func (ctx) (MIR_wrong_param_value_error, "MIR_remove_insn: wrong func item");
DLIST_REMOVE (MIR_insn_t, *insns, insn);
free (insn);
}
void MIR_remove_insn (MIR_context_t ctx, MIR_item_t func_item, MIR_insn_t insn) {
remove_insn (ctx, func_item, insn, &func_item->u.func->insns);
}
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) {
remove_insn (ctx, func_item, insn, insns);
}
}
static void remove_item (MIR_context_t ctx, MIR_item_t item) {
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);
if (item->u.func->global_vars != NULL) VARR_DESTROY (MIR_var_t, item->u.func->global_vars);
func_regs_finish (ctx, item->u.func);
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_lref_data_item:
if (item->addr != NULL && item->section_head_p) free (item->addr);
free (item->u.lref_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) bitmap_destroy (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);
VARR_DESTROY (MIR_op_t, temp_ops);
#if !MIR_NO_SCAN
scan_finish (ctx);
#endif
#if !MIR_NO_IO
io_finish (ctx);
#endif
VARR_DESTROY (uint8_t, temp_data);
VARR_DESTROY (uint8_t, used_label_p);
VARR_DESTROY (char, temp_string);
while (VARR_LENGTH (MIR_proto_t, unspec_protos) != 0) {
MIR_proto_t proto = VARR_POP (MIR_proto_t, unspec_protos);
VARR_DESTROY (MIR_var_t, proto->args);
free (proto);
}
VARR_DESTROY (MIR_proto_t, unspec_protos);
string_finish (&strings, &string_tab);
string_finish (&aliases, &alias_tab);
simplify_finish (ctx);
VARR_DESTROY (size_t, insn_nops);
code_finish (ctx);
hard_reg_name_finish (ctx);
if (curr_func != NULL)
MIR_get_error_func (ctx) (MIR_finish_error, "finish when function %s is not finished",
curr_func->name);
if (curr_module != NULL)
MIR_get_error_func (ctx) (MIR_finish_error, "finish when module %s is not finished",
curr_module->name);
free (ctx->string_ctx);
free (ctx->alias_ctx);
free (ctx);
ctx = NULL;
}
MIR_module_t MIR_new_module (MIR_context_t ctx, const char *name) {
if (curr_module != NULL)
MIR_get_error_func (ctx) (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)
MIR_get_error_func (ctx) (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_context_t ctx, MIR_type_t tp) {
int n;
char str[100];
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_RBLK: return "rblk";
case MIR_T_UNDEF: return "undef";
default:
if (MIR_blk_type_p (tp) && (n = tp - MIR_T_BLK) >= 0 && n < MIR_BLK_NUM) {
sprintf (str, "blk%d", n);
return get_ctx_str (ctx, str);
}
return "";
}
}
const char *MIR_type_str (MIR_context_t ctx, MIR_type_t tp) {
const char *str = type_str (ctx, tp);
if (strcmp (str, "") == 0)
MIR_get_error_func (ctx) (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_VAR: return "var";
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_VAR_MEM: return "var_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 = item_tab_find (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)
MIR_get_error_func (ctx) (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) {
MIR_get_error_func (ctx) (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)