[279] | 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 | #ifdef CONFIG_TCCBOOT
|
---|
| 28 | #include "tccboot.h"
|
---|
| 29 | #define CONFIG_TCC_STATIC
|
---|
| 30 | #else
|
---|
| 31 |
|
---|
| 32 | #include <stdlib.h>
|
---|
| 33 | #include <stdio.h>
|
---|
| 34 | #include <stdarg.h>
|
---|
| 35 | #include <string.h>
|
---|
| 36 | #include <errno.h>
|
---|
| 37 | #include <math.h>
|
---|
| 38 | #include <signal.h>
|
---|
| 39 | #define SA_RESETHAND 0x80000000
|
---|
| 40 | #define FPE_INTDIV 1
|
---|
| 41 | #define FPE_FLTDIV 3
|
---|
| 42 | typedef struct { int si_signo, si_errno, si_code; } siginfo_t;
|
---|
| 43 | #include <fcntl.h>
|
---|
| 44 | #include <setjmp.h>
|
---|
| 45 | #include <time.h>
|
---|
| 46 |
|
---|
| 47 | #ifndef _WIN32
|
---|
| 48 | # include <unistd.h>
|
---|
| 49 | # include <sys/time.h>
|
---|
| 50 | /*# include <sys/ucontext.h>*/
|
---|
| 51 | typedef struct __ucontext ucontext_t;
|
---|
| 52 | /*# include <sys/mman.h>*/
|
---|
| 53 | #define PROT_READ 1
|
---|
| 54 | #define PROT_WRITE 2
|
---|
| 55 | #define PROT_EXEC 4
|
---|
| 56 | # ifndef CONFIG_TCC_STATIC
|
---|
| 57 | # include <dlfcn.h>
|
---|
| 58 | # endif
|
---|
| 59 | #else
|
---|
| 60 | # include <unistd.h>
|
---|
| 61 | # include <sys/time.h>
|
---|
| 62 | # include <sys/ucontext.h>
|
---|
| 63 | # include <sys/mman.h>
|
---|
| 64 | # include <stdint.h>
|
---|
| 65 | # define inline __inline
|
---|
| 66 | # define inp next_inp
|
---|
| 67 | # ifdef LIBTCC_AS_DLL
|
---|
| 68 | # define LIBTCCAPI __declspec(dllexport)
|
---|
| 69 | # define PUB_FUNC LIBTCCAPI
|
---|
| 70 | # endif
|
---|
| 71 | #endif
|
---|
| 72 |
|
---|
| 73 | #endif /* !CONFIG_TCCBOOT */
|
---|
| 74 |
|
---|
| 75 | #ifndef O_BINARY
|
---|
| 76 | # define O_BINARY 0
|
---|
| 77 | #endif
|
---|
| 78 |
|
---|
| 79 | #include "elf.h"
|
---|
| 80 | #ifdef TCC_TARGET_X86_64
|
---|
| 81 | # define ELFCLASSW ELFCLASS64
|
---|
| 82 | # define ElfW(type) Elf##64##_##type
|
---|
| 83 | # define ELFW(type) ELF##64##_##type
|
---|
| 84 | # define ElfW_Rel ElfW(Rela)
|
---|
| 85 | # define SHT_RELX SHT_RELA
|
---|
| 86 | # define REL_SECTION_FMT ".rela%s"
|
---|
| 87 | /* XXX: DLL with PLT would only work with x86-64 for now */
|
---|
| 88 | # define TCC_OUTPUT_DLL_WITH_PLT
|
---|
| 89 | #else
|
---|
| 90 | # define ELFCLASSW ELFCLASS32
|
---|
| 91 | # define ElfW(type) Elf##32##_##type
|
---|
| 92 | # define ELFW(type) ELF##32##_##type
|
---|
| 93 | # define ElfW_Rel ElfW(Rel)
|
---|
| 94 | # define SHT_RELX SHT_REL
|
---|
| 95 | # define REL_SECTION_FMT ".rel%s"
|
---|
| 96 | #endif
|
---|
| 97 |
|
---|
| 98 | /* target address type */
|
---|
| 99 | #define addr_t ElfW(Addr)
|
---|
| 100 |
|
---|
| 101 | #include "stab.h"
|
---|
| 102 | #include "libtcc.h"
|
---|
| 103 |
|
---|
| 104 | /* parser debug */
|
---|
| 105 | //#define PARSE_DEBUG
|
---|
| 106 | /* preprocessor debug */
|
---|
| 107 | //#define PP_DEBUG
|
---|
| 108 | /* include file debug */
|
---|
| 109 | //#define INC_DEBUG
|
---|
| 110 | /* memory leak debug */
|
---|
| 111 | //#define MEM_DEBUG
|
---|
| 112 | /* assembler debug */
|
---|
| 113 | //#define ASM_DEBUG
|
---|
| 114 |
|
---|
| 115 | /* target selection */
|
---|
| 116 | //#define TCC_TARGET_I386 /* i386 code generator */
|
---|
| 117 | //#define TCC_TARGET_ARM /* ARMv4 code generator */
|
---|
| 118 | //#define TCC_TARGET_C67 /* TMS320C67xx code generator */
|
---|
| 119 | //#define TCC_TARGET_X86_64 /* x86-64 code generator */
|
---|
| 120 | //#define TCC_TARGET_IL /* IL code generator */
|
---|
| 121 |
|
---|
| 122 | /* default target is I386 */
|
---|
| 123 | #if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_ARM) && \
|
---|
| 124 | !defined(TCC_TARGET_C67) && !defined(TCC_TARGET_X86_64) && \
|
---|
| 125 | !defined(TCC_TARGET_IL)
|
---|
| 126 | #define TCC_TARGET_I386
|
---|
| 127 | #endif
|
---|
| 128 |
|
---|
| 129 | #if !defined(TCC_UCLIBC) && !defined(TCC_TARGET_ARM) && \
|
---|
| 130 | !defined(TCC_TARGET_C67) && !defined(TCC_TARGET_X86_64)
|
---|
| 131 | #define CONFIG_TCC_BCHECK /* enable bound checking code */
|
---|
| 132 | #endif
|
---|
| 133 |
|
---|
| 134 | /* define it to include assembler support */
|
---|
| 135 | #if !defined(TCC_TARGET_ARM) && !defined(TCC_TARGET_C67)
|
---|
| 136 | #define CONFIG_TCC_ASM
|
---|
| 137 | #endif
|
---|
| 138 |
|
---|
| 139 | /* object format selection */
|
---|
| 140 | #if defined(TCC_TARGET_C67)
|
---|
| 141 | #define TCC_TARGET_COFF
|
---|
| 142 | #endif
|
---|
| 143 |
|
---|
| 144 | /* only native compiler supports -run */
|
---|
| 145 | #if defined _WIN32 == defined TCC_TARGET_PE
|
---|
| 146 | # if (defined __i386__ || defined _X86_) && defined TCC_TARGET_I386
|
---|
| 147 | # define TCC_IS_NATIVE
|
---|
| 148 | # elif (defined __x86_64__ || defined _AMD64_) && defined TCC_TARGET_X86_64
|
---|
| 149 | # define TCC_IS_NATIVE
|
---|
| 150 | # elif defined __arm__ && defined TCC_TARGET_ARM
|
---|
| 151 | # define TCC_IS_NATIVE
|
---|
| 152 | # endif
|
---|
| 153 | #endif
|
---|
| 154 |
|
---|
| 155 | #if defined TCC_IS_NATIVE && !defined CONFIG_TCCBOOT
|
---|
| 156 | # define CONFIG_TCC_BACKTRACE
|
---|
| 157 | #endif
|
---|
| 158 |
|
---|
| 159 | /* ------------ path configuration ------------ */
|
---|
| 160 |
|
---|
| 161 | #ifndef CONFIG_SYSROOT
|
---|
| 162 | # define CONFIG_SYSROOT ""
|
---|
| 163 | #endif
|
---|
| 164 | #ifndef CONFIG_TCCDIR
|
---|
| 165 | # define CONFIG_TCCDIR "."
|
---|
| 166 | #endif
|
---|
| 167 | #ifndef CONFIG_LDDIR
|
---|
| 168 | # define CONFIG_LDDIR "lib"
|
---|
| 169 | #endif
|
---|
| 170 |
|
---|
| 171 | /* path to find crt1.o, crti.o and crtn.o */
|
---|
| 172 | #ifndef CONFIG_TCC_CRTPREFIX
|
---|
| 173 | # define CONFIG_TCC_CRTPREFIX CONFIG_SYSROOT "/usr/" CONFIG_LDDIR
|
---|
| 174 | #endif
|
---|
| 175 |
|
---|
| 176 | /* Below: {B} is substituted by CONFIG_TCCDIR (rsp. -B option) */
|
---|
| 177 |
|
---|
| 178 | /* system include paths */
|
---|
| 179 | #ifndef CONFIG_TCC_SYSINCLUDEPATHS
|
---|
| 180 | # ifdef TCC_TARGET_PE
|
---|
| 181 | # define CONFIG_TCC_SYSINCLUDEPATHS "{B}/include;{B}/include/winapi"
|
---|
| 182 | # elif defined CONFIG_MULTIARCHDIR
|
---|
| 183 | # define CONFIG_TCC_SYSINCLUDEPATHS \
|
---|
| 184 | CONFIG_SYSROOT "/usr/local/include" \
|
---|
| 185 | ":" CONFIG_SYSROOT "/usr/local/include/" CONFIG_MULTIARCHDIR \
|
---|
| 186 | ":" CONFIG_SYSROOT "/usr/include" \
|
---|
| 187 | ":" CONFIG_SYSROOT "/usr/include/" CONFIG_MULTIARCHDIR \
|
---|
| 188 | ":" "{B}/include"
|
---|
| 189 | # else
|
---|
| 190 | # define CONFIG_TCC_SYSINCLUDEPATHS \
|
---|
| 191 | CONFIG_SYSROOT "/usr/local/include" \
|
---|
| 192 | ":" CONFIG_SYSROOT "/usr/include" \
|
---|
| 193 | ":" "{B}/include"
|
---|
| 194 | # endif
|
---|
| 195 | #endif
|
---|
| 196 |
|
---|
| 197 | /* library search paths */
|
---|
| 198 | #ifndef CONFIG_TCC_LIBPATHS
|
---|
| 199 | # ifdef TCC_TARGET_PE
|
---|
| 200 | # define CONFIG_TCC_LIBPATHS "{B}/lib"
|
---|
| 201 | # else
|
---|
| 202 | # define CONFIG_TCC_LIBPATHS \
|
---|
| 203 | CONFIG_SYSROOT "/usr/" CONFIG_LDDIR \
|
---|
| 204 | ":" CONFIG_SYSROOT "/" CONFIG_LDDIR \
|
---|
| 205 | ":" CONFIG_SYSROOT "/usr/local/" CONFIG_LDDIR
|
---|
| 206 | # endif
|
---|
| 207 | #endif
|
---|
| 208 |
|
---|
| 209 | /* name of ELF interpreter */
|
---|
| 210 | #ifndef CONFIG_TCC_ELFINTERP
|
---|
| 211 | # if defined __FreeBSD__
|
---|
| 212 | # define CONFIG_TCC_ELFINTERP "/libexec/ld-elf.so.1"
|
---|
| 213 | # elif defined __FreeBSD_kernel__
|
---|
| 214 | # define CONFIG_TCC_ELFINTERP "/lib/ld.so.1"
|
---|
| 215 | # elif defined TCC_ARM_HARDFLOAT
|
---|
| 216 | # define CONFIG_TCC_ELFINTERP "/lib/ld-linux-armhf.so.3"
|
---|
| 217 | # elif defined TCC_ARM_EABI
|
---|
| 218 | # define CONFIG_TCC_ELFINTERP "/lib/ld-linux.so.3"
|
---|
| 219 | # elif defined(TCC_TARGET_X86_64)
|
---|
| 220 | # define CONFIG_TCC_ELFINTERP "/lib64/ld-linux-x86-64.so.2"
|
---|
| 221 | # elif defined(TCC_UCLIBC)
|
---|
| 222 | # define CONFIG_TCC_ELFINTERP "/lib/ld-uClibc.so.0"
|
---|
| 223 | # elif defined(TCC_TARGET_PE)
|
---|
| 224 | # define CONFIG_TCC_ELFINTERP "-"
|
---|
| 225 | # else
|
---|
| 226 | # define CONFIG_TCC_ELFINTERP "/lib/ld-linux.so.2"
|
---|
| 227 | # endif
|
---|
| 228 | #endif
|
---|
| 229 |
|
---|
| 230 | /* library to use with CONFIG_USE_LIBGCC instead of libtcc1.a */
|
---|
| 231 | #define TCC_LIBGCC CONFIG_SYSROOT "/" CONFIG_LDDIR "/libgcc_s.so.1"
|
---|
| 232 |
|
---|
| 233 | /* -------------------------------------------- */
|
---|
| 234 | /* include the target specific definitions */
|
---|
| 235 |
|
---|
| 236 | #define TARGET_DEFS_ONLY
|
---|
| 237 | #ifdef TCC_TARGET_I386
|
---|
| 238 | # include "i386-gen.c"
|
---|
| 239 | #endif
|
---|
| 240 | #ifdef TCC_TARGET_X86_64
|
---|
| 241 | # include "x86_64-gen.c"
|
---|
| 242 | #endif
|
---|
| 243 | #ifdef TCC_TARGET_ARM
|
---|
| 244 | # include "arm-gen.c"
|
---|
| 245 | #endif
|
---|
| 246 | #ifdef TCC_TARGET_C67
|
---|
| 247 | # include "coff.h"
|
---|
| 248 | # include "c67-gen.c"
|
---|
| 249 | #endif
|
---|
| 250 | #ifdef TCC_TARGET_IL
|
---|
| 251 | #include "il-gen.c"
|
---|
| 252 | #endif
|
---|
| 253 | #undef TARGET_DEFS_ONLY
|
---|
| 254 |
|
---|
| 255 | /* -------------------------------------------- */
|
---|
| 256 |
|
---|
| 257 | #define INCLUDE_STACK_SIZE 32
|
---|
| 258 | #define IFDEF_STACK_SIZE 64
|
---|
| 259 | #define VSTACK_SIZE 256
|
---|
| 260 | #define STRING_MAX_SIZE 1024
|
---|
| 261 | #define PACK_STACK_SIZE 8
|
---|
| 262 |
|
---|
| 263 | #define TOK_HASH_SIZE 8192 /* must be a power of two */
|
---|
| 264 | #define TOK_ALLOC_INCR 512 /* must be a power of two */
|
---|
| 265 | #define TOK_MAX_SIZE 4 /* token max size in int unit when stored in string */
|
---|
| 266 |
|
---|
| 267 | /* token symbol management */
|
---|
| 268 | typedef struct TokenSym {
|
---|
| 269 | struct TokenSym *hash_next;
|
---|
| 270 | struct Sym *sym_define; /* direct pointer to define */
|
---|
| 271 | struct Sym *sym_label; /* direct pointer to label */
|
---|
| 272 | struct Sym *sym_struct; /* direct pointer to structure */
|
---|
| 273 | struct Sym *sym_identifier; /* direct pointer to identifier */
|
---|
| 274 | int tok; /* token number */
|
---|
| 275 | int len;
|
---|
| 276 | char str[1];
|
---|
| 277 | } TokenSym;
|
---|
| 278 |
|
---|
| 279 | #ifdef TCC_TARGET_PE
|
---|
| 280 | typedef unsigned short nwchar_t;
|
---|
| 281 | #else
|
---|
| 282 | typedef int nwchar_t;
|
---|
| 283 | #endif
|
---|
| 284 |
|
---|
| 285 | typedef struct CString {
|
---|
| 286 | int size; /* size in bytes */
|
---|
| 287 | void *data; /* either 'char *' or 'nwchar_t *' */
|
---|
| 288 | int size_allocated;
|
---|
| 289 | void *data_allocated; /* if non NULL, data has been malloced */
|
---|
| 290 | } CString;
|
---|
| 291 |
|
---|
| 292 | /* type definition */
|
---|
| 293 | typedef struct CType {
|
---|
| 294 | int t;
|
---|
| 295 | struct Sym *ref;
|
---|
| 296 | } CType;
|
---|
| 297 |
|
---|
| 298 | /* constant value */
|
---|
| 299 | typedef union CValue {
|
---|
| 300 | long double ld;
|
---|
| 301 | double d;
|
---|
| 302 | float f;
|
---|
| 303 | int i;
|
---|
| 304 | unsigned int ui;
|
---|
| 305 | unsigned int ul; /* address (should be unsigned long on 64 bit cpu) */
|
---|
| 306 | long long ll;
|
---|
| 307 | unsigned long long ull;
|
---|
| 308 | struct CString *cstr;
|
---|
| 309 | void *ptr;
|
---|
| 310 | int tab[LDOUBLE_SIZE/4];
|
---|
| 311 | } CValue;
|
---|
| 312 |
|
---|
| 313 | /* value on stack */
|
---|
| 314 | typedef struct SValue {
|
---|
| 315 | CType type; /* type */
|
---|
| 316 | unsigned short r; /* register + flags */
|
---|
| 317 | unsigned short r2; /* second register, used for 'long long'
|
---|
| 318 | type. If not used, set to VT_CONST */
|
---|
| 319 | CValue c; /* constant, if VT_CONST */
|
---|
| 320 | struct Sym *sym; /* symbol, if (VT_SYM | VT_CONST) */
|
---|
| 321 | } SValue;
|
---|
| 322 |
|
---|
| 323 | /* symbol management */
|
---|
| 324 | typedef struct Sym {
|
---|
| 325 | int v; /* symbol token */
|
---|
| 326 | char *asm_label; /* associated asm label */
|
---|
| 327 | long r; /* associated register */
|
---|
| 328 | union {
|
---|
| 329 | long c; /* associated number */
|
---|
| 330 | int *d; /* define token stream */
|
---|
| 331 | };
|
---|
| 332 | CType type; /* associated type */
|
---|
| 333 | union {
|
---|
| 334 | struct Sym *next; /* next related symbol */
|
---|
| 335 | long jnext; /* next jump label */
|
---|
| 336 | };
|
---|
| 337 | struct Sym *prev; /* prev symbol in stack */
|
---|
| 338 | struct Sym *prev_tok; /* previous symbol for this token */
|
---|
| 339 | } Sym;
|
---|
| 340 |
|
---|
| 341 | /* section definition */
|
---|
| 342 | /* XXX: use directly ELF structure for parameters ? */
|
---|
| 343 | /* special flag to indicate that the section should not be linked to
|
---|
| 344 | the other ones */
|
---|
| 345 | #define SHF_PRIVATE 0x80000000
|
---|
| 346 |
|
---|
| 347 | /* special flag, too */
|
---|
| 348 | #define SECTION_ABS ((void *)1)
|
---|
| 349 |
|
---|
| 350 | typedef struct Section {
|
---|
| 351 | unsigned long data_offset; /* current data offset */
|
---|
| 352 | unsigned char *data; /* section data */
|
---|
| 353 | unsigned long data_allocated; /* used for realloc() handling */
|
---|
| 354 | int sh_name; /* elf section name (only used during output) */
|
---|
| 355 | int sh_num; /* elf section number */
|
---|
| 356 | int sh_type; /* elf section type */
|
---|
| 357 | int sh_flags; /* elf section flags */
|
---|
| 358 | int sh_info; /* elf section info */
|
---|
| 359 | int sh_addralign; /* elf section alignment */
|
---|
| 360 | int sh_entsize; /* elf entry size */
|
---|
| 361 | unsigned long sh_size; /* section size (only used during output) */
|
---|
| 362 | addr_t sh_addr; /* address at which the section is relocated */
|
---|
| 363 | unsigned long sh_offset; /* file offset */
|
---|
| 364 | int nb_hashed_syms; /* used to resize the hash table */
|
---|
| 365 | struct Section *link; /* link to another section */
|
---|
| 366 | struct Section *reloc; /* corresponding section for relocation, if any */
|
---|
| 367 | struct Section *hash; /* hash table for symbols */
|
---|
| 368 | struct Section *next;
|
---|
| 369 | char name[1]; /* section name */
|
---|
| 370 | } Section;
|
---|
| 371 |
|
---|
| 372 | typedef struct DLLReference {
|
---|
| 373 | int level;
|
---|
| 374 | void *handle;
|
---|
| 375 | char name[1];
|
---|
| 376 | } DLLReference;
|
---|
| 377 |
|
---|
| 378 | /* GNUC attribute definition */
|
---|
| 379 | typedef struct AttributeDef {
|
---|
| 380 | unsigned
|
---|
| 381 | func_call : 3, /* calling convention (0..5), see below */
|
---|
| 382 | aligned : 5, /* alignement (0..16) */
|
---|
| 383 | packed : 1,
|
---|
| 384 | func_export : 1,
|
---|
| 385 | func_import : 1,
|
---|
| 386 | func_args : 5,
|
---|
| 387 | mode : 4,
|
---|
| 388 | weak : 1,
|
---|
| 389 | fill : 11;
|
---|
| 390 | struct Section *section;
|
---|
| 391 | int alias_target; /* token */
|
---|
| 392 | } AttributeDef;
|
---|
| 393 |
|
---|
| 394 | /* gr: wrappers for casting sym->r for other purposes */
|
---|
| 395 | #define FUNC_CALL(r) (((AttributeDef*)&(r))->func_call)
|
---|
| 396 | #define FUNC_EXPORT(r) (((AttributeDef*)&(r))->func_export)
|
---|
| 397 | #define FUNC_IMPORT(r) (((AttributeDef*)&(r))->func_import)
|
---|
| 398 | #define FUNC_ARGS(r) (((AttributeDef*)&(r))->func_args)
|
---|
| 399 | #define FUNC_ALIGN(r) (((AttributeDef*)&(r))->aligned)
|
---|
| 400 | #define FUNC_PACKED(r) (((AttributeDef*)&(r))->packed)
|
---|
| 401 | #define ATTR_MODE(r) (((AttributeDef*)&(r))->mode)
|
---|
| 402 | #define INT_ATTR(ad) (*(int*)(ad))
|
---|
| 403 |
|
---|
| 404 | /* -------------------------------------------------- */
|
---|
| 405 |
|
---|
| 406 | #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
|
---|
| 407 | #define SYM_FIELD 0x20000000 /* struct/union field symbol space */
|
---|
| 408 | #define SYM_FIRST_ANOM 0x10000000 /* first anonymous sym */
|
---|
| 409 |
|
---|
| 410 | /* stored in 'Sym.c' field */
|
---|
| 411 | #define FUNC_NEW 1 /* ansi function prototype */
|
---|
| 412 | #define FUNC_OLD 2 /* old function prototype */
|
---|
| 413 | #define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */
|
---|
| 414 |
|
---|
| 415 | /* stored in 'Sym.r' field */
|
---|
| 416 | #define FUNC_CDECL 0 /* standard c call */
|
---|
| 417 | #define FUNC_STDCALL 1 /* pascal c call */
|
---|
| 418 | #define FUNC_FASTCALL1 2 /* first param in %eax */
|
---|
| 419 | #define FUNC_FASTCALL2 3 /* first parameters in %eax, %edx */
|
---|
| 420 | #define FUNC_FASTCALL3 4 /* first parameter in %eax, %edx, %ecx */
|
---|
| 421 | #define FUNC_FASTCALLW 5 /* first parameter in %ecx, %edx */
|
---|
| 422 |
|
---|
| 423 | /* field 'Sym.t' for macros */
|
---|
| 424 | #define MACRO_OBJ 0 /* object like macro */
|
---|
| 425 | #define MACRO_FUNC 1 /* function like macro */
|
---|
| 426 |
|
---|
| 427 | /* field 'Sym.r' for C labels */
|
---|
| 428 | #define LABEL_DEFINED 0 /* label is defined */
|
---|
| 429 | #define LABEL_FORWARD 1 /* label is forward defined */
|
---|
| 430 | #define LABEL_DECLARED 2 /* label is declared but never used */
|
---|
| 431 |
|
---|
| 432 | /* type_decl() types */
|
---|
| 433 | #define TYPE_ABSTRACT 1 /* type without variable */
|
---|
| 434 | #define TYPE_DIRECT 2 /* type with variable */
|
---|
| 435 |
|
---|
| 436 | #define IO_BUF_SIZE 8192
|
---|
| 437 |
|
---|
| 438 | typedef struct BufferedFile {
|
---|
| 439 | uint8_t *buf_ptr;
|
---|
| 440 | uint8_t *buf_end;
|
---|
| 441 | int fd;
|
---|
| 442 | struct BufferedFile *prev;
|
---|
| 443 | int line_num; /* current line number - here to simplify code */
|
---|
| 444 | int ifndef_macro; /* #ifndef macro / #endif search */
|
---|
| 445 | int ifndef_macro_saved; /* saved ifndef_macro */
|
---|
| 446 | int *ifdef_stack_ptr; /* ifdef_stack value at the start of the file */
|
---|
| 447 | char filename[1024]; /* filename */
|
---|
| 448 | unsigned char buffer[IO_BUF_SIZE + 1]; /* extra size for CH_EOB char */
|
---|
| 449 | } BufferedFile;
|
---|
| 450 |
|
---|
| 451 | #define CH_EOB '\\' /* end of buffer or '\0' char in file */
|
---|
| 452 | #define CH_EOF (-1) /* end of file */
|
---|
| 453 |
|
---|
| 454 | /* parsing state (used to save parser state to reparse part of the
|
---|
| 455 | source several times) */
|
---|
| 456 | typedef struct ParseState {
|
---|
| 457 | const int *macro_ptr;
|
---|
| 458 | int line_num;
|
---|
| 459 | int tok;
|
---|
| 460 | CValue tokc;
|
---|
| 461 | } ParseState;
|
---|
| 462 |
|
---|
| 463 | /* used to record tokens */
|
---|
| 464 | typedef struct TokenString {
|
---|
| 465 | int *str;
|
---|
| 466 | int len;
|
---|
| 467 | int allocated_len;
|
---|
| 468 | int last_line_num;
|
---|
| 469 | } TokenString;
|
---|
| 470 |
|
---|
| 471 | /* inline functions */
|
---|
| 472 | typedef struct InlineFunc {
|
---|
| 473 | int *token_str;
|
---|
| 474 | Sym *sym;
|
---|
| 475 | char filename[1];
|
---|
| 476 | } InlineFunc;
|
---|
| 477 |
|
---|
| 478 | /* include file cache, used to find files faster and also to eliminate
|
---|
| 479 | inclusion if the include file is protected by #ifndef ... #endif */
|
---|
| 480 | typedef struct CachedInclude {
|
---|
| 481 | int ifndef_macro;
|
---|
| 482 | int hash_next; /* -1 if none */
|
---|
| 483 | char filename[1]; /* path specified in #include */
|
---|
| 484 | } CachedInclude;
|
---|
| 485 |
|
---|
| 486 | #define CACHED_INCLUDES_HASH_SIZE 512
|
---|
| 487 |
|
---|
| 488 | #ifdef CONFIG_TCC_ASM
|
---|
| 489 | typedef struct ExprValue {
|
---|
| 490 | uint32_t v;
|
---|
| 491 | Sym *sym;
|
---|
| 492 | } ExprValue;
|
---|
| 493 |
|
---|
| 494 | #define MAX_ASM_OPERANDS 30
|
---|
| 495 | typedef struct ASMOperand {
|
---|
| 496 | int id; /* GCC 3 optionnal identifier (0 if number only supported */
|
---|
| 497 | char *constraint;
|
---|
| 498 | char asm_str[16]; /* computed asm string for operand */
|
---|
| 499 | SValue *vt; /* C value of the expression */
|
---|
| 500 | int ref_index; /* if >= 0, gives reference to a output constraint */
|
---|
| 501 | int input_index; /* if >= 0, gives reference to an input constraint */
|
---|
| 502 | int priority; /* priority, used to assign registers */
|
---|
| 503 | int reg; /* if >= 0, register number used for this operand */
|
---|
| 504 | int is_llong; /* true if double register value */
|
---|
| 505 | int is_memory; /* true if memory operand */
|
---|
| 506 | int is_rw; /* for '+' modifier */
|
---|
| 507 | } ASMOperand;
|
---|
| 508 | #endif
|
---|
| 509 |
|
---|
| 510 | struct sym_attr {
|
---|
| 511 | unsigned long got_offset;
|
---|
| 512 | #ifdef TCC_TARGET_ARM
|
---|
| 513 | unsigned char plt_thumb_stub:1;
|
---|
| 514 | #endif
|
---|
| 515 | };
|
---|
| 516 |
|
---|
| 517 | struct TCCState {
|
---|
| 518 |
|
---|
| 519 | int verbose; /* if true, display some information during compilation */
|
---|
| 520 | int nostdinc; /* if true, no standard headers are added */
|
---|
| 521 | int nostdlib; /* if true, no standard libraries are added */
|
---|
| 522 | int nocommon; /* if true, do not use common symbols for .bss data */
|
---|
| 523 | int static_link; /* if true, static linking is performed */
|
---|
| 524 | int rdynamic; /* if true, all symbols are exported */
|
---|
| 525 | int symbolic; /* if true, resolve symbols in the current module first */
|
---|
| 526 | int alacarte_link; /* if true, only link in referenced objects from archive */
|
---|
| 527 |
|
---|
| 528 | char *tcc_lib_path; /* CONFIG_TCCDIR or -B option */
|
---|
| 529 | char *soname; /* as specified on the command line (-soname) */
|
---|
| 530 | char *rpath; /* as specified on the command line (-Wl,-rpath=) */
|
---|
| 531 |
|
---|
| 532 | /* output type, see TCC_OUTPUT_XXX */
|
---|
| 533 | int output_type;
|
---|
| 534 | /* output format, see TCC_OUTPUT_FORMAT_xxx */
|
---|
| 535 | int output_format;
|
---|
| 536 |
|
---|
| 537 | /* C language options */
|
---|
| 538 | int char_is_unsigned;
|
---|
| 539 | int leading_underscore;
|
---|
| 540 |
|
---|
| 541 | /* warning switches */
|
---|
| 542 | int warn_write_strings;
|
---|
| 543 | int warn_unsupported;
|
---|
| 544 | int warn_error;
|
---|
| 545 | int warn_none;
|
---|
| 546 | int warn_implicit_function_declaration;
|
---|
| 547 |
|
---|
| 548 | /* compile with debug symbol (and use them if error during execution) */
|
---|
| 549 | int do_debug;
|
---|
| 550 | #ifdef CONFIG_TCC_BCHECK
|
---|
| 551 | /* compile with built-in memory and bounds checker */
|
---|
| 552 | int do_bounds_check;
|
---|
| 553 | #endif
|
---|
| 554 |
|
---|
| 555 | addr_t text_addr; /* address of text section */
|
---|
| 556 | int has_text_addr;
|
---|
| 557 |
|
---|
| 558 | unsigned long section_align; /* section alignment */
|
---|
| 559 |
|
---|
| 560 | char *init_symbol; /* symbols to call at load-time (not used currently) */
|
---|
| 561 | char *fini_symbol; /* symbols to call at unload-time (not used currently) */
|
---|
| 562 |
|
---|
| 563 | #ifdef TCC_TARGET_I386
|
---|
| 564 | int seg_size; /* 32. Can be 16 with i386 assembler (.code16) */
|
---|
| 565 | #endif
|
---|
| 566 |
|
---|
| 567 | /* array of all loaded dlls (including those referenced by loaded dlls) */
|
---|
| 568 | DLLReference **loaded_dlls;
|
---|
| 569 | int nb_loaded_dlls;
|
---|
| 570 |
|
---|
| 571 | /* include paths */
|
---|
| 572 | char **include_paths;
|
---|
| 573 | int nb_include_paths;
|
---|
| 574 |
|
---|
| 575 | char **sysinclude_paths;
|
---|
| 576 | int nb_sysinclude_paths;
|
---|
| 577 |
|
---|
| 578 | /* library paths */
|
---|
| 579 | char **library_paths;
|
---|
| 580 | int nb_library_paths;
|
---|
| 581 |
|
---|
| 582 | /* crt?.o object path */
|
---|
| 583 | char **crt_paths;
|
---|
| 584 | int nb_crt_paths;
|
---|
| 585 |
|
---|
| 586 | /* error handling */
|
---|
| 587 | void *error_opaque;
|
---|
| 588 | void (*error_func)(void *opaque, const char *msg);
|
---|
| 589 | int error_set_jmp_enabled;
|
---|
| 590 | jmp_buf error_jmp_buf;
|
---|
| 591 | int nb_errors;
|
---|
| 592 |
|
---|
| 593 | /* output file for preprocessing (-E) */
|
---|
| 594 | FILE *ppfp;
|
---|
| 595 |
|
---|
| 596 | /* for -MD/-MF: collected dependencies for this compilation */
|
---|
| 597 | char **target_deps;
|
---|
| 598 | int nb_target_deps;
|
---|
| 599 |
|
---|
| 600 | /* compilation */
|
---|
| 601 | BufferedFile *include_stack[INCLUDE_STACK_SIZE];
|
---|
| 602 | BufferedFile **include_stack_ptr;
|
---|
| 603 |
|
---|
| 604 | int ifdef_stack[IFDEF_STACK_SIZE];
|
---|
| 605 | int *ifdef_stack_ptr;
|
---|
| 606 |
|
---|
| 607 | /* included files enclosed with #ifndef MACRO */
|
---|
| 608 | int cached_includes_hash[CACHED_INCLUDES_HASH_SIZE];
|
---|
| 609 | CachedInclude **cached_includes;
|
---|
| 610 | int nb_cached_includes;
|
---|
| 611 |
|
---|
| 612 | /* #pragma pack stack */
|
---|
| 613 | int pack_stack[PACK_STACK_SIZE];
|
---|
| 614 | int *pack_stack_ptr;
|
---|
| 615 |
|
---|
| 616 | /* inline functions are stored as token lists and compiled last
|
---|
| 617 | only if referenced */
|
---|
| 618 | struct InlineFunc **inline_fns;
|
---|
| 619 | int nb_inline_fns;
|
---|
| 620 |
|
---|
| 621 | /* sections */
|
---|
| 622 | Section **sections;
|
---|
| 623 | int nb_sections; /* number of sections, including first dummy section */
|
---|
| 624 |
|
---|
| 625 | Section **priv_sections;
|
---|
| 626 | int nb_priv_sections; /* number of private sections */
|
---|
| 627 |
|
---|
| 628 | /* got & plt handling */
|
---|
| 629 | Section *got;
|
---|
| 630 | Section *plt;
|
---|
| 631 | struct sym_attr *sym_attrs;
|
---|
| 632 | int nb_sym_attrs;
|
---|
| 633 | /* give the correspondance from symtab indexes to dynsym indexes */
|
---|
| 634 | int *symtab_to_dynsym;
|
---|
| 635 |
|
---|
| 636 | /* temporary dynamic symbol sections (for dll loading) */
|
---|
| 637 | Section *dynsymtab_section;
|
---|
| 638 | /* exported dynamic symbol section */
|
---|
| 639 | Section *dynsym;
|
---|
| 640 | /* copy of the gobal symtab_section variable */
|
---|
| 641 | Section *symtab;
|
---|
| 642 | /* tiny assembler state */
|
---|
| 643 | Sym *asm_labels;
|
---|
| 644 |
|
---|
| 645 | #ifdef TCC_TARGET_PE
|
---|
| 646 | /* PE info */
|
---|
| 647 | int pe_subsystem;
|
---|
| 648 | unsigned pe_file_align;
|
---|
| 649 | unsigned pe_stack_size;
|
---|
| 650 | # ifdef TCC_TARGET_X86_64
|
---|
| 651 | Section *uw_pdata;
|
---|
| 652 | int uw_sym;
|
---|
| 653 | unsigned uw_offs;
|
---|
| 654 | # endif
|
---|
| 655 | #endif
|
---|
| 656 |
|
---|
| 657 | #ifdef TCC_IS_NATIVE
|
---|
| 658 | /* for tcc_relocate */
|
---|
| 659 | void *runtime_mem;
|
---|
| 660 | # ifdef HAVE_SELINUX
|
---|
| 661 | void *write_mem;
|
---|
| 662 | unsigned long mem_size;
|
---|
| 663 | # endif
|
---|
| 664 | # if !defined TCC_TARGET_PE && (defined TCC_TARGET_X86_64 || defined TCC_TARGET_ARM)
|
---|
| 665 | /* write PLT and GOT here */
|
---|
| 666 | char *runtime_plt_and_got;
|
---|
| 667 | unsigned runtime_plt_and_got_offset;
|
---|
| 668 | # define TCC_HAS_RUNTIME_PLTGOT
|
---|
| 669 | # endif
|
---|
| 670 | #endif
|
---|
| 671 |
|
---|
| 672 | /* used by main and tcc_parse_args only */
|
---|
| 673 | char **files; /* files seen on command line */
|
---|
| 674 | int nb_files; /* number thereof */
|
---|
| 675 | int nb_libraries; /* number of libs thereof */
|
---|
| 676 | char *outfile; /* output filename */
|
---|
| 677 | char *option_m; /* only -m32/-m64 handled */
|
---|
| 678 | int print_search_dirs; /* option */
|
---|
| 679 | int option_r; /* option -r */
|
---|
| 680 | int do_bench; /* option -bench */
|
---|
| 681 | int gen_deps; /* option -MD */
|
---|
| 682 | char *deps_outfile; /* option -MF */
|
---|
| 683 | };
|
---|
| 684 |
|
---|
| 685 | /* The current value can be: */
|
---|
| 686 | #define VT_VALMASK 0x003f /* mask for value location, register or: */
|
---|
| 687 | #define VT_CONST 0x0030 /* constant in vc (must be first non register value) */
|
---|
| 688 | #define VT_LLOCAL 0x0031 /* lvalue, offset on stack */
|
---|
| 689 | #define VT_LOCAL 0x0032 /* offset on stack */
|
---|
| 690 | #define VT_CMP 0x0033 /* the value is stored in processor flags (in vc) */
|
---|
| 691 | #define VT_JMP 0x0034 /* value is the consequence of jmp true (even) */
|
---|
| 692 | #define VT_JMPI 0x0035 /* value is the consequence of jmp false (odd) */
|
---|
| 693 | #define VT_REF 0x0040 /* value is pointer to structure rather than address */
|
---|
| 694 | #define VT_LVAL 0x0100 /* var is an lvalue */
|
---|
| 695 | #define VT_SYM 0x0200 /* a symbol value is added */
|
---|
| 696 | #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
|
---|
| 697 | char/short stored in integer registers) */
|
---|
| 698 | #define VT_MUSTBOUND 0x0800 /* bound checking must be done before
|
---|
| 699 | dereferencing value */
|
---|
| 700 | #define VT_BOUNDED 0x8000 /* value is bounded. The address of the
|
---|
| 701 | bounding function call point is in vc */
|
---|
| 702 | #define VT_LVAL_BYTE 0x1000 /* lvalue is a byte */
|
---|
| 703 | #define VT_LVAL_SHORT 0x2000 /* lvalue is a short */
|
---|
| 704 | #define VT_LVAL_UNSIGNED 0x4000 /* lvalue is unsigned */
|
---|
| 705 | #define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
|
---|
| 706 |
|
---|
| 707 | /* types */
|
---|
| 708 | #define VT_BTYPE 0x000f /* mask for basic type */
|
---|
| 709 | #define VT_INT 0 /* integer type */
|
---|
| 710 | #define VT_BYTE 1 /* signed byte type */
|
---|
| 711 | #define VT_SHORT 2 /* short type */
|
---|
| 712 | #define VT_VOID 3 /* void type */
|
---|
| 713 | #define VT_PTR 4 /* pointer */
|
---|
| 714 | #define VT_ENUM 5 /* enum definition */
|
---|
| 715 | #define VT_FUNC 6 /* function type */
|
---|
| 716 | #define VT_STRUCT 7 /* struct/union definition */
|
---|
| 717 | #define VT_FLOAT 8 /* IEEE float */
|
---|
| 718 | #define VT_DOUBLE 9 /* IEEE double */
|
---|
| 719 | #define VT_LDOUBLE 10 /* IEEE long double */
|
---|
| 720 | #define VT_BOOL 11 /* ISOC99 boolean type */
|
---|
| 721 | #define VT_LLONG 12 /* 64 bit integer */
|
---|
| 722 | #define VT_LONG 13 /* long integer (NEVER USED as type, only
|
---|
| 723 | during parsing) */
|
---|
| 724 | #define VT_UNSIGNED 0x0010 /* unsigned type */
|
---|
| 725 | #define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */
|
---|
| 726 | #define VT_BITFIELD 0x0040 /* bitfield modifier */
|
---|
| 727 | #define VT_CONSTANT 0x0800 /* const modifier */
|
---|
| 728 | #define VT_VOLATILE 0x1000 /* volatile modifier */
|
---|
| 729 | #define VT_SIGNED 0x2000 /* signed type */
|
---|
| 730 | #define VT_VLA 0x00020000 /* VLA type (also has VT_PTR and VT_ARRAY) */
|
---|
| 731 |
|
---|
| 732 | /* storage */
|
---|
| 733 | #define VT_EXTERN 0x00000080 /* extern definition */
|
---|
| 734 | #define VT_STATIC 0x00000100 /* static variable */
|
---|
| 735 | #define VT_TYPEDEF 0x00000200 /* typedef definition */
|
---|
| 736 | #define VT_INLINE 0x00000400 /* inline definition */
|
---|
| 737 | #define VT_IMPORT 0x00004000 /* win32: extern data imported from dll */
|
---|
| 738 | #define VT_EXPORT 0x00008000 /* win32: data exported from dll */
|
---|
| 739 | #define VT_WEAK 0x00010000 /* weak symbol */
|
---|
| 740 |
|
---|
| 741 | #define VT_STRUCT_SHIFT 18 /* shift for bitfield shift values (max: 32 - 2*6) */
|
---|
| 742 |
|
---|
| 743 | /* type mask (except storage) */
|
---|
| 744 | #define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE | VT_IMPORT | VT_EXPORT | VT_WEAK)
|
---|
| 745 | #define VT_TYPE (~(VT_STORAGE))
|
---|
| 746 |
|
---|
| 747 | /* token values */
|
---|
| 748 |
|
---|
| 749 | /* warning: the following compare tokens depend on i386 asm code */
|
---|
| 750 | #define TOK_ULT 0x92
|
---|
| 751 | #define TOK_UGE 0x93
|
---|
| 752 | #define TOK_EQ 0x94
|
---|
| 753 | #define TOK_NE 0x95
|
---|
| 754 | #define TOK_ULE 0x96
|
---|
| 755 | #define TOK_UGT 0x97
|
---|
| 756 | #define TOK_Nset 0x98
|
---|
| 757 | #define TOK_Nclear 0x99
|
---|
| 758 | #define TOK_LT 0x9c
|
---|
| 759 | #define TOK_GE 0x9d
|
---|
| 760 | #define TOK_LE 0x9e
|
---|
| 761 | #define TOK_GT 0x9f
|
---|
| 762 |
|
---|
| 763 | #define TOK_LAND 0xa0
|
---|
| 764 | #define TOK_LOR 0xa1
|
---|
| 765 |
|
---|
| 766 | #define TOK_DEC 0xa2
|
---|
| 767 | #define TOK_MID 0xa3 /* inc/dec, to void constant */
|
---|
| 768 | #define TOK_INC 0xa4
|
---|
| 769 | #define TOK_UDIV 0xb0 /* unsigned division */
|
---|
| 770 | #define TOK_UMOD 0xb1 /* unsigned modulo */
|
---|
| 771 | #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
|
---|
| 772 | #define TOK_CINT 0xb3 /* number in tokc */
|
---|
| 773 | #define TOK_CCHAR 0xb4 /* char constant in tokc */
|
---|
| 774 | #define TOK_STR 0xb5 /* pointer to string in tokc */
|
---|
| 775 | #define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */
|
---|
| 776 | #define TOK_LCHAR 0xb7
|
---|
| 777 | #define TOK_LSTR 0xb8
|
---|
| 778 | #define TOK_CFLOAT 0xb9 /* float constant */
|
---|
| 779 | #define TOK_LINENUM 0xba /* line number info */
|
---|
| 780 | #define TOK_CDOUBLE 0xc0 /* double constant */
|
---|
| 781 | #define TOK_CLDOUBLE 0xc1 /* long double constant */
|
---|
| 782 | #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
|
---|
| 783 | #define TOK_ADDC1 0xc3 /* add with carry generation */
|
---|
| 784 | #define TOK_ADDC2 0xc4 /* add with carry use */
|
---|
| 785 | #define TOK_SUBC1 0xc5 /* add with carry generation */
|
---|
| 786 | #define TOK_SUBC2 0xc6 /* add with carry use */
|
---|
| 787 | #define TOK_CUINT 0xc8 /* unsigned int constant */
|
---|
| 788 | #define TOK_CLLONG 0xc9 /* long long constant */
|
---|
| 789 | #define TOK_CULLONG 0xca /* unsigned long long constant */
|
---|
| 790 | #define TOK_ARROW 0xcb
|
---|
| 791 | #define TOK_DOTS 0xcc /* three dots */
|
---|
| 792 | #define TOK_SHR 0xcd /* unsigned shift right */
|
---|
| 793 | #define TOK_PPNUM 0xce /* preprocessor number */
|
---|
| 794 | #define TOK_NOSUBST 0xcf /* means following token has already been pp'd */
|
---|
| 795 |
|
---|
| 796 | #define TOK_SHL 0x01 /* shift left */
|
---|
| 797 | #define TOK_SAR 0x02 /* signed shift right */
|
---|
| 798 |
|
---|
| 799 | /* assignement operators : normal operator or 0x80 */
|
---|
| 800 | #define TOK_A_MOD 0xa5
|
---|
| 801 | #define TOK_A_AND 0xa6
|
---|
| 802 | #define TOK_A_MUL 0xaa
|
---|
| 803 | #define TOK_A_ADD 0xab
|
---|
| 804 | #define TOK_A_SUB 0xad
|
---|
| 805 | #define TOK_A_DIV 0xaf
|
---|
| 806 | #define TOK_A_XOR 0xde
|
---|
| 807 | #define TOK_A_OR 0xfc
|
---|
| 808 | #define TOK_A_SHL 0x81
|
---|
| 809 | #define TOK_A_SAR 0x82
|
---|
| 810 |
|
---|
| 811 | #ifndef offsetof
|
---|
| 812 | #define offsetof(type, field) ((size_t) &((type *)0)->field)
|
---|
| 813 | #endif
|
---|
| 814 |
|
---|
| 815 | #ifndef countof
|
---|
| 816 | #define countof(tab) (sizeof(tab) / sizeof((tab)[0]))
|
---|
| 817 | #endif
|
---|
| 818 |
|
---|
| 819 | #define TOK_EOF (-1) /* end of file */
|
---|
| 820 | #define TOK_LINEFEED 10 /* line feed */
|
---|
| 821 |
|
---|
| 822 | /* all identificators and strings have token above that */
|
---|
| 823 | #define TOK_IDENT 256
|
---|
| 824 |
|
---|
| 825 | #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
|
---|
| 826 | #define TOK_ASM_int TOK_INT
|
---|
| 827 | #define TOK_ASM_weak TOK_WEAK1
|
---|
| 828 |
|
---|
| 829 | #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
|
---|
| 830 | /* only used for i386 asm opcodes definitions */
|
---|
| 831 | #define DEF_BWL(x) \
|
---|
| 832 | DEF(TOK_ASM_ ## x ## b, #x "b") \
|
---|
| 833 | DEF(TOK_ASM_ ## x ## w, #x "w") \
|
---|
| 834 | DEF(TOK_ASM_ ## x ## l, #x "l") \
|
---|
| 835 | DEF(TOK_ASM_ ## x, #x)
|
---|
| 836 | #define DEF_WL(x) \
|
---|
| 837 | DEF(TOK_ASM_ ## x ## w, #x "w") \
|
---|
| 838 | DEF(TOK_ASM_ ## x ## l, #x "l") \
|
---|
| 839 | DEF(TOK_ASM_ ## x, #x)
|
---|
| 840 | #ifdef TCC_TARGET_X86_64
|
---|
| 841 | # define DEF_BWLQ(x) \
|
---|
| 842 | DEF(TOK_ASM_ ## x ## b, #x "b") \
|
---|
| 843 | DEF(TOK_ASM_ ## x ## w, #x "w") \
|
---|
| 844 | DEF(TOK_ASM_ ## x ## l, #x "l") \
|
---|
| 845 | DEF(TOK_ASM_ ## x ## q, #x "q") \
|
---|
| 846 | DEF(TOK_ASM_ ## x, #x)
|
---|
| 847 | # define DEF_WLQ(x) \
|
---|
| 848 | DEF(TOK_ASM_ ## x ## w, #x "w") \
|
---|
| 849 | DEF(TOK_ASM_ ## x ## l, #x "l") \
|
---|
| 850 | DEF(TOK_ASM_ ## x ## q, #x "q") \
|
---|
| 851 | DEF(TOK_ASM_ ## x, #x)
|
---|
| 852 | # define DEF_BWLX DEF_BWLQ
|
---|
| 853 | # define DEF_WLX DEF_WLQ
|
---|
| 854 | /* number of sizes + 1 */
|
---|
| 855 | # define NBWLX 5
|
---|
| 856 | #else
|
---|
| 857 | # define DEF_BWLX DEF_BWL
|
---|
| 858 | # define DEF_WLX DEF_WL
|
---|
| 859 | /* number of sizes + 1 */
|
---|
| 860 | # define NBWLX 4
|
---|
| 861 | #endif
|
---|
| 862 |
|
---|
| 863 | #define DEF_FP1(x) \
|
---|
| 864 | DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
|
---|
| 865 | DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
|
---|
| 866 | DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
|
---|
| 867 | DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
|
---|
| 868 |
|
---|
| 869 | #define DEF_FP(x) \
|
---|
| 870 | DEF(TOK_ASM_ ## f ## x, "f" #x ) \
|
---|
| 871 | DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
|
---|
| 872 | DEF_FP1(x)
|
---|
| 873 |
|
---|
| 874 | #define DEF_ASMTEST(x) \
|
---|
| 875 | DEF_ASM(x ## o) \
|
---|
| 876 | DEF_ASM(x ## no) \
|
---|
| 877 | DEF_ASM(x ## b) \
|
---|
| 878 | DEF_ASM(x ## c) \
|
---|
| 879 | DEF_ASM(x ## nae) \
|
---|
| 880 | DEF_ASM(x ## nb) \
|
---|
| 881 | DEF_ASM(x ## nc) \
|
---|
| 882 | DEF_ASM(x ## ae) \
|
---|
| 883 | DEF_ASM(x ## e) \
|
---|
| 884 | DEF_ASM(x ## z) \
|
---|
| 885 | DEF_ASM(x ## ne) \
|
---|
| 886 | DEF_ASM(x ## nz) \
|
---|
| 887 | DEF_ASM(x ## be) \
|
---|
| 888 | DEF_ASM(x ## na) \
|
---|
| 889 | DEF_ASM(x ## nbe) \
|
---|
| 890 | DEF_ASM(x ## a) \
|
---|
| 891 | DEF_ASM(x ## s) \
|
---|
| 892 | DEF_ASM(x ## ns) \
|
---|
| 893 | DEF_ASM(x ## p) \
|
---|
| 894 | DEF_ASM(x ## pe) \
|
---|
| 895 | DEF_ASM(x ## np) \
|
---|
| 896 | DEF_ASM(x ## po) \
|
---|
| 897 | DEF_ASM(x ## l) \
|
---|
| 898 | DEF_ASM(x ## nge) \
|
---|
| 899 | DEF_ASM(x ## nl) \
|
---|
| 900 | DEF_ASM(x ## ge) \
|
---|
| 901 | DEF_ASM(x ## le) \
|
---|
| 902 | DEF_ASM(x ## ng) \
|
---|
| 903 | DEF_ASM(x ## nle) \
|
---|
| 904 | DEF_ASM(x ## g)
|
---|
| 905 |
|
---|
| 906 | #endif // defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
|
---|
| 907 |
|
---|
| 908 | enum tcc_token {
|
---|
| 909 | TOK_LAST = TOK_IDENT - 1,
|
---|
| 910 | #define DEF(id, str) id,
|
---|
| 911 | #include "tcctok.h"
|
---|
| 912 | #undef DEF
|
---|
| 913 | };
|
---|
| 914 |
|
---|
| 915 | #define TOK_UIDENT TOK_DEFINE
|
---|
| 916 |
|
---|
| 917 | #if 0//def _WIN32
|
---|
| 918 | #define snprintf _snprintf
|
---|
| 919 | #define vsnprintf _vsnprintf
|
---|
| 920 | #ifndef __GNUC__
|
---|
| 921 | #define strtold (long double)strtod
|
---|
| 922 | #define strtof (float)strtod
|
---|
| 923 | #define strtoll _strtoi64
|
---|
| 924 | #define strtoull _strtoui64
|
---|
| 925 | #endif
|
---|
| 926 | #else
|
---|
| 927 | /* XXX: need to define this to use them in non ISOC99 context */
|
---|
| 928 | extern float strtof (const char *__nptr, char **__endptr);
|
---|
| 929 | extern long double strtold (const char *__nptr, char **__endptr);
|
---|
| 930 | #endif
|
---|
| 931 |
|
---|
| 932 | #ifdef _WIN32
|
---|
| 933 | #define IS_DIRSEP(c) (c == '/' || c == '\\')
|
---|
| 934 | #define IS_ABSPATH(p) (IS_DIRSEP(p[0]) || (p[0] && p[1] == ':' && IS_DIRSEP(p[2])))
|
---|
| 935 | #define PATHCMP stricmp
|
---|
| 936 | #else
|
---|
| 937 | #define IS_DIRSEP(c) (c == '/')
|
---|
| 938 | #define IS_ABSPATH(p) IS_DIRSEP(p[0])
|
---|
| 939 | #define PATHCMP strcmp
|
---|
| 940 | #endif
|
---|
| 941 |
|
---|
| 942 | #ifdef TCC_TARGET_PE
|
---|
| 943 | #define PATHSEP ';'
|
---|
| 944 | #else
|
---|
| 945 | #define PATHSEP ':'
|
---|
| 946 | #endif
|
---|
| 947 |
|
---|
| 948 | /* space exlcuding newline */
|
---|
| 949 | static inline int is_space(int ch)
|
---|
| 950 | {
|
---|
| 951 | return ch == ' ' || ch == '\t' || ch == '\v' || ch == '\f' || ch == '\r';
|
---|
| 952 | }
|
---|
| 953 |
|
---|
| 954 | static inline int isid(int c)
|
---|
| 955 | {
|
---|
| 956 | return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_';
|
---|
| 957 | }
|
---|
| 958 |
|
---|
| 959 | static inline int isnum(int c)
|
---|
| 960 | {
|
---|
| 961 | return c >= '0' && c <= '9';
|
---|
| 962 | }
|
---|
| 963 |
|
---|
| 964 | static inline int isoct(int c)
|
---|
| 965 | {
|
---|
| 966 | return c >= '0' && c <= '7';
|
---|
| 967 | }
|
---|
| 968 |
|
---|
| 969 | static inline int toup(int c)
|
---|
| 970 | {
|
---|
| 971 | return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c;
|
---|
| 972 | }
|
---|
| 973 |
|
---|
| 974 | #ifndef PUB_FUNC
|
---|
| 975 | # define PUB_FUNC
|
---|
| 976 | #endif
|
---|
| 977 |
|
---|
| 978 | #ifdef ONE_SOURCE
|
---|
| 979 | #define ST_INLN static inline
|
---|
| 980 | #define ST_FUNC static
|
---|
| 981 | #define ST_DATA static
|
---|
| 982 | #else
|
---|
| 983 | #define ST_INLN
|
---|
| 984 | #define ST_FUNC
|
---|
| 985 | #define ST_DATA extern
|
---|
| 986 | #endif
|
---|
| 987 |
|
---|
| 988 | /* ------------ libtcc.c ------------ */
|
---|
| 989 |
|
---|
| 990 | /* use GNU C extensions */
|
---|
| 991 | ST_DATA int gnu_ext;
|
---|
| 992 | /* use Tiny C extensions */
|
---|
| 993 | ST_DATA int tcc_ext;
|
---|
| 994 | /* XXX: get rid of this ASAP */
|
---|
| 995 | ST_DATA struct TCCState *tcc_state;
|
---|
| 996 |
|
---|
| 997 | #ifdef MEM_DEBUG
|
---|
| 998 | ST_DATA int mem_cur_size;
|
---|
| 999 | ST_DATA int mem_max_size;
|
---|
| 1000 | #endif
|
---|
| 1001 |
|
---|
| 1002 | #define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
|
---|
| 1003 | #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
|
---|
| 1004 | #define AFF_PREPROCESS 0x0004 /* preprocess file */
|
---|
| 1005 |
|
---|
| 1006 | /* public functions currently used by the tcc main function */
|
---|
| 1007 | PUB_FUNC char *pstrcpy(char *buf, int buf_size, const char *s);
|
---|
| 1008 | PUB_FUNC char *pstrcat(char *buf, int buf_size, const char *s);
|
---|
| 1009 | PUB_FUNC char *pstrncpy(char *out, const char *in, size_t num);
|
---|
| 1010 | PUB_FUNC char *tcc_basename(const char *name);
|
---|
| 1011 | PUB_FUNC char *tcc_fileextension (const char *name);
|
---|
| 1012 | PUB_FUNC void tcc_free(void *ptr);
|
---|
| 1013 | PUB_FUNC void *tcc_malloc(unsigned long size);
|
---|
| 1014 | PUB_FUNC void *tcc_mallocz(unsigned long size);
|
---|
| 1015 | PUB_FUNC void *tcc_realloc(void *ptr, unsigned long size);
|
---|
| 1016 | PUB_FUNC char *tcc_strdup(const char *str);
|
---|
| 1017 | #define free(p) use_tcc_free(p)
|
---|
| 1018 | #define malloc(s) use_tcc_malloc(s)
|
---|
| 1019 | #define realloc(p, s) use_tcc_realloc(p, s)
|
---|
| 1020 | #undef strdup
|
---|
| 1021 | #define strdup(s) use_tcc_strdup(s)
|
---|
| 1022 | PUB_FUNC void tcc_memstats(void);
|
---|
| 1023 | PUB_FUNC void tcc_error_noabort(const char *fmt, ...);
|
---|
| 1024 | PUB_FUNC void tcc_error(const char *fmt, ...);
|
---|
| 1025 | PUB_FUNC void tcc_warning(const char *fmt, ...);
|
---|
| 1026 |
|
---|
| 1027 | /* other utilities */
|
---|
| 1028 | ST_FUNC void dynarray_add(void ***ptab, int *nb_ptr, void *data);
|
---|
| 1029 | ST_FUNC void dynarray_reset(void *pp, int *n);
|
---|
| 1030 | ST_FUNC void cstr_ccat(CString *cstr, int ch);
|
---|
| 1031 | ST_FUNC void cstr_cat(CString *cstr, const char *str);
|
---|
| 1032 | ST_FUNC void cstr_wccat(CString *cstr, int ch);
|
---|
| 1033 | ST_FUNC void cstr_new(CString *cstr);
|
---|
| 1034 | ST_FUNC void cstr_free(CString *cstr);
|
---|
| 1035 | ST_FUNC void cstr_reset(CString *cstr);
|
---|
| 1036 |
|
---|
| 1037 | ST_FUNC Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags);
|
---|
| 1038 | ST_FUNC void section_realloc(Section *sec, unsigned long new_size);
|
---|
| 1039 | ST_FUNC void *section_ptr_add(Section *sec, unsigned long size);
|
---|
| 1040 | ST_FUNC void section_reserve(Section *sec, unsigned long size);
|
---|
| 1041 | ST_FUNC Section *find_section(TCCState *s1, const char *name);
|
---|
| 1042 |
|
---|
| 1043 | ST_FUNC void put_extern_sym2(Sym *sym, Section *section, addr_t value, unsigned long size, int can_add_underscore);
|
---|
| 1044 | ST_FUNC void put_extern_sym(Sym *sym, Section *section, addr_t value, unsigned long size);
|
---|
| 1045 | ST_FUNC void greloc(Section *s, Sym *sym, unsigned long offset, int type);
|
---|
| 1046 |
|
---|
| 1047 | ST_INLN void sym_free(Sym *sym);
|
---|
| 1048 | ST_FUNC Sym *sym_push2(Sym **ps, int v, int t, long c);
|
---|
| 1049 | ST_FUNC Sym *sym_find2(Sym *s, int v);
|
---|
| 1050 | ST_FUNC Sym *sym_push(int v, CType *type, int r, int c);
|
---|
| 1051 | ST_FUNC void sym_pop(Sym **ptop, Sym *b);
|
---|
| 1052 | ST_INLN Sym *struct_find(int v);
|
---|
| 1053 | ST_INLN Sym *sym_find(int v);
|
---|
| 1054 | ST_FUNC Sym *global_identifier_push(int v, int t, int c);
|
---|
| 1055 |
|
---|
| 1056 | ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen);
|
---|
| 1057 | ST_FUNC int tcc_open(TCCState *s1, const char *filename);
|
---|
| 1058 | ST_FUNC void tcc_close(void);
|
---|
| 1059 |
|
---|
| 1060 | ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags);
|
---|
| 1061 | ST_FUNC int tcc_add_crt(TCCState *s, const char *filename);
|
---|
| 1062 | ST_FUNC int tcc_add_dll(TCCState *s, const char *filename, int flags);
|
---|
| 1063 |
|
---|
| 1064 | PUB_FUNC void tcc_print_stats(TCCState *s, int64_t total_time);
|
---|
| 1065 | PUB_FUNC int tcc_parse_args(TCCState *s, int argc, char **argv);
|
---|
| 1066 |
|
---|
| 1067 | /* ------------ tccpp.c ------------ */
|
---|
| 1068 |
|
---|
| 1069 | ST_DATA struct BufferedFile *file;
|
---|
| 1070 | ST_DATA int ch, tok;
|
---|
| 1071 | ST_DATA CValue tokc;
|
---|
| 1072 | ST_DATA const int *macro_ptr;
|
---|
| 1073 | ST_DATA int parse_flags;
|
---|
| 1074 | ST_DATA int tok_flags;
|
---|
| 1075 | ST_DATA CString tokcstr; /* current parsed string, if any */
|
---|
| 1076 |
|
---|
| 1077 | /* display benchmark infos */
|
---|
| 1078 | ST_DATA int total_lines;
|
---|
| 1079 | ST_DATA int total_bytes;
|
---|
| 1080 | ST_DATA int tok_ident;
|
---|
| 1081 | ST_DATA TokenSym **table_ident;
|
---|
| 1082 |
|
---|
| 1083 | #define TOK_FLAG_BOL 0x0001 /* beginning of line before */
|
---|
| 1084 | #define TOK_FLAG_BOF 0x0002 /* beginning of file before */
|
---|
| 1085 | #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
|
---|
| 1086 | #define TOK_FLAG_EOF 0x0008 /* end of file */
|
---|
| 1087 |
|
---|
| 1088 | #define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
|
---|
| 1089 | #define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */
|
---|
| 1090 | #define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a
|
---|
| 1091 | token. line feed is also
|
---|
| 1092 | returned at eof */
|
---|
| 1093 | #define PARSE_FLAG_ASM_COMMENTS 0x0008 /* '#' can be used for line comment */
|
---|
| 1094 | #define PARSE_FLAG_SPACES 0x0010 /* next() returns space tokens (for -E) */
|
---|
| 1095 |
|
---|
| 1096 | ST_FUNC TokenSym *tok_alloc(const char *str, int len);
|
---|
| 1097 | ST_FUNC char *get_tok_str(int v, CValue *cv);
|
---|
| 1098 | ST_FUNC void save_parse_state(ParseState *s);
|
---|
| 1099 | ST_FUNC void restore_parse_state(ParseState *s);
|
---|
| 1100 | ST_INLN void tok_str_new(TokenString *s);
|
---|
| 1101 | ST_FUNC void tok_str_free(int *str);
|
---|
| 1102 | ST_FUNC void tok_str_add(TokenString *s, int t);
|
---|
| 1103 | ST_FUNC void tok_str_add_tok(TokenString *s);
|
---|
| 1104 | ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg);
|
---|
| 1105 | ST_FUNC void define_undef(Sym *s);
|
---|
| 1106 | ST_INLN Sym *define_find(int v);
|
---|
| 1107 | ST_FUNC void free_defines(Sym *b);
|
---|
| 1108 | ST_FUNC Sym *label_find(int v);
|
---|
| 1109 | ST_FUNC Sym *label_push(Sym **ptop, int v, int flags);
|
---|
| 1110 | ST_FUNC void label_pop(Sym **ptop, Sym *slast);
|
---|
| 1111 | ST_FUNC void parse_define(void);
|
---|
| 1112 | ST_FUNC void preprocess(int is_bof);
|
---|
| 1113 | ST_FUNC void next_nomacro(void);
|
---|
| 1114 | ST_FUNC void next(void);
|
---|
| 1115 | ST_INLN void unget_tok(int last_tok);
|
---|
| 1116 | ST_FUNC void preprocess_init(TCCState *s1);
|
---|
| 1117 | ST_FUNC void preprocess_new(void);
|
---|
| 1118 | ST_FUNC int tcc_preprocess(TCCState *s1);
|
---|
| 1119 | ST_FUNC void skip(int c);
|
---|
| 1120 | ST_FUNC void expect(const char *msg);
|
---|
| 1121 |
|
---|
| 1122 | /* ------------ tccgen.c ------------ */
|
---|
| 1123 |
|
---|
| 1124 | ST_DATA Section *text_section, *data_section, *bss_section; /* predefined sections */
|
---|
| 1125 | ST_DATA Section *cur_text_section; /* current section where function code is generated */
|
---|
| 1126 | #ifdef CONFIG_TCC_ASM
|
---|
| 1127 | ST_DATA Section *last_text_section; /* to handle .previous asm directive */
|
---|
| 1128 | #endif
|
---|
| 1129 | #ifdef CONFIG_TCC_BCHECK
|
---|
| 1130 | /* bound check related sections */
|
---|
| 1131 | ST_DATA Section *bounds_section; /* contains global data bound description */
|
---|
| 1132 | ST_DATA Section *lbounds_section; /* contains local data bound description */
|
---|
| 1133 | #endif
|
---|
| 1134 | /* symbol sections */
|
---|
| 1135 | ST_DATA Section *symtab_section, *strtab_section;
|
---|
| 1136 | /* debug sections */
|
---|
| 1137 | ST_DATA Section *stab_section, *stabstr_section;
|
---|
| 1138 |
|
---|
| 1139 | #define SYM_POOL_NB (8192 / sizeof(Sym))
|
---|
| 1140 | ST_DATA Sym *sym_free_first;
|
---|
| 1141 | ST_DATA void **sym_pools;
|
---|
| 1142 | ST_DATA int nb_sym_pools;
|
---|
| 1143 |
|
---|
| 1144 | ST_DATA Sym *global_stack;
|
---|
| 1145 | ST_DATA Sym *local_stack;
|
---|
| 1146 | ST_DATA Sym *local_label_stack;
|
---|
| 1147 | ST_DATA Sym *global_label_stack;
|
---|
| 1148 | ST_DATA Sym *define_stack;
|
---|
| 1149 | ST_DATA CType char_pointer_type, func_old_type, int_type, size_type;
|
---|
| 1150 | ST_DATA SValue __vstack[1+/*to make bcheck happy*/ VSTACK_SIZE], *vtop;
|
---|
| 1151 | #define vstack (__vstack + 1)
|
---|
| 1152 | ST_DATA int rsym, anon_sym, ind, loc;
|
---|
| 1153 |
|
---|
| 1154 | ST_DATA int const_wanted; /* true if constant wanted */
|
---|
| 1155 | ST_DATA int nocode_wanted; /* true if no code generation wanted for an expression */
|
---|
| 1156 | ST_DATA int global_expr; /* true if compound literals must be allocated globally (used during initializers parsing */
|
---|
| 1157 | ST_DATA CType func_vt; /* current function return type (used by return instruction) */
|
---|
| 1158 | ST_DATA int func_vc;
|
---|
| 1159 | ST_DATA int last_line_num, last_ind, func_ind; /* debug last line number and pc */
|
---|
| 1160 | ST_DATA char *funcname;
|
---|
| 1161 |
|
---|
| 1162 | ST_INLN int is_float(int t);
|
---|
| 1163 | ST_FUNC int ieee_finite(double d);
|
---|
| 1164 | ST_FUNC void test_lvalue(void);
|
---|
| 1165 | ST_FUNC void swap(int *p, int *q);
|
---|
| 1166 | ST_FUNC void vpushi(int v);
|
---|
| 1167 | ST_FUNC Sym *external_global_sym(int v, CType *type, int r);
|
---|
| 1168 | ST_FUNC void vset(CType *type, int r, int v);
|
---|
| 1169 | ST_FUNC void vswap(void);
|
---|
| 1170 | ST_FUNC void vpush_global_sym(CType *type, int v);
|
---|
| 1171 | ST_FUNC void vrote(SValue *e, int n);
|
---|
| 1172 | ST_FUNC void vrott(int n);
|
---|
| 1173 | ST_FUNC void vrotb(int n);
|
---|
| 1174 | #ifdef TCC_TARGET_ARM
|
---|
| 1175 | ST_FUNC int get_reg_ex(int rc, int rc2);
|
---|
| 1176 | ST_FUNC void lexpand_nr(void);
|
---|
| 1177 | #endif
|
---|
| 1178 | ST_FUNC void vpushv(SValue *v);
|
---|
| 1179 | ST_FUNC void save_reg(int r);
|
---|
| 1180 | ST_FUNC int get_reg(int rc);
|
---|
| 1181 | ST_FUNC void save_regs(int n);
|
---|
| 1182 | ST_FUNC int gv(int rc);
|
---|
| 1183 | ST_FUNC void gv2(int rc1, int rc2);
|
---|
| 1184 | ST_FUNC void vpop(void);
|
---|
| 1185 | ST_FUNC void gen_op(int op);
|
---|
| 1186 | ST_FUNC int type_size(CType *type, int *a);
|
---|
| 1187 | ST_FUNC void mk_pointer(CType *type);
|
---|
| 1188 | ST_FUNC void vstore(void);
|
---|
| 1189 | ST_FUNC void inc(int post, int c);
|
---|
| 1190 | ST_FUNC void parse_asm_str(CString *astr);
|
---|
| 1191 | ST_FUNC int lvalue_type(int t);
|
---|
| 1192 | ST_FUNC void indir(void);
|
---|
| 1193 | ST_FUNC void unary(void);
|
---|
| 1194 | ST_FUNC void expr_prod(void);
|
---|
| 1195 | ST_FUNC void expr_sum(void);
|
---|
| 1196 | ST_FUNC void gexpr(void);
|
---|
| 1197 | ST_FUNC int expr_const(void);
|
---|
| 1198 | ST_FUNC void gen_inline_functions(void);
|
---|
| 1199 | ST_FUNC void decl(int l);
|
---|
| 1200 | #if defined CONFIG_TCC_BCHECK || defined TCC_TARGET_C67
|
---|
| 1201 | ST_FUNC Sym *get_sym_ref(CType *type, Section *sec, unsigned long offset, unsigned long size);
|
---|
| 1202 | #endif
|
---|
| 1203 |
|
---|
| 1204 | /* ------------ tccelf.c ------------ */
|
---|
| 1205 |
|
---|
| 1206 | #define TCC_OUTPUT_FORMAT_ELF 0 /* default output format: ELF */
|
---|
| 1207 | #define TCC_OUTPUT_FORMAT_BINARY 1 /* binary image output */
|
---|
| 1208 | #define TCC_OUTPUT_FORMAT_COFF 2 /* COFF */
|
---|
| 1209 |
|
---|
| 1210 | #define ARMAG "!<arch>\012" /* For COFF and a.out archives */
|
---|
| 1211 |
|
---|
| 1212 | typedef struct {
|
---|
| 1213 | unsigned int n_strx; /* index into string table of name */
|
---|
| 1214 | unsigned char n_type; /* type of symbol */
|
---|
| 1215 | unsigned char n_other; /* misc info (usually empty) */
|
---|
| 1216 | unsigned short n_desc; /* description field */
|
---|
| 1217 | unsigned int n_value; /* value of symbol */
|
---|
| 1218 | } Stab_Sym;
|
---|
| 1219 |
|
---|
| 1220 | 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);
|
---|
| 1221 |
|
---|
| 1222 | ST_FUNC int put_elf_str(Section *s, const char *sym);
|
---|
| 1223 | ST_FUNC int put_elf_sym(Section *s, addr_t value, unsigned long size, int info, int other, int shndx, const char *name);
|
---|
| 1224 | ST_FUNC int add_elf_sym(Section *s, addr_t value, unsigned long size, int info, int other, int sh_num, const char *name);
|
---|
| 1225 | ST_FUNC int find_elf_sym(Section *s, const char *name);
|
---|
| 1226 | ST_FUNC void put_elf_reloc(Section *symtab, Section *s, unsigned long offset, int type, int symbol);
|
---|
| 1227 |
|
---|
| 1228 | ST_FUNC void put_stabs(const char *str, int type, int other, int desc, unsigned long value);
|
---|
| 1229 | ST_FUNC void put_stabs_r(const char *str, int type, int other, int desc, unsigned long value, Section *sec, int sym_index);
|
---|
| 1230 | ST_FUNC void put_stabn(int type, int other, int desc, int value);
|
---|
| 1231 | ST_FUNC void put_stabd(int type, int other, int desc);
|
---|
| 1232 |
|
---|
| 1233 | ST_FUNC void relocate_common_syms(void);
|
---|
| 1234 | ST_FUNC void relocate_syms(TCCState *s1, int do_resolve);
|
---|
| 1235 | ST_FUNC void relocate_section(TCCState *s1, Section *s);
|
---|
| 1236 |
|
---|
| 1237 | ST_FUNC void tcc_add_linker_symbols(TCCState *s1);
|
---|
| 1238 | ST_FUNC int tcc_load_object_file(TCCState *s1, int fd, unsigned long file_offset);
|
---|
| 1239 | ST_FUNC int tcc_load_archive(TCCState *s1, int fd);
|
---|
| 1240 | ST_FUNC void tcc_add_bcheck(TCCState *s1);
|
---|
| 1241 |
|
---|
| 1242 | ST_FUNC void build_got_entries(TCCState *s1);
|
---|
| 1243 | ST_FUNC void tcc_add_runtime(TCCState *s1);
|
---|
| 1244 |
|
---|
| 1245 | ST_FUNC addr_t get_elf_sym_addr(TCCState *s, const char *name, int err);
|
---|
| 1246 | #ifdef TCC_IS_NATIVE
|
---|
| 1247 | ST_FUNC void *tcc_get_symbol_err(TCCState *s, const char *name);
|
---|
| 1248 | #endif
|
---|
| 1249 |
|
---|
| 1250 | #ifndef TCC_TARGET_PE
|
---|
| 1251 | ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level);
|
---|
| 1252 | ST_FUNC int tcc_load_ldscript(TCCState *s1);
|
---|
| 1253 | ST_FUNC uint8_t *parse_comment(uint8_t *p);
|
---|
| 1254 | ST_FUNC void minp(void);
|
---|
| 1255 | ST_INLN void inp(void);
|
---|
| 1256 | ST_FUNC int handle_eob(void);
|
---|
| 1257 | #endif
|
---|
| 1258 |
|
---|
| 1259 | /* ------------ xxx-gen.c ------------ */
|
---|
| 1260 |
|
---|
| 1261 | #ifdef TCC_TARGET_X86_64
|
---|
| 1262 | ST_DATA const int reg_classes[NB_REGS+7];
|
---|
| 1263 | #else
|
---|
| 1264 | ST_DATA const int reg_classes[NB_REGS];
|
---|
| 1265 | #endif
|
---|
| 1266 |
|
---|
| 1267 | ST_FUNC void gsym_addr(int t, int a);
|
---|
| 1268 | ST_FUNC void gsym(int t);
|
---|
| 1269 | ST_FUNC void load(int r, SValue *sv);
|
---|
| 1270 | ST_FUNC void store(int r, SValue *v);
|
---|
| 1271 | ST_FUNC void gfunc_call(int nb_args);
|
---|
| 1272 | ST_FUNC void gfunc_prolog(CType *func_type);
|
---|
| 1273 | ST_FUNC void gfunc_epilog(void);
|
---|
| 1274 | ST_FUNC int gjmp(int t);
|
---|
| 1275 | ST_FUNC void gjmp_addr(int a);
|
---|
| 1276 | ST_FUNC int gtst(int inv, int t);
|
---|
| 1277 | ST_FUNC void gen_opi(int op);
|
---|
| 1278 | ST_FUNC void gen_opf(int op);
|
---|
| 1279 | ST_FUNC void gen_cvt_ftoi(int t);
|
---|
| 1280 | ST_FUNC void gen_cvt_ftof(int t);
|
---|
| 1281 | ST_FUNC void ggoto(void);
|
---|
| 1282 | #ifndef TCC_TARGET_C67
|
---|
| 1283 | ST_FUNC void o(unsigned int c);
|
---|
| 1284 | #endif
|
---|
| 1285 | #ifndef TCC_TARGET_ARM
|
---|
| 1286 | ST_FUNC void gen_cvt_itof(int t);
|
---|
| 1287 | #endif
|
---|
| 1288 |
|
---|
| 1289 | /* ------------ i386-gen.c ------------ */
|
---|
| 1290 | #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
|
---|
| 1291 | ST_FUNC void g(int c);
|
---|
| 1292 | ST_FUNC int oad(int c, int s);
|
---|
| 1293 | ST_FUNC void gen_le16(int c);
|
---|
| 1294 | ST_FUNC void gen_le32(int c);
|
---|
| 1295 | ST_FUNC void gen_addr32(int r, Sym *sym, int c);
|
---|
| 1296 | ST_FUNC void gen_addrpc32(int r, Sym *sym, int c);
|
---|
| 1297 | #endif
|
---|
| 1298 |
|
---|
| 1299 | #ifdef CONFIG_TCC_BCHECK
|
---|
| 1300 | ST_FUNC void gen_bounded_ptr_add(void);
|
---|
| 1301 | ST_FUNC void gen_bounded_ptr_deref(void);
|
---|
| 1302 | #endif
|
---|
| 1303 |
|
---|
| 1304 | /* ------------ x86_64-gen.c ------------ */
|
---|
| 1305 | #ifdef TCC_TARGET_X86_64
|
---|
| 1306 | ST_FUNC void gen_addr64(int r, Sym *sym, int64_t c);
|
---|
| 1307 | ST_FUNC void gen_opl(int op);
|
---|
| 1308 | #endif
|
---|
| 1309 |
|
---|
| 1310 | /* ------------ arm-gen.c ------------ */
|
---|
| 1311 | #ifdef TCC_TARGET_ARM
|
---|
| 1312 | ST_FUNC void arm_init_types(void);
|
---|
| 1313 | ST_FUNC uint32_t encbranch(int pos, int addr, int fail);
|
---|
| 1314 | ST_FUNC void gen_cvt_itof1(int t);
|
---|
| 1315 | #endif
|
---|
| 1316 |
|
---|
| 1317 | /* ------------ c67-gen.c ------------ */
|
---|
| 1318 | #ifdef TCC_TARGET_C67
|
---|
| 1319 | #endif
|
---|
| 1320 |
|
---|
| 1321 | /* ------------ tcccoff.c ------------ */
|
---|
| 1322 |
|
---|
| 1323 | #ifdef TCC_TARGET_COFF
|
---|
| 1324 | ST_FUNC int tcc_output_coff(TCCState *s1, FILE *f);
|
---|
| 1325 | ST_FUNC int tcc_load_coff(TCCState * s1, int fd);
|
---|
| 1326 | #endif
|
---|
| 1327 |
|
---|
| 1328 | /* ------------ tccasm.c ------------ */
|
---|
| 1329 | ST_FUNC void asm_instr(void);
|
---|
| 1330 | ST_FUNC void asm_global_instr(void);
|
---|
| 1331 | #ifdef CONFIG_TCC_ASM
|
---|
| 1332 | ST_FUNC int find_constraint(ASMOperand *operands, int nb_operands, const char *name, const char **pp);
|
---|
| 1333 | ST_FUNC void asm_expr(TCCState *s1, ExprValue *pe);
|
---|
| 1334 | ST_FUNC int asm_int_expr(TCCState *s1);
|
---|
| 1335 | ST_FUNC int tcc_assemble(TCCState *s1, int do_preprocess);
|
---|
| 1336 | /* ------------ i386-asm.c ------------ */
|
---|
| 1337 | ST_FUNC void gen_expr32(ExprValue *pe);
|
---|
| 1338 | ST_FUNC void asm_opcode(TCCState *s1, int opcode);
|
---|
| 1339 | ST_FUNC void asm_compute_constraints(ASMOperand *operands, int nb_operands, int nb_outputs, const uint8_t *clobber_regs, int *pout_reg);
|
---|
| 1340 | ST_FUNC void subst_asm_operand(CString *add_str, SValue *sv, int modifier);
|
---|
| 1341 | ST_FUNC void asm_gen_code(ASMOperand *operands, int nb_operands, int nb_outputs, int is_output, uint8_t *clobber_regs, int out_reg);
|
---|
| 1342 | ST_FUNC void asm_clobber(uint8_t *clobber_regs, const char *str);
|
---|
| 1343 | #endif
|
---|
| 1344 |
|
---|
| 1345 | /* ------------ tccpe.c -------------- */
|
---|
| 1346 | #ifdef TCC_TARGET_PE
|
---|
| 1347 | ST_FUNC int pe_load_file(struct TCCState *s1, const char *filename, int fd);
|
---|
| 1348 | ST_FUNC int pe_output_file(TCCState * s1, const char *filename);
|
---|
| 1349 | ST_FUNC int pe_putimport(TCCState *s1, int dllindex, const char *name, addr_t value);
|
---|
| 1350 | ST_FUNC SValue *pe_getimport(SValue *sv, SValue *v2);
|
---|
| 1351 | #ifdef TCC_TARGET_X86_64
|
---|
| 1352 | ST_FUNC void pe_add_unwind_data(unsigned start, unsigned end, unsigned stack);
|
---|
| 1353 | #endif
|
---|
| 1354 | #endif
|
---|
| 1355 |
|
---|
| 1356 | /* ------------ tccrun.c ----------------- */
|
---|
| 1357 | #ifdef TCC_IS_NATIVE
|
---|
| 1358 | #ifdef CONFIG_TCC_STATIC
|
---|
| 1359 | #define RTLD_LAZY 0x001
|
---|
| 1360 | #define RTLD_NOW 0x002
|
---|
| 1361 | #define RTLD_GLOBAL 0x100
|
---|
| 1362 | #define RTLD_DEFAULT NULL
|
---|
| 1363 | /* dummy function for profiling */
|
---|
| 1364 | ST_FUNC void *dlopen(const char *filename, int flag);
|
---|
| 1365 | ST_FUNC void dlclose(void *p);
|
---|
| 1366 | ST_FUNC const char *dlerror(void);
|
---|
| 1367 | ST_FUNC void *resolve_sym(TCCState *s1, const char *symbol);
|
---|
| 1368 | #elif !defined _WIN32
|
---|
| 1369 | ST_FUNC void *resolve_sym(TCCState *s1, const char *symbol);
|
---|
| 1370 | #endif
|
---|
| 1371 |
|
---|
| 1372 | #ifdef CONFIG_TCC_BACKTRACE
|
---|
| 1373 | ST_DATA int rt_num_callers;
|
---|
| 1374 | ST_DATA const char **rt_bound_error_msg;
|
---|
| 1375 | ST_DATA void *rt_prog_main;
|
---|
| 1376 | ST_FUNC void tcc_set_num_callers(int n);
|
---|
| 1377 | #endif
|
---|
| 1378 | #endif
|
---|
| 1379 |
|
---|
| 1380 | /********************************************************/
|
---|
| 1381 | #undef ST_DATA
|
---|
| 1382 | #ifdef ONE_SOURCE
|
---|
| 1383 | #define ST_DATA static
|
---|
| 1384 | #else
|
---|
| 1385 | #define ST_DATA
|
---|
| 1386 | #endif
|
---|
| 1387 | /********************************************************/
|
---|
| 1388 | #endif /* _TCC_H */
|
---|