2 * TCC - Tiny C Compiler
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #if !defined ONE_SOURCE || ONE_SOURCE
26 #ifdef TCC_TARGET_I386
28 #include "i386-link.c"
30 #elif defined(TCC_TARGET_ARM)
34 #elif defined(TCC_TARGET_ARM64)
35 #include "arm64-gen.c"
36 #include "arm64-link.c"
38 #elif defined(TCC_TARGET_C67)
42 #elif defined(TCC_TARGET_X86_64)
43 #include "x86_64-gen.c"
44 #include "x86_64-link.c"
46 #elif defined(TCC_TARGET_RISCV64)
47 #include "riscv64-gen.c"
48 #include "riscv64-link.c"
49 #include "riscv64-asm.c"
59 #endif /* ONE_SOURCE */
63 /********************************************************/
64 /* global variables */
66 /* XXX: get rid of this ASAP (or maybe not) */
67 ST_DATA
struct TCCState
*tcc_state
;
73 /********************************************************/
75 ST_FUNC
char *normalize_slashes(char *path
)
78 for (p
= path
; *p
; ++p
)
84 static HMODULE tcc_module
;
86 /* on win32, we suppose the lib and includes are at the location of 'tcc.exe' */
87 static void tcc_set_lib_path_w32(TCCState
*s
)
90 GetModuleFileNameA(tcc_module
, path
, sizeof path
);
91 p
= tcc_basename(normalize_slashes(strlwr(path
)));
95 tcc_set_lib_path(s
, path
);
99 static void tcc_add_systemdir(TCCState
*s
)
102 GetSystemDirectory(buf
, sizeof buf
);
103 tcc_add_library_path(s
, normalize_slashes(buf
));
108 BOOL WINAPI
DllMain (HINSTANCE hDll
, DWORD dwReason
, LPVOID lpReserved
)
110 if (DLL_PROCESS_ATTACH
== dwReason
)
117 /********************************************************/
118 #ifndef CONFIG_TCC_SEMLOCK
122 static int tcc_sem_init
;
123 static CRITICAL_SECTION tcc_cr
;
124 static void wait_sem(void)
127 InitializeCriticalSection(&tcc_cr
), tcc_sem_init
= 1;
128 EnterCriticalSection(&tcc_cr
);
130 #define WAIT_SEM() wait_sem()
131 #define POST_SEM() LeaveCriticalSection(&tcc_cr);
133 #include <semaphore.h>
134 static int tcc_sem_init
;
135 static sem_t tcc_sem
;
136 static void wait_sem(void)
139 sem_init(&tcc_sem
, 0, 1), tcc_sem_init
= 1;
140 while (sem_wait (&tcc_sem
) < 0 && errno
== EINTR
);
142 #define WAIT_SEM() wait_sem()
143 #define POST_SEM() sem_post(&tcc_sem)
146 /********************************************************/
147 /* copy a string and truncate it. */
148 ST_FUNC
char *pstrcpy(char *buf
, size_t buf_size
, const char *s
)
155 q_end
= buf
+ buf_size
- 1;
167 /* strcat and truncate. */
168 ST_FUNC
char *pstrcat(char *buf
, size_t buf_size
, const char *s
)
173 pstrcpy(buf
+ len
, buf_size
- len
, s
);
177 ST_FUNC
char *pstrncpy(char *out
, const char *in
, size_t num
)
179 memcpy(out
, in
, num
);
184 /* extract the basename of a file */
185 PUB_FUNC
char *tcc_basename(const char *name
)
187 char *p
= strchr(name
, 0);
188 while (p
> name
&& !IS_DIRSEP(p
[-1]))
193 /* extract extension part of a file
195 * (if no extension, return pointer to end-of-string)
197 PUB_FUNC
char *tcc_fileextension (const char *name
)
199 char *b
= tcc_basename(name
);
200 char *e
= strrchr(b
, '.');
201 return e
? e
: strchr(b
, 0);
204 /********************************************************/
205 /* memory management */
213 PUB_FUNC
void tcc_free(void *ptr
)
218 PUB_FUNC
void *tcc_malloc(unsigned long size
)
223 _tcc_error("memory full (malloc)");
227 PUB_FUNC
void *tcc_mallocz(unsigned long size
)
230 ptr
= tcc_malloc(size
);
231 memset(ptr
, 0, size
);
235 PUB_FUNC
void *tcc_realloc(void *ptr
, unsigned long size
)
238 ptr1
= realloc(ptr
, size
);
240 _tcc_error("memory full (realloc)");
244 PUB_FUNC
char *tcc_strdup(const char *str
)
247 ptr
= tcc_malloc(strlen(str
) + 1);
254 #define MEM_DEBUG_MAGIC1 0xFEEDDEB1
255 #define MEM_DEBUG_MAGIC2 0xFEEDDEB2
256 #define MEM_DEBUG_MAGIC3 0xFEEDDEB3
257 #define MEM_DEBUG_FILE_LEN 40
258 #define MEM_DEBUG_CHECK3(header) \
259 ((mem_debug_header_t*)((char*)header + header->size))->magic3
260 #define MEM_USER_PTR(header) \
261 ((char *)header + offsetof(mem_debug_header_t, magic3))
262 #define MEM_HEADER_PTR(ptr) \
263 (mem_debug_header_t *)((char*)ptr - offsetof(mem_debug_header_t, magic3))
265 struct mem_debug_header
{
268 struct mem_debug_header
*prev
;
269 struct mem_debug_header
*next
;
271 char file_name
[MEM_DEBUG_FILE_LEN
+ 1];
273 ALIGNED(16) unsigned magic3
;
276 typedef struct mem_debug_header mem_debug_header_t
;
278 static mem_debug_header_t
*mem_debug_chain
;
279 static unsigned mem_cur_size
;
280 static unsigned mem_max_size
;
282 static mem_debug_header_t
*malloc_check(void *ptr
, const char *msg
)
284 mem_debug_header_t
* header
= MEM_HEADER_PTR(ptr
);
285 if (header
->magic1
!= MEM_DEBUG_MAGIC1
||
286 header
->magic2
!= MEM_DEBUG_MAGIC2
||
287 MEM_DEBUG_CHECK3(header
) != MEM_DEBUG_MAGIC3
||
288 header
->size
== (unsigned)-1) {
289 fprintf(stderr
, "%s check failed\n", msg
);
290 if (header
->magic1
== MEM_DEBUG_MAGIC1
)
291 fprintf(stderr
, "%s:%u: block allocated here.\n",
292 header
->file_name
, header
->line_num
);
298 PUB_FUNC
void *tcc_malloc_debug(unsigned long size
, const char *file
, int line
)
301 mem_debug_header_t
*header
;
303 header
= malloc(sizeof(mem_debug_header_t
) + size
);
305 _tcc_error("memory full (malloc)");
307 header
->magic1
= MEM_DEBUG_MAGIC1
;
308 header
->magic2
= MEM_DEBUG_MAGIC2
;
310 MEM_DEBUG_CHECK3(header
) = MEM_DEBUG_MAGIC3
;
311 header
->line_num
= line
;
312 ofs
= strlen(file
) - MEM_DEBUG_FILE_LEN
;
313 strncpy(header
->file_name
, file
+ (ofs
> 0 ? ofs
: 0), MEM_DEBUG_FILE_LEN
);
314 header
->file_name
[MEM_DEBUG_FILE_LEN
] = 0;
316 header
->next
= mem_debug_chain
;
319 header
->next
->prev
= header
;
320 mem_debug_chain
= header
;
322 mem_cur_size
+= size
;
323 if (mem_cur_size
> mem_max_size
)
324 mem_max_size
= mem_cur_size
;
326 return MEM_USER_PTR(header
);
329 PUB_FUNC
void tcc_free_debug(void *ptr
)
331 mem_debug_header_t
*header
;
334 header
= malloc_check(ptr
, "tcc_free");
335 mem_cur_size
-= header
->size
;
336 header
->size
= (unsigned)-1;
338 header
->next
->prev
= header
->prev
;
340 header
->prev
->next
= header
->next
;
341 if (header
== mem_debug_chain
)
342 mem_debug_chain
= header
->next
;
346 PUB_FUNC
void *tcc_mallocz_debug(unsigned long size
, const char *file
, int line
)
349 ptr
= tcc_malloc_debug(size
,file
,line
);
350 memset(ptr
, 0, size
);
354 PUB_FUNC
void *tcc_realloc_debug(void *ptr
, unsigned long size
, const char *file
, int line
)
356 mem_debug_header_t
*header
;
357 int mem_debug_chain_update
= 0;
359 return tcc_malloc_debug(size
, file
, line
);
360 header
= malloc_check(ptr
, "tcc_realloc");
361 mem_cur_size
-= header
->size
;
362 mem_debug_chain_update
= (header
== mem_debug_chain
);
363 header
= realloc(header
, sizeof(mem_debug_header_t
) + size
);
365 _tcc_error("memory full (realloc)");
367 MEM_DEBUG_CHECK3(header
) = MEM_DEBUG_MAGIC3
;
369 header
->next
->prev
= header
;
371 header
->prev
->next
= header
;
372 if (mem_debug_chain_update
)
373 mem_debug_chain
= header
;
374 mem_cur_size
+= size
;
375 if (mem_cur_size
> mem_max_size
)
376 mem_max_size
= mem_cur_size
;
377 return MEM_USER_PTR(header
);
380 PUB_FUNC
char *tcc_strdup_debug(const char *str
, const char *file
, int line
)
383 ptr
= tcc_malloc_debug(strlen(str
) + 1, file
, line
);
388 PUB_FUNC
void tcc_memcheck(void)
391 mem_debug_header_t
*header
= mem_debug_chain
;
392 fprintf(stderr
, "MEM_DEBUG: mem_leak= %d bytes, mem_max_size= %d bytes\n",
393 mem_cur_size
, mem_max_size
);
395 fprintf(stderr
, "%s:%u: error: %u bytes leaked\n",
396 header
->file_name
, header
->line_num
, header
->size
);
397 header
= header
->next
;
404 #endif /* MEM_DEBUG */
406 #define free(p) use_tcc_free(p)
407 #define malloc(s) use_tcc_malloc(s)
408 #define realloc(p, s) use_tcc_realloc(p, s)
410 /********************************************************/
413 ST_FUNC
void dynarray_add(void *ptab
, int *nb_ptr
, void *data
)
419 pp
= *(void ***)ptab
;
420 /* every power of two we double array size */
421 if ((nb
& (nb
- 1)) == 0) {
426 pp
= tcc_realloc(pp
, nb_alloc
* sizeof(void *));
433 ST_FUNC
void dynarray_reset(void *pp
, int *n
)
436 for (p
= *(void***)pp
; *n
; ++p
, --*n
)
439 tcc_free(*(void**)pp
);
443 static void tcc_split_path(TCCState
*s
, void *p_ary
, int *p_nb_ary
, const char *in
)
451 for (p
= in
; c
= *p
, c
!= '\0' && c
!= PATHSEP
[0]; ++p
) {
452 if (c
== '{' && p
[1] && p
[2] == '}') {
455 cstr_cat(&str
, s
->tcc_lib_path
, -1);
456 if (c
== 'f' && file
) {
457 /* substitute current file's dir */
458 const char *f
= file
->true_filename
;
459 const char *b
= tcc_basename(f
);
461 cstr_cat(&str
, f
, b
- f
- 1);
463 cstr_cat(&str
, ".", 1);
470 cstr_ccat(&str
, '\0');
471 dynarray_add(p_ary
, p_nb_ary
, tcc_strdup(str
.data
));
478 /********************************************************/
480 static void strcat_vprintf(char *buf
, int buf_size
, const char *fmt
, va_list ap
)
484 vsnprintf(buf
+ len
, buf_size
- len
, fmt
, ap
);
487 static void strcat_printf(char *buf
, int buf_size
, const char *fmt
, ...)
491 strcat_vprintf(buf
, buf_size
, fmt
, ap
);
496 #define ERROR_NOABORT 1
497 #define ERROR_ERROR 2
499 PUB_FUNC
void tcc_enter_state(TCCState
*s1
)
505 PUB_FUNC
void tcc_exit_state(void)
511 static void error1(int mode
, const char *fmt
, va_list ap
)
514 BufferedFile
**pf
, *f
;
515 TCCState
*s1
= tcc_state
;
519 /* can happen only if called from tcc_malloc(): 'out of memory' */
522 if (s1
&& !s1
->error_set_jmp_enabled
)
523 /* tcc_state just was set by tcc_enter_state() */
526 if (mode
== ERROR_WARN
) {
534 if (s1
->error_set_jmp_enabled
) { /* we're called while parsing a file */
535 /* use upper file if inline ":asm:" or token ":paste:" */
536 for (f
= file
; f
&& f
->filename
[0] == ':'; f
= f
->prev
)
540 for(pf
= s1
->include_stack
; pf
< s1
->include_stack_ptr
; pf
++)
541 strcat_printf(buf
, sizeof(buf
), "In file included from %s:%d:\n",
542 (*pf
)->filename
, (*pf
)->line_num
);
543 strcat_printf(buf
, sizeof(buf
), "%s:%d: ",
544 f
->filename
, f
->line_num
- !!(tok_flags
& TOK_FLAG_BOL
));
545 } else if (s1
->current_filename
) {
546 strcat_printf(buf
, sizeof(buf
), "%s: ", s1
->current_filename
);
551 strcat_printf(buf
, sizeof(buf
), "tcc: ");
552 if (mode
== ERROR_WARN
)
553 strcat_printf(buf
, sizeof(buf
), "warning: ");
555 strcat_printf(buf
, sizeof(buf
), "error: ");
556 strcat_vprintf(buf
, sizeof(buf
), fmt
, ap
);
557 if (!s1
|| !s1
->error_func
) {
558 /* default case: stderr */
559 if (s1
&& s1
->output_type
== TCC_OUTPUT_PREPROCESS
&& s1
->ppfp
== stdout
)
560 /* print a newline during tcc -E */
561 printf("\n"), fflush(stdout
);
562 fflush(stdout
); /* flush -v output */
563 fprintf(stderr
, "%s\n", buf
);
564 fflush(stderr
); /* print error/warning now (win32) */
566 s1
->error_func(s1
->error_opaque
, buf
);
569 if (mode
!= ERROR_WARN
)
571 if (mode
!= ERROR_ERROR
)
573 if (s1
->error_set_jmp_enabled
)
574 longjmp(s1
->error_jmp_buf
, 1);
579 LIBTCCAPI
void tcc_set_error_func(TCCState
*s
, void *error_opaque
, TCCErrorFunc error_func
)
581 s
->error_opaque
= error_opaque
;
582 s
->error_func
= error_func
;
585 LIBTCCAPI TCCErrorFunc
tcc_get_error_func(TCCState
*s
)
587 return s
->error_func
;
590 LIBTCCAPI
void *tcc_get_error_opaque(TCCState
*s
)
592 return s
->error_opaque
;
595 /* error without aborting current compilation */
596 PUB_FUNC
void _tcc_error_noabort(const char *fmt
, ...)
600 error1(ERROR_NOABORT
, fmt
, ap
);
604 PUB_FUNC
void _tcc_error(const char *fmt
, ...)
608 for (;;) error1(ERROR_ERROR
, fmt
, ap
);
611 PUB_FUNC
void _tcc_warning(const char *fmt
, ...)
615 error1(ERROR_WARN
, fmt
, ap
);
619 /********************************************************/
622 ST_FUNC
void tcc_open_bf(TCCState
*s1
, const char *filename
, int initlen
)
625 int buflen
= initlen
? initlen
: IO_BUF_SIZE
;
627 bf
= tcc_mallocz(sizeof(BufferedFile
) + buflen
);
628 bf
->buf_ptr
= bf
->buffer
;
629 bf
->buf_end
= bf
->buffer
+ initlen
;
630 bf
->buf_end
[0] = CH_EOB
; /* put eob symbol */
631 pstrcpy(bf
->filename
, sizeof(bf
->filename
), filename
);
633 normalize_slashes(bf
->filename
);
635 bf
->true_filename
= bf
->filename
;
637 bf
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
641 tok_flags
= TOK_FLAG_BOL
| TOK_FLAG_BOF
;
644 ST_FUNC
void tcc_close(void)
646 TCCState
*s1
= tcc_state
;
647 BufferedFile
*bf
= file
;
650 total_lines
+= bf
->line_num
;
652 if (bf
->true_filename
!= bf
->filename
)
653 tcc_free(bf
->true_filename
);
658 static int _tcc_open(TCCState
*s1
, const char *filename
)
661 if (strcmp(filename
, "-") == 0)
662 fd
= 0, filename
= "<stdin>";
664 fd
= open(filename
, O_RDONLY
| O_BINARY
);
665 if ((s1
->verbose
== 2 && fd
>= 0) || s1
->verbose
== 3)
666 printf("%s %*s%s\n", fd
< 0 ? "nf":"->",
667 (int)(s1
->include_stack_ptr
- s1
->include_stack
), "", filename
);
671 ST_FUNC
int tcc_open(TCCState
*s1
, const char *filename
)
673 int fd
= _tcc_open(s1
, filename
);
676 tcc_open_bf(s1
, filename
, 0);
681 /* compile the file opened in 'file'. Return non zero if errors. */
682 static int tcc_compile(TCCState
*s1
, int filetype
, const char *str
, int fd
)
684 /* Here we enter the code section where we use the global variables for
685 parsing and code generation (tccpp.c, tccgen.c, <target>-gen.c).
686 Other threads need to wait until we're done.
688 Alternatively we could use thread local storage for those global
689 variables, which may or may not have advantages */
693 if (setjmp(s1
->error_jmp_buf
) == 0) {
695 s1
->error_set_jmp_enabled
= 1;
699 int len
= strlen(str
);
700 tcc_open_bf(s1
, "<string>", len
);
701 memcpy(file
->buffer
, str
, len
);
703 tcc_open_bf(s1
, str
, 0);
707 is_asm
= !!(filetype
& (AFF_TYPE_ASM
|AFF_TYPE_ASMPP
));
708 tccelf_begin_file(s1
);
709 preprocess_start(s1
, is_asm
);
711 if (s1
->output_type
== TCC_OUTPUT_PREPROCESS
) {
714 #ifdef CONFIG_TCC_ASM
715 tcc_assemble(s1
, !!(filetype
& AFF_TYPE_ASMPP
));
717 tcc_error_noabort("asm not supported");
723 s1
->error_set_jmp_enabled
= 0;
729 return s1
->nb_errors
!= 0 ? -1 : 0;
732 LIBTCCAPI
int tcc_compile_string(TCCState
*s
, const char *str
)
734 return tcc_compile(s
, s
->filetype
, str
, -1);
737 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
738 LIBTCCAPI
void tcc_define_symbol(TCCState
*s1
, const char *sym
, const char *value
)
742 cstr_printf(&s1
->cmdline_defs
, "#define %s %s\n", sym
, value
);
745 /* undefine a preprocessor symbol */
746 LIBTCCAPI
void tcc_undefine_symbol(TCCState
*s1
, const char *sym
)
748 cstr_printf(&s1
->cmdline_defs
, "#undef %s\n", sym
);
752 LIBTCCAPI TCCState
*tcc_new(void)
756 s
= tcc_mallocz(sizeof(TCCState
));
768 s
->dollars_in_identifiers
= 1; /*on by default like in gcc/clang*/
769 s
->cversion
= 199901; /* default unless -std=c11 is supplied */
770 s
->warn_implicit_function_declaration
= 1;
771 s
->ms_extensions
= 1;
773 #ifdef CHAR_IS_UNSIGNED
774 s
->char_is_unsigned
= 1;
776 #ifdef TCC_TARGET_I386
779 /* enable this if you want symbols with leading underscore on windows: */
780 #if 0 /* def TCC_TARGET_PE */
781 s
->leading_underscore
= 1;
784 /* might be used in error() before preprocess_start() */
785 s
->include_stack_ptr
= s
->include_stack
;
790 tcc_set_lib_path_w32(s
);
792 tcc_set_lib_path(s
, CONFIG_TCCDIR
);
796 /* define __TINYC__ 92X */
797 char buffer
[32]; int a
,b
,c
;
798 sscanf(TCC_VERSION
, "%d.%d.%d", &a
, &b
, &c
);
799 sprintf(buffer
, "%d", a
*10000 + b
*100 + c
);
800 tcc_define_symbol(s
, "__TINYC__", buffer
);
803 /* standard defines */
804 tcc_define_symbol(s
, "__STDC__", NULL
);
805 tcc_define_symbol(s
, "__STDC_VERSION__", "199901L");
806 tcc_define_symbol(s
, "__STDC_HOSTED__", NULL
);
809 #if defined(TCC_TARGET_I386)
810 tcc_define_symbol(s
, "__i386__", NULL
);
811 tcc_define_symbol(s
, "__i386", NULL
);
812 tcc_define_symbol(s
, "i386", NULL
);
813 #elif defined(TCC_TARGET_X86_64)
814 tcc_define_symbol(s
, "__x86_64__", NULL
);
815 #elif defined(TCC_TARGET_ARM)
816 tcc_define_symbol(s
, "__ARM_ARCH_4__", NULL
);
817 tcc_define_symbol(s
, "__arm_elf__", NULL
);
818 tcc_define_symbol(s
, "__arm_elf", NULL
);
819 tcc_define_symbol(s
, "arm_elf", NULL
);
820 tcc_define_symbol(s
, "__arm__", NULL
);
821 tcc_define_symbol(s
, "__arm", NULL
);
822 tcc_define_symbol(s
, "arm", NULL
);
823 tcc_define_symbol(s
, "__APCS_32__", NULL
);
824 tcc_define_symbol(s
, "__ARMEL__", NULL
);
825 #if defined(TCC_ARM_EABI)
826 tcc_define_symbol(s
, "__ARM_EABI__", NULL
);
828 #if defined(TCC_ARM_HARDFLOAT)
829 s
->float_abi
= ARM_HARD_FLOAT
;
830 tcc_define_symbol(s
, "__ARM_PCS_VFP", NULL
);
832 s
->float_abi
= ARM_SOFTFP_FLOAT
;
834 #elif defined(TCC_TARGET_ARM64)
835 tcc_define_symbol(s
, "__aarch64__", NULL
);
836 #elif defined TCC_TARGET_C67
837 tcc_define_symbol(s
, "__C67__", NULL
);
838 #elif defined TCC_TARGET_RISCV64
839 tcc_define_symbol(s
, "__riscv", NULL
);
840 tcc_define_symbol(s
, "__riscv_xlen", "64");
841 tcc_define_symbol(s
, "__riscv_flen", "64");
842 tcc_define_symbol(s
, "__riscv_div", NULL
);
843 tcc_define_symbol(s
, "__riscv_mul", NULL
);
844 tcc_define_symbol(s
, "__riscv_fdiv", NULL
);
845 tcc_define_symbol(s
, "__riscv_fsqrt", NULL
);
846 tcc_define_symbol(s
, "__riscv_float_abi_double", NULL
);
850 tcc_define_symbol(s
, "_WIN32", NULL
);
851 tcc_define_symbol(s
, "__declspec(x)", "__attribute__((x))");
852 tcc_define_symbol(s
, "__cdecl", "");
853 # ifdef TCC_TARGET_X86_64
854 tcc_define_symbol(s
, "_WIN64", NULL
);
857 tcc_define_symbol(s
, "__unix__", NULL
);
858 tcc_define_symbol(s
, "__unix", NULL
);
859 tcc_define_symbol(s
, "unix", NULL
);
860 # if defined(__linux__)
861 tcc_define_symbol(s
, "__linux__", NULL
);
862 tcc_define_symbol(s
, "__linux", NULL
);
864 # if defined(__FreeBSD__)
865 tcc_define_symbol(s
, "__FreeBSD__", "__FreeBSD__");
866 /* No 'Thread Storage Local' on FreeBSD with tcc */
867 tcc_define_symbol(s
, "__NO_TLS", NULL
);
869 # if defined(__FreeBSD_kernel__)
870 tcc_define_symbol(s
, "__FreeBSD_kernel__", NULL
);
872 # if defined(__NetBSD__)
873 tcc_define_symbol(s
, "__NetBSD__", "__NetBSD__");
875 # if defined(__OpenBSD__)
876 tcc_define_symbol(s
, "__OpenBSD__", "__OpenBSD__");
880 /* TinyCC & gcc defines */
883 tcc_define_symbol(s
, "__SIZE_TYPE__", "unsigned int");
884 tcc_define_symbol(s
, "__PTRDIFF_TYPE__", "int");
885 tcc_define_symbol(s
, "__ILP32__", NULL
);
888 tcc_define_symbol(s
, "__SIZE_TYPE__", "unsigned long long");
889 tcc_define_symbol(s
, "__PTRDIFF_TYPE__", "long long");
890 tcc_define_symbol(s
, "__LLP64__", NULL
);
892 /* Other 64bit systems. */
893 tcc_define_symbol(s
, "__SIZE_TYPE__", "unsigned long");
894 tcc_define_symbol(s
, "__PTRDIFF_TYPE__", "long");
895 tcc_define_symbol(s
, "__LP64__", NULL
);
897 tcc_define_symbol(s
, "__SIZEOF_POINTER__", PTR_SIZE
== 4 ? "4" : "8");
900 tcc_define_symbol(s
, "__WCHAR_TYPE__", "unsigned short");
901 tcc_define_symbol(s
, "__WINT_TYPE__", "unsigned short");
903 tcc_define_symbol(s
, "__WCHAR_TYPE__", "int");
904 /* wint_t is unsigned int by default, but (signed) int on BSDs
905 and unsigned short on windows. Other OSes might have still
906 other conventions, sigh. */
907 # if defined(__FreeBSD__) || defined (__FreeBSD_kernel__) \
908 || defined(__NetBSD__) || defined(__OpenBSD__)
909 tcc_define_symbol(s
, "__WINT_TYPE__", "int");
911 /* define __GNUC__ to have some useful stuff from sys/cdefs.h
912 that are unconditionally used in FreeBSDs other system headers :/ */
913 tcc_define_symbol(s
, "__GNUC__", "2");
914 tcc_define_symbol(s
, "__GNUC_MINOR__", "7");
915 tcc_define_symbol(s
, "__builtin_alloca", "alloca");
918 tcc_define_symbol(s
, "__WINT_TYPE__", "unsigned int");
920 tcc_define_symbol(s
, "__REDIRECT(name, proto, alias)",
921 "name proto __asm__ (#alias)");
922 tcc_define_symbol(s
, "__REDIRECT_NTH(name, proto, alias)",
923 "name proto __asm__ (#alias) __THROW");
925 /* Some GCC builtins that are simple to express as macros. */
926 tcc_define_symbol(s
, "__builtin_extract_return_addr(x)", "x");
927 #endif /* ndef TCC_TARGET_PE */
928 #ifdef TCC_TARGET_MACHO
929 /* emulate APPLE-GCC to make libc's headerfiles compile: */
930 tcc_define_symbol(s
, "__APPLE__", "1");
931 tcc_define_symbol(s
, "__GNUC__", "4"); /* darwin emits warning on GCC<4 */
933 /* avoids usage of GCC/clang specific builtins in libc-headerfiles: */
934 tcc_define_symbol(s
, "__FINITE_MATH_ONLY__", "1");
935 tcc_define_symbol(s
, "_FORTIFY_SOURCE", "0");
936 #endif /* ndef TCC_TARGET_MACHO */
940 LIBTCCAPI
void tcc_delete(TCCState
*s1
)
945 /* free library paths */
946 dynarray_reset(&s1
->library_paths
, &s1
->nb_library_paths
);
947 dynarray_reset(&s1
->crt_paths
, &s1
->nb_crt_paths
);
949 /* free include paths */
950 dynarray_reset(&s1
->include_paths
, &s1
->nb_include_paths
);
951 dynarray_reset(&s1
->sysinclude_paths
, &s1
->nb_sysinclude_paths
);
953 tcc_free(s1
->tcc_lib_path
);
954 tcc_free(s1
->soname
);
956 tcc_free(s1
->init_symbol
);
957 tcc_free(s1
->fini_symbol
);
958 tcc_free(s1
->outfile
);
959 tcc_free(s1
->deps_outfile
);
960 dynarray_reset(&s1
->files
, &s1
->nb_files
);
961 dynarray_reset(&s1
->target_deps
, &s1
->nb_target_deps
);
962 dynarray_reset(&s1
->pragma_libs
, &s1
->nb_pragma_libs
);
963 dynarray_reset(&s1
->argv
, &s1
->argc
);
965 cstr_free(&s1
->cmdline_defs
);
966 cstr_free(&s1
->cmdline_incl
);
968 /* free runtime memory */
974 if (0 == --nb_states
)
979 LIBTCCAPI
int tcc_set_output_type(TCCState
*s
, int output_type
)
981 s
->output_type
= output_type
;
983 /* always elf for objects */
984 if (output_type
== TCC_OUTPUT_OBJ
)
985 s
->output_format
= TCC_OUTPUT_FORMAT_ELF
;
987 if (s
->char_is_unsigned
)
988 tcc_define_symbol(s
, "__CHAR_UNSIGNED__", NULL
);
990 if (s
->cversion
== 201112) {
991 tcc_undefine_symbol(s
, "__STDC_VERSION__");
992 tcc_define_symbol(s
, "__STDC_VERSION__", "201112L");
993 tcc_define_symbol(s
, "__STDC_NO_ATOMICS__", NULL
);
994 tcc_define_symbol(s
, "__STDC_NO_COMPLEX__", NULL
);
995 tcc_define_symbol(s
, "__STDC_NO_THREADS__", NULL
);
996 #ifndef TCC_TARGET_PE
997 /* on Linux, this conflicts with a define introduced by
998 /usr/include/stdc-predef.h included by glibc libs
999 tcc_define_symbol(s, "__STDC_ISO_10646__", "201605L"); */
1000 tcc_define_symbol(s
, "__STDC_UTF_16__", NULL
);
1001 tcc_define_symbol(s
, "__STDC_UTF_32__", NULL
);
1005 if (s
->optimize
> 0)
1006 tcc_define_symbol(s
, "__OPTIMIZE__", NULL
);
1008 if (s
->option_pthread
)
1009 tcc_define_symbol(s
, "_REENTRANT", NULL
);
1012 /* default include paths */
1013 /* -isystem paths have already been handled */
1014 tcc_add_sysinclude_path(s
, CONFIG_TCC_SYSINCLUDEPATHS
);
1017 #ifdef CONFIG_TCC_BCHECK
1018 if (s
->do_bounds_check
) {
1019 /* if bound checking, then add corresponding sections */
1020 tccelf_bounds_new(s
);
1022 tcc_define_symbol(s
, "__BOUNDS_CHECKING_ON", NULL
);
1026 /* add debug sections */
1030 tcc_add_library_path(s
, CONFIG_TCC_LIBPATHS
);
1032 #ifdef TCC_TARGET_PE
1034 if (!s
->nostdlib
&& output_type
!= TCC_OUTPUT_OBJ
)
1035 tcc_add_systemdir(s
);
1038 /* paths for crt objects */
1039 tcc_split_path(s
, &s
->crt_paths
, &s
->nb_crt_paths
, CONFIG_TCC_CRTPREFIX
);
1040 /* add libc crt1/crti objects */
1041 if ((output_type
== TCC_OUTPUT_EXE
|| output_type
== TCC_OUTPUT_DLL
) &&
1043 if (output_type
!= TCC_OUTPUT_DLL
)
1044 tcc_add_crt(s
, "crt1.o");
1045 tcc_add_crt(s
, "crti.o");
1051 LIBTCCAPI
int tcc_add_include_path(TCCState
*s
, const char *pathname
)
1053 tcc_split_path(s
, &s
->include_paths
, &s
->nb_include_paths
, pathname
);
1057 LIBTCCAPI
int tcc_add_sysinclude_path(TCCState
*s
, const char *pathname
)
1059 tcc_split_path(s
, &s
->sysinclude_paths
, &s
->nb_sysinclude_paths
, pathname
);
1063 ST_FUNC
int tcc_add_file_internal(TCCState
*s1
, const char *filename
, int flags
)
1068 fd
= _tcc_open(s1
, filename
);
1070 if (flags
& AFF_PRINT_ERROR
)
1071 tcc_error_noabort("file '%s' not found", filename
);
1075 s1
->current_filename
= filename
;
1076 if (flags
& AFF_TYPE_BIN
) {
1080 obj_type
= tcc_object_type(fd
, &ehdr
);
1081 lseek(fd
, 0, SEEK_SET
);
1083 #ifdef TCC_TARGET_MACHO
1084 if (0 == obj_type
&& 0 == strcmp(tcc_fileextension(filename
), ".dylib"))
1085 obj_type
= AFF_BINTYPE_DYN
;
1089 case AFF_BINTYPE_REL
:
1090 ret
= tcc_load_object_file(s1
, fd
, 0);
1092 #ifndef TCC_TARGET_PE
1093 case AFF_BINTYPE_DYN
:
1094 if (s1
->output_type
== TCC_OUTPUT_MEMORY
) {
1096 #ifdef TCC_IS_NATIVE
1097 if (NULL
== dlopen(filename
, RTLD_GLOBAL
| RTLD_LAZY
))
1101 #ifndef TCC_TARGET_MACHO
1102 ret
= tcc_load_dll(s1
, fd
, filename
,
1103 (flags
& AFF_REFERENCED_DLL
) != 0);
1110 case AFF_BINTYPE_AR
:
1111 ret
= tcc_load_archive(s1
, fd
, !(flags
& AFF_WHOLE_ARCHIVE
));
1113 #ifdef TCC_TARGET_COFF
1114 case AFF_BINTYPE_C67
:
1115 ret
= tcc_load_coff(s1
, fd
);
1119 #ifdef TCC_TARGET_PE
1120 ret
= pe_load_file(s1
, filename
, fd
);
1121 #elif defined(TCC_TARGET_MACHO)
1124 /* as GNU ld, consider it is an ld script if not recognized */
1125 ret
= tcc_load_ldscript(s1
, fd
);
1128 tcc_error_noabort("%s: unrecognized file type %d", filename
,
1134 /* update target deps */
1135 dynarray_add(&s1
->target_deps
, &s1
->nb_target_deps
, tcc_strdup(filename
));
1136 ret
= tcc_compile(s1
, flags
, filename
, fd
);
1138 s1
->current_filename
= NULL
;
1142 LIBTCCAPI
int tcc_add_file(TCCState
*s
, const char *filename
)
1144 int filetype
= s
->filetype
;
1145 if (0 == (filetype
& AFF_TYPE_MASK
)) {
1146 /* use a file extension to detect a filetype */
1147 const char *ext
= tcc_fileextension(filename
);
1150 if (!strcmp(ext
, "S"))
1151 filetype
= AFF_TYPE_ASMPP
;
1152 else if (!strcmp(ext
, "s"))
1153 filetype
= AFF_TYPE_ASM
;
1154 else if (!PATHCMP(ext
, "c") || !PATHCMP(ext
, "i"))
1155 filetype
= AFF_TYPE_C
;
1157 filetype
|= AFF_TYPE_BIN
;
1159 filetype
= AFF_TYPE_C
;
1162 return tcc_add_file_internal(s
, filename
, filetype
| AFF_PRINT_ERROR
);
1165 LIBTCCAPI
int tcc_add_library_path(TCCState
*s
, const char *pathname
)
1167 tcc_split_path(s
, &s
->library_paths
, &s
->nb_library_paths
, pathname
);
1171 static int tcc_add_library_internal(TCCState
*s
, const char *fmt
,
1172 const char *filename
, int flags
, char **paths
, int nb_paths
)
1177 for(i
= 0; i
< nb_paths
; i
++) {
1178 snprintf(buf
, sizeof(buf
), fmt
, paths
[i
], filename
);
1179 if (tcc_add_file_internal(s
, buf
, flags
| AFF_TYPE_BIN
) == 0)
1185 /* find and load a dll. Return non zero if not found */
1186 /* XXX: add '-rpath' option support ? */
1187 ST_FUNC
int tcc_add_dll(TCCState
*s
, const char *filename
, int flags
)
1189 return tcc_add_library_internal(s
, "%s/%s", filename
, flags
,
1190 s
->library_paths
, s
->nb_library_paths
);
1193 #ifndef TCC_TARGET_PE
1194 ST_FUNC
int tcc_add_crt(TCCState
*s1
, const char *filename
)
1196 if (-1 == tcc_add_library_internal(s1
, "%s/%s",
1197 filename
, 0, s1
->crt_paths
, s1
->nb_crt_paths
))
1198 tcc_error_noabort("file '%s' not found", filename
);
1203 /* the library name is the same as the argument of the '-l' option */
1204 LIBTCCAPI
int tcc_add_library(TCCState
*s
, const char *libraryname
)
1206 #if defined TCC_TARGET_PE
1207 const char *libs
[] = { "%s/%s.def", "%s/lib%s.def", "%s/%s.dll", "%s/lib%s.dll", "%s/lib%s.a", NULL
};
1208 const char **pp
= s
->static_link
? libs
+ 4 : libs
;
1209 #elif defined TCC_TARGET_MACHO
1210 const char *libs
[] = { "%s/lib%s.dylib", "%s/lib%s.a", NULL
};
1211 const char **pp
= s
->static_link
? libs
+ 1 : libs
;
1213 const char *libs
[] = { "%s/lib%s.so", "%s/lib%s.a", NULL
};
1214 const char **pp
= s
->static_link
? libs
+ 1 : libs
;
1216 int flags
= s
->filetype
& AFF_WHOLE_ARCHIVE
;
1218 if (0 == tcc_add_library_internal(s
, *pp
,
1219 libraryname
, flags
, s
->library_paths
, s
->nb_library_paths
))
1226 PUB_FUNC
int tcc_add_library_err(TCCState
*s1
, const char *libname
)
1228 int ret
= tcc_add_library(s1
, libname
);
1230 tcc_error_noabort("library '%s' not found", libname
);
1234 /* handle #pragma comment(lib,) */
1235 ST_FUNC
void tcc_add_pragma_libs(TCCState
*s1
)
1238 for (i
= 0; i
< s1
->nb_pragma_libs
; i
++)
1239 tcc_add_library_err(s1
, s1
->pragma_libs
[i
]);
1242 LIBTCCAPI
int tcc_add_symbol(TCCState
*s1
, const char *name
, const void *val
)
1244 #ifdef TCC_TARGET_PE
1245 /* On x86_64 'val' might not be reachable with a 32bit offset.
1246 So it is handled here as if it were in a DLL. */
1247 pe_putimport(s1
, 0, name
, (uintptr_t)val
);
1249 set_elf_sym(symtab_section
, (uintptr_t)val
, 0,
1250 ELFW(ST_INFO
)(STB_GLOBAL
, STT_NOTYPE
), 0,
1256 LIBTCCAPI
void tcc_set_lib_path(TCCState
*s
, const char *path
)
1258 tcc_free(s
->tcc_lib_path
);
1259 s
->tcc_lib_path
= tcc_strdup(path
);
1262 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
1263 #define FD_INVERT 0x0002 /* invert value before storing */
1265 typedef struct FlagDef
{
1271 static int no_flag(const char **pp
)
1273 const char *p
= *pp
;
1274 if (*p
!= 'n' || *++p
!= 'o' || *++p
!= '-')
1280 ST_FUNC
int set_flag(TCCState
*s
, const FlagDef
*flags
, const char *name
)
1291 for (ret
= -1, p
= flags
; p
->name
; ++p
) {
1293 if (strcmp(r
, p
->name
))
1296 if (0 == (p
->flags
& WD_ALL
))
1300 *((unsigned char *)s
+ p
->offset
) =
1301 p
->flags
& FD_INVERT
? !value
: value
;
1311 static int strstart(const char *val
, const char **str
)
1326 /* Like strstart, but automatically takes into account that ld options can
1328 * - start with double or single dash (e.g. '--soname' or '-soname')
1329 * - arguments can be given as separate or after '=' (e.g. '-Wl,-soname,x.so'
1330 * or '-Wl,-soname=x.so')
1332 * you provide `val` always in 'option[=]' form (no leading -)
1334 static int link_option(const char *str
, const char *val
, const char **ptr
)
1339 /* there should be 1 or 2 dashes */
1345 /* then str & val should match (potentially up to '=') */
1356 while (*q
!= '\0' && *q
!= '=') {
1363 /* '=' near eos means ',' or '=' is ok */
1367 if (*p
!= ',' && *p
!= '=')
1377 static const char *skip_linker_arg(const char **str
)
1379 const char *s1
= *str
;
1380 const char *s2
= strchr(s1
, ',');
1381 *str
= s2
? s2
++ : (s2
= s1
+ strlen(s1
));
1385 static void copy_linker_arg(char **pp
, const char *s
, int sep
)
1391 p
[l
= strlen(p
)] = sep
, ++l
;
1392 skip_linker_arg(&q
);
1393 pstrncpy(l
+ (*pp
= tcc_realloc(p
, q
- s
+ l
+ 1)), s
, q
- s
);
1396 /* set linker options */
1397 static int tcc_set_linker(TCCState
*s
, const char *option
)
1402 const char *p
= NULL
;
1407 if (link_option(option
, "Bsymbolic", &p
)) {
1409 } else if (link_option(option
, "nostdlib", &p
)) {
1411 } else if (link_option(option
, "fini=", &p
)) {
1412 copy_linker_arg(&s
->fini_symbol
, p
, 0);
1414 } else if (link_option(option
, "image-base=", &p
)
1415 || link_option(option
, "Ttext=", &p
)) {
1416 s
->text_addr
= strtoull(p
, &end
, 16);
1417 s
->has_text_addr
= 1;
1418 } else if (link_option(option
, "init=", &p
)) {
1419 copy_linker_arg(&s
->init_symbol
, p
, 0);
1421 } else if (link_option(option
, "oformat=", &p
)) {
1422 #if defined(TCC_TARGET_PE)
1423 if (strstart("pe-", &p
)) {
1425 if (strstart("elf64-", &p
)) {
1427 if (strstart("elf32-", &p
)) {
1429 s
->output_format
= TCC_OUTPUT_FORMAT_ELF
;
1430 } else if (!strcmp(p
, "binary")) {
1431 s
->output_format
= TCC_OUTPUT_FORMAT_BINARY
;
1432 #ifdef TCC_TARGET_COFF
1433 } else if (!strcmp(p
, "coff")) {
1434 s
->output_format
= TCC_OUTPUT_FORMAT_COFF
;
1439 } else if (link_option(option
, "as-needed", &p
)) {
1441 } else if (link_option(option
, "O", &p
)) {
1443 } else if (link_option(option
, "export-all-symbols", &p
)) {
1445 } else if (link_option(option
, "export-dynamic", &p
)) {
1447 } else if (link_option(option
, "rpath=", &p
)) {
1448 copy_linker_arg(&s
->rpath
, p
, ':');
1449 } else if (link_option(option
, "enable-new-dtags", &p
)) {
1450 s
->enable_new_dtags
= 1;
1451 } else if (link_option(option
, "section-alignment=", &p
)) {
1452 s
->section_align
= strtoul(p
, &end
, 16);
1453 } else if (link_option(option
, "soname=", &p
)) {
1454 copy_linker_arg(&s
->soname
, p
, 0);
1455 #ifdef TCC_TARGET_PE
1456 } else if (link_option(option
, "large-address-aware", &p
)) {
1457 s
->pe_characteristics
|= 0x20;
1458 } else if (link_option(option
, "file-alignment=", &p
)) {
1459 s
->pe_file_align
= strtoul(p
, &end
, 16);
1460 } else if (link_option(option
, "stack=", &p
)) {
1461 s
->pe_stack_size
= strtoul(p
, &end
, 10);
1462 } else if (link_option(option
, "subsystem=", &p
)) {
1463 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1464 if (!strcmp(p
, "native")) {
1465 s
->pe_subsystem
= 1;
1466 } else if (!strcmp(p
, "console")) {
1467 s
->pe_subsystem
= 3;
1468 } else if (!strcmp(p
, "gui") || !strcmp(p
, "windows")) {
1469 s
->pe_subsystem
= 2;
1470 } else if (!strcmp(p
, "posix")) {
1471 s
->pe_subsystem
= 7;
1472 } else if (!strcmp(p
, "efiapp")) {
1473 s
->pe_subsystem
= 10;
1474 } else if (!strcmp(p
, "efiboot")) {
1475 s
->pe_subsystem
= 11;
1476 } else if (!strcmp(p
, "efiruntime")) {
1477 s
->pe_subsystem
= 12;
1478 } else if (!strcmp(p
, "efirom")) {
1479 s
->pe_subsystem
= 13;
1480 #elif defined(TCC_TARGET_ARM)
1481 if (!strcmp(p
, "wince")) {
1482 s
->pe_subsystem
= 9;
1487 } else if (ret
= link_option(option
, "?whole-archive", &p
), ret
) {
1489 s
->filetype
|= AFF_WHOLE_ARCHIVE
;
1491 s
->filetype
&= ~AFF_WHOLE_ARCHIVE
;
1496 tcc_error("unsupported linker option '%s'", option
);
1499 if (ignoring
&& s
->warn_unsupported
)
1500 tcc_warning("unsupported linker option '%s'", option
);
1502 option
= skip_linker_arg(&p
);
1507 typedef struct TCCOption
{
1530 TCC_OPTION_dumpversion
,
1539 TCC_OPTION_traditional
,
1544 TCC_OPTION_mfloat_abi
,
1548 TCC_OPTION_iwithprefix
,
1550 TCC_OPTION_nostdinc
,
1551 TCC_OPTION_nostdlib
,
1552 TCC_OPTION_print_search_dirs
,
1553 TCC_OPTION_rdynamic
,
1555 TCC_OPTION_pedantic
,
1568 #define TCC_OPTION_HAS_ARG 0x0001
1569 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
1571 static const TCCOption tcc_options
[] = {
1572 { "h", TCC_OPTION_HELP
, 0 },
1573 { "-help", TCC_OPTION_HELP
, 0 },
1574 { "?", TCC_OPTION_HELP
, 0 },
1575 { "hh", TCC_OPTION_HELP2
, 0 },
1576 { "v", TCC_OPTION_v
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1577 { "-version", TCC_OPTION_v
, 0 }, /* handle as verbose, also prints version*/
1578 { "I", TCC_OPTION_I
, TCC_OPTION_HAS_ARG
},
1579 { "D", TCC_OPTION_D
, TCC_OPTION_HAS_ARG
},
1580 { "U", TCC_OPTION_U
, TCC_OPTION_HAS_ARG
},
1581 { "P", TCC_OPTION_P
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1582 { "L", TCC_OPTION_L
, TCC_OPTION_HAS_ARG
},
1583 { "B", TCC_OPTION_B
, TCC_OPTION_HAS_ARG
},
1584 { "l", TCC_OPTION_l
, TCC_OPTION_HAS_ARG
},
1585 { "bench", TCC_OPTION_bench
, 0 },
1586 #ifdef CONFIG_TCC_BACKTRACE
1587 { "bt", TCC_OPTION_bt
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1589 #ifdef CONFIG_TCC_BCHECK
1590 { "b", TCC_OPTION_b
, 0 },
1592 { "g", TCC_OPTION_g
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1593 { "c", TCC_OPTION_c
, 0 },
1594 { "dumpversion", TCC_OPTION_dumpversion
, 0},
1595 { "d", TCC_OPTION_d
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1596 { "static", TCC_OPTION_static
, 0 },
1597 { "std", TCC_OPTION_std
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1598 { "shared", TCC_OPTION_shared
, 0 },
1599 { "soname", TCC_OPTION_soname
, TCC_OPTION_HAS_ARG
},
1600 { "o", TCC_OPTION_o
, TCC_OPTION_HAS_ARG
},
1601 { "-param", TCC_OPTION_param
, TCC_OPTION_HAS_ARG
},
1602 { "pedantic", TCC_OPTION_pedantic
, 0},
1603 { "pthread", TCC_OPTION_pthread
, 0},
1604 { "run", TCC_OPTION_run
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1605 { "rdynamic", TCC_OPTION_rdynamic
, 0 },
1606 { "r", TCC_OPTION_r
, 0 },
1607 { "s", TCC_OPTION_s
, 0 },
1608 { "traditional", TCC_OPTION_traditional
, 0 },
1609 { "Wl,", TCC_OPTION_Wl
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1610 { "Wp,", TCC_OPTION_Wp
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1611 { "W", TCC_OPTION_W
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1612 { "O", TCC_OPTION_O
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1613 #ifdef TCC_TARGET_ARM
1614 { "mfloat-abi", TCC_OPTION_mfloat_abi
, TCC_OPTION_HAS_ARG
},
1616 { "m", TCC_OPTION_m
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1617 { "f", TCC_OPTION_f
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1618 { "isystem", TCC_OPTION_isystem
, TCC_OPTION_HAS_ARG
},
1619 { "include", TCC_OPTION_include
, TCC_OPTION_HAS_ARG
},
1620 { "nostdinc", TCC_OPTION_nostdinc
, 0 },
1621 { "nostdlib", TCC_OPTION_nostdlib
, 0 },
1622 { "print-search-dirs", TCC_OPTION_print_search_dirs
, 0 },
1623 { "w", TCC_OPTION_w
, 0 },
1624 { "pipe", TCC_OPTION_pipe
, 0},
1625 { "E", TCC_OPTION_E
, 0},
1626 { "MD", TCC_OPTION_MD
, 0},
1627 { "MF", TCC_OPTION_MF
, TCC_OPTION_HAS_ARG
},
1628 { "x", TCC_OPTION_x
, TCC_OPTION_HAS_ARG
},
1629 { "ar", TCC_OPTION_ar
, 0},
1630 #ifdef TCC_TARGET_PE
1631 { "impdef", TCC_OPTION_impdef
, 0},
1636 static const FlagDef options_W
[] = {
1638 { offsetof(TCCState
, warn_unsupported
), 0, "unsupported" },
1639 { offsetof(TCCState
, warn_write_strings
), 0, "write-strings" },
1640 { offsetof(TCCState
, warn_error
), 0, "error" },
1641 { offsetof(TCCState
, warn_gcc_compat
), 0, "gcc-compat" },
1642 { offsetof(TCCState
, warn_implicit_function_declaration
), WD_ALL
,
1643 "implicit-function-declaration" },
1647 static const FlagDef options_f
[] = {
1648 { offsetof(TCCState
, char_is_unsigned
), 0, "unsigned-char" },
1649 { offsetof(TCCState
, char_is_unsigned
), FD_INVERT
, "signed-char" },
1650 { offsetof(TCCState
, nocommon
), FD_INVERT
, "common" },
1651 { offsetof(TCCState
, leading_underscore
), 0, "leading-underscore" },
1652 { offsetof(TCCState
, ms_extensions
), 0, "ms-extensions" },
1653 { offsetof(TCCState
, dollars_in_identifiers
), 0, "dollars-in-identifiers" },
1657 static const FlagDef options_m
[] = {
1658 { offsetof(TCCState
, ms_bitfields
), 0, "ms-bitfields" },
1659 #ifdef TCC_TARGET_X86_64
1660 { offsetof(TCCState
, nosse
), FD_INVERT
, "sse" },
1665 static void parse_option_D(TCCState
*s1
, const char *optarg
)
1667 char *sym
= tcc_strdup(optarg
);
1668 char *value
= strchr(sym
, '=');
1671 tcc_define_symbol(s1
, sym
, value
);
1675 static void args_parser_add_file(TCCState
*s
, const char* filename
, int filetype
)
1677 struct filespec
*f
= tcc_malloc(sizeof *f
+ strlen(filename
));
1679 strcpy(f
->name
, filename
);
1680 dynarray_add(&s
->files
, &s
->nb_files
, f
);
1683 static int args_parser_make_argv(const char *r
, int *argc
, char ***argv
)
1688 while (c
= (unsigned char)*r
, c
&& c
<= ' ')
1694 while (c
= (unsigned char)*r
, c
) {
1696 if (c
== '\\' && (*r
== '"' || *r
== '\\')) {
1698 } else if (c
== '"') {
1701 } else if (q
== 0 && c
<= ' ') {
1707 //printf("<%s>\n", str.data), fflush(stdout);
1708 dynarray_add(argv
, argc
, tcc_strdup(str
.data
));
1715 /* read list file */
1716 static void args_parser_listfile(TCCState
*s
,
1717 const char *filename
, int optind
, int *pargc
, char ***pargv
)
1726 fd
= open(filename
, O_RDONLY
| O_BINARY
);
1728 tcc_error("listfile '%s' not found", filename
);
1730 len
= lseek(fd
, 0, SEEK_END
);
1731 p
= tcc_malloc(len
+ 1), p
[len
] = 0;
1732 lseek(fd
, 0, SEEK_SET
), read(fd
, p
, len
), close(fd
);
1734 for (i
= 0; i
< *pargc
; ++i
)
1736 args_parser_make_argv(p
, &argc
, &argv
);
1738 dynarray_add(&argv
, &argc
, tcc_strdup((*pargv
)[i
]));
1741 dynarray_reset(&s
->argv
, &s
->argc
);
1742 *pargc
= s
->argc
= argc
, *pargv
= s
->argv
= argv
;
1745 PUB_FUNC
int tcc_parse_args(TCCState
*s
, int *pargc
, char ***pargv
, int optind
)
1748 const TCCOption
*popt
;
1749 const char *optarg
, *r
;
1750 const char *run
= NULL
;
1752 CString linker_arg
; /* collect -Wl options */
1753 int tool
= 0, arg_start
= 0, noaction
= optind
;
1754 char **argv
= *pargv
;
1757 cstr_new(&linker_arg
);
1759 while (optind
< argc
) {
1761 if (r
[0] == '@' && r
[1] != '\0') {
1762 args_parser_listfile(s
, r
+ 1, optind
, &argc
, &argv
);
1767 if (r
[0] == '-' && r
[1] == 'v' && r
[2] == 0)
1772 if (r
[0] != '-' || r
[1] == '\0') {
1773 if (r
[0] != '@') /* allow "tcc file(s) -run @ args ..." */
1774 args_parser_add_file(s
, r
, s
->filetype
);
1776 tcc_set_options(s
, run
);
1777 arg_start
= optind
- 1;
1783 /* find option in table */
1784 for(popt
= tcc_options
; ; ++popt
) {
1785 const char *p1
= popt
->name
;
1786 const char *r1
= r
+ 1;
1788 tcc_error("invalid option -- '%s'", r
);
1789 if (!strstart(p1
, &r1
))
1792 if (popt
->flags
& TCC_OPTION_HAS_ARG
) {
1793 if (*r1
== '\0' && !(popt
->flags
& TCC_OPTION_NOSEP
)) {
1796 tcc_error("argument to '%s' is missing", r
);
1797 optarg
= argv
[optind
++];
1799 } else if (*r1
!= '\0')
1804 switch(popt
->index
) {
1805 case TCC_OPTION_HELP
:
1808 case TCC_OPTION_HELP2
:
1812 tcc_add_include_path(s
, optarg
);
1815 parse_option_D(s
, optarg
);
1818 tcc_undefine_symbol(s
, optarg
);
1821 tcc_add_library_path(s
, optarg
);
1824 /* set tcc utilities path (mainly for tcc development) */
1825 tcc_set_lib_path(s
, optarg
);
1828 args_parser_add_file(s
, optarg
, AFF_TYPE_LIB
| (s
->filetype
& ~AFF_TYPE_MASK
));
1831 case TCC_OPTION_pthread
:
1832 s
->option_pthread
= 1;
1834 case TCC_OPTION_bench
:
1837 #ifdef CONFIG_TCC_BACKTRACE
1839 s
->rt_num_callers
= atoi(optarg
);
1840 s
->do_backtrace
= 1;
1844 #ifdef CONFIG_TCC_BCHECK
1846 s
->do_bounds_check
= 1;
1847 s
->do_backtrace
= 1;
1858 tcc_warning("-%s: overriding compiler action already specified", popt
->name
);
1864 else if (*optarg
== 'M')
1866 else if (*optarg
== 't')
1868 else if (isnum(*optarg
))
1869 s
->g_debug
|= atoi(optarg
);
1871 goto unsupported_option
;
1873 case TCC_OPTION_static
:
1876 case TCC_OPTION_std
:
1877 if (strcmp(optarg
, "=c11") == 0)
1878 s
->cversion
= 201112;
1880 case TCC_OPTION_shared
:
1882 goto set_output_type
;
1883 case TCC_OPTION_soname
:
1884 s
->soname
= tcc_strdup(optarg
);
1888 tcc_warning("multiple -o option");
1889 tcc_free(s
->outfile
);
1891 s
->outfile
= tcc_strdup(optarg
);
1894 /* generate a .o merging several output files */
1897 goto set_output_type
;
1898 case TCC_OPTION_isystem
:
1899 tcc_add_sysinclude_path(s
, optarg
);
1901 case TCC_OPTION_include
:
1902 cstr_printf(&s
->cmdline_incl
, "#include \"%s\"\n", optarg
);
1904 case TCC_OPTION_nostdinc
:
1907 case TCC_OPTION_nostdlib
:
1910 case TCC_OPTION_run
:
1911 #ifndef TCC_IS_NATIVE
1912 tcc_error("-run is not available in a cross compiler");
1915 x
= TCC_OUTPUT_MEMORY
;
1916 goto set_output_type
;
1918 do ++s
->verbose
; while (*optarg
++ == 'v');
1922 if (set_flag(s
, options_f
, optarg
) < 0)
1923 goto unsupported_option
;
1925 #ifdef TCC_TARGET_ARM
1926 case TCC_OPTION_mfloat_abi
:
1927 /* tcc doesn't support soft float yet */
1928 if (!strcmp(optarg
, "softfp")) {
1929 s
->float_abi
= ARM_SOFTFP_FLOAT
;
1930 tcc_undefine_symbol(s
, "__ARM_PCS_VFP");
1931 } else if (!strcmp(optarg
, "hard"))
1932 s
->float_abi
= ARM_HARD_FLOAT
;
1934 tcc_error("unsupported float abi '%s'", optarg
);
1938 if (set_flag(s
, options_m
, optarg
) < 0) {
1939 if (x
= atoi(optarg
), x
!= 32 && x
!= 64)
1940 goto unsupported_option
;
1941 if (PTR_SIZE
!= x
/8)
1948 if (optarg
[0] && set_flag(s
, options_W
, optarg
) < 0)
1949 goto unsupported_option
;
1954 case TCC_OPTION_rdynamic
:
1958 if (linker_arg
.size
)
1959 --linker_arg
.size
, cstr_ccat(&linker_arg
, ',');
1960 cstr_cat(&linker_arg
, optarg
, 0);
1961 if (tcc_set_linker(s
, linker_arg
.data
))
1962 cstr_free(&linker_arg
);
1968 x
= TCC_OUTPUT_PREPROCESS
;
1969 goto set_output_type
;
1971 s
->Pflag
= atoi(optarg
) + 1;
1977 s
->deps_outfile
= tcc_strdup(optarg
);
1979 case TCC_OPTION_dumpversion
:
1980 printf ("%s\n", TCC_VERSION
);
1987 else if (*optarg
== 'a')
1989 else if (*optarg
== 'b')
1991 else if (*optarg
== 'n')
1994 tcc_warning("unsupported language '%s'", optarg
);
1995 s
->filetype
= x
| (s
->filetype
& ~AFF_TYPE_MASK
);
1998 s
->optimize
= atoi(optarg
);
2000 case TCC_OPTION_print_search_dirs
:
2003 case TCC_OPTION_impdef
:
2009 arg_start
= optind
- 1;
2010 if (arg_start
!= noaction
)
2011 tcc_error("cannot parse %s here", r
);
2014 case TCC_OPTION_traditional
:
2015 case TCC_OPTION_pedantic
:
2016 case TCC_OPTION_pipe
:
2022 if (s
->warn_unsupported
)
2023 tcc_warning("unsupported option '%s'", r
);
2027 if (linker_arg
.size
) {
2028 r
= linker_arg
.data
;
2031 *pargc
= argc
- arg_start
;
2032 *pargv
= argv
+ arg_start
;
2035 if (optind
!= noaction
)
2037 if (s
->verbose
== 2)
2038 return OPT_PRINT_DIRS
;
2044 LIBTCCAPI
void tcc_set_options(TCCState
*s
, const char *r
)
2048 args_parser_make_argv(r
, &argc
, &argv
);
2049 tcc_parse_args(s
, &argc
, &argv
, 0);
2050 dynarray_reset(&argv
, &argc
);
2053 PUB_FUNC
void tcc_print_stats(TCCState
*s1
, unsigned total_time
)
2057 if (total_bytes
< 1)
2059 fprintf(stderr
, "* %d idents, %d lines, %d bytes\n"
2060 "* %0.3f s, %u lines/s, %0.1f MB/s\n",
2061 total_idents
, total_lines
, total_bytes
,
2062 (double)total_time
/1000,
2063 (unsigned)total_lines
*1000/total_time
,
2064 (double)total_bytes
/1000/total_time
);
2066 fprintf(stderr
, "* %d bytes memory used\n", mem_max_size
);