1 /*
2  *  TCC - Tiny C Compiler
3  *
4  *  Copyright (c) 2001-2004 Fabrice Bellard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 
21 #ifndef _TCC_H
22 #define _TCC_H
23 
24 #define _GNU_SOURCE
25 #include "config.h"
26 
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <math.h>
33 #include <fcntl.h>
34 #include <setjmp.h>
35 #include <time.h>
36 
37 #ifndef _WIN32
38 # include <unistd.h>
39 # include <sys/time.h>
40 # ifndef CONFIG_TCC_STATIC
41 #  include <dlfcn.h>
42 # endif
43 /* XXX: need to define this to use them in non ISOC99 context */
44 extern float strtof (const char *__nptr, char **__endptr);
45 extern long double strtold (const char *__nptr, char **__endptr);
46 #endif
47 
48 #ifdef _WIN32
49 # define WIN32_LEAN_AND_MEAN
50 # include <windows.h>
51 # include <io.h> /* open, close etc. */
52 # include <direct.h> /* getcwd */
53 # ifdef __GNUC__
54 #  include <stdint.h>
55 # endif
56 # define inline __inline
57 # define snprintf _snprintf
58 # define vsnprintf _vsnprintf
59 # ifndef __GNUC__
60 #  define strtold (long double)strtod
61 #  define strtof (float)strtod
62 #  define strtoll _strtoi64
63 #  define strtoull _strtoui64
64 # endif
65 # ifdef LIBTCC_AS_DLL
66 #  define LIBTCCAPI __declspec(dllexport)
67 #  define PUB_FUNC LIBTCCAPI
68 # endif
69 # define inp next_inp /* inp is an intrinsic on msvc/mingw */
70 # ifdef _MSC_VER
71 #  pragma warning (disable : 4244)  // conversion from 'uint64_t' to 'int', possible loss of data
72 #  pragma warning (disable : 4267)  // conversion from 'size_t' to 'int', possible loss of data
73 #  pragma warning (disable : 4996)  // The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name
74 #  pragma warning (disable : 4018)  // signed/unsigned mismatch
75 #  pragma warning (disable : 4146)  // unary minus operator applied to unsigned type, result still unsigned
76 #  define ssize_t intptr_t
77 #  ifdef _X86_
78 #   define __i386__ 1
79 #  endif
80 #  ifdef _AMD64_
81 #   define __x86_64__ 1
82 #  endif
83 # endif
84 # undef CONFIG_TCC_STATIC
85 #endif
86 
87 #ifndef O_BINARY
88 # define O_BINARY 0
89 #endif
90 
91 #ifndef offsetof
92 #define offsetof(type, field) ((size_t) &((type *)0)->field)
93 #endif
94 
95 #ifndef countof
96 #define countof(tab) (sizeof(tab) / sizeof((tab)[0]))
97 #endif
98 
99 #ifdef _MSC_VER
100 # define NORETURN __declspec(noreturn)
101 # define ALIGNED(x) __declspec(align(x))
102 # define PRINTF_LIKE(x,y)
103 #else
104 # define NORETURN __attribute__((noreturn))
105 # define ALIGNED(x) __attribute__((aligned(x)))
106 # define PRINTF_LIKE(x,y) __attribute__ ((format (printf, (x), (y))))
107 #endif
108 
109 /* gnu headers use to #define __attribute__ to empty for non-gcc compilers */
110 #ifdef __TINYC__
111 # undef __attribute__
112 #endif
113 
114 #ifdef _WIN32
115 # define IS_DIRSEP(c) (c == '/' || c == '\\')
116 # define IS_ABSPATH(p) (IS_DIRSEP(p[0]) || (p[0] && p[1] == ':' && IS_DIRSEP(p[2])))
117 # define PATHCMP stricmp
118 # define PATHSEP ";"
119 #else
120 # define IS_DIRSEP(c) (c == '/')
121 # define IS_ABSPATH(p) IS_DIRSEP(p[0])
122 # define PATHCMP strcmp
123 # define PATHSEP ":"
124 #endif
125 
126 /* -------------------------------------------- */
127 
128 /* parser debug */
129 /* #define PARSE_DEBUG */
130 /* preprocessor debug */
131 /* #define PP_DEBUG */
132 /* include file debug */
133 /* #define INC_DEBUG */
134 /* memory leak debug (only for single threaded usage) */
135 /* #define MEM_DEBUG */
136 /* assembler debug */
137 /* #define ASM_DEBUG */
138 
139 /* target selection */
140 /* #define TCC_TARGET_I386   *//* i386 code generator */
141 /* #define TCC_TARGET_X86_64 *//* x86-64 code generator */
142 /* #define TCC_TARGET_ARM    *//* ARMv4 code generator */
143 /* #define TCC_TARGET_ARM64  *//* ARMv8 code generator */
144 /* #define TCC_TARGET_C67    *//* TMS320C67xx code generator */
145 /* #define TCC_TARGET_RISCV64 *//* risc-v code generator */
146 
147 /* default target is I386 */
148 #if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_ARM) && \
149     !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_C67) && \
150     !defined(TCC_TARGET_X86_64) && !defined(TCC_TARGET_RISCV64)
151 # if defined __x86_64__
152 #  define TCC_TARGET_X86_64
153 # elif defined __arm__
154 #  define TCC_TARGET_ARM
155 #  define TCC_ARM_EABI
156 #  define TCC_ARM_VFP
157 #  define TCC_ARM_HARDFLOAT
158 # elif defined __aarch64__
159 #  define TCC_TARGET_ARM64
160 # elif defined __riscv
161 #  define TCC_TARGET_RISCV64
162 # else
163 #  define TCC_TARGET_I386
164 # endif
165 # ifdef _WIN32
166 #  define TCC_TARGET_PE 1
167 # endif
168 #endif
169 
170 /* only native compiler supports -run */
171 #if defined _WIN32 == defined TCC_TARGET_PE
172 # if defined __i386__ && defined TCC_TARGET_I386
173 #  define TCC_IS_NATIVE
174 # elif defined __x86_64__ && defined TCC_TARGET_X86_64
175 #  define TCC_IS_NATIVE
176 # elif defined __arm__ && defined TCC_TARGET_ARM
177 #  define TCC_IS_NATIVE
178 # elif defined __aarch64__ && defined TCC_TARGET_ARM64
179 #  define TCC_IS_NATIVE
180 # elif defined __riscv && defined __LP64__ && defined TCC_TARGET_RISCV64
181 #  define TCC_IS_NATIVE
182 # endif
183 #endif
184 
185 #if defined TCC_IS_NATIVE && !defined CONFIG_TCCBOOT
186 # define CONFIG_TCC_BACKTRACE
187 # if (defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64) \
188   && !defined TCC_UCLIBC && !defined TCC_MUSL
189 # define CONFIG_TCC_BCHECK /* enable bound checking code */
190 # endif
191 #endif
192 
193 /* ------------ path configuration ------------ */
194 
195 #ifndef CONFIG_SYSROOT
196 # define CONFIG_SYSROOT ""
197 #endif
198 #ifndef CONFIG_TCCDIR
199 # define CONFIG_TCCDIR "/usr/local/lib/tcc"
200 #endif
201 #ifndef CONFIG_LDDIR
202 # define CONFIG_LDDIR "lib"
203 #endif
204 #ifdef CONFIG_TRIPLET
205 # define USE_TRIPLET(s) s "/" CONFIG_TRIPLET
206 # define ALSO_TRIPLET(s) USE_TRIPLET(s) ":" s
207 #else
208 # define USE_TRIPLET(s) s
209 # define ALSO_TRIPLET(s) s
210 #endif
211 
212 /* path to find crt1.o, crti.o and crtn.o */
213 #ifndef CONFIG_TCC_CRTPREFIX
214 # define CONFIG_TCC_CRTPREFIX USE_TRIPLET(CONFIG_SYSROOT "/usr/" CONFIG_LDDIR)
215 #endif
216 
217 /* Below: {B} is substituted by CONFIG_TCCDIR (rsp. -B option) */
218 
219 /* system include paths */
220 #ifndef CONFIG_TCC_SYSINCLUDEPATHS
221 # ifdef TCC_TARGET_PE
222 #  define CONFIG_TCC_SYSINCLUDEPATHS "{B}/include"PATHSEP"{B}/include/winapi"
223 # else
224 #  define CONFIG_TCC_SYSINCLUDEPATHS \
225         "{B}/include" \
226     ":" ALSO_TRIPLET(CONFIG_SYSROOT "/usr/local/include") \
227     ":" ALSO_TRIPLET(CONFIG_SYSROOT "/usr/include")
228 # endif
229 #endif
230 
231 /* library search paths */
232 #ifndef CONFIG_TCC_LIBPATHS
233 # ifdef TCC_TARGET_PE
234 #  define CONFIG_TCC_LIBPATHS "{B}/lib"
235 # else
236 #  define CONFIG_TCC_LIBPATHS \
237         ALSO_TRIPLET(CONFIG_SYSROOT "/usr/" CONFIG_LDDIR) \
238     ":" ALSO_TRIPLET(CONFIG_SYSROOT "/" CONFIG_LDDIR) \
239     ":" ALSO_TRIPLET(CONFIG_SYSROOT "/usr/local/" CONFIG_LDDIR)
240 # endif
241 #endif
242 
243 /* name of ELF interpreter */
244 #ifndef CONFIG_TCC_ELFINTERP
245 # if defined __FreeBSD__
246 #  define CONFIG_TCC_ELFINTERP "/libexec/ld-elf.so.1"
247 # elif defined __FreeBSD_kernel__
248 #  if defined(TCC_TARGET_X86_64)
249 #   define CONFIG_TCC_ELFINTERP "/lib/ld-kfreebsd-x86-64.so.1"
250 #  else
251 #   define CONFIG_TCC_ELFINTERP "/lib/ld.so.1"
252 #  endif
253 # elif defined __DragonFly__
254 #  define CONFIG_TCC_ELFINTERP "/usr/libexec/ld-elf.so.2"
255 # elif defined __NetBSD__
256 #  define CONFIG_TCC_ELFINTERP "/usr/libexec/ld.elf_so"
257 # elif defined __GNU__
258 #  define CONFIG_TCC_ELFINTERP "/lib/ld.so"
259 # elif defined(TCC_TARGET_PE)
260 #  define CONFIG_TCC_ELFINTERP "-"
261 # elif defined(TCC_UCLIBC)
262 #  define CONFIG_TCC_ELFINTERP "/lib/ld-uClibc.so.0" /* is there a uClibc for x86_64 ? */
263 # elif defined TCC_TARGET_ARM64
264 #  if defined(TCC_MUSL)
265 #   define CONFIG_TCC_ELFINTERP "/lib/ld-musl-aarch64.so.1"
266 #  else
267 #   define CONFIG_TCC_ELFINTERP "/lib/ld-linux-aarch64.so.1"
268 #  endif
269 # elif defined(TCC_TARGET_X86_64)
270 #  if defined(TCC_MUSL)
271 #   define CONFIG_TCC_ELFINTERP "/lib/ld-musl-x86_64.so.1"
272 #  else
273 #   define CONFIG_TCC_ELFINTERP "/lib64/ld-linux-x86-64.so.2"
274 #  endif
275 # elif defined(TCC_TARGET_RISCV64)
276 #  define CONFIG_TCC_ELFINTERP "/lib/ld-linux-riscv64-lp64d.so.1"
277 # elif !defined(TCC_ARM_EABI)
278 #  if defined(TCC_MUSL)
279 #   if defined(TCC_TARGET_I386)
280 #     define CONFIG_TCC_ELFINTERP "/lib/ld-musl-i386.so.1"
281 #    else
282 #     define CONFIG_TCC_ELFINTERP "/lib/ld-musl-arm.so.1"
283 #    endif
284 #  else
285 #   define CONFIG_TCC_ELFINTERP "/lib/ld-linux.so.2"
286 #  endif
287 # endif
288 #endif
289 
290 /* var elf_interp dans *-gen.c */
291 #ifdef CONFIG_TCC_ELFINTERP
292 # define DEFAULT_ELFINTERP(s) CONFIG_TCC_ELFINTERP
293 #else
294 # define DEFAULT_ELFINTERP(s) default_elfinterp(s)
295 #endif
296 
297 /* (target specific) libtcc1.a */
298 #ifndef TCC_LIBTCC1
299 # define TCC_LIBTCC1 "libtcc1.a"
300 #endif
301 
302 /* library to use with CONFIG_USE_LIBGCC instead of libtcc1.a */
303 #if defined CONFIG_USE_LIBGCC && !defined TCC_LIBGCC
304 #define TCC_LIBGCC USE_TRIPLET(CONFIG_SYSROOT "/" CONFIG_LDDIR) "/libgcc_s.so.1"
305 #endif
306 
307 /* -------------------------------------------- */
308 
309 #include "libtcc.h"
310 #include "elf.h"
311 #include "stab.h"
312 
313 /* -------------------------------------------- */
314 
315 #ifndef PUB_FUNC /* functions used by tcc.c but not in libtcc.h */
316 # define PUB_FUNC
317 #endif
318 
319 #ifndef ONE_SOURCE
320 # define ONE_SOURCE 1
321 #endif
322 
323 /* support using libtcc from threads */
324 #define CONFIG_TCC_SEMLOCK
325 
326 #if ONE_SOURCE
327 #define ST_INLN static inline
328 #define ST_FUNC static
329 #define ST_DATA static
330 #else
331 #define ST_INLN
332 #define ST_FUNC
333 #define ST_DATA extern
334 #endif
335 
336 #ifdef TCC_PROFILE /* profile all functions */
337 # define static
338 #endif
339 
340 /* -------------------------------------------- */
341 /* include the target specific definitions */
342 
343 #define TARGET_DEFS_ONLY
344 #ifdef TCC_TARGET_I386
345 # include "i386-gen.c"
346 # include "i386-link.c"
347 #elif defined TCC_TARGET_X86_64
348 # include "x86_64-gen.c"
349 # include "x86_64-link.c"
350 #elif defined TCC_TARGET_ARM
351 # include "arm-gen.c"
352 # include "arm-link.c"
353 # include "arm-asm.c"
354 #elif defined TCC_TARGET_ARM64
355 # include "arm64-gen.c"
356 # include "arm64-link.c"
357 #elif defined TCC_TARGET_C67
358 # define TCC_TARGET_COFF
359 # include "coff.h"
360 # include "c67-gen.c"
361 # include "c67-link.c"
362 #elif defined(TCC_TARGET_RISCV64)
363 # include "riscv64-gen.c"
364 # include "riscv64-link.c"
365 #else
366 #error unknown target
367 #endif
368 #undef TARGET_DEFS_ONLY
369 
370 /* -------------------------------------------- */
371 
372 #if PTR_SIZE == 8
373 # define ELFCLASSW ELFCLASS64
374 # define ElfW(type) Elf##64##_##type
375 # define ELFW(type) ELF##64##_##type
376 # define ElfW_Rel ElfW(Rela)
377 # define SHT_RELX SHT_RELA
378 # define REL_SECTION_FMT ".rela%s"
379 #else
380 # define ELFCLASSW ELFCLASS32
381 # define ElfW(type) Elf##32##_##type
382 # define ELFW(type) ELF##32##_##type
383 # define ElfW_Rel ElfW(Rel)
384 # define SHT_RELX SHT_REL
385 # define REL_SECTION_FMT ".rel%s"
386 #endif
387 /* target address type */
388 #define addr_t ElfW(Addr)
389 #define ElfSym ElfW(Sym)
390 
391 #if PTR_SIZE == 8 && !defined TCC_TARGET_PE
392 # define LONG_SIZE 8
393 #else
394 # define LONG_SIZE 4
395 #endif
396 
397 /* -------------------------------------------- */
398 
399 #define INCLUDE_STACK_SIZE  32
400 #define IFDEF_STACK_SIZE    64
401 #define VSTACK_SIZE         256
402 #define STRING_MAX_SIZE     1024
403 #define TOKSTR_MAX_SIZE     256
404 #define PACK_STACK_SIZE     8
405 
406 #define TOK_HASH_SIZE       16384 /* must be a power of two */
407 #define TOK_ALLOC_INCR      512  /* must be a power of two */
408 #define TOK_MAX_SIZE        4 /* token max size in int unit when stored in string */
409 
410 /* token symbol management */
411 typedef struct TokenSym {
412     struct TokenSym *hash_next;
413     struct Sym *sym_define; /* direct pointer to define */
414     struct Sym *sym_label; /* direct pointer to label */
415     struct Sym *sym_struct; /* direct pointer to structure */
416     struct Sym *sym_identifier; /* direct pointer to identifier */
417     int tok; /* token number */
418     int len;
419     char str[1];
420 } TokenSym;
421 
422 #ifdef TCC_TARGET_PE
423 typedef unsigned short nwchar_t;
424 #else
425 typedef int nwchar_t;
426 #endif
427 
428 typedef struct CString {
429     int size; /* size in bytes */
430     void *data; /* either 'char *' or 'nwchar_t *' */
431     int size_allocated;
432 } CString;
433 
434 /* type definition */
435 typedef struct CType {
436     int t;
437     struct Sym *ref;
438 } CType;
439 
440 /* constant value */
441 typedef union CValue {
442     long double ld;
443     double d;
444     float f;
445     uint64_t i;
446     struct {
447         int size;
448         const void *data;
449     } str;
450     int tab[LDOUBLE_SIZE/4];
451 } CValue;
452 
453 /* value on stack */
454 typedef struct SValue {
455     CType type;      /* type */
456     unsigned short r;      /* register + flags */
457     unsigned short r2;     /* second register, used for 'long long'
458                               type. If not used, set to VT_CONST */
459     union {
460       struct { int jtrue, jfalse; }; /* forward jmps */
461       CValue c;         /* constant, if VT_CONST */
462     };
463     union {
464       struct { unsigned short cmp_op, cmp_r; }; /* VT_CMP operation */
465       struct Sym *sym;  /* symbol, if (VT_SYM | VT_CONST), or if */
466     };                  /* result of unary() for an identifier. */
467 
468 } SValue;
469 
470 /* symbol attributes */
471 struct SymAttr {
472     unsigned short
473     aligned     : 5, /* alignment as log2+1 (0 == unspecified) */
474     packed      : 1,
475     weak        : 1,
476     visibility  : 2,
477     dllexport   : 1,
478     nodecorate  : 1,
479     dllimport   : 1,
480     addrtaken   : 1,
481     xxxx        : 3; /* not used */
482 };
483 
484 /* function attributes or temporary attributes for parsing */
485 struct FuncAttr {
486     unsigned
487     func_call   : 3, /* calling convention (0..5), see below */
488     func_type   : 2, /* FUNC_OLD/NEW/ELLIPSIS */
489     func_noreturn : 1, /* attribute((noreturn)) */
490     func_ctor   : 1, /* attribute((constructor)) */
491     func_dtor   : 1, /* attribute((destructor)) */
492     func_args   : 8; /* PE __stdcall args */
493 };
494 
495 /* symbol management */
496 typedef struct Sym {
497     int v; /* symbol token */
498     unsigned short r; /* associated register or VT_CONST/VT_LOCAL and LVAL type */
499     struct SymAttr a; /* symbol attributes */
500     union {
501         struct {
502             int c; /* associated number or Elf symbol index */
503             union {
504                 int sym_scope; /* scope level for locals */
505                 int jnext; /* next jump label */
506                 struct FuncAttr f; /* function attributes */
507                 int auxtype; /* bitfield access type */
508             };
509         };
510         long long enum_val; /* enum constant if IS_ENUM_VAL */
511         int *d; /* define token stream */
512         struct Sym *ncl; /* next cleanup */
513     };
514     CType type; /* associated type */
515     union {
516         struct Sym *next; /* next related symbol (for fields and anoms) */
517         struct Sym *cleanupstate; /* in defined labels */
518         int asm_label; /* associated asm label */
519     };
520     struct Sym *prev; /* prev symbol in stack */
521     struct Sym *prev_tok; /* previous symbol for this token */
522 } Sym;
523 
524 /* section definition */
525 typedef struct Section {
526     unsigned long data_offset; /* current data offset */
527     unsigned char *data;       /* section data */
528     unsigned long data_allocated; /* used for realloc() handling */
529     TCCState *s1;
530     int sh_name;             /* elf section name (only used during output) */
531     int sh_num;              /* elf section number */
532     int sh_type;             /* elf section type */
533     int sh_flags;            /* elf section flags */
534     int sh_info;             /* elf section info */
535     int sh_addralign;        /* elf section alignment */
536     int sh_entsize;          /* elf entry size */
537     unsigned long sh_size;   /* section size (only used during output) */
538     addr_t sh_addr;          /* address at which the section is relocated */
539     unsigned long sh_offset; /* file offset */
540     int nb_hashed_syms;      /* used to resize the hash table */
541     struct Section *link;    /* link to another section */
542     struct Section *reloc;   /* corresponding section for relocation, if any */
543     struct Section *hash;    /* hash table for symbols */
544     struct Section *prev;    /* previous section on section stack */
545     char name[1];           /* section name */
546 } Section;
547 
548 typedef struct DLLReference {
549     int level;
550     void *handle;
551     char name[1];
552 } DLLReference;
553 
554 /* -------------------------------------------------- */
555 
556 #define SYM_STRUCT     0x40000000 /* struct/union/enum symbol space */
557 #define SYM_FIELD      0x20000000 /* struct/union field symbol space */
558 #define SYM_FIRST_ANOM 0x10000000 /* first anonymous sym */
559 
560 /* stored in 'Sym->f.func_type' field */
561 #define FUNC_NEW       1 /* ansi function prototype */
562 #define FUNC_OLD       2 /* old function prototype */
563 #define FUNC_ELLIPSIS  3 /* ansi function prototype with ... */
564 
565 /* stored in 'Sym->f.func_call' field */
566 #define FUNC_CDECL     0 /* standard c call */
567 #define FUNC_STDCALL   1 /* pascal c call */
568 #define FUNC_FASTCALL1 2 /* first param in %eax */
569 #define FUNC_FASTCALL2 3 /* first parameters in %eax, %edx */
570 #define FUNC_FASTCALL3 4 /* first parameter in %eax, %edx, %ecx */
571 #define FUNC_FASTCALLW 5 /* first parameter in %ecx, %edx */
572 
573 /* field 'Sym.t' for macros */
574 #define MACRO_OBJ      0 /* object like macro */
575 #define MACRO_FUNC     1 /* function like macro */
576 
577 /* field 'Sym.r' for C labels */
578 #define LABEL_DEFINED  0 /* label is defined */
579 #define LABEL_FORWARD  1 /* label is forward defined */
580 #define LABEL_DECLARED 2 /* label is declared but never used */
581 #define LABEL_GONE     3 /* label isn't in scope, but not yet popped
582                             from local_label_stack (stmt exprs) */
583 
584 /* type_decl() types */
585 #define TYPE_ABSTRACT  1 /* type without variable */
586 #define TYPE_DIRECT    2 /* type with variable */
587 
588 #define IO_BUF_SIZE 8192
589 
590 typedef struct BufferedFile {
591     uint8_t *buf_ptr;
592     uint8_t *buf_end;
593     int fd;
594     struct BufferedFile *prev;
595     int line_num;    /* current line number - here to simplify code */
596     int line_ref;    /* tcc -E: last printed line */
597     int ifndef_macro;  /* #ifndef macro / #endif search */
598     int ifndef_macro_saved; /* saved ifndef_macro */
599     int *ifdef_stack_ptr; /* ifdef_stack value at the start of the file */
600     int include_next_index; /* next search path */
601     char filename[1024];    /* filename */
602     char *true_filename; /* filename not modified by # line directive */
603     unsigned char unget[4];
604     unsigned char buffer[1]; /* extra size for CH_EOB char */
605 } BufferedFile;
606 
607 #define CH_EOB   '\\'       /* end of buffer or '\0' char in file */
608 #define CH_EOF   (-1)   /* end of file */
609 
610 /* used to record tokens */
611 typedef struct TokenString {
612     int *str;
613     int len;
614     int lastlen;
615     int allocated_len;
616     int last_line_num;
617     int save_line_num;
618     /* used to chain token-strings with begin/end_macro() */
619     struct TokenString *prev;
620     const int *prev_ptr;
621     char alloc;
622 } TokenString;
623 
624 /* GNUC attribute definition */
625 typedef struct AttributeDef {
626     struct SymAttr a;
627     struct FuncAttr f;
628     struct Section *section;
629     Sym *cleanup_func;
630     int alias_target; /* token */
631     int asm_label; /* associated asm label */
632     char attr_mode; /* __attribute__((__mode__(...))) */
633 } AttributeDef;
634 
635 /* inline functions */
636 typedef struct InlineFunc {
637     TokenString *func_str;
638     Sym *sym;
639     char filename[1];
640 } InlineFunc;
641 
642 /* include file cache, used to find files faster and also to eliminate
643    inclusion if the include file is protected by #ifndef ... #endif */
644 typedef struct CachedInclude {
645     int ifndef_macro;
646     int once;
647     int hash_next; /* -1 if none */
648     char filename[1]; /* path specified in #include */
649 } CachedInclude;
650 
651 #define CACHED_INCLUDES_HASH_SIZE 32
652 
653 #ifdef CONFIG_TCC_ASM
654 typedef struct ExprValue {
655     uint64_t v;
656     Sym *sym;
657     int pcrel;
658 } ExprValue;
659 
660 #define MAX_ASM_OPERANDS 30
661 typedef struct ASMOperand {
662     int id; /* GCC 3 optional identifier (0 if number only supported */
663     char *constraint;
664     char asm_str[16]; /* computed asm string for operand */
665     SValue *vt; /* C value of the expression */
666     int ref_index; /* if >= 0, gives reference to a output constraint */
667     int input_index; /* if >= 0, gives reference to an input constraint */
668     int priority; /* priority, used to assign registers */
669     int reg; /* if >= 0, register number used for this operand */
670     int is_llong; /* true if double register value */
671     int is_memory; /* true if memory operand */
672     int is_rw;     /* for '+' modifier */
673 } ASMOperand;
674 #endif
675 
676 /* extra symbol attributes (not in symbol table) */
677 struct sym_attr {
678     unsigned got_offset;
679     unsigned plt_offset;
680     int plt_sym;
681     int dyn_index;
682 #ifdef TCC_TARGET_ARM
683     unsigned char plt_thumb_stub:1;
684 #endif
685 };
686 
687 struct TCCState {
688     unsigned char verbose; /* if true, display some information during compilation */
689     unsigned char nostdinc; /* if true, no standard headers are added */
690     unsigned char nostdlib; /* if true, no standard libraries are added */
691     unsigned char nocommon; /* if true, do not use common symbols for .bss data */
692     unsigned char static_link; /* if true, static linking is performed */
693     unsigned char rdynamic; /* if true, all symbols are exported */
694     unsigned char symbolic; /* if true, resolve symbols in the current module first */
695     unsigned char filetype; /* file type for compilation (NONE,C,ASM) */
696     unsigned int  cversion; /* supported C ISO version, 199901 (the default), 201112, ... */
697 
698     char *tcc_lib_path; /* CONFIG_TCCDIR or -B option */
699     char *soname; /* as specified on the command line (-soname) */
700     char *rpath; /* as specified on the command line (-Wl,-rpath=) */
701     unsigned char enable_new_dtags; /* ditto, (-Wl,--enable-new-dtags) */
702 
703     /* output type, see TCC_OUTPUT_XXX */
704     int output_type;
705     /* output format, see TCC_OUTPUT_FORMAT_xxx */
706     int output_format;
707 
708     /* C language options */
709     unsigned char char_is_unsigned;
710     unsigned char leading_underscore;
711     unsigned char ms_extensions; /* allow nested named struct w/o identifier behave like unnamed */
712     unsigned char dollars_in_identifiers;  /* allows '$' char in identifiers */
713     unsigned char ms_bitfields; /* if true, emulate MS algorithm for aligning bitfields */
714 
715     /* warning switches */
716     unsigned char warn_write_strings;
717     unsigned char warn_unsupported;
718     unsigned char warn_error;
719     unsigned char warn_none;
720     unsigned char warn_implicit_function_declaration;
721     unsigned char warn_gcc_compat;
722 
723     /* compile with debug symbol (and use them if error during execution) */
724     unsigned char do_debug;
725     unsigned char do_backtrace;
726 #ifdef CONFIG_TCC_BCHECK
727     /* compile with built-in memory and bounds checker */
728     unsigned char do_bounds_check;
729 #endif
730 #ifdef TCC_TARGET_ARM
731     enum float_abi float_abi; /* float ABI of the generated code*/
732 #endif
733     int run_test; /* nth test to run with -dt -run */
734 
735     addr_t text_addr; /* address of text section */
736     unsigned char has_text_addr;
737 
738     unsigned section_align; /* section alignment */
739 
740     /* use GNU C extensions */
741     unsigned char gnu_ext;
742     /* use TinyCC extensions */
743     unsigned char tcc_ext;
744 
745     char *init_symbol; /* symbols to call at load-time (not used currently) */
746     char *fini_symbol; /* symbols to call at unload-time (not used currently) */
747 
748 #ifdef TCC_TARGET_I386
749     int seg_size; /* 32. Can be 16 with i386 assembler (.code16) */
750 #endif
751 #ifdef TCC_TARGET_X86_64
752     unsigned char nosse; /* For -mno-sse support. */
753 #endif
754 
755     /* array of all loaded dlls (including those referenced by loaded dlls) */
756     DLLReference **loaded_dlls;
757     int nb_loaded_dlls;
758 
759     /* include paths */
760     char **include_paths;
761     int nb_include_paths;
762 
763     char **sysinclude_paths;
764     int nb_sysinclude_paths;
765 
766     /* library paths */
767     char **library_paths;
768     int nb_library_paths;
769 
770     /* crt?.o object path */
771     char **crt_paths;
772     int nb_crt_paths;
773 
774     /* -D / -U options */
775     CString cmdline_defs;
776     /* -include options */
777     CString cmdline_incl;
778 
779     /* error handling */
780     void *error_opaque;
781     void (*error_func)(void *opaque, const char *msg);
782     int error_set_jmp_enabled;
783     jmp_buf error_jmp_buf;
784     int nb_errors;
785 
786     /* output file for preprocessing (-E) */
787     FILE *ppfp;
788     enum {
789 	LINE_MACRO_OUTPUT_FORMAT_GCC,
790 	LINE_MACRO_OUTPUT_FORMAT_NONE,
791 	LINE_MACRO_OUTPUT_FORMAT_STD,
792     LINE_MACRO_OUTPUT_FORMAT_P10 = 11
793     } Pflag; /* -P switch */
794     char dflag; /* -dX value */
795 
796     /* for -MD/-MF: collected dependencies for this compilation */
797     char **target_deps;
798     int nb_target_deps;
799 
800     /* compilation */
801     BufferedFile *include_stack[INCLUDE_STACK_SIZE];
802     BufferedFile **include_stack_ptr;
803 
804     int ifdef_stack[IFDEF_STACK_SIZE];
805     int *ifdef_stack_ptr;
806 
807     /* included files enclosed with #ifndef MACRO */
808     int cached_includes_hash[CACHED_INCLUDES_HASH_SIZE];
809     CachedInclude **cached_includes;
810     int nb_cached_includes;
811 
812     /* #pragma pack stack */
813     int pack_stack[PACK_STACK_SIZE];
814     int *pack_stack_ptr;
815     char **pragma_libs;
816     int nb_pragma_libs;
817 
818     /* inline functions are stored as token lists and compiled last
819        only if referenced */
820     struct InlineFunc **inline_fns;
821     int nb_inline_fns;
822 
823     /* sections */
824     Section **sections;
825     int nb_sections; /* number of sections, including first dummy section */
826 
827     Section **priv_sections;
828     int nb_priv_sections; /* number of private sections */
829 
830     /* got & plt handling */
831     Section *got;
832     Section *plt;
833 
834     /* predefined sections */
835     Section *text_section, *data_section, *bss_section;
836     Section *common_section;
837     Section *cur_text_section; /* current section where function code is generated */
838 #ifdef CONFIG_TCC_BCHECK
839     /* bound check related sections */
840     Section *bounds_section; /* contains global data bound description */
841     Section *lbounds_section; /* contains local data bound description */
842 #endif
843     /* symbol sections */
844     Section *symtab_section;
845     /* debug sections */
846     Section *stab_section;
847     /* Is there a new undefined sym since last new_undef_sym() */
848     int new_undef_sym;
849 
850     /* temporary dynamic symbol sections (for dll loading) */
851     Section *dynsymtab_section;
852     /* exported dynamic symbol section */
853     Section *dynsym;
854     /* copy of the global symtab_section variable */
855     Section *symtab;
856     /* extra attributes (eg. GOT/PLT value) for symtab symbols */
857     struct sym_attr *sym_attrs;
858     int nb_sym_attrs;
859     /* ptr to next reloc entry reused */
860     ElfW_Rel *qrel;
861 #   define qrel s1->qrel
862 
863 #ifdef TCC_TARGET_PE
864     /* PE info */
865     int pe_subsystem;
866     unsigned pe_characteristics;
867     unsigned pe_file_align;
868     unsigned pe_stack_size;
869     addr_t pe_imagebase;
870 # ifdef TCC_TARGET_X86_64
871     Section *uw_pdata;
872     int uw_sym;
873     unsigned uw_offs;
874 # endif
875 # define ELF_OBJ_ONLY
876 #endif
877 
878 #ifndef ELF_OBJ_ONLY
879     int nb_sym_versions;
880     struct sym_version *sym_versions;
881     int nb_sym_to_version;
882     int *sym_to_version;
883     int dt_verneednum;
884     Section *versym_section;
885     Section *verneed_section;
886 #endif
887 
888 #ifdef TCC_IS_NATIVE
889     const char *runtime_main;
890     void **runtime_mem;
891     int nb_runtime_mem;
892 #endif
893 
894 #ifdef CONFIG_TCC_BACKTRACE
895     int rt_num_callers;
896 #endif
897 
898     int fd, cc; /* used by tcc_load_ldscript */
899 
900     /* benchmark info */
901     int total_idents;
902     int total_lines;
903     int total_bytes;
904 
905     /* option -dnum (for general development purposes) */
906     int g_debug;
907 
908     /* used by main and tcc_parse_args only */
909     struct filespec **files; /* files seen on command line */
910     int nb_files; /* number thereof */
911     int nb_libraries; /* number of libs thereof */
912     char *outfile; /* output filename */
913     unsigned char option_r; /* option -r */
914     unsigned char do_bench; /* option -bench */
915     int gen_deps; /* option -MD  */
916     char *deps_outfile; /* option -MF */
917     unsigned char option_pthread; /* -pthread option */
918     int argc;
919     char **argv;
920 };
921 
922 struct filespec {
923     char type;
924     char name[1];
925 };
926 
927 /* The current value can be: */
928 #define VT_VALMASK   0x003f  /* mask for value location, register or: */
929 #define VT_CONST     0x0030  /* constant in vc (must be first non register value) */
930 #define VT_LLOCAL    0x0031  /* lvalue, offset on stack */
931 #define VT_LOCAL     0x0032  /* offset on stack */
932 #define VT_CMP       0x0033  /* the value is stored in processor flags (in vc) */
933 #define VT_JMP       0x0034  /* value is the consequence of jmp true (even) */
934 #define VT_JMPI      0x0035  /* value is the consequence of jmp false (odd) */
935 #define VT_LVAL      0x0100  /* var is an lvalue */
936 #define VT_SYM       0x0200  /* a symbol value is added */
937 #define VT_MUSTCAST  0x0C00  /* value must be casted to be correct (used for
938                                 char/short stored in integer registers) */
939 #define VT_MUSTBOUND 0x4000  /* bound checking must be done before
940                                 dereferencing value */
941 #define VT_BOUNDED   0x8000  /* value is bounded. The address of the
942                                 bounding function call point is in vc */
943 /* types */
944 #define VT_BTYPE       0x000f  /* mask for basic type */
945 #define VT_VOID             0  /* void type */
946 #define VT_BYTE             1  /* signed byte type */
947 #define VT_SHORT            2  /* short type */
948 #define VT_INT              3  /* integer type */
949 #define VT_LLONG            4  /* 64 bit integer */
950 #define VT_PTR              5  /* pointer */
951 #define VT_FUNC             6  /* function type */
952 #define VT_STRUCT           7  /* struct/union definition */
953 #define VT_FLOAT            8  /* IEEE float */
954 #define VT_DOUBLE           9  /* IEEE double */
955 #define VT_LDOUBLE         10  /* IEEE long double */
956 #define VT_BOOL            11  /* ISOC99 boolean type */
957 #define VT_QLONG           13  /* 128-bit integer. Only used for x86-64 ABI */
958 #define VT_QFLOAT          14  /* 128-bit float. Only used for x86-64 ABI */
959 
960 #define VT_UNSIGNED    0x0010  /* unsigned type */
961 #define VT_DEFSIGN     0x0020  /* explicitly signed or unsigned */
962 #define VT_ARRAY       0x0040  /* array type (also has VT_PTR) */
963 #define VT_BITFIELD    0x0080  /* bitfield modifier */
964 #define VT_CONSTANT    0x0100  /* const modifier */
965 #define VT_VOLATILE    0x0200  /* volatile modifier */
966 #define VT_VLA         0x0400  /* VLA type (also has VT_PTR and VT_ARRAY) */
967 #define VT_LONG        0x0800  /* long type (also has VT_INT rsp. VT_LLONG) */
968 
969 /* storage */
970 #define VT_EXTERN  0x00001000  /* extern definition */
971 #define VT_STATIC  0x00002000  /* static variable */
972 #define VT_TYPEDEF 0x00004000  /* typedef definition */
973 #define VT_INLINE  0x00008000  /* inline definition */
974 /* currently unused: 0x000[1248]0000  */
975 
976 #define VT_STRUCT_SHIFT 20     /* shift for bitfield shift values (32 - 2*6) */
977 #define VT_STRUCT_MASK (((1U << (6+6)) - 1) << VT_STRUCT_SHIFT | VT_BITFIELD)
978 #define BIT_POS(t) (((t) >> VT_STRUCT_SHIFT) & 0x3f)
979 #define BIT_SIZE(t) (((t) >> (VT_STRUCT_SHIFT + 6)) & 0x3f)
980 
981 #define VT_UNION    (1 << VT_STRUCT_SHIFT | VT_STRUCT)
982 #define VT_ENUM     (2 << VT_STRUCT_SHIFT) /* integral type is an enum really */
983 #define VT_ENUM_VAL (3 << VT_STRUCT_SHIFT) /* integral type is an enum constant really */
984 
985 #define IS_ENUM(t) ((t & VT_STRUCT_MASK) == VT_ENUM)
986 #define IS_ENUM_VAL(t) ((t & VT_STRUCT_MASK) == VT_ENUM_VAL)
987 #define IS_UNION(t) ((t & (VT_STRUCT_MASK|VT_BTYPE)) == VT_UNION)
988 
989 /* type mask (except storage) */
990 #define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE)
991 #define VT_TYPE (~(VT_STORAGE|VT_STRUCT_MASK))
992 
993 /* symbol was created by tccasm.c first */
994 #define VT_ASM (VT_VOID | VT_UNSIGNED)
995 #define IS_ASM_SYM(sym) (((sym)->type.t & (VT_BTYPE | VT_ASM)) == VT_ASM)
996 
997 /* general: set/get the pseudo-bitfield value for bit-mask M */
998 #define BFVAL(M,N) ((unsigned)((M) & ~((M) << 1)) * (N))
999 #define BFGET(X,M) (((X) & (M)) / BFVAL(M,1))
1000 #define BFSET(X,M,N) ((X) = ((X) & ~(M)) | BFVAL(M,N))
1001 
1002 /* token values */
1003 
1004 /* warning: the following compare tokens depend on i386 asm code */
1005 #define TOK_ULT 0x92
1006 #define TOK_UGE 0x93
1007 #define TOK_EQ  0x94
1008 #define TOK_NE  0x95
1009 #define TOK_ULE 0x96
1010 #define TOK_UGT 0x97
1011 #define TOK_Nset 0x98
1012 #define TOK_Nclear 0x99
1013 #define TOK_LT  0x9c
1014 #define TOK_GE  0x9d
1015 #define TOK_LE  0x9e
1016 #define TOK_GT  0x9f
1017 
1018 #define TOK_LAND  0xa0
1019 #define TOK_LOR   0xa1
1020 #define TOK_DEC   0xa2
1021 #define TOK_MID   0xa3 /* inc/dec, to void constant */
1022 #define TOK_INC   0xa4
1023 #define TOK_UDIV  0xb0 /* unsigned division */
1024 #define TOK_UMOD  0xb1 /* unsigned modulo */
1025 #define TOK_PDIV  0xb2 /* fast division with undefined rounding for pointers */
1026 
1027 /* tokens that carry values (in additional token string space / tokc) --> */
1028 #define TOK_CCHAR   0xb3 /* char constant in tokc */
1029 #define TOK_LCHAR   0xb4
1030 #define TOK_CINT    0xb5 /* number in tokc */
1031 #define TOK_CUINT   0xb6 /* unsigned int constant */
1032 #define TOK_CLLONG  0xb7 /* long long constant */
1033 #define TOK_CULLONG 0xb8 /* unsigned long long constant */
1034 #define TOK_STR     0xb9 /* pointer to string in tokc */
1035 #define TOK_LSTR    0xba
1036 #define TOK_CFLOAT  0xbb /* float constant */
1037 #define TOK_CDOUBLE 0xbc /* double constant */
1038 #define TOK_CLDOUBLE 0xbd /* long double constant */
1039 #define TOK_PPNUM   0xbe /* preprocessor number */
1040 #define TOK_PPSTR   0xbf /* preprocessor string */
1041 #define TOK_LINENUM 0xc0 /* line number info */
1042 #define TOK_TWODOTS 0xa8 /* C++ token ? */
1043 /* <-- */
1044 
1045 #define TOK_UMULL    0xc2 /* unsigned 32x32 -> 64 mul */
1046 #define TOK_ADDC1    0xc3 /* add with carry generation */
1047 #define TOK_ADDC2    0xc4 /* add with carry use */
1048 #define TOK_SUBC1    0xc5 /* add with carry generation */
1049 #define TOK_SUBC2    0xc6 /* add with carry use */
1050 #define TOK_ARROW    0xc7
1051 #define TOK_DOTS     0xc8 /* three dots */
1052 #define TOK_SHR      0xc9 /* unsigned shift right */
1053 #define TOK_TWOSHARPS 0xca /* ## preprocessing token */
1054 #define TOK_PLCHLDR  0xcb /* placeholder token as defined in C99 */
1055 #define TOK_NOSUBST  0xcc /* means following token has already been pp'd */
1056 #define TOK_PPJOIN   0xcd /* A '##' in the right position to mean pasting */
1057 #define TOK_CLONG    0xce /* long constant */
1058 #define TOK_CULONG   0xcf /* unsigned long constant */
1059 
1060 #define TOK_SHL   0x01 /* shift left */
1061 #define TOK_SAR   0x02 /* signed shift right */
1062 
1063 /* assignment operators : normal operator or 0x80 */
1064 #define TOK_A_MOD 0xa5
1065 #define TOK_A_AND 0xa6
1066 #define TOK_A_MUL 0xaa
1067 #define TOK_A_ADD 0xab
1068 #define TOK_A_SUB 0xad
1069 #define TOK_A_DIV 0xaf
1070 #define TOK_A_XOR 0xde
1071 #define TOK_A_OR  0xfc
1072 #define TOK_A_SHL 0x81
1073 #define TOK_A_SAR 0x82
1074 
1075 #define TOK_EOF       (-1)  /* end of file */
1076 #define TOK_LINEFEED  10    /* line feed */
1077 
1078 /* all identifiers and strings have token above that */
1079 #define TOK_IDENT 256
1080 
1081 #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
1082 #define TOK_ASM_int TOK_INT
1083 #define DEF_ASMDIR(x) DEF(TOK_ASMDIR_ ## x, "." #x)
1084 #define TOK_ASMDIR_FIRST TOK_ASMDIR_byte
1085 #define TOK_ASMDIR_LAST TOK_ASMDIR_section
1086 
1087 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
1088 /* only used for i386 asm opcodes definitions */
1089 #define DEF_BWL(x) \
1090  DEF(TOK_ASM_ ## x ## b, #x "b") \
1091  DEF(TOK_ASM_ ## x ## w, #x "w") \
1092  DEF(TOK_ASM_ ## x ## l, #x "l") \
1093  DEF(TOK_ASM_ ## x, #x)
1094 #define DEF_WL(x) \
1095  DEF(TOK_ASM_ ## x ## w, #x "w") \
1096  DEF(TOK_ASM_ ## x ## l, #x "l") \
1097  DEF(TOK_ASM_ ## x, #x)
1098 #ifdef TCC_TARGET_X86_64
1099 # define DEF_BWLQ(x) \
1100  DEF(TOK_ASM_ ## x ## b, #x "b") \
1101  DEF(TOK_ASM_ ## x ## w, #x "w") \
1102  DEF(TOK_ASM_ ## x ## l, #x "l") \
1103  DEF(TOK_ASM_ ## x ## q, #x "q") \
1104  DEF(TOK_ASM_ ## x, #x)
1105 # define DEF_WLQ(x) \
1106  DEF(TOK_ASM_ ## x ## w, #x "w") \
1107  DEF(TOK_ASM_ ## x ## l, #x "l") \
1108  DEF(TOK_ASM_ ## x ## q, #x "q") \
1109  DEF(TOK_ASM_ ## x, #x)
1110 # define DEF_BWLX DEF_BWLQ
1111 # define DEF_WLX DEF_WLQ
1112 /* number of sizes + 1 */
1113 # define NBWLX 5
1114 #else
1115 # define DEF_BWLX DEF_BWL
1116 # define DEF_WLX DEF_WL
1117 /* number of sizes + 1 */
1118 # define NBWLX 4
1119 #endif
1120 
1121 #define DEF_FP1(x) \
1122  DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
1123  DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
1124  DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
1125  DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
1126 
1127 #define DEF_FP(x) \
1128  DEF(TOK_ASM_ ## f ## x, "f" #x ) \
1129  DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
1130  DEF_FP1(x)
1131 
1132 #define DEF_ASMTEST(x,suffix) \
1133  DEF_ASM(x ## o ## suffix) \
1134  DEF_ASM(x ## no ## suffix) \
1135  DEF_ASM(x ## b ## suffix) \
1136  DEF_ASM(x ## c ## suffix) \
1137  DEF_ASM(x ## nae ## suffix) \
1138  DEF_ASM(x ## nb ## suffix) \
1139  DEF_ASM(x ## nc ## suffix) \
1140  DEF_ASM(x ## ae ## suffix) \
1141  DEF_ASM(x ## e ## suffix) \
1142  DEF_ASM(x ## z ## suffix) \
1143  DEF_ASM(x ## ne ## suffix) \
1144  DEF_ASM(x ## nz ## suffix) \
1145  DEF_ASM(x ## be ## suffix) \
1146  DEF_ASM(x ## na ## suffix) \
1147  DEF_ASM(x ## nbe ## suffix) \
1148  DEF_ASM(x ## a ## suffix) \
1149  DEF_ASM(x ## s ## suffix) \
1150  DEF_ASM(x ## ns ## suffix) \
1151  DEF_ASM(x ## p ## suffix) \
1152  DEF_ASM(x ## pe ## suffix) \
1153  DEF_ASM(x ## np ## suffix) \
1154  DEF_ASM(x ## po ## suffix) \
1155  DEF_ASM(x ## l ## suffix) \
1156  DEF_ASM(x ## nge ## suffix) \
1157  DEF_ASM(x ## nl ## suffix) \
1158  DEF_ASM(x ## ge ## suffix) \
1159  DEF_ASM(x ## le ## suffix) \
1160  DEF_ASM(x ## ng ## suffix) \
1161  DEF_ASM(x ## nle ## suffix) \
1162  DEF_ASM(x ## g ## suffix)
1163 
1164 #endif /* defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64 */
1165 
1166 enum tcc_token {
1167     TOK_LAST = TOK_IDENT - 1
1168 #define DEF(id, str) ,id
1169 #include "tcctok.h"
1170 #undef DEF
1171 };
1172 
1173 /* keywords: tok >= TOK_IDENT && tok < TOK_UIDENT */
1174 #define TOK_UIDENT TOK_DEFINE
1175 
1176 /* ------------ libtcc.c ------------ */
1177 
1178 ST_DATA struct TCCState *tcc_state;
1179 
1180 /* public functions currently used by the tcc main function */
1181 ST_FUNC char *pstrcpy(char *buf, size_t buf_size, const char *s);
1182 ST_FUNC char *pstrcat(char *buf, size_t buf_size, const char *s);
1183 ST_FUNC char *pstrncpy(char *out, const char *in, size_t num);
1184 PUB_FUNC char *tcc_basename(const char *name);
1185 PUB_FUNC char *tcc_fileextension (const char *name);
1186 
1187 #ifndef MEM_DEBUG
1188 PUB_FUNC void tcc_free(void *ptr);
1189 PUB_FUNC void *tcc_malloc(unsigned long size);
1190 PUB_FUNC void *tcc_mallocz(unsigned long size);
1191 PUB_FUNC void *tcc_realloc(void *ptr, unsigned long size);
1192 PUB_FUNC char *tcc_strdup(const char *str);
1193 #else
1194 #define tcc_free(ptr)           tcc_free_debug(ptr)
1195 #define tcc_malloc(size)        tcc_malloc_debug(size, __FILE__, __LINE__)
1196 #define tcc_mallocz(size)       tcc_mallocz_debug(size, __FILE__, __LINE__)
1197 #define tcc_realloc(ptr,size)   tcc_realloc_debug(ptr, size, __FILE__, __LINE__)
1198 #define tcc_strdup(str)         tcc_strdup_debug(str, __FILE__, __LINE__)
1199 PUB_FUNC void tcc_free_debug(void *ptr);
1200 PUB_FUNC void *tcc_malloc_debug(unsigned long size, const char *file, int line);
1201 PUB_FUNC void *tcc_mallocz_debug(unsigned long size, const char *file, int line);
1202 PUB_FUNC void *tcc_realloc_debug(void *ptr, unsigned long size, const char *file, int line);
1203 PUB_FUNC char *tcc_strdup_debug(const char *str, const char *file, int line);
1204 #endif
1205 
1206 #define free(p) use_tcc_free(p)
1207 #define malloc(s) use_tcc_malloc(s)
1208 #define realloc(p, s) use_tcc_realloc(p, s)
1209 #undef strdup
1210 #define strdup(s) use_tcc_strdup(s)
1211 PUB_FUNC void _tcc_error_noabort(const char *fmt, ...) PRINTF_LIKE(1,2);
1212 PUB_FUNC NORETURN void _tcc_error(const char *fmt, ...) PRINTF_LIKE(1,2);
1213 PUB_FUNC void _tcc_warning(const char *fmt, ...) PRINTF_LIKE(1,2);
1214 
1215 /* other utilities */
1216 ST_FUNC void dynarray_add(void *ptab, int *nb_ptr, void *data);
1217 ST_FUNC void dynarray_reset(void *pp, int *n);
1218 ST_INLN void cstr_ccat(CString *cstr, int ch);
1219 ST_FUNC void cstr_cat(CString *cstr, const char *str, int len);
1220 ST_FUNC void cstr_wccat(CString *cstr, int ch);
1221 ST_FUNC void cstr_new(CString *cstr);
1222 ST_FUNC void cstr_free(CString *cstr);
1223 ST_FUNC int cstr_printf(CString *cs, const char *fmt, ...) PRINTF_LIKE(2,3);
1224 ST_FUNC void cstr_reset(CString *cstr);
1225 
1226 ST_INLN void sym_free(Sym *sym);
1227 ST_FUNC Sym *sym_push2(Sym **ps, int v, int t, int c);
1228 ST_FUNC Sym *sym_find2(Sym *s, int v);
1229 ST_FUNC Sym *sym_push(int v, CType *type, int r, int c);
1230 ST_FUNC void sym_pop(Sym **ptop, Sym *b, int keep);
1231 ST_INLN Sym *struct_find(int v);
1232 ST_INLN Sym *sym_find(int v);
1233 ST_FUNC Sym *global_identifier_push(int v, int t, int c);
1234 
1235 ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen);
1236 ST_FUNC int tcc_open(TCCState *s1, const char *filename);
1237 ST_FUNC void tcc_close(void);
1238 
1239 ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags);
1240 /* flags: */
1241 #define AFF_PRINT_ERROR     0x10 /* print error if file not found */
1242 #define AFF_REFERENCED_DLL  0x20 /* load a referenced dll from another dll */
1243 #define AFF_TYPE_BIN        0x40 /* file to add is binary */
1244 #define AFF_WHOLE_ARCHIVE   0x80 /* load all objects from archive */
1245 /* s->filetype: */
1246 #define AFF_TYPE_NONE   0
1247 #define AFF_TYPE_C      1
1248 #define AFF_TYPE_ASM    2
1249 #define AFF_TYPE_ASMPP  4
1250 #define AFF_TYPE_LIB    8
1251 #define AFF_TYPE_MASK   (15 | AFF_TYPE_BIN)
1252 /* values from tcc_object_type(...) */
1253 #define AFF_BINTYPE_REL 1
1254 #define AFF_BINTYPE_DYN 2
1255 #define AFF_BINTYPE_AR  3
1256 #define AFF_BINTYPE_C67 4
1257 
1258 
1259 #ifndef TCC_TARGET_PE
1260 ST_FUNC int tcc_add_crt(TCCState *s, const char *filename);
1261 #endif
1262 ST_FUNC int tcc_add_dll(TCCState *s, const char *filename, int flags);
1263 #ifdef CONFIG_TCC_BCHECK
1264 ST_FUNC void tcc_add_bcheck(TCCState *s1);
1265 #endif
1266 #ifdef CONFIG_TCC_BACKTRACE
1267 ST_FUNC void tcc_add_btstub(TCCState *s1);
1268 #endif
1269 ST_FUNC void tcc_add_pragma_libs(TCCState *s1);
1270 PUB_FUNC int tcc_add_library_err(TCCState *s, const char *f);
1271 PUB_FUNC void tcc_print_stats(TCCState *s, unsigned total_time);
1272 PUB_FUNC int tcc_parse_args(TCCState *s, int *argc, char ***argv, int optind);
1273 #ifdef _WIN32
1274 ST_FUNC char *normalize_slashes(char *path);
1275 #endif
1276 
1277 /* tcc_parse_args return codes: */
1278 #define OPT_HELP 1
1279 #define OPT_HELP2 2
1280 #define OPT_V 3
1281 #define OPT_PRINT_DIRS 4
1282 #define OPT_AR 5
1283 #define OPT_IMPDEF 6
1284 #define OPT_M32 32
1285 #define OPT_M64 64
1286 
1287 /* ------------ tccpp.c ------------ */
1288 
1289 ST_DATA struct BufferedFile *file;
1290 ST_DATA int ch, tok;
1291 ST_DATA CValue tokc;
1292 ST_DATA const int *macro_ptr;
1293 ST_DATA int parse_flags;
1294 ST_DATA int tok_flags;
1295 ST_DATA CString tokcstr; /* current parsed string, if any */
1296 
1297 /* display benchmark infos */
1298 ST_DATA int tok_ident;
1299 ST_DATA TokenSym **table_ident;
1300 
1301 #define TOK_FLAG_BOL   0x0001 /* beginning of line before */
1302 #define TOK_FLAG_BOF   0x0002 /* beginning of file before */
1303 #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
1304 #define TOK_FLAG_EOF   0x0008 /* end of file */
1305 
1306 #define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
1307 #define PARSE_FLAG_TOK_NUM    0x0002 /* return numbers instead of TOK_PPNUM */
1308 #define PARSE_FLAG_LINEFEED   0x0004 /* line feed is returned as a
1309                                         token. line feed is also
1310                                         returned at eof */
1311 #define PARSE_FLAG_ASM_FILE 0x0008 /* we processing an asm file: '#' can be used for line comment, etc. */
1312 #define PARSE_FLAG_SPACES     0x0010 /* next() returns space tokens (for -E) */
1313 #define PARSE_FLAG_ACCEPT_STRAYS 0x0020 /* next() returns '\\' token */
1314 #define PARSE_FLAG_TOK_STR    0x0040 /* return parsed strings instead of TOK_PPSTR */
1315 
1316 /* isidnum_table flags: */
1317 #define IS_SPC 1
1318 #define IS_ID  2
1319 #define IS_NUM 4
1320 
1321 ST_FUNC TokenSym *tok_alloc(const char *str, int len);
1322 ST_FUNC const char *get_tok_str(int v, CValue *cv);
1323 ST_FUNC void begin_macro(TokenString *str, int alloc);
1324 ST_FUNC void end_macro(void);
1325 ST_FUNC int set_idnum(int c, int val);
1326 ST_INLN void tok_str_new(TokenString *s);
1327 ST_FUNC TokenString *tok_str_alloc(void);
1328 ST_FUNC void tok_str_free(TokenString *s);
1329 ST_FUNC void tok_str_free_str(int *str);
1330 ST_FUNC void tok_str_add(TokenString *s, int t);
1331 ST_FUNC void tok_str_add_tok(TokenString *s);
1332 ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg);
1333 ST_FUNC void define_undef(Sym *s);
1334 ST_INLN Sym *define_find(int v);
1335 ST_FUNC void free_defines(Sym *b);
1336 ST_FUNC Sym *label_find(int v);
1337 ST_FUNC Sym *label_push(Sym **ptop, int v, int flags);
1338 ST_FUNC void label_pop(Sym **ptop, Sym *slast, int keep);
1339 ST_FUNC void parse_define(void);
1340 ST_FUNC void preprocess(int is_bof);
1341 ST_FUNC void next_nomacro(void);
1342 ST_FUNC void next(void);
1343 ST_INLN void unget_tok(int last_tok);
1344 ST_FUNC void preprocess_start(TCCState *s1, int is_asm);
1345 ST_FUNC void preprocess_end(TCCState *s1);
1346 ST_FUNC void tccpp_new(TCCState *s);
1347 ST_FUNC void tccpp_delete(TCCState *s);
1348 ST_FUNC int tcc_preprocess(TCCState *s1);
1349 ST_FUNC void skip(int c);
1350 ST_FUNC NORETURN void expect(const char *msg);
1351 
1352 /* space excluding newline */
is_space(int ch)1353 static inline int is_space(int ch) {
1354     return ch == ' ' || ch == '\t' || ch == '\v' || ch == '\f' || ch == '\r';
1355 }
isid(int c)1356 static inline int isid(int c) {
1357     return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_';
1358 }
isnum(int c)1359 static inline int isnum(int c) {
1360     return c >= '0' && c <= '9';
1361 }
isoct(int c)1362 static inline int isoct(int c) {
1363     return c >= '0' && c <= '7';
1364 }
toup(int c)1365 static inline int toup(int c) {
1366     return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c;
1367 }
1368 
1369 /* ------------ tccgen.c ------------ */
1370 
1371 #define SYM_POOL_NB (8192 / sizeof(Sym))
1372 
1373 ST_DATA Sym *global_stack;
1374 ST_DATA Sym *local_stack;
1375 ST_DATA Sym *local_label_stack;
1376 ST_DATA Sym *global_label_stack;
1377 ST_DATA Sym *define_stack;
1378 ST_DATA CType int_type, func_old_type, char_pointer_type;
1379 ST_DATA SValue *vtop;
1380 ST_DATA int rsym, anon_sym, ind, loc;
1381 
1382 ST_DATA int const_wanted; /* true if constant wanted */
1383 ST_DATA int nocode_wanted; /* true if no code generation wanted for an expression */
1384 ST_DATA int global_expr;  /* true if compound literals must be allocated globally (used during initializers parsing */
1385 ST_DATA CType func_vt; /* current function return type (used by return instruction) */
1386 ST_DATA int func_var; /* true if current function is variadic */
1387 ST_DATA int func_vc;
1388 ST_DATA const char *funcname;
1389 
1390 ST_FUNC void tcc_debug_start(TCCState *s1);
1391 ST_FUNC void tcc_debug_end(TCCState *s1);
1392 ST_FUNC void tcc_debug_bincl(TCCState *s1);
1393 ST_FUNC void tcc_debug_eincl(TCCState *s1);
1394 ST_FUNC void tcc_debug_putfile(TCCState *s1, const char *filename);
1395 ST_FUNC void tcc_debug_funcstart(TCCState *s1, Sym *sym);
1396 ST_FUNC void tcc_debug_funcend(TCCState *s1, int size);
1397 ST_FUNC void tcc_debug_line(TCCState *s1);
1398 
1399 ST_FUNC void tccgen_init(TCCState *s1);
1400 ST_FUNC int tccgen_compile(TCCState *s1);
1401 ST_FUNC void tccgen_finish(TCCState *s1);
1402 ST_FUNC void check_vstack(void);
1403 
1404 ST_INLN int is_float(int t);
1405 ST_FUNC int ieee_finite(double d);
1406 ST_FUNC void test_lvalue(void);
1407 ST_FUNC void vpushi(int v);
1408 ST_FUNC ElfSym *elfsym(Sym *);
1409 ST_FUNC void update_storage(Sym *sym);
1410 ST_FUNC Sym *external_global_sym(int v, CType *type);
1411 ST_FUNC void vset(CType *type, int r, int v);
1412 ST_FUNC void vset_VT_CMP(int op);
1413 ST_FUNC void vswap(void);
1414 ST_FUNC void vpush_global_sym(CType *type, int v);
1415 ST_FUNC void vrote(SValue *e, int n);
1416 ST_FUNC void vrott(int n);
1417 ST_FUNC void vrotb(int n);
1418 #if PTR_SIZE == 4
1419 ST_FUNC void lexpand(void);
1420 #endif
1421 #ifdef TCC_TARGET_ARM
1422 ST_FUNC int get_reg_ex(int rc, int rc2);
1423 #endif
1424 ST_FUNC void vpushv(SValue *v);
1425 ST_FUNC void save_reg(int r);
1426 ST_FUNC void save_reg_upstack(int r, int n);
1427 ST_FUNC int get_reg(int rc);
1428 ST_FUNC void save_regs(int n);
1429 ST_FUNC void gaddrof(void);
1430 ST_FUNC int gv(int rc);
1431 ST_FUNC void gv2(int rc1, int rc2);
1432 ST_FUNC void vpop(void);
1433 ST_FUNC void gen_op(int op);
1434 ST_FUNC int type_size(CType *type, int *a);
1435 ST_FUNC void mk_pointer(CType *type);
1436 ST_FUNC void vstore(void);
1437 ST_FUNC void inc(int post, int c);
1438 ST_FUNC void parse_mult_str (CString *astr, const char *msg);
1439 ST_FUNC void parse_asm_str(CString *astr);
1440 ST_FUNC void indir(void);
1441 ST_FUNC void unary(void);
1442 ST_FUNC void gexpr(void);
1443 ST_FUNC int expr_const(void);
1444 #if defined CONFIG_TCC_BCHECK || defined TCC_TARGET_C67
1445 ST_FUNC Sym *get_sym_ref(CType *type, Section *sec, unsigned long offset, unsigned long size);
1446 #endif
1447 #if defined TCC_TARGET_X86_64 && !defined TCC_TARGET_PE
1448 ST_FUNC int classify_x86_64_va_arg(CType *ty);
1449 #endif
1450 #ifdef CONFIG_TCC_BCHECK
1451 ST_FUNC void gbound_args(int nb_args);
1452 #endif
1453 
1454 /* ------------ tccelf.c ------------ */
1455 
1456 #define TCC_OUTPUT_FORMAT_ELF    0 /* default output format: ELF */
1457 #define TCC_OUTPUT_FORMAT_BINARY 1 /* binary image output */
1458 #define TCC_OUTPUT_FORMAT_COFF   2 /* COFF */
1459 
1460 #define ARMAG  "!<arch>\012"    /* For COFF and a.out archives */
1461 
1462 typedef struct {
1463     unsigned int n_strx;         /* index into string table of name */
1464     unsigned char n_type;         /* type of symbol */
1465     unsigned char n_other;        /* misc info (usually empty) */
1466     unsigned short n_desc;        /* description field */
1467     unsigned int n_value;        /* value of symbol */
1468 } Stab_Sym;
1469 
1470 ST_FUNC void tccelf_new(TCCState *s);
1471 ST_FUNC void tccelf_delete(TCCState *s);
1472 ST_FUNC void tccelf_stab_new(TCCState *s);
1473 ST_FUNC void tccelf_begin_file(TCCState *s1);
1474 ST_FUNC void tccelf_end_file(TCCState *s1);
1475 #ifdef CONFIG_TCC_BCHECK
1476 ST_FUNC void tccelf_bounds_new(TCCState *s);
1477 #endif
1478 ST_FUNC Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags);
1479 ST_FUNC void section_realloc(Section *sec, unsigned long new_size);
1480 ST_FUNC size_t section_add(Section *sec, addr_t size, int align);
1481 ST_FUNC void *section_ptr_add(Section *sec, addr_t size);
1482 ST_FUNC void section_reserve(Section *sec, unsigned long size);
1483 ST_FUNC Section *find_section(TCCState *s1, const char *name);
1484 ST_FUNC Section *new_symtab(TCCState *s1, const char *symtab_name, int sh_type, int sh_flags, const char *strtab_name, const char *hash_name, int hash_sh_flags);
1485 
1486 ST_FUNC void put_extern_sym2(Sym *sym, int sh_num, addr_t value, unsigned long size, int can_add_underscore);
1487 ST_FUNC void put_extern_sym(Sym *sym, Section *section, addr_t value, unsigned long size);
1488 #if PTR_SIZE == 4
1489 ST_FUNC void greloc(Section *s, Sym *sym, unsigned long offset, int type);
1490 #endif
1491 ST_FUNC void greloca(Section *s, Sym *sym, unsigned long offset, int type, addr_t addend);
1492 
1493 ST_FUNC int put_elf_str(Section *s, const char *sym);
1494 ST_FUNC int put_elf_sym(Section *s, addr_t value, unsigned long size, int info, int other, int shndx, const char *name);
1495 ST_FUNC int set_elf_sym(Section *s, addr_t value, unsigned long size, int info, int other, int shndx, const char *name);
1496 ST_FUNC int find_elf_sym(Section *s, const char *name);
1497 ST_FUNC void put_elf_reloc(Section *symtab, Section *s, unsigned long offset, int type, int symbol);
1498 ST_FUNC void put_elf_reloca(Section *symtab, Section *s, unsigned long offset, int type, int symbol, addr_t addend);
1499 
1500 ST_FUNC void put_stabs(TCCState *s1, const char *str, int type, int other, int desc, unsigned long value);
1501 ST_FUNC void put_stabs_r(TCCState *s1, const char *str, int type, int other, int desc, unsigned long value, Section *sec, int sym_index);
1502 ST_FUNC void put_stabn(TCCState *s1, int type, int other, int desc, int value);
1503 
1504 ST_FUNC void resolve_common_syms(TCCState *s1);
1505 ST_FUNC void relocate_syms(TCCState *s1, Section *symtab, int do_resolve);
1506 ST_FUNC void relocate_section(TCCState *s1, Section *s);
1507 
1508 ST_FUNC int tcc_object_type(int fd, ElfW(Ehdr) *h);
1509 ST_FUNC int tcc_load_object_file(TCCState *s1, int fd, unsigned long file_offset);
1510 ST_FUNC int tcc_load_archive(TCCState *s1, int fd, int alacarte);
1511 ST_FUNC void add_array(TCCState *s1, const char *sec, int c);
1512 
1513 #ifndef ELF_OBJ_ONLY
1514 ST_FUNC void build_got_entries(TCCState *s1);
1515 #endif
1516 ST_FUNC struct sym_attr *get_sym_attr(TCCState *s1, int index, int alloc);
1517 ST_FUNC void squeeze_multi_relocs(Section *sec, size_t oldrelocoffset);
1518 
1519 ST_FUNC addr_t get_elf_sym_addr(TCCState *s, const char *name, int err);
1520 ST_FUNC void list_elf_symbols(TCCState *s, void *ctx,
1521     void (*symbol_cb)(void *ctx, const char *name, const void *val));
1522 #if defined TCC_IS_NATIVE || defined TCC_TARGET_PE
1523 ST_FUNC void *tcc_get_symbol_err(TCCState *s, const char *name);
1524 #endif
1525 
1526 ST_FUNC int set_global_sym(TCCState *s1, const char *name, Section *sec, long offs);
1527 
1528 #ifndef TCC_TARGET_PE
1529 ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level);
1530 ST_FUNC int tcc_load_ldscript(TCCState *s1, int fd);
1531 ST_FUNC void tcc_add_runtime(TCCState *s1);
1532 #endif
1533 
1534 /* ------------ xxx-link.c ------------ */
1535 
1536 /* Whether to generate a GOT/PLT entry and when. NO_GOTPLT_ENTRY is first so
1537    that unknown relocation don't create a GOT or PLT entry */
1538 enum gotplt_entry {
1539     NO_GOTPLT_ENTRY,	/* never generate (eg. GLOB_DAT & JMP_SLOT relocs) */
1540     BUILD_GOT_ONLY,	/* only build GOT (eg. TPOFF relocs) */
1541     AUTO_GOTPLT_ENTRY,	/* generate if sym is UNDEF */
1542     ALWAYS_GOTPLT_ENTRY	/* always generate (eg. PLTOFF relocs) */
1543 };
1544 
1545 #ifndef ELF_OBJ_ONLY
1546 ST_FUNC int code_reloc (int reloc_type);
1547 ST_FUNC int gotplt_entry_type (int reloc_type);
1548 ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr);
1549 ST_FUNC void relocate_plt(TCCState *s1);
1550 #endif
1551 ST_FUNC void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val);
1552 
1553 /* ------------ xxx-gen.c ------------ */
1554 
1555 ST_DATA const int reg_classes[NB_REGS];
1556 
1557 ST_FUNC void gsym_addr(int t, int a);
1558 ST_FUNC void gsym(int t);
1559 ST_FUNC void load(int r, SValue *sv);
1560 ST_FUNC void store(int r, SValue *v);
1561 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *align, int *regsize);
1562 ST_FUNC void gfunc_call(int nb_args);
1563 ST_FUNC void gfunc_prolog(Sym *func_sym);
1564 ST_FUNC void gfunc_epilog(void);
1565 ST_FUNC void gen_fill_nops(int);
1566 ST_FUNC int gjmp(int t);
1567 ST_FUNC void gjmp_addr(int a);
1568 ST_FUNC int gjmp_cond(int op, int t);
1569 ST_FUNC int gjmp_append(int n, int t);
1570 ST_FUNC void gen_opi(int op);
1571 ST_FUNC void gen_opf(int op);
1572 ST_FUNC void gen_cvt_ftoi(int t);
1573 ST_FUNC void gen_cvt_itof(int t);
1574 ST_FUNC void gen_cvt_ftof(int t);
1575 ST_FUNC void ggoto(void);
1576 #ifndef TCC_TARGET_C67
1577 ST_FUNC void o(unsigned int c);
1578 #endif
1579 ST_FUNC void gen_vla_sp_save(int addr);
1580 ST_FUNC void gen_vla_sp_restore(int addr);
1581 ST_FUNC void gen_vla_alloc(CType *type, int align);
1582 
read16le(unsigned char *p)1583 static inline uint16_t read16le(unsigned char *p) {
1584     return p[0] | (uint16_t)p[1] << 8;
1585 }
write16le(unsigned char *p, uint16_t x)1586 static inline void write16le(unsigned char *p, uint16_t x) {
1587     p[0] = x & 255;  p[1] = x >> 8 & 255;
1588 }
read32le(unsigned char *p)1589 static inline uint32_t read32le(unsigned char *p) {
1590   return read16le(p) | (uint32_t)read16le(p + 2) << 16;
1591 }
write32le(unsigned char *p, uint32_t x)1592 static inline void write32le(unsigned char *p, uint32_t x) {
1593     write16le(p, x);  write16le(p + 2, x >> 16);
1594 }
add32le(unsigned char *p, int32_t x)1595 static inline void add32le(unsigned char *p, int32_t x) {
1596     write32le(p, read32le(p) + x);
1597 }
read64le(unsigned char *p)1598 static inline uint64_t read64le(unsigned char *p) {
1599   return read32le(p) | (uint64_t)read32le(p + 4) << 32;
1600 }
write64le(unsigned char *p, uint64_t x)1601 static inline void write64le(unsigned char *p, uint64_t x) {
1602     write32le(p, x);  write32le(p + 4, x >> 32);
1603 }
add64le(unsigned char *p, int64_t x)1604 static inline void add64le(unsigned char *p, int64_t x) {
1605     write64le(p, read64le(p) + x);
1606 }
1607 
1608 /* ------------ i386-gen.c ------------ */
1609 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
1610 ST_FUNC void g(int c);
1611 ST_FUNC void gen_le16(int c);
1612 ST_FUNC void gen_le32(int c);
1613 ST_FUNC void gen_addr32(int r, Sym *sym, int c);
1614 ST_FUNC void gen_addrpc32(int r, Sym *sym, int c);
1615 ST_FUNC void gen_cvt_csti(int t);
1616 #endif
1617 
1618 #ifdef CONFIG_TCC_BCHECK
1619 ST_FUNC void gen_bounded_ptr_add(void);
1620 ST_FUNC void gen_bounded_ptr_deref(void);
1621 #endif
1622 
1623 /* ------------ x86_64-gen.c ------------ */
1624 #ifdef TCC_TARGET_X86_64
1625 ST_FUNC void gen_addr64(int r, Sym *sym, int64_t c);
1626 ST_FUNC void gen_opl(int op);
1627 #ifdef TCC_TARGET_PE
1628 ST_FUNC void gen_vla_result(int addr);
1629 #endif
1630 ST_FUNC void gen_cvt_sxtw(void);
1631 ST_FUNC void gen_cvt_csti(int t);
1632 #endif
1633 
1634 /* ------------ arm-gen.c ------------ */
1635 #ifdef TCC_TARGET_ARM
1636 #if defined(TCC_ARM_EABI) && !defined(CONFIG_TCC_ELFINTERP)
1637 PUB_FUNC const char *default_elfinterp(struct TCCState *s);
1638 #endif
1639 ST_FUNC void arm_init(struct TCCState *s);
1640 #endif
1641 
1642 /* ------------ arm64-gen.c ------------ */
1643 #ifdef TCC_TARGET_ARM64
1644 ST_FUNC void gen_opl(int op);
1645 ST_FUNC void gfunc_return(CType *func_type);
1646 ST_FUNC void gen_va_start(void);
1647 ST_FUNC void gen_va_arg(CType *t);
1648 ST_FUNC void gen_clear_cache(void);
1649 ST_FUNC void gen_cvt_sxtw(void);
1650 ST_FUNC void gen_cvt_csti(int t);
1651 #endif
1652 
1653 /* ------------ riscv64-gen.c ------------ */
1654 #ifdef TCC_TARGET_RISCV64
1655 ST_FUNC void gen_opl(int op);
1656 //ST_FUNC void gfunc_return(CType *func_type);
1657 ST_FUNC void gen_va_start(void);
1658 ST_FUNC void arch_transfer_ret_regs(int);
1659 ST_FUNC void gen_cvt_sxtw(void);
1660 #endif
1661 
1662 /* ------------ c67-gen.c ------------ */
1663 #ifdef TCC_TARGET_C67
1664 #endif
1665 
1666 /* ------------ tcccoff.c ------------ */
1667 
1668 #ifdef TCC_TARGET_COFF
1669 ST_FUNC int tcc_output_coff(TCCState *s1, FILE *f);
1670 ST_FUNC int tcc_load_coff(TCCState * s1, int fd);
1671 #endif
1672 
1673 /* ------------ tccasm.c ------------ */
1674 ST_FUNC void asm_instr(void);
1675 ST_FUNC void asm_global_instr(void);
1676 #ifdef CONFIG_TCC_ASM
1677 ST_FUNC int find_constraint(ASMOperand *operands, int nb_operands, const char *name, const char **pp);
1678 ST_FUNC Sym* get_asm_sym(int name, Sym *csym);
1679 ST_FUNC void asm_expr(TCCState *s1, ExprValue *pe);
1680 ST_FUNC int asm_int_expr(TCCState *s1);
1681 ST_FUNC int tcc_assemble(TCCState *s1, int do_preprocess);
1682 /* ------------ i386-asm.c ------------ */
1683 ST_FUNC void gen_expr32(ExprValue *pe);
1684 #ifdef TCC_TARGET_X86_64
1685 ST_FUNC void gen_expr64(ExprValue *pe);
1686 #endif
1687 ST_FUNC void asm_opcode(TCCState *s1, int opcode);
1688 ST_FUNC int asm_parse_regvar(int t);
1689 ST_FUNC void asm_compute_constraints(ASMOperand *operands, int nb_operands, int nb_outputs, const uint8_t *clobber_regs, int *pout_reg);
1690 ST_FUNC void subst_asm_operand(CString *add_str, SValue *sv, int modifier);
1691 ST_FUNC void asm_gen_code(ASMOperand *operands, int nb_operands, int nb_outputs, int is_output, uint8_t *clobber_regs, int out_reg);
1692 ST_FUNC void asm_clobber(uint8_t *clobber_regs, const char *str);
1693 #endif
1694 
1695 /* ------------ tccpe.c -------------- */
1696 #ifdef TCC_TARGET_PE
1697 ST_FUNC int pe_load_file(struct TCCState *s1, const char *filename, int fd);
1698 ST_FUNC int pe_output_file(TCCState * s1, const char *filename);
1699 ST_FUNC int pe_putimport(TCCState *s1, int dllindex, const char *name, addr_t value);
1700 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
1701 ST_FUNC SValue *pe_getimport(SValue *sv, SValue *v2);
1702 #endif
1703 #ifdef TCC_TARGET_X86_64
1704 ST_FUNC void pe_add_unwind_data(unsigned start, unsigned end, unsigned stack);
1705 #endif
1706 PUB_FUNC int tcc_get_dllexports(const char *filename, char **pp);
1707 /* symbol properties stored in Elf32_Sym->st_other */
1708 # define ST_PE_EXPORT 0x10
1709 # define ST_PE_IMPORT 0x20
1710 # define ST_PE_STDCALL 0x40
1711 #endif
1712 #define ST_ASM_SET 0x04
1713 
1714 /* ------------ tccrun.c ----------------- */
1715 #ifdef TCC_IS_NATIVE
1716 #ifdef CONFIG_TCC_STATIC
1717 #define RTLD_LAZY       0x001
1718 #define RTLD_NOW        0x002
1719 #define RTLD_GLOBAL     0x100
1720 #define RTLD_DEFAULT    NULL
1721 /* dummy function for profiling */
1722 ST_FUNC void *dlopen(const char *filename, int flag);
1723 ST_FUNC void dlclose(void *p);
1724 ST_FUNC const char *dlerror(void);
1725 ST_FUNC void *dlsym(void *handle, const char *symbol);
1726 #endif
1727 ST_FUNC void tcc_run_free(TCCState *s1);
1728 #endif
1729 
1730 /* ------------ tcctools.c ----------------- */
1731 #if 0 /* included in tcc.c */
1732 ST_FUNC int tcc_tool_ar(TCCState *s, int argc, char **argv);
1733 #ifdef TCC_TARGET_PE
1734 ST_FUNC int tcc_tool_impdef(TCCState *s, int argc, char **argv);
1735 #endif
1736 ST_FUNC void tcc_tool_cross(TCCState *s, char **argv, int option);
1737 ST_FUNC void gen_makedeps(TCCState *s, const char *target, const char *filename);
1738 #endif
1739 
1740 /********************************************************/
1741 #undef ST_DATA
1742 #if ONE_SOURCE
1743 #define ST_DATA static
1744 #else
1745 #define ST_DATA
1746 #endif
1747 /********************************************************/
1748 
1749 #define text_section        TCC_STATE_VAR(text_section)
1750 #define data_section        TCC_STATE_VAR(data_section)
1751 #define bss_section         TCC_STATE_VAR(bss_section)
1752 #define common_section      TCC_STATE_VAR(common_section)
1753 #define cur_text_section    TCC_STATE_VAR(cur_text_section)
1754 #define bounds_section      TCC_STATE_VAR(bounds_section)
1755 #define lbounds_section     TCC_STATE_VAR(lbounds_section)
1756 #define symtab_section      TCC_STATE_VAR(symtab_section)
1757 #define stab_section        TCC_STATE_VAR(stab_section)
1758 #define stabstr_section     stab_section->link
1759 #define gnu_ext             TCC_STATE_VAR(gnu_ext)
1760 #define tcc_error_noabort   TCC_SET_STATE(_tcc_error_noabort)
1761 #define tcc_error           TCC_SET_STATE(_tcc_error)
1762 #define tcc_warning         TCC_SET_STATE(_tcc_warning)
1763 
1764 #define total_idents        TCC_STATE_VAR(total_idents)
1765 #define total_lines         TCC_STATE_VAR(total_lines)
1766 #define total_bytes         TCC_STATE_VAR(total_bytes)
1767 
1768 PUB_FUNC void tcc_enter_state(TCCState *s1);
1769 
1770 /********************************************************/
1771 #endif /* _TCC_H */
1772 
1773 #undef TCC_STATE_VAR
1774 #undef TCC_SET_STATE
1775 
1776 #ifdef USING_GLOBALS
1777 # define TCC_STATE_VAR(sym) tcc_state->sym
1778 # define TCC_SET_STATE(fn) fn
1779 # undef USING_GLOBALS
1780 #else
1781 # define TCC_STATE_VAR(sym) s1->sym
1782 # define TCC_SET_STATE(fn) (tcc_enter_state(s1),fn)
1783 /* actually we could avoid the tcc_enter_state(s1) hack by using
1784    __VA_ARGS__ except that some compiler doesn't support it. */
1785 #endif
1786