forked from swizl/tinycc_zh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arm-gen.c
2151 lines (2025 loc) · 54.4 KB
/
arm-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
/*
* ARMv4 code generator for TCC
*
* Copyright (c) 2003 Daniel Gl?ckner
* Copyright (c) 2012 Thomas Preud'homme
*
* Based on i386-gen.c by 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
*/
#如定义 TARGET_DEFS_ONLY
#如 已定义(TCC_ARM_EABI) && !已定义(TCC_ARM_VFP)
#错误 "Currently TinyCC only supports float computation with VFP instructions"
#了如
/* number of available registers */
#如定义 TCC_ARM_VFP
#定义 NB_REGS 13
#另
#定义 NB_REGS 9
#了如
#如未定义 TCC_CPU_VERSION
# 定义 TCC_CPU_VERSION 5
#了如
/* 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). */
#定义 RC_INT 0x0001 /* generic integer register */
#定义 RC_FLOAT 0x0002 /* generic float register */
#定义 RC_R0 0x0004
#定义 RC_R1 0x0008
#定义 RC_R2 0x0010
#定义 RC_R3 0x0020
#定义 RC_R12 0x0040
#定义 RC_F0 0x0080
#定义 RC_F1 0x0100
#定义 RC_F2 0x0200
#定义 RC_F3 0x0400
#如定义 TCC_ARM_VFP
#定义 RC_F4 0x0800
#定义 RC_F5 0x1000
#定义 RC_F6 0x2000
#定义 RC_F7 0x4000
#了如
#定义 RC_IRET RC_R0 /* function return: integer register */
#定义 RC_LRET RC_R1 /* function return: second integer register */
#定义 RC_FRET RC_F0 /* function return: float register */
/* pretty names for the registers */
枚举 {
TREG_R0 = 0,
TREG_R1,
TREG_R2,
TREG_R3,
TREG_R12,
TREG_F0,
TREG_F1,
TREG_F2,
TREG_F3,
#如定义 TCC_ARM_VFP
TREG_F4,
TREG_F5,
TREG_F6,
TREG_F7,
#了如
TREG_SP = 13,
TREG_LR,
};
#如定义 TCC_ARM_VFP
#定义 T2CPR(t) (((t) & VT_BTYPE) != VT_FLOAT ? 0x100 : 0)
#了如
/* return registers for function */
#定义 REG_IRET TREG_R0 /* single word int return register */
#定义 REG_LRET TREG_R1 /* second word return register (for long long) */
#定义 REG_FRET TREG_F0 /* float return register */
#如定义 TCC_ARM_EABI
#定义 TOK___divdi3 TOK___aeabi_ldivmod
#定义 TOK___moddi3 TOK___aeabi_ldivmod
#定义 TOK___udivdi3 TOK___aeabi_uldivmod
#定义 TOK___umoddi3 TOK___aeabi_uldivmod
#了如
/* defined if function parameters must be evaluated in reverse order */
#定义 INVERT_FUNC_PARAMS
/* defined if structures are passed as pointers. Otherwise structures
are directly pushed on stack. */
/* #定义 FUNC_STRUCT_PARAM_AS_PTR */
/* pointer size, in bytes */
#定义 PTR_SIZE 4
/* long double size and alignment, in bytes */
#如定义 TCC_ARM_VFP
#定义 LDOUBLE_SIZE 8
#了如
#如未定义 LDOUBLE_SIZE
#定义 LDOUBLE_SIZE 8
#了如
#如定义 TCC_ARM_EABI
#定义 LDOUBLE_ALIGN 8
#另
#定义 LDOUBLE_ALIGN 4
#了如
/* maximum alignment (for aligned attribute support) */
#定义 MAX_ALIGN 8
#定义 CHAR_IS_UNSIGNED
/******************************************************/
#另 /* ! TARGET_DEFS_ONLY */
/******************************************************/
#包含 "tcc.h"
枚举 float_abi float_abi;
ST_DATA 不变 整 reg_classes[NB_REGS] = {
/* r0 */ RC_INT | RC_R0,
/* r1 */ RC_INT | RC_R1,
/* r2 */ RC_INT | RC_R2,
/* r3 */ RC_INT | RC_R3,
/* r12 */ RC_INT | RC_R12,
/* f0 */ RC_FLOAT | RC_F0,
/* f1 */ RC_FLOAT | RC_F1,
/* f2 */ RC_FLOAT | RC_F2,
/* f3 */ RC_FLOAT | RC_F3,
#如定义 TCC_ARM_VFP
/* d4/s8 */ RC_FLOAT | RC_F4,
/* d5/s10 */ RC_FLOAT | RC_F5,
/* d6/s12 */ RC_FLOAT | RC_F6,
/* d7/s14 */ RC_FLOAT | RC_F7,
#了如
};
静态 整 func_sub_sp_offset, last_itod_magic;
静态 整 leaffunc;
#如 已定义(TCC_ARM_EABI) && 已定义(TCC_ARM_VFP)
静态 CType float_type, double_type, func_float_type, func_double_type;
ST_FUNC 空 arm_init(结构 TCCState *s)
{
float_type.t = VT_FLOAT;
double_type.t = VT_DOUBLE;
func_float_type.t = VT_FUNC;
func_float_type.ref = sym_push(SYM_FIELD, &float_type, FUNC_CDECL, FUNC_OLD);
func_double_type.t = VT_FUNC;
func_double_type.ref = sym_push(SYM_FIELD, &double_type, FUNC_CDECL, FUNC_OLD);
float_abi = s->float_abi;
#如未定义 TCC_ARM_HARDFLOAT
tcc_warning("soft float ABI currently not supported: default to softfp");
#了如
}
#另
#定义 func_float_type func_old_type
#定义 func_double_type func_old_type
#定义 func_ldouble_type func_old_type
ST_FUNC 空 arm_init(结构 TCCState *s)
{
#如 0
#如 !已定义 (TCC_ARM_VFP)
tcc_warning("Support for FPA is deprecated and will be removed in next"
" release");
#了如
#如 !已定义 (TCC_ARM_EABI)
tcc_warning("Support for OABI is deprecated and will be removed in next"
" release");
#了如
#了如
}
#了如
静态 整 two2mask(整 a,整 b) {
返回 (reg_classes[a]|reg_classes[b])&~(RC_INT|RC_FLOAT);
}
静态 整 regmask(整 r) {
返回 reg_classes[r]&~(RC_INT|RC_FLOAT);
}
/******************************************************/
#如 已定义(TCC_ARM_EABI) && !已定义(CONFIG_TCC_ELFINTERP)
不变 字 *default_elfinterp(结构 TCCState *s)
{
如 (s->float_abi == ARM_HARD_FLOAT)
返回 "/lib/ld-linux-armhf.so.3";
另
返回 "/lib/ld-linux.so.3";
}
#了如
空 o(uint32_t i)
{
/* this is a good place to start adding big-endian support*/
整 ind1;
如 (nocode_wanted)
返回;
ind1 = ind + 4;
如 (!cur_text_section)
tcc_error("compiler error! This happens f.ex. if the compiler\n"
"can't evaluate constant expressions outside of a function.");
如 (ind1 > cur_text_section->data_allocated)
section_realloc(cur_text_section, ind1);
cur_text_section->data[ind++] = i&255;
i>>=8;
cur_text_section->data[ind++] = i&255;
i>>=8;
cur_text_section->data[ind++] = i&255;
i>>=8;
cur_text_section->data[ind++] = i;
}
静态 uint32_t stuff_const(uint32_t op, uint32_t c)
{
整 try_neg=0;
uint32_t nc = 0, negop = 0;
转接(op&0x1F00000)
{
事例 0x800000: //add
事例 0x400000: //sub
try_neg=1;
negop=op^0xC00000;
nc=-c;
跳出;
事例 0x1A00000: //mov
事例 0x1E00000: //mvn
try_neg=1;
negop=op^0x400000;
nc=~c;
跳出;
事例 0x200000: //xor
如(c==~0)
返回 (op&0xF010F000)|((op>>16)&0xF)|0x1E00000;
跳出;
事例 0x0: //and
如(c==~0)
返回 (op&0xF010F000)|((op>>16)&0xF)|0x1A00000;
事例 0x1C00000: //bic
try_neg=1;
negop=op^0x1C00000;
nc=~c;
跳出;
事例 0x1800000: //orr
如(c==~0)
返回 (op&0xFFF0FFFF)|0x1E00000;
跳出;
}
运行 {
uint32_t m;
整 i;
如(c<256) /* catch undefined <<32 */
返回 op|c;
对于(i=2;i<32;i+=2) {
m=(0xff>>i)|(0xff<<(32-i));
如(!(c&~m))
返回 op|(i<<7)|(c<<i)|(c>>(32-i));
}
op=negop;
c=nc;
} 当(try_neg--);
返回 0;
}
//only add,sub
空 stuff_const_harder(uint32_t op, uint32_t v) {
uint32_t x;
x=stuff_const(op,v);
如(x)
o(x);
另 {
uint32_t a[16], nv, no, o2, n2;
整 i,j,k;
a[0]=0xff;
o2=(op&0xfff0ffff)|((op&0xf000)<<4);;
对于(i=1;i<16;i++)
a[i]=(a[i-1]>>2)|(a[i-1]<<30);
对于(i=0;i<12;i++)
对于(j=i<4?i+12:15;j>=i+4;j--)
如((v&(a[i]|a[j]))==v) {
o(stuff_const(op,v&a[i]));
o(stuff_const(o2,v&a[j]));
返回;
}
no=op^0xC00000;
n2=o2^0xC00000;
nv=-v;
对于(i=0;i<12;i++)
对于(j=i<4?i+12:15;j>=i+4;j--)
如((nv&(a[i]|a[j]))==nv) {
o(stuff_const(no,nv&a[i]));
o(stuff_const(n2,nv&a[j]));
返回;
}
对于(i=0;i<8;i++)
对于(j=i+4;j<12;j++)
对于(k=i<4?i+12:15;k>=j+4;k--)
如((v&(a[i]|a[j]|a[k]))==v) {
o(stuff_const(op,v&a[i]));
o(stuff_const(o2,v&a[j]));
o(stuff_const(o2,v&a[k]));
返回;
}
no=op^0xC00000;
nv=-v;
对于(i=0;i<8;i++)
对于(j=i+4;j<12;j++)
对于(k=i<4?i+12:15;k>=j+4;k--)
如((nv&(a[i]|a[j]|a[k]))==nv) {
o(stuff_const(no,nv&a[i]));
o(stuff_const(n2,nv&a[j]));
o(stuff_const(n2,nv&a[k]));
返回;
}
o(stuff_const(op,v&a[0]));
o(stuff_const(o2,v&a[4]));
o(stuff_const(o2,v&a[8]));
o(stuff_const(o2,v&a[12]));
}
}
uint32_t encbranch(整 pos, 整 addr, 整 fail)
{
addr-=pos+8;
addr/=4;
如(addr>=0x1000000 || addr<-0x1000000) {
如(fail)
tcc_error("FIXME: function bigger than 32MB");
返回 0;
}
返回 0x0A000000|(addr&0xffffff);
}
整 decbranch(整 pos)
{
整 x;
x=*(uint32_t *)(cur_text_section->data + pos);
x&=0x00ffffff;
如(x&0x800000)
x-=0x1000000;
返回 x*4+pos+8;
}
/* output a symbol and patch all calls to it */
空 gsym_addr(整 t, 整 a)
{
uint32_t *x;
整 lt;
当(t) {
x=(uint32_t *)(cur_text_section->data + t);
t=decbranch(lt=t);
如(a==lt+4)
*x=0xE1A00000; // nop
另 {
*x &= 0xff000000;
*x |= encbranch(lt,a,1);
}
}
}
空 gsym(整 t)
{
gsym_addr(t, ind);
}
#如定义 TCC_ARM_VFP
静态 uint32_t vfpr(整 r)
{
如(r<TREG_F0 || r>TREG_F7)
tcc_error("compiler error! register %i is no vfp register",r);
返回 r - TREG_F0;
}
#另
静态 uint32_t fpr(整 r)
{
如(r<TREG_F0 || r>TREG_F3)
tcc_error("compiler error! register %i is no fpa register",r);
返回 r - TREG_F0;
}
#了如
静态 uint32_t intr(整 r)
{
如(r == TREG_R12)
返回 12;
如(r >= TREG_R0 && r <= TREG_R3)
返回 r - TREG_R0;
如 (r >= TREG_SP && r <= TREG_LR)
返回 r + (13 - TREG_SP);
tcc_error("compiler error! register %i is no int register",r);
}
静态 空 calcaddr(uint32_t *base, 整 *off, 整 *sgn, 整 maxoff, 无符 shift)
{
如(*off>maxoff || *off&((1<<shift)-1)) {
uint32_t x, y;
x=0xE280E000;
如(*sgn)
x=0xE240E000;
x|=(*base)<<16;
*base=14; // lr
y=stuff_const(x,*off&~maxoff);
如(y) {
o(y);
*off&=maxoff;
返回;
}
y=stuff_const(x,(*off+maxoff)&~maxoff);
如(y) {
o(y);
*sgn=!*sgn;
*off=((*off+maxoff)&~maxoff)-*off;
返回;
}
stuff_const_harder(x,*off&~maxoff);
*off&=maxoff;
}
}
静态 uint32_t mapcc(整 cc)
{
转接(cc)
{
事例 TOK_ULT:
返回 0x30000000; /* CC/LO */
事例 TOK_UGE:
返回 0x20000000; /* CS/HS */
事例 TOK_EQ:
返回 0x00000000; /* EQ */
事例 TOK_NE:
返回 0x10000000; /* NE */
事例 TOK_ULE:
返回 0x90000000; /* LS */
事例 TOK_UGT:
返回 0x80000000; /* HI */
事例 TOK_Nset:
返回 0x40000000; /* MI */
事例 TOK_Nclear:
返回 0x50000000; /* PL */
事例 TOK_LT:
返回 0xB0000000; /* LT */
事例 TOK_GE:
返回 0xA0000000; /* GE */
事例 TOK_LE:
返回 0xD0000000; /* LE */
事例 TOK_GT:
返回 0xC0000000; /* GT */
}
tcc_error("unexpected condition code");
返回 0xE0000000; /* AL */
}
静态 整 negcc(整 cc)
{
转接(cc)
{
事例 TOK_ULT:
返回 TOK_UGE;
事例 TOK_UGE:
返回 TOK_ULT;
事例 TOK_EQ:
返回 TOK_NE;
事例 TOK_NE:
返回 TOK_EQ;
事例 TOK_ULE:
返回 TOK_UGT;
事例 TOK_UGT:
返回 TOK_ULE;
事例 TOK_Nset:
返回 TOK_Nclear;
事例 TOK_Nclear:
返回 TOK_Nset;
事例 TOK_LT:
返回 TOK_GE;
事例 TOK_GE:
返回 TOK_LT;
事例 TOK_LE:
返回 TOK_GT;
事例 TOK_GT:
返回 TOK_LE;
}
tcc_error("unexpected condition code");
返回 TOK_NE;
}
/* load 'r' from value 'sv' */
空 load(整 r, SValue *sv)
{
整 v, ft, fc, fr, sign;
uint32_t op;
SValue v1;
fr = sv->r;
ft = sv->type.t;
fc = sv->c.i;
如(fc>=0)
sign=0;
另 {
sign=1;
fc=-fc;
}
v = fr & VT_VALMASK;
如 (fr & VT_LVAL) {
uint32_t base = 0xB; // fp
如(v == VT_LLOCAL) {
v1.type.t = VT_PTR;
v1.r = VT_LOCAL | VT_LVAL;
v1.c.i = sv->c.i;
load(TREG_LR, &v1);
base = 14; /* lr */
fc=sign=0;
v=VT_LOCAL;
} 另 如(v == VT_CONST) {
v1.type.t = VT_PTR;
v1.r = fr&~VT_LVAL;
v1.c.i = sv->c.i;
v1.sym=sv->sym;
load(TREG_LR, &v1);
base = 14; /* lr */
fc=sign=0;
v=VT_LOCAL;
} 另 如(v < VT_CONST) {
base=intr(v);
fc=sign=0;
v=VT_LOCAL;
}
如(v == VT_LOCAL) {
如(is_float(ft)) {
calcaddr(&base,&fc,&sign,1020,2);
#如定义 TCC_ARM_VFP
op=0xED100A00; /* flds */
如(!sign)
op|=0x800000;
如 ((ft & VT_BTYPE) != VT_FLOAT)
op|=0x100; /* flds -> fldd */
o(op|(vfpr(r)<<12)|(fc>>2)|(base<<16));
#另
op=0xED100100;
如(!sign)
op|=0x800000;
#如 LDOUBLE_SIZE == 8
如 ((ft & VT_BTYPE) != VT_FLOAT)
op|=0x8000;
#另
如 ((ft & VT_BTYPE) == VT_DOUBLE)
op|=0x8000;
另 如 ((ft & VT_BTYPE) == VT_LDOUBLE)
op|=0x400000;
#了如
o(op|(fpr(r)<<12)|(fc>>2)|(base<<16));
#了如
} 另 如((ft & (VT_BTYPE|VT_UNSIGNED)) == VT_BYTE
|| (ft & VT_BTYPE) == VT_SHORT) {
calcaddr(&base,&fc,&sign,255,0);
op=0xE1500090;
如 ((ft & VT_BTYPE) == VT_SHORT)
op|=0x20;
如 ((ft & VT_UNSIGNED) == 0)
op|=0x40;
如(!sign)
op|=0x800000;
o(op|(intr(r)<<12)|(base<<16)|((fc&0xf0)<<4)|(fc&0xf));
} 另 {
calcaddr(&base,&fc,&sign,4095,0);
op=0xE5100000;
如(!sign)
op|=0x800000;
如 ((ft & VT_BTYPE) == VT_BYTE || (ft & VT_BTYPE) == VT_BOOL)
op|=0x400000;
o(op|(intr(r)<<12)|fc|(base<<16));
}
返回;
}
} 另 {
如 (v == VT_CONST) {
op=stuff_const(0xE3A00000|(intr(r)<<12),sv->c.i);
如 (fr & VT_SYM || !op) {
o(0xE59F0000|(intr(r)<<12));
o(0xEA000000);
如(fr & VT_SYM)
greloc(cur_text_section, sv->sym, ind, R_ARM_ABS32);
o(sv->c.i);
} 另
o(op);
返回;
} 另 如 (v == VT_LOCAL) {
op=stuff_const(0xE28B0000|(intr(r)<<12),sv->c.i);
如 (fr & VT_SYM || !op) {
o(0xE59F0000|(intr(r)<<12));
o(0xEA000000);
如(fr & VT_SYM) // needed ?
greloc(cur_text_section, sv->sym, ind, R_ARM_ABS32);
o(sv->c.i);
o(0xE08B0000|(intr(r)<<12)|intr(r));
} 另
o(op);
返回;
} 另 如(v == VT_CMP) {
o(mapcc(sv->c.i)|0x3A00001|(intr(r)<<12));
o(mapcc(negcc(sv->c.i))|0x3A00000|(intr(r)<<12));
返回;
} 另 如 (v == VT_JMP || v == VT_JMPI) {
整 t;
t = v & 1;
o(0xE3A00000|(intr(r)<<12)|t);
o(0xEA000000);
gsym(sv->c.i);
o(0xE3A00000|(intr(r)<<12)|(t^1));
返回;
} 另 如 (v < VT_CONST) {
如(is_float(ft))
#如定义 TCC_ARM_VFP
o(0xEEB00A40|(vfpr(r)<<12)|vfpr(v)|T2CPR(ft)); /* fcpyX */
#另
o(0xEE008180|(fpr(r)<<12)|fpr(v));
#了如
另
o(0xE1A00000|(intr(r)<<12)|intr(v));
返回;
}
}
tcc_error("load unimplemented!");
}
/* store register 'r' in lvalue 'v' */
空 store(整 r, SValue *sv)
{
SValue v1;
整 v, ft, fc, fr, sign;
uint32_t op;
fr = sv->r;
ft = sv->type.t;
fc = sv->c.i;
如(fc>=0)
sign=0;
另 {
sign=1;
fc=-fc;
}
v = fr & VT_VALMASK;
如 (fr & VT_LVAL || fr == VT_LOCAL) {
uint32_t base = 0xb; /* fp */
如(v < VT_CONST) {
base=intr(v);
v=VT_LOCAL;
fc=sign=0;
} 另 如(v == VT_CONST) {
v1.type.t = ft;
v1.r = fr&~VT_LVAL;
v1.c.i = sv->c.i;
v1.sym=sv->sym;
load(TREG_LR, &v1);
base = 14; /* lr */
fc=sign=0;
v=VT_LOCAL;
}
如(v == VT_LOCAL) {
如(is_float(ft)) {
calcaddr(&base,&fc,&sign,1020,2);
#如定义 TCC_ARM_VFP
op=0xED000A00; /* fsts */
如(!sign)
op|=0x800000;
如 ((ft & VT_BTYPE) != VT_FLOAT)
op|=0x100; /* fsts -> fstd */
o(op|(vfpr(r)<<12)|(fc>>2)|(base<<16));
#另
op=0xED000100;
如(!sign)
op|=0x800000;
#如 LDOUBLE_SIZE == 8
如 ((ft & VT_BTYPE) != VT_FLOAT)
op|=0x8000;
#另
如 ((ft & VT_BTYPE) == VT_DOUBLE)
op|=0x8000;
如 ((ft & VT_BTYPE) == VT_LDOUBLE)
op|=0x400000;
#了如
o(op|(fpr(r)<<12)|(fc>>2)|(base<<16));
#了如
返回;
} 另 如((ft & VT_BTYPE) == VT_SHORT) {
calcaddr(&base,&fc,&sign,255,0);
op=0xE14000B0;
如(!sign)
op|=0x800000;
o(op|(intr(r)<<12)|(base<<16)|((fc&0xf0)<<4)|(fc&0xf));
} 另 {
calcaddr(&base,&fc,&sign,4095,0);
op=0xE5000000;
如(!sign)
op|=0x800000;
如 ((ft & VT_BTYPE) == VT_BYTE || (ft & VT_BTYPE) == VT_BOOL)
op|=0x400000;
o(op|(intr(r)<<12)|fc|(base<<16));
}
返回;
}
}
tcc_error("store unimplemented");
}
静态 空 gadd_sp(整 val)
{
stuff_const_harder(0xE28DD000,val);
}
/* 'is_jmp' is '1' if it is a jump */
静态 空 gcall_or_jmp(整 is_jmp)
{
整 r;
如 ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
uint32_t x;
/* constant case */
x=encbranch(ind,ind+vtop->c.i,0);
如(x) {
如 (vtop->r & VT_SYM) {
/* relocation case */
greloc(cur_text_section, vtop->sym, ind, R_ARM_PC24);
} 另
put_elf_reloc(symtab_section, cur_text_section, ind, R_ARM_PC24, 0);
o(x|(is_jmp?0xE0000000:0xE1000000));
} 另 {
如(!is_jmp)
o(0xE28FE004); // add lr,pc,#4
o(0xE51FF004); // ldr pc,[pc,#-4]
如 (vtop->r & VT_SYM)
greloc(cur_text_section, vtop->sym, ind, R_ARM_ABS32);
o(vtop->c.i);
}
} 另 {
/* otherwise, indirect call */
r = gv(RC_INT);
如(!is_jmp)
o(0xE1A0E00F); // mov lr,pc
o(0xE1A0F000|intr(r)); // mov pc,r
}
}
静态 整 unalias_ldbl(整 btype)
{
#如 LDOUBLE_SIZE == 8
如 (btype == VT_LDOUBLE)
btype = VT_DOUBLE;
#了如
返回 btype;
}
/* Return whether a structure is an homogeneous float aggregate or not.
The answer is true if all the elements of the structure are of the same
primitive float type and there is less than 4 elements.
type: the type corresponding to the structure to be tested */
静态 整 is_hgen_float_aggr(CType *type)
{
如 ((type->t & VT_BTYPE) == VT_STRUCT) {
结构 Sym *ref;
整 btype, nb_fields = 0;
ref = type->ref->next;
btype = unalias_ldbl(ref->type.t & VT_BTYPE);
如 (btype == VT_FLOAT || btype == VT_DOUBLE) {
对于(; ref && btype == unalias_ldbl(ref->type.t & VT_BTYPE); ref = ref->next, nb_fields++);
返回 !ref && nb_fields <= 4;
}
}
返回 0;
}
结构 avail_regs {
有符 字 avail[3]; /* 3 holes max with only float and double alignments */
整 first_hole; /* first available hole */
整 last_hole; /* last available hole (none if equal to first_hole) */
整 first_free_reg; /* next free register in the sequence, hole excluded */
};
#定义 AVAIL_REGS_INITIALIZER (结构 avail_regs) { { 0, 0, 0}, 0, 0, 0 }
/* Find suitable registers for a VFP Co-Processor Register Candidate (VFP CPRC
param) according to the rules described in the procedure call standard for
the ARM architecture (AAPCS). If found, the registers are assigned to this
VFP CPRC parameter. Registers are allocated in sequence unless a hole exists
and the parameter is a single float.
avregs: opaque structure to keep track of available VFP co-processor regs
align: alignment constraints for the param, as returned by type_size()
size: size of the parameter, as returned by type_size() */
整 assign_vfpreg(结构 avail_regs *avregs, 整 align, 整 size)
{
整 first_reg = 0;
如 (avregs->first_free_reg == -1)
返回 -1;
如 (align >> 3) { /* double alignment */
first_reg = avregs->first_free_reg;
/* alignment constraint not respected so use next reg and record hole */
如 (first_reg & 1)
avregs->avail[avregs->last_hole++] = first_reg++;
} 另 { /* no special alignment (float or array of float) */
/* if single float and a hole is available, assign the param to it */
如 (size == 4 && avregs->first_hole != avregs->last_hole)
返回 avregs->avail[avregs->first_hole++];
另
first_reg = avregs->first_free_reg;
}
如 (first_reg + size / 4 <= 16) {
avregs->first_free_reg = first_reg + size / 4;
返回 first_reg;
}
avregs->first_free_reg = -1;
返回 -1;
}
/* Returns whether all params need to be passed in core registers or not.
This is the case for function part of the runtime ABI. */
整 floats_in_core_regs(SValue *sval)
{
如 (!sval->sym)
返回 0;
转接 (sval->sym->v) {
事例 TOK___floatundisf:
事例 TOK___floatundidf:
事例 TOK___fixunssfdi:
事例 TOK___fixunsdfdi:
#如未定义 TCC_ARM_VFP
事例 TOK___fixunsxfdi:
#了如
事例 TOK___floatdisf:
事例 TOK___floatdidf:
事例 TOK___fixsfdi:
事例 TOK___fixdfdi:
返回 1;
缺省:
返回 0;
}
}
/* Return the number of registers needed to return the struct, or 0 if
returning via struct pointer. */
ST_FUNC 整 gfunc_sret(CType *vt, 整 variadic, CType *ret, 整 *ret_align, 整 *regsize) {
#如定义 TCC_ARM_EABI
整 size, align;
size = type_size(vt, &align);
如 (float_abi == ARM_HARD_FLOAT && !variadic &&
(is_float(vt->t) || is_hgen_float_aggr(vt))) {
*ret_align = 8;
*regsize = 8;
ret->ref = NULL;
ret->t = VT_DOUBLE;
返回 (size + 7) >> 3;
} 另 如 (size <= 4) {
*ret_align = 4;
*regsize = 4;
ret->ref = NULL;
ret->t = VT_INT;
返回 1;
} 另
返回 0;
#另
返回 0;
#了如
}
/* Parameters are classified according to how they are copied to their final
destination for the function call. Because the copying is performed class
after class according to the order in the union below, it is important that
some constraints about the order of the members of this union are respected:
- CORE_STRUCT_CLASS must come after STACK_CLASS;
- CORE_CLASS must come after STACK_CLASS, CORE_STRUCT_CLASS and
VFP_STRUCT_CLASS;
- VFP_STRUCT_CLASS must come after VFP_CLASS.
See the comment for the main loop in copy_params() for the reason. */
枚举 reg_class {
STACK_CLASS = 0,
CORE_STRUCT_CLASS,
VFP_CLASS,
VFP_STRUCT_CLASS,
CORE_CLASS,
NB_CLASSES
};
结构 param_plan {
整 start; /* first reg or addr used depending on the class */
整 end; /* last reg used or next free addr depending on the class */
SValue *sval; /* pointer to SValue on the value stack */
结构 param_plan *prev; /* previous element in this class */
};
结构 plan {
结构 param_plan *pplans; /* array of all the param plans */
结构 param_plan *clsplans[NB_CLASSES]; /* per class lists of param plans */
};
#定义 add_param_plan(plan,pplan,class) \
运行 { \
pplan.prev = plan->clsplans[class]; \
plan->pplans[plan ## _nb] = pplan; \
plan->clsplans[class] = &plan->pplans[plan ## _nb++]; \
} 当(0)
/* Assign parameters to registers and stack with alignment according to the
rules in the procedure call standard for the ARM architecture (AAPCS).
The overall assignment is recorded in an array of per parameter structures
called parameter plans. The parameter plans are also further organized in a
number of linked lists, one per class of parameter (see the comment for the
definition of union reg_class).
nb_args: number of parameters of the function for which a call is generated
float_abi: float ABI in use for this function call
plan: the structure where the overall assignment is recorded
todo: a bitmap that record which core registers hold a parameter
Returns the amount of stack space needed for parameter passing
Note: this function allocated an array in plan->pplans with tcc_malloc. It
is the responsibility of the caller to free this array once used (ie not
before copy_params). */
静态 整 assign_regs(整 nb_args, 整 float_abi, 结构 plan *plan, 整 *todo)
{
整 i, size, align;
整 ncrn /* next core register number */, nsaa /* next stacked argument address*/;
整 plan_nb = 0;
结构 param_plan pplan;
结构 avail_regs avregs = AVAIL_REGS_INITIALIZER;
ncrn = nsaa = 0;
*todo = 0;
plan->pplans = tcc_malloc(nb_args * 求长度(*plan->pplans));
memset(plan->clsplans, 0, 求长度(plan->clsplans));
对于(i = nb_args; i-- ;) {
整 j, start_vfpreg = 0;
CType type = vtop[-i].type;
type.t &= ~VT_ARRAY;
size = type_size(&type, &align);
size = (size + 3) & ~3;
align = (align + 3) & ~3;
转接(vtop[-i].type.t & VT_BTYPE) {
事例 VT_STRUCT:
事例 VT_FLOAT:
事例 VT_DOUBLE:
事例 VT_LDOUBLE:
如 (float_abi == ARM_HARD_FLOAT) {
整 is_hfa = 0; /* Homogeneous float aggregate */
如 (is_float(vtop[-i].type.t)
|| (is_hfa = is_hgen_float_aggr(&vtop[-i].type))) {
整 end_vfpreg;
start_vfpreg = assign_vfpreg(&avregs, align, size);
end_vfpreg = start_vfpreg + ((size - 1) >> 2);
如 (start_vfpreg >= 0) {
pplan = (结构 param_plan) {start_vfpreg, end_vfpreg, &vtop[-i]};
如 (is_hfa)
add_param_plan(plan, pplan, VFP_STRUCT_CLASS);
另
add_param_plan(plan, pplan, VFP_CLASS);
继续;
} 另
跳出;
}
}
ncrn = (ncrn + (align-1)/4) & ~((align/4) - 1);
如 (ncrn + size/4 <= 4 || (ncrn < 4 && start_vfpreg != -1)) {
/* The parameter is allocated both in core register and on stack. As
* such, it can be of either class: it would either be the last of
* CORE_STRUCT_CLASS or the first of STACK_CLASS. */
对于 (j = ncrn; j < 4 && j < ncrn + size / 4; j++)
*todo|=(1<<j);