2 * TCC - Tiny C Compiler
4 * Copyright (c) 2001-2004 Fabrice Bellard
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.
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.
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
21 #if !defined ONE_SOURCE || ONE_SOURCE
26 #ifdef TCC_TARGET_I386
28 #include "i386-link.c"
30 #elif defined(TCC_TARGET_ARM)
34 #elif defined(TCC_TARGET_ARM64)
35 #include "arm64-gen.c"
36 #include "arm64-link.c"
38 #elif defined(TCC_TARGET_C67)
42 #elif defined(TCC_TARGET_X86_64)
43 #include "x86_64-gen.c"
44 #include "x86_64-link.c"
46 #elif defined(TCC_TARGET_RISCV64)
47 #include "riscv64-gen.c"
48 #include "riscv64-link.c"
49 #include "riscv64-asm.c"
59 #ifdef TCC_TARGET_MACHO
62 #endif /* ONE_SOURCE */
66 /********************************************************/
67 /* global variables */
69 /* XXX: get rid of this ASAP (or maybe not) */
70 ST_DATA
struct TCCState
*tcc_state
;
76 /********************************************************/
78 ST_FUNC
char *normalize_slashes(char *path
)
81 for (p
= path
; *p
; ++p
)
87 static HMODULE tcc_module
;
89 /* on win32, we suppose the lib and includes are at the location of 'tcc.exe' */
90 static void tcc_set_lib_path_w32(TCCState
*s
)
93 GetModuleFileNameA(tcc_module
, path
, sizeof path
);
94 p
= tcc_basename(normalize_slashes(strlwr(path
)));
98 tcc_set_lib_path(s
, path
);
102 static void tcc_add_systemdir(TCCState
*s
)
105 GetSystemDirectory(buf
, sizeof buf
);
106 tcc_add_library_path(s
, normalize_slashes(buf
));
111 BOOL WINAPI
DllMain (HINSTANCE hDll
, DWORD dwReason
, LPVOID lpReserved
)
113 if (DLL_PROCESS_ATTACH
== dwReason
)
120 /********************************************************/
121 #ifndef CONFIG_TCC_SEMLOCK
125 static int tcc_sem_init
;
126 static CRITICAL_SECTION tcc_cr
;
127 static void wait_sem(void)
130 InitializeCriticalSection(&tcc_cr
), tcc_sem_init
= 1;
131 EnterCriticalSection(&tcc_cr
);
133 #define WAIT_SEM() wait_sem()
134 #define POST_SEM() LeaveCriticalSection(&tcc_cr);
135 #elif defined __APPLE__
136 /* Half-compatible MacOS doesn't have non-shared (process local)
137 semaphores. Use the dispatch framework for lightweight locks. */
138 #include <dispatch/dispatch.h>
139 static int tcc_sem_init
;
140 static dispatch_semaphore_t tcc_sem
;
141 static void wait_sem(void)
144 tcc_sem
= dispatch_semaphore_create(1), tcc_sem_init
= 1;
145 dispatch_semaphore_wait(tcc_sem
, DISPATCH_TIME_FOREVER
);
147 #define WAIT_SEM() wait_sem()
148 #define POST_SEM() dispatch_semaphore_signal(tcc_sem)
150 #include <semaphore.h>
151 static int tcc_sem_init
;
152 static sem_t tcc_sem
;
153 static void wait_sem(void)
156 sem_init(&tcc_sem
, 0, 1), tcc_sem_init
= 1;
157 while (sem_wait (&tcc_sem
) < 0 && errno
== EINTR
);
159 #define WAIT_SEM() wait_sem()
160 #define POST_SEM() sem_post(&tcc_sem)
163 /********************************************************/
164 /* copy a string and truncate it. */
165 ST_FUNC
char *pstrcpy(char *buf
, size_t buf_size
, const char *s
)
172 q_end
= buf
+ buf_size
- 1;
184 /* strcat and truncate. */
185 ST_FUNC
char *pstrcat(char *buf
, size_t buf_size
, const char *s
)
190 pstrcpy(buf
+ len
, buf_size
- len
, s
);
194 ST_FUNC
char *pstrncpy(char *out
, const char *in
, size_t num
)
196 memcpy(out
, in
, num
);
201 /* extract the basename of a file */
202 PUB_FUNC
char *tcc_basename(const char *name
)
204 char *p
= strchr(name
, 0);
205 while (p
> name
&& !IS_DIRSEP(p
[-1]))
210 /* extract extension part of a file
212 * (if no extension, return pointer to end-of-string)
214 PUB_FUNC
char *tcc_fileextension (const char *name
)
216 char *b
= tcc_basename(name
);
217 char *e
= strrchr(b
, '.');
218 return e
? e
: strchr(b
, 0);
221 /********************************************************/
222 /* memory management */
230 PUB_FUNC
void tcc_free(void *ptr
)
235 PUB_FUNC
void *tcc_malloc(unsigned long size
)
240 _tcc_error("memory full (malloc)");
244 PUB_FUNC
void *tcc_mallocz(unsigned long size
)
247 ptr
= tcc_malloc(size
);
248 memset(ptr
, 0, size
);
252 PUB_FUNC
void *tcc_realloc(void *ptr
, unsigned long size
)
255 ptr1
= realloc(ptr
, size
);
257 _tcc_error("memory full (realloc)");
261 PUB_FUNC
char *tcc_strdup(const char *str
)
264 ptr
= tcc_malloc(strlen(str
) + 1);
271 #define MEM_DEBUG_MAGIC1 0xFEEDDEB1
272 #define MEM_DEBUG_MAGIC2 0xFEEDDEB2
273 #define MEM_DEBUG_MAGIC3 0xFEEDDEB3
274 #define MEM_DEBUG_FILE_LEN 40
275 #define MEM_DEBUG_CHECK3(header) \
276 ((mem_debug_header_t*)((char*)header + header->size))->magic3
277 #define MEM_USER_PTR(header) \
278 ((char *)header + offsetof(mem_debug_header_t, magic3))
279 #define MEM_HEADER_PTR(ptr) \
280 (mem_debug_header_t *)((char*)ptr - offsetof(mem_debug_header_t, magic3))
282 struct mem_debug_header
{
285 struct mem_debug_header
*prev
;
286 struct mem_debug_header
*next
;
288 char file_name
[MEM_DEBUG_FILE_LEN
+ 1];
290 ALIGNED(16) unsigned magic3
;
293 typedef struct mem_debug_header mem_debug_header_t
;
295 static mem_debug_header_t
*mem_debug_chain
;
296 static unsigned mem_cur_size
;
297 static unsigned mem_max_size
;
299 static mem_debug_header_t
*malloc_check(void *ptr
, const char *msg
)
301 mem_debug_header_t
* header
= MEM_HEADER_PTR(ptr
);
302 if (header
->magic1
!= MEM_DEBUG_MAGIC1
||
303 header
->magic2
!= MEM_DEBUG_MAGIC2
||
304 MEM_DEBUG_CHECK3(header
) != MEM_DEBUG_MAGIC3
||
305 header
->size
== (unsigned)-1) {
306 fprintf(stderr
, "%s check failed\n", msg
);
307 if (header
->magic1
== MEM_DEBUG_MAGIC1
)
308 fprintf(stderr
, "%s:%u: block allocated here.\n",
309 header
->file_name
, header
->line_num
);
315 PUB_FUNC
void *tcc_malloc_debug(unsigned long size
, const char *file
, int line
)
318 mem_debug_header_t
*header
;
320 header
= malloc(sizeof(mem_debug_header_t
) + size
);
322 _tcc_error("memory full (malloc)");
324 header
->magic1
= MEM_DEBUG_MAGIC1
;
325 header
->magic2
= MEM_DEBUG_MAGIC2
;
327 MEM_DEBUG_CHECK3(header
) = MEM_DEBUG_MAGIC3
;
328 header
->line_num
= line
;
329 ofs
= strlen(file
) - MEM_DEBUG_FILE_LEN
;
330 strncpy(header
->file_name
, file
+ (ofs
> 0 ? ofs
: 0), MEM_DEBUG_FILE_LEN
);
331 header
->file_name
[MEM_DEBUG_FILE_LEN
] = 0;
333 header
->next
= mem_debug_chain
;
336 header
->next
->prev
= header
;
337 mem_debug_chain
= header
;
339 mem_cur_size
+= size
;
340 if (mem_cur_size
> mem_max_size
)
341 mem_max_size
= mem_cur_size
;
343 return MEM_USER_PTR(header
);
346 PUB_FUNC
void tcc_free_debug(void *ptr
)
348 mem_debug_header_t
*header
;
351 header
= malloc_check(ptr
, "tcc_free");
352 mem_cur_size
-= header
->size
;
353 header
->size
= (unsigned)-1;
355 header
->next
->prev
= header
->prev
;
357 header
->prev
->next
= header
->next
;
358 if (header
== mem_debug_chain
)
359 mem_debug_chain
= header
->next
;
363 PUB_FUNC
void *tcc_mallocz_debug(unsigned long size
, const char *file
, int line
)
366 ptr
= tcc_malloc_debug(size
,file
,line
);
367 memset(ptr
, 0, size
);
371 PUB_FUNC
void *tcc_realloc_debug(void *ptr
, unsigned long size
, const char *file
, int line
)
373 mem_debug_header_t
*header
;
374 int mem_debug_chain_update
= 0;
376 return tcc_malloc_debug(size
, file
, line
);
377 header
= malloc_check(ptr
, "tcc_realloc");
378 mem_cur_size
-= header
->size
;
379 mem_debug_chain_update
= (header
== mem_debug_chain
);
380 header
= realloc(header
, sizeof(mem_debug_header_t
) + size
);
382 _tcc_error("memory full (realloc)");
384 MEM_DEBUG_CHECK3(header
) = MEM_DEBUG_MAGIC3
;
386 header
->next
->prev
= header
;
388 header
->prev
->next
= header
;
389 if (mem_debug_chain_update
)
390 mem_debug_chain
= header
;
391 mem_cur_size
+= size
;
392 if (mem_cur_size
> mem_max_size
)
393 mem_max_size
= mem_cur_size
;
394 return MEM_USER_PTR(header
);
397 PUB_FUNC
char *tcc_strdup_debug(const char *str
, const char *file
, int line
)
400 ptr
= tcc_malloc_debug(strlen(str
) + 1, file
, line
);
405 PUB_FUNC
void tcc_memcheck(void)
408 mem_debug_header_t
*header
= mem_debug_chain
;
409 fprintf(stderr
, "MEM_DEBUG: mem_leak= %d bytes, mem_max_size= %d bytes\n",
410 mem_cur_size
, mem_max_size
);
412 fprintf(stderr
, "%s:%u: error: %u bytes leaked\n",
413 header
->file_name
, header
->line_num
, header
->size
);
414 header
= header
->next
;
421 #endif /* MEM_DEBUG */
423 #define free(p) use_tcc_free(p)
424 #define malloc(s) use_tcc_malloc(s)
425 #define realloc(p, s) use_tcc_realloc(p, s)
427 /********************************************************/
430 ST_FUNC
void dynarray_add(void *ptab
, int *nb_ptr
, void *data
)
436 pp
= *(void ***)ptab
;
437 /* every power of two we double array size */
438 if ((nb
& (nb
- 1)) == 0) {
443 pp
= tcc_realloc(pp
, nb_alloc
* sizeof(void *));
450 ST_FUNC
void dynarray_reset(void *pp
, int *n
)
453 for (p
= *(void***)pp
; *n
; ++p
, --*n
)
456 tcc_free(*(void**)pp
);
460 static void tcc_split_path(TCCState
*s
, void *p_ary
, int *p_nb_ary
, const char *in
)
468 for (p
= in
; c
= *p
, c
!= '\0' && c
!= PATHSEP
[0]; ++p
) {
469 if (c
== '{' && p
[1] && p
[2] == '}') {
472 cstr_cat(&str
, s
->tcc_lib_path
, -1);
473 if (c
== 'f' && file
) {
474 /* substitute current file's dir */
475 const char *f
= file
->true_filename
;
476 const char *b
= tcc_basename(f
);
478 cstr_cat(&str
, f
, b
- f
- 1);
480 cstr_cat(&str
, ".", 1);
487 cstr_ccat(&str
, '\0');
488 dynarray_add(p_ary
, p_nb_ary
, tcc_strdup(str
.data
));
495 /********************************************************/
497 static void strcat_vprintf(char *buf
, int buf_size
, const char *fmt
, va_list ap
)
501 vsnprintf(buf
+ len
, buf_size
- len
, fmt
, ap
);
504 static void strcat_printf(char *buf
, int buf_size
, const char *fmt
, ...)
508 strcat_vprintf(buf
, buf_size
, fmt
, ap
);
513 #define ERROR_NOABORT 1
514 #define ERROR_ERROR 2
516 PUB_FUNC
void tcc_enter_state(TCCState
*s1
)
522 PUB_FUNC
void tcc_exit_state(void)
528 static void error1(int mode
, const char *fmt
, va_list ap
)
531 BufferedFile
**pf
, *f
;
532 TCCState
*s1
= tcc_state
;
536 /* can happen only if called from tcc_malloc(): 'out of memory' */
539 if (s1
&& !s1
->error_set_jmp_enabled
)
540 /* tcc_state just was set by tcc_enter_state() */
543 if (mode
== ERROR_WARN
) {
551 if (s1
->error_set_jmp_enabled
) { /* we're called while parsing a file */
552 /* use upper file if inline ":asm:" or token ":paste:" */
553 for (f
= file
; f
&& f
->filename
[0] == ':'; f
= f
->prev
)
557 for(pf
= s1
->include_stack
; pf
< s1
->include_stack_ptr
; pf
++)
558 strcat_printf(buf
, sizeof(buf
), "In file included from %s:%d:\n",
559 (*pf
)->filename
, (*pf
)->line_num
);
560 strcat_printf(buf
, sizeof(buf
), "%s:%d: ",
561 f
->filename
, f
->line_num
- !!(tok_flags
& TOK_FLAG_BOL
));
562 } else if (s1
->current_filename
) {
563 strcat_printf(buf
, sizeof(buf
), "%s: ", s1
->current_filename
);
568 strcat_printf(buf
, sizeof(buf
), "tcc: ");
569 if (mode
== ERROR_WARN
)
570 strcat_printf(buf
, sizeof(buf
), "warning: ");
572 strcat_printf(buf
, sizeof(buf
), "error: ");
573 strcat_vprintf(buf
, sizeof(buf
), fmt
, ap
);
574 if (!s1
|| !s1
->error_func
) {
575 /* default case: stderr */
576 if (s1
&& s1
->output_type
== TCC_OUTPUT_PREPROCESS
&& s1
->ppfp
== stdout
)
577 /* print a newline during tcc -E */
578 printf("\n"), fflush(stdout
);
579 fflush(stdout
); /* flush -v output */
580 fprintf(stderr
, "%s\n", buf
);
581 fflush(stderr
); /* print error/warning now (win32) */
583 s1
->error_func(s1
->error_opaque
, buf
);
586 if (mode
!= ERROR_WARN
)
588 if (mode
!= ERROR_ERROR
)
590 if (s1
->error_set_jmp_enabled
)
591 longjmp(s1
->error_jmp_buf
, 1);
596 LIBTCCAPI
void tcc_set_error_func(TCCState
*s
, void *error_opaque
, TCCErrorFunc error_func
)
598 s
->error_opaque
= error_opaque
;
599 s
->error_func
= error_func
;
602 LIBTCCAPI TCCErrorFunc
tcc_get_error_func(TCCState
*s
)
604 return s
->error_func
;
607 LIBTCCAPI
void *tcc_get_error_opaque(TCCState
*s
)
609 return s
->error_opaque
;
612 /* error without aborting current compilation */
613 PUB_FUNC
void _tcc_error_noabort(const char *fmt
, ...)
617 error1(ERROR_NOABORT
, fmt
, ap
);
621 PUB_FUNC
void _tcc_error(const char *fmt
, ...)
625 for (;;) error1(ERROR_ERROR
, fmt
, ap
);
628 PUB_FUNC
void _tcc_warning(const char *fmt
, ...)
632 error1(ERROR_WARN
, fmt
, ap
);
636 /********************************************************/
639 ST_FUNC
void tcc_open_bf(TCCState
*s1
, const char *filename
, int initlen
)
642 int buflen
= initlen
? initlen
: IO_BUF_SIZE
;
644 bf
= tcc_mallocz(sizeof(BufferedFile
) + buflen
);
645 bf
->buf_ptr
= bf
->buffer
;
646 bf
->buf_end
= bf
->buffer
+ initlen
;
647 bf
->buf_end
[0] = CH_EOB
; /* put eob symbol */
648 pstrcpy(bf
->filename
, sizeof(bf
->filename
), filename
);
650 normalize_slashes(bf
->filename
);
652 bf
->true_filename
= bf
->filename
;
654 bf
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
658 tok_flags
= TOK_FLAG_BOL
| TOK_FLAG_BOF
;
661 ST_FUNC
void tcc_close(void)
663 TCCState
*s1
= tcc_state
;
664 BufferedFile
*bf
= file
;
667 total_lines
+= bf
->line_num
;
669 if (bf
->true_filename
!= bf
->filename
)
670 tcc_free(bf
->true_filename
);
675 static int _tcc_open(TCCState
*s1
, const char *filename
)
678 if (strcmp(filename
, "-") == 0)
679 fd
= 0, filename
= "<stdin>";
681 fd
= open(filename
, O_RDONLY
| O_BINARY
);
682 if ((s1
->verbose
== 2 && fd
>= 0) || s1
->verbose
== 3)
683 printf("%s %*s%s\n", fd
< 0 ? "nf":"->",
684 (int)(s1
->include_stack_ptr
- s1
->include_stack
), "", filename
);
688 ST_FUNC
int tcc_open(TCCState
*s1
, const char *filename
)
690 int fd
= _tcc_open(s1
, filename
);
693 tcc_open_bf(s1
, filename
, 0);
698 /* compile the file opened in 'file'. Return non zero if errors. */
699 static int tcc_compile(TCCState
*s1
, int filetype
, const char *str
, int fd
)
701 /* Here we enter the code section where we use the global variables for
702 parsing and code generation (tccpp.c, tccgen.c, <target>-gen.c).
703 Other threads need to wait until we're done.
705 Alternatively we could use thread local storage for those global
706 variables, which may or may not have advantages */
710 if (setjmp(s1
->error_jmp_buf
) == 0) {
711 s1
->error_set_jmp_enabled
= 1;
715 int len
= strlen(str
);
716 tcc_open_bf(s1
, "<string>", len
);
717 memcpy(file
->buffer
, str
, len
);
719 tcc_open_bf(s1
, str
, 0);
723 tccelf_begin_file(s1
);
724 preprocess_start(s1
, filetype
);
726 if (s1
->output_type
== TCC_OUTPUT_PREPROCESS
) {
728 } else if (filetype
& (AFF_TYPE_ASM
| AFF_TYPE_ASMPP
)) {
729 #ifdef CONFIG_TCC_ASM
730 tcc_assemble(s1
, !!(filetype
& AFF_TYPE_ASMPP
));
732 tcc_error_noabort("asm not supported");
738 s1
->error_set_jmp_enabled
= 0;
744 return s1
->nb_errors
!= 0 ? -1 : 0;
747 LIBTCCAPI
int tcc_compile_string(TCCState
*s
, const char *str
)
749 return tcc_compile(s
, s
->filetype
, str
, -1);
752 /* define a preprocessor symbol. value can be NULL, sym can be "sym=val" */
753 LIBTCCAPI
void tcc_define_symbol(TCCState
*s1
, const char *sym
, const char *value
)
756 if (NULL
== (eq
= strchr(sym
, '=')))
759 value
= *eq
? eq
+ 1 : "1";
760 cstr_printf(&s1
->cmdline_defs
, "#define %.*s %s\n", (int)(eq
-sym
), sym
, value
);
763 /* undefine a preprocessor symbol */
764 LIBTCCAPI
void tcc_undefine_symbol(TCCState
*s1
, const char *sym
)
766 cstr_printf(&s1
->cmdline_defs
, "#undef %s\n", sym
);
770 LIBTCCAPI TCCState
*tcc_new(void)
774 s
= tcc_mallocz(sizeof(TCCState
));
786 s
->dollars_in_identifiers
= 1; /*on by default like in gcc/clang*/
787 s
->cversion
= 199901; /* default unless -std=c11 is supplied */
788 s
->warn_implicit_function_declaration
= 1;
789 s
->ms_extensions
= 1;
791 #ifdef CHAR_IS_UNSIGNED
792 s
->char_is_unsigned
= 1;
794 #ifdef TCC_TARGET_I386
797 /* enable this if you want symbols with leading underscore on windows: */
798 #if defined TCC_TARGET_MACHO /* || defined TCC_TARGET_PE */
799 s
->leading_underscore
= 1;
802 /* might be used in error() before preprocess_start() */
803 s
->include_stack_ptr
= s
->include_stack
;
808 tcc_set_lib_path_w32(s
);
810 tcc_set_lib_path(s
, CONFIG_TCCDIR
);
814 /* define __TINYC__ 92X */
815 char buffer
[32]; int a
,b
,c
;
816 sscanf(TCC_VERSION
, "%d.%d.%d", &a
, &b
, &c
);
817 sprintf(buffer
, "%d", a
*10000 + b
*100 + c
);
818 tcc_define_symbol(s
, "__TINYC__", buffer
);
821 /* standard defines */
822 tcc_define_symbol(s
, "__STDC__", NULL
);
823 tcc_define_symbol(s
, "__STDC_VERSION__", "199901L");
824 tcc_define_symbol(s
, "__STDC_HOSTED__", NULL
);
827 #if defined(TCC_TARGET_I386)
828 tcc_define_symbol(s
, "__i386__", NULL
);
829 tcc_define_symbol(s
, "__i386", NULL
);
830 tcc_define_symbol(s
, "i386", NULL
);
831 #elif defined(TCC_TARGET_X86_64)
832 tcc_define_symbol(s
, "__x86_64__", NULL
);
833 #elif defined(TCC_TARGET_ARM)
834 tcc_define_symbol(s
, "__ARM_ARCH_4__", NULL
);
835 tcc_define_symbol(s
, "__arm_elf__", NULL
);
836 tcc_define_symbol(s
, "__arm_elf", NULL
);
837 tcc_define_symbol(s
, "arm_elf", NULL
);
838 tcc_define_symbol(s
, "__arm__", NULL
);
839 tcc_define_symbol(s
, "__arm", NULL
);
840 tcc_define_symbol(s
, "arm", NULL
);
841 tcc_define_symbol(s
, "__APCS_32__", NULL
);
842 tcc_define_symbol(s
, "__ARMEL__", NULL
);
843 #if defined(TCC_ARM_EABI)
844 tcc_define_symbol(s
, "__ARM_EABI__", NULL
);
846 #if defined(TCC_ARM_HARDFLOAT)
847 s
->float_abi
= ARM_HARD_FLOAT
;
848 tcc_define_symbol(s
, "__ARM_PCS_VFP", NULL
);
850 s
->float_abi
= ARM_SOFTFP_FLOAT
;
852 #elif defined(TCC_TARGET_ARM64)
853 tcc_define_symbol(s
, "__aarch64__", NULL
);
854 #elif defined TCC_TARGET_C67
855 tcc_define_symbol(s
, "__C67__", NULL
);
856 #elif defined TCC_TARGET_RISCV64
857 tcc_define_symbol(s
, "__riscv", NULL
);
858 tcc_define_symbol(s
, "__riscv_xlen", "64");
859 tcc_define_symbol(s
, "__riscv_flen", "64");
860 tcc_define_symbol(s
, "__riscv_div", NULL
);
861 tcc_define_symbol(s
, "__riscv_mul", NULL
);
862 tcc_define_symbol(s
, "__riscv_fdiv", NULL
);
863 tcc_define_symbol(s
, "__riscv_fsqrt", NULL
);
864 tcc_define_symbol(s
, "__riscv_float_abi_double", NULL
);
868 tcc_define_symbol(s
, "_WIN32", NULL
);
869 tcc_define_symbol(s
, "__declspec(x)", "__attribute__((x))");
870 tcc_define_symbol(s
, "__cdecl", "");
871 # ifdef TCC_TARGET_X86_64
872 tcc_define_symbol(s
, "_WIN64", NULL
);
875 tcc_define_symbol(s
, "__unix__", NULL
);
876 tcc_define_symbol(s
, "__unix", NULL
);
877 tcc_define_symbol(s
, "unix", NULL
);
878 # if defined(__linux__)
879 tcc_define_symbol(s
, "__linux__", NULL
);
880 tcc_define_symbol(s
, "__linux", NULL
);
882 # if defined(__FreeBSD__)
883 tcc_define_symbol(s
, "__FreeBSD__", "__FreeBSD__");
884 /* No 'Thread Storage Local' on FreeBSD with tcc */
885 tcc_define_symbol(s
, "__NO_TLS", NULL
);
887 # if defined(__FreeBSD_kernel__)
888 tcc_define_symbol(s
, "__FreeBSD_kernel__", NULL
);
890 # if defined(__NetBSD__)
891 tcc_define_symbol(s
, "__NetBSD__", "__NetBSD__");
893 # if defined(__OpenBSD__)
894 tcc_define_symbol(s
, "__OpenBSD__", "__OpenBSD__");
898 /* TinyCC & gcc defines */
901 tcc_define_symbol(s
, "__SIZE_TYPE__", "unsigned int");
902 tcc_define_symbol(s
, "__PTRDIFF_TYPE__", "int");
903 tcc_define_symbol(s
, "__ILP32__", NULL
);
906 tcc_define_symbol(s
, "__SIZE_TYPE__", "unsigned long long");
907 tcc_define_symbol(s
, "__PTRDIFF_TYPE__", "long long");
908 tcc_define_symbol(s
, "__LLP64__", NULL
);
910 /* Other 64bit systems. */
911 tcc_define_symbol(s
, "__SIZE_TYPE__", "unsigned long");
912 tcc_define_symbol(s
, "__PTRDIFF_TYPE__", "long");
913 tcc_define_symbol(s
, "__LP64__", NULL
);
915 tcc_define_symbol(s
, "__SIZEOF_POINTER__", PTR_SIZE
== 4 ? "4" : "8");
918 tcc_define_symbol(s
, "__WCHAR_TYPE__", "unsigned short");
919 tcc_define_symbol(s
, "__WINT_TYPE__", "unsigned short");
921 tcc_define_symbol(s
, "__WCHAR_TYPE__", "int");
922 /* wint_t is unsigned int by default, but (signed) int on BSDs
923 and unsigned short on windows. Other OSes might have still
924 other conventions, sigh. */
925 # if defined(__FreeBSD__) || defined (__FreeBSD_kernel__) \
926 || defined(__NetBSD__) || defined(__OpenBSD__)
927 tcc_define_symbol(s
, "__WINT_TYPE__", "int");
929 /* define __GNUC__ to have some useful stuff from sys/cdefs.h
930 that are unconditionally used in FreeBSDs other system headers :/ */
931 tcc_define_symbol(s
, "__GNUC__", "2");
932 tcc_define_symbol(s
, "__GNUC_MINOR__", "7");
933 tcc_define_symbol(s
, "__builtin_alloca", "alloca");
936 tcc_define_symbol(s
, "__WINT_TYPE__", "unsigned int");
938 tcc_define_symbol(s
, "__REDIRECT(name, proto, alias)",
939 "name proto __asm__ (#alias)");
940 tcc_define_symbol(s
, "__REDIRECT_NTH(name, proto, alias)",
941 "name proto __asm__ (#alias) __THROW");
943 /* Some GCC builtins that are simple to express as macros. */
944 tcc_define_symbol(s
, "__builtin_extract_return_addr(x)", "x");
945 #endif /* ndef TCC_TARGET_PE */
946 #ifdef TCC_TARGET_MACHO
947 /* emulate APPLE-GCC to make libc's headerfiles compile: */
948 tcc_define_symbol(s
, "__APPLE__", "1");
949 tcc_define_symbol(s
, "__GNUC__", "4"); /* darwin emits warning on GCC<4 */
950 tcc_define_symbol(s
, "__APPLE_CC__", "1"); /* for <TargetConditionals.h> */
951 tcc_define_symbol(s
, "_DONT_USE_CTYPE_INLINE_", "1");
952 tcc_define_symbol(s
, "__builtin_alloca", "alloca"); /* as we claim GNUC */
954 tcc_define_symbol(s
, "__builtin_huge_val()", "1e500");
955 tcc_define_symbol(s
, "__builtin_huge_valf()", "1e50f");
956 tcc_define_symbol(s
, "__builtin_huge_vall()", "1e5000L");
957 tcc_define_symbol(s
, "__builtin_nanf(ignored_string)", "__nan()");
958 /* used by _fd_def.h */
959 tcc_define_symbol(s
, "__builtin_bzero(p, ignored_size)", "bzero(p, sizeof(*(p)))");
960 /* used by floats.h to implement FLT_ROUNDS C99 macro. 1 == to nearest */
961 tcc_define_symbol(s
, "__builtin_flt_rounds()", "1");
963 /* avoids usage of GCC/clang specific builtins in libc-headerfiles: */
964 tcc_define_symbol(s
, "__FINITE_MATH_ONLY__", "1");
965 tcc_define_symbol(s
, "_FORTIFY_SOURCE", "0");
966 #endif /* ndef TCC_TARGET_MACHO */
969 tcc_define_symbol(s
, "__SIZEOF_LONG__", "4");
970 tcc_define_symbol(s
, "__LONG_MAX__", "0x7fffffffL");
972 tcc_define_symbol(s
, "__SIZEOF_LONG__", "8");
973 tcc_define_symbol(s
, "__LONG_MAX__", "0x7fffffffffffffffL");
975 tcc_define_symbol(s
, "__SIZEOF_INT__", "4");
976 tcc_define_symbol(s
, "__SIZEOF_LONG_LONG__", "8");
977 tcc_define_symbol(s
, "__CHAR_BIT__", "8");
978 tcc_define_symbol(s
, "__ORDER_LITTLE_ENDIAN__", "1234");
979 tcc_define_symbol(s
, "__ORDER_BIG_ENDIAN__", "4321");
980 tcc_define_symbol(s
, "__BYTE_ORDER__", "__ORDER_LITTLE_ENDIAN__");
981 tcc_define_symbol(s
, "__INT_MAX__", "0x7fffffff");
982 tcc_define_symbol(s
, "__LONG_LONG_MAX__", "0x7fffffffffffffffLL");
983 tcc_define_symbol(s
, "__builtin_offsetof(type,field)", "((__SIZE_TYPE__) &((type *)0)->field)");
987 LIBTCCAPI
void tcc_delete(TCCState
*s1
)
992 /* free library paths */
993 dynarray_reset(&s1
->library_paths
, &s1
->nb_library_paths
);
994 dynarray_reset(&s1
->crt_paths
, &s1
->nb_crt_paths
);
996 /* free include paths */
997 dynarray_reset(&s1
->include_paths
, &s1
->nb_include_paths
);
998 dynarray_reset(&s1
->sysinclude_paths
, &s1
->nb_sysinclude_paths
);
1000 tcc_free(s1
->tcc_lib_path
);
1001 tcc_free(s1
->soname
);
1002 tcc_free(s1
->rpath
);
1003 tcc_free(s1
->init_symbol
);
1004 tcc_free(s1
->fini_symbol
);
1005 tcc_free(s1
->outfile
);
1006 tcc_free(s1
->deps_outfile
);
1007 dynarray_reset(&s1
->files
, &s1
->nb_files
);
1008 dynarray_reset(&s1
->target_deps
, &s1
->nb_target_deps
);
1009 dynarray_reset(&s1
->pragma_libs
, &s1
->nb_pragma_libs
);
1010 dynarray_reset(&s1
->argv
, &s1
->argc
);
1012 cstr_free(&s1
->cmdline_defs
);
1013 cstr_free(&s1
->cmdline_incl
);
1014 #ifdef TCC_IS_NATIVE
1015 /* free runtime memory */
1021 if (0 == --nb_states
)
1026 LIBTCCAPI
int tcc_set_output_type(TCCState
*s
, int output_type
)
1028 s
->output_type
= output_type
;
1030 /* always elf for objects */
1031 if (output_type
== TCC_OUTPUT_OBJ
)
1032 s
->output_format
= TCC_OUTPUT_FORMAT_ELF
;
1034 if (s
->char_is_unsigned
)
1035 tcc_define_symbol(s
, "__CHAR_UNSIGNED__", NULL
);
1037 if (s
->cversion
== 201112) {
1038 tcc_undefine_symbol(s
, "__STDC_VERSION__");
1039 tcc_define_symbol(s
, "__STDC_VERSION__", "201112L");
1040 tcc_define_symbol(s
, "__STDC_NO_ATOMICS__", NULL
);
1041 tcc_define_symbol(s
, "__STDC_NO_COMPLEX__", NULL
);
1042 tcc_define_symbol(s
, "__STDC_NO_THREADS__", NULL
);
1043 #ifndef TCC_TARGET_PE
1044 /* on Linux, this conflicts with a define introduced by
1045 /usr/include/stdc-predef.h included by glibc libs
1046 tcc_define_symbol(s, "__STDC_ISO_10646__", "201605L"); */
1047 tcc_define_symbol(s
, "__STDC_UTF_16__", NULL
);
1048 tcc_define_symbol(s
, "__STDC_UTF_32__", NULL
);
1052 if (s
->optimize
> 0)
1053 tcc_define_symbol(s
, "__OPTIMIZE__", NULL
);
1055 if (s
->option_pthread
)
1056 tcc_define_symbol(s
, "_REENTRANT", NULL
);
1058 if (s
->leading_underscore
)
1059 tcc_define_symbol(s
, "__leading_underscore", NULL
);
1062 /* default include paths */
1063 /* -isystem paths have already been handled */
1064 tcc_add_sysinclude_path(s
, CONFIG_TCC_SYSINCLUDEPATHS
);
1067 #ifdef CONFIG_TCC_BCHECK
1068 if (s
->do_bounds_check
) {
1069 /* if bound checking, then add corresponding sections */
1070 tccelf_bounds_new(s
);
1072 tcc_define_symbol(s
, "__BOUNDS_CHECKING_ON", NULL
);
1076 /* add debug sections */
1080 tcc_add_library_path(s
, CONFIG_TCC_LIBPATHS
);
1082 #ifdef TCC_TARGET_PE
1084 if (!s
->nostdlib
&& output_type
!= TCC_OUTPUT_OBJ
)
1085 tcc_add_systemdir(s
);
1088 /* paths for crt objects */
1089 tcc_split_path(s
, &s
->crt_paths
, &s
->nb_crt_paths
, CONFIG_TCC_CRTPREFIX
);
1090 /* add libc crt1/crti objects */
1091 if ((output_type
== TCC_OUTPUT_EXE
|| output_type
== TCC_OUTPUT_DLL
) &&
1093 #ifndef TCC_TARGET_MACHO
1094 /* Mach-O with LC_MAIN doesn't need any crt startup code. */
1095 if (output_type
!= TCC_OUTPUT_DLL
)
1096 tcc_add_crt(s
, "crt1.o");
1097 tcc_add_crt(s
, "crti.o");
1104 LIBTCCAPI
int tcc_add_include_path(TCCState
*s
, const char *pathname
)
1106 tcc_split_path(s
, &s
->include_paths
, &s
->nb_include_paths
, pathname
);
1110 LIBTCCAPI
int tcc_add_sysinclude_path(TCCState
*s
, const char *pathname
)
1112 tcc_split_path(s
, &s
->sysinclude_paths
, &s
->nb_sysinclude_paths
, pathname
);
1116 ST_FUNC
int tcc_add_file_internal(TCCState
*s1
, const char *filename
, int flags
)
1121 fd
= _tcc_open(s1
, filename
);
1123 if (flags
& AFF_PRINT_ERROR
)
1124 tcc_error_noabort("file '%s' not found", filename
);
1128 s1
->current_filename
= filename
;
1129 if (flags
& AFF_TYPE_BIN
) {
1133 obj_type
= tcc_object_type(fd
, &ehdr
);
1134 lseek(fd
, 0, SEEK_SET
);
1136 #ifdef TCC_TARGET_MACHO
1137 if (0 == obj_type
&& 0 == strcmp(tcc_fileextension(filename
), ".dylib"))
1138 obj_type
= AFF_BINTYPE_DYN
;
1142 case AFF_BINTYPE_REL
:
1143 ret
= tcc_load_object_file(s1
, fd
, 0);
1145 #ifndef TCC_TARGET_PE
1146 case AFF_BINTYPE_DYN
:
1147 if (s1
->output_type
== TCC_OUTPUT_MEMORY
) {
1149 #ifdef TCC_IS_NATIVE
1150 if (NULL
== dlopen(filename
, RTLD_GLOBAL
| RTLD_LAZY
))
1154 #ifndef TCC_TARGET_MACHO
1155 ret
= tcc_load_dll(s1
, fd
, filename
,
1156 (flags
& AFF_REFERENCED_DLL
) != 0);
1158 ret
= macho_load_dll(s1
, fd
, filename
,
1159 (flags
& AFF_REFERENCED_DLL
) != 0);
1164 case AFF_BINTYPE_AR
:
1165 ret
= tcc_load_archive(s1
, fd
, !(flags
& AFF_WHOLE_ARCHIVE
));
1167 #ifdef TCC_TARGET_COFF
1168 case AFF_BINTYPE_C67
:
1169 ret
= tcc_load_coff(s1
, fd
);
1173 #ifdef TCC_TARGET_PE
1174 ret
= pe_load_file(s1
, filename
, fd
);
1175 #elif defined(TCC_TARGET_MACHO)
1178 /* as GNU ld, consider it is an ld script if not recognized */
1179 ret
= tcc_load_ldscript(s1
, fd
);
1182 tcc_error_noabort("%s: unrecognized file type %d", filename
,
1188 /* update target deps */
1189 dynarray_add(&s1
->target_deps
, &s1
->nb_target_deps
, tcc_strdup(filename
));
1190 ret
= tcc_compile(s1
, flags
, filename
, fd
);
1192 s1
->current_filename
= NULL
;
1196 LIBTCCAPI
int tcc_add_file(TCCState
*s
, const char *filename
)
1198 int filetype
= s
->filetype
;
1199 if (0 == (filetype
& AFF_TYPE_MASK
)) {
1200 /* use a file extension to detect a filetype */
1201 const char *ext
= tcc_fileextension(filename
);
1204 if (!strcmp(ext
, "S"))
1205 filetype
= AFF_TYPE_ASMPP
;
1206 else if (!strcmp(ext
, "s"))
1207 filetype
= AFF_TYPE_ASM
;
1208 else if (!PATHCMP(ext
, "c") || !PATHCMP(ext
, "i"))
1209 filetype
= AFF_TYPE_C
;
1211 filetype
|= AFF_TYPE_BIN
;
1213 filetype
= AFF_TYPE_C
;
1216 return tcc_add_file_internal(s
, filename
, filetype
| AFF_PRINT_ERROR
);
1219 LIBTCCAPI
int tcc_add_library_path(TCCState
*s
, const char *pathname
)
1221 tcc_split_path(s
, &s
->library_paths
, &s
->nb_library_paths
, pathname
);
1225 static int tcc_add_library_internal(TCCState
*s
, const char *fmt
,
1226 const char *filename
, int flags
, char **paths
, int nb_paths
)
1231 for(i
= 0; i
< nb_paths
; i
++) {
1232 snprintf(buf
, sizeof(buf
), fmt
, paths
[i
], filename
);
1233 if (tcc_add_file_internal(s
, buf
, flags
| AFF_TYPE_BIN
) == 0)
1239 #ifndef TCC_TARGET_MACHO
1240 /* find and load a dll. Return non zero if not found */
1241 /* XXX: add '-rpath' option support ? */
1242 ST_FUNC
int tcc_add_dll(TCCState
*s
, const char *filename
, int flags
)
1244 return tcc_add_library_internal(s
, "%s/%s", filename
, flags
,
1245 s
->library_paths
, s
->nb_library_paths
);
1249 #if !defined TCC_TARGET_PE && !defined TCC_TARGET_MACHO
1250 ST_FUNC
int tcc_add_crt(TCCState
*s1
, const char *filename
)
1252 if (-1 == tcc_add_library_internal(s1
, "%s/%s",
1253 filename
, 0, s1
->crt_paths
, s1
->nb_crt_paths
))
1254 tcc_error_noabort("file '%s' not found", filename
);
1259 /* the library name is the same as the argument of the '-l' option */
1260 LIBTCCAPI
int tcc_add_library(TCCState
*s
, const char *libraryname
)
1262 #if defined TCC_TARGET_PE
1263 const char *libs
[] = { "%s/%s.def", "%s/lib%s.def", "%s/%s.dll", "%s/lib%s.dll", "%s/lib%s.a", NULL
};
1264 const char **pp
= s
->static_link
? libs
+ 4 : libs
;
1265 #elif defined TCC_TARGET_MACHO
1266 const char *libs
[] = { "%s/lib%s.dylib", "%s/lib%s.a", NULL
};
1267 const char **pp
= s
->static_link
? libs
+ 1 : libs
;
1269 const char *libs
[] = { "%s/lib%s.so", "%s/lib%s.a", NULL
};
1270 const char **pp
= s
->static_link
? libs
+ 1 : libs
;
1272 int flags
= s
->filetype
& AFF_WHOLE_ARCHIVE
;
1274 if (0 == tcc_add_library_internal(s
, *pp
,
1275 libraryname
, flags
, s
->library_paths
, s
->nb_library_paths
))
1282 PUB_FUNC
int tcc_add_library_err(TCCState
*s1
, const char *libname
)
1284 int ret
= tcc_add_library(s1
, libname
);
1286 tcc_error_noabort("library '%s' not found", libname
);
1290 /* handle #pragma comment(lib,) */
1291 ST_FUNC
void tcc_add_pragma_libs(TCCState
*s1
)
1294 for (i
= 0; i
< s1
->nb_pragma_libs
; i
++)
1295 tcc_add_library_err(s1
, s1
->pragma_libs
[i
]);
1298 LIBTCCAPI
int tcc_add_symbol(TCCState
*s1
, const char *name
, const void *val
)
1300 #ifdef TCC_TARGET_PE
1301 /* On x86_64 'val' might not be reachable with a 32bit offset.
1302 So it is handled here as if it were in a DLL. */
1303 pe_putimport(s1
, 0, name
, (uintptr_t)val
);
1306 if (s1
->leading_underscore
) {
1308 pstrcpy(buf
+ 1, sizeof(buf
) - 1, name
);
1311 set_global_sym(s1
, name
, NULL
, (addr_t
)(uintptr_t)val
); /* NULL: SHN_ABS */
1316 LIBTCCAPI
void tcc_set_lib_path(TCCState
*s
, const char *path
)
1318 tcc_free(s
->tcc_lib_path
);
1319 s
->tcc_lib_path
= tcc_strdup(path
);
1322 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
1323 #define FD_INVERT 0x0002 /* invert value before storing */
1325 typedef struct FlagDef
{
1331 static int no_flag(const char **pp
)
1333 const char *p
= *pp
;
1334 if (*p
!= 'n' || *++p
!= 'o' || *++p
!= '-')
1340 ST_FUNC
int set_flag(TCCState
*s
, const FlagDef
*flags
, const char *name
)
1351 for (ret
= -1, p
= flags
; p
->name
; ++p
) {
1353 if (strcmp(r
, p
->name
))
1356 if (0 == (p
->flags
& WD_ALL
))
1360 *((unsigned char *)s
+ p
->offset
) =
1361 p
->flags
& FD_INVERT
? !value
: value
;
1371 static int strstart(const char *val
, const char **str
)
1386 /* Like strstart, but automatically takes into account that ld options can
1388 * - start with double or single dash (e.g. '--soname' or '-soname')
1389 * - arguments can be given as separate or after '=' (e.g. '-Wl,-soname,x.so'
1390 * or '-Wl,-soname=x.so')
1392 * you provide `val` always in 'option[=]' form (no leading -)
1394 static int link_option(const char *str
, const char *val
, const char **ptr
)
1399 /* there should be 1 or 2 dashes */
1405 /* then str & val should match (potentially up to '=') */
1416 while (*q
!= '\0' && *q
!= '=') {
1423 /* '=' near eos means ',' or '=' is ok */
1427 if (*p
!= ',' && *p
!= '=')
1437 static const char *skip_linker_arg(const char **str
)
1439 const char *s1
= *str
;
1440 const char *s2
= strchr(s1
, ',');
1441 *str
= s2
? s2
++ : (s2
= s1
+ strlen(s1
));
1445 static void copy_linker_arg(char **pp
, const char *s
, int sep
)
1451 p
[l
= strlen(p
)] = sep
, ++l
;
1452 skip_linker_arg(&q
);
1453 pstrncpy(l
+ (*pp
= tcc_realloc(p
, q
- s
+ l
+ 1)), s
, q
- s
);
1456 /* set linker options */
1457 static int tcc_set_linker(TCCState
*s
, const char *option
)
1462 const char *p
= NULL
;
1467 if (link_option(option
, "Bsymbolic", &p
)) {
1469 } else if (link_option(option
, "nostdlib", &p
)) {
1471 } else if (link_option(option
, "fini=", &p
)) {
1472 copy_linker_arg(&s
->fini_symbol
, p
, 0);
1474 } else if (link_option(option
, "image-base=", &p
)
1475 || link_option(option
, "Ttext=", &p
)) {
1476 s
->text_addr
= strtoull(p
, &end
, 16);
1477 s
->has_text_addr
= 1;
1478 } else if (link_option(option
, "init=", &p
)) {
1479 copy_linker_arg(&s
->init_symbol
, p
, 0);
1481 } else if (link_option(option
, "oformat=", &p
)) {
1482 #if defined(TCC_TARGET_PE)
1483 if (strstart("pe-", &p
)) {
1485 if (strstart("elf64-", &p
)) {
1487 if (strstart("elf32-", &p
)) {
1489 s
->output_format
= TCC_OUTPUT_FORMAT_ELF
;
1490 } else if (!strcmp(p
, "binary")) {
1491 s
->output_format
= TCC_OUTPUT_FORMAT_BINARY
;
1492 #ifdef TCC_TARGET_COFF
1493 } else if (!strcmp(p
, "coff")) {
1494 s
->output_format
= TCC_OUTPUT_FORMAT_COFF
;
1499 } else if (link_option(option
, "as-needed", &p
)) {
1501 } else if (link_option(option
, "O", &p
)) {
1503 } else if (link_option(option
, "export-all-symbols", &p
)) {
1505 } else if (link_option(option
, "export-dynamic", &p
)) {
1507 } else if (link_option(option
, "rpath=", &p
)) {
1508 copy_linker_arg(&s
->rpath
, p
, ':');
1509 } else if (link_option(option
, "enable-new-dtags", &p
)) {
1510 s
->enable_new_dtags
= 1;
1511 } else if (link_option(option
, "section-alignment=", &p
)) {
1512 s
->section_align
= strtoul(p
, &end
, 16);
1513 } else if (link_option(option
, "soname=", &p
)) {
1514 copy_linker_arg(&s
->soname
, p
, 0);
1515 #ifdef TCC_TARGET_PE
1516 } else if (link_option(option
, "large-address-aware", &p
)) {
1517 s
->pe_characteristics
|= 0x20;
1518 } else if (link_option(option
, "file-alignment=", &p
)) {
1519 s
->pe_file_align
= strtoul(p
, &end
, 16);
1520 } else if (link_option(option
, "stack=", &p
)) {
1521 s
->pe_stack_size
= strtoul(p
, &end
, 10);
1522 } else if (link_option(option
, "subsystem=", &p
)) {
1523 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1524 if (!strcmp(p
, "native")) {
1525 s
->pe_subsystem
= 1;
1526 } else if (!strcmp(p
, "console")) {
1527 s
->pe_subsystem
= 3;
1528 } else if (!strcmp(p
, "gui") || !strcmp(p
, "windows")) {
1529 s
->pe_subsystem
= 2;
1530 } else if (!strcmp(p
, "posix")) {
1531 s
->pe_subsystem
= 7;
1532 } else if (!strcmp(p
, "efiapp")) {
1533 s
->pe_subsystem
= 10;
1534 } else if (!strcmp(p
, "efiboot")) {
1535 s
->pe_subsystem
= 11;
1536 } else if (!strcmp(p
, "efiruntime")) {
1537 s
->pe_subsystem
= 12;
1538 } else if (!strcmp(p
, "efirom")) {
1539 s
->pe_subsystem
= 13;
1540 #elif defined(TCC_TARGET_ARM)
1541 if (!strcmp(p
, "wince")) {
1542 s
->pe_subsystem
= 9;
1547 } else if (ret
= link_option(option
, "?whole-archive", &p
), ret
) {
1549 s
->filetype
|= AFF_WHOLE_ARCHIVE
;
1551 s
->filetype
&= ~AFF_WHOLE_ARCHIVE
;
1556 tcc_error("unsupported linker option '%s'", option
);
1559 if (ignoring
&& s
->warn_unsupported
)
1560 tcc_warning("unsupported linker option '%s'", option
);
1562 option
= skip_linker_arg(&p
);
1567 typedef struct TCCOption
{
1590 TCC_OPTION_dumpversion
,
1599 TCC_OPTION_traditional
,
1604 TCC_OPTION_mfloat_abi
,
1608 TCC_OPTION_iwithprefix
,
1610 TCC_OPTION_nostdinc
,
1611 TCC_OPTION_nostdlib
,
1612 TCC_OPTION_print_search_dirs
,
1613 TCC_OPTION_rdynamic
,
1615 TCC_OPTION_pedantic
,
1629 #define TCC_OPTION_HAS_ARG 0x0001
1630 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
1632 static const TCCOption tcc_options
[] = {
1633 { "h", TCC_OPTION_HELP
, 0 },
1634 { "-help", TCC_OPTION_HELP
, 0 },
1635 { "?", TCC_OPTION_HELP
, 0 },
1636 { "hh", TCC_OPTION_HELP2
, 0 },
1637 { "v", TCC_OPTION_v
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1638 { "-version", TCC_OPTION_v
, 0 }, /* handle as verbose, also prints version*/
1639 { "I", TCC_OPTION_I
, TCC_OPTION_HAS_ARG
},
1640 { "D", TCC_OPTION_D
, TCC_OPTION_HAS_ARG
},
1641 { "U", TCC_OPTION_U
, TCC_OPTION_HAS_ARG
},
1642 { "P", TCC_OPTION_P
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1643 { "L", TCC_OPTION_L
, TCC_OPTION_HAS_ARG
},
1644 { "B", TCC_OPTION_B
, TCC_OPTION_HAS_ARG
},
1645 { "l", TCC_OPTION_l
, TCC_OPTION_HAS_ARG
},
1646 { "bench", TCC_OPTION_bench
, 0 },
1647 #ifdef CONFIG_TCC_BACKTRACE
1648 { "bt", TCC_OPTION_bt
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1650 #ifdef CONFIG_TCC_BCHECK
1651 { "b", TCC_OPTION_b
, 0 },
1653 { "g", TCC_OPTION_g
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1654 { "c", TCC_OPTION_c
, 0 },
1655 { "dumpversion", TCC_OPTION_dumpversion
, 0},
1656 { "d", TCC_OPTION_d
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1657 { "static", TCC_OPTION_static
, 0 },
1658 { "std", TCC_OPTION_std
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1659 { "shared", TCC_OPTION_shared
, 0 },
1660 { "soname", TCC_OPTION_soname
, TCC_OPTION_HAS_ARG
},
1661 { "o", TCC_OPTION_o
, TCC_OPTION_HAS_ARG
},
1662 { "-param", TCC_OPTION_param
, TCC_OPTION_HAS_ARG
},
1663 { "pedantic", TCC_OPTION_pedantic
, 0},
1664 { "pthread", TCC_OPTION_pthread
, 0},
1665 { "run", TCC_OPTION_run
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1666 { "rdynamic", TCC_OPTION_rdynamic
, 0 },
1667 { "r", TCC_OPTION_r
, 0 },
1668 { "s", TCC_OPTION_s
, 0 },
1669 { "traditional", TCC_OPTION_traditional
, 0 },
1670 { "Wl,", TCC_OPTION_Wl
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1671 { "Wp,", TCC_OPTION_Wp
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1672 { "W", TCC_OPTION_W
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1673 { "O", TCC_OPTION_O
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1674 #ifdef TCC_TARGET_ARM
1675 { "mfloat-abi", TCC_OPTION_mfloat_abi
, TCC_OPTION_HAS_ARG
},
1677 { "m", TCC_OPTION_m
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1678 { "f", TCC_OPTION_f
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1679 { "isystem", TCC_OPTION_isystem
, TCC_OPTION_HAS_ARG
},
1680 { "include", TCC_OPTION_include
, TCC_OPTION_HAS_ARG
},
1681 { "nostdinc", TCC_OPTION_nostdinc
, 0 },
1682 { "nostdlib", TCC_OPTION_nostdlib
, 0 },
1683 { "print-search-dirs", TCC_OPTION_print_search_dirs
, 0 },
1684 { "w", TCC_OPTION_w
, 0 },
1685 { "pipe", TCC_OPTION_pipe
, 0},
1686 { "E", TCC_OPTION_E
, 0},
1687 { "MD", TCC_OPTION_MD
, 0},
1688 { "MF", TCC_OPTION_MF
, TCC_OPTION_HAS_ARG
},
1689 { "x", TCC_OPTION_x
, TCC_OPTION_HAS_ARG
},
1690 { "ar", TCC_OPTION_ar
, 0},
1691 #ifdef TCC_TARGET_PE
1692 { "impdef", TCC_OPTION_impdef
, 0},
1694 { "C", TCC_OPTION_C
, 0},
1698 static const FlagDef options_W
[] = {
1700 { offsetof(TCCState
, warn_unsupported
), 0, "unsupported" },
1701 { offsetof(TCCState
, warn_write_strings
), 0, "write-strings" },
1702 { offsetof(TCCState
, warn_error
), 0, "error" },
1703 { offsetof(TCCState
, warn_gcc_compat
), 0, "gcc-compat" },
1704 { offsetof(TCCState
, warn_implicit_function_declaration
), WD_ALL
,
1705 "implicit-function-declaration" },
1709 static const FlagDef options_f
[] = {
1710 { offsetof(TCCState
, char_is_unsigned
), 0, "unsigned-char" },
1711 { offsetof(TCCState
, char_is_unsigned
), FD_INVERT
, "signed-char" },
1712 { offsetof(TCCState
, nocommon
), FD_INVERT
, "common" },
1713 { offsetof(TCCState
, leading_underscore
), 0, "leading-underscore" },
1714 { offsetof(TCCState
, ms_extensions
), 0, "ms-extensions" },
1715 { offsetof(TCCState
, dollars_in_identifiers
), 0, "dollars-in-identifiers" },
1719 static const FlagDef options_m
[] = {
1720 { offsetof(TCCState
, ms_bitfields
), 0, "ms-bitfields" },
1721 #ifdef TCC_TARGET_X86_64
1722 { offsetof(TCCState
, nosse
), FD_INVERT
, "sse" },
1727 static void args_parser_add_file(TCCState
*s
, const char* filename
, int filetype
)
1729 struct filespec
*f
= tcc_malloc(sizeof *f
+ strlen(filename
));
1731 strcpy(f
->name
, filename
);
1732 dynarray_add(&s
->files
, &s
->nb_files
, f
);
1735 static int args_parser_make_argv(const char *r
, int *argc
, char ***argv
)
1740 while (c
= (unsigned char)*r
, c
&& c
<= ' ')
1746 while (c
= (unsigned char)*r
, c
) {
1748 if (c
== '\\' && (*r
== '"' || *r
== '\\')) {
1750 } else if (c
== '"') {
1753 } else if (q
== 0 && c
<= ' ') {
1759 //printf("<%s>\n", str.data), fflush(stdout);
1760 dynarray_add(argv
, argc
, tcc_strdup(str
.data
));
1767 /* read list file */
1768 static void args_parser_listfile(TCCState
*s
,
1769 const char *filename
, int optind
, int *pargc
, char ***pargv
)
1778 fd
= open(filename
, O_RDONLY
| O_BINARY
);
1780 tcc_error("listfile '%s' not found", filename
);
1782 len
= lseek(fd
, 0, SEEK_END
);
1783 p
= tcc_malloc(len
+ 1), p
[len
] = 0;
1784 lseek(fd
, 0, SEEK_SET
), read(fd
, p
, len
), close(fd
);
1786 for (i
= 0; i
< *pargc
; ++i
)
1788 args_parser_make_argv(p
, &argc
, &argv
);
1790 dynarray_add(&argv
, &argc
, tcc_strdup((*pargv
)[i
]));
1793 dynarray_reset(&s
->argv
, &s
->argc
);
1794 *pargc
= s
->argc
= argc
, *pargv
= s
->argv
= argv
;
1797 PUB_FUNC
int tcc_parse_args(TCCState
*s
, int *pargc
, char ***pargv
, int optind
)
1800 const TCCOption
*popt
;
1801 const char *optarg
, *r
;
1802 const char *run
= NULL
;
1804 CString linker_arg
; /* collect -Wl options */
1805 int tool
= 0, arg_start
= 0, noaction
= optind
;
1806 char **argv
= *pargv
;
1809 cstr_new(&linker_arg
);
1811 while (optind
< argc
) {
1813 if (r
[0] == '@' && r
[1] != '\0') {
1814 args_parser_listfile(s
, r
+ 1, optind
, &argc
, &argv
);
1819 if (r
[0] == '-' && r
[1] == 'v' && r
[2] == 0)
1824 if (r
[0] != '-' || r
[1] == '\0') {
1825 if (r
[0] != '@') /* allow "tcc file(s) -run @ args ..." */
1826 args_parser_add_file(s
, r
, s
->filetype
);
1828 tcc_set_options(s
, run
);
1829 arg_start
= optind
- 1;
1835 /* find option in table */
1836 for(popt
= tcc_options
; ; ++popt
) {
1837 const char *p1
= popt
->name
;
1838 const char *r1
= r
+ 1;
1840 tcc_error("invalid option -- '%s'", r
);
1841 if (!strstart(p1
, &r1
))
1844 if (popt
->flags
& TCC_OPTION_HAS_ARG
) {
1845 if (*r1
== '\0' && !(popt
->flags
& TCC_OPTION_NOSEP
)) {
1848 tcc_error("argument to '%s' is missing", r
);
1849 optarg
= argv
[optind
++];
1851 } else if (*r1
!= '\0')
1856 switch(popt
->index
) {
1857 case TCC_OPTION_HELP
:
1860 case TCC_OPTION_HELP2
:
1864 tcc_add_include_path(s
, optarg
);
1867 tcc_define_symbol(s
, optarg
, NULL
);
1870 tcc_undefine_symbol(s
, optarg
);
1873 tcc_add_library_path(s
, optarg
);
1876 /* set tcc utilities path (mainly for tcc development) */
1877 tcc_set_lib_path(s
, optarg
);
1880 args_parser_add_file(s
, optarg
, AFF_TYPE_LIB
| (s
->filetype
& ~AFF_TYPE_MASK
));
1883 case TCC_OPTION_pthread
:
1884 s
->option_pthread
= 1;
1886 case TCC_OPTION_bench
:
1889 #ifdef CONFIG_TCC_BACKTRACE
1891 s
->rt_num_callers
= atoi(optarg
);
1892 s
->do_backtrace
= 1;
1896 #ifdef CONFIG_TCC_BCHECK
1898 s
->do_bounds_check
= 1;
1899 s
->do_backtrace
= 1;
1910 tcc_warning("-%s: overriding compiler action already specified", popt
->name
);
1916 else if (*optarg
== 'M')
1918 else if (*optarg
== 't')
1920 else if (isnum(*optarg
))
1921 s
->g_debug
|= atoi(optarg
);
1923 goto unsupported_option
;
1925 case TCC_OPTION_static
:
1928 case TCC_OPTION_std
:
1929 if (strcmp(optarg
, "=c11") == 0)
1930 s
->cversion
= 201112;
1932 case TCC_OPTION_shared
:
1934 goto set_output_type
;
1935 case TCC_OPTION_soname
:
1936 s
->soname
= tcc_strdup(optarg
);
1940 tcc_warning("multiple -o option");
1941 tcc_free(s
->outfile
);
1943 s
->outfile
= tcc_strdup(optarg
);
1946 /* generate a .o merging several output files */
1949 goto set_output_type
;
1950 case TCC_OPTION_isystem
:
1951 tcc_add_sysinclude_path(s
, optarg
);
1953 case TCC_OPTION_include
:
1954 cstr_printf(&s
->cmdline_incl
, "#include \"%s\"\n", optarg
);
1956 case TCC_OPTION_nostdinc
:
1959 case TCC_OPTION_nostdlib
:
1962 case TCC_OPTION_run
:
1963 #ifndef TCC_IS_NATIVE
1964 tcc_error("-run is not available in a cross compiler");
1967 x
= TCC_OUTPUT_MEMORY
;
1968 goto set_output_type
;
1970 do ++s
->verbose
; while (*optarg
++ == 'v');
1974 if (set_flag(s
, options_f
, optarg
) < 0)
1975 goto unsupported_option
;
1977 #ifdef TCC_TARGET_ARM
1978 case TCC_OPTION_mfloat_abi
:
1979 /* tcc doesn't support soft float yet */
1980 if (!strcmp(optarg
, "softfp")) {
1981 s
->float_abi
= ARM_SOFTFP_FLOAT
;
1982 tcc_undefine_symbol(s
, "__ARM_PCS_VFP");
1983 } else if (!strcmp(optarg
, "hard"))
1984 s
->float_abi
= ARM_HARD_FLOAT
;
1986 tcc_error("unsupported float abi '%s'", optarg
);
1990 if (set_flag(s
, options_m
, optarg
) < 0) {
1991 if (x
= atoi(optarg
), x
!= 32 && x
!= 64)
1992 goto unsupported_option
;
1993 if (PTR_SIZE
!= x
/8)
2000 if (optarg
[0] && set_flag(s
, options_W
, optarg
) < 0)
2001 goto unsupported_option
;
2006 case TCC_OPTION_rdynamic
:
2010 if (linker_arg
.size
)
2011 --linker_arg
.size
, cstr_ccat(&linker_arg
, ',');
2012 cstr_cat(&linker_arg
, optarg
, 0);
2013 if (tcc_set_linker(s
, linker_arg
.data
))
2014 cstr_free(&linker_arg
);
2020 x
= TCC_OUTPUT_PREPROCESS
;
2021 goto set_output_type
;
2023 s
->Pflag
= atoi(optarg
) + 1;
2029 s
->deps_outfile
= tcc_strdup(optarg
);
2031 case TCC_OPTION_dumpversion
:
2032 printf ("%s\n", TCC_VERSION
);
2039 else if (*optarg
== 'a')
2041 else if (*optarg
== 'b')
2043 else if (*optarg
== 'n')
2046 tcc_warning("unsupported language '%s'", optarg
);
2047 s
->filetype
= x
| (s
->filetype
& ~AFF_TYPE_MASK
);
2050 s
->optimize
= atoi(optarg
);
2052 case TCC_OPTION_print_search_dirs
:
2055 case TCC_OPTION_impdef
:
2061 arg_start
= optind
- 1;
2062 if (arg_start
!= noaction
)
2063 tcc_error("cannot parse %s here", r
);
2066 case TCC_OPTION_traditional
:
2067 case TCC_OPTION_pedantic
:
2068 case TCC_OPTION_pipe
:
2075 if (s
->warn_unsupported
)
2076 tcc_warning("unsupported option '%s'", r
);
2080 if (linker_arg
.size
) {
2081 r
= linker_arg
.data
;
2084 *pargc
= argc
- arg_start
;
2085 *pargv
= argv
+ arg_start
;
2088 if (optind
!= noaction
)
2090 if (s
->verbose
== 2)
2091 return OPT_PRINT_DIRS
;
2097 LIBTCCAPI
void tcc_set_options(TCCState
*s
, const char *r
)
2101 args_parser_make_argv(r
, &argc
, &argv
);
2102 tcc_parse_args(s
, &argc
, &argv
, 0);
2103 dynarray_reset(&argv
, &argc
);
2106 PUB_FUNC
void tcc_print_stats(TCCState
*s1
, unsigned total_time
)
2110 if (total_bytes
< 1)
2112 fprintf(stderr
, "* %d idents, %d lines, %d bytes\n"
2113 "* %0.3f s, %u lines/s, %0.1f MB/s\n",
2114 total_idents
, total_lines
, total_bytes
,
2115 (double)total_time
/1000,
2116 (unsigned)total_lines
*1000/total_time
,
2117 (double)total_bytes
/1000/total_time
);
2119 fprintf(stderr
, "* %d bytes memory used\n", mem_max_size
);