-
Notifications
You must be signed in to change notification settings - Fork 22
/
ir_main.c
1503 lines (1369 loc) · 41.1 KB
/
ir_main.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
/*
* IR - Lightweight JIT Compilation Framework
* (IR CLI driver)
* Copyright (C) 2022 Zend by Perforce.
* Authors: Dmitry Stogov <[email protected]>
*/
#include "ir.h"
#include "ir_private.h" // TODO: move this together with loader
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
# include <windows.h>
#elif defined(__linux__) || defined(__sun)
# include <alloca.h>
#endif
#ifndef _WIN32
# include <sys/time.h>
static double ir_time(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
}
#else
static double ir_time(void)
{
FILETIME filetime;
GetSystemTimeAsFileTime(&filetime);
return (double)((((uint64_t)filetime.dwHighDateTime << 32) | (uint64_t)filetime.dwLowDateTime)/10) /
1000000.0;
}
#endif
static double ir_atexit_start = 0.0;
static void ir_atexit(void)
{
if (ir_atexit_start) {
double t = ir_time();
fprintf(stderr, "\nexecution time = %0.6f\n", t - ir_atexit_start);
ir_atexit_start = 0.0;
}
}
static void help(const char *cmd)
{
printf(
#if HAVE_LLVM
"Usage: %s [options] [--llvm-bitcode|--llvm-asm] input-file...\n"
#else
"Usage: %s [options] input-file...\n"
#endif
"Options:\n"
" -O[012] - optimization level (default: 2)\n"
" -fno-inline - disable function inlining\n"
" -S - dump final target assembler code\n"
" --run ... - run the main() function of generated code\n"
" (the remaining arguments are passed to main)\n"
#if defined(IR_TARGET_X86) || defined(IR_TARGET_X64)
" -mavx - use AVX instruction set\n"
" -mno-bmi1 - disable BMI1 instruction set\n"
#endif
" -muse-fp - use base frame pointer register\n"
" --emit-c [file-name] - convert to C source\n"
" --emit-llvm [file-name] - convert to LLVM\n"
" --save [file-name] - save IR\n"
" --save-cfg - save IR\n"
" --save-cfg-map - save IR\n"
" --save-rules - save IR\n"
" --save-regs - save IR\n"
" --save-use_lists - save IR\n"
" --dot [file-name] - dump IR graph\n"
" --dump [file-name] - dump IR table\n"
" --dump-after-load - dump IR after load and local optimization\n"
" --dump-after-sccp - dump IR after SCCP optimization pass\n"
" --dump-after-cfg - dump IR after CFG construction\n"
" --dump-after-dom - dump IR after Dominators tree construction\n"
" --dump-after-loop - dump IR after Loop detection\n"
" --dump-after-gcm - dump IR after GCM optimization pass\n"
" --dump-after-schedule - dump IR after SCHEDULE pass\n"
" --dump-after-live-ranges - dump IR after live ranges identification\n"
" --dump-after-coalescing - dump IR after live ranges coalescing\n"
" --dump-after-regalloc - dump IR after register allocation\n"
" --dump-after-all - dump IR after each pass\n"
" --dump-final - dump IR after all pass\n"
" --dump-size - dump generated code size\n"
" --dump-time - dump compilation and execution time\n"
" --dump-use-lists - dump def->use lists\n"
" --dump-cfg - dump CFG (Control Flow Graph)\n"
" --dump-cfg-map - dump CFG map (instruction to BB number)\n"
" --dump-live-ranges - dump live ranges\n"
#ifdef IR_DEBUG
" --debug-sccp - debug SCCP optimization pass\n"
" --debug-gcm - debug GCM optimization pass\n"
" --debug-gcm-split - debug floating node splitting\n"
" --debug-schedule - debug SCHEDULE optimization pass\n"
" --debug-ra - debug register allocator\n"
" --debug-regset <bit-mask> - restrict available register set\n"
" --debug-bb-schedule - debug BB PLCEMENT optimization pass\n"
#endif
" --disable-gdb - disable JIT code registration in GDB\n"
" --target - print JIT target\n"
" --version\n"
" --help\n",
cmd);
}
#define IR_DUMP_SAVE (1<<0)
#define IR_DUMP_DUMP (1<<1)
#define IR_DUMP_DOT (1<<2)
#define IR_DUMP_USE_LISTS (1<<3)
#define IR_DUMP_CFG (1<<4)
#define IR_DUMP_CFG_MAP (1<<5)
#define IR_DUMP_LIVE_RANGES (1<<6)
#define IR_DUMP_CODEGEN (1<<7)
#define IR_DUMP_AFTER_LOAD (1<<16)
#define IR_DUMP_AFTER_SCCP (1<<17)
#define IR_DUMP_AFTER_CFG (1<<18)
#define IR_DUMP_AFTER_DOM (1<<19)
#define IR_DUMP_AFTER_LOOP (1<<20)
#define IR_DUMP_AFTER_GCM (1<<21)
#define IR_DUMP_AFTER_SCHEDULE (1<<22)
#define IR_DUMP_AFTER_LIVE_RANGES (1<<23)
#define IR_DUMP_AFTER_COALESCING (1<<24)
#define IR_DUMP_AFTER_REGALLOC (1<<25)
#define IR_DUMP_AFTER_ALL (1<<29)
#define IR_DUMP_FINAL (1<<30)
static int _save(ir_ctx *ctx, uint32_t save_flags, uint32_t dump, uint32_t pass, FILE *f, const char *func_name)
{
char fn[4096];
bool close = 0;
if (!f) {
if (dump & IR_DUMP_AFTER_ALL) {
if (pass == IR_DUMP_AFTER_LOAD) {
snprintf(fn, sizeof(fn)-1, "01-load-%s.ir", func_name);
} else if (pass == IR_DUMP_AFTER_SCCP) {
snprintf(fn, sizeof(fn)-1, "02-sccp-%s.ir", func_name);
} else if (pass == IR_DUMP_AFTER_CFG) {
snprintf(fn, sizeof(fn)-1, "03-cfg-%s.ir", func_name);
} else if (pass == IR_DUMP_AFTER_DOM) {
snprintf(fn, sizeof(fn)-1, "04-dom-%s.ir", func_name);
} else if (pass == IR_DUMP_AFTER_LOOP) {
snprintf(fn, sizeof(fn)-1, "05-loop-%s.ir", func_name);
} else if (pass == IR_DUMP_AFTER_GCM) {
snprintf(fn, sizeof(fn)-1, "06-gcm-%s.ir", func_name);
} else if (pass == IR_DUMP_AFTER_SCHEDULE) {
snprintf(fn, sizeof(fn)-1, "07-schedule-%s.ir", func_name);
} else if (pass == IR_DUMP_AFTER_LIVE_RANGES) {
snprintf(fn, sizeof(fn)-1, "08-live-ranges-%s.ir", func_name);
} else if (pass == IR_DUMP_AFTER_COALESCING) {
snprintf(fn, sizeof(fn)-1, "09-coalescing-%s.ir", func_name);
} else if (pass == IR_DUMP_AFTER_REGALLOC) {
snprintf(fn, sizeof(fn)-1, "10-regalloc-%s.ir", func_name);
} else if (pass == IR_DUMP_FINAL) {
if (dump & IR_DUMP_CODEGEN) {
snprintf(fn, sizeof(fn)-1, "11-codegen-%s.ir", func_name);
} else {
snprintf(fn, sizeof(fn)-1, "11-final-%s.ir", func_name);
}
} else {
f = stderr; // TODO:
}
} else {
snprintf(fn, sizeof(fn)-1, "%s.ir", func_name);
}
f = fopen(fn, "w+");
if (!f) {
fprintf(stderr, "ERROR: Cannot create file '%s'\n", fn);
return 0;
}
close = 1;
}
if (pass == IR_DUMP_FINAL && (dump & IR_DUMP_CODEGEN)) {
ir_dump_codegen(ctx, f);
} else if (dump & IR_DUMP_SAVE) {
ir_save(ctx, save_flags, f);
}
if (dump & IR_DUMP_DUMP) {
ir_dump(ctx, f);
}
if (dump & IR_DUMP_DOT) {
ir_dump_dot(ctx, func_name, f);
}
if (dump & IR_DUMP_USE_LISTS) {
ir_dump_use_lists(ctx, f);
}
if (dump & IR_DUMP_CFG) {
ir_dump_cfg(ctx, f);
}
if (dump & IR_DUMP_CFG_MAP) {
ir_dump_cfg_map(ctx, f);
}
if (dump & IR_DUMP_LIVE_RANGES) {
ir_dump_live_ranges(ctx, f);
}
if (close) {
fclose(f);
}
return 1;
}
int ir_compile_func(ir_ctx *ctx, int opt_level, uint32_t save_flags, uint32_t dump, FILE *dump_file, const char *func_name)
{
if ((dump & (IR_DUMP_AFTER_LOAD|IR_DUMP_AFTER_ALL))
&& !_save(ctx, save_flags, dump, IR_DUMP_AFTER_LOAD, dump_file, func_name)) {
return 0;
}
if (opt_level > 0 || (ctx->flags & (IR_GEN_NATIVE|IR_GEN_CODE))) {
ir_build_def_use_lists(ctx);
}
#ifdef IR_DEBUG
ir_check(ctx);
#endif
/* Global Optimization */
if (opt_level > 1) {
ir_sccp(ctx);
if ((dump & (IR_DUMP_AFTER_SCCP|IR_DUMP_AFTER_ALL))
&& !_save(ctx, save_flags, dump, IR_DUMP_AFTER_SCCP, dump_file, func_name)) {
return 0;
}
#ifdef IR_DEBUG
ir_check(ctx);
#endif
}
if (opt_level > 0 || (ctx->flags & (IR_GEN_NATIVE|IR_GEN_CODE))) {
ir_build_cfg(ctx);
if ((dump & (IR_DUMP_AFTER_CFG|IR_DUMP_AFTER_ALL))
&& !_save(ctx, save_flags, dump, IR_DUMP_AFTER_CFG, dump_file, func_name)) {
return 0;
}
#ifdef IR_DEBUG
ir_check(ctx);
#endif
}
/* Schedule */
if (opt_level > 0) {
ir_build_dominators_tree(ctx);
if ((dump & (IR_DUMP_AFTER_DOM|IR_DUMP_AFTER_ALL))
&& !_save(ctx, save_flags, dump, IR_DUMP_AFTER_DOM, dump_file, func_name)) {
return 0;
}
ir_find_loops(ctx);
if ((dump & (IR_DUMP_AFTER_LOOP|IR_DUMP_AFTER_ALL))
&& !_save(ctx, save_flags, dump, IR_DUMP_AFTER_LOOP, dump_file, func_name)) {
return 0;
}
ir_gcm(ctx);
if ((dump & (IR_DUMP_AFTER_GCM|IR_DUMP_AFTER_ALL))
&& !_save(ctx, save_flags, dump, IR_DUMP_AFTER_GCM, dump_file, func_name)) {
return 0;
}
#ifdef IR_DEBUG
ir_check(ctx);
#endif
ir_schedule(ctx);
if ((dump & (IR_DUMP_AFTER_SCHEDULE|IR_DUMP_AFTER_ALL))
&& !_save(ctx, save_flags, dump, IR_DUMP_AFTER_SCHEDULE, dump_file, func_name)) {
return 0;
}
#ifdef IR_DEBUG
ir_check(ctx);
#endif
}
if (ctx->flags & IR_GEN_NATIVE) {
ir_match(ctx);
}
if (opt_level > 0) {
ir_assign_virtual_registers(ctx);
ir_compute_live_ranges(ctx);
if ((dump & (IR_DUMP_AFTER_LIVE_RANGES|IR_DUMP_AFTER_ALL))
&& !_save(ctx, save_flags, dump, IR_DUMP_AFTER_LIVE_RANGES, dump_file, func_name)) {
return 0;
}
ir_coalesce(ctx);
if ((dump & (IR_DUMP_AFTER_COALESCING|IR_DUMP_AFTER_ALL))
&& !_save(ctx, save_flags, dump, IR_DUMP_AFTER_COALESCING, dump_file, func_name)) {
return 0;
}
if (ctx->flags & IR_GEN_NATIVE) {
ir_reg_alloc(ctx);
if ((dump & (IR_DUMP_AFTER_REGALLOC|IR_DUMP_AFTER_ALL))
&& !_save(ctx, save_flags, dump, IR_DUMP_AFTER_REGALLOC, dump_file, func_name)) {
return 0;
}
}
ir_schedule_blocks(ctx);
} else if (ctx->flags & (IR_GEN_NATIVE|IR_GEN_CODE)) {
ir_assign_virtual_registers(ctx);
ir_compute_dessa_moves(ctx);
}
if ((dump & (IR_DUMP_FINAL|IR_DUMP_AFTER_ALL|IR_DUMP_CODEGEN))
&& !_save(ctx, save_flags, dump, IR_DUMP_FINAL, dump_file, func_name)) {
return 0;
}
#ifdef IR_DEBUG
ir_check(ctx);
#endif
return 1;
}
typedef struct _ir_sym {
void *addr;
void *thunk_addr;
} ir_sym;
typedef struct _ir_reloc {
void *addr;
ir_ref sym;
} ir_reloc;
typedef struct _ir_main_loader {
ir_loader loader;
int opt_level;
uint32_t mflags;
uint64_t debug_regset;
uint32_t save_flags;
uint32_t dump;
bool dump_asm;
bool dump_size;
bool run;
bool gdb;
size_t size;
void *main;
FILE *dump_file;
FILE *c_file;
FILE *llvm_file;
ir_strtab symtab;
ir_sym *sym;
ir_reloc *reloc;
ir_ref sym_count;
ir_ref reloc_count;
void *data_start;
size_t data_pos;
ir_code_buffer code_buffer;
} ir_main_loader;
static void ir_loader_free_symbols(ir_main_loader *l)
{
ir_strtab_free(&l->symtab);
if (l->sym) {
ir_mem_free(l->sym);
}
if (l->reloc) {
ir_mem_free(l->reloc);
}
}
static void ir_loader_add_reloc(ir_main_loader *l, const char *name, void *addr)
{
ir_reloc *r;
ir_ref val = ir_strtab_count(&l->symtab) + 1;
ir_ref sym = ir_strtab_lookup(&l->symtab, name, strlen(name), val);
if (sym == val) {
if (val >= l->sym_count) {
l->sym_count += 16;
l->sym = ir_mem_realloc(l->sym, sizeof(ir_sym) * l->sym_count);
}
l->sym[val].addr = NULL;
l->sym[val].thunk_addr = NULL;
}
l->reloc = ir_mem_realloc(l->reloc, sizeof(ir_reloc) * (l->reloc_count + 1));
r = &l->reloc[l->reloc_count];
r->addr = addr;
r->sym = sym;
l->reloc_count++;
}
static bool ir_loader_fix_relocs(ir_main_loader *l)
{
bool ret = 1;
ir_ref n = l->reloc_count;
if (n > 0) {
ir_reloc *r = l->reloc;
ir_sym *s;
ir_mem_unprotect(l->code_buffer.start, (char*)l->code_buffer.end - (char*)l->code_buffer.start);
for (; n > 0; r++, n--) {
IR_ASSERT(r->sym > 0 && r->sym < l->sym_count);
s = &l->sym[r->sym];
if (s->addr) {
*(void**)r->addr = (void*)((uintptr_t)s->addr + *(uintptr_t*)r->addr);
#if 0
uintptr_t addr;
memcpy(&addr, r->addr, sizeof(void*));
addr += (uintptr_t)s->addr;
memcpy(r->addr, &addr, sizeof(void*));
#endif
} else {
fprintf(stderr, "Undefined symbol: %s\n", ir_strtab_str(&l->symtab, r->sym - 1));
ret = 0;
break;
}
}
ir_mem_protect(l->code_buffer.start, (char*)l->code_buffer.end - (char*)l->code_buffer.start);
}
if (ret) {
/* Check for unresolved external symbols */
n = ir_strtab_count(&l->symtab);
if (n > 0) {
ir_sym *s = l->sym + 1;
ir_ref j;
for (j = 1; j < n; s++, j++) {
if (!s->addr && s->thunk_addr) {
fprintf(stderr, "Undefined symbol: %s\n", ir_strtab_str(&l->symtab, j - 1));
ret = 0;
}
}
}
}
return ret;
}
static bool ir_loader_add_sym(ir_loader *loader, const char *name, void *addr)
{
ir_main_loader *l = (ir_main_loader*)loader;
uint32_t len = (uint32_t)strlen(name);
ir_ref val = ir_strtab_count(&l->symtab) + 1;
ir_ref old_val = ir_strtab_lookup(&l->symtab, name, len, val);
if (old_val != val) {
if (addr && !l->sym[old_val].addr) {
/* Update forward declaration */
l->sym[old_val].addr = addr;
if (l->sym[old_val].thunk_addr) {
// TODO: Fix thunk or relocation ???
if (l->code_buffer.start) {
ir_mem_unprotect(l->code_buffer.start, (char*)l->code_buffer.end - (char*)l->code_buffer.start);
}
ir_fix_thunk(l->sym[old_val].thunk_addr, addr);
if (l->code_buffer.start) {
ir_mem_protect(l->code_buffer.start, (char*)l->code_buffer.end - (char*)l->code_buffer.start);
}
}
return 1;
}
return 0;
}
if (val >= l->sym_count) {
l->sym_count += 16;
l->sym = ir_mem_realloc(l->sym, sizeof(ir_sym) * l->sym_count);
}
l->sym[val].addr = addr;
l->sym[val].thunk_addr = NULL;
return 1;
}
static bool ir_loader_has_sym(ir_loader *loader, const char *name)
{
ir_main_loader *l = (ir_main_loader*)loader;
uint32_t len = (uint32_t)strlen(name);
ir_ref val = ir_strtab_find(&l->symtab, name, len);
return val != 0;
}
static void* ir_loader_resolve_sym_name(ir_loader *loader, const char *name, bool add_thunk)
{
ir_main_loader *l = (ir_main_loader*)loader;
uint32_t len = (uint32_t)strlen(name);
ir_ref val = ir_strtab_find(&l->symtab, name, len);
void *addr;
if (val) {
if (l->sym[val].addr) {
return l->sym[val].addr;
}
if (!l->sym[val].thunk_addr && add_thunk) {
/* Undefined declaration */
// TODO: Add thunk or relocation ???
size_t size;
l->sym[val].thunk_addr = ir_emit_thunk(&l->code_buffer, NULL, &size);
ir_disasm_add_symbol(name, (uint64_t)(uintptr_t)l->sym[val].thunk_addr, size);
}
return l->sym[val].thunk_addr;
}
addr = ir_resolve_sym_name(name);
if (addr) {
ir_loader_add_sym(loader, name, addr); /* cache */
}
return addr;
}
static bool ir_loader_external_sym_dcl(ir_loader *loader, const char *name, uint32_t flags)
{
ir_main_loader *l = (ir_main_loader*) loader;
if (ir_loader_has_sym(loader, name)) {
return 1;
}
if ((l->dump & IR_DUMP_SAVE) && (l->dump_file)) {
fprintf(l->dump_file, "extern %s %s;\n", (flags & IR_CONST) ? "const" : "var", name);
}
if (l->c_file) {
ir_emit_c_sym_decl(name, flags | IR_EXTERN, l->c_file);
}
if (l->llvm_file) {
ir_emit_llvm_sym_decl(name, flags | IR_EXTERN, l->llvm_file);
}
if (l->dump_asm || l->dump_size || l->run) {
void *addr = ir_loader_resolve_sym_name(loader, name, 0);
if (!addr) {
return 0;
}
if (l->dump_asm) {
ir_disasm_add_symbol(name, (uintptr_t)addr, sizeof(void*));
}
} else {
ir_loader_add_sym(loader, name, NULL);
}
return 1;
}
static void ir_dump_func_dcl(const char *name, uint32_t flags, ir_type ret_type, uint32_t params_count, const uint8_t *param_types, FILE *f)
{
if (flags & IR_EXTERN) {
fprintf(f, "extern ");
} else if (flags & IR_STATIC) {
fprintf(f, "static ");
}
fprintf(f, "func %s(", name);
if (params_count) {
const uint8_t *p = param_types;
fprintf(f, "%s", ir_type_cname[*p]);
p++;
while (--params_count) {
fprintf(f, ", %s", ir_type_cname[*p]);
p++;
}
if (flags & IR_VARARG_FUNC) {
fprintf(f, ", ...");
}
} else if (flags & IR_VARARG_FUNC) {
fprintf(f, "...");
}
fprintf(f, "): %s", ir_type_cname[ret_type]);
if (flags & IR_FASTCALL_FUNC) {
fprintf(f, " __fastcall");
} else if (flags & IR_BUILTIN_FUNC) {
fprintf(f, " __builtin");
}
fprintf(f, ";\n");
}
static bool ir_loader_external_func_dcl(ir_loader *loader, const char *name, uint32_t flags,
ir_type ret_type, uint32_t params_count, const uint8_t *param_types)
{
ir_main_loader *l = (ir_main_loader*) loader;
if (ir_loader_has_sym(loader, name)) {
return 1;
}
if ((l->dump & IR_DUMP_SAVE) && (l->dump_file)) {
ir_dump_func_dcl(name, flags | IR_EXTERN, ret_type, params_count, param_types, l->dump_file);
}
if (l->c_file) {
ir_emit_c_func_decl(name, flags | IR_EXTERN, ret_type, params_count, param_types, l->c_file);
}
if (l->llvm_file) {
ir_emit_llvm_func_decl(name, flags | IR_EXTERN, ret_type, params_count, param_types, l->llvm_file);
}
if (l->dump_asm || l->dump_size || l->run) {
void *addr = ir_loader_resolve_sym_name(loader, name, 0);
if (!addr) {
/* Unresolved external function */
ir_loader_add_sym(loader, name, NULL);
return 1;
}
if (l->dump_asm) {
ir_disasm_add_symbol(name, (uintptr_t)addr, sizeof(void*));
}
} else {
ir_loader_add_sym(loader, name, NULL);
}
return 1;
}
static bool ir_loader_forward_func_dcl(ir_loader *loader, const char *name, uint32_t flags,
ir_type ret_type, uint32_t params_count, const uint8_t *param_types)
{
ir_main_loader *l = (ir_main_loader*) loader;
if (ir_loader_has_sym(loader, name)) {
return 1;
}
if ((l->dump & IR_DUMP_SAVE) && (l->dump_file)) {
ir_dump_func_dcl(name, flags, ret_type, params_count, param_types, l->dump_file);
}
if (l->c_file) {
ir_emit_c_func_decl(name, flags, ret_type, params_count, param_types, l->c_file);
}
ir_loader_add_sym(loader, name, NULL);
return 1;
}
static bool ir_loader_sym_dcl(ir_loader *loader, const char *name, uint32_t flags, size_t size)
{
ir_main_loader *l = (ir_main_loader*) loader;
l->data_start = NULL;
l->data_pos = 0;
if ((l->dump & IR_DUMP_SAVE) && (l->dump_file)) {
if (flags & IR_STATIC) {
fprintf(l->dump_file, "static ");
}
fprintf(l->dump_file, "%s %s [%" PRIuPTR "]%s",
(flags & IR_CONST) ? "const" : "var", name, size,
(flags & IR_INITIALIZED) ? ((flags & IR_CONST_STRING) ? " = " : " = {\n") : ";\n");
}
if (l->c_file) {
ir_emit_c_sym_decl(name, flags, l->c_file);
}
if (l->llvm_file) {
ir_emit_llvm_sym_decl(name, flags, l->llvm_file);
}
if (l->dump_asm || l->dump_size || l->run) {
void *data;
if (flags & IR_CONST) {
data = l->code_buffer.pos;
/* Data Alignment */
if (size > 8) {
data = (void*)IR_ALIGNED_SIZE(((size_t)(data)), 16);
} else if (size == 8) {
data = (void*)IR_ALIGNED_SIZE(((size_t)(data)), 8);
} else if (size >= 4) {
data = (void*)IR_ALIGNED_SIZE(((size_t)(data)), 4);
} else if (size >= 2) {
data = (void*)IR_ALIGNED_SIZE(((size_t)(data)), 2);
}
if (size > (size_t)((char*)l->code_buffer.end - (char*)data)) {
return 0;
}
l->code_buffer.pos = (char*)data + size;
ir_mem_unprotect(l->code_buffer.start, (char*)l->code_buffer.end - (char*)l->code_buffer.start);
} else {
data = ir_mem_malloc(size);
}
if (!ir_loader_add_sym(loader, name, data)) {
ir_mem_free(data);
return 0;
}
memset(data, 0, size);
if (flags & IR_INITIALIZED) {
l->data_start = data;
}
if (l->dump_asm) {
ir_disasm_add_symbol(name, (uintptr_t)data, size);
}
#ifndef _WIN32
if (l->gdb) {
ir_gdb_register(name, data, size, 0, 0);
}
#endif
} else {
ir_loader_add_sym(loader, name, NULL);
}
return 1;
}
static bool ir_loader_sym_data(ir_loader *loader, ir_type type, uint32_t count, const void *data)
{
ir_main_loader *l = (ir_main_loader*) loader;
size_t size = ir_type_size[type];
if ((l->dump & IR_DUMP_SAVE) && (l->dump_file)) {
const void *p = data;
uint32_t i;
switch (size) {
case 1:
for (i = 0; i < count; i++) {
fprintf(l->dump_file, "\t%s 0x%02x,\n", ir_type_cname[type], (uint32_t)*(uint8_t*)p);
}
break;
case 2:
for (i = 0; i < count; i++) {
fprintf(l->dump_file, "\t%s 0x%04x,\n", ir_type_cname[type], (uint32_t)*(uint16_t*)p);
}
break;
case 4:
for (i = 0; i < count; i++) {
fprintf(l->dump_file, "\t%s 0x%08x,\n", ir_type_cname[type], *(uint32_t*)p);
}
break;
case 8:
for (i = 0; i < count; i++) {
fprintf(l->dump_file, "\t%s 0x%016" PRIx64 ",\n", ir_type_cname[type], *(uint64_t*)p);
}
break;
}
}
if (l->c_file) {
// TODO:
}
if (l->llvm_file) {
// TODO:
}
if (l->dump_asm || l->dump_size || l->run) {
IR_ASSERT(l->data_start);
if (count == 1) {
memcpy((char*)l->data_start + l->data_pos, data, size);
} else {
size_t pos = 0;
uint32_t i;
IR_ASSERT(count > 1);
for (i = 0; i < count; i++) {
memcpy((char*)l->data_start + l->data_pos + pos, data, size);
pos += size;
}
}
}
l->data_pos += size * count;
return 1;
}
static bool ir_loader_sym_data_str(ir_loader *loader, const char *str, size_t len)
{
ir_main_loader *l = (ir_main_loader*) loader;
if ((l->dump & IR_DUMP_SAVE) && (l->dump_file)) {
fprintf(l->dump_file, "\"");
ir_print_escaped_str(str, len, l->dump_file);
fprintf(l->dump_file, "\"");
}
if (l->c_file) {
// TODO:
}
if (l->llvm_file) {
// TODO:
}
if (l->dump_asm || l->dump_size || l->run) {
IR_ASSERT(l->data_start);
memcpy((char*)l->data_start + l->data_pos, str, len);
}
l->data_pos += len;
return 1;
}
static bool ir_loader_sym_data_pad(ir_loader *loader, size_t offset)
{
ir_main_loader *l = (ir_main_loader*) loader;
size_t i;
IR_ASSERT(offset >= l->data_pos);
offset -= l->data_pos;
if (offset) {
if ((l->dump & IR_DUMP_SAVE) && (l->dump_file)) {
for (i = 0; i < offset; i++) {
fprintf(l->dump_file, "\tuint8_t 0x00,\n");
}
}
if (l->c_file) {
// TODO:
}
if (l->llvm_file) {
// TODO:
}
if (l->dump_asm || l->dump_size || l->run) {
IR_ASSERT(l->data_start);
memset((char*)l->data_start + l->data_pos, 0, offset);
}
l->data_pos += offset;
}
return 1;
}
static bool ir_loader_sym_data_ref(ir_loader *loader, ir_op op, const char *ref, uintptr_t offset)
{
ir_main_loader *l = (ir_main_loader*) loader;
IR_ASSERT(op == IR_FUNC || op == IR_SYM);
if ((l->dump & IR_DUMP_SAVE) && (l->dump_file)) {
if (!offset) {
fprintf(l->dump_file, "\t%s %s(%s),\n", ir_type_cname[IR_ADDR], op == IR_FUNC ? "func" : "sym", ref);
} else {
fprintf(l->dump_file, "\t%s %s(%s)+0x%" PRIxPTR ",\n", ir_type_cname[IR_ADDR], op == IR_FUNC ? "func" : "sym", ref, offset);
}
}
if (l->c_file) {
// TODO:
}
if (l->llvm_file) {
// TODO:
}
if (l->dump_asm || l->dump_size || l->run) {
void *data = (char*)l->data_start + l->data_pos;
void *addr = ir_loader_resolve_sym_name(loader, ref, 0);
if (!addr) {
ir_loader_add_reloc(l, ref, data);
}
IR_ASSERT(l->data_start);
addr = (void*)((uintptr_t)(addr) + offset);
memcpy(data, &addr, sizeof(void*));
}
l->data_pos += sizeof(void*);
return 1;
}
static bool ir_loader_sym_data_end(ir_loader *loader, uint32_t flags)
{
ir_main_loader *l = (ir_main_loader*) loader;
if ((l->dump & IR_DUMP_SAVE) && (l->dump_file)) {
if (flags & IR_CONST_STRING) {
fprintf(l->dump_file, ";\n");
} else {
fprintf(l->dump_file, "};\n");
}
}
if (l->c_file) {
// TODO:
}
if (l->llvm_file) {
// TODO:
}
if (l->dump_asm || l->dump_size || l->run) {
if ((char*)l->data_start >= (char*)l->code_buffer.start
&& (char*)l->data_start < (char*)l->code_buffer.end) {
ir_mem_protect(l->code_buffer.start, (char*)l->code_buffer.end - (char*)l->code_buffer.start);
}
// TODO:
}
return 1;
}
static bool ir_loader_func_init(ir_loader *loader, ir_ctx *ctx, const char *name)
{
ir_main_loader *l = (ir_main_loader*) loader;
ir_init(ctx, loader->default_func_flags, 256, 1024);
ctx->mflags = l->mflags;
ctx->fixed_regset = ~l->debug_regset;
ctx->loader = loader;
return 1;
}
static bool ir_loader_func_process(ir_loader *loader, ir_ctx *ctx, const char *name)
{
ir_main_loader *l = (ir_main_loader*) loader;
// TODO: remove this
if (ctx->ret_type == (ir_type)-1) {
ir_ref ref = ctx->ir_base[1].op1;
while (ref) {
ir_insn *insn = &ctx->ir_base[ref];
if (insn->op == IR_RETURN) {
ctx->ret_type = insn->op2 ? ctx->ir_base[insn->op2].type : IR_VOID;
break;
} else if (insn->op == IR_UNREACHABLE && ctx->ir_base[insn->op1].op == IR_TAILCALL) {
ctx->ret_type = ctx->ir_base[insn->op1].type;
break;
}
ref = ctx->ir_base[ref].op3;
}
}
if (name == NULL) {
name = (l->run) ? "main" : "test";
} else if ((l->dump & IR_DUMP_SAVE) && l->dump_file) {
if (ctx->flags & IR_STATIC) {
fprintf(l->dump_file, "static ");
}
fprintf(l->dump_file, "func %s(", name);
if (ctx->ir_base[2].op == IR_PARAM) {
ir_insn *insn = &ctx->ir_base[2];
fprintf(l->dump_file, "%s", ir_type_cname[insn->type]);
insn++;
while (insn->op == IR_PARAM) {
fprintf(l->dump_file, ", %s", ir_type_cname[insn->type]);
insn++;;
}
if (ctx->flags & IR_VARARG_FUNC) {
fprintf(l->dump_file, ", ...");
}
} else if (ctx->flags & IR_VARARG_FUNC) {
fprintf(l->dump_file, "...");
}
fprintf(l->dump_file, "): %s", ir_type_cname[ctx->ret_type != (ir_type)-1 ? ctx->ret_type : IR_VOID]);
if (ctx->flags & IR_FASTCALL_FUNC) {
fprintf(l->dump_file, " __fastcall");
}
fprintf(l->dump_file, "\n");
}
if (!ir_compile_func(ctx, l->opt_level, l->save_flags, l->dump, l->dump_file, name)) {
return 0;
}
if (l->c_file) {
if (!ir_emit_c(ctx, name, l->c_file)) {
fprintf(stderr, "\nERROR: %d\n", ctx->status);
return 0;
}
}
if (l->llvm_file) {
if (!ir_emit_llvm(ctx, name, l->llvm_file)) {
fprintf(stderr, "\nERROR: %d\n", ctx->status);
return 0;
}
}
if (l->dump_asm || l->dump_size || l->run) {
size_t size;
void *entry;
if (l->code_buffer.start) {
ctx->code_buffer = &l->code_buffer;
ir_mem_unprotect(l->code_buffer.start, (char*)l->code_buffer.end - (char*)l->code_buffer.start);
}
entry = ir_emit_code(ctx, &size);
#ifndef _WIN32
if (l->gdb) {
if (!l->code_buffer.start) {
ir_mem_unprotect(entry, size);
}
ir_gdb_register(name, entry, size, sizeof(void*), 0);
if (!l->code_buffer.start) {
ir_mem_protect(entry, size);
}
}
#endif
if (l->code_buffer.start) {
ir_mem_protect(l->code_buffer.start, (char*)l->code_buffer.end - (char*)l->code_buffer.start);
}
if (entry) {
if (!l->code_buffer.start) {
l->size += size;
l->size = IR_ALIGNED_SIZE(l->size, 16);
}
if (!ir_loader_add_sym(loader, name, entry)) {
fprintf(stderr, "\nERROR: Symbol redefinition: %s\n", name);
return 0;
}
if (l->dump_asm) {
ir_ref i;
ir_insn *insn;
ir_disasm_add_symbol(name, (uintptr_t)entry, size);
for (i = IR_UNUSED + 1, insn = ctx->ir_base - i; i < ctx->consts_count; i++, insn--) {
if (insn->op == IR_FUNC) {
const char *name = ir_get_str(ctx, insn->val.name);
void *addr = ir_loader_resolve_sym_name(loader, name, 0);
IR_ASSERT(addr);
ir_disasm_add_symbol(name, (uintptr_t)addr, sizeof(void*));
//TODO: } else if (insn->op == IR_SYM) {
}
}
ir_disasm(name, entry, size, 0, ctx, stderr);
}
if (l->run) {
#ifndef _WIN32