forked from lucvoo/sparse
-
Notifications
You must be signed in to change notification settings - Fork 1
/
compile-i386.c
2394 lines (1995 loc) · 54.8 KB
/
compile-i386.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
/*
* sparse/compile-i386.c
*
* Copyright (C) 2003 Transmeta Corp.
* 2003 Linus Torvalds
* Copyright 2003 Jeff Garzik
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* x86 backend
*
* TODO list:
* in general, any non-32bit SYM_BASETYPE is unlikely to work.
* complex initializers
* bitfields
* global struct/union variables
* addressing structures, and members of structures (as opposed to
* scalars) on the stack. Requires smarter stack frame allocation.
* labels / goto
* any function argument that isn't 32 bits (or promoted to such)
* inline asm
* floating point
*
*/
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>
#include "lib.h"
#include "allocate.h"
#include "token.h"
#include "parse.h"
#include "symbol.h"
#include "scope.h"
#include "expression.h"
#include "target.h"
#include "compile.h"
#include "bitmap.h"
struct textbuf {
unsigned int len; /* does NOT include terminating null */
char *text;
struct textbuf *next;
struct textbuf *prev;
};
struct loop_stack {
int continue_lbl;
int loop_bottom_lbl;
struct loop_stack *next;
};
struct atom;
struct storage;
DECLARE_PTR_LIST(str_list, struct atom);
DECLARE_PTR_LIST(atom_list, struct atom);
DECLARE_PTR_LIST(storage_list, struct storage);
struct function {
int stack_size;
int pseudo_nr;
struct storage_list *pseudo_list;
struct atom_list *atom_list;
struct str_list *str_list;
struct loop_stack *loop_stack;
struct symbol **argv;
unsigned int argc;
int ret_target;
};
enum storage_type {
STOR_PSEUDO, /* variable stored on the stack */
STOR_ARG, /* function argument */
STOR_SYM, /* a symbol we can directly ref in the asm */
STOR_REG, /* scratch register */
STOR_VALUE, /* integer constant */
STOR_LABEL, /* label / jump target */
STOR_LABELSYM, /* label generated from symbol's pointer value */
};
struct reg_info {
const char *name;
struct storage *contains;
const unsigned char aliases[12];
#define own_regno aliases[0]
};
struct storage {
enum storage_type type;
unsigned long flags;
/* STOR_REG */
struct reg_info *reg;
struct symbol *ctype;
union {
/* STOR_PSEUDO */
struct {
int pseudo;
int offset;
int size;
};
/* STOR_ARG */
struct {
int idx;
};
/* STOR_SYM */
struct {
struct symbol *sym;
};
/* STOR_VALUE */
struct {
long long value;
};
/* STOR_LABEL */
struct {
int label;
};
/* STOR_LABELSYM */
struct {
struct symbol *labelsym;
};
};
};
enum {
STOR_LABEL_VAL = (1 << 0),
STOR_WANTS_FREE = (1 << 1),
};
struct symbol_private {
struct storage *addr;
};
enum atom_type {
ATOM_TEXT,
ATOM_INSN,
ATOM_CSTR,
};
struct atom {
enum atom_type type;
union {
/* stuff for text */
struct {
char *text;
unsigned int text_len; /* w/o terminating null */
};
/* stuff for insns */
struct {
char insn[32];
char comment[40];
struct storage *op1;
struct storage *op2;
};
/* stuff for C strings */
struct {
struct string *string;
int label;
};
};
};
static struct function *current_func = NULL;
static struct textbuf *unit_post_text = NULL;
static const char *current_section;
static void emit_comment(const char * fmt, ...) FORMAT_ATTR(1);
static void emit_move(struct storage *src, struct storage *dest,
struct symbol *ctype, const char *comment);
static struct storage *x86_address_gen(struct expression *expr);
static struct storage *x86_symbol_expr(struct symbol *sym);
static void x86_symbol(struct symbol *sym);
static struct storage *x86_statement(struct statement *stmt);
static struct storage *x86_expression(struct expression *expr);
enum registers {
NOREG,
AL, DL, CL, BL, AH, DH, CH, BH, // 8-bit
AX, DX, CX, BX, SI, DI, BP, SP, // 16-bit
EAX, EDX, ECX, EBX, ESI, EDI, EBP, ESP, // 32-bit
EAX_EDX, ECX_EBX, ESI_EDI, // 64-bit
};
/* This works on regno's, reg_info's and hardreg_storage's */
#define byte_reg(reg) ((reg) - 16)
#define highbyte_reg(reg) ((reg)-12)
#define word_reg(reg) ((reg)-8)
#define REGINFO(nr, str, conflicts...) [nr] = { .name = str, .aliases = { nr , conflicts } }
static struct reg_info reg_info_table[] = {
REGINFO( AL, "%al", AX, EAX, EAX_EDX),
REGINFO( DL, "%dl", DX, EDX, EAX_EDX),
REGINFO( CL, "%cl", CX, ECX, ECX_EBX),
REGINFO( BL, "%bl", BX, EBX, ECX_EBX),
REGINFO( AH, "%ah", AX, EAX, EAX_EDX),
REGINFO( DH, "%dh", DX, EDX, EAX_EDX),
REGINFO( CH, "%ch", CX, ECX, ECX_EBX),
REGINFO( BH, "%bh", BX, EBX, ECX_EBX),
REGINFO( AX, "%ax", AL, AH, EAX, EAX_EDX),
REGINFO( DX, "%dx", DL, DH, EDX, EAX_EDX),
REGINFO( CX, "%cx", CL, CH, ECX, ECX_EBX),
REGINFO( BX, "%bx", BL, BH, EBX, ECX_EBX),
REGINFO( SI, "%si", ESI, ESI_EDI),
REGINFO( DI, "%di", EDI, ESI_EDI),
REGINFO( BP, "%bp", EBP),
REGINFO( SP, "%sp", ESP),
REGINFO(EAX, "%eax", AL, AH, AX, EAX_EDX),
REGINFO(EDX, "%edx", DL, DH, DX, EAX_EDX),
REGINFO(ECX, "%ecx", CL, CH, CX, ECX_EBX),
REGINFO(EBX, "%ebx", BL, BH, BX, ECX_EBX),
REGINFO(ESI, "%esi", SI, ESI_EDI),
REGINFO(EDI, "%edi", DI, ESI_EDI),
REGINFO(EBP, "%ebp", BP),
REGINFO(ESP, "%esp", SP),
REGINFO(EAX_EDX, "%eax:%edx", AL, AH, AX, EAX, DL, DH, DX, EDX),
REGINFO(ECX_EBX, "%ecx:%ebx", CL, CH, CX, ECX, BL, BH, BX, EBX),
REGINFO(ESI_EDI, "%esi:%edi", SI, ESI, DI, EDI),
};
#define REGSTORAGE(nr) [nr] = { .type = STOR_REG, .reg = reg_info_table + (nr) }
static struct storage hardreg_storage_table[] = {
REGSTORAGE(AL), REGSTORAGE(DL), REGSTORAGE(CL), REGSTORAGE(BL),
REGSTORAGE(AH), REGSTORAGE(DH), REGSTORAGE(CH), REGSTORAGE(BH),
REGSTORAGE(AX), REGSTORAGE(DX), REGSTORAGE(CX), REGSTORAGE(BX),
REGSTORAGE(SI), REGSTORAGE(DI), REGSTORAGE(BP), REGSTORAGE(SP),
REGSTORAGE(EAX), REGSTORAGE(EDX), REGSTORAGE(ECX), REGSTORAGE(EBX),
REGSTORAGE(ESI), REGSTORAGE(EDI), REGSTORAGE(EBP), REGSTORAGE(ESP),
REGSTORAGE(EAX_EDX), REGSTORAGE(ECX_EBX), REGSTORAGE(ESI_EDI),
};
#define REG_EAX (&hardreg_storage_table[EAX])
#define REG_ECX (&hardreg_storage_table[ECX])
#define REG_EDX (&hardreg_storage_table[EDX])
#define REG_ESP (&hardreg_storage_table[ESP])
#define REG_DL (&hardreg_storage_table[DL])
#define REG_DX (&hardreg_storage_table[DX])
#define REG_AL (&hardreg_storage_table[AL])
#define REG_AX (&hardreg_storage_table[AX])
static DECLARE_BITMAP(regs_in_use, 256);
static inline struct storage * reginfo_reg(struct reg_info *info)
{
return hardreg_storage_table + info->own_regno;
}
static struct storage * get_hardreg(struct storage *reg, int clear)
{
struct reg_info *info = reg->reg;
const unsigned char *aliases;
int regno;
aliases = info->aliases;
while ((regno = *aliases++) != NOREG) {
if (test_bit(regno, regs_in_use))
goto busy;
if (clear)
reg_info_table[regno].contains = NULL;
}
set_bit(info->own_regno, regs_in_use);
return reg;
busy:
fprintf(stderr, "register %s is busy\n", info->name);
if (regno + reg_info_table != info)
fprintf(stderr, " conflicts with %s\n", reg_info_table[regno].name);
exit(1);
}
static void put_reg(struct storage *reg)
{
struct reg_info *info = reg->reg;
int regno = info->own_regno;
if (test_and_clear_bit(regno, regs_in_use))
return;
fprintf(stderr, "freeing already free'd register %s\n", reg_info_table[regno].name);
}
struct regclass {
const char *name;
const unsigned char regs[30];
};
static struct regclass regclass_8 = { "8-bit", { AL, DL, CL, BL, AH, DH, CH, BH }};
static struct regclass regclass_16 = { "16-bit", { AX, DX, CX, BX, SI, DI, BP }};
static struct regclass regclass_32 = { "32-bit", { EAX, EDX, ECX, EBX, ESI, EDI, EBP }};
static struct regclass regclass_64 = { "64-bit", { EAX_EDX, ECX_EBX, ESI_EDI }};
static struct regclass regclass_32_8 = { "32-bit bytes", { EAX, EDX, ECX, EBX }};
static struct regclass *get_regclass_bits(int bits)
{
switch (bits) {
case 8: return ®class_8;
case 16: return ®class_16;
case 64: return ®class_64;
default: return ®class_32;
}
}
static struct regclass *get_regclass(struct expression *expr)
{
return get_regclass_bits(expr->ctype->bit_size);
}
static int register_busy(int regno)
{
if (!test_bit(regno, regs_in_use)) {
struct reg_info *info = reg_info_table + regno;
const unsigned char *regs = info->aliases+1;
while ((regno = *regs) != NOREG) {
regs++;
if (test_bit(regno, regs_in_use))
goto busy;
}
return 0;
}
busy:
return 1;
}
static struct storage *get_reg(struct regclass *class)
{
const unsigned char *regs = class->regs;
int regno;
while ((regno = *regs) != NOREG) {
regs++;
if (register_busy(regno))
continue;
return get_hardreg(hardreg_storage_table + regno, 1);
}
fprintf(stderr, "Ran out of %s registers\n", class->name);
exit(1);
}
static struct storage *get_reg_value(struct storage *value, struct regclass *class)
{
struct reg_info *info;
struct storage *reg;
/* Do we already have it somewhere */
info = value->reg;
if (info && info->contains == value) {
emit_comment("already have register %s", info->name);
return get_hardreg(hardreg_storage_table + info->own_regno, 0);
}
reg = get_reg(class);
emit_move(value, reg, value->ctype, "reload register");
info = reg->reg;
info->contains = value;
value->reg = info;
return reg;
}
static struct storage *temp_from_bits(unsigned int bit_size)
{
return get_reg(get_regclass_bits(bit_size));
}
static inline unsigned int pseudo_offset(struct storage *s)
{
if (s->type != STOR_PSEUDO)
return 123456; /* intentionally bogus value */
return s->offset;
}
static inline unsigned int arg_offset(struct storage *s)
{
if (s->type != STOR_ARG)
return 123456; /* intentionally bogus value */
/* FIXME: this is wrong wrong wrong */
return current_func->stack_size + ((1 + s->idx) * 4);
}
static const char *pretty_offset(int ofs)
{
static char esp_buf[64];
if (ofs)
sprintf(esp_buf, "%d(%%esp)", ofs);
else
strcpy(esp_buf, "(%esp)");
return esp_buf;
}
static void stor_sym_init(struct symbol *sym)
{
struct storage *stor;
struct symbol_private *priv;
priv = calloc(1, sizeof(*priv) + sizeof(*stor));
if (!priv)
die("OOM in stor_sym_init");
stor = (struct storage *) (priv + 1);
priv->addr = stor;
stor->type = STOR_SYM;
stor->sym = sym;
}
static const char *stor_op_name(struct storage *s)
{
static char name[32];
switch (s->type) {
case STOR_PSEUDO:
strcpy(name, pretty_offset((int) pseudo_offset(s)));
break;
case STOR_ARG:
strcpy(name, pretty_offset((int) arg_offset(s)));
break;
case STOR_SYM:
strcpy(name, show_ident(s->sym->ident));
break;
case STOR_REG:
strcpy(name, s->reg->name);
break;
case STOR_VALUE:
sprintf(name, "$%lld", s->value);
break;
case STOR_LABEL:
sprintf(name, "%s.L%d", s->flags & STOR_LABEL_VAL ? "$" : "",
s->label);
break;
case STOR_LABELSYM:
sprintf(name, "%s.LS%p", s->flags & STOR_LABEL_VAL ? "$" : "",
s->labelsym);
break;
}
return name;
}
static struct atom *new_atom(enum atom_type type)
{
struct atom *atom;
atom = calloc(1, sizeof(*atom)); /* TODO: chunked alloc */
if (!atom)
die("nuclear OOM");
atom->type = type;
return atom;
}
static inline void push_cstring(struct function *f, struct string *str,
int label)
{
struct atom *atom;
atom = new_atom(ATOM_CSTR);
atom->string = str;
atom->label = label;
add_ptr_list(&f->str_list, atom); /* note: _not_ atom_list */
}
static inline void push_atom(struct function *f, struct atom *atom)
{
add_ptr_list(&f->atom_list, atom);
}
static void push_text_atom(struct function *f, const char *text)
{
struct atom *atom = new_atom(ATOM_TEXT);
atom->text = strdup(text);
atom->text_len = strlen(text);
push_atom(f, atom);
}
static struct storage *new_storage(enum storage_type type)
{
struct storage *stor;
stor = calloc(1, sizeof(*stor));
if (!stor)
die("OOM in new_storage");
stor->type = type;
return stor;
}
static struct storage *stack_alloc(int n_bytes)
{
struct function *f = current_func;
struct storage *stor;
assert(f != NULL);
stor = new_storage(STOR_PSEUDO);
stor->type = STOR_PSEUDO;
stor->pseudo = f->pseudo_nr;
stor->offset = f->stack_size; /* FIXME: stack req. natural align */
stor->size = n_bytes;
f->stack_size += n_bytes;
f->pseudo_nr++;
add_ptr_list(&f->pseudo_list, stor);
return stor;
}
static struct storage *new_labelsym(struct symbol *sym)
{
struct storage *stor;
stor = new_storage(STOR_LABELSYM);
if (stor) {
stor->flags |= STOR_WANTS_FREE;
stor->labelsym = sym;
}
return stor;
}
static struct storage *new_val(long long value)
{
struct storage *stor;
stor = new_storage(STOR_VALUE);
if (stor) {
stor->flags |= STOR_WANTS_FREE;
stor->value = value;
}
return stor;
}
static int new_label(void)
{
static int label = 0;
return ++label;
}
static void textbuf_push(struct textbuf **buf_p, const char *text)
{
struct textbuf *tmp, *list = *buf_p;
unsigned int text_len = strlen(text);
unsigned int alloc_len = text_len + 1 + sizeof(*list);
tmp = calloc(1, alloc_len);
if (!tmp)
die("OOM on textbuf alloc");
tmp->text = ((void *) tmp) + sizeof(*tmp);
memcpy(tmp->text, text, text_len + 1);
tmp->len = text_len;
/* add to end of list */
if (!list) {
list = tmp;
tmp->prev = tmp;
} else {
tmp->prev = list->prev;
tmp->prev->next = tmp;
list->prev = tmp;
}
tmp->next = list;
*buf_p = list;
}
static void textbuf_emit(struct textbuf **buf_p)
{
struct textbuf *tmp, *list = *buf_p;
while (list) {
tmp = list;
if (tmp->next == tmp)
list = NULL;
else {
tmp->prev->next = tmp->next;
tmp->next->prev = tmp->prev;
list = tmp->next;
}
fputs(tmp->text, stdout);
free(tmp);
}
*buf_p = list;
}
static void insn(const char *insn, struct storage *op1, struct storage *op2,
const char *comment_in)
{
struct function *f = current_func;
struct atom *atom = new_atom(ATOM_INSN);
assert(insn != NULL);
strcpy(atom->insn, insn);
if (comment_in && (*comment_in))
strncpy(atom->comment, comment_in,
sizeof(atom->comment) - 1);
atom->op1 = op1;
atom->op2 = op2;
push_atom(f, atom);
}
static void emit_comment(const char *fmt, ...)
{
struct function *f = current_func;
static char tmpbuf[100] = "\t# ";
va_list args;
int i;
va_start(args, fmt);
i = vsnprintf(tmpbuf+3, sizeof(tmpbuf)-4, fmt, args);
va_end(args);
tmpbuf[i+3] = '\n';
tmpbuf[i+4] = '\0';
push_text_atom(f, tmpbuf);
}
static void emit_label (int label, const char *comment)
{
struct function *f = current_func;
char s[64];
if (!comment)
sprintf(s, ".L%d:\n", label);
else
sprintf(s, ".L%d:\t\t\t\t\t# %s\n", label, comment);
push_text_atom(f, s);
}
static void emit_labelsym (struct symbol *sym, const char *comment)
{
struct function *f = current_func;
char s[64];
if (!comment)
sprintf(s, ".LS%p:\n", sym);
else
sprintf(s, ".LS%p:\t\t\t\t# %s\n", sym, comment);
push_text_atom(f, s);
}
void emit_unit_begin(const char *basename)
{
printf("\t.file\t\"%s\"\n", basename);
}
void emit_unit_end(void)
{
textbuf_emit(&unit_post_text);
printf("\t.ident\t\"sparse silly x86 backend (version %s)\"\n", sparse_version);
}
/* conditionally switch sections */
static void emit_section(const char *s)
{
if (s == current_section)
return;
if (current_section && (!strcmp(s, current_section)))
return;
printf("\t%s\n", s);
current_section = s;
}
static void emit_insn_atom(struct function *f, struct atom *atom)
{
char s[128];
char comment[64];
struct storage *op1 = atom->op1;
struct storage *op2 = atom->op2;
if (atom->comment[0])
sprintf(comment, "\t\t# %s", atom->comment);
else
comment[0] = 0;
if (atom->op2) {
char tmp[16];
strcpy(tmp, stor_op_name(op1));
sprintf(s, "\t%s\t%s, %s%s\n",
atom->insn, tmp, stor_op_name(op2), comment);
} else if (atom->op1)
sprintf(s, "\t%s\t%s%s%s\n",
atom->insn, stor_op_name(op1),
comment[0] ? "\t" : "", comment);
else
sprintf(s, "\t%s\t%s%s\n",
atom->insn,
comment[0] ? "\t\t" : "", comment);
if (write(STDOUT_FILENO, s, strlen(s)) < 0)
die("can't write to stdout");
}
static void emit_atom_list(struct function *f)
{
struct atom *atom;
FOR_EACH_PTR(f->atom_list, atom) {
switch (atom->type) {
case ATOM_TEXT: {
if (write(STDOUT_FILENO, atom->text, atom->text_len) < 0)
die("can't write to stdout");
break;
}
case ATOM_INSN:
emit_insn_atom(f, atom);
break;
case ATOM_CSTR:
assert(0);
break;
}
} END_FOR_EACH_PTR(atom);
}
static void emit_string_list(struct function *f)
{
struct atom *atom;
emit_section(".section\t.rodata");
FOR_EACH_PTR(f->str_list, atom) {
/* FIXME: escape " in string */
printf(".L%d:\n", atom->label);
printf("\t.string\t%s\n", show_string(atom->string));
free(atom);
} END_FOR_EACH_PTR(atom);
}
static void func_cleanup(struct function *f)
{
struct storage *stor;
struct atom *atom;
FOR_EACH_PTR(f->atom_list, atom) {
if ((atom->type == ATOM_TEXT) && (atom->text))
free(atom->text);
if (atom->op1 && (atom->op1->flags & STOR_WANTS_FREE))
free(atom->op1);
if (atom->op2 && (atom->op2->flags & STOR_WANTS_FREE))
free(atom->op2);
free(atom);
} END_FOR_EACH_PTR(atom);
FOR_EACH_PTR(f->pseudo_list, stor) {
free(stor);
} END_FOR_EACH_PTR(stor);
free_ptr_list(&f->pseudo_list);
free(f);
}
/* function prologue */
static void emit_func_pre(struct symbol *sym)
{
struct function *f;
struct symbol *arg;
unsigned int i, argc = 0, alloc_len;
unsigned char *mem;
struct symbol_private *privbase;
struct storage *storage_base;
struct symbol *base_type = sym->ctype.base_type;
FOR_EACH_PTR(base_type->arguments, arg) {
argc++;
} END_FOR_EACH_PTR(arg);
alloc_len =
sizeof(*f) +
(argc * sizeof(struct symbol *)) +
(argc * sizeof(struct symbol_private)) +
(argc * sizeof(struct storage));
mem = calloc(1, alloc_len);
if (!mem)
die("OOM on func info");
f = (struct function *) mem;
mem += sizeof(*f);
f->argv = (struct symbol **) mem;
mem += (argc * sizeof(struct symbol *));
privbase = (struct symbol_private *) mem;
mem += (argc * sizeof(struct symbol_private));
storage_base = (struct storage *) mem;
f->argc = argc;
f->ret_target = new_label();
i = 0;
FOR_EACH_PTR(base_type->arguments, arg) {
f->argv[i] = arg;
arg->aux = &privbase[i];
storage_base[i].type = STOR_ARG;
storage_base[i].idx = i;
privbase[i].addr = &storage_base[i];
i++;
} END_FOR_EACH_PTR(arg);
assert(current_func == NULL);
current_func = f;
}
/* function epilogue */
static void emit_func_post(struct symbol *sym)
{
const char *name = show_ident(sym->ident);
struct function *f = current_func;
int stack_size = f->stack_size;
if (f->str_list)
emit_string_list(f);
/* function prologue */
emit_section(".text");
if ((sym->ctype.modifiers & MOD_STATIC) == 0)
printf(".globl %s\n", name);
printf("\t.type\t%s, @function\n", name);
printf("%s:\n", name);
if (stack_size) {
char pseudo_const[16];
sprintf(pseudo_const, "$%d", stack_size);
printf("\tsubl\t%s, %%esp\n", pseudo_const);
}
/* function epilogue */
/* jump target for 'return' statements */
emit_label(f->ret_target, NULL);
if (stack_size) {
struct storage *val;
val = new_storage(STOR_VALUE);
val->value = (long long) (stack_size);
val->flags = STOR_WANTS_FREE;
insn("addl", val, REG_ESP, NULL);
}
insn("ret", NULL, NULL, NULL);
/* output everything to stdout */
fflush(stdout); /* paranoia; needed? */
emit_atom_list(f);
/* function footer */
name = show_ident(sym->ident);
printf("\t.size\t%s, .-%s\n", name, name);
func_cleanup(f);
current_func = NULL;
}
/* emit object (a.k.a. variable, a.k.a. data) prologue */
static void emit_object_pre(const char *name, unsigned long modifiers,
unsigned long alignment, unsigned int byte_size)
{
if ((modifiers & MOD_STATIC) == 0)
printf(".globl %s\n", name);
emit_section(".data");
if (alignment)
printf("\t.align %lu\n", alignment);
printf("\t.type\t%s, @object\n", name);
printf("\t.size\t%s, %d\n", name, byte_size);
printf("%s:\n", name);
}
/* emit value (only) for an initializer scalar */
static void emit_scalar(struct expression *expr, unsigned int bit_size)
{
const char *type;
long long ll;
assert(expr->type == EXPR_VALUE);
if (expr->value == 0ULL) {
printf("\t.zero\t%d\n", bit_size / 8);
return;
}
ll = (long long) expr->value;
switch (bit_size) {
case 8: type = "byte"; ll = (char) ll; break;
case 16: type = "value"; ll = (short) ll; break;
case 32: type = "long"; ll = (int) ll; break;
case 64: type = "quad"; break;
default: type = NULL; break;
}
assert(type != NULL);
printf("\t.%s\t%lld\n", type, ll);
}
static void emit_global_noinit(const char *name, unsigned long modifiers,
unsigned long alignment, unsigned int byte_size)
{
char s[64];
if (modifiers & MOD_STATIC) {
sprintf(s, "\t.local\t%s\n", name);
textbuf_push(&unit_post_text, s);
}
if (alignment)
sprintf(s, "\t.comm\t%s,%d,%lu\n", name, byte_size, alignment);
else
sprintf(s, "\t.comm\t%s,%d\n", name, byte_size);
textbuf_push(&unit_post_text, s);
}
static int ea_current, ea_last;
static void emit_initializer(struct symbol *sym,
struct expression *expr)
{
int distance = ea_current - ea_last - 1;
if (distance > 0)
printf("\t.zero\t%d\n", (sym->bit_size / 8) * distance);
if (expr->type == EXPR_VALUE) {
struct symbol *base_type = sym->ctype.base_type;
assert(base_type != NULL);
emit_scalar(expr, sym->bit_size / get_expression_value(base_type->array_size));
return;
}
if (expr->type != EXPR_INITIALIZER)
return;
assert(0); /* FIXME */
}
static int sort_array_cmp(const struct expression *a,
const struct expression *b)
{
int a_ofs = 0, b_ofs = 0;
if (a->type == EXPR_POS)
a_ofs = (int) a->init_offset;
if (b->type == EXPR_POS)
b_ofs = (int) b->init_offset;
return a_ofs - b_ofs;
}
/* move to front-end? */
static void sort_array(struct expression *expr)
{
struct expression *entry, **list;
unsigned int elem, sorted, i;
elem = expression_list_size(expr->expr_list);
if (!elem)