-
Notifications
You must be signed in to change notification settings - Fork 4
/
example.c
1957 lines (1696 loc) · 44.1 KB
/
example.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
/*
* Example of how to write a compiler with sparse
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <assert.h>
#include "symbol.h"
#include "expression.h"
#include "linearize.h"
#include "flow.h"
#include "storage.h"
#include "target.h"
static const char *opcodes[] = {
[OP_BADOP] = "bad_op",
/* Fn entrypoint */
[OP_ENTRY] = "<entry-point>",
/* Terminator */
[OP_RET] = "ret",
[OP_BR] = "br",
[OP_CBR] = "cbr",
[OP_SWITCH] = "switch",
[OP_INVOKE] = "invoke",
[OP_COMPUTEDGOTO] = "jmp *",
[OP_UNWIND] = "unwind",
/* Binary */
[OP_ADD] = "add",
[OP_SUB] = "sub",
[OP_MULU] = "mulu",
[OP_MULS] = "muls",
[OP_DIVU] = "divu",
[OP_DIVS] = "divs",
[OP_MODU] = "modu",
[OP_MODS] = "mods",
[OP_SHL] = "shl",
[OP_LSR] = "lsr",
[OP_ASR] = "asr",
/* Logical */
[OP_AND] = "and",
[OP_OR] = "or",
[OP_XOR] = "xor",
[OP_AND_BOOL] = "and-bool",
[OP_OR_BOOL] = "or-bool",
/* Binary comparison */
[OP_SET_EQ] = "seteq",
[OP_SET_NE] = "setne",
[OP_SET_LE] = "setle",
[OP_SET_GE] = "setge",
[OP_SET_LT] = "setlt",
[OP_SET_GT] = "setgt",
[OP_SET_B] = "setb",
[OP_SET_A] = "seta",
[OP_SET_BE] = "setbe",
[OP_SET_AE] = "setae",
/* Uni */
[OP_NOT] = "not",
[OP_NEG] = "neg",
/* Special three-input */
[OP_SEL] = "select",
/* Memory */
[OP_MALLOC] = "malloc",
[OP_FREE] = "free",
[OP_ALLOCA] = "alloca",
[OP_LOAD] = "load",
[OP_STORE] = "store",
[OP_SETVAL] = "set",
[OP_GET_ELEMENT_PTR] = "getelem",
/* Other */
[OP_PHI] = "phi",
[OP_PHISOURCE] = "phisrc",
[OP_COPY] = "copy",
[OP_CAST] = "cast",
[OP_SCAST] = "scast",
[OP_FPCAST] = "fpcast",
[OP_PTRCAST] = "ptrcast",
[OP_CALL] = "call",
[OP_VANEXT] = "va_next",
[OP_VAARG] = "va_arg",
[OP_SLICE] = "slice",
[OP_SNOP] = "snop",
[OP_LNOP] = "lnop",
[OP_NOP] = "nop",
[OP_DEATHNOTE] = "dead",
[OP_ASM] = "asm",
/* Sparse tagging (line numbers, context, whatever) */
[OP_CONTEXT] = "context",
};
static int last_reg, stack_offset;
struct hardreg {
const char *name;
struct pseudo_list *contains;
unsigned busy:16,
dead:8,
used:1;
};
#define TAG_DEAD 1
#define TAG_DIRTY 2
/* Our "switch" generation is very very stupid. */
#define SWITCH_REG (1)
static void output_bb(struct basic_block *bb, unsigned long generation);
/*
* We only know about the caller-clobbered registers
* right now.
*/
static struct hardreg hardregs[] = {
{ .name = "%eax" },
{ .name = "%edx" },
{ .name = "%ecx" },
{ .name = "%ebx" },
{ .name = "%esi" },
{ .name = "%edi" },
{ .name = "%ebp" },
{ .name = "%esp" },
};
#define REGNO 6
#define REG_EBP 6
#define REG_ESP 7
struct bb_state {
struct position pos;
struct storage_hash_list *inputs;
struct storage_hash_list *outputs;
struct storage_hash_list *internal;
/* CC cache.. */
int cc_opcode, cc_dead;
pseudo_t cc_target;
};
enum optype {
OP_UNDEF,
OP_REG,
OP_VAL,
OP_MEM,
OP_ADDR,
};
struct operand {
enum optype type;
int size;
union {
struct hardreg *reg;
long long value;
struct /* OP_MEM and OP_ADDR */ {
unsigned int offset;
unsigned int scale;
struct symbol *sym;
struct hardreg *base;
struct hardreg *index;
};
};
};
static const char *show_op(struct bb_state *state, struct operand *op)
{
static char buf[256][4];
static int bufnr;
char *p, *ret;
int nr;
nr = (bufnr + 1) & 3;
bufnr = nr;
ret = p = buf[nr];
switch (op->type) {
case OP_UNDEF:
return "undef";
case OP_REG:
return op->reg->name;
case OP_VAL:
sprintf(p, "$%lld", op->value);
break;
case OP_MEM:
case OP_ADDR:
if (op->offset)
p += sprintf(p, "%d", op->offset);
if (op->sym)
p += sprintf(p, "%s%s",
op->offset ? "+" : "",
show_ident(op->sym->ident));
if (op->base || op->index) {
p += sprintf(p, "(%s%s%s",
op->base ? op->base->name : "",
(op->base && op->index) ? "," : "",
op->index ? op->index->name : "");
if (op->scale > 1)
p += sprintf(p, ",%d", op->scale);
*p++ = ')';
*p = '\0';
}
break;
}
return ret;
}
static struct storage_hash *find_storage_hash(pseudo_t pseudo, struct storage_hash_list *list)
{
struct storage_hash *entry;
FOR_EACH_PTR(list, entry) {
if (entry->pseudo == pseudo)
return entry;
} END_FOR_EACH_PTR(entry);
return NULL;
}
static struct storage_hash *find_or_create_hash(pseudo_t pseudo, struct storage_hash_list **listp)
{
struct storage_hash *entry;
entry = find_storage_hash(pseudo, *listp);
if (!entry) {
entry = alloc_storage_hash(alloc_storage());
entry->pseudo = pseudo;
add_ptr_list(listp, entry);
}
return entry;
}
/* Eventually we should just build it up in memory */
static void FORMAT_ATTR(2) output_line(struct bb_state *state, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
}
static void FORMAT_ATTR(2) output_label(struct bb_state *state, const char *fmt, ...)
{
static char buffer[512];
va_list args;
va_start(args, fmt);
vsnprintf(buffer, sizeof(buffer), fmt, args);
va_end(args);
output_line(state, "%s:\n", buffer);
}
static void FORMAT_ATTR(2) output_insn(struct bb_state *state, const char *fmt, ...)
{
static char buffer[512];
va_list args;
va_start(args, fmt);
vsnprintf(buffer, sizeof(buffer), fmt, args);
va_end(args);
output_line(state, "\t%s\n", buffer);
}
#define output_insn(state, fmt, arg...) \
output_insn(state, fmt "\t\t# %s" , ## arg , __FUNCTION__)
static void FORMAT_ATTR(2) output_comment(struct bb_state *state, const char *fmt, ...)
{
static char buffer[512];
va_list args;
if (!verbose)
return;
va_start(args, fmt);
vsnprintf(buffer, sizeof(buffer), fmt, args);
va_end(args);
output_line(state, "\t# %s\n", buffer);
}
static const char *show_memop(struct storage *storage)
{
static char buffer[1000];
if (!storage)
return "undef";
switch (storage->type) {
case REG_FRAME:
sprintf(buffer, "%d(FP)", storage->offset);
break;
case REG_STACK:
sprintf(buffer, "%d(SP)", storage->offset);
break;
case REG_REG:
return hardregs[storage->regno].name;
default:
return show_storage(storage);
}
return buffer;
}
static int alloc_stack_offset(int size)
{
int ret = stack_offset;
stack_offset = ret + size;
return ret;
}
static void alloc_stack(struct bb_state *state, struct storage *storage)
{
storage->type = REG_STACK;
storage->offset = alloc_stack_offset(4);
}
/*
* Can we re-generate the pseudo, so that we don't need to
* flush it to memory? We can regenerate:
* - immediates and symbol addresses
* - pseudos we got as input in non-registers
* - pseudos we've already saved off earlier..
*/
static int can_regenerate(struct bb_state *state, pseudo_t pseudo)
{
struct storage_hash *in;
switch (pseudo->type) {
case PSEUDO_VAL:
case PSEUDO_SYM:
return 1;
default:
in = find_storage_hash(pseudo, state->inputs);
if (in && in->storage->type != REG_REG)
return 1;
in = find_storage_hash(pseudo, state->internal);
if (in)
return 1;
}
return 0;
}
static void flush_one_pseudo(struct bb_state *state, struct hardreg *hardreg, pseudo_t pseudo)
{
struct storage_hash *out;
struct storage *storage;
if (can_regenerate(state, pseudo))
return;
output_comment(state, "flushing %s from %s", show_pseudo(pseudo), hardreg->name);
out = find_storage_hash(pseudo, state->internal);
if (!out) {
out = find_storage_hash(pseudo, state->outputs);
if (!out)
out = find_or_create_hash(pseudo, &state->internal);
}
storage = out->storage;
switch (storage->type) {
default:
/*
* Aieee - the next user wants it in a register, but we
* need to flush it to memory in between. Which means that
* we need to allocate an internal one, dammit..
*/
out = find_or_create_hash(pseudo, &state->internal);
storage = out->storage;
/* Fall through */
case REG_UDEF:
alloc_stack(state, storage);
/* Fall through */
case REG_STACK:
output_insn(state, "movl %s,%s", hardreg->name, show_memop(storage));
break;
}
}
/* Flush a hardreg out to the storage it has.. */
static void flush_reg(struct bb_state *state, struct hardreg *reg)
{
pseudo_t pseudo;
if (reg->busy)
output_comment(state, "reg %s flushed while busy is %d!", reg->name, reg->busy);
if (!reg->contains)
return;
reg->dead = 0;
reg->used = 1;
FOR_EACH_PTR(reg->contains, pseudo) {
if (CURRENT_TAG(pseudo) & TAG_DEAD)
continue;
if (!(CURRENT_TAG(pseudo) & TAG_DIRTY))
continue;
flush_one_pseudo(state, reg, pseudo);
} END_FOR_EACH_PTR(pseudo);
free_ptr_list(®->contains);
}
static struct storage_hash *find_pseudo_storage(struct bb_state *state, pseudo_t pseudo, struct hardreg *reg)
{
struct storage_hash *src;
src = find_storage_hash(pseudo, state->internal);
if (!src) {
src = find_storage_hash(pseudo, state->inputs);
if (!src) {
src = find_storage_hash(pseudo, state->outputs);
/* Undefined? Screw it! */
if (!src)
return NULL;
/*
* If we found output storage, it had better be local stack
* that we flushed to earlier..
*/
if (src->storage->type != REG_STACK)
return NULL;
}
}
/*
* Incoming pseudo with out any pre-set storage allocation?
* We can make up our own, and obviously prefer to get it
* in the register we already selected (if it hasn't been
* used yet).
*/
if (src->storage->type == REG_UDEF) {
if (reg && !reg->used) {
src->storage->type = REG_REG;
src->storage->regno = reg - hardregs;
return NULL;
}
alloc_stack(state, src->storage);
}
return src;
}
static void mark_reg_dead(struct bb_state *state, pseudo_t pseudo, struct hardreg *reg)
{
pseudo_t p;
FOR_EACH_PTR(reg->contains, p) {
if (p != pseudo)
continue;
if (CURRENT_TAG(p) & TAG_DEAD)
continue;
output_comment(state, "marking pseudo %s in reg %s dead", show_pseudo(pseudo), reg->name);
TAG_CURRENT(p, TAG_DEAD);
reg->dead++;
} END_FOR_EACH_PTR(p);
}
static void add_pseudo_reg(struct bb_state *state, pseudo_t pseudo, struct hardreg *reg)
{
output_comment(state, "added pseudo %s to reg %s", show_pseudo(pseudo), reg->name);
add_ptr_list_tag(®->contains, pseudo, TAG_DIRTY);
}
static struct hardreg *preferred_reg(struct bb_state *state, pseudo_t target)
{
struct storage_hash *dst;
dst = find_storage_hash(target, state->outputs);
if (dst) {
struct storage *storage = dst->storage;
if (storage->type == REG_REG)
return hardregs + storage->regno;
}
return NULL;
}
static struct hardreg *empty_reg(struct bb_state *state)
{
int i;
struct hardreg *reg = hardregs;
for (i = 0; i < REGNO; i++, reg++) {
if (!reg->contains)
return reg;
}
return NULL;
}
static struct hardreg *target_reg(struct bb_state *state, pseudo_t pseudo, pseudo_t target)
{
int i;
int unable_to_find_reg = 0;
struct hardreg *reg;
/* First, see if we have a preferred target register.. */
reg = preferred_reg(state, target);
if (reg && !reg->contains)
goto found;
reg = empty_reg(state);
if (reg)
goto found;
i = last_reg;
do {
i++;
if (i >= REGNO)
i = 0;
reg = hardregs + i;
if (!reg->busy) {
flush_reg(state, reg);
last_reg = i;
goto found;
}
} while (i != last_reg);
assert(unable_to_find_reg);
found:
add_pseudo_reg(state, pseudo, reg);
return reg;
}
static struct hardreg *find_in_reg(struct bb_state *state, pseudo_t pseudo)
{
int i;
struct hardreg *reg;
for (i = 0; i < REGNO; i++) {
pseudo_t p;
reg = hardregs + i;
FOR_EACH_PTR(reg->contains, p) {
if (p == pseudo) {
last_reg = i;
output_comment(state, "found pseudo %s in reg %s (busy=%d)", show_pseudo(pseudo), reg->name, reg->busy);
return reg;
}
} END_FOR_EACH_PTR(p);
}
return NULL;
}
static void flush_pseudo(struct bb_state *state, pseudo_t pseudo, struct storage *storage)
{
struct hardreg *reg = find_in_reg(state, pseudo);
if (reg)
flush_reg(state, reg);
}
static void flush_cc_cache_to_reg(struct bb_state *state, pseudo_t pseudo, struct hardreg *reg)
{
int opcode = state->cc_opcode;
state->cc_opcode = 0;
state->cc_target = NULL;
output_insn(state, "%s %s", opcodes[opcode], reg->name);
}
static void flush_cc_cache(struct bb_state *state)
{
pseudo_t pseudo = state->cc_target;
if (pseudo) {
struct hardreg *dst;
state->cc_target = NULL;
if (!state->cc_dead) {
dst = target_reg(state, pseudo, pseudo);
flush_cc_cache_to_reg(state, pseudo, dst);
}
}
}
static void add_cc_cache(struct bb_state *state, int opcode, pseudo_t pseudo)
{
assert(!state->cc_target);
state->cc_target = pseudo;
state->cc_opcode = opcode;
state->cc_dead = 0;
output_comment(state, "caching %s", opcodes[opcode]);
}
/* Fill a hardreg with the pseudo it has */
static struct hardreg *fill_reg(struct bb_state *state, struct hardreg *hardreg, pseudo_t pseudo)
{
struct storage_hash *src;
struct instruction *def;
if (state->cc_target == pseudo) {
flush_cc_cache_to_reg(state, pseudo, hardreg);
return hardreg;
}
switch (pseudo->type) {
case PSEUDO_VAL:
output_insn(state, "movl $%lld,%s", pseudo->value, hardreg->name);
break;
case PSEUDO_SYM:
src = find_pseudo_storage(state, pseudo, NULL);
/* Static thing? */
if (!src) {
output_insn(state, "movl $<%s>,%s", show_pseudo(pseudo), hardreg->name);
break;
}
switch (src->storage->type) {
case REG_REG:
/* Aiaiaiaiaii! Need to flush it to temporary memory */
src = find_or_create_hash(pseudo, &state->internal);
/* Fall through */
default:
alloc_stack(state, src->storage);
/* Fall through */
case REG_STACK:
case REG_FRAME:
flush_pseudo(state, pseudo, src->storage);
output_insn(state, "leal %s,%s", show_memop(src->storage), hardreg->name);
break;
}
break;
case PSEUDO_ARG:
case PSEUDO_REG:
def = pseudo->def;
if (def && def->opcode == OP_SETVAL) {
output_insn(state, "movl $<%s>,%s", show_pseudo(def->target), hardreg->name);
break;
}
src = find_pseudo_storage(state, pseudo, hardreg);
if (!src)
break;
if (src->flags & TAG_DEAD)
mark_reg_dead(state, pseudo, hardreg);
output_insn(state, "mov.%d %s,%s", 32, show_memop(src->storage), hardreg->name);
break;
default:
output_insn(state, "reload %s from %s", hardreg->name, show_pseudo(pseudo));
break;
}
return hardreg;
}
static struct hardreg *getreg(struct bb_state *state, pseudo_t pseudo, pseudo_t target)
{
struct hardreg *reg;
reg = find_in_reg(state, pseudo);
if (reg)
return reg;
reg = target_reg(state, pseudo, target);
return fill_reg(state, reg, pseudo);
}
static void move_reg(struct bb_state *state, struct hardreg *src, struct hardreg *dst)
{
output_insn(state, "movl %s,%s", src->name, dst->name);
}
static struct hardreg *copy_reg(struct bb_state *state, struct hardreg *src, pseudo_t target)
{
int i;
struct hardreg *reg;
/* If the container has been killed off, just re-use it */
if (!src->contains)
return src;
/* If "src" only has one user, and the contents are dead, we can re-use it */
if (src->busy == 1 && src->dead == 1)
return src;
reg = preferred_reg(state, target);
if (reg && !reg->contains) {
output_comment(state, "copying %s to preferred target %s", show_pseudo(target), reg->name);
move_reg(state, src, reg);
return reg;
}
for (i = 0; i < REGNO; i++) {
reg = hardregs + i;
if (!reg->contains) {
output_comment(state, "copying %s to %s", show_pseudo(target), reg->name);
output_insn(state, "movl %s,%s", src->name, reg->name);
return reg;
}
}
flush_reg(state, src);
return src;
}
static void put_operand(struct bb_state *state, struct operand *op)
{
switch (op->type) {
case OP_REG:
op->reg->busy--;
break;
case OP_ADDR:
case OP_MEM:
if (op->base)
op->base->busy--;
if (op->index)
op->index->busy--;
break;
default:
break;
}
}
static struct operand *alloc_op(void)
{
struct operand *op = malloc(sizeof(*op));
memset(op, 0, sizeof(*op));
return op;
}
static struct operand *get_register_operand(struct bb_state *state, pseudo_t pseudo, pseudo_t target)
{
struct operand *op = alloc_op();
op->type = OP_REG;
op->reg = getreg(state, pseudo, target);
op->reg->busy++;
return op;
}
static int get_sym_frame_offset(struct bb_state *state, pseudo_t pseudo)
{
int offset = pseudo->nr;
if (offset < 0) {
offset = alloc_stack_offset(4);
pseudo->nr = offset;
}
return offset;
}
static struct operand *get_generic_operand(struct bb_state *state, pseudo_t pseudo)
{
struct hardreg *reg;
struct storage *src;
struct storage_hash *hash;
struct operand *op = malloc(sizeof(*op));
memset(op, 0, sizeof(*op));
switch (pseudo->type) {
case PSEUDO_VAL:
op->type = OP_VAL;
op->value = pseudo->value;
break;
case PSEUDO_SYM: {
struct symbol *sym = pseudo->sym;
op->type = OP_ADDR;
if (sym->ctype.modifiers & MOD_NONLOCAL) {
op->sym = sym;
break;
}
op->base = hardregs + REG_EBP;
op->offset = get_sym_frame_offset(state, pseudo);
break;
}
default:
reg = find_in_reg(state, pseudo);
if (reg) {
op->type = OP_REG;
op->reg = reg;
reg->busy++;
break;
}
hash = find_pseudo_storage(state, pseudo, NULL);
if (!hash)
break;
src = hash->storage;
switch (src->type) {
case REG_REG:
op->type = OP_REG;
op->reg = hardregs + src->regno;
op->reg->busy++;
break;
case REG_FRAME:
op->type = OP_MEM;
op->offset = src->offset;
op->base = hardregs + REG_EBP;
break;
case REG_STACK:
op->type = OP_MEM;
op->offset = src->offset;
op->base = hardregs + REG_ESP;
break;
default:
break;
}
}
return op;
}
/* Callers should be made to use the proper "operand" formats */
static const char *generic(struct bb_state *state, pseudo_t pseudo)
{
struct hardreg *reg;
struct operand *op = get_generic_operand(state, pseudo);
static char buf[100];
const char *str;
switch (op->type) {
case OP_ADDR:
if (!op->offset && op->base && !op->sym)
return op->base->name;
if (op->sym && !op->base) {
int len = sprintf(buf, "$ %s", show_op(state, op));
if (op->offset)
sprintf(buf + len, " + %d", op->offset);
return buf;
}
str = show_op(state, op);
put_operand(state, op);
reg = target_reg(state, pseudo, NULL);
output_insn(state, "lea %s,%s", show_op(state, op), reg->name);
return reg->name;
default:
str = show_op(state, op);
}
put_operand(state, op);
return str;
}
static struct operand *get_address_operand(struct bb_state *state, struct instruction *memop)
{
struct hardreg *base;
struct operand *op = get_generic_operand(state, memop->src);
switch (op->type) {
case OP_ADDR:
op->offset += memop->offset;
break;
default:
put_operand(state, op);
base = getreg(state, memop->src, NULL);
op->type = OP_ADDR;
op->base = base;
base->busy++;
op->offset = memop->offset;
op->sym = NULL;
}
return op;
}
static const char *address(struct bb_state *state, struct instruction *memop)
{
struct operand *op = get_address_operand(state, memop);
const char *str = show_op(state, op);
put_operand(state, op);
return str;
}
static const char *reg_or_imm(struct bb_state *state, pseudo_t pseudo)
{
switch(pseudo->type) {
case PSEUDO_VAL:
return show_pseudo(pseudo);
default:
return getreg(state, pseudo, NULL)->name;
}
}
static void kill_dead_reg(struct hardreg *reg)
{
if (reg->dead) {
pseudo_t p;
FOR_EACH_PTR(reg->contains, p) {
if (CURRENT_TAG(p) & TAG_DEAD) {
DELETE_CURRENT_PTR(p);
reg->dead--;
}
} END_FOR_EACH_PTR(p);
PACK_PTR_LIST(®->contains);
assert(!reg->dead);
}
}
static struct hardreg *target_copy_reg(struct bb_state *state, struct hardreg *src, pseudo_t target)
{
kill_dead_reg(src);
return copy_reg(state, src, target);
}
static void do_binop(struct bb_state *state, struct instruction *insn, pseudo_t val1, pseudo_t val2)
{
const char *op = opcodes[insn->opcode];
struct operand *src = get_register_operand(state, val1, insn->target);
struct operand *src2 = get_generic_operand(state, val2);
struct hardreg *dst;
dst = target_copy_reg(state, src->reg, insn->target);
output_insn(state, "%s.%d %s,%s", op, insn->size, show_op(state, src2), dst->name);
put_operand(state, src);
put_operand(state, src2);
add_pseudo_reg(state, insn->target, dst);
}
static void generate_binop(struct bb_state *state, struct instruction *insn)
{
flush_cc_cache(state);
do_binop(state, insn, insn->src1, insn->src2);
}
static int is_dead_reg(struct bb_state *state, pseudo_t pseudo, struct hardreg *reg)
{
pseudo_t p;
FOR_EACH_PTR(reg->contains, p) {
if (p == pseudo)
return CURRENT_TAG(p) & TAG_DEAD;
} END_FOR_EACH_PTR(p);
return 0;
}
/*
* Commutative binops are much more flexible, since we can switch the
* sources around to satisfy the target register, or to avoid having
* to load one of them into a register..
*/
static void generate_commutative_binop(struct bb_state *state, struct instruction *insn)
{
pseudo_t src1, src2;
struct hardreg *reg1, *reg2;
flush_cc_cache(state);
src1 = insn->src1;
src2 = insn->src2;
reg2 = find_in_reg(state, src2);
if (!reg2)
goto dont_switch;
reg1 = find_in_reg(state, src1);
if (!reg1)
goto do_switch;
if (!is_dead_reg(state, src2, reg2))
goto dont_switch;
if (!is_dead_reg(state, src1, reg1))
goto do_switch;
/* Both are dead. Is one preferable? */
if (reg2 != preferred_reg(state, insn->target))
goto dont_switch;
do_switch:
src1 = src2;
src2 = insn->src1;
dont_switch:
do_binop(state, insn, src1, src2);
}
/*
* This marks a pseudo dead. It still stays on the hardreg list (the hardreg
* still has its value), but it's scheduled to be killed after the next
* "sequence point" when we call "kill_read_pseudos()"
*/
static void mark_pseudo_dead(struct bb_state *state, pseudo_t pseudo)
{
int i;
struct storage_hash *src;
if (state->cc_target == pseudo)
state->cc_dead = 1;
src = find_pseudo_storage(state, pseudo, NULL);
if (src)
src->flags |= TAG_DEAD;
for (i = 0; i < REGNO; i++)
mark_reg_dead(state, pseudo, hardregs + i);
}
static void kill_dead_pseudos(struct bb_state *state)
{
int i;
for (i = 0; i < REGNO; i++) {
kill_dead_reg(hardregs + i);
}
}
static void generate_store(struct instruction *insn, struct bb_state *state)
{
output_insn(state, "mov.%d %s,%s", insn->size, reg_or_imm(state, insn->target), address(state, insn));
}
static void generate_load(struct instruction *insn, struct bb_state *state)
{
const char *input = address(state, insn);
struct hardreg *dst;
kill_dead_pseudos(state);
dst = target_reg(state, insn->target, NULL);
output_insn(state, "mov.%d %s,%s", insn->size, input, dst->name);
}
static void kill_pseudo(struct bb_state *state, pseudo_t pseudo)