History

Runtime lib functions

Yesterday I felt the urge to change a few things in TinyCC. This is the first and biggest change of all of them. - use __aeabi_*divmod functions in ARM EABI to make binaries depend solely on standardized library functions - refactor ARM floating point <-> integer conversion a bit - rename long long->float and shift library functions to correspond to the names used by GCC - compile more tokens conditionally to reduce the size of TinyCC The intention is primarily to allow users of the ARM target to use libgcc (which is usually available as a shared library) instead of libtcc1 (which can't be compiled for ARM due to lack of an inline assembler). Changing the EABI target to use the divmod functions in theory allows to use it without libtcc1 on any (not necessarily GCC based) ARM EABI system. Daniel

Daniel Glöckner committed 18 years ago

Commit 12265da1 parent 2c657f64 files · +140 −94Browse files

Unified · Split

4 files
  1. arm-gen.c +75 −41
  2. libtcc1.c +6 −6
  3. tcc.c +26 −17
  4. tcctok.h +33 −30
arm-gen.c+75−41
@@ -110,6 +110,13 @@ static int regmask(int r) { · 109 unmodified lines
110110 #define REG_LRET TREG_R1 /* second word return register (for long long) */
111111 #define REG_FRET TREG_F0 /* float return register */
112112
113+#ifdef TCC_ARM_EABI
114+#define TOK___divdi3 TOK___aeabi_ldivmod
115+#define TOK___moddi3 TOK___aeabi_ldivmod
116+#define TOK___udivdi3 TOK___aeabi_uldivmod
117+#define TOK___umoddi3 TOK___aeabi_uldivmod
118+#endif
119+
113120 /* defined if function parameters must be evaluated in reverse order */
114121 #define INVERT_FUNC_PARAMS
115122
@@ -119,6 +126,11 @@ static int regmask(int r) { · 3 unmodified lines
119126
120127 #if defined(TCC_ARM_EABI) && defined(TCC_ARM_VFP)
121128 static CType float_type, double_type, func_float_type, func_double_type;
129+#define func_ldouble_type func_double_type
130+#else
131+#define func_float_type func_old_type
132+#define func_double_type func_old_type
133+#define func_ldouble_type func_old_type
122134 #endif
123135
124136 /* pointer size, in bytes */
@@ -1083,6 +1095,7 @@ void gen_opi(int op) · 958 unmodified lines
10831095 {
10841096 int c, func = 0;
10851097 unsigned long opc = 0,r,fr;
1098+ unsigned short retreg = REG_IRET;
10861099
10871100 c=0;
10881101 switch(op) {
@@ -1151,11 +1164,21 @@ void gen_opi(int op) · 62 unmodified lines
11511164 c=3;
11521165 break;
11531166 case '%':
1167+#ifdef TCC_ARM_EABI
1168+ func=TOK___aeabi_idivmod;
1169+ retreg=REG_LRET;
1170+#else
11541171 func=TOK___modsi3;
1172+#endif
11551173 c=3;
11561174 break;
11571175 case TOK_UMOD:
1176+#ifdef TCC_ARM_EABI
1177+ func=TOK___aeabi_uidivmod;
1178+ retreg=REG_LRET;
1179+#else
11581180 func=TOK___umodsi3;
1181+#endif
11591182 c=3;
11601183 break;
11611184 case TOK_UMULL:
@@ -1230,7 +1253,7 @@ done: · 68 unmodified lines
12301253 vrott(3);
12311254 gfunc_call(2);
12321255 vpushi(0);
1233− vtop->r = REG_IRET;
1256+ vtop->r = retreg;
12341257 break;
12351258 default:
12361259 error("gen_opi %i unimplemented!",op);
@@ -1547,11 +1570,14 @@ void gen_opf(int op) · 310 unmodified lines
15471570
15481571 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
15491572 and 'long long' cases. */
1550−void gen_cvt_itof(int t)
1573+void gen_cvt_itof1(int t)
15511574 {
15521575 int r,r2,bt;
15531576 bt=vtop->type.t & VT_BTYPE;
15541577 if(bt == VT_INT || bt == VT_SHORT || bt == VT_BYTE) {
1578+#ifndef TCC_ARM_VFP
1579+ unsigned int dsize=0;
1580+#endif
15551581 r=intr(gv(RC_INT));
15561582 #ifdef TCC_ARM_VFP
15571583 r2=vfpr(vtop->r=get_reg(RC_FLOAT));
@@ -1562,10 +1588,12 @@ void gen_cvt_itof(int t) · 4 unmodified lines
15621588 o(0xEEB80A40|r2|T2CPR(t)); /* fYitoX*/
15631589 #else
15641590 r2=fpr(vtop->r=get_reg(RC_FLOAT));
1565− o(0xEE000190|(r2<<16)|(r<<12));
1591+ if((t & VT_BTYPE) != VT_FLOAT)
1592+ dsize=0x80; /* flts -> fltd */
1593+ o(0xEE000110|dsize|(r2<<16)|(r<<12)); /* flts */
15661594 if((vtop->type.t & (VT_UNSIGNED|VT_BTYPE)) == (VT_UNSIGNED|VT_INT)) {
15671595 unsigned int off=0;
1568− o(0xE3500000|(r<<12));
1596+ o(0xE3500000|(r<<12)); /* cmp */
15691597 r=fpr(get_reg(RC_FLOAT));
15701598 if(last_itod_magic) {
15711599 off=ind+8-last_itod_magic;
@@ -1573,44 +1601,50 @@ void gen_cvt_itof(int t) · 1 unmodified line
15731601 if(off>255)
15741602 off=0;
15751603 }
1576− o(0xBD1F8100|(r<<12)|off);
1604+ o(0xBD1F0100|(r<<12)|off); /* ldflts */
15771605 if(!off) {
1578− o(0xEA000001);
1606+ o(0xEA000000); /* b */
15791607 last_itod_magic=ind;
1580− o(0x41F00000);
1581− o(0);
1608+ o(0x4F800000); /* 4294967296.0f */
15821609 }
1583− o(0xBE000180|(r2<<16)|(r2<<12)|r);
1610+ o(0xBE000100|dsize|(r2<<16)|(r2<<12)|r); /* adflt */
15841611 }
15851612 #endif
15861613 return;
15871614 } else if(bt == VT_LLONG) {
15881615 int func;
1589− CType *func_type = &func_old_type;
1590−#ifdef TCC_ARM_VFP
1591−#ifdef TCC_ARM_EABI
1592− func_type = &func_double_type;
1593−#endif
1616+ CType *func_type = 0;
15941617 if((t & VT_BTYPE) == VT_FLOAT) {
1595−#ifdef TCC_ARM_EABI
15961618 func_type = &func_float_type;
1597−#endif
15981619 if(vtop->type.t & VT_UNSIGNED)
1599− func=TOK___ulltof;
1620+ func=TOK___floatundisf;
16001621 else
1601− func=TOK___slltof;
1602− } else
1622+ func=TOK___floatdisf;
1623+#if LDOUBLE_SIZE != 8
1624+ } else if((t & VT_BTYPE) == VT_LDOUBLE) {
1625+ func_type = &func_ldouble_type;
1626+ if(vtop->type.t & VT_UNSIGNED)
1627+ func=TOK___floatundixf;
1628+ else
1629+ func=TOK___floatdixf;
1630+ } else if((t & VT_BTYPE) == VT_DOUBLE) {
1631+#else
1632+ } else if((t & VT_BTYPE) == VT_DOUBLE || (t & VT_BTYPE) == VT_LDOUBLE) {
16031633 #endif
1604− if(vtop->type.t & VT_UNSIGNED)
1605− func=TOK___ulltold;
1606− else
1607− func=TOK___slltold;
1608− vpush_global_sym(func_type, func);
1609− vswap();
1610− gfunc_call(1);
1611− vpushi(0);
1612− vtop->r=TREG_F0;
1613− return;
1634+ func_type = &func_double_type;
1635+ if(vtop->type.t & VT_UNSIGNED)
1636+ func=TOK___floatundidf;
1637+ else
1638+ func=TOK___floatdidf;
1639+ }
1640+ if(func_type) {
1641+ vpush_global_sym(func_type, func);
1642+ vswap();
1643+ gfunc_call(1);
1644+ vpushi(0);
1645+ vtop->r=TREG_F0;
1646+ return;
1647+ }
16141648 }
16151649 error("unimplemented gen_cvt_itof %x!",vtop->type.t);
16161650 }
@@ -1634,33 +1668,33 @@ void gen_cvt_ftoi(int t) · 17 unmodified lines
16341668 if(u) {
16351669 if(r2 == VT_FLOAT)
16361670 func=TOK___fixunssfsi;
1637− else if(r2 == VT_DOUBLE)
1638− func=TOK___fixunsdfsi;
1671+#if LDOUBLE_SIZE != 8
16391672 else if(r2 == VT_LDOUBLE)
1640−#if LDOUBLE_SIZE == 8
1641− func=TOK___fixunsdfsi;
1642−#else
16431673 func=TOK___fixunsxfsi;
1674+ else if(r2 == VT_DOUBLE)
1675+#else
1676+ else if(r2 == VT_LDOUBLE || r2 == VT_DOUBLE)
16441677 #endif
1678+ func=TOK___fixunsdfsi;
16451679 } else {
16461680 r=fpr(gv(RC_FLOAT));
16471681 r2=intr(vtop->r=get_reg(RC_INT));
16481682 o(0xEE100170|(r2<<12)|r);
1649− return;
1683+ return;
16501684 }
16511685 #endif
16521686 } else if(t == VT_LLONG) { // unsigned handled in gen_cvt_ftoi1
16531687 if(r2 == VT_FLOAT)
16541688 func=TOK___fixsfdi;
1655− else if(r2 == VT_DOUBLE)
1656− func=TOK___fixdfdi;
1689+#if LDOUBLE_SIZE != 8
16571690 else if(r2 == VT_LDOUBLE)
1658−#if LDOUBLE_SIZE == 8
1659− func=TOK___fixdfdi;
1660−#else
16611691 func=TOK___fixxfdi;
1692+ else if(r2 == VT_DOUBLE)
1693+#else
1694+ else if(r2 == VT_LDOUBLE || r2 == VT_DOUBLE)
16621695 #endif
1663− }
1696+ func=TOK___fixdfdi;
1697+ }
16641698 if(func) {
16651699 vpush_global_sym(&func_old_type, func);
16661700 vswap();
libtcc1.c+6−6
@@ -419,7 +419,7 @@ unsigned long long __umoddi3(unsigned long long u, unsigned long long v) · 418 unmodified lines
419419 }
420420
421421 /* XXX: fix tcc's code generator to do this instead */
422−long long __sardi3(long long a, int b)
422+long long __ashrdi3(long long a, int b)
423423 {
424424 #ifdef __TINYC__
425425 DWunion u;
@@ -438,7 +438,7 @@ long long __sardi3(long long a, int b) · 12 unmodified lines
438438 }
439439
440440 /* XXX: fix tcc's code generator to do this instead */
441−unsigned long long __shrdi3(unsigned long long a, int b)
441+unsigned long long __lshrdi3(unsigned long long a, int b)
442442 {
443443 #ifdef __TINYC__
444444 DWunion u;
@@ -457,7 +457,7 @@ unsigned long long __shrdi3(unsigned long long a, int b) · 12 unmodified lines
457457 }
458458
459459 /* XXX: fix tcc's code generator to do this instead */
460−long long __shldi3(long long a, int b)
460+long long __ashldi3(long long a, int b)
461461 {
462462 #ifdef __TINYC__
463463 DWunion u;
@@ -483,7 +483,7 @@ unsigned short __tcc_int_fpu_control = 0x137f | 0x0c00; · 19 unmodified lines
483483 #endif
484484
485485 /* XXX: fix tcc's code generator to do this instead */
486−float __ulltof(unsigned long long a)
486+float __floatundisf(unsigned long long a)
487487 {
488488 DWunion uu;
489489 XFtype r;
@@ -498,7 +498,7 @@ float __ulltof(unsigned long long a) · 8 unmodified lines
498498 }
499499 }
500500
501−double __ulltod(unsigned long long a)
501+double __floatundidf(unsigned long long a)
502502 {
503503 DWunion uu;
504504 XFtype r;
@@ -513,7 +513,7 @@ double __ulltod(unsigned long long a) · 8 unmodified lines
513513 }
514514 }
515515
516−long double __ulltold(unsigned long long a)
516+long double __floatundixf(unsigned long long a)
517517 {
518518 DWunion uu;
519519 XFtype r;
tcc.c+26−17
@@ -5261,6 +5261,8 @@ void gen_opl(int op) · 5260 unmodified lines
52615261 {
52625262 int t, a, b, op1, c, i;
52635263 int func;
5264+ unsigned short reg_iret = REG_IRET;
5265+ unsigned short reg_lret = REG_LRET;
52645266 SValue tmp;
52655267
52665268 switch(op) {
@@ -5273,17 +5275,22 @@ void gen_opl(int op) · 6 unmodified lines
52735275 goto gen_func;
52745276 case '%':
52755277 func = TOK___moddi3;
5276− goto gen_func;
5278+ goto gen_mod_func;
52775279 case TOK_UMOD:
52785280 func = TOK___umoddi3;
5281+ gen_mod_func:
5282+#ifdef TCC_ARM_EABI
5283+ reg_iret = TREG_R2;
5284+ reg_lret = TREG_R3;
5285+#endif
52795286 gen_func:
52805287 /* call generic long long function */
52815288 vpush_global_sym(&func_old_type, func);
52825289 vrott(3);
52835290 gfunc_call(2);
52845291 vpushi(0);
5285− vtop->r = REG_IRET;
5286− vtop->r2 = REG_LRET;
5292+ vtop->r = reg_iret;
5293+ vtop->r2 = reg_lret;
52875294 break;
52885295 case '^':
52895296 case '&':
@@ -5407,13 +5414,13 @@ void gen_opl(int op) · 117 unmodified lines
54075414 /* XXX: should provide a faster fallback on x86 ? */
54085415 switch(op) {
54095416 case TOK_SAR:
5410− func = TOK___sardi3;
5417+ func = TOK___ashrdi3;
54115418 goto gen_func;
54125419 case TOK_SHR:
5413− func = TOK___shrdi3;
5420+ func = TOK___lshrdi3;
54145421 goto gen_func;
54155422 case TOK_SHL:
5416− func = TOK___shldi3;
5423+ func = TOK___ashldi3;
54175424 goto gen_func;
54185425 }
54195426 }
@@ -5878,6 +5885,7 @@ void gen_op(int op) · 458 unmodified lines
58785885 }
58795886 }
58805887
5888+#ifndef TCC_TARGET_ARM
58815889 /* generic itof for unsigned long long case */
58825890 void gen_cvt_itof1(int t)
58835891 {
@@ -5885,11 +5893,13 @@ void gen_cvt_itof1(int t) · 1 unmodified line
58855893 (VT_LLONG | VT_UNSIGNED)) {
58865894
58875895 if (t == VT_FLOAT)
5888− vpush_global_sym(&func_old_type, TOK___ulltof);
5889− else if (t == VT_DOUBLE)
5890− vpush_global_sym(&func_old_type, TOK___ulltod);
5896+ vpush_global_sym(&func_old_type, TOK___floatundisf);
5897+#if LDOUBLE_SIZE != 8
5898+ else if (t == VT_LDOUBLE)
5899+ vpush_global_sym(&func_old_type, TOK___floatundixf);
5900+#endif
58915901 else
5892− vpush_global_sym(&func_old_type, TOK___ulltold);
5902+ vpush_global_sym(&func_old_type, TOK___floatundidf);
58935903 vrott(2);
58945904 gfunc_call(1);
58955905 vpushi(0);
@@ -5898,6 +5908,7 @@ void gen_cvt_itof1(int t) · 2 unmodified lines
58985908 gen_cvt_itof(t);
58995909 }
59005910 }
5911+#endif
59015912
59025913 /* generic ftoi for unsigned long long case */
59035914 void gen_cvt_ftoi1(int t)
@@ -5909,10 +5920,12 @@ void gen_cvt_ftoi1(int t) · 5 unmodified lines
59095920 st = vtop->type.t & VT_BTYPE;
59105921 if (st == VT_FLOAT)
59115922 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
5912− else if (st == VT_DOUBLE)
5913− vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
5914− else
5923+#if LDOUBLE_SIZE != 8
5924+ else if (st == VT_LDOUBLE)
59155925 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
5926+#endif
5927+ else
5928+ vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
59165929 vrott(2);
59175930 gfunc_call(1);
59185931 vpushi(0);
@@ -6020,11 +6033,7 @@ static void gen_cast(CType *type) · 101 unmodified lines
60206033 }
60216034 } else {
60226035 do_itof:
6023−#if !defined(TCC_TARGET_ARM)
60246036 gen_cvt_itof1(dbt);
6025−#else
6026− gen_cvt_itof(dbt);
6027−#endif
60286037 }
60296038 } else if (sf) {
60306039 /* convert fp to int */
tcctok.h+33−30
@@ -126,41 +126,42 @@ · 125 unmodified lines
126126 DEF(TOK_memcpy4, "__aeabi_memcpy4")
127127 DEF(TOK_memcpy8, "__aeabi_memcpy8")
128128 DEF(TOK_memset, "__aeabi_memset")
129+ DEF(TOK___aeabi_ldivmod, "__aeabi_ldivmod")
130+ DEF(TOK___aeabi_uldivmod, "__aeabi_uldivmod")
129131 #else
130132 DEF(TOK_memcpy, "memcpy")
131133 DEF(TOK_memset, "memset")
132−#endif
133134 DEF(TOK___divdi3, "__divdi3")
134135 DEF(TOK___moddi3, "__moddi3")
135136 DEF(TOK___udivdi3, "__udivdi3")
136137 DEF(TOK___umoddi3, "__umoddi3")
138+#endif
137139 #if defined(TCC_TARGET_ARM)
138− DEF(TOK___modsi3, "__modsi3")
139− DEF(TOK___umodsi3, "__umodsi3")
140140 #ifdef TCC_ARM_EABI
141+ DEF(TOK___aeabi_idivmod, "__aeabi_idivmod")
142+ DEF(TOK___aeabi_uidivmod, "__aeabi_uidivmod")
141143 DEF(TOK___divsi3, "__aeabi_idiv")
142144 DEF(TOK___udivsi3, "__aeabi_uidiv")
143− DEF(TOK___sardi3, "__aeabi_lasr")
144− DEF(TOK___shrdi3, "__aeabi_llsr")
145− DEF(TOK___shldi3, "__aeabi_llsl")
146− DEF(TOK___slltof, "__aeabi_l2f")
147− DEF(TOK___slltold, "__aeabi_l2d")
145+ DEF(TOK___floatdisf, "__aeabi_l2f")
146+ DEF(TOK___floatdidf, "__aeabi_l2d")
148147 DEF(TOK___fixsfdi, "__aeabi_f2lz")
149148 DEF(TOK___fixdfdi, "__aeabi_d2lz")
150− DEF(TOK___fixxfdi, "__aeabi_d2lz")
151149 #else
150+ DEF(TOK___modsi3, "__modsi3")
151+ DEF(TOK___umodsi3, "__umodsi3")
152152 DEF(TOK___divsi3, "__divsi3")
153153 DEF(TOK___udivsi3, "__udivsi3")
154− DEF(TOK___sardi3, "__ashrdi3")
155− DEF(TOK___shrdi3, "__lshrdi3")
156− DEF(TOK___shldi3, "__ashldi3")
157− DEF(TOK___slltold, "__slltold")
154+ DEF(TOK___floatdisf, "__floatdisf")
155+ DEF(TOK___floatdidf, "__floatdidf")
156+#ifndef TCC_ARM_VFP
157+ DEF(TOK___floatdixf, "__floatdixf")
158158 DEF(TOK___fixunssfsi, "__fixunssfsi")
159159 DEF(TOK___fixunsdfsi, "__fixunsdfsi")
160160 DEF(TOK___fixunsxfsi, "__fixunsxfsi")
161+ DEF(TOK___fixxfdi, "__fixxfdi")
162+#endif
161163 DEF(TOK___fixsfdi, "__fixsfdi")
162164 DEF(TOK___fixdfdi, "__fixdfdi")
163− DEF(TOK___fixxfdi, "__fixxfdi")
164165 #endif
165166 #elif defined(TCC_TARGET_C67)
166167 DEF(TOK__divi, "_divi")
@@ -169,33 +170,35 @@ · 2 unmodified lines
169170 DEF(TOK__divd, "_divd")
170171 DEF(TOK__remi, "_remi")
171172 DEF(TOK__remu, "_remu")
172− DEF(TOK___sardi3, "__sardi3")
173− DEF(TOK___shrdi3, "__shrdi3")
174− DEF(TOK___shldi3, "__shldi3")
175−#else
176− /* XXX: same names on i386 ? */
177− DEF(TOK___sardi3, "__sardi3")
178− DEF(TOK___shrdi3, "__shrdi3")
179− DEF(TOK___shldi3, "__shldi3")
180173 #endif
174+#ifdef TCC_TARGET_I386
181175 DEF(TOK___tcc_int_fpu_control, "__tcc_int_fpu_control")
182176 DEF(TOK___tcc_fpu_control, "__tcc_fpu_control")
177+#endif
183178 #ifdef TCC_ARM_EABI
184− DEF(TOK___ulltof, "__aeabi_ul2f")
185− DEF(TOK___ulltod, "__aeabi_ul2d")
186− DEF(TOK___ulltold, "__aeabi_ul2d")
179+ DEF(TOK___ashrdi3, "__aeabi_lasr")
180+ DEF(TOK___lshrdi3, "__aeabi_llsr")
181+ DEF(TOK___ashldi3, "__aeabi_llsl")
182+ DEF(TOK___floatundisf, "__aeabi_ul2f")
183+ DEF(TOK___floatundidf, "__aeabi_ul2d")
187184 DEF(TOK___fixunssfdi, "__aeabi_f2ulz")
188185 DEF(TOK___fixunsdfdi, "__aeabi_d2ulz")
189− DEF(TOK___fixunsxfdi, "__aeabi_d2ulz")
190186 #else
191− DEF(TOK___ulltof, "__ulltof")
192− DEF(TOK___ulltod, "__ulltod")
193− DEF(TOK___ulltold, "__ulltold")
187+ DEF(TOK___ashrdi3, "__ashrdi3")
188+ DEF(TOK___lshrdi3, "__lshrdi3")
189+ DEF(TOK___ashldi3, "__ashldi3")
190+ DEF(TOK___floatundisf, "__floatundisf")
191+ DEF(TOK___floatundidf, "__floatundidf")
192+#ifndef TCC_ARM_VFP
193+ DEF(TOK___floatundixf, "__floatundixf")
194+ DEF(TOK___fixunsxfdi, "__fixunsxfdi")
195+#endif
194196 DEF(TOK___fixunssfdi, "__fixunssfdi")
195197 DEF(TOK___fixunsdfdi, "__fixunsdfdi")
196− DEF(TOK___fixunsxfdi, "__fixunsxfdi")
197198 #endif
199+#ifdef TCC_TARGET_PE
198200 DEF(TOK___chkstk, "__chkstk")
201+#endif
199202
200203 /* bound checking symbols */
201204 #ifdef CONFIG_TCC_BCHECK

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t