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"
54 #ifdef TCC_TARGET_ARM64
55 #include "arm64-gen.c"
56 #include "arm64-link.c"
63 #ifdef TCC_TARGET_X86_64
64 #include "x86_64-gen.c"
65 #include "x86_64-link.c"
74 #endif /* ONE_SOURCE */
76 /********************************************************/
77 #ifndef CONFIG_TCC_ASM
78 ST_FUNC
void asm_instr(void)
80 tcc_error("inline asm() not supported");
82 ST_FUNC
void asm_global_instr(void)
84 tcc_error("inline asm() not supported");
88 /********************************************************/
90 ST_FUNC
char *normalize_slashes(char *path
)
93 for (p
= path
; *p
; ++p
)
99 static HMODULE tcc_module
;
101 /* on win32, we suppose the lib and includes are at the location of 'tcc.exe' */
102 static void tcc_set_lib_path_w32(TCCState
*s
)
105 GetModuleFileNameA(tcc_module
, path
, sizeof path
);
106 p
= tcc_basename(normalize_slashes(strlwr(path
)));
110 tcc_set_lib_path(s
, path
);
114 static void tcc_add_systemdir(TCCState
*s
)
117 GetSystemDirectory(buf
, sizeof buf
);
118 tcc_add_library_path(s
, normalize_slashes(buf
));
123 BOOL WINAPI
DllMain (HINSTANCE hDll
, DWORD dwReason
, LPVOID lpReserved
)
125 if (DLL_PROCESS_ATTACH
== dwReason
)
132 /********************************************************/
133 /* copy a string and truncate it. */
134 ST_FUNC
char *pstrcpy(char *buf
, int buf_size
, const char *s
)
141 q_end
= buf
+ buf_size
- 1;
153 /* strcat and truncate. */
154 ST_FUNC
char *pstrcat(char *buf
, int buf_size
, const char *s
)
159 pstrcpy(buf
+ len
, buf_size
- len
, s
);
163 ST_FUNC
char *pstrncpy(char *out
, const char *in
, size_t num
)
165 memcpy(out
, in
, num
);
170 /* extract the basename of a file */
171 PUB_FUNC
char *tcc_basename(const char *name
)
173 char *p
= strchr(name
, 0);
174 while (p
> name
&& !IS_DIRSEP(p
[-1]))
179 /* extract extension part of a file
181 * (if no extension, return pointer to end-of-string)
183 PUB_FUNC
char *tcc_fileextension (const char *name
)
185 char *b
= tcc_basename(name
);
186 char *e
= strrchr(b
, '.');
187 return e
? e
: strchr(b
, 0);
190 /********************************************************/
191 /* memory management */
199 PUB_FUNC
void tcc_free(void *ptr
)
204 PUB_FUNC
void *tcc_malloc(unsigned long size
)
209 tcc_error("memory full (malloc)");
213 PUB_FUNC
void *tcc_mallocz(unsigned long size
)
216 ptr
= tcc_malloc(size
);
217 memset(ptr
, 0, size
);
221 PUB_FUNC
void *tcc_realloc(void *ptr
, unsigned long size
)
224 ptr1
= realloc(ptr
, size
);
226 tcc_error("memory full (realloc)");
230 PUB_FUNC
char *tcc_strdup(const char *str
)
233 ptr
= tcc_malloc(strlen(str
) + 1);
238 PUB_FUNC
void tcc_memcheck(void)
244 #define MEM_DEBUG_MAGIC1 0xFEEDDEB1
245 #define MEM_DEBUG_MAGIC2 0xFEEDDEB2
246 #define MEM_DEBUG_MAGIC3 0xFEEDDEB3
247 #define MEM_DEBUG_FILE_LEN 40
248 #define MEM_DEBUG_CHECK3(header) \
249 ((mem_debug_header_t*)((char*)header + header->size))->magic3
250 #define MEM_USER_PTR(header) \
251 ((char *)header + offsetof(mem_debug_header_t, magic3))
252 #define MEM_HEADER_PTR(ptr) \
253 (mem_debug_header_t *)((char*)ptr - offsetof(mem_debug_header_t, magic3))
255 struct mem_debug_header
{
258 struct mem_debug_header
*prev
;
259 struct mem_debug_header
*next
;
261 char file_name
[MEM_DEBUG_FILE_LEN
+ 1];
263 ALIGNED(16) unsigned magic3
;
266 typedef struct mem_debug_header mem_debug_header_t
;
268 static mem_debug_header_t
*mem_debug_chain
;
269 static unsigned mem_cur_size
;
270 static unsigned mem_max_size
;
272 static mem_debug_header_t
*malloc_check(void *ptr
, const char *msg
)
274 mem_debug_header_t
* header
= MEM_HEADER_PTR(ptr
);
275 if (header
->magic1
!= MEM_DEBUG_MAGIC1
||
276 header
->magic2
!= MEM_DEBUG_MAGIC2
||
277 MEM_DEBUG_CHECK3(header
) != MEM_DEBUG_MAGIC3
||
278 header
->size
== (unsigned)-1) {
279 fprintf(stderr
, "%s check failed\n", msg
);
280 if (header
->magic1
== MEM_DEBUG_MAGIC1
)
281 fprintf(stderr
, "%s:%u: block allocated here.\n",
282 header
->file_name
, header
->line_num
);
288 PUB_FUNC
void *tcc_malloc_debug(unsigned long size
, const char *file
, int line
)
291 mem_debug_header_t
*header
;
293 header
= malloc(sizeof(mem_debug_header_t
) + size
);
295 tcc_error("memory full (malloc)");
297 header
->magic1
= MEM_DEBUG_MAGIC1
;
298 header
->magic2
= MEM_DEBUG_MAGIC2
;
300 MEM_DEBUG_CHECK3(header
) = MEM_DEBUG_MAGIC3
;
301 header
->line_num
= line
;
302 ofs
= strlen(file
) - MEM_DEBUG_FILE_LEN
;
303 strncpy(header
->file_name
, file
+ (ofs
> 0 ? ofs
: 0), MEM_DEBUG_FILE_LEN
);
304 header
->file_name
[MEM_DEBUG_FILE_LEN
] = 0;
306 header
->next
= mem_debug_chain
;
309 header
->next
->prev
= header
;
310 mem_debug_chain
= header
;
312 mem_cur_size
+= size
;
313 if (mem_cur_size
> mem_max_size
)
314 mem_max_size
= mem_cur_size
;
316 return MEM_USER_PTR(header
);
319 PUB_FUNC
void tcc_free_debug(void *ptr
)
321 mem_debug_header_t
*header
;
324 header
= malloc_check(ptr
, "tcc_free");
325 mem_cur_size
-= header
->size
;
326 header
->size
= (unsigned)-1;
328 header
->next
->prev
= header
->prev
;
330 header
->prev
->next
= header
->next
;
331 if (header
== mem_debug_chain
)
332 mem_debug_chain
= header
->next
;
336 PUB_FUNC
void *tcc_mallocz_debug(unsigned long size
, const char *file
, int line
)
339 ptr
= tcc_malloc_debug(size
,file
,line
);
340 memset(ptr
, 0, size
);
344 PUB_FUNC
void *tcc_realloc_debug(void *ptr
, unsigned long size
, const char *file
, int line
)
346 mem_debug_header_t
*header
;
347 int mem_debug_chain_update
= 0;
349 return tcc_malloc_debug(size
, file
, line
);
350 header
= malloc_check(ptr
, "tcc_realloc");
351 mem_cur_size
-= header
->size
;
352 mem_debug_chain_update
= (header
== mem_debug_chain
);
353 header
= realloc(header
, sizeof(mem_debug_header_t
) + size
);
355 tcc_error("memory full (realloc)");
357 MEM_DEBUG_CHECK3(header
) = MEM_DEBUG_MAGIC3
;
359 header
->next
->prev
= header
;
361 header
->prev
->next
= header
;
362 if (mem_debug_chain_update
)
363 mem_debug_chain
= header
;
364 mem_cur_size
+= size
;
365 if (mem_cur_size
> mem_max_size
)
366 mem_max_size
= mem_cur_size
;
367 return MEM_USER_PTR(header
);
370 PUB_FUNC
char *tcc_strdup_debug(const char *str
, const char *file
, int line
)
373 ptr
= tcc_malloc_debug(strlen(str
) + 1, file
, line
);
378 PUB_FUNC
void tcc_memcheck(void)
381 mem_debug_header_t
*header
= mem_debug_chain
;
382 fprintf(stderr
, "MEM_DEBUG: mem_leak= %d bytes, mem_max_size= %d bytes\n",
383 mem_cur_size
, mem_max_size
);
385 fprintf(stderr
, "%s:%u: error: %u bytes leaked\n",
386 header
->file_name
, header
->line_num
, header
->size
);
387 header
= header
->next
;
394 #endif /* MEM_DEBUG */
396 #define free(p) use_tcc_free(p)
397 #define malloc(s) use_tcc_malloc(s)
398 #define realloc(p, s) use_tcc_realloc(p, s)
400 /********************************************************/
403 ST_FUNC
void dynarray_add(void *ptab
, int *nb_ptr
, void *data
)
409 pp
= *(void ***)ptab
;
410 /* every power of two we double array size */
411 if ((nb
& (nb
- 1)) == 0) {
416 pp
= tcc_realloc(pp
, nb_alloc
* sizeof(void *));
423 ST_FUNC
void dynarray_reset(void *pp
, int *n
)
426 for (p
= *(void***)pp
; *n
; ++p
, --*n
)
429 tcc_free(*(void**)pp
);
433 static void tcc_split_path(TCCState
*s
, void *p_ary
, int *p_nb_ary
, const char *in
)
441 for (p
= in
; c
= *p
, c
!= '\0' && c
!= PATHSEP
[0]; ++p
) {
442 if (c
== '{' && p
[1] && p
[2] == '}') {
445 cstr_cat(&str
, s
->tcc_lib_path
, -1);
451 cstr_ccat(&str
, '\0');
452 dynarray_add(p_ary
, p_nb_ary
, tcc_strdup(str
.data
));
459 /********************************************************/
461 static void strcat_vprintf(char *buf
, int buf_size
, const char *fmt
, va_list ap
)
465 vsnprintf(buf
+ len
, buf_size
- len
, fmt
, ap
);
468 static void strcat_printf(char *buf
, int buf_size
, const char *fmt
, ...)
472 strcat_vprintf(buf
, buf_size
, fmt
, ap
);
476 static void error1(TCCState
*s1
, int is_warning
, const char *fmt
, va_list ap
)
479 BufferedFile
**pf
, *f
;
482 /* use upper file if inline ":asm:" or token ":paste:" */
483 for (f
= file
; f
&& f
->filename
[0] == ':'; f
= f
->prev
)
486 for(pf
= s1
->include_stack
; pf
< s1
->include_stack_ptr
; pf
++)
487 strcat_printf(buf
, sizeof(buf
), "In file included from %s:%d:\n",
488 (*pf
)->filename
, (*pf
)->line_num
);
489 if (s1
->error_set_jmp_enabled
) {
490 strcat_printf(buf
, sizeof(buf
), "%s:%d: ",
491 f
->filename
, f
->line_num
- !!(tok_flags
& TOK_FLAG_BOL
));
493 strcat_printf(buf
, sizeof(buf
), "%s: ",
497 strcat_printf(buf
, sizeof(buf
), "tcc: ");
500 strcat_printf(buf
, sizeof(buf
), "warning: ");
502 strcat_printf(buf
, sizeof(buf
), "error: ");
503 strcat_vprintf(buf
, sizeof(buf
), fmt
, ap
);
505 if (!s1
->error_func
) {
506 /* default case: stderr */
507 if (s1
->output_type
== TCC_OUTPUT_PREPROCESS
&& s1
->ppfp
== stdout
)
508 /* print a newline during tcc -E */
509 printf("\n"), fflush(stdout
);
510 fflush(stdout
); /* flush -v output */
511 fprintf(stderr
, "%s\n", buf
);
512 fflush(stderr
); /* print error/warning now (win32) */
514 s1
->error_func(s1
->error_opaque
, buf
);
516 if (!is_warning
|| s1
->warn_error
)
520 LIBTCCAPI
void tcc_set_error_func(TCCState
*s
, void *error_opaque
,
521 void (*error_func
)(void *opaque
, const char *msg
))
523 s
->error_opaque
= error_opaque
;
524 s
->error_func
= error_func
;
527 /* error without aborting current compilation */
528 PUB_FUNC
void tcc_error_noabort(const char *fmt
, ...)
530 TCCState
*s1
= tcc_state
;
534 error1(s1
, 0, fmt
, ap
);
538 PUB_FUNC
void tcc_error(const char *fmt
, ...)
540 TCCState
*s1
= tcc_state
;
544 error1(s1
, 0, fmt
, ap
);
546 /* better than nothing: in some cases, we accept to handle errors */
547 if (s1
->error_set_jmp_enabled
) {
548 longjmp(s1
->error_jmp_buf
, 1);
550 /* XXX: eliminate this someday */
555 PUB_FUNC
void tcc_warning(const char *fmt
, ...)
557 TCCState
*s1
= tcc_state
;
564 error1(s1
, 1, fmt
, ap
);
568 /********************************************************/
571 ST_FUNC
void tcc_open_bf(TCCState
*s1
, const char *filename
, int initlen
)
574 int buflen
= initlen
? initlen
: IO_BUF_SIZE
;
576 bf
= tcc_mallocz(sizeof(BufferedFile
) + buflen
);
577 bf
->buf_ptr
= bf
->buffer
;
578 bf
->buf_end
= bf
->buffer
+ initlen
;
579 bf
->buf_end
[0] = CH_EOB
; /* put eob symbol */
580 pstrcpy(bf
->filename
, sizeof(bf
->filename
), filename
);
581 bf
->true_filename
= bf
->filename
;
583 bf
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
587 tok_flags
= TOK_FLAG_BOL
| TOK_FLAG_BOF
;
590 ST_FUNC
void tcc_close(void)
592 BufferedFile
*bf
= file
;
595 total_lines
+= bf
->line_num
;
597 if (bf
->true_filename
!= bf
->filename
)
598 tcc_free(bf
->true_filename
);
603 ST_FUNC
int tcc_open(TCCState
*s1
, const char *filename
)
606 if (strcmp(filename
, "-") == 0)
607 fd
= 0, filename
= "<stdin>";
609 fd
= open(filename
, O_RDONLY
| O_BINARY
);
610 if ((s1
->verbose
== 2 && fd
>= 0) || s1
->verbose
== 3)
611 printf("%s %*s%s\n", fd
< 0 ? "nf":"->",
612 (int)(s1
->include_stack_ptr
- s1
->include_stack
), "", filename
);
615 tcc_open_bf(s1
, filename
, 0);
617 normalize_slashes(file
->filename
);
623 /* compile the file opened in 'file'. Return non zero if errors. */
624 static int tcc_compile(TCCState
*s1
, int filetype
)
629 define_start
= define_stack
;
630 is_asm
= !!(filetype
& (AFF_TYPE_ASM
|AFF_TYPE_ASMPP
));
631 tccelf_begin_file(s1
);
633 if (setjmp(s1
->error_jmp_buf
) == 0) {
635 s1
->error_set_jmp_enabled
= 1;
637 preprocess_start(s1
, is_asm
);
638 if (s1
->output_type
== TCC_OUTPUT_PREPROCESS
) {
641 #ifdef CONFIG_TCC_ASM
642 tcc_assemble(s1
, !!(filetype
& AFF_TYPE_ASMPP
));
644 tcc_error_noabort("asm not supported");
650 s1
->error_set_jmp_enabled
= 0;
653 free_inline_functions(s1
);
654 /* reset define stack, but keep -D and built-ins */
655 free_defines(define_start
);
656 sym_pop(&global_stack
, NULL
, 0);
657 sym_pop(&local_stack
, NULL
, 0);
659 return s1
->nb_errors
!= 0 ? -1 : 0;
662 LIBTCCAPI
int tcc_compile_string(TCCState
*s
, const char *str
)
667 tcc_open_bf(s
, "<string>", len
);
668 memcpy(file
->buffer
, str
, len
);
669 ret
= tcc_compile(s
, s
->filetype
);
674 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
675 LIBTCCAPI
void tcc_define_symbol(TCCState
*s1
, const char *sym
, const char *value
)
682 len2
= strlen(value
);
684 /* init file structure */
685 tcc_open_bf(s1
, "<define>", len1
+ len2
+ 1);
686 memcpy(file
->buffer
, sym
, len1
);
687 file
->buffer
[len1
] = ' ';
688 memcpy(file
->buffer
+ len1
+ 1, value
, len2
);
690 /* parse with define parser */
696 /* undefine a preprocessor symbol */
697 LIBTCCAPI
void tcc_undefine_symbol(TCCState
*s1
, const char *sym
)
701 ts
= tok_alloc(sym
, strlen(sym
));
702 s
= define_find(ts
->tok
);
703 /* undefine symbol by putting an invalid name */
708 /* cleanup all static data used during compilation */
709 static void tcc_cleanup(void)
711 if (NULL
== tcc_state
)
715 tccpp_delete(tcc_state
);
718 dynarray_reset(&sym_pools
, &nb_sym_pools
);
719 /* reset symbol stack */
720 sym_free_first
= NULL
;
723 LIBTCCAPI TCCState
*tcc_new(void)
729 s
= tcc_mallocz(sizeof(TCCState
));
736 s
->dollars_in_identifiers
= 1; /*on by default like in gcc/clang*/
737 s
->cversion
= 199901; /* default unless -std=c11 is supplied */
738 s
->warn_implicit_function_declaration
= 1;
739 s
->ms_extensions
= 1;
741 #ifdef CHAR_IS_UNSIGNED
742 s
->char_is_unsigned
= 1;
744 #ifdef TCC_TARGET_I386
747 /* enable this if you want symbols with leading underscore on windows: */
748 #if 0 /* def TCC_TARGET_PE */
749 s
->leading_underscore
= 1;
752 tcc_set_lib_path_w32(s
);
754 tcc_set_lib_path(s
, CONFIG_TCCDIR
);
759 /* we add dummy defines for some special macros to speed up tests
760 and to have working defined() */
761 define_push(TOK___LINE__
, MACRO_OBJ
, NULL
, NULL
);
762 define_push(TOK___FILE__
, MACRO_OBJ
, NULL
, NULL
);
763 define_push(TOK___DATE__
, MACRO_OBJ
, NULL
, NULL
);
764 define_push(TOK___TIME__
, MACRO_OBJ
, NULL
, NULL
);
765 define_push(TOK___COUNTER__
, MACRO_OBJ
, NULL
, NULL
);
767 /* define __TINYC__ 92X */
768 char buffer
[32]; int a
,b
,c
;
769 sscanf(TCC_VERSION
, "%d.%d.%d", &a
, &b
, &c
);
770 sprintf(buffer
, "%d", a
*10000 + b
*100 + c
);
771 tcc_define_symbol(s
, "__TINYC__", buffer
);
774 /* standard defines */
775 tcc_define_symbol(s
, "__STDC__", NULL
);
776 tcc_define_symbol(s
, "__STDC_VERSION__", "199901L");
777 tcc_define_symbol(s
, "__STDC_HOSTED__", NULL
);
780 #if defined(TCC_TARGET_I386)
781 tcc_define_symbol(s
, "__i386__", NULL
);
782 tcc_define_symbol(s
, "__i386", NULL
);
783 tcc_define_symbol(s
, "i386", NULL
);
784 #elif defined(TCC_TARGET_X86_64)
785 tcc_define_symbol(s
, "__x86_64__", NULL
);
786 #elif defined(TCC_TARGET_ARM)
787 tcc_define_symbol(s
, "__ARM_ARCH_4__", NULL
);
788 tcc_define_symbol(s
, "__arm_elf__", NULL
);
789 tcc_define_symbol(s
, "__arm_elf", NULL
);
790 tcc_define_symbol(s
, "arm_elf", NULL
);
791 tcc_define_symbol(s
, "__arm__", NULL
);
792 tcc_define_symbol(s
, "__arm", NULL
);
793 tcc_define_symbol(s
, "arm", NULL
);
794 tcc_define_symbol(s
, "__APCS_32__", NULL
);
795 tcc_define_symbol(s
, "__ARMEL__", NULL
);
796 #if defined(TCC_ARM_EABI)
797 tcc_define_symbol(s
, "__ARM_EABI__", NULL
);
799 #if defined(TCC_ARM_HARDFLOAT)
800 s
->float_abi
= ARM_HARD_FLOAT
;
801 tcc_define_symbol(s
, "__ARM_PCS_VFP", NULL
);
803 s
->float_abi
= ARM_SOFTFP_FLOAT
;
805 #elif defined(TCC_TARGET_ARM64)
806 tcc_define_symbol(s
, "__aarch64__", NULL
);
807 #elif defined TCC_TARGET_C67
808 tcc_define_symbol(s
, "__C67__", NULL
);
812 tcc_define_symbol(s
, "_WIN32", NULL
);
813 # ifdef TCC_TARGET_X86_64
814 tcc_define_symbol(s
, "_WIN64", NULL
);
817 tcc_define_symbol(s
, "__unix__", NULL
);
818 tcc_define_symbol(s
, "__unix", NULL
);
819 tcc_define_symbol(s
, "unix", NULL
);
820 # if defined(__linux__)
821 tcc_define_symbol(s
, "__linux__", NULL
);
822 tcc_define_symbol(s
, "__linux", NULL
);
824 # if defined(__FreeBSD__)
825 tcc_define_symbol(s
, "__FreeBSD__", "__FreeBSD__");
826 /* No 'Thread Storage Local' on FreeBSD with tcc */
827 tcc_define_symbol(s
, "__NO_TLS", NULL
);
829 # if defined(__FreeBSD_kernel__)
830 tcc_define_symbol(s
, "__FreeBSD_kernel__", NULL
);
832 # if defined(__NetBSD__)
833 tcc_define_symbol(s
, "__NetBSD__", "__NetBSD__");
835 # if defined(__OpenBSD__)
836 tcc_define_symbol(s
, "__OpenBSD__", "__OpenBSD__");
840 /* TinyCC & gcc defines */
843 tcc_define_symbol(s
, "__SIZE_TYPE__", "unsigned int");
844 tcc_define_symbol(s
, "__PTRDIFF_TYPE__", "int");
845 tcc_define_symbol(s
, "__ILP32__", NULL
);
848 tcc_define_symbol(s
, "__SIZE_TYPE__", "unsigned long long");
849 tcc_define_symbol(s
, "__PTRDIFF_TYPE__", "long long");
850 tcc_define_symbol(s
, "__LLP64__", NULL
);
852 /* Other 64bit systems. */
853 tcc_define_symbol(s
, "__SIZE_TYPE__", "unsigned long");
854 tcc_define_symbol(s
, "__PTRDIFF_TYPE__", "long");
855 tcc_define_symbol(s
, "__LP64__", NULL
);
859 tcc_define_symbol(s
, "__WCHAR_TYPE__", "unsigned short");
860 tcc_define_symbol(s
, "__WINT_TYPE__", "unsigned short");
862 tcc_define_symbol(s
, "__WCHAR_TYPE__", "int");
863 /* wint_t is unsigned int by default, but (signed) int on BSDs
864 and unsigned short on windows. Other OSes might have still
865 other conventions, sigh. */
866 # if defined(__FreeBSD__) || defined (__FreeBSD_kernel__) \
867 || defined(__NetBSD__) || defined(__OpenBSD__)
868 tcc_define_symbol(s
, "__WINT_TYPE__", "int");
870 /* define __GNUC__ to have some useful stuff from sys/cdefs.h
871 that are unconditionally used in FreeBSDs other system headers :/ */
872 tcc_define_symbol(s
, "__GNUC__", "2");
873 tcc_define_symbol(s
, "__GNUC_MINOR__", "7");
874 tcc_define_symbol(s
, "__builtin_alloca", "alloca");
877 tcc_define_symbol(s
, "__WINT_TYPE__", "unsigned int");
879 tcc_define_symbol(s
, "__REDIRECT(name, proto, alias)",
880 "name proto __asm__ (#alias)");
881 tcc_define_symbol(s
, "__REDIRECT_NTH(name, proto, alias)",
882 "name proto __asm__ (#alias) __THROW");
884 # if defined(TCC_MUSL)
885 tcc_define_symbol(s
, "__DEFINED_va_list", "");
886 tcc_define_symbol(s
, "__DEFINED___isoc_va_list", "");
887 tcc_define_symbol(s
, "__isoc_va_list", "void *");
888 # endif /* TCC_MUSL */
889 /* Some GCC builtins that are simple to express as macros. */
890 tcc_define_symbol(s
, "__builtin_extract_return_addr(x)", "x");
891 #endif /* ndef TCC_TARGET_PE */
895 LIBTCCAPI
void tcc_delete(TCCState
*s1
)
902 /* free library paths */
903 dynarray_reset(&s1
->library_paths
, &s1
->nb_library_paths
);
904 dynarray_reset(&s1
->crt_paths
, &s1
->nb_crt_paths
);
906 /* free include paths */
907 dynarray_reset(&s1
->cached_includes
, &s1
->nb_cached_includes
);
908 dynarray_reset(&s1
->include_paths
, &s1
->nb_include_paths
);
909 dynarray_reset(&s1
->sysinclude_paths
, &s1
->nb_sysinclude_paths
);
910 dynarray_reset(&s1
->cmd_include_files
, &s1
->nb_cmd_include_files
);
912 tcc_free(s1
->tcc_lib_path
);
913 tcc_free(s1
->soname
);
915 tcc_free(s1
->init_symbol
);
916 tcc_free(s1
->fini_symbol
);
917 tcc_free(s1
->outfile
);
918 tcc_free(s1
->deps_outfile
);
919 dynarray_reset(&s1
->files
, &s1
->nb_files
);
920 dynarray_reset(&s1
->target_deps
, &s1
->nb_target_deps
);
921 dynarray_reset(&s1
->pragma_libs
, &s1
->nb_pragma_libs
);
922 dynarray_reset(&s1
->argv
, &s1
->argc
);
925 /* free runtime memory */
930 if (0 == --nb_states
)
934 LIBTCCAPI
int tcc_set_output_type(TCCState
*s
, int output_type
)
936 s
->output_type
= output_type
;
938 /* always elf for objects */
939 if (output_type
== TCC_OUTPUT_OBJ
)
940 s
->output_format
= TCC_OUTPUT_FORMAT_ELF
;
942 if (s
->char_is_unsigned
)
943 tcc_define_symbol(s
, "__CHAR_UNSIGNED__", NULL
);
946 /* default include paths */
947 /* -isystem paths have already been handled */
948 tcc_add_sysinclude_path(s
, CONFIG_TCC_SYSINCLUDEPATHS
);
951 #ifdef CONFIG_TCC_BCHECK
952 if (s
->do_bounds_check
) {
953 /* if bound checking, then add corresponding sections */
954 tccelf_bounds_new(s
);
956 tcc_define_symbol(s
, "__BOUNDS_CHECKING_ON", NULL
);
960 /* add debug sections */
964 tcc_add_library_path(s
, CONFIG_TCC_LIBPATHS
);
968 if (!s
->nostdlib
&& output_type
!= TCC_OUTPUT_OBJ
)
969 tcc_add_systemdir(s
);
972 /* paths for crt objects */
973 tcc_split_path(s
, &s
->crt_paths
, &s
->nb_crt_paths
, CONFIG_TCC_CRTPREFIX
);
974 /* add libc crt1/crti objects */
975 if ((output_type
== TCC_OUTPUT_EXE
|| output_type
== TCC_OUTPUT_DLL
) &&
977 if (output_type
!= TCC_OUTPUT_DLL
)
978 tcc_add_crt(s
, "crt1.o");
979 tcc_add_crt(s
, "crti.o");
985 LIBTCCAPI
int tcc_add_include_path(TCCState
*s
, const char *pathname
)
987 tcc_split_path(s
, &s
->include_paths
, &s
->nb_include_paths
, pathname
);
991 LIBTCCAPI
int tcc_add_sysinclude_path(TCCState
*s
, const char *pathname
)
993 tcc_split_path(s
, &s
->sysinclude_paths
, &s
->nb_sysinclude_paths
, pathname
);
997 ST_FUNC
int tcc_add_file_internal(TCCState
*s1
, const char *filename
, int flags
)
1002 ret
= tcc_open(s1
, filename
);
1004 if (flags
& AFF_PRINT_ERROR
)
1005 tcc_error_noabort("file '%s' not found", filename
);
1009 /* update target deps */
1010 dynarray_add(&s1
->target_deps
, &s1
->nb_target_deps
,
1011 tcc_strdup(filename
));
1013 if (flags
& AFF_TYPE_BIN
) {
1018 obj_type
= tcc_object_type(fd
, &ehdr
);
1019 lseek(fd
, 0, SEEK_SET
);
1021 #ifdef TCC_TARGET_MACHO
1022 if (0 == obj_type
&& 0 == strcmp(tcc_fileextension(filename
), ".dylib"))
1023 obj_type
= AFF_BINTYPE_DYN
;
1027 case AFF_BINTYPE_REL
:
1028 ret
= tcc_load_object_file(s1
, fd
, 0);
1030 #ifndef TCC_TARGET_PE
1031 case AFF_BINTYPE_DYN
:
1032 if (s1
->output_type
== TCC_OUTPUT_MEMORY
) {
1034 #ifdef TCC_IS_NATIVE
1035 if (NULL
== dlopen(filename
, RTLD_GLOBAL
| RTLD_LAZY
))
1039 ret
= tcc_load_dll(s1
, fd
, filename
,
1040 (flags
& AFF_REFERENCED_DLL
) != 0);
1044 case AFF_BINTYPE_AR
:
1045 ret
= tcc_load_archive(s1
, fd
, !(flags
& AFF_WHOLE_ARCHIVE
));
1047 #ifdef TCC_TARGET_COFF
1048 case AFF_BINTYPE_C67
:
1049 ret
= tcc_load_coff(s1
, fd
);
1053 #ifdef TCC_TARGET_PE
1054 ret
= pe_load_file(s1
, filename
, fd
);
1056 /* as GNU ld, consider it is an ld script if not recognized */
1057 ret
= tcc_load_ldscript(s1
);
1060 tcc_error_noabort("unrecognized file type");
1064 ret
= tcc_compile(s1
, flags
);
1070 LIBTCCAPI
int tcc_add_file(TCCState
*s
, const char *filename
)
1072 int filetype
= s
->filetype
;
1073 if (0 == (filetype
& AFF_TYPE_MASK
)) {
1074 /* use a file extension to detect a filetype */
1075 const char *ext
= tcc_fileextension(filename
);
1078 if (!strcmp(ext
, "S"))
1079 filetype
= AFF_TYPE_ASMPP
;
1080 else if (!strcmp(ext
, "s"))
1081 filetype
= AFF_TYPE_ASM
;
1082 else if (!PATHCMP(ext
, "c") || !PATHCMP(ext
, "i"))
1083 filetype
= AFF_TYPE_C
;
1085 filetype
|= AFF_TYPE_BIN
;
1087 filetype
= AFF_TYPE_C
;
1090 return tcc_add_file_internal(s
, filename
, filetype
| AFF_PRINT_ERROR
);
1093 LIBTCCAPI
int tcc_add_library_path(TCCState
*s
, const char *pathname
)
1095 tcc_split_path(s
, &s
->library_paths
, &s
->nb_library_paths
, pathname
);
1099 static int tcc_add_library_internal(TCCState
*s
, const char *fmt
,
1100 const char *filename
, int flags
, char **paths
, int nb_paths
)
1105 for(i
= 0; i
< nb_paths
; i
++) {
1106 snprintf(buf
, sizeof(buf
), fmt
, paths
[i
], filename
);
1107 if (tcc_add_file_internal(s
, buf
, flags
| AFF_TYPE_BIN
) == 0)
1113 /* find and load a dll. Return non zero if not found */
1114 /* XXX: add '-rpath' option support ? */
1115 ST_FUNC
int tcc_add_dll(TCCState
*s
, const char *filename
, int flags
)
1117 return tcc_add_library_internal(s
, "%s/%s", filename
, flags
,
1118 s
->library_paths
, s
->nb_library_paths
);
1121 ST_FUNC
int tcc_add_crt(TCCState
*s
, const char *filename
)
1123 if (-1 == tcc_add_library_internal(s
, "%s/%s",
1124 filename
, 0, s
->crt_paths
, s
->nb_crt_paths
))
1125 tcc_error_noabort("file '%s' not found", filename
);
1129 /* the library name is the same as the argument of the '-l' option */
1130 LIBTCCAPI
int tcc_add_library(TCCState
*s
, const char *libraryname
)
1132 #if defined TCC_TARGET_PE
1133 const char *libs
[] = { "%s/%s.def", "%s/lib%s.def", "%s/%s.dll", "%s/lib%s.dll", "%s/lib%s.a", NULL
};
1134 const char **pp
= s
->static_link
? libs
+ 4 : libs
;
1135 #elif defined TCC_TARGET_MACHO
1136 const char *libs
[] = { "%s/lib%s.dylib", "%s/lib%s.a", NULL
};
1137 const char **pp
= s
->static_link
? libs
+ 1 : libs
;
1139 const char *libs
[] = { "%s/lib%s.so", "%s/lib%s.a", NULL
};
1140 const char **pp
= s
->static_link
? libs
+ 1 : libs
;
1142 int flags
= s
->filetype
& AFF_WHOLE_ARCHIVE
;
1144 if (0 == tcc_add_library_internal(s
, *pp
,
1145 libraryname
, flags
, s
->library_paths
, s
->nb_library_paths
))
1152 PUB_FUNC
int tcc_add_library_err(TCCState
*s
, const char *libname
)
1154 int ret
= tcc_add_library(s
, libname
);
1156 tcc_error_noabort("library '%s' not found", libname
);
1160 /* handle #pragma comment(lib,) */
1161 ST_FUNC
void tcc_add_pragma_libs(TCCState
*s1
)
1164 for (i
= 0; i
< s1
->nb_pragma_libs
; i
++)
1165 tcc_add_library_err(s1
, s1
->pragma_libs
[i
]);
1168 LIBTCCAPI
int tcc_add_symbol(TCCState
*s
, const char *name
, const void *val
)
1170 #ifdef TCC_TARGET_PE
1171 /* On x86_64 'val' might not be reachable with a 32bit offset.
1172 So it is handled here as if it were in a DLL. */
1173 pe_putimport(s
, 0, name
, (uintptr_t)val
);
1175 set_elf_sym(symtab_section
, (uintptr_t)val
, 0,
1176 ELFW(ST_INFO
)(STB_GLOBAL
, STT_NOTYPE
), 0,
1182 LIBTCCAPI
void tcc_set_lib_path(TCCState
*s
, const char *path
)
1184 tcc_free(s
->tcc_lib_path
);
1185 s
->tcc_lib_path
= tcc_strdup(path
);
1188 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
1189 #define FD_INVERT 0x0002 /* invert value before storing */
1191 typedef struct FlagDef
{
1197 static int no_flag(const char **pp
)
1199 const char *p
= *pp
;
1200 if (*p
!= 'n' || *++p
!= 'o' || *++p
!= '-')
1206 ST_FUNC
int set_flag(TCCState
*s
, const FlagDef
*flags
, const char *name
)
1217 for (ret
= -1, p
= flags
; p
->name
; ++p
) {
1219 if (strcmp(r
, p
->name
))
1222 if (0 == (p
->flags
& WD_ALL
))
1226 *(int*)((char *)s
+ p
->offset
) =
1227 p
->flags
& FD_INVERT
? !value
: value
;
1237 static int strstart(const char *val
, const char **str
)
1252 /* Like strstart, but automatically takes into account that ld options can
1254 * - start with double or single dash (e.g. '--soname' or '-soname')
1255 * - arguments can be given as separate or after '=' (e.g. '-Wl,-soname,x.so'
1256 * or '-Wl,-soname=x.so')
1258 * you provide `val` always in 'option[=]' form (no leading -)
1260 static int link_option(const char *str
, const char *val
, const char **ptr
)
1265 /* there should be 1 or 2 dashes */
1271 /* then str & val should match (potentially up to '=') */
1282 while (*q
!= '\0' && *q
!= '=') {
1289 /* '=' near eos means ',' or '=' is ok */
1293 if (*p
!= ',' && *p
!= '=')
1303 static const char *skip_linker_arg(const char **str
)
1305 const char *s1
= *str
;
1306 const char *s2
= strchr(s1
, ',');
1307 *str
= s2
? s2
++ : (s2
= s1
+ strlen(s1
));
1311 static void copy_linker_arg(char **pp
, const char *s
, int sep
)
1317 p
[l
= strlen(p
)] = sep
, ++l
;
1318 skip_linker_arg(&q
);
1319 pstrncpy(l
+ (*pp
= tcc_realloc(p
, q
- s
+ l
+ 1)), s
, q
- s
);
1322 /* set linker options */
1323 static int tcc_set_linker(TCCState
*s
, const char *option
)
1327 const char *p
= NULL
;
1332 if (link_option(option
, "Bsymbolic", &p
)) {
1334 } else if (link_option(option
, "nostdlib", &p
)) {
1336 } else if (link_option(option
, "fini=", &p
)) {
1337 copy_linker_arg(&s
->fini_symbol
, p
, 0);
1339 } else if (link_option(option
, "image-base=", &p
)
1340 || link_option(option
, "Ttext=", &p
)) {
1341 s
->text_addr
= strtoull(p
, &end
, 16);
1342 s
->has_text_addr
= 1;
1343 } else if (link_option(option
, "init=", &p
)) {
1344 copy_linker_arg(&s
->init_symbol
, p
, 0);
1346 } else if (link_option(option
, "oformat=", &p
)) {
1347 #if defined(TCC_TARGET_PE)
1348 if (strstart("pe-", &p
)) {
1350 if (strstart("elf64-", &p
)) {
1352 if (strstart("elf32-", &p
)) {
1354 s
->output_format
= TCC_OUTPUT_FORMAT_ELF
;
1355 } else if (!strcmp(p
, "binary")) {
1356 s
->output_format
= TCC_OUTPUT_FORMAT_BINARY
;
1357 #ifdef TCC_TARGET_COFF
1358 } else if (!strcmp(p
, "coff")) {
1359 s
->output_format
= TCC_OUTPUT_FORMAT_COFF
;
1364 } else if (link_option(option
, "as-needed", &p
)) {
1366 } else if (link_option(option
, "O", &p
)) {
1368 } else if (link_option(option
, "export-all-symbols", &p
)) {
1370 } else if (link_option(option
, "export-dynamic", &p
)) {
1372 } else if (link_option(option
, "rpath=", &p
)) {
1373 copy_linker_arg(&s
->rpath
, p
, ':');
1374 } else if (link_option(option
, "enable-new-dtags", &p
)) {
1375 s
->enable_new_dtags
= 1;
1376 } else if (link_option(option
, "section-alignment=", &p
)) {
1377 s
->section_align
= strtoul(p
, &end
, 16);
1378 } else if (link_option(option
, "soname=", &p
)) {
1379 copy_linker_arg(&s
->soname
, p
, 0);
1380 #ifdef TCC_TARGET_PE
1381 } else if (link_option(option
, "large-address-aware", &p
)) {
1382 s
->pe_characteristics
|= 0x20;
1383 } else if (link_option(option
, "file-alignment=", &p
)) {
1384 s
->pe_file_align
= strtoul(p
, &end
, 16);
1385 } else if (link_option(option
, "stack=", &p
)) {
1386 s
->pe_stack_size
= strtoul(p
, &end
, 10);
1387 } else if (link_option(option
, "subsystem=", &p
)) {
1388 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1389 if (!strcmp(p
, "native")) {
1390 s
->pe_subsystem
= 1;
1391 } else if (!strcmp(p
, "console")) {
1392 s
->pe_subsystem
= 3;
1393 } else if (!strcmp(p
, "gui") || !strcmp(p
, "windows")) {
1394 s
->pe_subsystem
= 2;
1395 } else if (!strcmp(p
, "posix")) {
1396 s
->pe_subsystem
= 7;
1397 } else if (!strcmp(p
, "efiapp")) {
1398 s
->pe_subsystem
= 10;
1399 } else if (!strcmp(p
, "efiboot")) {
1400 s
->pe_subsystem
= 11;
1401 } else if (!strcmp(p
, "efiruntime")) {
1402 s
->pe_subsystem
= 12;
1403 } else if (!strcmp(p
, "efirom")) {
1404 s
->pe_subsystem
= 13;
1405 #elif defined(TCC_TARGET_ARM)
1406 if (!strcmp(p
, "wince")) {
1407 s
->pe_subsystem
= 9;
1412 } else if (ret
= link_option(option
, "?whole-archive", &p
), ret
) {
1414 s
->filetype
|= AFF_WHOLE_ARCHIVE
;
1416 s
->filetype
&= ~AFF_WHOLE_ARCHIVE
;
1421 tcc_error("unsupported linker option '%s'", option
);
1424 if (ignoring
&& s
->warn_unsupported
)
1425 tcc_warning("unsupported linker option '%s'", option
);
1427 option
= skip_linker_arg(&p
);
1432 typedef struct TCCOption
{
1454 TCC_OPTION_dumpversion
,
1463 TCC_OPTION_traditional
,
1468 TCC_OPTION_mfloat_abi
,
1472 TCC_OPTION_iwithprefix
,
1474 TCC_OPTION_nostdinc
,
1475 TCC_OPTION_nostdlib
,
1476 TCC_OPTION_print_search_dirs
,
1477 TCC_OPTION_rdynamic
,
1479 TCC_OPTION_pedantic
,
1492 #define TCC_OPTION_HAS_ARG 0x0001
1493 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
1495 static const TCCOption tcc_options
[] = {
1496 { "h", TCC_OPTION_HELP
, 0 },
1497 { "-help", TCC_OPTION_HELP
, 0 },
1498 { "?", TCC_OPTION_HELP
, 0 },
1499 { "hh", TCC_OPTION_HELP2
, 0 },
1500 { "v", TCC_OPTION_v
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1501 { "I", TCC_OPTION_I
, TCC_OPTION_HAS_ARG
},
1502 { "D", TCC_OPTION_D
, TCC_OPTION_HAS_ARG
},
1503 { "U", TCC_OPTION_U
, TCC_OPTION_HAS_ARG
},
1504 { "P", TCC_OPTION_P
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1505 { "L", TCC_OPTION_L
, TCC_OPTION_HAS_ARG
},
1506 { "B", TCC_OPTION_B
, TCC_OPTION_HAS_ARG
},
1507 { "l", TCC_OPTION_l
, TCC_OPTION_HAS_ARG
},
1508 { "bench", TCC_OPTION_bench
, 0 },
1509 #ifdef CONFIG_TCC_BACKTRACE
1510 { "bt", TCC_OPTION_bt
, TCC_OPTION_HAS_ARG
},
1512 #ifdef CONFIG_TCC_BCHECK
1513 { "b", TCC_OPTION_b
, 0 },
1515 { "g", TCC_OPTION_g
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1516 { "c", TCC_OPTION_c
, 0 },
1517 { "dumpversion", TCC_OPTION_dumpversion
, 0},
1518 { "d", TCC_OPTION_d
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1519 { "static", TCC_OPTION_static
, 0 },
1520 { "std", TCC_OPTION_std
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1521 { "shared", TCC_OPTION_shared
, 0 },
1522 { "soname", TCC_OPTION_soname
, TCC_OPTION_HAS_ARG
},
1523 { "o", TCC_OPTION_o
, TCC_OPTION_HAS_ARG
},
1524 { "-param", TCC_OPTION_param
, TCC_OPTION_HAS_ARG
},
1525 { "pedantic", TCC_OPTION_pedantic
, 0},
1526 { "pthread", TCC_OPTION_pthread
, 0},
1527 { "run", TCC_OPTION_run
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1528 { "rdynamic", TCC_OPTION_rdynamic
, 0 },
1529 { "r", TCC_OPTION_r
, 0 },
1530 { "s", TCC_OPTION_s
, 0 },
1531 { "traditional", TCC_OPTION_traditional
, 0 },
1532 { "Wl,", TCC_OPTION_Wl
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1533 { "Wp,", TCC_OPTION_Wp
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1534 { "W", TCC_OPTION_W
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1535 { "O", TCC_OPTION_O
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1536 #ifdef TCC_TARGET_ARM
1537 { "mfloat-abi", TCC_OPTION_mfloat_abi
, TCC_OPTION_HAS_ARG
},
1539 { "m", TCC_OPTION_m
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1540 { "f", TCC_OPTION_f
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1541 { "isystem", TCC_OPTION_isystem
, TCC_OPTION_HAS_ARG
},
1542 { "include", TCC_OPTION_include
, TCC_OPTION_HAS_ARG
},
1543 { "nostdinc", TCC_OPTION_nostdinc
, 0 },
1544 { "nostdlib", TCC_OPTION_nostdlib
, 0 },
1545 { "print-search-dirs", TCC_OPTION_print_search_dirs
, 0 },
1546 { "w", TCC_OPTION_w
, 0 },
1547 { "pipe", TCC_OPTION_pipe
, 0},
1548 { "E", TCC_OPTION_E
, 0},
1549 { "MD", TCC_OPTION_MD
, 0},
1550 { "MF", TCC_OPTION_MF
, TCC_OPTION_HAS_ARG
},
1551 { "x", TCC_OPTION_x
, TCC_OPTION_HAS_ARG
},
1552 { "ar", TCC_OPTION_ar
, 0},
1553 #ifdef TCC_TARGET_PE
1554 { "impdef", TCC_OPTION_impdef
, 0},
1559 static const FlagDef options_W
[] = {
1561 { offsetof(TCCState
, warn_unsupported
), 0, "unsupported" },
1562 { offsetof(TCCState
, warn_write_strings
), 0, "write-strings" },
1563 { offsetof(TCCState
, warn_error
), 0, "error" },
1564 { offsetof(TCCState
, warn_gcc_compat
), 0, "gcc-compat" },
1565 { offsetof(TCCState
, warn_implicit_function_declaration
), WD_ALL
,
1566 "implicit-function-declaration" },
1570 static const FlagDef options_f
[] = {
1571 { offsetof(TCCState
, char_is_unsigned
), 0, "unsigned-char" },
1572 { offsetof(TCCState
, char_is_unsigned
), FD_INVERT
, "signed-char" },
1573 { offsetof(TCCState
, nocommon
), FD_INVERT
, "common" },
1574 { offsetof(TCCState
, leading_underscore
), 0, "leading-underscore" },
1575 { offsetof(TCCState
, ms_extensions
), 0, "ms-extensions" },
1576 { offsetof(TCCState
, dollars_in_identifiers
), 0, "dollars-in-identifiers" },
1580 static const FlagDef options_m
[] = {
1581 { offsetof(TCCState
, ms_bitfields
), 0, "ms-bitfields" },
1582 #ifdef TCC_TARGET_X86_64
1583 { offsetof(TCCState
, nosse
), FD_INVERT
, "sse" },
1588 static void parse_option_D(TCCState
*s1
, const char *optarg
)
1590 char *sym
= tcc_strdup(optarg
);
1591 char *value
= strchr(sym
, '=');
1594 tcc_define_symbol(s1
, sym
, value
);
1598 static void args_parser_add_file(TCCState
*s
, const char* filename
, int filetype
)
1600 struct filespec
*f
= tcc_malloc(sizeof *f
+ strlen(filename
));
1602 strcpy(f
->name
, filename
);
1603 dynarray_add(&s
->files
, &s
->nb_files
, f
);
1606 static int args_parser_make_argv(const char *r
, int *argc
, char ***argv
)
1611 while (c
= (unsigned char)*r
, c
&& c
<= ' ')
1617 while (c
= (unsigned char)*r
, c
) {
1619 if (c
== '\\' && (*r
== '"' || *r
== '\\')) {
1621 } else if (c
== '"') {
1624 } else if (q
== 0 && c
<= ' ') {
1630 //printf("<%s>\n", str.data), fflush(stdout);
1631 dynarray_add(argv
, argc
, tcc_strdup(str
.data
));
1638 /* read list file */
1639 static void args_parser_listfile(TCCState
*s
,
1640 const char *filename
, int optind
, int *pargc
, char ***pargv
)
1648 fd
= open(filename
, O_RDONLY
| O_BINARY
);
1650 tcc_error("listfile '%s' not found", filename
);
1652 len
= lseek(fd
, 0, SEEK_END
);
1653 p
= tcc_malloc(len
+ 1), p
[len
] = 0;
1654 lseek(fd
, 0, SEEK_SET
), read(fd
, p
, len
), close(fd
);
1656 for (i
= 0; i
< *pargc
; ++i
)
1658 args_parser_make_argv(p
, &argc
, &argv
);
1660 dynarray_add(&argv
, &argc
, tcc_strdup((*pargv
)[i
]));
1663 dynarray_reset(&s
->argv
, &s
->argc
);
1664 *pargc
= s
->argc
= argc
, *pargv
= s
->argv
= argv
;
1667 PUB_FUNC
int tcc_parse_args(TCCState
*s
, int *pargc
, char ***pargv
, int optind
)
1669 const TCCOption
*popt
;
1670 const char *optarg
, *r
;
1671 const char *run
= NULL
;
1674 CString linker_arg
; /* collect -Wl options */
1675 int tool
= 0, arg_start
= 0, noaction
= optind
;
1676 char **argv
= *pargv
;
1679 cstr_new(&linker_arg
);
1681 while (optind
< argc
) {
1683 if (r
[0] == '@' && r
[1] != '\0') {
1684 args_parser_listfile(s
, r
+ 1, optind
, &argc
, &argv
);
1689 if (r
[0] == '-' && r
[1] == 'v' && r
[2] == 0)
1694 if (r
[0] != '-' || r
[1] == '\0') {
1695 if (r
[0] != '@') /* allow "tcc file(s) -run @ args ..." */
1696 args_parser_add_file(s
, r
, s
->filetype
);
1698 tcc_set_options(s
, run
);
1699 arg_start
= optind
- 1;
1705 /* find option in table */
1706 for(popt
= tcc_options
; ; ++popt
) {
1707 const char *p1
= popt
->name
;
1708 const char *r1
= r
+ 1;
1710 tcc_error("invalid option -- '%s'", r
);
1711 if (!strstart(p1
, &r1
))
1714 if (popt
->flags
& TCC_OPTION_HAS_ARG
) {
1715 if (*r1
== '\0' && !(popt
->flags
& TCC_OPTION_NOSEP
)) {
1718 tcc_error("argument to '%s' is missing", r
);
1719 optarg
= argv
[optind
++];
1721 } else if (*r1
!= '\0')
1726 switch(popt
->index
) {
1727 case TCC_OPTION_HELP
:
1729 case TCC_OPTION_HELP2
:
1732 tcc_add_include_path(s
, optarg
);
1735 parse_option_D(s
, optarg
);
1738 tcc_undefine_symbol(s
, optarg
);
1741 tcc_add_library_path(s
, optarg
);
1744 /* set tcc utilities path (mainly for tcc development) */
1745 tcc_set_lib_path(s
, optarg
);
1748 args_parser_add_file(s
, optarg
, AFF_TYPE_LIB
| (s
->filetype
& ~AFF_TYPE_MASK
));
1751 case TCC_OPTION_pthread
:
1752 parse_option_D(s
, "_REENTRANT");
1753 s
->option_pthread
= 1;
1755 case TCC_OPTION_bench
:
1758 #ifdef CONFIG_TCC_BACKTRACE
1760 tcc_set_num_callers(atoi(optarg
));
1763 #ifdef CONFIG_TCC_BCHECK
1765 s
->do_bounds_check
= 1;
1776 tcc_warning("-%s: overriding compiler action already specified", popt
->name
);
1782 else if (*optarg
== 'M')
1784 else if (*optarg
== 't')
1786 else if (isnum(*optarg
))
1787 g_debug
= atoi(optarg
);
1789 goto unsupported_option
;
1791 case TCC_OPTION_static
:
1794 case TCC_OPTION_std
:
1795 if (*optarg
== '=') {
1796 if (strcmp(optarg
, "=c11") == 0) {
1797 tcc_undefine_symbol(s
, "__STDC_VERSION__");
1798 tcc_define_symbol(s
, "__STDC_VERSION__", "201112L");
1800 * The integer constant 1, intended to indicate
1801 * that the implementation does not support atomic
1802 * types (including the _Atomic type qualifier) and
1803 * the <stdatomic.h> header.
1805 tcc_define_symbol(s
, "__STDC_NO_ATOMICS__", "1");
1807 * The integer constant 1, intended to indicate
1808 * that the implementation does not support complex
1809 * types or the <complex.h> header.
1811 tcc_define_symbol(s
, "__STDC_NO_COMPLEX__", "1");
1813 * The integer constant 1, intended to indicate
1814 * that the implementation does not support the
1815 * <threads.h> header.
1817 tcc_define_symbol(s
, "__STDC_NO_THREADS__", "1");
1819 * __STDC_NO_VLA__, tcc supports VLA.
1820 * The integer constant 1, intended to indicate
1821 * that the implementation does not support
1822 * variable length arrays or variably modified
1825 #if !defined(TCC_TARGET_PE)
1827 * An integer constant of the form yyyymmL (for
1828 * example, 199712L). If this symbol is defined,
1829 * then every character in the Unicode required
1830 * set, when stored in an object of type
1831 * wchar_t, has the same value as the short
1832 * identifier of that character.
1835 /* on Linux, this conflicts with a define introduced by
1836 * /usr/include/stdc-predef.h included by glibc libs;
1837 * clang doesn't define it at all so it's probably not necessary
1839 tcc_define_symbol(s
, "__STDC_ISO_10646__", "201605L");
1842 * The integer constant 1, intended to indicate
1843 * that values of type char16_t are UTF−16
1844 * encoded. If some other encoding is used, the
1845 * macro shall not be defined and the actual
1846 * encoding used is implementation defined.
1848 tcc_define_symbol(s
, "__STDC_UTF_16__", "1");
1850 * The integer constant 1, intended to indicate
1851 * that values of type char32_t are UTF−32
1852 * encoded. If some other encoding is used, the
1853 * macro shall not be defined and the actual
1854 * encoding used is implementationdefined.
1856 tcc_define_symbol(s
, "__STDC_UTF_32__", "1");
1857 #endif /* !TCC_TARGET_PE */
1858 s
->cversion
= 201112;
1862 * silently ignore other values, a current purpose:
1863 * allow to use a tcc as a reference compiler for "make test"
1866 case TCC_OPTION_shared
:
1868 goto set_output_type
;
1869 case TCC_OPTION_soname
:
1870 s
->soname
= tcc_strdup(optarg
);
1874 tcc_warning("multiple -o option");
1875 tcc_free(s
->outfile
);
1877 s
->outfile
= tcc_strdup(optarg
);
1880 /* generate a .o merging several output files */
1883 goto set_output_type
;
1884 case TCC_OPTION_isystem
:
1885 tcc_add_sysinclude_path(s
, optarg
);
1887 case TCC_OPTION_include
:
1888 dynarray_add(&s
->cmd_include_files
,
1889 &s
->nb_cmd_include_files
, tcc_strdup(optarg
));
1891 case TCC_OPTION_nostdinc
:
1894 case TCC_OPTION_nostdlib
:
1897 case TCC_OPTION_run
:
1898 #ifndef TCC_IS_NATIVE
1899 tcc_error("-run is not available in a cross compiler");
1902 x
= TCC_OUTPUT_MEMORY
;
1903 goto set_output_type
;
1905 do ++s
->verbose
; while (*optarg
++ == 'v');
1909 if (set_flag(s
, options_f
, optarg
) < 0)
1910 goto unsupported_option
;
1912 #ifdef TCC_TARGET_ARM
1913 case TCC_OPTION_mfloat_abi
:
1914 /* tcc doesn't support soft float yet */
1915 if (!strcmp(optarg
, "softfp")) {
1916 s
->float_abi
= ARM_SOFTFP_FLOAT
;
1917 tcc_undefine_symbol(s
, "__ARM_PCS_VFP");
1918 } else if (!strcmp(optarg
, "hard"))
1919 s
->float_abi
= ARM_HARD_FLOAT
;
1921 tcc_error("unsupported float abi '%s'", optarg
);
1925 if (set_flag(s
, options_m
, optarg
) < 0) {
1926 if (x
= atoi(optarg
), x
!= 32 && x
!= 64)
1927 goto unsupported_option
;
1928 if (PTR_SIZE
!= x
/8)
1934 if (set_flag(s
, options_W
, optarg
) < 0)
1935 goto unsupported_option
;
1940 case TCC_OPTION_rdynamic
:
1944 if (linker_arg
.size
)
1945 --linker_arg
.size
, cstr_ccat(&linker_arg
, ',');
1946 cstr_cat(&linker_arg
, optarg
, 0);
1947 if (tcc_set_linker(s
, linker_arg
.data
))
1948 cstr_free(&linker_arg
);
1954 x
= TCC_OUTPUT_PREPROCESS
;
1955 goto set_output_type
;
1957 s
->Pflag
= atoi(optarg
) + 1;
1963 s
->deps_outfile
= tcc_strdup(optarg
);
1965 case TCC_OPTION_dumpversion
:
1966 printf ("%s\n", TCC_VERSION
);
1973 else if (*optarg
== 'a')
1975 else if (*optarg
== 'b')
1977 else if (*optarg
== 'n')
1980 tcc_warning("unsupported language '%s'", optarg
);
1981 s
->filetype
= x
| (s
->filetype
& ~AFF_TYPE_MASK
);
1984 last_o
= atoi(optarg
);
1986 case TCC_OPTION_print_search_dirs
:
1989 case TCC_OPTION_impdef
:
1995 arg_start
= optind
- 1;
1996 if (arg_start
!= noaction
)
1997 tcc_error("cannot parse %s here", r
);
2000 case TCC_OPTION_traditional
:
2001 case TCC_OPTION_pedantic
:
2002 case TCC_OPTION_pipe
:
2008 if (s
->warn_unsupported
)
2009 tcc_warning("unsupported option '%s'", r
);
2014 tcc_define_symbol(s
, "__OPTIMIZE__", NULL
);
2015 if (linker_arg
.size
) {
2016 r
= linker_arg
.data
;
2019 *pargc
= argc
- arg_start
;
2020 *pargv
= argv
+ arg_start
;
2023 if (optind
!= noaction
)
2025 if (s
->verbose
== 2)
2026 return OPT_PRINT_DIRS
;
2032 LIBTCCAPI
void tcc_set_options(TCCState
*s
, const char *r
)
2036 args_parser_make_argv(r
, &argc
, &argv
);
2037 tcc_parse_args(s
, &argc
, &argv
, 0);
2038 dynarray_reset(&argv
, &argc
);
2041 PUB_FUNC
void tcc_print_stats(TCCState
*s
, unsigned total_time
)
2045 if (total_bytes
< 1)
2047 fprintf(stderr
, "* %d idents, %d lines, %d bytes\n"
2048 "* %0.3f s, %u lines/s, %0.1f MB/s\n",
2049 tok_ident
- TOK_IDENT
, total_lines
, total_bytes
,
2050 (double)total_time
/1000,
2051 (unsigned)total_lines
*1000/total_time
,
2052 (double)total_bytes
/1000/total_time
);
2054 fprintf(stderr
, "* %d bytes memory used\n", mem_max_size
);