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"
37 #elif defined(TCC_TARGET_C67)
41 #elif defined(TCC_TARGET_X86_64)
42 #include "x86_64-gen.c"
43 #include "x86_64-link.c"
45 #elif defined(TCC_TARGET_RISCV64)
46 #include "riscv64-gen.c"
47 #include "riscv64-link.c"
57 #endif /* ONE_SOURCE */
61 /********************************************************/
62 /* global variables */
64 /* XXX: get rid of this ASAP (or maybe not) */
65 ST_DATA
struct TCCState
*tcc_state
;
71 /********************************************************/
73 ST_FUNC
char *normalize_slashes(char *path
)
76 for (p
= path
; *p
; ++p
)
82 static HMODULE tcc_module
;
84 /* on win32, we suppose the lib and includes are at the location of 'tcc.exe' */
85 static void tcc_set_lib_path_w32(TCCState
*s
)
88 GetModuleFileNameA(tcc_module
, path
, sizeof path
);
89 p
= tcc_basename(normalize_slashes(strlwr(path
)));
93 tcc_set_lib_path(s
, path
);
97 static void tcc_add_systemdir(TCCState
*s
)
100 GetSystemDirectory(buf
, sizeof buf
);
101 tcc_add_library_path(s
, normalize_slashes(buf
));
106 BOOL WINAPI
DllMain (HINSTANCE hDll
, DWORD dwReason
, LPVOID lpReserved
)
108 if (DLL_PROCESS_ATTACH
== dwReason
)
115 /********************************************************/
116 #ifndef CONFIG_TCC_SEMLOCK
120 static int tcc_sem_init
;
121 static CRITICAL_SECTION tcc_cr
;
122 static void wait_sem(void)
125 InitializeCriticalSection(&tcc_cr
), tcc_sem_init
= 1;
126 EnterCriticalSection(&tcc_cr
);
128 #define WAIT_SEM() wait_sem()
129 #define POST_SEM() LeaveCriticalSection(&tcc_cr);
131 #include <semaphore.h>
132 static int tcc_sem_init
;
133 static sem_t tcc_sem
;
134 static void wait_sem(void)
137 sem_init(&tcc_sem
, 0, 1), tcc_sem_init
= 1;
138 while (sem_wait (&tcc_sem
) < 0 && errno
== EINTR
);
140 #define WAIT_SEM() wait_sem()
141 #define POST_SEM() sem_post(&tcc_sem)
144 /********************************************************/
145 /* copy a string and truncate it. */
146 ST_FUNC
char *pstrcpy(char *buf
, size_t buf_size
, const char *s
)
153 q_end
= buf
+ buf_size
- 1;
165 /* strcat and truncate. */
166 ST_FUNC
char *pstrcat(char *buf
, size_t buf_size
, const char *s
)
171 pstrcpy(buf
+ len
, buf_size
- len
, s
);
175 ST_FUNC
char *pstrncpy(char *out
, const char *in
, size_t num
)
177 memcpy(out
, in
, num
);
182 /* extract the basename of a file */
183 PUB_FUNC
char *tcc_basename(const char *name
)
185 char *p
= strchr(name
, 0);
186 while (p
> name
&& !IS_DIRSEP(p
[-1]))
191 /* extract extension part of a file
193 * (if no extension, return pointer to end-of-string)
195 PUB_FUNC
char *tcc_fileextension (const char *name
)
197 char *b
= tcc_basename(name
);
198 char *e
= strrchr(b
, '.');
199 return e
? e
: strchr(b
, 0);
202 /********************************************************/
203 /* memory management */
211 PUB_FUNC
void tcc_free(void *ptr
)
216 PUB_FUNC
void *tcc_malloc(unsigned long size
)
221 _tcc_error("memory full (malloc)");
225 PUB_FUNC
void *tcc_mallocz(unsigned long size
)
228 ptr
= tcc_malloc(size
);
229 memset(ptr
, 0, size
);
233 PUB_FUNC
void *tcc_realloc(void *ptr
, unsigned long size
)
236 ptr1
= realloc(ptr
, size
);
238 _tcc_error("memory full (realloc)");
242 PUB_FUNC
char *tcc_strdup(const char *str
)
245 ptr
= tcc_malloc(strlen(str
) + 1);
252 #define MEM_DEBUG_MAGIC1 0xFEEDDEB1
253 #define MEM_DEBUG_MAGIC2 0xFEEDDEB2
254 #define MEM_DEBUG_MAGIC3 0xFEEDDEB3
255 #define MEM_DEBUG_FILE_LEN 40
256 #define MEM_DEBUG_CHECK3(header) \
257 ((mem_debug_header_t*)((char*)header + header->size))->magic3
258 #define MEM_USER_PTR(header) \
259 ((char *)header + offsetof(mem_debug_header_t, magic3))
260 #define MEM_HEADER_PTR(ptr) \
261 (mem_debug_header_t *)((char*)ptr - offsetof(mem_debug_header_t, magic3))
263 struct mem_debug_header
{
266 struct mem_debug_header
*prev
;
267 struct mem_debug_header
*next
;
269 char file_name
[MEM_DEBUG_FILE_LEN
+ 1];
271 ALIGNED(16) unsigned magic3
;
274 typedef struct mem_debug_header mem_debug_header_t
;
276 static mem_debug_header_t
*mem_debug_chain
;
277 static unsigned mem_cur_size
;
278 static unsigned mem_max_size
;
280 static mem_debug_header_t
*malloc_check(void *ptr
, const char *msg
)
282 mem_debug_header_t
* header
= MEM_HEADER_PTR(ptr
);
283 if (header
->magic1
!= MEM_DEBUG_MAGIC1
||
284 header
->magic2
!= MEM_DEBUG_MAGIC2
||
285 MEM_DEBUG_CHECK3(header
) != MEM_DEBUG_MAGIC3
||
286 header
->size
== (unsigned)-1) {
287 fprintf(stderr
, "%s check failed\n", msg
);
288 if (header
->magic1
== MEM_DEBUG_MAGIC1
)
289 fprintf(stderr
, "%s:%u: block allocated here.\n",
290 header
->file_name
, header
->line_num
);
296 PUB_FUNC
void *tcc_malloc_debug(unsigned long size
, const char *file
, int line
)
299 mem_debug_header_t
*header
;
301 header
= malloc(sizeof(mem_debug_header_t
) + size
);
303 _tcc_error("memory full (malloc)");
305 header
->magic1
= MEM_DEBUG_MAGIC1
;
306 header
->magic2
= MEM_DEBUG_MAGIC2
;
308 MEM_DEBUG_CHECK3(header
) = MEM_DEBUG_MAGIC3
;
309 header
->line_num
= line
;
310 ofs
= strlen(file
) - MEM_DEBUG_FILE_LEN
;
311 strncpy(header
->file_name
, file
+ (ofs
> 0 ? ofs
: 0), MEM_DEBUG_FILE_LEN
);
312 header
->file_name
[MEM_DEBUG_FILE_LEN
] = 0;
314 header
->next
= mem_debug_chain
;
317 header
->next
->prev
= header
;
318 mem_debug_chain
= header
;
320 mem_cur_size
+= size
;
321 if (mem_cur_size
> mem_max_size
)
322 mem_max_size
= mem_cur_size
;
324 return MEM_USER_PTR(header
);
327 PUB_FUNC
void tcc_free_debug(void *ptr
)
329 mem_debug_header_t
*header
;
332 header
= malloc_check(ptr
, "tcc_free");
333 mem_cur_size
-= header
->size
;
334 header
->size
= (unsigned)-1;
336 header
->next
->prev
= header
->prev
;
338 header
->prev
->next
= header
->next
;
339 if (header
== mem_debug_chain
)
340 mem_debug_chain
= header
->next
;
344 PUB_FUNC
void *tcc_mallocz_debug(unsigned long size
, const char *file
, int line
)
347 ptr
= tcc_malloc_debug(size
,file
,line
);
348 memset(ptr
, 0, size
);
352 PUB_FUNC
void *tcc_realloc_debug(void *ptr
, unsigned long size
, const char *file
, int line
)
354 mem_debug_header_t
*header
;
355 int mem_debug_chain_update
= 0;
357 return tcc_malloc_debug(size
, file
, line
);
358 header
= malloc_check(ptr
, "tcc_realloc");
359 mem_cur_size
-= header
->size
;
360 mem_debug_chain_update
= (header
== mem_debug_chain
);
361 header
= realloc(header
, sizeof(mem_debug_header_t
) + size
);
363 _tcc_error("memory full (realloc)");
365 MEM_DEBUG_CHECK3(header
) = MEM_DEBUG_MAGIC3
;
367 header
->next
->prev
= header
;
369 header
->prev
->next
= header
;
370 if (mem_debug_chain_update
)
371 mem_debug_chain
= header
;
372 mem_cur_size
+= size
;
373 if (mem_cur_size
> mem_max_size
)
374 mem_max_size
= mem_cur_size
;
375 return MEM_USER_PTR(header
);
378 PUB_FUNC
char *tcc_strdup_debug(const char *str
, const char *file
, int line
)
381 ptr
= tcc_malloc_debug(strlen(str
) + 1, file
, line
);
386 PUB_FUNC
void tcc_memcheck(void)
389 mem_debug_header_t
*header
= mem_debug_chain
;
390 fprintf(stderr
, "MEM_DEBUG: mem_leak= %d bytes, mem_max_size= %d bytes\n",
391 mem_cur_size
, mem_max_size
);
393 fprintf(stderr
, "%s:%u: error: %u bytes leaked\n",
394 header
->file_name
, header
->line_num
, header
->size
);
395 header
= header
->next
;
402 #endif /* MEM_DEBUG */
404 #define free(p) use_tcc_free(p)
405 #define malloc(s) use_tcc_malloc(s)
406 #define realloc(p, s) use_tcc_realloc(p, s)
408 /********************************************************/
411 ST_FUNC
void dynarray_add(void *ptab
, int *nb_ptr
, void *data
)
417 pp
= *(void ***)ptab
;
418 /* every power of two we double array size */
419 if ((nb
& (nb
- 1)) == 0) {
424 pp
= tcc_realloc(pp
, nb_alloc
* sizeof(void *));
431 ST_FUNC
void dynarray_reset(void *pp
, int *n
)
434 for (p
= *(void***)pp
; *n
; ++p
, --*n
)
437 tcc_free(*(void**)pp
);
441 static void tcc_split_path(TCCState
*s
, void *p_ary
, int *p_nb_ary
, const char *in
)
449 for (p
= in
; c
= *p
, c
!= '\0' && c
!= PATHSEP
[0]; ++p
) {
450 if (c
== '{' && p
[1] && p
[2] == '}') {
453 cstr_cat(&str
, s
->tcc_lib_path
, -1);
454 if (c
== 'f' && file
) {
455 /* substitute current file's dir */
456 const char *f
= file
->true_filename
;
457 const char *b
= tcc_basename(f
);
459 cstr_cat(&str
, f
, b
- f
- 1);
461 cstr_cat(&str
, ".", 1);
468 cstr_ccat(&str
, '\0');
469 dynarray_add(p_ary
, p_nb_ary
, tcc_strdup(str
.data
));
476 /********************************************************/
478 static void strcat_vprintf(char *buf
, int buf_size
, const char *fmt
, va_list ap
)
482 vsnprintf(buf
+ len
, buf_size
- len
, fmt
, ap
);
485 static void strcat_printf(char *buf
, int buf_size
, const char *fmt
, ...)
489 strcat_vprintf(buf
, buf_size
, fmt
, ap
);
494 #define ERROR_NOABORT 1
495 #define ERROR_ERROR 2
497 PUB_FUNC
void tcc_enter_state(TCCState
*s1
)
503 static void error1(int mode
, const char *fmt
, va_list ap
)
506 BufferedFile
**pf
, *f
;
507 TCCState
*s1
= tcc_state
;
509 /* 's1->error_set_jmp_enabled' means that we're called from
510 within the parser/generator and 'tcc_state' was already
511 set (i.e. not by the function above).
513 Otherwise, 's1 = NULL' means we're called because of severe
514 problems from tcc_malloc() which under normal conditions
515 should never happen. */
517 if (s1
&& !s1
->error_set_jmp_enabled
) {
522 if (mode
== ERROR_WARN
) {
530 /* use upper file if inline ":asm:" or token ":paste:" */
531 for (f
= file
; f
&& f
->filename
[0] == ':'; f
= f
->prev
)
534 for(pf
= s1
->include_stack
; pf
< s1
->include_stack_ptr
; pf
++)
535 strcat_printf(buf
, sizeof(buf
), "In file included from %s:%d:\n",
536 (*pf
)->filename
, (*pf
)->line_num
);
537 if (s1
->error_set_jmp_enabled
) {
538 strcat_printf(buf
, sizeof(buf
), "%s:%d: ",
539 f
->filename
, f
->line_num
- !!(tok_flags
& TOK_FLAG_BOL
));
541 strcat_printf(buf
, sizeof(buf
), "%s: ",
545 strcat_printf(buf
, sizeof(buf
), "tcc: ");
547 if (mode
== ERROR_WARN
)
548 strcat_printf(buf
, sizeof(buf
), "warning: ");
550 strcat_printf(buf
, sizeof(buf
), "error: ");
551 strcat_vprintf(buf
, sizeof(buf
), fmt
, ap
);
552 if (!s1
|| !s1
->error_func
) {
553 /* default case: stderr */
554 if (s1
&& s1
->output_type
== TCC_OUTPUT_PREPROCESS
&& s1
->ppfp
== stdout
)
555 /* print a newline during tcc -E */
556 printf("\n"), fflush(stdout
);
557 fflush(stdout
); /* flush -v output */
558 fprintf(stderr
, "%s\n", buf
);
559 fflush(stderr
); /* print error/warning now (win32) */
561 s1
->error_func(s1
->error_opaque
, buf
);
564 if (mode
!= ERROR_WARN
)
566 if (mode
!= ERROR_ERROR
)
568 if (s1
->error_set_jmp_enabled
)
569 longjmp(s1
->error_jmp_buf
, 1);
574 LIBTCCAPI
void tcc_set_error_func(TCCState
*s
, void *error_opaque
, TCCErrorFunc error_func
)
576 s
->error_opaque
= error_opaque
;
577 s
->error_func
= error_func
;
580 LIBTCCAPI TCCErrorFunc
tcc_get_error_func(TCCState
*s
)
582 return s
->error_func
;
585 LIBTCCAPI
void *tcc_get_error_opaque(TCCState
*s
)
587 return s
->error_opaque
;
590 /* error without aborting current compilation */
591 PUB_FUNC
void _tcc_error_noabort(const char *fmt
, ...)
595 error1(ERROR_NOABORT
, fmt
, ap
);
599 PUB_FUNC
void _tcc_error(const char *fmt
, ...)
603 for (;;) error1(ERROR_ERROR
, fmt
, ap
);
606 PUB_FUNC
void _tcc_warning(const char *fmt
, ...)
610 error1(ERROR_WARN
, fmt
, ap
);
614 /********************************************************/
617 ST_FUNC
void tcc_open_bf(TCCState
*s1
, const char *filename
, int initlen
)
620 int buflen
= initlen
? initlen
: IO_BUF_SIZE
;
622 bf
= tcc_mallocz(sizeof(BufferedFile
) + buflen
);
623 bf
->buf_ptr
= bf
->buffer
;
624 bf
->buf_end
= bf
->buffer
+ initlen
;
625 bf
->buf_end
[0] = CH_EOB
; /* put eob symbol */
626 pstrcpy(bf
->filename
, sizeof(bf
->filename
), filename
);
628 normalize_slashes(bf
->filename
);
630 bf
->true_filename
= bf
->filename
;
632 bf
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
636 tok_flags
= TOK_FLAG_BOL
| TOK_FLAG_BOF
;
639 ST_FUNC
void tcc_close(void)
641 TCCState
*s1
= tcc_state
;
642 BufferedFile
*bf
= file
;
645 total_lines
+= bf
->line_num
;
647 if (bf
->true_filename
!= bf
->filename
)
648 tcc_free(bf
->true_filename
);
653 static int _tcc_open(TCCState
*s1
, const char *filename
)
656 if (strcmp(filename
, "-") == 0)
657 fd
= 0, filename
= "<stdin>";
659 fd
= open(filename
, O_RDONLY
| O_BINARY
);
660 if ((s1
->verbose
== 2 && fd
>= 0) || s1
->verbose
== 3)
661 printf("%s %*s%s\n", fd
< 0 ? "nf":"->",
662 (int)(s1
->include_stack_ptr
- s1
->include_stack
), "", filename
);
666 ST_FUNC
int tcc_open(TCCState
*s1
, const char *filename
)
668 int fd
= _tcc_open(s1
, filename
);
671 tcc_open_bf(s1
, filename
, 0);
676 /* compile the file opened in 'file'. Return non zero if errors. */
677 static int tcc_compile(TCCState
*s1
, int filetype
, const char *str
, int fd
)
679 /* Here we enter the code section where we use the global variables for
680 parsing and code generation (tccpp.c, tccgen.c, <target>-gen.c).
681 Other threads need to wait until we're done.
683 Alternatively we could use thread local storage for those global
684 variables, which may or may not have advantages */
689 if (setjmp(s1
->error_jmp_buf
) == 0) {
691 s1
->error_set_jmp_enabled
= 1;
695 int len
= strlen(str
);
696 tcc_open_bf(s1
, "<string>", len
);
697 memcpy(file
->buffer
, str
, len
);
699 tcc_open_bf(s1
, str
, 0);
703 is_asm
= !!(filetype
& (AFF_TYPE_ASM
|AFF_TYPE_ASMPP
));
704 tccelf_begin_file(s1
);
705 preprocess_start(s1
, is_asm
);
707 if (s1
->output_type
== TCC_OUTPUT_PREPROCESS
) {
710 #ifdef CONFIG_TCC_ASM
711 tcc_assemble(s1
, !!(filetype
& AFF_TYPE_ASMPP
));
713 tcc_error_noabort("asm not supported");
719 s1
->error_set_jmp_enabled
= 0;
726 return s1
->nb_errors
!= 0 ? -1 : 0;
729 LIBTCCAPI
int tcc_compile_string(TCCState
*s
, const char *str
)
731 return tcc_compile(s
, s
->filetype
, str
, -1);
734 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
735 LIBTCCAPI
void tcc_define_symbol(TCCState
*s1
, const char *sym
, const char *value
)
739 cstr_printf(&s1
->cmdline_defs
, "#define %s %s\n", sym
, value
);
742 /* undefine a preprocessor symbol */
743 LIBTCCAPI
void tcc_undefine_symbol(TCCState
*s1
, const char *sym
)
745 cstr_printf(&s1
->cmdline_defs
, "#undef %s\n", sym
);
749 LIBTCCAPI TCCState
*tcc_new(void)
753 s
= tcc_mallocz(sizeof(TCCState
));
765 s
->dollars_in_identifiers
= 1; /*on by default like in gcc/clang*/
766 s
->cversion
= 199901; /* default unless -std=c11 is supplied */
767 s
->warn_implicit_function_declaration
= 1;
768 s
->ms_extensions
= 1;
770 #ifdef CHAR_IS_UNSIGNED
771 s
->char_is_unsigned
= 1;
773 #ifdef TCC_TARGET_I386
776 /* enable this if you want symbols with leading underscore on windows: */
777 #if 0 /* def TCC_TARGET_PE */
778 s
->leading_underscore
= 1;
781 /* might be used in error() before preprocess_start() */
782 s
->include_stack_ptr
= s
->include_stack
;
787 tcc_set_lib_path_w32(s
);
789 tcc_set_lib_path(s
, CONFIG_TCCDIR
);
793 /* define __TINYC__ 92X */
794 char buffer
[32]; int a
,b
,c
;
795 sscanf(TCC_VERSION
, "%d.%d.%d", &a
, &b
, &c
);
796 sprintf(buffer
, "%d", a
*10000 + b
*100 + c
);
797 tcc_define_symbol(s
, "__TINYC__", buffer
);
800 /* standard defines */
801 tcc_define_symbol(s
, "__STDC__", NULL
);
802 tcc_define_symbol(s
, "__STDC_VERSION__", "199901L");
803 tcc_define_symbol(s
, "__STDC_HOSTED__", NULL
);
806 #if defined(TCC_TARGET_I386)
807 tcc_define_symbol(s
, "__i386__", NULL
);
808 tcc_define_symbol(s
, "__i386", NULL
);
809 tcc_define_symbol(s
, "i386", NULL
);
810 #elif defined(TCC_TARGET_X86_64)
811 tcc_define_symbol(s
, "__x86_64__", NULL
);
812 #elif defined(TCC_TARGET_ARM)
813 tcc_define_symbol(s
, "__ARM_ARCH_4__", NULL
);
814 tcc_define_symbol(s
, "__arm_elf__", NULL
);
815 tcc_define_symbol(s
, "__arm_elf", NULL
);
816 tcc_define_symbol(s
, "arm_elf", NULL
);
817 tcc_define_symbol(s
, "__arm__", NULL
);
818 tcc_define_symbol(s
, "__arm", NULL
);
819 tcc_define_symbol(s
, "arm", NULL
);
820 tcc_define_symbol(s
, "__APCS_32__", NULL
);
821 tcc_define_symbol(s
, "__ARMEL__", NULL
);
822 #if defined(TCC_ARM_EABI)
823 tcc_define_symbol(s
, "__ARM_EABI__", NULL
);
825 #if defined(TCC_ARM_HARDFLOAT)
826 s
->float_abi
= ARM_HARD_FLOAT
;
827 tcc_define_symbol(s
, "__ARM_PCS_VFP", NULL
);
829 s
->float_abi
= ARM_SOFTFP_FLOAT
;
831 #elif defined(TCC_TARGET_ARM64)
832 tcc_define_symbol(s
, "__aarch64__", NULL
);
833 #elif defined TCC_TARGET_C67
834 tcc_define_symbol(s
, "__C67__", NULL
);
835 #elif defined TCC_TARGET_RISCV64
836 tcc_define_symbol(s
, "__riscv", NULL
);
837 tcc_define_symbol(s
, "__riscv_xlen", "64");
838 tcc_define_symbol(s
, "__riscv_flen", "64");
839 tcc_define_symbol(s
, "__riscv_div", NULL
);
840 tcc_define_symbol(s
, "__riscv_mul", NULL
);
841 tcc_define_symbol(s
, "__riscv_fdiv", NULL
);
842 tcc_define_symbol(s
, "__riscv_fsqrt", NULL
);
843 tcc_define_symbol(s
, "__riscv_float_abi_double", NULL
);
847 tcc_define_symbol(s
, "_WIN32", NULL
);
848 tcc_define_symbol(s
, "__declspec(x)", "__attribute__((x))");
849 tcc_define_symbol(s
, "__cdecl", "");
850 # ifdef TCC_TARGET_X86_64
851 tcc_define_symbol(s
, "_WIN64", NULL
);
854 tcc_define_symbol(s
, "__unix__", NULL
);
855 tcc_define_symbol(s
, "__unix", NULL
);
856 tcc_define_symbol(s
, "unix", NULL
);
857 # if defined(__linux__)
858 tcc_define_symbol(s
, "__linux__", NULL
);
859 tcc_define_symbol(s
, "__linux", NULL
);
861 # if defined(__FreeBSD__)
862 tcc_define_symbol(s
, "__FreeBSD__", "__FreeBSD__");
863 /* No 'Thread Storage Local' on FreeBSD with tcc */
864 tcc_define_symbol(s
, "__NO_TLS", NULL
);
866 # if defined(__FreeBSD_kernel__)
867 tcc_define_symbol(s
, "__FreeBSD_kernel__", NULL
);
869 # if defined(__NetBSD__)
870 tcc_define_symbol(s
, "__NetBSD__", "__NetBSD__");
872 # if defined(__OpenBSD__)
873 tcc_define_symbol(s
, "__OpenBSD__", "__OpenBSD__");
877 /* TinyCC & gcc defines */
880 tcc_define_symbol(s
, "__SIZE_TYPE__", "unsigned int");
881 tcc_define_symbol(s
, "__PTRDIFF_TYPE__", "int");
882 tcc_define_symbol(s
, "__ILP32__", NULL
);
885 tcc_define_symbol(s
, "__SIZE_TYPE__", "unsigned long long");
886 tcc_define_symbol(s
, "__PTRDIFF_TYPE__", "long long");
887 tcc_define_symbol(s
, "__LLP64__", NULL
);
889 /* Other 64bit systems. */
890 tcc_define_symbol(s
, "__SIZE_TYPE__", "unsigned long");
891 tcc_define_symbol(s
, "__PTRDIFF_TYPE__", "long");
892 tcc_define_symbol(s
, "__LP64__", NULL
);
894 tcc_define_symbol(s
, "__SIZEOF_POINTER__", PTR_SIZE
== 4 ? "4" : "8");
897 tcc_define_symbol(s
, "__WCHAR_TYPE__", "unsigned short");
898 tcc_define_symbol(s
, "__WINT_TYPE__", "unsigned short");
900 tcc_define_symbol(s
, "__WCHAR_TYPE__", "int");
901 /* wint_t is unsigned int by default, but (signed) int on BSDs
902 and unsigned short on windows. Other OSes might have still
903 other conventions, sigh. */
904 # if defined(__FreeBSD__) || defined (__FreeBSD_kernel__) \
905 || defined(__NetBSD__) || defined(__OpenBSD__)
906 tcc_define_symbol(s
, "__WINT_TYPE__", "int");
908 /* define __GNUC__ to have some useful stuff from sys/cdefs.h
909 that are unconditionally used in FreeBSDs other system headers :/ */
910 tcc_define_symbol(s
, "__GNUC__", "2");
911 tcc_define_symbol(s
, "__GNUC_MINOR__", "7");
912 tcc_define_symbol(s
, "__builtin_alloca", "alloca");
915 tcc_define_symbol(s
, "__WINT_TYPE__", "unsigned int");
917 tcc_define_symbol(s
, "__REDIRECT(name, proto, alias)",
918 "name proto __asm__ (#alias)");
919 tcc_define_symbol(s
, "__REDIRECT_NTH(name, proto, alias)",
920 "name proto __asm__ (#alias) __THROW");
922 # if defined(TCC_MUSL)
923 tcc_define_symbol(s
, "__DEFINED_va_list", "");
924 tcc_define_symbol(s
, "__DEFINED___isoc_va_list", "");
925 tcc_define_symbol(s
, "__isoc_va_list", "void *");
926 # endif /* TCC_MUSL */
927 /* Some GCC builtins that are simple to express as macros. */
928 tcc_define_symbol(s
, "__builtin_extract_return_addr(x)", "x");
929 #endif /* ndef TCC_TARGET_PE */
933 LIBTCCAPI
void tcc_delete(TCCState
*s1
)
938 /* free library paths */
939 dynarray_reset(&s1
->library_paths
, &s1
->nb_library_paths
);
940 dynarray_reset(&s1
->crt_paths
, &s1
->nb_crt_paths
);
942 /* free include paths */
943 dynarray_reset(&s1
->include_paths
, &s1
->nb_include_paths
);
944 dynarray_reset(&s1
->sysinclude_paths
, &s1
->nb_sysinclude_paths
);
946 tcc_free(s1
->tcc_lib_path
);
947 tcc_free(s1
->soname
);
949 tcc_free(s1
->init_symbol
);
950 tcc_free(s1
->fini_symbol
);
951 tcc_free(s1
->outfile
);
952 tcc_free(s1
->deps_outfile
);
953 dynarray_reset(&s1
->files
, &s1
->nb_files
);
954 dynarray_reset(&s1
->target_deps
, &s1
->nb_target_deps
);
955 dynarray_reset(&s1
->pragma_libs
, &s1
->nb_pragma_libs
);
956 dynarray_reset(&s1
->argv
, &s1
->argc
);
958 cstr_free(&s1
->cmdline_defs
);
959 cstr_free(&s1
->cmdline_incl
);
961 /* free runtime memory */
967 if (0 == --nb_states
)
972 LIBTCCAPI
int tcc_set_output_type(TCCState
*s
, int output_type
)
974 s
->output_type
= output_type
;
976 /* always elf for objects */
977 if (output_type
== TCC_OUTPUT_OBJ
)
978 s
->output_format
= TCC_OUTPUT_FORMAT_ELF
;
980 if (s
->char_is_unsigned
)
981 tcc_define_symbol(s
, "__CHAR_UNSIGNED__", NULL
);
984 /* default include paths */
985 /* -isystem paths have already been handled */
986 tcc_add_sysinclude_path(s
, CONFIG_TCC_SYSINCLUDEPATHS
);
989 #ifdef CONFIG_TCC_BCHECK
990 if (s
->do_bounds_check
) {
991 /* if bound checking, then add corresponding sections */
992 tccelf_bounds_new(s
);
994 tcc_define_symbol(s
, "__BOUNDS_CHECKING_ON", NULL
);
998 /* add debug sections */
1002 tcc_add_library_path(s
, CONFIG_TCC_LIBPATHS
);
1004 #ifdef TCC_TARGET_PE
1006 if (!s
->nostdlib
&& output_type
!= TCC_OUTPUT_OBJ
)
1007 tcc_add_systemdir(s
);
1010 /* paths for crt objects */
1011 tcc_split_path(s
, &s
->crt_paths
, &s
->nb_crt_paths
, CONFIG_TCC_CRTPREFIX
);
1012 /* add libc crt1/crti objects */
1013 if ((output_type
== TCC_OUTPUT_EXE
|| output_type
== TCC_OUTPUT_DLL
) &&
1015 if (output_type
!= TCC_OUTPUT_DLL
)
1016 tcc_add_crt(s
, "crt1.o");
1017 tcc_add_crt(s
, "crti.o");
1023 LIBTCCAPI
int tcc_add_include_path(TCCState
*s
, const char *pathname
)
1025 tcc_split_path(s
, &s
->include_paths
, &s
->nb_include_paths
, pathname
);
1029 LIBTCCAPI
int tcc_add_sysinclude_path(TCCState
*s
, const char *pathname
)
1031 tcc_split_path(s
, &s
->sysinclude_paths
, &s
->nb_sysinclude_paths
, pathname
);
1035 ST_FUNC
int tcc_add_file_internal(TCCState
*s1
, const char *filename
, int flags
)
1040 fd
= _tcc_open(s1
, filename
);
1042 if (flags
& AFF_PRINT_ERROR
)
1043 tcc_error_noabort("file '%s' not found", filename
);
1047 /* update target deps */
1048 dynarray_add(&s1
->target_deps
, &s1
->nb_target_deps
,
1049 tcc_strdup(filename
));
1051 if (flags
& AFF_TYPE_BIN
) {
1055 obj_type
= tcc_object_type(fd
, &ehdr
);
1056 lseek(fd
, 0, SEEK_SET
);
1058 #ifdef TCC_TARGET_MACHO
1059 if (0 == obj_type
&& 0 == strcmp(tcc_fileextension(filename
), ".dylib"))
1060 obj_type
= AFF_BINTYPE_DYN
;
1064 case AFF_BINTYPE_REL
:
1065 ret
= tcc_load_object_file(s1
, fd
, 0);
1067 #ifndef TCC_TARGET_PE
1068 case AFF_BINTYPE_DYN
:
1069 if (s1
->output_type
== TCC_OUTPUT_MEMORY
) {
1071 #ifdef TCC_IS_NATIVE
1072 if (NULL
== dlopen(filename
, RTLD_GLOBAL
| RTLD_LAZY
))
1076 ret
= tcc_load_dll(s1
, fd
, filename
,
1077 (flags
& AFF_REFERENCED_DLL
) != 0);
1081 case AFF_BINTYPE_AR
:
1082 ret
= tcc_load_archive(s1
, fd
, !(flags
& AFF_WHOLE_ARCHIVE
));
1084 #ifdef TCC_TARGET_COFF
1085 case AFF_BINTYPE_C67
:
1086 ret
= tcc_load_coff(s1
, fd
);
1090 #ifdef TCC_TARGET_PE
1091 ret
= pe_load_file(s1
, filename
, fd
);
1093 /* as GNU ld, consider it is an ld script if not recognized */
1094 ret
= tcc_load_ldscript(s1
, fd
);
1097 tcc_error_noabort("unrecognized file type");
1102 ret
= tcc_compile(s1
, flags
, filename
, fd
);
1107 LIBTCCAPI
int tcc_add_file(TCCState
*s
, const char *filename
)
1109 int filetype
= s
->filetype
;
1110 if (0 == (filetype
& AFF_TYPE_MASK
)) {
1111 /* use a file extension to detect a filetype */
1112 const char *ext
= tcc_fileextension(filename
);
1115 if (!strcmp(ext
, "S"))
1116 filetype
= AFF_TYPE_ASMPP
;
1117 else if (!strcmp(ext
, "s"))
1118 filetype
= AFF_TYPE_ASM
;
1119 else if (!PATHCMP(ext
, "c") || !PATHCMP(ext
, "i"))
1120 filetype
= AFF_TYPE_C
;
1122 filetype
|= AFF_TYPE_BIN
;
1124 filetype
= AFF_TYPE_C
;
1127 return tcc_add_file_internal(s
, filename
, filetype
| AFF_PRINT_ERROR
);
1130 LIBTCCAPI
int tcc_add_library_path(TCCState
*s
, const char *pathname
)
1132 tcc_split_path(s
, &s
->library_paths
, &s
->nb_library_paths
, pathname
);
1136 static int tcc_add_library_internal(TCCState
*s
, const char *fmt
,
1137 const char *filename
, int flags
, char **paths
, int nb_paths
)
1142 for(i
= 0; i
< nb_paths
; i
++) {
1143 snprintf(buf
, sizeof(buf
), fmt
, paths
[i
], filename
);
1144 if (tcc_add_file_internal(s
, buf
, flags
| AFF_TYPE_BIN
) == 0)
1150 /* find and load a dll. Return non zero if not found */
1151 /* XXX: add '-rpath' option support ? */
1152 ST_FUNC
int tcc_add_dll(TCCState
*s
, const char *filename
, int flags
)
1154 return tcc_add_library_internal(s
, "%s/%s", filename
, flags
,
1155 s
->library_paths
, s
->nb_library_paths
);
1158 #ifndef TCC_TARGET_PE
1159 ST_FUNC
int tcc_add_crt(TCCState
*s1
, const char *filename
)
1161 if (-1 == tcc_add_library_internal(s1
, "%s/%s",
1162 filename
, 0, s1
->crt_paths
, s1
->nb_crt_paths
))
1163 tcc_error_noabort("file '%s' not found", filename
);
1168 /* the library name is the same as the argument of the '-l' option */
1169 LIBTCCAPI
int tcc_add_library(TCCState
*s
, const char *libraryname
)
1171 #if defined TCC_TARGET_PE
1172 const char *libs
[] = { "%s/%s.def", "%s/lib%s.def", "%s/%s.dll", "%s/lib%s.dll", "%s/lib%s.a", NULL
};
1173 const char **pp
= s
->static_link
? libs
+ 4 : libs
;
1174 #elif defined TCC_TARGET_MACHO
1175 const char *libs
[] = { "%s/lib%s.dylib", "%s/lib%s.a", NULL
};
1176 const char **pp
= s
->static_link
? libs
+ 1 : libs
;
1178 const char *libs
[] = { "%s/lib%s.so", "%s/lib%s.a", NULL
};
1179 const char **pp
= s
->static_link
? libs
+ 1 : libs
;
1181 int flags
= s
->filetype
& AFF_WHOLE_ARCHIVE
;
1183 if (0 == tcc_add_library_internal(s
, *pp
,
1184 libraryname
, flags
, s
->library_paths
, s
->nb_library_paths
))
1191 PUB_FUNC
int tcc_add_library_err(TCCState
*s1
, const char *libname
)
1193 int ret
= tcc_add_library(s1
, libname
);
1195 tcc_error_noabort("library '%s' not found", libname
);
1199 /* handle #pragma comment(lib,) */
1200 ST_FUNC
void tcc_add_pragma_libs(TCCState
*s1
)
1203 for (i
= 0; i
< s1
->nb_pragma_libs
; i
++)
1204 tcc_add_library_err(s1
, s1
->pragma_libs
[i
]);
1207 LIBTCCAPI
int tcc_add_symbol(TCCState
*s1
, const char *name
, const void *val
)
1209 #ifdef TCC_TARGET_PE
1210 /* On x86_64 'val' might not be reachable with a 32bit offset.
1211 So it is handled here as if it were in a DLL. */
1212 pe_putimport(s1
, 0, name
, (uintptr_t)val
);
1214 set_elf_sym(symtab_section
, (uintptr_t)val
, 0,
1215 ELFW(ST_INFO
)(STB_GLOBAL
, STT_NOTYPE
), 0,
1221 LIBTCCAPI
void tcc_set_lib_path(TCCState
*s
, const char *path
)
1223 tcc_free(s
->tcc_lib_path
);
1224 s
->tcc_lib_path
= tcc_strdup(path
);
1227 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
1228 #define FD_INVERT 0x0002 /* invert value before storing */
1230 typedef struct FlagDef
{
1236 static int no_flag(const char **pp
)
1238 const char *p
= *pp
;
1239 if (*p
!= 'n' || *++p
!= 'o' || *++p
!= '-')
1245 ST_FUNC
int set_flag(TCCState
*s
, const FlagDef
*flags
, const char *name
)
1256 for (ret
= -1, p
= flags
; p
->name
; ++p
) {
1258 if (strcmp(r
, p
->name
))
1261 if (0 == (p
->flags
& WD_ALL
))
1265 *((unsigned char *)s
+ p
->offset
) =
1266 p
->flags
& FD_INVERT
? !value
: value
;
1276 static int strstart(const char *val
, const char **str
)
1291 /* Like strstart, but automatically takes into account that ld options can
1293 * - start with double or single dash (e.g. '--soname' or '-soname')
1294 * - arguments can be given as separate or after '=' (e.g. '-Wl,-soname,x.so'
1295 * or '-Wl,-soname=x.so')
1297 * you provide `val` always in 'option[=]' form (no leading -)
1299 static int link_option(const char *str
, const char *val
, const char **ptr
)
1304 /* there should be 1 or 2 dashes */
1310 /* then str & val should match (potentially up to '=') */
1321 while (*q
!= '\0' && *q
!= '=') {
1328 /* '=' near eos means ',' or '=' is ok */
1332 if (*p
!= ',' && *p
!= '=')
1342 static const char *skip_linker_arg(const char **str
)
1344 const char *s1
= *str
;
1345 const char *s2
= strchr(s1
, ',');
1346 *str
= s2
? s2
++ : (s2
= s1
+ strlen(s1
));
1350 static void copy_linker_arg(char **pp
, const char *s
, int sep
)
1356 p
[l
= strlen(p
)] = sep
, ++l
;
1357 skip_linker_arg(&q
);
1358 pstrncpy(l
+ (*pp
= tcc_realloc(p
, q
- s
+ l
+ 1)), s
, q
- s
);
1361 /* set linker options */
1362 static int tcc_set_linker(TCCState
*s
, const char *option
)
1367 const char *p
= NULL
;
1372 if (link_option(option
, "Bsymbolic", &p
)) {
1374 } else if (link_option(option
, "nostdlib", &p
)) {
1376 } else if (link_option(option
, "fini=", &p
)) {
1377 copy_linker_arg(&s
->fini_symbol
, p
, 0);
1379 } else if (link_option(option
, "image-base=", &p
)
1380 || link_option(option
, "Ttext=", &p
)) {
1381 s
->text_addr
= strtoull(p
, &end
, 16);
1382 s
->has_text_addr
= 1;
1383 } else if (link_option(option
, "init=", &p
)) {
1384 copy_linker_arg(&s
->init_symbol
, p
, 0);
1386 } else if (link_option(option
, "oformat=", &p
)) {
1387 #if defined(TCC_TARGET_PE)
1388 if (strstart("pe-", &p
)) {
1390 if (strstart("elf64-", &p
)) {
1392 if (strstart("elf32-", &p
)) {
1394 s
->output_format
= TCC_OUTPUT_FORMAT_ELF
;
1395 } else if (!strcmp(p
, "binary")) {
1396 s
->output_format
= TCC_OUTPUT_FORMAT_BINARY
;
1397 #ifdef TCC_TARGET_COFF
1398 } else if (!strcmp(p
, "coff")) {
1399 s
->output_format
= TCC_OUTPUT_FORMAT_COFF
;
1404 } else if (link_option(option
, "as-needed", &p
)) {
1406 } else if (link_option(option
, "O", &p
)) {
1408 } else if (link_option(option
, "export-all-symbols", &p
)) {
1410 } else if (link_option(option
, "export-dynamic", &p
)) {
1412 } else if (link_option(option
, "rpath=", &p
)) {
1413 copy_linker_arg(&s
->rpath
, p
, ':');
1414 } else if (link_option(option
, "enable-new-dtags", &p
)) {
1415 s
->enable_new_dtags
= 1;
1416 } else if (link_option(option
, "section-alignment=", &p
)) {
1417 s
->section_align
= strtoul(p
, &end
, 16);
1418 } else if (link_option(option
, "soname=", &p
)) {
1419 copy_linker_arg(&s
->soname
, p
, 0);
1420 #ifdef TCC_TARGET_PE
1421 } else if (link_option(option
, "large-address-aware", &p
)) {
1422 s
->pe_characteristics
|= 0x20;
1423 } else if (link_option(option
, "file-alignment=", &p
)) {
1424 s
->pe_file_align
= strtoul(p
, &end
, 16);
1425 } else if (link_option(option
, "stack=", &p
)) {
1426 s
->pe_stack_size
= strtoul(p
, &end
, 10);
1427 } else if (link_option(option
, "subsystem=", &p
)) {
1428 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1429 if (!strcmp(p
, "native")) {
1430 s
->pe_subsystem
= 1;
1431 } else if (!strcmp(p
, "console")) {
1432 s
->pe_subsystem
= 3;
1433 } else if (!strcmp(p
, "gui") || !strcmp(p
, "windows")) {
1434 s
->pe_subsystem
= 2;
1435 } else if (!strcmp(p
, "posix")) {
1436 s
->pe_subsystem
= 7;
1437 } else if (!strcmp(p
, "efiapp")) {
1438 s
->pe_subsystem
= 10;
1439 } else if (!strcmp(p
, "efiboot")) {
1440 s
->pe_subsystem
= 11;
1441 } else if (!strcmp(p
, "efiruntime")) {
1442 s
->pe_subsystem
= 12;
1443 } else if (!strcmp(p
, "efirom")) {
1444 s
->pe_subsystem
= 13;
1445 #elif defined(TCC_TARGET_ARM)
1446 if (!strcmp(p
, "wince")) {
1447 s
->pe_subsystem
= 9;
1452 } else if (ret
= link_option(option
, "?whole-archive", &p
), ret
) {
1454 s
->filetype
|= AFF_WHOLE_ARCHIVE
;
1456 s
->filetype
&= ~AFF_WHOLE_ARCHIVE
;
1461 tcc_error("unsupported linker option '%s'", option
);
1464 if (ignoring
&& s
->warn_unsupported
)
1465 tcc_warning("unsupported linker option '%s'", option
);
1467 option
= skip_linker_arg(&p
);
1472 typedef struct TCCOption
{
1495 TCC_OPTION_dumpversion
,
1504 TCC_OPTION_traditional
,
1509 TCC_OPTION_mfloat_abi
,
1513 TCC_OPTION_iwithprefix
,
1515 TCC_OPTION_nostdinc
,
1516 TCC_OPTION_nostdlib
,
1517 TCC_OPTION_print_search_dirs
,
1518 TCC_OPTION_rdynamic
,
1520 TCC_OPTION_pedantic
,
1533 #define TCC_OPTION_HAS_ARG 0x0001
1534 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
1536 static const TCCOption tcc_options
[] = {
1537 { "h", TCC_OPTION_HELP
, 0 },
1538 { "-help", TCC_OPTION_HELP
, 0 },
1539 { "?", TCC_OPTION_HELP
, 0 },
1540 { "hh", TCC_OPTION_HELP2
, 0 },
1541 { "v", TCC_OPTION_v
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1542 { "I", TCC_OPTION_I
, TCC_OPTION_HAS_ARG
},
1543 { "D", TCC_OPTION_D
, TCC_OPTION_HAS_ARG
},
1544 { "U", TCC_OPTION_U
, TCC_OPTION_HAS_ARG
},
1545 { "P", TCC_OPTION_P
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1546 { "L", TCC_OPTION_L
, TCC_OPTION_HAS_ARG
},
1547 { "B", TCC_OPTION_B
, TCC_OPTION_HAS_ARG
},
1548 { "l", TCC_OPTION_l
, TCC_OPTION_HAS_ARG
},
1549 { "bench", TCC_OPTION_bench
, 0 },
1550 #ifdef CONFIG_TCC_BACKTRACE
1551 { "bt", TCC_OPTION_bt
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1553 #ifdef CONFIG_TCC_BCHECK
1554 { "b", TCC_OPTION_b
, 0 },
1556 { "g", TCC_OPTION_g
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1557 { "c", TCC_OPTION_c
, 0 },
1558 { "dumpversion", TCC_OPTION_dumpversion
, 0},
1559 { "d", TCC_OPTION_d
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1560 { "static", TCC_OPTION_static
, 0 },
1561 { "std", TCC_OPTION_std
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1562 { "shared", TCC_OPTION_shared
, 0 },
1563 { "soname", TCC_OPTION_soname
, TCC_OPTION_HAS_ARG
},
1564 { "o", TCC_OPTION_o
, TCC_OPTION_HAS_ARG
},
1565 { "-param", TCC_OPTION_param
, TCC_OPTION_HAS_ARG
},
1566 { "pedantic", TCC_OPTION_pedantic
, 0},
1567 { "pthread", TCC_OPTION_pthread
, 0},
1568 { "run", TCC_OPTION_run
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1569 { "rdynamic", TCC_OPTION_rdynamic
, 0 },
1570 { "r", TCC_OPTION_r
, 0 },
1571 { "s", TCC_OPTION_s
, 0 },
1572 { "traditional", TCC_OPTION_traditional
, 0 },
1573 { "Wl,", TCC_OPTION_Wl
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1574 { "Wp,", TCC_OPTION_Wp
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1575 { "W", TCC_OPTION_W
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1576 { "O", TCC_OPTION_O
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1577 #ifdef TCC_TARGET_ARM
1578 { "mfloat-abi", TCC_OPTION_mfloat_abi
, TCC_OPTION_HAS_ARG
},
1580 { "m", TCC_OPTION_m
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1581 { "f", TCC_OPTION_f
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1582 { "isystem", TCC_OPTION_isystem
, TCC_OPTION_HAS_ARG
},
1583 { "include", TCC_OPTION_include
, TCC_OPTION_HAS_ARG
},
1584 { "nostdinc", TCC_OPTION_nostdinc
, 0 },
1585 { "nostdlib", TCC_OPTION_nostdlib
, 0 },
1586 { "print-search-dirs", TCC_OPTION_print_search_dirs
, 0 },
1587 { "w", TCC_OPTION_w
, 0 },
1588 { "pipe", TCC_OPTION_pipe
, 0},
1589 { "E", TCC_OPTION_E
, 0},
1590 { "MD", TCC_OPTION_MD
, 0},
1591 { "MF", TCC_OPTION_MF
, TCC_OPTION_HAS_ARG
},
1592 { "x", TCC_OPTION_x
, TCC_OPTION_HAS_ARG
},
1593 { "ar", TCC_OPTION_ar
, 0},
1594 #ifdef TCC_TARGET_PE
1595 { "impdef", TCC_OPTION_impdef
, 0},
1600 static const FlagDef options_W
[] = {
1602 { offsetof(TCCState
, warn_unsupported
), 0, "unsupported" },
1603 { offsetof(TCCState
, warn_write_strings
), 0, "write-strings" },
1604 { offsetof(TCCState
, warn_error
), 0, "error" },
1605 { offsetof(TCCState
, warn_gcc_compat
), 0, "gcc-compat" },
1606 { offsetof(TCCState
, warn_implicit_function_declaration
), WD_ALL
,
1607 "implicit-function-declaration" },
1611 static const FlagDef options_f
[] = {
1612 { offsetof(TCCState
, char_is_unsigned
), 0, "unsigned-char" },
1613 { offsetof(TCCState
, char_is_unsigned
), FD_INVERT
, "signed-char" },
1614 { offsetof(TCCState
, nocommon
), FD_INVERT
, "common" },
1615 { offsetof(TCCState
, leading_underscore
), 0, "leading-underscore" },
1616 { offsetof(TCCState
, ms_extensions
), 0, "ms-extensions" },
1617 { offsetof(TCCState
, dollars_in_identifiers
), 0, "dollars-in-identifiers" },
1621 static const FlagDef options_m
[] = {
1622 { offsetof(TCCState
, ms_bitfields
), 0, "ms-bitfields" },
1623 #ifdef TCC_TARGET_X86_64
1624 { offsetof(TCCState
, nosse
), FD_INVERT
, "sse" },
1629 static void parse_option_D(TCCState
*s1
, const char *optarg
)
1631 char *sym
= tcc_strdup(optarg
);
1632 char *value
= strchr(sym
, '=');
1635 tcc_define_symbol(s1
, sym
, value
);
1639 static void args_parser_add_file(TCCState
*s
, const char* filename
, int filetype
)
1641 struct filespec
*f
= tcc_malloc(sizeof *f
+ strlen(filename
));
1643 strcpy(f
->name
, filename
);
1644 dynarray_add(&s
->files
, &s
->nb_files
, f
);
1647 static int args_parser_make_argv(const char *r
, int *argc
, char ***argv
)
1652 while (c
= (unsigned char)*r
, c
&& c
<= ' ')
1658 while (c
= (unsigned char)*r
, c
) {
1660 if (c
== '\\' && (*r
== '"' || *r
== '\\')) {
1662 } else if (c
== '"') {
1665 } else if (q
== 0 && c
<= ' ') {
1671 //printf("<%s>\n", str.data), fflush(stdout);
1672 dynarray_add(argv
, argc
, tcc_strdup(str
.data
));
1679 /* read list file */
1680 static void args_parser_listfile(TCCState
*s
,
1681 const char *filename
, int optind
, int *pargc
, char ***pargv
)
1690 fd
= open(filename
, O_RDONLY
| O_BINARY
);
1692 tcc_error("listfile '%s' not found", filename
);
1694 len
= lseek(fd
, 0, SEEK_END
);
1695 p
= tcc_malloc(len
+ 1), p
[len
] = 0;
1696 lseek(fd
, 0, SEEK_SET
), read(fd
, p
, len
), close(fd
);
1698 for (i
= 0; i
< *pargc
; ++i
)
1700 args_parser_make_argv(p
, &argc
, &argv
);
1702 dynarray_add(&argv
, &argc
, tcc_strdup((*pargv
)[i
]));
1705 dynarray_reset(&s
->argv
, &s
->argc
);
1706 *pargc
= s
->argc
= argc
, *pargv
= s
->argv
= argv
;
1709 PUB_FUNC
int tcc_parse_args(TCCState
*s
, int *pargc
, char ***pargv
, int optind
)
1712 const TCCOption
*popt
;
1713 const char *optarg
, *r
;
1714 const char *run
= NULL
;
1717 CString linker_arg
; /* collect -Wl options */
1718 int tool
= 0, arg_start
= 0, noaction
= optind
;
1719 char **argv
= *pargv
;
1722 cstr_new(&linker_arg
);
1724 while (optind
< argc
) {
1726 if (r
[0] == '@' && r
[1] != '\0') {
1727 args_parser_listfile(s
, r
+ 1, optind
, &argc
, &argv
);
1732 if (r
[0] == '-' && r
[1] == 'v' && r
[2] == 0)
1737 if (r
[0] != '-' || r
[1] == '\0') {
1738 if (r
[0] != '@') /* allow "tcc file(s) -run @ args ..." */
1739 args_parser_add_file(s
, r
, s
->filetype
);
1741 tcc_set_options(s
, run
);
1742 arg_start
= optind
- 1;
1748 /* find option in table */
1749 for(popt
= tcc_options
; ; ++popt
) {
1750 const char *p1
= popt
->name
;
1751 const char *r1
= r
+ 1;
1753 tcc_error("invalid option -- '%s'", r
);
1754 if (!strstart(p1
, &r1
))
1757 if (popt
->flags
& TCC_OPTION_HAS_ARG
) {
1758 if (*r1
== '\0' && !(popt
->flags
& TCC_OPTION_NOSEP
)) {
1761 tcc_error("argument to '%s' is missing", r
);
1762 optarg
= argv
[optind
++];
1764 } else if (*r1
!= '\0')
1769 switch(popt
->index
) {
1770 case TCC_OPTION_HELP
:
1772 case TCC_OPTION_HELP2
:
1775 tcc_add_include_path(s
, optarg
);
1778 parse_option_D(s
, optarg
);
1781 tcc_undefine_symbol(s
, optarg
);
1784 tcc_add_library_path(s
, optarg
);
1787 /* set tcc utilities path (mainly for tcc development) */
1788 tcc_set_lib_path(s
, optarg
);
1791 args_parser_add_file(s
, optarg
, AFF_TYPE_LIB
| (s
->filetype
& ~AFF_TYPE_MASK
));
1794 case TCC_OPTION_pthread
:
1795 parse_option_D(s
, "_REENTRANT");
1796 s
->option_pthread
= 1;
1798 case TCC_OPTION_bench
:
1801 #ifdef CONFIG_TCC_BACKTRACE
1803 s
->rt_num_callers
= atoi(optarg
);
1804 s
->do_backtrace
= 1;
1808 #ifdef CONFIG_TCC_BCHECK
1810 s
->do_bounds_check
= 1;
1811 s
->do_backtrace
= 1;
1822 tcc_warning("-%s: overriding compiler action already specified", popt
->name
);
1828 else if (*optarg
== 'M')
1830 else if (*optarg
== 't')
1832 else if (isnum(*optarg
))
1833 s
->g_debug
|= atoi(optarg
);
1835 goto unsupported_option
;
1837 case TCC_OPTION_static
:
1840 case TCC_OPTION_std
:
1841 if (*optarg
== '=') {
1842 if (strcmp(optarg
, "=c11") == 0) {
1843 tcc_undefine_symbol(s
, "__STDC_VERSION__");
1844 tcc_define_symbol(s
, "__STDC_VERSION__", "201112L");
1846 * The integer constant 1, intended to indicate
1847 * that the implementation does not support atomic
1848 * types (including the _Atomic type qualifier) and
1849 * the <stdatomic.h> header.
1851 tcc_define_symbol(s
, "__STDC_NO_ATOMICS__", "1");
1853 * The integer constant 1, intended to indicate
1854 * that the implementation does not support complex
1855 * types or the <complex.h> header.
1857 tcc_define_symbol(s
, "__STDC_NO_COMPLEX__", "1");
1859 * The integer constant 1, intended to indicate
1860 * that the implementation does not support the
1861 * <threads.h> header.
1863 tcc_define_symbol(s
, "__STDC_NO_THREADS__", "1");
1865 * __STDC_NO_VLA__, tcc supports VLA.
1866 * The integer constant 1, intended to indicate
1867 * that the implementation does not support
1868 * variable length arrays or variably modified
1871 #if !defined(TCC_TARGET_PE)
1873 * An integer constant of the form yyyymmL (for
1874 * example, 199712L). If this symbol is defined,
1875 * then every character in the Unicode required
1876 * set, when stored in an object of type
1877 * wchar_t, has the same value as the short
1878 * identifier of that character.
1881 /* on Linux, this conflicts with a define introduced by
1882 * /usr/include/stdc-predef.h included by glibc libs;
1883 * clang doesn't define it at all so it's probably not necessary
1885 tcc_define_symbol(s
, "__STDC_ISO_10646__", "201605L");
1888 * The integer constant 1, intended to indicate
1889 * that values of type char16_t are UTF−16
1890 * encoded. If some other encoding is used, the
1891 * macro shall not be defined and the actual
1892 * encoding used is implementation defined.
1894 tcc_define_symbol(s
, "__STDC_UTF_16__", "1");
1896 * The integer constant 1, intended to indicate
1897 * that values of type char32_t are UTF−32
1898 * encoded. If some other encoding is used, the
1899 * macro shall not be defined and the actual
1900 * encoding used is implementationdefined.
1902 tcc_define_symbol(s
, "__STDC_UTF_32__", "1");
1903 #endif /* !TCC_TARGET_PE */
1904 s
->cversion
= 201112;
1908 * silently ignore other values, a current purpose:
1909 * allow to use a tcc as a reference compiler for "make test"
1912 case TCC_OPTION_shared
:
1914 goto set_output_type
;
1915 case TCC_OPTION_soname
:
1916 s
->soname
= tcc_strdup(optarg
);
1920 tcc_warning("multiple -o option");
1921 tcc_free(s
->outfile
);
1923 s
->outfile
= tcc_strdup(optarg
);
1926 /* generate a .o merging several output files */
1929 goto set_output_type
;
1930 case TCC_OPTION_isystem
:
1931 tcc_add_sysinclude_path(s
, optarg
);
1933 case TCC_OPTION_include
:
1934 cstr_printf(&s
->cmdline_incl
, "#include \"%s\"\n", optarg
);
1936 case TCC_OPTION_nostdinc
:
1939 case TCC_OPTION_nostdlib
:
1942 case TCC_OPTION_run
:
1943 #ifndef TCC_IS_NATIVE
1944 tcc_error("-run is not available in a cross compiler");
1947 x
= TCC_OUTPUT_MEMORY
;
1948 goto set_output_type
;
1950 do ++s
->verbose
; while (*optarg
++ == 'v');
1954 if (set_flag(s
, options_f
, optarg
) < 0)
1955 goto unsupported_option
;
1957 #ifdef TCC_TARGET_ARM
1958 case TCC_OPTION_mfloat_abi
:
1959 /* tcc doesn't support soft float yet */
1960 if (!strcmp(optarg
, "softfp")) {
1961 s
->float_abi
= ARM_SOFTFP_FLOAT
;
1962 tcc_undefine_symbol(s
, "__ARM_PCS_VFP");
1963 } else if (!strcmp(optarg
, "hard"))
1964 s
->float_abi
= ARM_HARD_FLOAT
;
1966 tcc_error("unsupported float abi '%s'", optarg
);
1970 if (set_flag(s
, options_m
, optarg
) < 0) {
1971 if (x
= atoi(optarg
), x
!= 32 && x
!= 64)
1972 goto unsupported_option
;
1973 if (PTR_SIZE
!= x
/8)
1980 if (optarg
[0] && set_flag(s
, options_W
, optarg
) < 0)
1981 goto unsupported_option
;
1986 case TCC_OPTION_rdynamic
:
1990 if (linker_arg
.size
)
1991 --linker_arg
.size
, cstr_ccat(&linker_arg
, ',');
1992 cstr_cat(&linker_arg
, optarg
, 0);
1993 if (tcc_set_linker(s
, linker_arg
.data
))
1994 cstr_free(&linker_arg
);
2000 x
= TCC_OUTPUT_PREPROCESS
;
2001 goto set_output_type
;
2003 s
->Pflag
= atoi(optarg
) + 1;
2009 s
->deps_outfile
= tcc_strdup(optarg
);
2011 case TCC_OPTION_dumpversion
:
2012 printf ("%s\n", TCC_VERSION
);
2019 else if (*optarg
== 'a')
2021 else if (*optarg
== 'b')
2023 else if (*optarg
== 'n')
2026 tcc_warning("unsupported language '%s'", optarg
);
2027 s
->filetype
= x
| (s
->filetype
& ~AFF_TYPE_MASK
);
2030 last_o
= atoi(optarg
);
2032 case TCC_OPTION_print_search_dirs
:
2035 case TCC_OPTION_impdef
:
2041 arg_start
= optind
- 1;
2042 if (arg_start
!= noaction
)
2043 tcc_error("cannot parse %s here", r
);
2046 case TCC_OPTION_traditional
:
2047 case TCC_OPTION_pedantic
:
2048 case TCC_OPTION_pipe
:
2054 if (s
->warn_unsupported
)
2055 tcc_warning("unsupported option '%s'", r
);
2060 tcc_define_symbol(s
, "__OPTIMIZE__", NULL
);
2061 if (linker_arg
.size
) {
2062 r
= linker_arg
.data
;
2065 *pargc
= argc
- arg_start
;
2066 *pargv
= argv
+ arg_start
;
2069 if (optind
!= noaction
)
2071 if (s
->verbose
== 2)
2072 return OPT_PRINT_DIRS
;
2078 LIBTCCAPI
void tcc_set_options(TCCState
*s
, const char *r
)
2082 args_parser_make_argv(r
, &argc
, &argv
);
2083 tcc_parse_args(s
, &argc
, &argv
, 0);
2084 dynarray_reset(&argv
, &argc
);
2087 PUB_FUNC
void tcc_print_stats(TCCState
*s1
, unsigned total_time
)
2091 if (total_bytes
< 1)
2093 fprintf(stderr
, "* %d idents, %d lines, %d bytes\n"
2094 "* %0.3f s, %u lines/s, %0.1f MB/s\n",
2095 total_idents
, total_lines
, total_bytes
,
2096 (double)total_time
/1000,
2097 (unsigned)total_lines
*1000/total_time
,
2098 (double)total_bytes
/1000/total_time
);
2100 fprintf(stderr
, "* %d bytes memory used\n", mem_max_size
);