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
23 /********************************************************/
24 /* global variables */
26 /* use GNU C extensions */
27 ST_DATA
int gnu_ext
= 1;
29 /* use TinyCC extensions */
30 ST_DATA
int tcc_ext
= 1;
32 /* XXX: get rid of this ASAP */
33 ST_DATA
struct TCCState
*tcc_state
;
37 /********************************************************/
44 #ifdef TCC_TARGET_I386
46 #include "i386-link.c"
48 #elif defined(TCC_TARGET_ARM)
52 #elif defined(TCC_TARGET_ARM64)
53 #include "arm64-gen.c"
54 #include "arm64-link.c"
55 #elif defined(TCC_TARGET_C67)
59 #elif defined(TCC_TARGET_X86_64)
60 #include "x86_64-gen.c"
61 #include "x86_64-link.c"
63 #elif defined(TCC_TARGET_RISCV64)
64 #include "riscv64-gen.c"
65 #include "riscv64-link.c"
75 #endif /* ONE_SOURCE */
77 /********************************************************/
78 #ifndef CONFIG_TCC_ASM
79 ST_FUNC
void asm_instr(void)
81 tcc_error("inline asm() not supported");
83 ST_FUNC
void asm_global_instr(void)
85 tcc_error("inline asm() not supported");
89 /********************************************************/
91 ST_FUNC
char *normalize_slashes(char *path
)
94 for (p
= path
; *p
; ++p
)
100 static HMODULE tcc_module
;
102 /* on win32, we suppose the lib and includes are at the location of 'tcc.exe' */
103 static void tcc_set_lib_path_w32(TCCState
*s
)
106 GetModuleFileNameA(tcc_module
, path
, sizeof path
);
107 p
= tcc_basename(normalize_slashes(strlwr(path
)));
111 tcc_set_lib_path(s
, path
);
115 static void tcc_add_systemdir(TCCState
*s
)
118 GetSystemDirectory(buf
, sizeof buf
);
119 tcc_add_library_path(s
, normalize_slashes(buf
));
124 BOOL WINAPI
DllMain (HINSTANCE hDll
, DWORD dwReason
, LPVOID lpReserved
)
126 if (DLL_PROCESS_ATTACH
== dwReason
)
133 /********************************************************/
134 /* copy a string and truncate it. */
135 ST_FUNC
char *pstrcpy(char *buf
, int buf_size
, const char *s
)
142 q_end
= buf
+ buf_size
- 1;
154 /* strcat and truncate. */
155 ST_FUNC
char *pstrcat(char *buf
, int buf_size
, const char *s
)
160 pstrcpy(buf
+ len
, buf_size
- len
, s
);
164 ST_FUNC
char *pstrncpy(char *out
, const char *in
, size_t num
)
166 memcpy(out
, in
, num
);
171 /* extract the basename of a file */
172 PUB_FUNC
char *tcc_basename(const char *name
)
174 char *p
= strchr(name
, 0);
175 while (p
> name
&& !IS_DIRSEP(p
[-1]))
180 /* extract extension part of a file
182 * (if no extension, return pointer to end-of-string)
184 PUB_FUNC
char *tcc_fileextension (const char *name
)
186 char *b
= tcc_basename(name
);
187 char *e
= strrchr(b
, '.');
188 return e
? e
: strchr(b
, 0);
191 /********************************************************/
192 /* memory management */
200 PUB_FUNC
void tcc_free(void *ptr
)
205 PUB_FUNC
void *tcc_malloc(unsigned long size
)
210 tcc_error("memory full (malloc)");
214 PUB_FUNC
void *tcc_mallocz(unsigned long size
)
217 ptr
= tcc_malloc(size
);
218 memset(ptr
, 0, size
);
222 PUB_FUNC
void *tcc_realloc(void *ptr
, unsigned long size
)
225 ptr1
= realloc(ptr
, size
);
227 tcc_error("memory full (realloc)");
231 PUB_FUNC
char *tcc_strdup(const char *str
)
234 ptr
= tcc_malloc(strlen(str
) + 1);
239 PUB_FUNC
void tcc_memcheck(void)
245 #define MEM_DEBUG_MAGIC1 0xFEEDDEB1
246 #define MEM_DEBUG_MAGIC2 0xFEEDDEB2
247 #define MEM_DEBUG_MAGIC3 0xFEEDDEB3
248 #define MEM_DEBUG_FILE_LEN 40
249 #define MEM_DEBUG_CHECK3(header) \
250 ((mem_debug_header_t*)((char*)header + header->size))->magic3
251 #define MEM_USER_PTR(header) \
252 ((char *)header + offsetof(mem_debug_header_t, magic3))
253 #define MEM_HEADER_PTR(ptr) \
254 (mem_debug_header_t *)((char*)ptr - offsetof(mem_debug_header_t, magic3))
256 struct mem_debug_header
{
259 struct mem_debug_header
*prev
;
260 struct mem_debug_header
*next
;
262 char file_name
[MEM_DEBUG_FILE_LEN
+ 1];
264 ALIGNED(16) unsigned magic3
;
267 typedef struct mem_debug_header mem_debug_header_t
;
269 static mem_debug_header_t
*mem_debug_chain
;
270 static unsigned mem_cur_size
;
271 static unsigned mem_max_size
;
273 static mem_debug_header_t
*malloc_check(void *ptr
, const char *msg
)
275 mem_debug_header_t
* header
= MEM_HEADER_PTR(ptr
);
276 if (header
->magic1
!= MEM_DEBUG_MAGIC1
||
277 header
->magic2
!= MEM_DEBUG_MAGIC2
||
278 MEM_DEBUG_CHECK3(header
) != MEM_DEBUG_MAGIC3
||
279 header
->size
== (unsigned)-1) {
280 fprintf(stderr
, "%s check failed\n", msg
);
281 if (header
->magic1
== MEM_DEBUG_MAGIC1
)
282 fprintf(stderr
, "%s:%u: block allocated here.\n",
283 header
->file_name
, header
->line_num
);
289 PUB_FUNC
void *tcc_malloc_debug(unsigned long size
, const char *file
, int line
)
292 mem_debug_header_t
*header
;
294 header
= malloc(sizeof(mem_debug_header_t
) + size
);
296 tcc_error("memory full (malloc)");
298 header
->magic1
= MEM_DEBUG_MAGIC1
;
299 header
->magic2
= MEM_DEBUG_MAGIC2
;
301 MEM_DEBUG_CHECK3(header
) = MEM_DEBUG_MAGIC3
;
302 header
->line_num
= line
;
303 ofs
= strlen(file
) - MEM_DEBUG_FILE_LEN
;
304 strncpy(header
->file_name
, file
+ (ofs
> 0 ? ofs
: 0), MEM_DEBUG_FILE_LEN
);
305 header
->file_name
[MEM_DEBUG_FILE_LEN
] = 0;
307 header
->next
= mem_debug_chain
;
310 header
->next
->prev
= header
;
311 mem_debug_chain
= header
;
313 mem_cur_size
+= size
;
314 if (mem_cur_size
> mem_max_size
)
315 mem_max_size
= mem_cur_size
;
317 return MEM_USER_PTR(header
);
320 PUB_FUNC
void tcc_free_debug(void *ptr
)
322 mem_debug_header_t
*header
;
325 header
= malloc_check(ptr
, "tcc_free");
326 mem_cur_size
-= header
->size
;
327 header
->size
= (unsigned)-1;
329 header
->next
->prev
= header
->prev
;
331 header
->prev
->next
= header
->next
;
332 if (header
== mem_debug_chain
)
333 mem_debug_chain
= header
->next
;
337 PUB_FUNC
void *tcc_mallocz_debug(unsigned long size
, const char *file
, int line
)
340 ptr
= tcc_malloc_debug(size
,file
,line
);
341 memset(ptr
, 0, size
);
345 PUB_FUNC
void *tcc_realloc_debug(void *ptr
, unsigned long size
, const char *file
, int line
)
347 mem_debug_header_t
*header
;
348 int mem_debug_chain_update
= 0;
350 return tcc_malloc_debug(size
, file
, line
);
351 header
= malloc_check(ptr
, "tcc_realloc");
352 mem_cur_size
-= header
->size
;
353 mem_debug_chain_update
= (header
== mem_debug_chain
);
354 header
= realloc(header
, sizeof(mem_debug_header_t
) + size
);
356 tcc_error("memory full (realloc)");
358 MEM_DEBUG_CHECK3(header
) = MEM_DEBUG_MAGIC3
;
360 header
->next
->prev
= header
;
362 header
->prev
->next
= header
;
363 if (mem_debug_chain_update
)
364 mem_debug_chain
= header
;
365 mem_cur_size
+= size
;
366 if (mem_cur_size
> mem_max_size
)
367 mem_max_size
= mem_cur_size
;
368 return MEM_USER_PTR(header
);
371 PUB_FUNC
char *tcc_strdup_debug(const char *str
, const char *file
, int line
)
374 ptr
= tcc_malloc_debug(strlen(str
) + 1, file
, line
);
379 PUB_FUNC
void tcc_memcheck(void)
382 mem_debug_header_t
*header
= mem_debug_chain
;
383 fprintf(stderr
, "MEM_DEBUG: mem_leak= %d bytes, mem_max_size= %d bytes\n",
384 mem_cur_size
, mem_max_size
);
386 fprintf(stderr
, "%s:%u: error: %u bytes leaked\n",
387 header
->file_name
, header
->line_num
, header
->size
);
388 header
= header
->next
;
395 #endif /* MEM_DEBUG */
397 #define free(p) use_tcc_free(p)
398 #define malloc(s) use_tcc_malloc(s)
399 #define realloc(p, s) use_tcc_realloc(p, s)
401 /********************************************************/
404 ST_FUNC
void dynarray_add(void *ptab
, int *nb_ptr
, void *data
)
410 pp
= *(void ***)ptab
;
411 /* every power of two we double array size */
412 if ((nb
& (nb
- 1)) == 0) {
417 pp
= tcc_realloc(pp
, nb_alloc
* sizeof(void *));
424 ST_FUNC
void dynarray_reset(void *pp
, int *n
)
427 for (p
= *(void***)pp
; *n
; ++p
, --*n
)
430 tcc_free(*(void**)pp
);
434 static void tcc_split_path(TCCState
*s
, void *p_ary
, int *p_nb_ary
, const char *in
)
442 for (p
= in
; c
= *p
, c
!= '\0' && c
!= PATHSEP
[0]; ++p
) {
443 if (c
== '{' && p
[1] && p
[2] == '}') {
446 cstr_cat(&str
, s
->tcc_lib_path
, -1);
452 cstr_ccat(&str
, '\0');
453 dynarray_add(p_ary
, p_nb_ary
, tcc_strdup(str
.data
));
460 /********************************************************/
462 static void strcat_vprintf(char *buf
, int buf_size
, const char *fmt
, va_list ap
)
466 vsnprintf(buf
+ len
, buf_size
- len
, fmt
, ap
);
469 static void strcat_printf(char *buf
, int buf_size
, const char *fmt
, ...)
473 strcat_vprintf(buf
, buf_size
, fmt
, ap
);
477 static void error1(TCCState
*s1
, int is_warning
, const char *fmt
, va_list ap
)
480 BufferedFile
**pf
, *f
;
483 /* use upper file if inline ":asm:" or token ":paste:" */
484 for (f
= file
; f
&& f
->filename
[0] == ':'; f
= f
->prev
)
487 for(pf
= s1
->include_stack
; pf
< s1
->include_stack_ptr
; pf
++)
488 strcat_printf(buf
, sizeof(buf
), "In file included from %s:%d:\n",
489 (*pf
)->filename
, (*pf
)->line_num
);
490 if (s1
->error_set_jmp_enabled
) {
491 strcat_printf(buf
, sizeof(buf
), "%s:%d: ",
492 f
->filename
, f
->line_num
- !!(tok_flags
& TOK_FLAG_BOL
));
494 strcat_printf(buf
, sizeof(buf
), "%s: ",
498 strcat_printf(buf
, sizeof(buf
), "tcc: ");
501 strcat_printf(buf
, sizeof(buf
), "warning: ");
503 strcat_printf(buf
, sizeof(buf
), "error: ");
504 strcat_vprintf(buf
, sizeof(buf
), fmt
, ap
);
506 if (!s1
->error_func
) {
507 /* default case: stderr */
508 if (s1
->output_type
== TCC_OUTPUT_PREPROCESS
&& s1
->ppfp
== stdout
)
509 /* print a newline during tcc -E */
510 printf("\n"), fflush(stdout
);
511 fflush(stdout
); /* flush -v output */
512 fprintf(stderr
, "%s\n", buf
);
513 fflush(stderr
); /* print error/warning now (win32) */
515 s1
->error_func(s1
->error_opaque
, buf
);
517 if (!is_warning
|| s1
->warn_error
)
521 LIBTCCAPI
void tcc_set_error_func(TCCState
*s
, void *error_opaque
, TCCErrorFunc error_func
)
523 s
->error_opaque
= error_opaque
;
524 s
->error_func
= error_func
;
527 LIBTCCAPI TCCErrorFunc
tcc_get_error_func(TCCState
*s
)
529 return s
->error_func
;
532 LIBTCCAPI
void *tcc_get_error_opaque(TCCState
*s
)
534 return s
->error_opaque
;
537 /* error without aborting current compilation */
538 PUB_FUNC
void tcc_error_noabort(const char *fmt
, ...)
540 TCCState
*s1
= tcc_state
;
544 error1(s1
, 0, fmt
, ap
);
548 PUB_FUNC
void tcc_error(const char *fmt
, ...)
550 TCCState
*s1
= tcc_state
;
554 error1(s1
, 0, fmt
, ap
);
556 /* better than nothing: in some cases, we accept to handle errors */
557 if (s1
->error_set_jmp_enabled
) {
558 longjmp(s1
->error_jmp_buf
, 1);
560 /* XXX: eliminate this someday */
565 PUB_FUNC
void tcc_warning(const char *fmt
, ...)
567 TCCState
*s1
= tcc_state
;
574 error1(s1
, 1, fmt
, ap
);
578 /********************************************************/
581 ST_FUNC
void tcc_open_bf(TCCState
*s1
, const char *filename
, int initlen
)
584 int buflen
= initlen
? initlen
: IO_BUF_SIZE
;
586 bf
= tcc_mallocz(sizeof(BufferedFile
) + buflen
);
587 bf
->buf_ptr
= bf
->buffer
;
588 bf
->buf_end
= bf
->buffer
+ initlen
;
589 bf
->buf_end
[0] = CH_EOB
; /* put eob symbol */
590 pstrcpy(bf
->filename
, sizeof(bf
->filename
), filename
);
591 bf
->true_filename
= bf
->filename
;
593 bf
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
597 tok_flags
= TOK_FLAG_BOL
| TOK_FLAG_BOF
;
600 ST_FUNC
void tcc_close(void)
602 BufferedFile
*bf
= file
;
605 total_lines
+= bf
->line_num
;
607 if (bf
->true_filename
!= bf
->filename
)
608 tcc_free(bf
->true_filename
);
613 ST_FUNC
int tcc_open(TCCState
*s1
, const char *filename
)
616 if (strcmp(filename
, "-") == 0)
617 fd
= 0, filename
= "<stdin>";
619 fd
= open(filename
, O_RDONLY
| O_BINARY
);
620 if ((s1
->verbose
== 2 && fd
>= 0) || s1
->verbose
== 3)
621 printf("%s %*s%s\n", fd
< 0 ? "nf":"->",
622 (int)(s1
->include_stack_ptr
- s1
->include_stack
), "", filename
);
625 tcc_open_bf(s1
, filename
, 0);
627 normalize_slashes(file
->filename
);
633 /* compile the file opened in 'file'. Return non zero if errors. */
634 static int tcc_compile(TCCState
*s1
, int filetype
)
639 define_start
= define_stack
;
640 is_asm
= !!(filetype
& (AFF_TYPE_ASM
|AFF_TYPE_ASMPP
));
641 tccelf_begin_file(s1
);
643 if (setjmp(s1
->error_jmp_buf
) == 0) {
645 s1
->error_set_jmp_enabled
= 1;
647 preprocess_start(s1
, is_asm
);
648 if (s1
->output_type
== TCC_OUTPUT_PREPROCESS
) {
651 #ifdef CONFIG_TCC_ASM
652 tcc_assemble(s1
, !!(filetype
& AFF_TYPE_ASMPP
));
654 tcc_error_noabort("asm not supported");
660 s1
->error_set_jmp_enabled
= 0;
663 free_inline_functions(s1
);
664 /* reset define stack, but keep -D and built-ins */
665 free_defines(define_start
);
666 sym_pop(&global_stack
, NULL
, 0);
667 sym_pop(&local_stack
, NULL
, 0);
669 return s1
->nb_errors
!= 0 ? -1 : 0;
672 LIBTCCAPI
int tcc_compile_string(TCCState
*s
, const char *str
)
677 tcc_open_bf(s
, "<string>", len
);
678 memcpy(file
->buffer
, str
, len
);
679 ret
= tcc_compile(s
, s
->filetype
);
684 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
685 LIBTCCAPI
void tcc_define_symbol(TCCState
*s1
, const char *sym
, const char *value
)
692 len2
= strlen(value
);
694 /* init file structure */
695 tcc_open_bf(s1
, "<define>", len1
+ len2
+ 1);
696 memcpy(file
->buffer
, sym
, len1
);
697 file
->buffer
[len1
] = ' ';
698 memcpy(file
->buffer
+ len1
+ 1, value
, len2
);
700 /* parse with define parser */
706 /* undefine a preprocessor symbol */
707 LIBTCCAPI
void tcc_undefine_symbol(TCCState
*s1
, const char *sym
)
711 ts
= tok_alloc(sym
, strlen(sym
));
712 s
= define_find(ts
->tok
);
715 tok_str_free_str(s
->d
);
720 /* cleanup all static data used during compilation */
721 static void tcc_cleanup(void)
723 if (NULL
== tcc_state
)
727 tccpp_delete(tcc_state
);
730 dynarray_reset(&sym_pools
, &nb_sym_pools
);
731 /* reset symbol stack */
732 sym_free_first
= NULL
;
735 LIBTCCAPI TCCState
*tcc_new(void)
741 s
= tcc_mallocz(sizeof(TCCState
));
748 s
->dollars_in_identifiers
= 1; /*on by default like in gcc/clang*/
749 s
->cversion
= 199901; /* default unless -std=c11 is supplied */
750 s
->warn_implicit_function_declaration
= 1;
751 s
->ms_extensions
= 1;
753 #ifdef CHAR_IS_UNSIGNED
754 s
->char_is_unsigned
= 1;
756 #ifdef TCC_TARGET_I386
759 /* enable this if you want symbols with leading underscore on windows: */
760 #if 0 /* def TCC_TARGET_PE */
761 s
->leading_underscore
= 1;
764 tcc_set_lib_path_w32(s
);
766 tcc_set_lib_path(s
, CONFIG_TCCDIR
);
771 /* we add dummy defines for some special macros to speed up tests
772 and to have working defined() */
773 define_push(TOK___LINE__
, MACRO_OBJ
, NULL
, NULL
);
774 define_push(TOK___FILE__
, MACRO_OBJ
, NULL
, NULL
);
775 define_push(TOK___DATE__
, MACRO_OBJ
, NULL
, NULL
);
776 define_push(TOK___TIME__
, MACRO_OBJ
, NULL
, NULL
);
777 define_push(TOK___COUNTER__
, MACRO_OBJ
, NULL
, NULL
);
779 /* define __TINYC__ 92X */
780 char buffer
[32]; int a
,b
,c
;
781 sscanf(TCC_VERSION
, "%d.%d.%d", &a
, &b
, &c
);
782 sprintf(buffer
, "%d", a
*10000 + b
*100 + c
);
783 tcc_define_symbol(s
, "__TINYC__", buffer
);
786 /* standard defines */
787 tcc_define_symbol(s
, "__STDC__", NULL
);
788 tcc_define_symbol(s
, "__STDC_VERSION__", "199901L");
789 tcc_define_symbol(s
, "__STDC_HOSTED__", NULL
);
792 #if defined(TCC_TARGET_I386)
793 tcc_define_symbol(s
, "__i386__", NULL
);
794 tcc_define_symbol(s
, "__i386", NULL
);
795 tcc_define_symbol(s
, "i386", NULL
);
796 #elif defined(TCC_TARGET_X86_64)
797 tcc_define_symbol(s
, "__x86_64__", NULL
);
798 #elif defined(TCC_TARGET_ARM)
799 tcc_define_symbol(s
, "__ARM_ARCH_4__", NULL
);
800 tcc_define_symbol(s
, "__arm_elf__", NULL
);
801 tcc_define_symbol(s
, "__arm_elf", NULL
);
802 tcc_define_symbol(s
, "arm_elf", NULL
);
803 tcc_define_symbol(s
, "__arm__", NULL
);
804 tcc_define_symbol(s
, "__arm", NULL
);
805 tcc_define_symbol(s
, "arm", NULL
);
806 tcc_define_symbol(s
, "__APCS_32__", NULL
);
807 tcc_define_symbol(s
, "__ARMEL__", NULL
);
808 #if defined(TCC_ARM_EABI)
809 tcc_define_symbol(s
, "__ARM_EABI__", NULL
);
811 #if defined(TCC_ARM_HARDFLOAT)
812 s
->float_abi
= ARM_HARD_FLOAT
;
813 tcc_define_symbol(s
, "__ARM_PCS_VFP", NULL
);
815 s
->float_abi
= ARM_SOFTFP_FLOAT
;
817 #elif defined(TCC_TARGET_ARM64)
818 tcc_define_symbol(s
, "__aarch64__", NULL
);
819 #elif defined TCC_TARGET_C67
820 tcc_define_symbol(s
, "__C67__", NULL
);
821 #elif defined TCC_TARGET_RISCV64
822 tcc_define_symbol(s
, "__riscv", NULL
);
823 tcc_define_symbol(s
, "__riscv_xlen", "64");
824 tcc_define_symbol(s
, "__riscv_flen", "64");
825 tcc_define_symbol(s
, "__riscv_div", NULL
);
826 tcc_define_symbol(s
, "__riscv_mul", NULL
);
827 tcc_define_symbol(s
, "__riscv_fdiv", NULL
);
828 tcc_define_symbol(s
, "__riscv_fsqrt", NULL
);
829 tcc_define_symbol(s
, "__riscv_float_abi_double", NULL
);
833 tcc_define_symbol(s
, "_WIN32", NULL
);
834 # ifdef TCC_TARGET_X86_64
835 tcc_define_symbol(s
, "_WIN64", NULL
);
838 tcc_define_symbol(s
, "__unix__", NULL
);
839 tcc_define_symbol(s
, "__unix", NULL
);
840 tcc_define_symbol(s
, "unix", NULL
);
841 # if defined(__linux__)
842 tcc_define_symbol(s
, "__linux__", NULL
);
843 tcc_define_symbol(s
, "__linux", NULL
);
845 # if defined(__FreeBSD__)
846 tcc_define_symbol(s
, "__FreeBSD__", "__FreeBSD__");
847 /* No 'Thread Storage Local' on FreeBSD with tcc */
848 tcc_define_symbol(s
, "__NO_TLS", NULL
);
850 # if defined(__FreeBSD_kernel__)
851 tcc_define_symbol(s
, "__FreeBSD_kernel__", NULL
);
853 # if defined(__NetBSD__)
854 tcc_define_symbol(s
, "__NetBSD__", "__NetBSD__");
856 # if defined(__OpenBSD__)
857 tcc_define_symbol(s
, "__OpenBSD__", "__OpenBSD__");
861 /* TinyCC & gcc defines */
864 tcc_define_symbol(s
, "__SIZE_TYPE__", "unsigned int");
865 tcc_define_symbol(s
, "__PTRDIFF_TYPE__", "int");
866 tcc_define_symbol(s
, "__ILP32__", NULL
);
869 tcc_define_symbol(s
, "__SIZE_TYPE__", "unsigned long long");
870 tcc_define_symbol(s
, "__PTRDIFF_TYPE__", "long long");
871 tcc_define_symbol(s
, "__LLP64__", NULL
);
873 /* Other 64bit systems. */
874 tcc_define_symbol(s
, "__SIZE_TYPE__", "unsigned long");
875 tcc_define_symbol(s
, "__PTRDIFF_TYPE__", "long");
876 tcc_define_symbol(s
, "__LP64__", NULL
);
878 tcc_define_symbol(s
, "__SIZEOF_POINTER__", PTR_SIZE
== 4 ? "4" : "8");
881 tcc_define_symbol(s
, "__WCHAR_TYPE__", "unsigned short");
882 tcc_define_symbol(s
, "__WINT_TYPE__", "unsigned short");
884 tcc_define_symbol(s
, "__WCHAR_TYPE__", "int");
885 /* wint_t is unsigned int by default, but (signed) int on BSDs
886 and unsigned short on windows. Other OSes might have still
887 other conventions, sigh. */
888 # if defined(__FreeBSD__) || defined (__FreeBSD_kernel__) \
889 || defined(__NetBSD__) || defined(__OpenBSD__)
890 tcc_define_symbol(s
, "__WINT_TYPE__", "int");
892 /* define __GNUC__ to have some useful stuff from sys/cdefs.h
893 that are unconditionally used in FreeBSDs other system headers :/ */
894 tcc_define_symbol(s
, "__GNUC__", "2");
895 tcc_define_symbol(s
, "__GNUC_MINOR__", "7");
896 tcc_define_symbol(s
, "__builtin_alloca", "alloca");
899 tcc_define_symbol(s
, "__WINT_TYPE__", "unsigned int");
901 tcc_define_symbol(s
, "__REDIRECT(name, proto, alias)",
902 "name proto __asm__ (#alias)");
903 tcc_define_symbol(s
, "__REDIRECT_NTH(name, proto, alias)",
904 "name proto __asm__ (#alias) __THROW");
906 # if defined(TCC_MUSL)
907 tcc_define_symbol(s
, "__DEFINED_va_list", "");
908 tcc_define_symbol(s
, "__DEFINED___isoc_va_list", "");
909 tcc_define_symbol(s
, "__isoc_va_list", "void *");
910 # endif /* TCC_MUSL */
911 /* Some GCC builtins that are simple to express as macros. */
912 tcc_define_symbol(s
, "__builtin_extract_return_addr(x)", "x");
913 #endif /* ndef TCC_TARGET_PE */
917 LIBTCCAPI
void tcc_delete(TCCState
*s1
)
924 /* free library paths */
925 dynarray_reset(&s1
->library_paths
, &s1
->nb_library_paths
);
926 dynarray_reset(&s1
->crt_paths
, &s1
->nb_crt_paths
);
928 /* free include paths */
929 dynarray_reset(&s1
->cached_includes
, &s1
->nb_cached_includes
);
930 dynarray_reset(&s1
->include_paths
, &s1
->nb_include_paths
);
931 dynarray_reset(&s1
->sysinclude_paths
, &s1
->nb_sysinclude_paths
);
932 dynarray_reset(&s1
->cmd_include_files
, &s1
->nb_cmd_include_files
);
934 tcc_free(s1
->tcc_lib_path
);
935 tcc_free(s1
->soname
);
937 tcc_free(s1
->init_symbol
);
938 tcc_free(s1
->fini_symbol
);
939 tcc_free(s1
->outfile
);
940 tcc_free(s1
->deps_outfile
);
941 dynarray_reset(&s1
->files
, &s1
->nb_files
);
942 dynarray_reset(&s1
->target_deps
, &s1
->nb_target_deps
);
943 dynarray_reset(&s1
->pragma_libs
, &s1
->nb_pragma_libs
);
944 dynarray_reset(&s1
->argv
, &s1
->argc
);
947 /* free runtime memory */
952 if (0 == --nb_states
)
956 LIBTCCAPI
int tcc_set_output_type(TCCState
*s
, int output_type
)
958 s
->output_type
= output_type
;
960 /* always elf for objects */
961 if (output_type
== TCC_OUTPUT_OBJ
)
962 s
->output_format
= TCC_OUTPUT_FORMAT_ELF
;
964 if (s
->char_is_unsigned
)
965 tcc_define_symbol(s
, "__CHAR_UNSIGNED__", NULL
);
968 /* default include paths */
969 /* -isystem paths have already been handled */
970 tcc_add_sysinclude_path(s
, CONFIG_TCC_SYSINCLUDEPATHS
);
973 #ifdef CONFIG_TCC_BCHECK
974 if (s
->do_bounds_check
) {
975 /* if bound checking, then add corresponding sections */
976 tccelf_bounds_new(s
);
978 tcc_define_symbol(s
, "__BOUNDS_CHECKING_ON", NULL
);
982 /* add debug sections */
986 tcc_add_library_path(s
, CONFIG_TCC_LIBPATHS
);
990 if (!s
->nostdlib
&& output_type
!= TCC_OUTPUT_OBJ
)
991 tcc_add_systemdir(s
);
994 /* paths for crt objects */
995 tcc_split_path(s
, &s
->crt_paths
, &s
->nb_crt_paths
, CONFIG_TCC_CRTPREFIX
);
996 /* add libc crt1/crti objects */
997 if ((output_type
== TCC_OUTPUT_EXE
|| output_type
== TCC_OUTPUT_DLL
) &&
999 if (output_type
!= TCC_OUTPUT_DLL
)
1000 tcc_add_crt(s
, "crt1.o");
1001 tcc_add_crt(s
, "crti.o");
1007 LIBTCCAPI
int tcc_add_include_path(TCCState
*s
, const char *pathname
)
1009 tcc_split_path(s
, &s
->include_paths
, &s
->nb_include_paths
, pathname
);
1013 LIBTCCAPI
int tcc_add_sysinclude_path(TCCState
*s
, const char *pathname
)
1015 tcc_split_path(s
, &s
->sysinclude_paths
, &s
->nb_sysinclude_paths
, pathname
);
1019 ST_FUNC
int tcc_add_file_internal(TCCState
*s1
, const char *filename
, int flags
)
1024 ret
= tcc_open(s1
, filename
);
1026 if (flags
& AFF_PRINT_ERROR
)
1027 tcc_error_noabort("file '%s' not found", filename
);
1031 /* update target deps */
1032 dynarray_add(&s1
->target_deps
, &s1
->nb_target_deps
,
1033 tcc_strdup(filename
));
1035 if (flags
& AFF_TYPE_BIN
) {
1040 obj_type
= tcc_object_type(fd
, &ehdr
);
1041 lseek(fd
, 0, SEEK_SET
);
1043 #ifdef TCC_TARGET_MACHO
1044 if (0 == obj_type
&& 0 == strcmp(tcc_fileextension(filename
), ".dylib"))
1045 obj_type
= AFF_BINTYPE_DYN
;
1049 case AFF_BINTYPE_REL
:
1050 ret
= tcc_load_object_file(s1
, fd
, 0);
1052 #ifndef TCC_TARGET_PE
1053 case AFF_BINTYPE_DYN
:
1054 if (s1
->output_type
== TCC_OUTPUT_MEMORY
) {
1056 #ifdef TCC_IS_NATIVE
1057 if (NULL
== dlopen(filename
, RTLD_GLOBAL
| RTLD_LAZY
))
1061 ret
= tcc_load_dll(s1
, fd
, filename
,
1062 (flags
& AFF_REFERENCED_DLL
) != 0);
1066 case AFF_BINTYPE_AR
:
1067 ret
= tcc_load_archive(s1
, fd
, !(flags
& AFF_WHOLE_ARCHIVE
));
1069 #ifdef TCC_TARGET_COFF
1070 case AFF_BINTYPE_C67
:
1071 ret
= tcc_load_coff(s1
, fd
);
1075 #ifdef TCC_TARGET_PE
1076 ret
= pe_load_file(s1
, filename
, fd
);
1078 /* as GNU ld, consider it is an ld script if not recognized */
1079 ret
= tcc_load_ldscript(s1
);
1082 tcc_error_noabort("unrecognized file type");
1086 ret
= tcc_compile(s1
, flags
);
1092 LIBTCCAPI
int tcc_add_file(TCCState
*s
, const char *filename
)
1094 int filetype
= s
->filetype
;
1095 if (0 == (filetype
& AFF_TYPE_MASK
)) {
1096 /* use a file extension to detect a filetype */
1097 const char *ext
= tcc_fileextension(filename
);
1100 if (!strcmp(ext
, "S"))
1101 filetype
= AFF_TYPE_ASMPP
;
1102 else if (!strcmp(ext
, "s"))
1103 filetype
= AFF_TYPE_ASM
;
1104 else if (!PATHCMP(ext
, "c") || !PATHCMP(ext
, "i"))
1105 filetype
= AFF_TYPE_C
;
1107 filetype
|= AFF_TYPE_BIN
;
1109 filetype
= AFF_TYPE_C
;
1112 return tcc_add_file_internal(s
, filename
, filetype
| AFF_PRINT_ERROR
);
1115 LIBTCCAPI
int tcc_add_library_path(TCCState
*s
, const char *pathname
)
1117 tcc_split_path(s
, &s
->library_paths
, &s
->nb_library_paths
, pathname
);
1121 static int tcc_add_library_internal(TCCState
*s
, const char *fmt
,
1122 const char *filename
, int flags
, char **paths
, int nb_paths
)
1127 for(i
= 0; i
< nb_paths
; i
++) {
1128 snprintf(buf
, sizeof(buf
), fmt
, paths
[i
], filename
);
1129 if (tcc_add_file_internal(s
, buf
, flags
| AFF_TYPE_BIN
) == 0)
1135 /* find and load a dll. Return non zero if not found */
1136 /* XXX: add '-rpath' option support ? */
1137 ST_FUNC
int tcc_add_dll(TCCState
*s
, const char *filename
, int flags
)
1139 return tcc_add_library_internal(s
, "%s/%s", filename
, flags
,
1140 s
->library_paths
, s
->nb_library_paths
);
1143 #ifndef TCC_TARGET_PE
1144 ST_FUNC
int tcc_add_crt(TCCState
*s
, const char *filename
)
1146 if (-1 == tcc_add_library_internal(s
, "%s/%s",
1147 filename
, 0, s
->crt_paths
, s
->nb_crt_paths
))
1148 tcc_error_noabort("file '%s' not found", filename
);
1153 /* the library name is the same as the argument of the '-l' option */
1154 LIBTCCAPI
int tcc_add_library(TCCState
*s
, const char *libraryname
)
1156 #if defined TCC_TARGET_PE
1157 const char *libs
[] = { "%s/%s.def", "%s/lib%s.def", "%s/%s.dll", "%s/lib%s.dll", "%s/lib%s.a", NULL
};
1158 const char **pp
= s
->static_link
? libs
+ 4 : libs
;
1159 #elif defined TCC_TARGET_MACHO
1160 const char *libs
[] = { "%s/lib%s.dylib", "%s/lib%s.a", NULL
};
1161 const char **pp
= s
->static_link
? libs
+ 1 : libs
;
1163 const char *libs
[] = { "%s/lib%s.so", "%s/lib%s.a", NULL
};
1164 const char **pp
= s
->static_link
? libs
+ 1 : libs
;
1166 int flags
= s
->filetype
& AFF_WHOLE_ARCHIVE
;
1168 if (0 == tcc_add_library_internal(s
, *pp
,
1169 libraryname
, flags
, s
->library_paths
, s
->nb_library_paths
))
1176 PUB_FUNC
int tcc_add_library_err(TCCState
*s
, const char *libname
)
1178 int ret
= tcc_add_library(s
, libname
);
1180 tcc_error_noabort("library '%s' not found", libname
);
1184 /* handle #pragma comment(lib,) */
1185 ST_FUNC
void tcc_add_pragma_libs(TCCState
*s1
)
1188 for (i
= 0; i
< s1
->nb_pragma_libs
; i
++)
1189 tcc_add_library_err(s1
, s1
->pragma_libs
[i
]);
1192 LIBTCCAPI
int tcc_add_symbol(TCCState
*s
, const char *name
, const void *val
)
1194 #ifdef TCC_TARGET_PE
1195 /* On x86_64 'val' might not be reachable with a 32bit offset.
1196 So it is handled here as if it were in a DLL. */
1197 pe_putimport(s
, 0, name
, (uintptr_t)val
);
1199 set_elf_sym(symtab_section
, (uintptr_t)val
, 0,
1200 ELFW(ST_INFO
)(STB_GLOBAL
, STT_NOTYPE
), 0,
1206 LIBTCCAPI
void tcc_set_lib_path(TCCState
*s
, const char *path
)
1208 tcc_free(s
->tcc_lib_path
);
1209 s
->tcc_lib_path
= tcc_strdup(path
);
1212 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
1213 #define FD_INVERT 0x0002 /* invert value before storing */
1215 typedef struct FlagDef
{
1221 static int no_flag(const char **pp
)
1223 const char *p
= *pp
;
1224 if (*p
!= 'n' || *++p
!= 'o' || *++p
!= '-')
1230 ST_FUNC
int set_flag(TCCState
*s
, const FlagDef
*flags
, const char *name
)
1241 for (ret
= -1, p
= flags
; p
->name
; ++p
) {
1243 if (strcmp(r
, p
->name
))
1246 if (0 == (p
->flags
& WD_ALL
))
1250 *(int*)((char *)s
+ p
->offset
) =
1251 p
->flags
& FD_INVERT
? !value
: value
;
1261 static int strstart(const char *val
, const char **str
)
1276 /* Like strstart, but automatically takes into account that ld options can
1278 * - start with double or single dash (e.g. '--soname' or '-soname')
1279 * - arguments can be given as separate or after '=' (e.g. '-Wl,-soname,x.so'
1280 * or '-Wl,-soname=x.so')
1282 * you provide `val` always in 'option[=]' form (no leading -)
1284 static int link_option(const char *str
, const char *val
, const char **ptr
)
1289 /* there should be 1 or 2 dashes */
1295 /* then str & val should match (potentially up to '=') */
1306 while (*q
!= '\0' && *q
!= '=') {
1313 /* '=' near eos means ',' or '=' is ok */
1317 if (*p
!= ',' && *p
!= '=')
1327 static const char *skip_linker_arg(const char **str
)
1329 const char *s1
= *str
;
1330 const char *s2
= strchr(s1
, ',');
1331 *str
= s2
? s2
++ : (s2
= s1
+ strlen(s1
));
1335 static void copy_linker_arg(char **pp
, const char *s
, int sep
)
1341 p
[l
= strlen(p
)] = sep
, ++l
;
1342 skip_linker_arg(&q
);
1343 pstrncpy(l
+ (*pp
= tcc_realloc(p
, q
- s
+ l
+ 1)), s
, q
- s
);
1346 /* set linker options */
1347 static int tcc_set_linker(TCCState
*s
, const char *option
)
1351 const char *p
= NULL
;
1356 if (link_option(option
, "Bsymbolic", &p
)) {
1358 } else if (link_option(option
, "nostdlib", &p
)) {
1360 } else if (link_option(option
, "fini=", &p
)) {
1361 copy_linker_arg(&s
->fini_symbol
, p
, 0);
1363 } else if (link_option(option
, "image-base=", &p
)
1364 || link_option(option
, "Ttext=", &p
)) {
1365 s
->text_addr
= strtoull(p
, &end
, 16);
1366 s
->has_text_addr
= 1;
1367 } else if (link_option(option
, "init=", &p
)) {
1368 copy_linker_arg(&s
->init_symbol
, p
, 0);
1370 } else if (link_option(option
, "oformat=", &p
)) {
1371 #if defined(TCC_TARGET_PE)
1372 if (strstart("pe-", &p
)) {
1374 if (strstart("elf64-", &p
)) {
1376 if (strstart("elf32-", &p
)) {
1378 s
->output_format
= TCC_OUTPUT_FORMAT_ELF
;
1379 } else if (!strcmp(p
, "binary")) {
1380 s
->output_format
= TCC_OUTPUT_FORMAT_BINARY
;
1381 #ifdef TCC_TARGET_COFF
1382 } else if (!strcmp(p
, "coff")) {
1383 s
->output_format
= TCC_OUTPUT_FORMAT_COFF
;
1388 } else if (link_option(option
, "as-needed", &p
)) {
1390 } else if (link_option(option
, "O", &p
)) {
1392 } else if (link_option(option
, "export-all-symbols", &p
)) {
1394 } else if (link_option(option
, "export-dynamic", &p
)) {
1396 } else if (link_option(option
, "rpath=", &p
)) {
1397 copy_linker_arg(&s
->rpath
, p
, ':');
1398 } else if (link_option(option
, "enable-new-dtags", &p
)) {
1399 s
->enable_new_dtags
= 1;
1400 } else if (link_option(option
, "section-alignment=", &p
)) {
1401 s
->section_align
= strtoul(p
, &end
, 16);
1402 } else if (link_option(option
, "soname=", &p
)) {
1403 copy_linker_arg(&s
->soname
, p
, 0);
1404 #ifdef TCC_TARGET_PE
1405 } else if (link_option(option
, "large-address-aware", &p
)) {
1406 s
->pe_characteristics
|= 0x20;
1407 } else if (link_option(option
, "file-alignment=", &p
)) {
1408 s
->pe_file_align
= strtoul(p
, &end
, 16);
1409 } else if (link_option(option
, "stack=", &p
)) {
1410 s
->pe_stack_size
= strtoul(p
, &end
, 10);
1411 } else if (link_option(option
, "subsystem=", &p
)) {
1412 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1413 if (!strcmp(p
, "native")) {
1414 s
->pe_subsystem
= 1;
1415 } else if (!strcmp(p
, "console")) {
1416 s
->pe_subsystem
= 3;
1417 } else if (!strcmp(p
, "gui") || !strcmp(p
, "windows")) {
1418 s
->pe_subsystem
= 2;
1419 } else if (!strcmp(p
, "posix")) {
1420 s
->pe_subsystem
= 7;
1421 } else if (!strcmp(p
, "efiapp")) {
1422 s
->pe_subsystem
= 10;
1423 } else if (!strcmp(p
, "efiboot")) {
1424 s
->pe_subsystem
= 11;
1425 } else if (!strcmp(p
, "efiruntime")) {
1426 s
->pe_subsystem
= 12;
1427 } else if (!strcmp(p
, "efirom")) {
1428 s
->pe_subsystem
= 13;
1429 #elif defined(TCC_TARGET_ARM)
1430 if (!strcmp(p
, "wince")) {
1431 s
->pe_subsystem
= 9;
1436 } else if (ret
= link_option(option
, "?whole-archive", &p
), ret
) {
1438 s
->filetype
|= AFF_WHOLE_ARCHIVE
;
1440 s
->filetype
&= ~AFF_WHOLE_ARCHIVE
;
1445 tcc_error("unsupported linker option '%s'", option
);
1448 if (ignoring
&& s
->warn_unsupported
)
1449 tcc_warning("unsupported linker option '%s'", option
);
1451 option
= skip_linker_arg(&p
);
1456 typedef struct TCCOption
{
1478 TCC_OPTION_dumpversion
,
1487 TCC_OPTION_traditional
,
1492 TCC_OPTION_mfloat_abi
,
1496 TCC_OPTION_iwithprefix
,
1498 TCC_OPTION_nostdinc
,
1499 TCC_OPTION_nostdlib
,
1500 TCC_OPTION_print_search_dirs
,
1501 TCC_OPTION_rdynamic
,
1503 TCC_OPTION_pedantic
,
1516 #define TCC_OPTION_HAS_ARG 0x0001
1517 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
1519 static const TCCOption tcc_options
[] = {
1520 { "h", TCC_OPTION_HELP
, 0 },
1521 { "-help", TCC_OPTION_HELP
, 0 },
1522 { "?", TCC_OPTION_HELP
, 0 },
1523 { "hh", TCC_OPTION_HELP2
, 0 },
1524 { "v", TCC_OPTION_v
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1525 { "I", TCC_OPTION_I
, TCC_OPTION_HAS_ARG
},
1526 { "D", TCC_OPTION_D
, TCC_OPTION_HAS_ARG
},
1527 { "U", TCC_OPTION_U
, TCC_OPTION_HAS_ARG
},
1528 { "P", TCC_OPTION_P
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1529 { "L", TCC_OPTION_L
, TCC_OPTION_HAS_ARG
},
1530 { "B", TCC_OPTION_B
, TCC_OPTION_HAS_ARG
},
1531 { "l", TCC_OPTION_l
, TCC_OPTION_HAS_ARG
},
1532 { "bench", TCC_OPTION_bench
, 0 },
1533 #ifdef CONFIG_TCC_BACKTRACE
1534 { "bt", TCC_OPTION_bt
, TCC_OPTION_HAS_ARG
},
1536 #ifdef CONFIG_TCC_BCHECK
1537 { "b", TCC_OPTION_b
, 0 },
1539 { "g", TCC_OPTION_g
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1540 { "c", TCC_OPTION_c
, 0 },
1541 { "dumpversion", TCC_OPTION_dumpversion
, 0},
1542 { "d", TCC_OPTION_d
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1543 { "static", TCC_OPTION_static
, 0 },
1544 { "std", TCC_OPTION_std
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1545 { "shared", TCC_OPTION_shared
, 0 },
1546 { "soname", TCC_OPTION_soname
, TCC_OPTION_HAS_ARG
},
1547 { "o", TCC_OPTION_o
, TCC_OPTION_HAS_ARG
},
1548 { "-param", TCC_OPTION_param
, TCC_OPTION_HAS_ARG
},
1549 { "pedantic", TCC_OPTION_pedantic
, 0},
1550 { "pthread", TCC_OPTION_pthread
, 0},
1551 { "run", TCC_OPTION_run
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1552 { "rdynamic", TCC_OPTION_rdynamic
, 0 },
1553 { "r", TCC_OPTION_r
, 0 },
1554 { "s", TCC_OPTION_s
, 0 },
1555 { "traditional", TCC_OPTION_traditional
, 0 },
1556 { "Wl,", TCC_OPTION_Wl
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1557 { "Wp,", TCC_OPTION_Wp
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1558 { "W", TCC_OPTION_W
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1559 { "O", TCC_OPTION_O
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1560 #ifdef TCC_TARGET_ARM
1561 { "mfloat-abi", TCC_OPTION_mfloat_abi
, TCC_OPTION_HAS_ARG
},
1563 { "m", TCC_OPTION_m
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1564 { "f", TCC_OPTION_f
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1565 { "isystem", TCC_OPTION_isystem
, TCC_OPTION_HAS_ARG
},
1566 { "include", TCC_OPTION_include
, TCC_OPTION_HAS_ARG
},
1567 { "nostdinc", TCC_OPTION_nostdinc
, 0 },
1568 { "nostdlib", TCC_OPTION_nostdlib
, 0 },
1569 { "print-search-dirs", TCC_OPTION_print_search_dirs
, 0 },
1570 { "w", TCC_OPTION_w
, 0 },
1571 { "pipe", TCC_OPTION_pipe
, 0},
1572 { "E", TCC_OPTION_E
, 0},
1573 { "MD", TCC_OPTION_MD
, 0},
1574 { "MF", TCC_OPTION_MF
, TCC_OPTION_HAS_ARG
},
1575 { "x", TCC_OPTION_x
, TCC_OPTION_HAS_ARG
},
1576 { "ar", TCC_OPTION_ar
, 0},
1577 #ifdef TCC_TARGET_PE
1578 { "impdef", TCC_OPTION_impdef
, 0},
1583 static const FlagDef options_W
[] = {
1585 { offsetof(TCCState
, warn_unsupported
), 0, "unsupported" },
1586 { offsetof(TCCState
, warn_write_strings
), 0, "write-strings" },
1587 { offsetof(TCCState
, warn_error
), 0, "error" },
1588 { offsetof(TCCState
, warn_gcc_compat
), 0, "gcc-compat" },
1589 { offsetof(TCCState
, warn_implicit_function_declaration
), WD_ALL
,
1590 "implicit-function-declaration" },
1594 static const FlagDef options_f
[] = {
1595 { offsetof(TCCState
, char_is_unsigned
), 0, "unsigned-char" },
1596 { offsetof(TCCState
, char_is_unsigned
), FD_INVERT
, "signed-char" },
1597 { offsetof(TCCState
, nocommon
), FD_INVERT
, "common" },
1598 { offsetof(TCCState
, leading_underscore
), 0, "leading-underscore" },
1599 { offsetof(TCCState
, ms_extensions
), 0, "ms-extensions" },
1600 { offsetof(TCCState
, dollars_in_identifiers
), 0, "dollars-in-identifiers" },
1604 static const FlagDef options_m
[] = {
1605 { offsetof(TCCState
, ms_bitfields
), 0, "ms-bitfields" },
1606 #ifdef TCC_TARGET_X86_64
1607 { offsetof(TCCState
, nosse
), FD_INVERT
, "sse" },
1612 static void parse_option_D(TCCState
*s1
, const char *optarg
)
1614 char *sym
= tcc_strdup(optarg
);
1615 char *value
= strchr(sym
, '=');
1618 tcc_define_symbol(s1
, sym
, value
);
1622 static void args_parser_add_file(TCCState
*s
, const char* filename
, int filetype
)
1624 struct filespec
*f
= tcc_malloc(sizeof *f
+ strlen(filename
));
1626 strcpy(f
->name
, filename
);
1627 dynarray_add(&s
->files
, &s
->nb_files
, f
);
1630 static int args_parser_make_argv(const char *r
, int *argc
, char ***argv
)
1635 while (c
= (unsigned char)*r
, c
&& c
<= ' ')
1641 while (c
= (unsigned char)*r
, c
) {
1643 if (c
== '\\' && (*r
== '"' || *r
== '\\')) {
1645 } else if (c
== '"') {
1648 } else if (q
== 0 && c
<= ' ') {
1654 //printf("<%s>\n", str.data), fflush(stdout);
1655 dynarray_add(argv
, argc
, tcc_strdup(str
.data
));
1662 /* read list file */
1663 static void args_parser_listfile(TCCState
*s
,
1664 const char *filename
, int optind
, int *pargc
, char ***pargv
)
1672 fd
= open(filename
, O_RDONLY
| O_BINARY
);
1674 tcc_error("listfile '%s' not found", filename
);
1676 len
= lseek(fd
, 0, SEEK_END
);
1677 p
= tcc_malloc(len
+ 1), p
[len
] = 0;
1678 lseek(fd
, 0, SEEK_SET
), read(fd
, p
, len
), close(fd
);
1680 for (i
= 0; i
< *pargc
; ++i
)
1682 args_parser_make_argv(p
, &argc
, &argv
);
1684 dynarray_add(&argv
, &argc
, tcc_strdup((*pargv
)[i
]));
1687 dynarray_reset(&s
->argv
, &s
->argc
);
1688 *pargc
= s
->argc
= argc
, *pargv
= s
->argv
= argv
;
1691 PUB_FUNC
int tcc_parse_args(TCCState
*s
, int *pargc
, char ***pargv
, int optind
)
1693 const TCCOption
*popt
;
1694 const char *optarg
, *r
;
1695 const char *run
= NULL
;
1698 CString linker_arg
; /* collect -Wl options */
1699 int tool
= 0, arg_start
= 0, noaction
= optind
;
1700 char **argv
= *pargv
;
1703 cstr_new(&linker_arg
);
1705 while (optind
< argc
) {
1707 if (r
[0] == '@' && r
[1] != '\0') {
1708 args_parser_listfile(s
, r
+ 1, optind
, &argc
, &argv
);
1713 if (r
[0] == '-' && r
[1] == 'v' && r
[2] == 0)
1718 if (r
[0] != '-' || r
[1] == '\0') {
1719 if (r
[0] != '@') /* allow "tcc file(s) -run @ args ..." */
1720 args_parser_add_file(s
, r
, s
->filetype
);
1722 tcc_set_options(s
, run
);
1723 arg_start
= optind
- 1;
1729 /* find option in table */
1730 for(popt
= tcc_options
; ; ++popt
) {
1731 const char *p1
= popt
->name
;
1732 const char *r1
= r
+ 1;
1734 tcc_error("invalid option -- '%s'", r
);
1735 if (!strstart(p1
, &r1
))
1738 if (popt
->flags
& TCC_OPTION_HAS_ARG
) {
1739 if (*r1
== '\0' && !(popt
->flags
& TCC_OPTION_NOSEP
)) {
1742 tcc_error("argument to '%s' is missing", r
);
1743 optarg
= argv
[optind
++];
1745 } else if (*r1
!= '\0')
1750 switch(popt
->index
) {
1751 case TCC_OPTION_HELP
:
1753 case TCC_OPTION_HELP2
:
1756 tcc_add_include_path(s
, optarg
);
1759 parse_option_D(s
, optarg
);
1762 tcc_undefine_symbol(s
, optarg
);
1765 tcc_add_library_path(s
, optarg
);
1768 /* set tcc utilities path (mainly for tcc development) */
1769 tcc_set_lib_path(s
, optarg
);
1772 args_parser_add_file(s
, optarg
, AFF_TYPE_LIB
| (s
->filetype
& ~AFF_TYPE_MASK
));
1775 case TCC_OPTION_pthread
:
1776 parse_option_D(s
, "_REENTRANT");
1777 s
->option_pthread
= 1;
1779 case TCC_OPTION_bench
:
1782 #ifdef CONFIG_TCC_BACKTRACE
1784 tcc_set_num_callers(atoi(optarg
));
1787 #ifdef CONFIG_TCC_BCHECK
1789 s
->do_bounds_check
= 1;
1800 tcc_warning("-%s: overriding compiler action already specified", popt
->name
);
1806 else if (*optarg
== 'M')
1808 else if (*optarg
== 't')
1810 else if (isnum(*optarg
))
1811 g_debug
= atoi(optarg
);
1813 goto unsupported_option
;
1815 case TCC_OPTION_static
:
1818 case TCC_OPTION_std
:
1819 if (*optarg
== '=') {
1820 if (strcmp(optarg
, "=c11") == 0) {
1821 tcc_undefine_symbol(s
, "__STDC_VERSION__");
1822 tcc_define_symbol(s
, "__STDC_VERSION__", "201112L");
1824 * The integer constant 1, intended to indicate
1825 * that the implementation does not support atomic
1826 * types (including the _Atomic type qualifier) and
1827 * the <stdatomic.h> header.
1829 tcc_define_symbol(s
, "__STDC_NO_ATOMICS__", "1");
1831 * The integer constant 1, intended to indicate
1832 * that the implementation does not support complex
1833 * types or the <complex.h> header.
1835 tcc_define_symbol(s
, "__STDC_NO_COMPLEX__", "1");
1837 * The integer constant 1, intended to indicate
1838 * that the implementation does not support the
1839 * <threads.h> header.
1841 tcc_define_symbol(s
, "__STDC_NO_THREADS__", "1");
1843 * __STDC_NO_VLA__, tcc supports VLA.
1844 * The integer constant 1, intended to indicate
1845 * that the implementation does not support
1846 * variable length arrays or variably modified
1849 #if !defined(TCC_TARGET_PE)
1851 * An integer constant of the form yyyymmL (for
1852 * example, 199712L). If this symbol is defined,
1853 * then every character in the Unicode required
1854 * set, when stored in an object of type
1855 * wchar_t, has the same value as the short
1856 * identifier of that character.
1859 /* on Linux, this conflicts with a define introduced by
1860 * /usr/include/stdc-predef.h included by glibc libs;
1861 * clang doesn't define it at all so it's probably not necessary
1863 tcc_define_symbol(s
, "__STDC_ISO_10646__", "201605L");
1866 * The integer constant 1, intended to indicate
1867 * that values of type char16_t are UTF−16
1868 * encoded. If some other encoding is used, the
1869 * macro shall not be defined and the actual
1870 * encoding used is implementation defined.
1872 tcc_define_symbol(s
, "__STDC_UTF_16__", "1");
1874 * The integer constant 1, intended to indicate
1875 * that values of type char32_t are UTF−32
1876 * encoded. If some other encoding is used, the
1877 * macro shall not be defined and the actual
1878 * encoding used is implementationdefined.
1880 tcc_define_symbol(s
, "__STDC_UTF_32__", "1");
1881 #endif /* !TCC_TARGET_PE */
1882 s
->cversion
= 201112;
1886 * silently ignore other values, a current purpose:
1887 * allow to use a tcc as a reference compiler for "make test"
1890 case TCC_OPTION_shared
:
1892 goto set_output_type
;
1893 case TCC_OPTION_soname
:
1894 s
->soname
= tcc_strdup(optarg
);
1898 tcc_warning("multiple -o option");
1899 tcc_free(s
->outfile
);
1901 s
->outfile
= tcc_strdup(optarg
);
1904 /* generate a .o merging several output files */
1907 goto set_output_type
;
1908 case TCC_OPTION_isystem
:
1909 tcc_add_sysinclude_path(s
, optarg
);
1911 case TCC_OPTION_include
:
1912 dynarray_add(&s
->cmd_include_files
,
1913 &s
->nb_cmd_include_files
, tcc_strdup(optarg
));
1915 case TCC_OPTION_nostdinc
:
1918 case TCC_OPTION_nostdlib
:
1921 case TCC_OPTION_run
:
1922 #ifndef TCC_IS_NATIVE
1923 tcc_error("-run is not available in a cross compiler");
1926 x
= TCC_OUTPUT_MEMORY
;
1927 goto set_output_type
;
1929 do ++s
->verbose
; while (*optarg
++ == 'v');
1933 if (set_flag(s
, options_f
, optarg
) < 0)
1934 goto unsupported_option
;
1936 #ifdef TCC_TARGET_ARM
1937 case TCC_OPTION_mfloat_abi
:
1938 /* tcc doesn't support soft float yet */
1939 if (!strcmp(optarg
, "softfp")) {
1940 s
->float_abi
= ARM_SOFTFP_FLOAT
;
1941 tcc_undefine_symbol(s
, "__ARM_PCS_VFP");
1942 } else if (!strcmp(optarg
, "hard"))
1943 s
->float_abi
= ARM_HARD_FLOAT
;
1945 tcc_error("unsupported float abi '%s'", optarg
);
1949 if (set_flag(s
, options_m
, optarg
) < 0) {
1950 if (x
= atoi(optarg
), x
!= 32 && x
!= 64)
1951 goto unsupported_option
;
1952 if (PTR_SIZE
!= x
/8)
1958 if (set_flag(s
, options_W
, optarg
) < 0)
1959 goto unsupported_option
;
1964 case TCC_OPTION_rdynamic
:
1968 if (linker_arg
.size
)
1969 --linker_arg
.size
, cstr_ccat(&linker_arg
, ',');
1970 cstr_cat(&linker_arg
, optarg
, 0);
1971 if (tcc_set_linker(s
, linker_arg
.data
))
1972 cstr_free(&linker_arg
);
1978 x
= TCC_OUTPUT_PREPROCESS
;
1979 goto set_output_type
;
1981 s
->Pflag
= atoi(optarg
) + 1;
1987 s
->deps_outfile
= tcc_strdup(optarg
);
1989 case TCC_OPTION_dumpversion
:
1990 printf ("%s\n", TCC_VERSION
);
1997 else if (*optarg
== 'a')
1999 else if (*optarg
== 'b')
2001 else if (*optarg
== 'n')
2004 tcc_warning("unsupported language '%s'", optarg
);
2005 s
->filetype
= x
| (s
->filetype
& ~AFF_TYPE_MASK
);
2008 last_o
= atoi(optarg
);
2010 case TCC_OPTION_print_search_dirs
:
2013 case TCC_OPTION_impdef
:
2019 arg_start
= optind
- 1;
2020 if (arg_start
!= noaction
)
2021 tcc_error("cannot parse %s here", r
);
2024 case TCC_OPTION_traditional
:
2025 case TCC_OPTION_pedantic
:
2026 case TCC_OPTION_pipe
:
2032 if (s
->warn_unsupported
)
2033 tcc_warning("unsupported option '%s'", r
);
2038 tcc_define_symbol(s
, "__OPTIMIZE__", NULL
);
2039 if (linker_arg
.size
) {
2040 r
= linker_arg
.data
;
2043 *pargc
= argc
- arg_start
;
2044 *pargv
= argv
+ arg_start
;
2047 if (optind
!= noaction
)
2049 if (s
->verbose
== 2)
2050 return OPT_PRINT_DIRS
;
2056 LIBTCCAPI
void tcc_set_options(TCCState
*s
, const char *r
)
2060 args_parser_make_argv(r
, &argc
, &argv
);
2061 tcc_parse_args(s
, &argc
, &argv
, 0);
2062 dynarray_reset(&argv
, &argc
);
2065 PUB_FUNC
void tcc_print_stats(TCCState
*s
, unsigned total_time
)
2069 if (total_bytes
< 1)
2071 fprintf(stderr
, "* %d idents, %d lines, %d bytes\n"
2072 "* %0.3f s, %u lines/s, %0.1f MB/s\n",
2073 tok_ident
- TOK_IDENT
, total_lines
, total_bytes
,
2074 (double)total_time
/1000,
2075 (unsigned)total_lines
*1000/total_time
,
2076 (double)total_bytes
/1000/total_time
);
2078 fprintf(stderr
, "* %d bytes memory used\n", mem_max_size
);