-
Notifications
You must be signed in to change notification settings - Fork 13
/
backend-8086.c
1537 lines (1459 loc) · 32 KB
/
backend-8086.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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include "compiler.h"
#include "backend.h"
/*
* TODO: assembler sort out and add syntax annotations for word
* ops mem,const where size is not implied
* - Optimize various mem,const cases we can use
* - Track values and pointers for writeback supression
* - Volatile/elimination of nstore etc
* - Conditions
* - CCONLY handling
* - XOR for constant 0 set
* - Register variables using SI and DI and maybe CX as we could
* push it for the few cases we use it
* - Shift eq ops (>>= etc)
* - 32bit math helpers
* - *= /= %=
* - Boolean ops
*
* Register usage is pretty much determined by the 8086 design
*
* DX:AX 32bit working register (DX is also sometimes used as
* scratch (choice forced by CBW CWD MUL DIV)
* BX Scratch pointer/indexing register (forced by addr modes)
* CX Loop counts (could push/pop and also get an integer
* register variable out of it) (forced by LOOP/REP)
* SI/DI Register pointer variables (used for rep ops but not usually
* by a compiler inline)
* BP Frame pointer (forced by memory access ops. We know sp/bp
* difference but there is no offset[sp] mode
*
*/
#define BYTE(x) (((unsigned)(x)) & 0xFF)
#define WORD(x) (((unsigned)(x)) & 0xFFFF)
/*
* State for the current function
*/
static unsigned frame_len; /* Number of bytes of stack frame */
static unsigned argbase = 4; /* Will vary once we add reg vars */
static unsigned unreachable;
static unsigned labelid;
static unsigned get_size(unsigned t)
{
if (PTR(t))
return 2;
if (t == CSHORT || t == USHORT)
return 2;
if (t == CCHAR || t == UCHAR)
return 1;
if (t == CLONG || t == ULONG || t == FLOAT)
return 4;
if (t == CLONGLONG || t == ULONGLONG || t == DOUBLE)
return 8;
if (t == VOID)
return 0;
fprintf(stderr, "type %x\n", t);
error("gs");
return 0;
}
static unsigned get_stack_size(unsigned t)
{
unsigned n = get_size(t);
if (n == 1)
return 2;
return n;
}
static unsigned get_label(void)
{
return ++labelid;
}
#define T_CALLNAME (T_USER+0) /* Function call by name */
#define T_NREF (T_USER+1) /* Load of C global/static */
#define T_LBREF (T_USER+2) /* Ditto for labelled strings or local static */
#define T_LREF (T_USER+3) /* Ditto for local */
#define T_NSTORE (T_USER+4) /* Store to a C global/static */
#define T_LBSTORE (T_USER+5)
#define T_LSTORE (T_USER+6)
/* Chance to rewrite the tree from the top rather than none by node
upwards. We will use this for 8bit ops at some point and for cconly
propagation */
struct node *gen_rewrite(struct node *n)
{
return n;
}
static void squash_node(struct node *n, struct node *o)
{
n->value = o->value;
n->val2 = o->val2;
n->snum = o->snum;
free_node(o);
}
static void squash_left(struct node *n, unsigned op)
{
struct node *l = n->left;
n->op = op;
squash_node(n, l);
n->left = NULL;
}
static void squash_right(struct node *n, unsigned op)
{
struct node *r = n->right;
n->op = op;
squash_node(n, r);
n->right = NULL;
}
/*
* Things we can access without messing with DX:AX. On the
* 8086 we have a pretty good toolset.
*/
static unsigned is_simple(struct node *n)
{
unsigned op = n->op;
/* We want constants on the right above all other items */
if (op == T_CONSTANT)
return 10;
if (op == T_LOCAL || op == T_ARGUMENT || op == T_NAME || op == T_LABEL)
return 9;
if (op == T_LREF || op == T_LBREF || op == T_NREF)
return 8;
return 0;
}
/*
* Our chance to do tree rewriting. We don't do much
* at this point, but we do rewrite name references and function calls
* to make them easier to process.
*
* TODO: lots of other rewrites will help from the other targets
* eg EQPLUS. Also we want to rewrite *x++ and *x-- type ops on a local
* that do something to the result to a single op so we can generate
* mov bx,n[bp],add 1,n[bp] mov [bx],0 etc
*/
struct node *gen_rewrite_node(struct node *n)
{
struct node *l = n->left;
struct node *r = n->right;
unsigned op = n->op;
unsigned nt = n->type;
if (op == T_DEREF) {
if (r->op == T_LOCAL || r->op == T_ARGUMENT) {
if (r->op == T_ARGUMENT)
r->value += argbase + frame_len;
squash_right(n, T_LREF);
return n;
}
#if 0
if (r->op == T_REG) {
squash_right(n, T_RREF);
return n;
}
#endif
if (r->op == T_NAME) {
squash_right(n, T_NREF);
return n;
}
if (r->op == T_LABEL) {
squash_right(n, T_LBREF);
return n;
}
} else if (op == T_EQ) {
if (l->op == T_NAME) {
squash_left(n, T_NSTORE);
return n;
}
if (l->op == T_LABEL) {
squash_left(n, T_LBSTORE);
return n;
}
if (l->op == T_LOCAL || l->op == T_ARGUMENT) {
if (l->op == T_ARGUMENT)
l->value += argbase + frame_len;
squash_left(n, T_LSTORE);
return n;
}
#if 0
if (l->op == T_REG) {
squash_left(n, T_RSTORE);
return n;
}
#endif
/* Eliminate casts for sign, pointer conversion or same */
} else if (op == T_CAST) {
if (nt == r->type || (nt ^ r->type) == UNSIGNED ||
(PTR(nt) && PTR(r->type))) {
free_node(n);
return r;
}
/* Rewrite function call of a name into a new node so we can
turn it easily into call xyz */
} else if (op == T_FUNCCALL && r->op == T_NAME && PTR(r->type) == 1) {
n->op = T_CALLNAME;
n->snum = r->snum;
n->value = r->value;
free_node(r);
n->right = NULL;
}
/* Commutive operations. We can swap the sides over on these */
/* We also need to rewrite LT GT LTEQ GTEQ at some point so constants
are on the right and rewrite the op */
if (op == T_AND || op == T_OR || op == T_HAT || op == T_STAR || op == T_PLUS) {
/* printf(";left %d right %d\n", is_simple(n->left), is_simple(n->right)); */
if (is_simple(n->left) > is_simple(n->right)) {
n->right = l;
n->left = r;
}
}
return n;
}
/* Export the C symbol */
void gen_export(const char *name)
{
printf(" .export _%s\n", name);
}
void gen_segment(unsigned s)
{
switch(s) {
case A_CODE:
printf("\t.text\n");
break;
case A_DATA:
printf("\t.data\n");
break;
case A_LITERAL:
printf("\t.literal\n");
break;
case A_BSS:
printf("\t.bss\n");
break;
default:
error("gseg");
}
}
void gen_prologue(const char *name)
{
unreachable = 0;
printf("_%s:\n", name);
}
/* Generate the stack frame */
void gen_frame(unsigned size, unsigned aframe)
{
frame_len = size;
/* TODO: push si/di if reg args */
if (cpu >= 186)
printf("\tenter %d,0\n", size);
else {
printf("\tpush bp\n");
printf("\tmov bp,sp\n");
if (size)
printf("\tsub sp,%d\n", size);
}
}
void gen_epilogue(unsigned size, unsigned argsize)
{
/* TODO: pop si/di if reg args */
if (!unreachable) {
if (cpu >= 186) {
printf("\tleave\n");
} else {
printf("\tmov sp,bp\n");
printf("\tpop bp\n");
}
printf("\tret\n");
}
unreachable = 1;
}
void gen_label(const char *tail, unsigned n)
{
unreachable = 0;
printf("L%d%s:\n", n, tail);
}
unsigned gen_exit(const char *tail, unsigned n)
{
printf("\tjmp L%d%s\n", n, tail);
unreachable = 1;
return 0;
}
void gen_jump(const char *tail, unsigned n)
{
printf("\tjmp L%d%s\n", n, tail);
unreachable = 1;
}
void gen_jfalse(const char *tail, unsigned n)
{
printf("\tjz L%d%s\n", n, tail);
}
void gen_jtrue(const char *tail, unsigned n)
{
printf("\tjnz L%d%s\n", n, tail);
}
void gen_switch(unsigned n, unsigned type)
{
gen_helpcall(NULL);
printf("switch");
helper_type(type, 0);
printf("\n\t.word Sw%d\n", n);
unreachable = 1;
}
void gen_switchdata(unsigned n, unsigned size)
{
printf("Sw%d:\n", n);
printf("\t.word %d\n", size);
}
void gen_case_label(unsigned tag, unsigned entry)
{
unreachable = 0;
printf("Sw%d_%d:\n", tag, entry);
}
void gen_case_data(unsigned tag, unsigned entry)
{
printf("\t.word Sw%d_%d\n", tag, entry);
}
void gen_helpcall(struct node *n)
{
printf("\tcall __");
}
void gen_helptail(struct node *n)
{
}
void gen_helpclean(struct node *n)
{
}
void gen_data_label(const char *name, unsigned align)
{
printf("_%s:\n", name);
}
void gen_space(unsigned value)
{
printf("\t.ds %d\n", value);
}
void gen_text_data(struct node *n)
{
printf("\t.word T%d\n", n->val2);
}
void gen_literal(unsigned n)
{
if (n)
printf("T%d:\n", n);
}
void gen_name(struct node *n)
{
printf("\t.word _%s+%d\n", namestr(n->snum), WORD(n->value));
}
void gen_value(unsigned type, unsigned long value)
{
if (PTR(type)) {
printf("\t.word %u\n", (unsigned) value);
return;
}
switch (type) {
case CCHAR:
case UCHAR:
printf("\t.byte %u\n", (unsigned) value & 0xFF);
break;
case CSHORT:
case USHORT:
printf("\t.word %d\n", (unsigned) value & 0xFFFF);
break;
case CLONG:
case ULONG:
case FLOAT:
/* We are little endian */
printf("\t.word %d\n", (unsigned) (value & 0xFFFF));
printf("\t.word %d\n", (unsigned) ((value >> 16) & 0xFFFF));
break;
default:
error("unsuported type");
}
}
void gen_start(void)
{
}
void gen_end(void)
{
}
void gen_tree(struct node *n)
{
codegen_lr(n);
printf(";\n");
}
unsigned gen_push(struct node *n)
{
unsigned s = get_stack_size(n->type);
if (s == 4)
printf("\npush dx\n");
printf("\tpush ax\n");
return 1;
}
/* Perform an operation between (DX:)AX and the right hand side directly
if possible. */
unsigned op_direct(struct node *r, unsigned size, const char *op, const char *op2)
{
const char *lr = "ax";
const char *name;
if (size == 1)
lr = "al";
unsigned v = r->value;
/* We can't shortcut floats like this */
if (r->type == FLOAT)
return 0;
switch(r->op) {
case T_CONSTANT:
if (size == 1) {
printf("\t%s al,%u\n", op, v & 0xFF);
return 1;
}
printf("\t%s ax,%u\n", op, v);
if (size == 4)
printf("\t%s dx,%u\n", op2, (unsigned)((r->value >> 16) & 0xFFFF));
return 1;
case T_NREF:
name = namestr(r->snum);
printf("\t%s %s,_%s+%u\n", op, lr, name, v);
if (size == 4)
printf("\t%s dx,_%s+%u\n", op2, name, v + 2);
return 1;
case T_LBREF:
printf("\t%s %s,[T%u+%u]\n", op, lr, r->val2, v);
if (size == 4)
printf("\t%s dx,[T%u+%u]\n", op2, r->val2, v + 2);
return 1;
case T_LREF:
printf("\t%s %s,[bp + %u]\n", op, lr, v);
if (size == 4)
printf("\t%s dx,[bp + %u]\n", op2, v + 2);
return 1;
case T_NAME:
name = namestr(r->snum);
if (size == 1) {
printf("\t%s al,<_%s+%u\n", op, name, v);
return 1;
}
printf("\t%s ax,_%s+%u\n", op, name, v);
if (size == 4)
printf("\t%s dx,0\n", op2);
return 1;
case T_LABEL:
if (size == 1) {
printf("\t%s al,T%u+%u\n", op, r->val2, v);
return 1;
}
printf("\t%s ax,T%u+%u\n", op, r->val2, v);
if (size == 4)
printf("\t%s dx,0\n", op2);
return 1;
}
return 0;
}
/* nr is 0 if we need the result, iv is set if the result is the original
value (x++ and x-- cases) */
unsigned bx_operation(unsigned size, const char *op, const char *op2,
unsigned nr, unsigned iv)
{
/* R has been evaluated, L is in BX */
if (size == 1) {
if (!nr && iv)
printf("\tmov ah,[bx]\n");
printf("\t%s [bx],al\n", op);
if (!nr) {
if (iv)
printf("\tmov al,ah\n");
else
printf("\tmov al,[bx]\n");
}
} else if (size == 2) {
if (!nr && iv)
printf("\tmov dx,[bx]\n");
printf("\t%s [bx],ax\n", op);
if (!nr) {
if (iv)
printf("\tmov ax,dx\n");
else
printf("\tmov ax,[bx]\n");
}
/* Size 4 never done with iv */
} else if (size == 4) {
printf("\t%s [bx],ax\n", op);
printf("\t%s 2[bx],dx\n", op2);
if (!nr) {
printf("\tmov ax,[bx]\n");
printf("\tmov dx,2[bx]\n");
}
}
return 1;
}
/* BP relative operation using a constant. The 8086 can do things like
add 4[bp],1 */
unsigned bp_const_op(struct node *n, const char *op, const char *op2,
unsigned nr, unsigned iv)
{
unsigned size = get_size(n->type);
unsigned v = n->value;
unsigned long cval = n->right->value;
/* R has been evaluated, L is in BX */
if (n->left->op == T_ARGUMENT)
v += argbase + frame_len;
if (size == 1) {
if (!nr && iv)
printf("\tmov al,[bp + %u]\n", v);
printf("\t%s byte [bp + %u],%lu\n", op, v, cval & 0xFF);
if (!nr && !iv)
printf("\tmov al,[bp + %u]\n", v);
} else if (size == 2) {
if (!nr && iv)
printf("\tmov ax,[bp + %u]\n", v);
printf("\t%s word [bp + %u],%lu\n", op, v, cval & 0xFFFF);
if (!nr && !iv)
printf("\tmov ax,[bp + %u]\n", v);
/* Size 4 never done with iv */
} else if (size == 4) {
printf("\t%s word [bp + %u],%lu\n", op, v, cval & 0xFFFF);
printf("\t%s word [bp + %u],%lu\n", op2, v + 2, cval >> 16);
if (!nr) {
printf("\tmov ax,[bp + %u]\n", v);
printf("\tmov dx,[bp + %u]\n", v + 2);
}
}
return 1;
}
/* Stack relative operations (eg ++ on a local) */
unsigned bp_operation(struct node *n, const char *op, const char *op2,
unsigned nr, unsigned iv)
{
unsigned size = get_size(n->type);
unsigned v = n->value;
printf(";bp operation %x (%x)\n", n->op, n->right->op);
/* R has been evaluated, L is in BX */
if (n->left->op == T_ARGUMENT)
v += argbase + frame_len;
if (size == 1) {
if (!nr && iv)
printf("\tmov ah,[bp + %u]\n", v);
printf("\t%s [bp + %u],al\n", op, v);
if (!nr) {
if (iv)
printf("\tmov al,ah\n");
else
printf("\tmov al,[bp + %u]\n", v);
}
} else if (size == 2) {
if (!nr && iv)
printf("\tmov dx,[bp + %u]\n", v);
printf("\t%s [bp + %u],ax\n", op, v);
if (!nr) {
if (iv)
printf("\tmov ax,dx\n");
else
printf("\tmov ax,[bp + %u]\n", v);
}
/* Size 4 never done with iv */
} else if (size == 4) {
printf("\t%s [bp + %u],ax\n", op, v);
printf("\t%s [bp + %u],dx\n", op2, v + 2);
if (!nr) {
printf("\tmov ax,[bp + %u]\n", v);
printf("\tmov dx,[bp + %u]\n", v + 2);
}
}
return 1;
}
unsigned can_load_reg(struct node *n)
{
switch(n->op) {
case T_CONSTANT:
case T_NAME:
case T_LABEL:
case T_LOCAL:
case T_ARGUMENT:
case T_LREF:
case T_LBREF:
case T_NREF:
return 1;
}
return 0;
}
void load_reg(const char *reg, struct node *n)
{
unsigned v = n->value;
switch(n->op) {
case T_CONSTANT:
printf("\tmov %s,%u\n", reg, v);
break;
case T_NAME:
printf("\tmov %s,_%s+%u\n", reg, namestr(n->snum), v);
break;
case T_LABEL:
printf("\tmov %s,T%u+%u\n", reg, n->val2, v);
break;
case T_ARGUMENT:
v += argbase + frame_len;
case T_LOCAL:
printf("\tlea %s,[bp + %u]\n", reg, v);
break;
case T_LREF:
printf("\tmov %s,[bp + %u]\n", reg, v);
break;
case T_LBREF:
printf("\tmov %s,[T%u+%u]\n", reg, n->val2, v);
break;
case T_NREF:
printf("\tmov %s,[_%s+%u]\n", reg, namestr(n->snum), v);
break;
}
}
/* TODO: we need a shortcut form because we can do
cmp 4[bp],0 and the like for const + simple forms
also rewrites to turn 0, 4[bp] into 4[bp],0 and switch
the tests (lteq to gteq etc) */
/* Use the type of the arguments as the type of the result is the
boolean */
unsigned cond_direct(struct node *n, const char *opu, const char *op)
{
struct node *r = n->right;
unsigned size = get_size(r->type);
/* Need to chain the right comparisons including signed and
unsigned parts. Do this out of line */
if (size == 4)
return 0;
if (r->type & UNSIGNED)
op = opu;
/* First try it directly */
/* We ought to consider reversing it, but that's probably best
as a rewrite rule when we add re-ordering to the rewrite
rules TODO */
if (!op_direct(r, size, "cmp", "cmp"))
return 0;
/* Until we have CCONLY sorted */
printf("\tcall __cc%s\n", op);
n->flags |= ISBOOL;
return 1;
}
static unsigned shift_const(const char *op, struct node *n)
{
unsigned size = get_size(n->type);
struct node *r = n->right;
unsigned v;
const char *reg = "ax";
if (size == 1)
reg = "al";
/* 8086 has no shift multiple */
if (size > 2)
return 0;
if (r->op == T_CONSTANT) {
/* TODO add short form helper rules somewhere
eg mov ah,al, xor al,al etc */
v = r->value & 0x0F;
if (v == 0)
return 1;
if (cpu >= 186) {
printf("\t%s %s,%u\n", op, reg, v);
return 1;
}
if (v <= 4) {
while(v--)
printf("\t%s %s\n", op, reg);
return 1;
}
}
if (can_load_reg(r)) {
load_reg("cl", r);
if (cpu < 186) {
unsigned l = get_label();
printf("\tX%u:\n", l);
printf("\t%s %s\n", op, reg);
printf("\tloopne X%u\n", l);
return 1;
} else {
printf("\t%s %s,cl\n", op, reg);
return 1;
}
}
return 0;
}
/*
* If possible turn this node into a direct access. We've already checked
* that the right hand side is suitable. If this returns 0 it will instead
* fall back to doing it stack based.
*/
unsigned gen_direct(struct node *n)
{
struct node *r = n->right;
unsigned size = get_size(n->type);
// unsigned nr = n->flags & NORETURN;
// unsigned v = n->value;
switch(n->op) {
/* Clean up is special and must be handled directly. It also has the
type of the function return so don't use that for the cleanup value
in n->right */
case T_CLEANUP:
if (r->value)
printf("\tadd sp,%u\n", (unsigned)(r->value & 0xFFFF));
return 1;
case T_PLUS:
return op_direct(r, size, "add", "adc");
case T_MINUS:
return op_direct(r, size, "sub", "sbc");
case T_AND:
return op_direct(r, size, "and", "and");
case T_OR:
return op_direct(r, size, "and", "and");
case T_HAT:
return op_direct(r, size, "and", "and");
/* 80186 has const multishift TODO */
case T_EQEQ:
return cond_direct(n, "z", "z");
case T_BANGEQ:
return cond_direct(n, "nz", "nz");
case T_LTEQ:
return cond_direct(n, "be", "le");
case T_GTEQ:
return cond_direct(n, "ae", "ge");
case T_LT:
return cond_direct(n, "b", "lt");
case T_GT:
return cond_direct(n, "a", "gt");
case T_LTLT:
/* DX:AX << n */
if (shift_const("shl", n))
return 1;
break;
case T_GTGT:
if (n->type & UNSIGNED) {
if (shift_const("shr", n))
return 1;
} else {
if (shift_const("sar", n))
return 1;
}
break;
}
return 0;
}
/*
* Allow the code generator to shortcut the generation of the argument
* of a single argument operator (for example to shortcut constant cases
* or simple name loads that can be done better directly)
*/
unsigned gen_uni_direct(struct node *n)
{
struct node *r = n->right;
unsigned size = get_size(n->type);
unsigned nr = n->flags & NORETURN;
unsigned v = n->value;
unsigned rv;
switch(n->op) {
case T_LSTORE:
if (nr && r->op == T_CONSTANT) {
rv = r->value;
switch(size) {
case 1:
printf("\tmov [bp + %u],%u\n", v, rv & 0xFF);
return 1;
case 2:
printf("\tmov [bp + %u],%u\n", v, rv & 0xFFFF);
return 1;
case 4:
printf("\tmov [bp + %u],%u\n", v, rv & 0xFFFF);
printf("\tmov [bp + %u],%u\n", v + 2,
((unsigned)(r->value >> 16) & 0xFFFF));
return 1;
}
}
return 0;
}
return 0;
}
/* Try and do the pluseq and other ops by evaluating the right then loading
the left into bx */
unsigned bx_shortcut(struct node *n, const char *op, const char *op2,
unsigned nr, unsigned iv)
{
unsigned size = get_size(n->type);
struct node *l = n->left;
struct node *r = n->right;
/* Can't do floats this way and for now don't deal with
PLUSPLUS/MINUSMINUS of dword */
if (n->type == FLOAT)
return 0;
if (size == 4 && !nr && iv)
return 0;
if (l->op == T_LOCAL || l->op == T_ARGUMENT) {
if (r->op == T_CONSTANT)
return bp_const_op(n, op, op2, nr, iv);
codegen_lr(n->right);
return bp_operation(n, op, op2, nr, iv);
}
/* TODO right const forms without loading into ax */
if (can_load_reg(n->left)) {
/* TODO: const ops as we have with locals */
/* Get the value into DX:AX */
codegen_lr(n->right);
/* Will not damage DX:AX */
load_reg("bx", n->left);
/* We are now doing ops on [bx] */
return bx_operation(size, op, op2, nr, iv);
}
return 0;
}
/*
* Allow the code generator to shortcut trees it knows
*/
unsigned gen_shortcut(struct node *n)
{
struct node *l = n->left;
struct node *r = n->right;
unsigned nr = n->flags & NORETURN;
unsigned size = get_size(n->type);
if (unreachable)
return 1;
switch(n->op) {
case T_COMMA:
/* The comma operator discards the result of the left side,
then evaluates the right. Avoid pushing/popping and
generating stuff that is surplus */
l->flags |= NORETURN;
codegen_lr(l);
/* Parent determines child node requirements */
codegen_lr(r);
r->flags |= nr;
return 1;
case T_EQ:
/* Need to look at (complex) = simple form later TODO */
if (!can_load_reg(n->left))
return 0;
/* Optimised path for the usual simple = expression case */
codegen_lr(n->right);
load_reg("bx", n->left);
switch(size) {
case 1:
printf("\tmov [bx],al\n");
return 1;
case 4:
printf("\tmov 2[bx],dx\n");
/* Fall through */
case 2:
printf("\tmov [bx],ax\n");
return 1;
}
return 0;
case T_PLUSEQ:
return bx_shortcut(n, "add", "adc", nr, 0);
case T_MINUSEQ:
return bx_shortcut(n, "add", "adc", nr, 0);
case T_PLUSPLUS:
return bx_shortcut(n, "add", "adc", nr, 1);
case T_MINUSMINUS:
return bx_shortcut(n, "add", "adc", nr, 1);
case T_ANDEQ:
return bx_shortcut(n, "and", "and", nr, 0);
case T_OREQ:
return bx_shortcut(n, "and", "and", nr, 0);
case T_HATEQ:
return bx_shortcut(n, "and", "and", nr, 0);
/* SHLEQ/SHREQ */
}
return 0;
}
unsigned boolop(struct node *n, const char *opu, const char *op)
{
struct node *r = n->right;
unsigned size = get_size(r->type);
if (r->type & UNSIGNED)
op = opu;
if (size == 4)
return 0;
printf("\tpop bx\n");
if (size == 2)
printf("\tcmp bx,ax\n");
else
printf("\tcmp bl,al\n");
printf("\tcall __cc%s\n", op);
n->flags |= ISBOOL;
return 1;
}
unsigned gen_cast(struct node *n)
{
unsigned lt = n->type;
unsigned rt = n->right->type;
unsigned ls;
unsigned rs;
if (PTR(rt))
rt = USHORT;
if (PTR(lt))
lt = USHORT;
/* Floats and stuff handled by helper */
if (!IS_INTARITH(lt) || !IS_INTARITH(rt))
return 0;
ls = get_size(lt);
rs = get_size(rt);
/* Size shrink is free */
if (ls <= rs)
return 1;
if (!(rt & UNSIGNED)) {
/* We have helpers yay! */
if (rs == 1)
printf("\tcbw\n");
if (ls == 4)
printf("\tcwd\n");
return 1;
}
if (rs == 1)
printf("\tmov ah,0\n");
if (ls == 4)
printf("\txor dx,dx\n");
return 1;
}
unsigned gen_node(struct node *n)
{
unsigned size = get_size(n->type);
unsigned nr = n->flags & NORETURN;
unsigned v = n->value;
const char *name;
switch(n->op) {
case T_CALLNAME:
printf("\tcall_%s+%u\n", namestr(n->snum), v);
return 1;
case T_FUNCCALL:
printf("\tmov bx,ax\n");
printf("\tcall [bx]\n");
return 1;
case T_CAST:
return gen_cast(n);