-
Notifications
You must be signed in to change notification settings - Fork 0
/
i386-gen.c
1135 lines (1046 loc) · 31.6 KB
/
i386-gen.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
/*
* X86 code generator for TCC
*
* Copyright (c) 2001-2004 Fabrice Bellard
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifdef TARGET_DEFS_ONLY
/* number of available registers */
#define NB_REGS 5
#define NB_ASM_REGS 8
#define CONFIG_TCC_ASM
/* a register can belong to several classes. The classes must be
sorted from more general to more precise (see gv2() code which does
assumptions on it). */
#define RC_INT 0x0001 /* generic integer register */
#define RC_FLOAT 0x0002 /* generic float register */
#define RC_EAX 0x0004
#define RC_ST0 0x0008
#define RC_ECX 0x0010
#define RC_EDX 0x0020
#define RC_EBX 0x0040
#define RC_IRET RC_EAX /* function return: integer register */
#define RC_IRE2 RC_EDX /* function return: second integer register */
#define RC_FRET RC_ST0 /* function return: float register */
/* pretty names for the registers */
enum {
TREG_EAX = 0,
TREG_ECX,
TREG_EDX,
TREG_EBX,
TREG_ST0,
TREG_ESP = 4
};
/* return registers for function */
#define REG_IRET TREG_EAX /* single word int return register */
#define REG_IRE2 TREG_EDX /* second word return register (for long long) */
#define REG_FRET TREG_ST0 /* float return register */
/* defined if function parameters must be evaluated in reverse order */
#define INVERT_FUNC_PARAMS
/* defined if structures are passed as pointers. Otherwise structures
are directly pushed on stack. */
/* #define FUNC_STRUCT_PARAM_AS_PTR */
/* pointer size, in bytes */
#define PTR_SIZE 4
/* long double size and alignment, in bytes */
#define LDOUBLE_SIZE 12
#define LDOUBLE_ALIGN 4
/* maximum alignment (for aligned attribute support) */
#define MAX_ALIGN 8
/* define if return values need to be extended explicitely
at caller side (for interfacing with non-TCC compilers) */
#define PROMOTE_RET
/******************************************************/
#else /* ! TARGET_DEFS_ONLY */
/******************************************************/
#define USING_GLOBALS
#include "tcc.h"
ST_DATA const char * const target_machine_defs =
"__i386__\0"
"__i386\0"
;
/* define to 1/0 to [not] have EBX as 4th register */
#define USE_EBX 0
ST_DATA const int reg_classes[NB_REGS] = {
/* eax */ RC_INT | RC_EAX,
/* ecx */ RC_INT | RC_ECX,
/* edx */ RC_INT | RC_EDX,
/* ebx */ (RC_INT | RC_EBX) * USE_EBX,
/* st0 */ RC_FLOAT | RC_ST0,
};
#ifdef CONFIG_TCC_BCHECK
static void gen_bounds_prolog(TCCState *S);
static void gen_bounds_epilog(TCCState *S);
#endif
/* XXX: make it faster ? */
ST_FUNC void g(TCCState *S, int c)
{
int ind1;
if (S->nocode_wanted)
return;
ind1 = S->ind + 1;
if (ind1 > cur_text_section->data_allocated)
section_realloc(S, cur_text_section, ind1);
cur_text_section->data[S->ind] = c;
S->ind = ind1;
}
ST_FUNC void o(TCCState *S, unsigned int c)
{
while (c) {
g(S, c);
c = c >> 8;
}
}
ST_FUNC void gen_le16(TCCState *S, int v)
{
g(S, v);
g(S, v >> 8);
}
ST_FUNC void gen_le32(TCCState *S, int c)
{
g(S, c);
g(S, c >> 8);
g(S, c >> 16);
g(S, c >> 24);
}
/* output a symbol and patch all calls to it */
ST_FUNC void gsym_addr(TCCState *S, int t, int a)
{
while (t) {
unsigned char *ptr = cur_text_section->data + t;
uint32_t n = read32le(ptr); /* next value */
write32le(ptr, a - t - 4);
t = n;
}
}
/* instruction + 4 bytes data. Return the address of the data */
static int oad(TCCState *S, int c, int s)
{
int t;
if (S->nocode_wanted)
return s;
o(S, c);
t = S->ind;
gen_le32(S, s);
return t;
}
ST_FUNC void gen_fill_nops(TCCState *S, int bytes)
{
while (bytes--)
g(S, 0x90);
}
/* generate jmp to a label */
#define gjmp2(s, instr,lbl) oad(s, instr,lbl)
/* output constant with relocation if 'r & VT_SYM' is true */
ST_FUNC void gen_addr32(TCCState *S, int r, Sym *sym, int c)
{
if (r & VT_SYM)
greloc(S, cur_text_section, sym, S->ind, R_386_32);
gen_le32(S, c);
}
ST_FUNC void gen_addrpc32(TCCState *S, int r, Sym *sym, int c)
{
if (r & VT_SYM)
greloc(S, cur_text_section, sym, S->ind, R_386_PC32);
gen_le32(S, c - 4);
}
/* generate a modrm reference. 'op_reg' contains the additional 3
opcode bits */
static void gen_modrm(TCCState *S, int op_reg, int r, Sym *sym, int c)
{
op_reg = op_reg << 3;
if ((r & VT_VALMASK) == VT_CONST) {
/* constant memory reference */
o(S, 0x05 | op_reg);
gen_addr32(S, r, sym, c);
} else if ((r & VT_VALMASK) == VT_LOCAL) {
/* currently, we use only ebp as base */
if (c == (char)c) {
/* short reference */
o(S, 0x45 | op_reg);
g(S, c);
} else {
oad(S, 0x85 | op_reg, c);
}
} else {
g(S, 0x00 | op_reg | (r & VT_VALMASK));
}
}
/* load 'r' from value 'sv' */
ST_FUNC void load(TCCState *S, int r, SValue *sv)
{
int v, t, ft, fc, fr;
SValue v1;
#ifdef TCC_TARGET_PE
SValue v2;
sv = pe_getimport(S, sv, &v2);
#endif
fr = sv->r;
ft = sv->type.t & ~VT_DEFSIGN;
fc = sv->c.i;
ft &= ~(VT_VOLATILE | VT_CONSTANT);
v = fr & VT_VALMASK;
if (fr & VT_LVAL) {
if (v == VT_LLOCAL) {
v1.type.t = VT_INT;
v1.r = VT_LOCAL | VT_LVAL;
v1.c.i = fc;
v1.sym = NULL;
fr = r;
if (!(reg_classes[fr] & RC_INT))
fr = get_reg(S, RC_INT);
load(S, fr, &v1);
}
if ((ft & VT_BTYPE) == VT_FLOAT) {
o(S, 0xd9); /* flds */
r = 0;
} else if ((ft & VT_BTYPE) == VT_DOUBLE) {
o(S, 0xdd); /* fldl */
r = 0;
} else if ((ft & VT_BTYPE) == VT_LDOUBLE) {
o(S, 0xdb); /* fldt */
r = 5;
} else if ((ft & VT_TYPE) == VT_BYTE || (ft & VT_TYPE) == VT_BOOL) {
o(S, 0xbe0f); /* movsbl */
} else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) {
o(S, 0xb60f); /* movzbl */
} else if ((ft & VT_TYPE) == VT_SHORT) {
o(S, 0xbf0f); /* movswl */
} else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) {
o(S, 0xb70f); /* movzwl */
} else {
o(S, 0x8b); /* movl */
}
gen_modrm(S, r, fr, sv->sym, fc);
} else {
if (v == VT_CONST) {
o(S, 0xb8 + r); /* mov $xx, r */
gen_addr32(S, fr, sv->sym, fc);
} else if (v == VT_LOCAL) {
if (fc) {
o(S, 0x8d); /* lea xxx(%ebp), r */
gen_modrm(S, r, VT_LOCAL, sv->sym, fc);
} else {
o(S, 0x89);
o(S, 0xe8 + r); /* mov %ebp, r */
}
} else if (v == VT_CMP) {
o(S, 0x0f); /* setxx %br */
o(S, fc);
o(S, 0xc0 + r);
o(S, 0xc0b60f + r * 0x90000); /* movzbl %al, %eax */
} else if (v == VT_JMP || v == VT_JMPI) {
t = v & 1;
oad(S, 0xb8 + r, t); /* mov $1, r */
o(S, 0x05eb); /* jmp after */
gsym(S, fc);
oad(S, 0xb8 + r, t ^ 1); /* mov $0, r */
} else if (v != r) {
o(S, 0x89);
o(S, 0xc0 + r + v * 8); /* mov v, r */
}
}
}
/* store register 'r' in lvalue 'v' */
ST_FUNC void store(TCCState *S, int r, SValue *v)
{
int fr, bt, ft, fc;
#ifdef TCC_TARGET_PE
SValue v2;
v = pe_getimport(S, v, &v2);
#endif
ft = v->type.t;
fc = v->c.i;
fr = v->r & VT_VALMASK;
ft &= ~(VT_VOLATILE | VT_CONSTANT);
bt = ft & VT_BTYPE;
/* XXX: incorrect if float reg to reg */
if (bt == VT_FLOAT) {
o(S, 0xd9); /* fsts */
r = 2;
} else if (bt == VT_DOUBLE) {
o(S, 0xdd); /* fstpl */
r = 2;
} else if (bt == VT_LDOUBLE) {
o(S, 0xc0d9); /* fld %st(0) */
o(S, 0xdb); /* fstpt */
r = 7;
} else {
if (bt == VT_SHORT)
o(S, 0x66);
if (bt == VT_BYTE || bt == VT_BOOL)
o(S, 0x88);
else
o(S, 0x89);
}
if (fr == VT_CONST ||
fr == VT_LOCAL ||
(v->r & VT_LVAL)) {
gen_modrm(S, r, v->r, v->sym, fc);
} else if (fr != r) {
o(S, 0xc0 + fr + r * 8); /* mov r, fr */
}
}
static void gadd_sp(TCCState *S, int val)
{
if (val == (char)val) {
o(S, 0xc483);
g(S, val);
} else {
oad(S, 0xc481, val); /* add $xxx, %esp */
}
}
#if defined CONFIG_TCC_BCHECK || defined TCC_TARGET_PE
static void gen_static_call(TCCState *S, int v)
{
Sym *sym;
sym = external_helper_sym(S, v);
oad(S, 0xe8, -4);
greloc(S, cur_text_section, sym, S->ind-4, R_386_PC32);
}
#endif
/* 'is_jmp' is '1' if it is a jump */
static void gcall_or_jmp(TCCState *S, int is_jmp)
{
int r;
if ((S->vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST && (S->vtop->r & VT_SYM)) {
/* constant and relocation case */
greloc(S, cur_text_section, S->vtop->sym, S->ind + 1, R_386_PC32);
oad(S, 0xe8 + is_jmp, S->vtop->c.i - 4); /* call/jmp im */
} else {
/* otherwise, indirect call */
r = gv(S, RC_INT);
o(S, 0xff); /* call/jmp *r */
o(S, 0xd0 + r + (is_jmp << 4));
}
}
static const uint8_t fastcall_regs[3] = { TREG_EAX, TREG_EDX, TREG_ECX };
static const uint8_t fastcallw_regs[2] = { TREG_ECX, TREG_EDX };
/* Return the number of registers needed to return the struct, or 0 if
returning via struct pointer. */
ST_FUNC int gfunc_sret(TCCState *S, CType *vt, int variadic, CType *ret, int *ret_align, int *regsize)
{
#if defined(TCC_TARGET_PE) || TARGETOS_FreeBSD || TARGETOS_OpenBSD
int size, align;
*ret_align = 1; // Never have to re-align return values for x86
*regsize = 4;
size = type_size(vt, &align);
if (size > 8 || (size & (size - 1)))
return 0;
if (size == 8)
ret->t = VT_LLONG;
else if (size == 4)
ret->t = VT_INT;
else if (size == 2)
ret->t = VT_SHORT;
else
ret->t = VT_BYTE;
ret->ref = NULL;
return 1;
#else
*ret_align = 1; // Never have to re-align return values for x86
return 0;
#endif
}
/* Generate function call. The function address is pushed first, then
all the parameters in call order. This functions pops all the
parameters and the function address. */
ST_FUNC void gfunc_call(TCCState *S, int nb_args)
{
int size, align, r, args_size, i, func_call;
Sym *func_sym;
#ifdef CONFIG_TCC_BCHECK
if (S->do_bounds_check)
gbound_args(S, nb_args);
#endif
args_size = 0;
for(i = 0;i < nb_args; i++) {
if ((S->vtop->type.t & VT_BTYPE) == VT_STRUCT) {
size = type_size(&S->vtop->type, &align);
/* align to stack align size */
size = (size + 3) & ~3;
/* allocate the necessary size on stack */
#ifdef TCC_TARGET_PE
if (size >= 4096) {
r = get_reg(S, RC_EAX);
oad(S, 0x68, size); // push size
/* cannot call normal 'alloca' with bound checking */
gen_static_call(S, tok_alloc_const(S, "__alloca"));
gadd_sp(S, 4);
} else
#endif
{
oad(S, 0xec81, size); /* sub $xxx, %esp */
/* generate structure store */
r = get_reg(S, RC_INT);
o(S, 0xe089 + (r << 8)); /* mov %esp, r */
}
vset(S, &S->vtop->type, r | VT_LVAL, 0);
vswap(S);
vstore(S);
args_size += size;
} else if (is_float(S->vtop->type.t)) {
gv(S, RC_FLOAT); /* only one float register */
if ((S->vtop->type.t & VT_BTYPE) == VT_FLOAT)
size = 4;
else if ((S->vtop->type.t & VT_BTYPE) == VT_DOUBLE)
size = 8;
else
size = 12;
oad(S, 0xec81, size); /* sub $xxx, %esp */
if (size == 12)
o(S, 0x7cdb);
else
o(S, 0x5cd9 + size - 4); /* fstp[s|l] 0(%esp) */
g(S, 0x24);
g(S, 0x00);
args_size += size;
} else {
/* simple type (currently always same size) */
/* XXX: implicit cast ? */
r = gv(S, RC_INT);
if ((S->vtop->type.t & VT_BTYPE) == VT_LLONG) {
size = 8;
o(S, 0x50 + S->vtop->r2); /* push r */
} else {
size = 4;
}
o(S, 0x50 + r); /* push r */
args_size += size;
}
S->vtop--;
}
save_regs(S, 0); /* save used temporary registers */
func_sym = S->vtop->type.ref;
func_call = func_sym->f.func_call;
/* fast call case */
if ((func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) ||
func_call == FUNC_FASTCALLW) {
int fastcall_nb_regs;
const uint8_t *fastcall_regs_ptr;
if (func_call == FUNC_FASTCALLW) {
fastcall_regs_ptr = fastcallw_regs;
fastcall_nb_regs = 2;
} else {
fastcall_regs_ptr = fastcall_regs;
fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
}
for(i = 0;i < fastcall_nb_regs; i++) {
if (args_size <= 0)
break;
o(S, 0x58 + fastcall_regs_ptr[i]); /* pop r */
/* XXX: incorrect for struct/floats */
args_size -= 4;
}
}
#if !defined(TCC_TARGET_PE) && !TARGETOS_FreeBSD || TARGETOS_OpenBSD
else if ((S->vtop->type.ref->type.t & VT_BTYPE) == VT_STRUCT)
args_size -= 4;
#endif
gcall_or_jmp(S, 0);
if (args_size && func_call != FUNC_STDCALL && func_call != FUNC_FASTCALLW)
gadd_sp(S, args_size);
S->vtop--;
}
#ifdef TCC_TARGET_PE
#define FUNC_PROLOG_SIZE (10 + USE_EBX)
#else
#define FUNC_PROLOG_SIZE (9 + USE_EBX)
#endif
/* generate function prolog of type 't' */
ST_FUNC void gfunc_prolog(TCCState *S, Sym *func_sym)
{
CType *func_type = &func_sym->type;
int addr, align, size, func_call, fastcall_nb_regs;
int param_index, param_addr;
const uint8_t *fastcall_regs_ptr;
Sym *sym;
CType *type;
sym = func_type->ref;
func_call = sym->f.func_call;
addr = 8;
S->loc = 0;
S->tccgen_func_vc = 0;
if (func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) {
fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
fastcall_regs_ptr = fastcall_regs;
} else if (func_call == FUNC_FASTCALLW) {
fastcall_nb_regs = 2;
fastcall_regs_ptr = fastcallw_regs;
} else {
fastcall_nb_regs = 0;
fastcall_regs_ptr = NULL;
}
param_index = 0;
S->ind += FUNC_PROLOG_SIZE;
S->sf_func_sub_sp_offset = S->ind;
/* if the function returns a structure, then add an
implicit pointer parameter */
#if defined(TCC_TARGET_PE) || TARGETOS_FreeBSD || TARGETOS_OpenBSD
size = type_size(&S->tccgen_func_vt,&align);
if (((S->tccgen_func_vt.t & VT_BTYPE) == VT_STRUCT)
&& (size > 8 || (size & (size - 1)))) {
#else
if ((S->tccgen_func_vt.t & VT_BTYPE) == VT_STRUCT) {
#endif
/* XXX: fastcall case ? */
S->tccgen_func_vc = addr;
addr += 4;
param_index++;
}
/* define parameters */
while ((sym = sym->next) != NULL) {
type = &sym->type;
size = type_size(type, &align);
size = (size + 3) & ~3;
#ifdef FUNC_STRUCT_PARAM_AS_PTR
/* structs are passed as pointer */
if ((type->t & VT_BTYPE) == VT_STRUCT) {
size = 4;
}
#endif
if (param_index < fastcall_nb_regs) {
/* save FASTCALL register */
S->loc -= 4;
o(S, 0x89); /* movl */
gen_modrm(S, fastcall_regs_ptr[param_index], VT_LOCAL, NULL, S->loc);
param_addr = S->loc;
} else {
param_addr = addr;
addr += size;
}
sym_push(S, sym->v & ~SYM_FIELD, type,
VT_LOCAL | VT_LVAL, param_addr);
param_index++;
}
S->sf_func_ret_sub = 0;
/* pascal type call or fastcall ? */
if (func_call == FUNC_STDCALL || func_call == FUNC_FASTCALLW)
S->sf_func_ret_sub = addr - 8;
#if !defined(TCC_TARGET_PE) && !TARGETOS_FreeBSD || TARGETOS_OpenBSD
else if (S->tccgen_func_vc)
S->sf_func_ret_sub = 4;
#endif
#ifdef CONFIG_TCC_BCHECK
if (S->do_bounds_check)
gen_bounds_prolog(S);
#endif
}
/* generate function epilog */
ST_FUNC void gfunc_epilog(TCCState *S)
{
addr_t v, saved_ind;
#ifdef CONFIG_TCC_BCHECK
if (S->do_bounds_check)
gen_bounds_epilog(S);
#endif
/* align local size to word & save local variables */
v = (-S->loc + 3) & -4;
#if USE_EBX
o(S, 0x8b);
gen_modrm(S, TREG_EBX, VT_LOCAL, NULL, -(v+4));
#endif
o(S, 0xc9); /* leave */
if (S->sf_func_ret_sub == 0) {
o(S, 0xc3); /* ret */
} else {
o(S, 0xc2); /* ret n */
g(S, S->sf_func_ret_sub);
g(S, S->sf_func_ret_sub >> 8);
}
saved_ind = S->ind;
S->ind = S->sf_func_sub_sp_offset - FUNC_PROLOG_SIZE;
#ifdef TCC_TARGET_PE
if (v >= 4096) {
oad(S, 0xb8, v); /* mov stacksize, %eax */
gen_static_call(S, TOK___chkstk); /* call __chkstk, (does the stackframe too) */
} else
#endif
{
o(S, 0xe58955); /* push %ebp, mov %esp, %ebp */
o(S, 0xec81); /* sub esp, stacksize */
gen_le32(S, v);
#ifdef TCC_TARGET_PE
o(S, 0x90); /* adjust to FUNC_PROLOG_SIZE */
#endif
}
o(S, 0x53 * USE_EBX); /* push ebx */
S->ind = saved_ind;
}
/* generate a jump to a label */
ST_FUNC int gjmp(TCCState *S, int t)
{
return gjmp2(S, 0xe9, t);
}
/* generate a jump to a fixed address */
ST_FUNC void gjmp_addr(TCCState *S, int a)
{
int r;
r = a - S->ind - 2;
if (r == (char)r) {
g(S, 0xeb);
g(S, r);
} else {
oad(S, 0xe9, a - S->ind - 5);
}
}
#if 0
/* generate a jump to a fixed address */
ST_FUNC void gjmp_cond_addr(TCCState *S, int a, int op)
{
int r = a - S->ind - 2;
if (r == (char)r)
g(S, op - 32), g(r);
else
g(S, 0x0f), gjmp2(S, op - 16, r - 4);
}
#endif
ST_FUNC int gjmp_append(TCCState *S, int n, int t)
{
void *p;
/* insert vtop->c jump list in t */
if (n) {
uint32_t n1 = n, n2;
while ((n2 = read32le(p = cur_text_section->data + n1)))
n1 = n2;
write32le(p, t);
t = n;
}
return t;
}
ST_FUNC int gjmp_cond(TCCState *S, int op, int t)
{
g(S, 0x0f);
t = gjmp2(S, op - 16, t);
return t;
}
ST_FUNC void gen_opi(TCCState *S, int op)
{
int r, fr, opc, c;
switch(op) {
case '+':
case TOK_ADDC1: /* add with carry generation */
opc = 0;
gen_op8:
if ((S->vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
/* constant case */
vswap(S);
r = gv(S, RC_INT);
vswap(S);
c = S->vtop->c.i;
if (c == (char)c) {
/* generate inc and dec for smaller code */
if ((c == 1 || c == -1) && (op == '+' || op == '-')) {
opc = (c == 1) ^ (op == '+');
o(S, 0x40 | (opc << 3) | r); // inc,dec
} else {
o(S, 0x83);
o(S, 0xc0 | (opc << 3) | r);
g(S, c);
}
} else {
o(S, 0x81);
oad(S, 0xc0 | (opc << 3) | r, c);
}
} else {
gv2(S, RC_INT, RC_INT);
r = S->vtop[-1].r;
fr = S->vtop[0].r;
o(S, (opc << 3) | 0x01);
o(S, 0xc0 + r + fr * 8);
}
S->vtop--;
if (op >= TOK_ULT && op <= TOK_GT)
vset_VT_CMP(S, op);
break;
case '-':
case TOK_SUBC1: /* sub with carry generation */
opc = 5;
goto gen_op8;
case TOK_ADDC2: /* add with carry use */
opc = 2;
goto gen_op8;
case TOK_SUBC2: /* sub with carry use */
opc = 3;
goto gen_op8;
case '&':
opc = 4;
goto gen_op8;
case '^':
opc = 6;
goto gen_op8;
case '|':
opc = 1;
goto gen_op8;
case '*':
gv2(S, RC_INT, RC_INT);
r = S->vtop[-1].r;
fr = S->vtop[0].r;
S->vtop--;
o(S, 0xaf0f); /* imul fr, r */
o(S, 0xc0 + fr + r * 8);
break;
case TOK_SHL:
opc = 4;
goto gen_shift;
case TOK_SHR:
opc = 5;
goto gen_shift;
case TOK_SAR:
opc = 7;
gen_shift:
opc = 0xc0 | (opc << 3);
if ((S->vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
/* constant case */
vswap(S);
r = gv(S, RC_INT);
vswap(S);
c = S->vtop->c.i & 0x1f;
o(S, 0xc1); /* shl/shr/sar $xxx, r */
o(S, opc | r);
g(S, c);
} else {
/* we generate the shift in ecx */
gv2(S, RC_INT, RC_ECX);
r = S->vtop[-1].r;
o(S, 0xd3); /* shl/shr/sar %cl, r */
o(S, opc | r);
}
S->vtop--;
break;
case '/':
case TOK_UDIV:
case TOK_PDIV:
case '%':
case TOK_UMOD:
case TOK_UMULL:
/* first operand must be in eax */
/* XXX: need better constraint for second operand */
gv2(S, RC_EAX, RC_ECX);
r = S->vtop[-1].r;
fr = S->vtop[0].r;
S->vtop--;
save_reg(S, TREG_EDX);
/* save EAX too if used otherwise */
save_reg_upstack(S, TREG_EAX, 1);
if (op == TOK_UMULL) {
o(S, 0xf7); /* mul fr */
o(S, 0xe0 + fr);
S->vtop->r2 = TREG_EDX;
r = TREG_EAX;
} else {
if (op == TOK_UDIV || op == TOK_UMOD) {
o(S, 0xf7d231); /* xor %edx, %edx, div fr, %eax */
o(S, 0xf0 + fr);
} else {
o(S, 0xf799); /* cltd, idiv fr, %eax */
o(S, 0xf8 + fr);
}
if (op == '%' || op == TOK_UMOD)
r = TREG_EDX;
else
r = TREG_EAX;
}
S->vtop->r = r;
break;
default:
opc = 7;
goto gen_op8;
}
}
/* generate a floating point operation 'v = t1 op t2' instruction. The
two operands are guaranteed to have the same floating point type */
/* XXX: need to use ST1 too */
ST_FUNC void gen_opf(TCCState *S, int op)
{
int a, ft, fc, swapped, r;
if (op == TOK_NEG) { /* unary minus */
gv(S, RC_FLOAT);
o(S, 0xe0d9); /* fchs */
return;
}
/* convert constants to memory references */
if ((S->vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
vswap(S);
gv(S, RC_FLOAT);
vswap(S);
}
if ((S->vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
gv(S, RC_FLOAT);
/* must put at least one value in the floating point register */
if ((S->vtop[-1].r & VT_LVAL) &&
(S->vtop[0].r & VT_LVAL)) {
vswap(S);
gv(S, RC_FLOAT);
vswap(S);
}
swapped = 0;
/* swap the stack if needed so that t1 is the register and t2 is
the memory reference */
if (S->vtop[-1].r & VT_LVAL) {
vswap(S);
swapped = 1;
}
if (op >= TOK_ULT && op <= TOK_GT) {
/* load on stack second operand */
load(S, TREG_ST0, S->vtop);
save_reg(S, TREG_EAX); /* eax is used by FP comparison code */
if (op == TOK_GE || op == TOK_GT)
swapped = !swapped;
else if (op == TOK_EQ || op == TOK_NE)
swapped = 0;
if (swapped)
o(S, 0xc9d9); /* fxch %st(1) */
if (op == TOK_EQ || op == TOK_NE)
o(S, 0xe9da); /* fucompp */
else
o(S, 0xd9de); /* fcompp */
o(S, 0xe0df); /* fnstsw %ax */
if (op == TOK_EQ) {
o(S, 0x45e480); /* and $0x45, %ah */
o(S, 0x40fC80); /* cmp $0x40, %ah */
} else if (op == TOK_NE) {
o(S, 0x45e480); /* and $0x45, %ah */
o(S, 0x40f480); /* xor $0x40, %ah */
op = TOK_NE;
} else if (op == TOK_GE || op == TOK_LE) {
o(S, 0x05c4f6); /* test $0x05, %ah */
op = TOK_EQ;
} else {
o(S, 0x45c4f6); /* test $0x45, %ah */
op = TOK_EQ;
}
S->vtop--;
vset_VT_CMP(S, op);
} else {
/* no memory reference possible for long double operations */
if ((S->vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
load(S, TREG_ST0, S->vtop);
swapped = !swapped;
}
switch(op) {
default:
case '+':
a = 0;
break;
case '-':
a = 4;
if (swapped)
a++;
break;
case '*':
a = 1;
break;
case '/':
a = 6;
if (swapped)
a++;
break;
}
ft = S->vtop->type.t;
fc = S->vtop->c.i;
if ((ft & VT_BTYPE) == VT_LDOUBLE) {
o(S, 0xde); /* fxxxp %st, %st(1) */
o(S, 0xc1 + (a << 3));
} else {
/* if saved lvalue, then we must reload it */
r = S->vtop->r;
if ((r & VT_VALMASK) == VT_LLOCAL) {
SValue v1;
r = get_reg(S, RC_INT);
v1.type.t = VT_INT;
v1.r = VT_LOCAL | VT_LVAL;
v1.c.i = fc;
v1.sym = NULL;
load(S, r, &v1);
fc = 0;
}
if ((ft & VT_BTYPE) == VT_DOUBLE)
o(S, 0xdc);
else
o(S, 0xd8);
gen_modrm(S, a, r, S->vtop->sym, fc);
}
S->vtop--;
}
}
/* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
and 'long long' cases. */
ST_FUNC void gen_cvt_itof(TCCState *S, int t)
{
save_reg(S, TREG_ST0);
gv(S, RC_INT);
if ((S->vtop->type.t & VT_BTYPE) == VT_LLONG) {
/* signed long long to float/double/long double (unsigned case
is handled generically) */
o(S, 0x50 + S->vtop->r2); /* push r2 */
o(S, 0x50 + (S->vtop->r & VT_VALMASK)); /* push r */
o(S, 0x242cdf); /* fildll (%esp) */
o(S, 0x08c483); /* add $8, %esp */
S->vtop->r2 = VT_CONST;
} else if ((S->vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
(VT_INT | VT_UNSIGNED)) {
/* unsigned int to float/double/long double */
o(S, 0x6a); /* push $0 */
g(S, 0x00);
o(S, 0x50 + (S->vtop->r & VT_VALMASK)); /* push r */
o(S, 0x242cdf); /* fildll (%esp) */
o(S, 0x08c483); /* add $8, %esp */
} else {
/* int to float/double/long double */
o(S, 0x50 + (S->vtop->r & VT_VALMASK)); /* push r */
o(S, 0x2404db); /* fildl (%esp) */
o(S, 0x04c483); /* add $4, %esp */
}
S->vtop->r2 = VT_CONST;
S->vtop->r = TREG_ST0;
}
/* convert fp to int 't' type */
ST_FUNC void gen_cvt_ftoi(TCCState *S, int t)
{
int bt = S->vtop->type.t & VT_BTYPE;
if (bt == VT_FLOAT)
vpush_helper_func(S, TOK___fixsfdi);
else if (bt == VT_LDOUBLE)
vpush_helper_func(S, TOK___fixxfdi);
else
vpush_helper_func(S, TOK___fixdfdi);
vswap(S);
gfunc_call(S, 1);
vpushi(S, 0);
S->vtop->r = REG_IRET;
if ((t & VT_BTYPE) == VT_LLONG)
S->vtop->r2 = REG_IRE2;
}