History

added runtime library - fixed more relocations

bellard committed 24 years ago

Commit 6cdecbe1 parent 3d902af4 files · +678 −132Browse files

Unified · Split

4 files
  1. i386-gen.c +26 −15
  2. libtcc1.c +556 −0
  3. tcc.c +57 −117
  4. tcctok.h +39 −0
i386-gen.c+26−15
@@ -822,27 +822,33 @@ void gen_cvt_ftof(int t) · 821 unmodified lines
822822 /* generate a bounded pointer addition */
823823 void gen_bounded_ptr_add(void)
824824 {
825− int addr;
825+ Sym *sym;
826+
826827 /* prepare fast i386 function call (args in eax and edx) */
827828 gv2(RC_EAX, RC_EDX);
828829 /* save all temporary registers */
829830 vtop -= 2;
830831 save_regs(0);
831832 /* do a fast function call */
832− addr = ind;
833− oad(0xe8, (int)__bound_ptr_add - ind - 5);
833+ sym = external_sym(TOK___bound_ptr_add, func_old_type, 0);
834+ greloc(cur_text_section, sym,
835+ ind + 1 - (int)cur_text_section->data, R_386_PC32);
836+ oad(0xe8, -4);
834837 /* returned pointer is in eax */
835838 vtop++;
836839 vtop->r = REG_EAX | VT_BOUNDED;
837− vtop->c.ul = addr; /* address of bounding function call point */
840+ /* address of bounding function call point */
841+ vtop->c.ptr = (cur_text_section->reloc->data_ptr - sizeof(Elf32_Rel));
838842 }
839843
840844 /* patch pointer addition in vtop so that pointer dereferencing is
841845 also tested */
842846 void gen_bounded_ptr_deref(void)
843847 {
844− void *func;
845− int size, align, addr;
848+ int func;
849+ int size, align;
850+ Elf32_Rel *rel;
851+ Sym *sym;
846852
847853 size = 0;
848854 /* XXX: put that code in generic part of tcc */
@@ -855,20 +861,25 @@ void gen_bounded_ptr_deref(void) · 6 unmodified lines
855861 if (!size)
856862 size = type_size(vtop->t, &align);
857863 switch(size) {
858− case 1: func = __bound_ptr_indir1; break;
859− case 2: func = __bound_ptr_indir2; break;
860− case 4: func = __bound_ptr_indir4; break;
861− case 8: func = __bound_ptr_indir8; break;
862− case 12: func = __bound_ptr_indir12; break;
863− case 16: func = __bound_ptr_indir16; break;
864+ case 1: func = TOK___bound_ptr_indir1; break;
865+ case 2: func = TOK___bound_ptr_indir2; break;
866+ case 4: func = TOK___bound_ptr_indir4; break;
867+ case 8: func = TOK___bound_ptr_indir8; break;
868+ case 12: func = TOK___bound_ptr_indir12; break;
869+ case 16: func = TOK___bound_ptr_indir16; break;
864870 default:
865871 error("unhandled size when derefencing bounded pointer");
866− func = NULL;
872+ func = 0;
867873 break;
868874 }
869875
870− addr = vtop->c.ul;
871− *(int *)(addr + 1) = (int)func - addr - 5;
876+ /* patch relocation */
877+ /* XXX: find a better solution ? */
878+ rel = vtop->c.ptr;
879+ sym = external_sym(func, func_old_type, 0);
880+ if (!sym->c)
881+ put_extern_sym(sym, NULL, 0);
882+ rel->r_info = ELF32_R_INFO(sym->c, ELF32_R_TYPE(rel->r_info));
872883 }
873884 #endif
874885
libtcc1.c+556−0
@@ -0,0 +1,556 @@
1+/* TCC runtime library.
2+ Parts of this code are (c) 2002 Fabrice Bellard
3+
4+ Copyright (C) 1987, 1988, 1992, 1994, 1995 Free Software Foundation, Inc.
5+
6+This file is free software; you can redistribute it and/or modify it
7+under the terms of the GNU General Public License as published by the
8+Free Software Foundation; either version 2, or (at your option) any
9+later version.
10+
11+In addition to the permissions in the GNU General Public License, the
12+Free Software Foundation gives you unlimited permission to link the
13+compiled version of this file into combinations with other programs,
14+and to distribute those combinations without any restriction coming
15+from the use of this file. (The General Public License restrictions
16+do apply in other respects; for example, they cover modification of
17+the file, and distribution when not linked into a combine
18+executable.)
19+
20+This file is distributed in the hope that it will be useful, but
21+WITHOUT ANY WARRANTY; without even the implied warranty of
22+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23+General Public License for more details.
24+
25+You should have received a copy of the GNU General Public License
26+along with this program; see the file COPYING. If not, write to
27+the Free Software Foundation, 59 Temple Place - Suite 330,
28+Boston, MA 02111-1307, USA.
29+*/
30+
31+#define W_TYPE_SIZE 32
32+#define BITS_PER_UNIT 8
33+
34+typedef int Wtype;
35+typedef unsigned int UWtype;
36+typedef unsigned int USItype;
37+typedef long long DWtype;
38+typedef unsigned long long UDWtype;
39+
40+struct DWstruct {
41+ Wtype low, high;
42+};
43+
44+typedef union
45+{
46+ struct DWstruct s;
47+ DWtype ll;
48+} DWunion;
49+
50+typedef long double XFtype;
51+#define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT)
52+#define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE)
53+
54+/* the following deal with IEEE single-precision numbers */
55+#define EXCESS 126
56+#define SIGNBIT 0x80000000
57+#define HIDDEN (1 << 23)
58+#define SIGN(fp) ((fp) & SIGNBIT)
59+#define EXP(fp) (((fp) >> 23) & 0xFF)
60+#define MANT(fp) (((fp) & 0x7FFFFF) | HIDDEN)
61+#define PACK(s,e,m) ((s) | ((e) << 23) | (m))
62+
63+/* the following deal with IEEE double-precision numbers */
64+#define EXCESSD 1022
65+#define HIDDEND (1 << 20)
66+#define EXPD(fp) (((fp.l.upper) >> 20) & 0x7FF)
67+#define SIGND(fp) ((fp.l.upper) & SIGNBIT)
68+#define MANTD(fp) (((((fp.l.upper) & 0xFFFFF) | HIDDEND) << 10) | \
69+ (fp.l.lower >> 22))
70+#define HIDDEND_LL ((long long)1 << 52)
71+#define MANTD_LL(fp) ((fp.ll & (HIDDEND_LL-1)) | HIDDEND_LL)
72+#define PACKD_LL(s,e,m) (((long long)((s)+((e)<<20))<<32)|(m))
73+
74+/* the following deal with x86 long double-precision numbers */
75+#define EXCESSLD 16382
76+#define EXPLD(fp) (fp.l.upper & 0x7fff)
77+#define SIGNLD(fp) ((fp.l.upper) & 0x8000)
78+
79+/* only for x86 */
80+union ldouble_long {
81+ long double ld;
82+ struct {
83+ unsigned long long lower;
84+ unsigned short upper;
85+ } l;
86+};
87+
88+union double_long {
89+ double d;
90+#if 1
91+ struct {
92+ unsigned long lower;
93+ long upper;
94+ } l;
95+#else
96+ struct {
97+ long upper;
98+ unsigned long lower;
99+ } l;
100+#endif
101+ long long ll;
102+};
103+
104+union float_long {
105+ float f;
106+ long l;
107+};
108+
109+/* XXX: use gcc/tcc intrinsic ? */
110+#if defined(__i386__)
111+#define sub_ddmmss(sh, sl, ah, al, bh, bl) \
112+ __asm__ ("subl %5,%1\n\tsbbl %3,%0" \
113+ : "=r" ((USItype) (sh)), \
114+ "=&r" ((USItype) (sl)) \
115+ : "0" ((USItype) (ah)), \
116+ "g" ((USItype) (bh)), \
117+ "1" ((USItype) (al)), \
118+ "g" ((USItype) (bl)))
119+#define umul_ppmm(w1, w0, u, v) \
120+ __asm__ ("mull %3" \
121+ : "=a" ((USItype) (w0)), \
122+ "=d" ((USItype) (w1)) \
123+ : "%0" ((USItype) (u)), \
124+ "rm" ((USItype) (v)))
125+#define udiv_qrnnd(q, r, n1, n0, dv) \
126+ __asm__ ("divl %4" \
127+ : "=a" ((USItype) (q)), \
128+ "=d" ((USItype) (r)) \
129+ : "0" ((USItype) (n0)), \
130+ "1" ((USItype) (n1)), \
131+ "rm" ((USItype) (dv)))
132+#define count_leading_zeros(count, x) \
133+ do { \
134+ USItype __cbtmp; \
135+ __asm__ ("bsrl %1,%0" \
136+ : "=r" (__cbtmp) : "rm" ((USItype) (x))); \
137+ (count) = __cbtmp ^ 31; \
138+ } while (0)
139+#else
140+#error unsupported CPU type
141+#endif
142+
143+/* most of this code is taken from libgcc2.c from gcc */
144+
145+static UDWtype __udivmoddi4 (UDWtype n, UDWtype d, UDWtype *rp)
146+{
147+ DWunion ww;
148+ DWunion nn, dd;
149+ DWunion rr;
150+ UWtype d0, d1, n0, n1, n2;
151+ UWtype q0, q1;
152+ UWtype b, bm;
153+
154+ nn.ll = n;
155+ dd.ll = d;
156+
157+ d0 = dd.s.low;
158+ d1 = dd.s.high;
159+ n0 = nn.s.low;
160+ n1 = nn.s.high;
161+
162+#if !UDIV_NEEDS_NORMALIZATION
163+ if (d1 == 0)
164+ {
165+ if (d0 > n1)
166+ {
167+ /* 0q = nn / 0D */
168+
169+ udiv_qrnnd (q0, n0, n1, n0, d0);
170+ q1 = 0;
171+
172+ /* Remainder in n0. */
173+ }
174+ else
175+ {
176+ /* qq = NN / 0d */
177+
178+ if (d0 == 0)
179+ d0 = 1 / d0; /* Divide intentionally by zero. */
180+
181+ udiv_qrnnd (q1, n1, 0, n1, d0);
182+ udiv_qrnnd (q0, n0, n1, n0, d0);
183+
184+ /* Remainder in n0. */
185+ }
186+
187+ if (rp != 0)
188+ {
189+ rr.s.low = n0;
190+ rr.s.high = 0;
191+ *rp = rr.ll;
192+ }
193+ }
194+
195+#else /* UDIV_NEEDS_NORMALIZATION */
196+
197+ if (d1 == 0)
198+ {
199+ if (d0 > n1)
200+ {
201+ /* 0q = nn / 0D */
202+
203+ count_leading_zeros (bm, d0);
204+
205+ if (bm != 0)
206+ {
207+ /* Normalize, i.e. make the most significant bit of the
208+ denominator set. */
209+
210+ d0 = d0 << bm;
211+ n1 = (n1 << bm) | (n0 >> (W_TYPE_SIZE - bm));
212+ n0 = n0 << bm;
213+ }
214+
215+ udiv_qrnnd (q0, n0, n1, n0, d0);
216+ q1 = 0;
217+
218+ /* Remainder in n0 >> bm. */
219+ }
220+ else
221+ {
222+ /* qq = NN / 0d */
223+
224+ if (d0 == 0)
225+ d0 = 1 / d0; /* Divide intentionally by zero. */
226+
227+ count_leading_zeros (bm, d0);
228+
229+ if (bm == 0)
230+ {
231+ /* From (n1 >= d0) /\ (the most significant bit of d0 is set),
232+ conclude (the most significant bit of n1 is set) /\ (the
233+ leading quotient digit q1 = 1).
234+
235+ This special case is necessary, not an optimization.
236+ (Shifts counts of W_TYPE_SIZE are undefined.) */
237+
238+ n1 -= d0;
239+ q1 = 1;
240+ }
241+ else
242+ {
243+ /* Normalize. */
244+
245+ b = W_TYPE_SIZE - bm;
246+
247+ d0 = d0 << bm;
248+ n2 = n1 >> b;
249+ n1 = (n1 << bm) | (n0 >> b);
250+ n0 = n0 << bm;
251+
252+ udiv_qrnnd (q1, n1, n2, n1, d0);
253+ }
254+
255+ /* n1 != d0... */
256+
257+ udiv_qrnnd (q0, n0, n1, n0, d0);
258+
259+ /* Remainder in n0 >> bm. */
260+ }
261+
262+ if (rp != 0)
263+ {
264+ rr.s.low = n0 >> bm;
265+ rr.s.high = 0;
266+ *rp = rr.ll;
267+ }
268+ }
269+#endif /* UDIV_NEEDS_NORMALIZATION */
270+
271+ else
272+ {
273+ if (d1 > n1)
274+ {
275+ /* 00 = nn / DD */
276+
277+ q0 = 0;
278+ q1 = 0;
279+
280+ /* Remainder in n1n0. */
281+ if (rp != 0)
282+ {
283+ rr.s.low = n0;
284+ rr.s.high = n1;
285+ *rp = rr.ll;
286+ }
287+ }
288+ else
289+ {
290+ /* 0q = NN / dd */
291+
292+ count_leading_zeros (bm, d1);
293+ if (bm == 0)
294+ {
295+ /* From (n1 >= d1) /\ (the most significant bit of d1 is set),
296+ conclude (the most significant bit of n1 is set) /\ (the
297+ quotient digit q0 = 0 or 1).
298+
299+ This special case is necessary, not an optimization. */
300+
301+ /* The condition on the next line takes advantage of that
302+ n1 >= d1 (true due to program flow). */
303+ if (n1 > d1 || n0 >= d0)
304+ {
305+ q0 = 1;
306+ sub_ddmmss (n1, n0, n1, n0, d1, d0);
307+ }
308+ else
309+ q0 = 0;
310+
311+ q1 = 0;
312+
313+ if (rp != 0)
314+ {
315+ rr.s.low = n0;
316+ rr.s.high = n1;
317+ *rp = rr.ll;
318+ }
319+ }
320+ else
321+ {
322+ UWtype m1, m0;
323+ /* Normalize. */
324+
325+ b = W_TYPE_SIZE - bm;
326+
327+ d1 = (d1 << bm) | (d0 >> b);
328+ d0 = d0 << bm;
329+ n2 = n1 >> b;
330+ n1 = (n1 << bm) | (n0 >> b);
331+ n0 = n0 << bm;
332+
333+ udiv_qrnnd (q0, n1, n2, n1, d1);
334+ umul_ppmm (m1, m0, q0, d0);
335+
336+ if (m1 > n1 || (m1 == n1 && m0 > n0))
337+ {
338+ q0--;
339+ sub_ddmmss (m1, m0, m1, m0, d1, d0);
340+ }
341+
342+ q1 = 0;
343+
344+ /* Remainder in (n1n0 - m1m0) >> bm. */
345+ if (rp != 0)
346+ {
347+ sub_ddmmss (n1, n0, n1, n0, m1, m0);
348+ rr.s.low = (n1 << b) | (n0 >> bm);
349+ rr.s.high = n1 >> bm;
350+ *rp = rr.ll;
351+ }
352+ }
353+ }
354+ }
355+
356+ ww.s.low = q0;
357+ ww.s.high = q1;
358+ return ww.ll;
359+}
360+
361+#define __negdi2(a) (-(a))
362+
363+long long __divdi3(long long u, long long v)
364+{
365+ int c = 0;
366+ DWunion uu, vv;
367+ DWtype w;
368+
369+ uu.ll = u;
370+ vv.ll = v;
371+
372+ if (uu.s.high < 0) {
373+ c = ~c;
374+ uu.ll = __negdi2 (uu.ll);
375+ }
376+ if (vv.s.high < 0) {
377+ c = ~c;
378+ vv.ll = __negdi2 (vv.ll);
379+ }
380+ w = __udivmoddi4 (uu.ll, vv.ll, (UDWtype *) 0);
381+ if (c)
382+ w = __negdi2 (w);
383+ return w;
384+}
385+
386+long long __moddi3(long long u, long long v)
387+{
388+ int c = 0;
389+ DWunion uu, vv;
390+ DWtype w;
391+
392+ uu.ll = u;
393+ vv.ll = v;
394+
395+ if (uu.s.high < 0) {
396+ c = ~c;
397+ uu.ll = __negdi2 (uu.ll);
398+ }
399+ if (vv.s.high < 0)
400+ vv.ll = __negdi2 (vv.ll);
401+
402+ __udivmoddi4 (uu.ll, vv.ll, &w);
403+ if (c)
404+ w = __negdi2 (w);
405+ return w;
406+}
407+
408+unsigned long long __udivdi3(unsigned long long u, unsigned long long v)
409+{
410+ return __udivmoddi4 (u, v, (UDWtype *) 0);
411+}
412+
413+unsigned long long __umoddi3(unsigned long long u, unsigned long long v)
414+{
415+ UDWtype w;
416+
417+ __udivmoddi4 (u, v, &w);
418+ return w;
419+}
420+
421+/* XXX: suppress that and patch tcc to do it */
422+long long __sardi3(long long a, int b)
423+{
424+ return a >> b;
425+}
426+
427+/* XXX: suppress that and patch tcc to do it */
428+unsigned long long __shrdi3(unsigned long long a, int b)
429+{
430+ return a >> b;
431+}
432+
433+/* XXX: suppress that and patch tcc to do it */
434+long long __shldi3(long long a, int b)
435+{
436+ return a << b;
437+}
438+
439+/* XXX: suppress that and patch tcc to do it */
440+float __ulltof(unsigned long long a)
441+{
442+ DWunion uu;
443+ XFtype r;
444+
445+ uu.ll = a;
446+ if (uu.s.high >= 0) {
447+ return (float)uu.ll;
448+ } else {
449+ r = (XFtype)uu.ll;
450+ r += 18446744073709551616.0;
451+ return (float)r;
452+ }
453+}
454+
455+double __ulltod(unsigned long long a)
456+{
457+ DWunion uu;
458+ XFtype r;
459+
460+ uu.ll = a;
461+ if (uu.s.high >= 0) {
462+ return (double)uu.ll;
463+ } else {
464+ r = (XFtype)uu.ll;
465+ r += 18446744073709551616.0;
466+ return (double)r;
467+ }
468+}
469+
470+long double __ulltold(unsigned long long a)
471+{
472+ DWunion uu;
473+ XFtype r;
474+
475+ uu.ll = a;
476+ if (uu.s.high >= 0) {
477+ return (long double)uu.ll;
478+ } else {
479+ r = (XFtype)uu.ll;
480+ r += 18446744073709551616.0;
481+ return (long double)r;
482+ }
483+}
484+
485+unsigned long long __fixunssfdi (float a1)
486+{
487+ register union float_long fl1;
488+ register int exp;
489+ register unsigned long l;
490+
491+ fl1.f = a1;
492+
493+ if (fl1.l == 0)
494+ return (0);
495+
496+ exp = EXP (fl1.l) - EXCESS - 24;
497+
498+ l = MANT(fl1.l);
499+ if (exp >= 41)
500+ return (unsigned long long)-1;
501+ else if (exp >= 0)
502+ return (unsigned long long)l << exp;
503+ else if (exp >= -23)
504+ return l >> -exp;
505+ else
506+ return 0;
507+}
508+
509+unsigned long long __fixunsdfdi (double a1)
510+{
511+ register union double_long dl1;
512+ register int exp;
513+ register unsigned long long l;
514+
515+ dl1.d = a1;
516+
517+ if (dl1.ll == 0)
518+ return (0);
519+
520+ exp = EXPD (dl1) - EXCESSD - 53;
521+
522+ l = MANTD_LL(dl1);
523+
524+ if (exp >= 12)
525+ return (unsigned long long)-1;
526+ else if (exp >= 0)
527+ return l << exp;
528+ else if (exp >= -52)
529+ return l >> -exp;
530+ else
531+ return 0;
532+}
533+
534+unsigned long long __fixunsxfdi (long double a1)
535+{
536+ register union ldouble_long dl1;
537+ register int exp;
538+ register unsigned long long l;
539+
540+ dl1.ld = a1;
541+
542+ if (dl1.l.lower == 0 && dl1.l.upper == 0)
543+ return (0);
544+
545+ exp = EXPLD (dl1) - EXCESSLD - 64;
546+
547+ l = dl1.l.lower;
548+
549+ if (exp > 0)
550+ return (unsigned long long)-1;
551+ else if (exp >= -63)
552+ return l >> -exp;
553+ else
554+ return 0;
555+}
556+
tcc.c+57−117
@@ -100,6 +100,7 @@ typedef union CValue { · 99 unmodified lines
100100 unsigned long long ull;
101101 struct TokenSym *ts;
102102 struct Sym *sym;
103+ void *ptr;
103104 int tab[1];
104105 } CValue;
105106
@@ -284,6 +285,7 @@ int ifdef_stack[IFDEF_STACK_SIZE], *ifdef_stack_ptr; · 178 unmodified lines
284285 char **include_paths;
285286 int nb_include_paths;
286287 int char_pointer_type;
288+int func_old_type;
287289
288290 /* compile with debug symbol (and use them if error during execution) */
289291 int do_debug = 0;
@@ -493,21 +495,9 @@ enum { · 203 unmodified lines
493495 /* special identifiers */
494496 TOK___FUNC__,
495497 TOK_MAIN,
496− /* attribute identifiers */
497− TOK_SECTION,
498− TOK___SECTION__,
499− TOK_ALIGNED,
500− TOK___ALIGNED__,
501− TOK_UNUSED,
502− TOK___UNUSED__,
503− TOK_CDECL,
504− TOK___CDECL,
505− TOK___CDECL__,
506− TOK_STDCALL,
507− TOK___STDCALL,
508− TOK___STDCALL__,
509− TOK_NORETURN,
510− TOK___NORETURN__,
498+#define DEF(id, str) id,
499+#include "tcctok.h"
500+#undef DEF
511501 };
512502
513503 char *tcc_keywords =
@@ -521,10 +511,10 @@ char *tcc_keywords = · 7 unmodified lines
521511 "defined\0undef\0error\0line\0"
522512 "__LINE__\0__FILE__\0__DATE__\0__TIME__\0__VA_ARGS__\0"
523513 "__func__\0main\0"
524−/* attributes */
525−"section\0__section__\0aligned\0__aligned__\0unused\0__unused__\0"
526−"cdecl\0__cdecl\0__cdecl__\0stdcall\0__stdcall\0__stdcall__\0"
527−"noreturn\0__noreturn__\0"
514+/* builtin functions */
515+#define DEF(id, str) str "\0"
516+#include "tcctok.h"
517+#undef DEF
528518 ;
529519
530520 #ifdef WIN32
@@ -595,6 +585,7 @@ void vset(int t, int r, int v); · 64 unmodified lines
595585 void type_to_str(char *buf, int buf_size,
596586 int t, const char *varstr);
597587 char *get_tok_str(int v, CValue *cv);
588+Sym *external_sym(int v, int u, int r);
598589
599590 /* section generation */
600591 void put_extern_sym(Sym *sym, Section *section, unsigned long value);
@@ -680,77 +671,6 @@ void *dlsym(void *handle, const char *symbol) · 79 unmodified lines
680671
681672 #endif
682673
683−/********************************************************/
684−/* runtime library is there */
685−/* XXX: we suppose that the host compiler handles 'long long'. It
686− would not be difficult to suppress this assumption */
687−
688−long long __divll(long long a, long long b)
689−{
690− return a / b;
691−}
692−
693−long long __modll(long long a, long long b)
694−{
695− return a % b;
696−}
697−
698−unsigned long long __divull(unsigned long long a, unsigned long long b)
699−{
700− return a / b;
701−}
702−
703−unsigned long long __modull(unsigned long long a, unsigned long long b)
704−{
705− return a % b;
706−}
707−
708−long long __sardi3(long long a, int b)
709−{
710− return a >> b;
711−}
712−
713−unsigned long long __shrdi3(unsigned long long a, int b)
714−{
715− return a >> b;
716−}
717−
718−long long __shldi3(long long a, int b)
719−{
720− return a << b;
721−}
722−
723−float __ulltof(unsigned long long a)
724−{
725− return (float)a;
726−}
727−
728−double __ulltod(unsigned long long a)
729−{
730− return (double)a;
731−}
732−
733−long double __ulltold(unsigned long long a)
734−{
735− return (long double)a;
736−}
737−
738−unsigned long long __ftoull(float a)
739−{
740− return (unsigned long long)a;
741−}
742−
743−unsigned long long __dtoull(double a)
744−{
745− return (unsigned long long)a;
746−}
747−
748−unsigned long long __ldtoull(long double a)
749−{
750− return (unsigned long long)a;
751−}
752−
753−
754674 /********************************************************/
755675
756676 /* we use our own 'finite' function to avoid potential problems with
@@ -2589,6 +2509,17 @@ void vpush_ref(int t, Section *sec, unsigned long offset) · 1832 unmodified lines
25892509 vsetc(t, VT_CONST | VT_SYM, &cval);
25902510 }
25912511
2512+/* push a reference to symbol v */
2513+void vpush_sym(int t, int v)
2514+{
2515+ Sym *sym;
2516+ CValue cval;
2517+
2518+ sym = external_sym(v, t, 0);
2519+ cval.sym = sym;
2520+ vsetc(t, VT_CONST | VT_SYM, &cval);
2521+}
2522+
25922523 void vset(int t, int r, int v)
25932524 {
25942525 CValue cval;
@@ -3019,29 +2950,29 @@ void gv_dup(void) · 424 unmodified lines
30192950 void gen_opl(int op)
30202951 {
30212952 int t, a, b, op1, c, i;
3022− void *func;
2953+ int func;
30232954 GFuncContext gf;
30242955 SValue tmp;
30252956
30262957 switch(op) {
30272958 case '/':
30282959 case TOK_PDIV:
3029− func = __divll;
2960+ func = TOK___divdi3;
30302961 goto gen_func;
30312962 case TOK_UDIV:
3032− func = __divull;
2963+ func = TOK___udivdi3;
30332964 goto gen_func;
30342965 case '%':
3035− func = __modll;
2966+ func = TOK___moddi3;
30362967 goto gen_func;
30372968 case TOK_UMOD:
3038− func = __modull;
2969+ func = TOK___umoddi3;
30392970 gen_func:
30402971 /* call generic long long function */
30412972 gfunc_start(&gf, FUNC_CDECL);
30422973 gfunc_param(&gf);
30432974 gfunc_param(&gf);
3044− vpushi((int)func);
2975+ vpush_sym(func_old_type, func);
30452976 gfunc_call(&gf);
30462977 vpushi(0);
30472978 vtop->r = REG_IRET;
@@ -3166,13 +3097,13 @@ void gen_opl(int op) · 118 unmodified lines
31663097 /* XXX: should provide a faster fallback on x86 ? */
31673098 switch(op) {
31683099 case TOK_SAR:
3169− func = __sardi3;
3100+ func = TOK___sardi3;
31703101 goto gen_func;
31713102 case TOK_SHR:
3172− func = __shrdi3;
3103+ func = TOK___shrdi3;
31733104 goto gen_func;
31743105 case TOK_SHL:
3175− func = __shldi3;
3106+ func = TOK___shldi3;
31763107 goto gen_func;
31773108 }
31783109 }
@@ -3561,11 +3492,11 @@ void gen_cvt_itof1(int t) · 382 unmodified lines
35613492 gfunc_start(&gf, FUNC_CDECL);
35623493 gfunc_param(&gf);
35633494 if (t == VT_FLOAT)
3564− vpushi((int)&__ulltof);
3495+ vpush_sym(func_old_type, TOK___ulltof);
35653496 else if (t == VT_DOUBLE)
3566− vpushi((int)&__ulltod);
3497+ vpush_sym(func_old_type, TOK___ulltod);
35673498 else
3568− vpushi((int)&__ulltold);
3499+ vpush_sym(func_old_type, TOK___ulltold);
35693500 gfunc_call(&gf);
35703501 vpushi(0);
35713502 vtop->r = REG_FRET;
@@ -3586,11 +3517,11 @@ void gen_cvt_ftoi1(int t) · 14 unmodified lines
35863517 st = vtop->t & VT_BTYPE;
35873518 gfunc_param(&gf);
35883519 if (st == VT_FLOAT)
3589− vpushi((int)&__ftoull);
3520+ vpush_sym(func_old_type, TOK___fixunssfdi);
35903521 else if (st == VT_DOUBLE)
3591− vpushi((int)&__dtoull);
3522+ vpush_sym(func_old_type, TOK___fixunsdfdi);
35923523 else
3593− vpushi((int)&__ldtoull);
3524+ vpush_sym(func_old_type, TOK___fixunsxfdi);
35943525 gfunc_call(&gf);
35953526 vpushi(0);
35963527 vtop->r = REG_IRET;
@@ -4049,7 +3980,7 @@ void vstore(void) · 452 unmodified lines
40493980 gfunc_param(&gf);
40503981
40513982 save_regs(0);
4052− vpushi((int)&memcpy);
3983+ vpush_sym(func_old_type, TOK_memcpy);
40533984 gfunc_call(&gf);
40543985 /* leave source on stack */
40553986 } else if (ft & VT_BITFIELD) {
@@ -4697,7 +4628,7 @@ void gfunc_param_typed(GFuncContext *gf, Sym *func, Sym *arg) · 641 unmodified lines
46974628
46984629 void unary(void)
46994630 {
4700− int n, t, ft, fc, p, align, size, r, data_offset;
4631+ int n, t, ft, fc, align, size, r, data_offset;
47014632 Sym *s;
47024633 GFuncContext gf;
47034634 AttributeDef ad;
@@ -4847,11 +4778,8 @@ void unary(void) · 143 unmodified lines
48474778 if (tok != '(')
48484779 error("'%s' undeclared", get_tok_str(t, NULL));
48494780 /* for simple function calls, we tolerate undeclared
4850− external reference */
4851− p = anon_sym++;
4852− sym_push1(&global_stack, p, 0, FUNC_OLD);
4853− /* int() function */
4854− s = external_sym(t, VT_FUNC | (p << VT_STRUCT_SHIFT), 0);
4781+ external reference to int() function */
4782+ s = external_sym(t, func_old_type, 0);
48554783 }
48564784 vset(s->t, s->r, s->c);
48574785 /* if forward reference, we must point to s */
@@ -5633,7 +5561,7 @@ void init_putz(int t, Section *sec, unsigned long c, int size) · 775 unmodified lines
56335561 gfunc_param(&gf);
56345562 vset(VT_INT, VT_LOCAL, c);
56355563 gfunc_param(&gf);
5636− vpushi((int)&memset);
5564+ vpush_sym(func_old_type, TOK_memset);
56375565 gfunc_call(&gf);
56385566 }
56395567 }
@@ -6190,7 +6118,8 @@ static int tcc_compile(TCCState *s) · 550 unmodified lines
61906118 {
61916119 Sym *define_start;
61926120 char buf[512];
6193−
6121+ int p;
6122+
61946123 funcname = "";
61956124 include_stack_ptr = include_stack;
61966125 ifdef_stack_ptr = ifdef_stack;
@@ -6215,6 +6144,10 @@ static int tcc_compile(TCCState *s) · 18 unmodified lines
62156144 /* define common 'char *' type because it is often used internally
62166145 for arrays and struct dereference */
62176146 char_pointer_type = mk_pointer(VT_BYTE);
6147+ /* define an old type function 'int func()' */
6148+ p = anon_sym++;
6149+ sym_push1(&global_stack, p, 0, FUNC_OLD);
6150+ func_old_type = VT_FUNC | (p << VT_STRUCT_SHIFT);
62186151
62196152 define_start = define_stack.top;
62206153 inp();
@@ -6807,8 +6740,6 @@ static void put32(unsigned char *p, unsigned int val) · 586 unmodified lines
68076740 static void build_got(void)
68086741 {
68096742 /* if no got, then create it */
6810− /* a got is needed even if static link for the symbol
6811− _GLOBAL_OFFSET_TABLE_ */
68126743 got = new_section(".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
68136744 got->sh_entsize = 4;
68146745 add_elf_sym(symtab_section, 0, 4, ELF32_ST_INFO(STB_GLOBAL, STT_OBJECT),
@@ -6933,6 +6864,12 @@ static void put_dt(Section *dynamic, int dt, unsigned long val) · 118 unmodified lines
69336864 dynamic->data_ptr += sizeof(Elf32_Dyn);
69346865 }
69356866
6867+/* add tcc runtime libraries */
6868+static void tcc_add_runtime(TCCState *s1)
6869+{
6870+ tcc_add_file(s1, CONFIG_TCC_PREFIX "/lib/tcc/libtcc1.o");
6871+}
6872+
69366873 /* add dynamic sections so that the executable is dynamically linked */
69376874 static char elf_interp[] = "/lib/ld-linux.so.2";
69386875
@@ -6963,8 +6900,9 @@ int tcc_output_file(TCCState *s1, const char *filename) · 24 unmodified lines
69636900
69646901 /* add libc crtn object */
69656902 if (file_type != TCC_OUTPUT_OBJ) {
6966− tcc_add_file(s1, CONFIG_TCC_CRT_PREFIX "/crtn.o");
6903+ tcc_add_runtime(s1);
69676904 tcc_add_library(s1, "c");
6905+ tcc_add_file(s1, CONFIG_TCC_CRT_PREFIX "/crtn.o");
69686906 }
69696907
69706908 interp = NULL;
@@ -8076,6 +8014,8 @@ int tcc_run(TCCState *s1, int argc, char **argv) · 1105 unmodified lines
80768014 int (*prog_main)(int, char **);
80778015 int i;
80788016
8017+ tcc_add_runtime(s1);
8018+
80798019 relocate_common_syms();
80808020
80818021 /* compute relocation address : section are relocated in place */
tcctok.h+39−0
@@ -0,0 +1,39 @@
1+/* attribute identifiers */
2+DEF(TOK_SECTION, "section")
3+DEF(TOK___SECTION__, "__section__")
4+DEF(TOK_ALIGNED, "aligned")
5+DEF(TOK___ALIGNED__, "__aligned__")
6+DEF(TOK_UNUSED, "unused")
7+DEF(TOK___UNUSED__, "__unused__")
8+DEF(TOK_CDECL, "cdecl")
9+DEF(TOK___CDECL, "__cdecl")
10+DEF(TOK___CDECL__, "__cdecl__")
11+DEF(TOK_STDCALL, "stdcall")
12+DEF(TOK___STDCALL, "__stdcall")
13+DEF(TOK___STDCALL__, "__stdcall__")
14+DEF(TOK_NORETURN, "noreturn")
15+DEF(TOK___NORETURN__, "__noreturn__")
16+
17+/* builtin functions */
18+DEF(TOK_memcpy, "memcpy")
19+DEF(TOK_memset, "memset")
20+DEF(TOK___divdi3, "__divdi3")
21+DEF(TOK___moddi3, "__moddi3")
22+DEF(TOK___udivdi3, "__udivdi3")
23+DEF(TOK___umoddi3, "__umoddi3")
24+DEF(TOK___sardi3, "__sardi3")
25+DEF(TOK___shrdi3, "__shrdi3")
26+DEF(TOK___shldi3, "__shldi3")
27+DEF(TOK___ulltof, "__ulltof")
28+DEF(TOK___ulltod, "__ulltod")
29+DEF(TOK___ulltold, "__ulltold")
30+DEF(TOK___fixunssfdi, "__fixunssfdi")
31+DEF(TOK___fixunsdfdi, "__fixunsdfdi")
32+DEF(TOK___fixunsxfdi, "__fixunsxfdi")
33+DEF(TOK___bound_ptr_add, "__bound_ptr_add")
34+DEF(TOK___bound_ptr_indir1, "__bound_ptr_indir1")
35+DEF(TOK___bound_ptr_indir2, "__bound_ptr_indir2")
36+DEF(TOK___bound_ptr_indir4, "__bound_ptr_indir4")
37+DEF(TOK___bound_ptr_indir8, "__bound_ptr_indir8")
38+DEF(TOK___bound_ptr_indir12, "__bound_ptr_indir12")
39+DEF(TOK___bound_ptr_indir16, "__bound_ptr_indir16")

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