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"
52 #ifdef TCC_TARGET_ARM64
53 #include "arm64-gen.c"
54 #include "arm64-link.c"
60 #ifdef TCC_TARGET_X86_64
61 #include "x86_64-gen.c"
62 #include "x86_64-link.c"
66 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
70 #ifdef TCC_TARGET_COFF
76 #endif /* ONE_SOURCE */
78 /********************************************************/
79 #ifndef CONFIG_TCC_ASM
80 ST_FUNC
void asm_instr(void)
82 tcc_error("inline asm() not supported");
84 ST_FUNC
void asm_global_instr(void)
86 tcc_error("inline asm() not supported");
90 /********************************************************/
92 ST_FUNC
char *normalize_slashes(char *path
)
95 for (p
= path
; *p
; ++p
)
101 static HMODULE tcc_module
;
103 /* on win32, we suppose the lib and includes are at the location of 'tcc.exe' */
104 static void tcc_set_lib_path_w32(TCCState
*s
)
107 GetModuleFileNameA(tcc_module
, path
, sizeof path
);
108 p
= tcc_basename(normalize_slashes(strlwr(path
)));
109 if (p
- 5 > path
&& 0 == strncmp(p
- 5, "/bin/", 5))
114 tcc_set_lib_path(s
, path
);
118 static void tcc_add_systemdir(TCCState
*s
)
121 GetSystemDirectory(buf
, sizeof buf
);
122 tcc_add_library_path(s
, normalize_slashes(buf
));
127 BOOL WINAPI
DllMain (HINSTANCE hDll
, DWORD dwReason
, LPVOID lpReserved
)
129 if (DLL_PROCESS_ATTACH
== dwReason
)
136 /********************************************************/
137 /* copy a string and truncate it. */
138 ST_FUNC
char *pstrcpy(char *buf
, int buf_size
, const char *s
)
145 q_end
= buf
+ buf_size
- 1;
157 /* strcat and truncate. */
158 ST_FUNC
char *pstrcat(char *buf
, int buf_size
, const char *s
)
163 pstrcpy(buf
+ len
, buf_size
- len
, s
);
167 ST_FUNC
char *pstrncpy(char *out
, const char *in
, size_t num
)
169 memcpy(out
, in
, num
);
174 /* extract the basename of a file */
175 PUB_FUNC
char *tcc_basename(const char *name
)
177 char *p
= strchr(name
, 0);
178 while (p
> name
&& !IS_DIRSEP(p
[-1]))
183 /* extract extension part of a file
185 * (if no extension, return pointer to end-of-string)
187 PUB_FUNC
char *tcc_fileextension (const char *name
)
189 char *b
= tcc_basename(name
);
190 char *e
= strrchr(b
, '.');
191 return e
? e
: strchr(b
, 0);
194 /********************************************************/
195 /* memory management */
203 PUB_FUNC
void tcc_free(void *ptr
)
208 PUB_FUNC
void *tcc_malloc(unsigned long size
)
213 tcc_error("memory full (malloc)");
217 PUB_FUNC
void *tcc_mallocz(unsigned long size
)
220 ptr
= tcc_malloc(size
);
221 memset(ptr
, 0, size
);
225 PUB_FUNC
void *tcc_realloc(void *ptr
, unsigned long size
)
228 ptr1
= realloc(ptr
, size
);
230 tcc_error("memory full (realloc)");
234 PUB_FUNC
char *tcc_strdup(const char *str
)
237 ptr
= tcc_malloc(strlen(str
) + 1);
242 PUB_FUNC
void tcc_memcheck(void)
248 #define MEM_DEBUG_MAGIC1 0xFEEDDEB1
249 #define MEM_DEBUG_MAGIC2 0xFEEDDEB2
250 #define MEM_DEBUG_MAGIC3 0xFEEDDEB3
251 #define MEM_DEBUG_FILE_LEN 40
252 #define MEM_DEBUG_CHECK3(header) \
253 ((mem_debug_header_t*)((char*)header + header->size))->magic3
254 #define MEM_USER_PTR(header) \
255 ((char *)header + offsetof(mem_debug_header_t, magic3))
256 #define MEM_HEADER_PTR(ptr) \
257 (mem_debug_header_t *)((char*)ptr - offsetof(mem_debug_header_t, magic3))
259 struct mem_debug_header
{
262 struct mem_debug_header
*prev
;
263 struct mem_debug_header
*next
;
265 char file_name
[MEM_DEBUG_FILE_LEN
+ 1];
267 __attribute__((aligned(16))) unsigned magic3
;
270 typedef struct mem_debug_header mem_debug_header_t
;
272 static mem_debug_header_t
*mem_debug_chain
;
273 static unsigned mem_cur_size
;
274 static unsigned mem_max_size
;
276 static mem_debug_header_t
*malloc_check(void *ptr
, const char *msg
)
278 mem_debug_header_t
* header
= MEM_HEADER_PTR(ptr
);
279 if (header
->magic1
!= MEM_DEBUG_MAGIC1
||
280 header
->magic2
!= MEM_DEBUG_MAGIC2
||
281 MEM_DEBUG_CHECK3(header
) != MEM_DEBUG_MAGIC3
||
282 header
->size
== (unsigned)-1) {
283 fprintf(stderr
, "%s check failed\n", msg
);
284 if (header
->magic1
== MEM_DEBUG_MAGIC1
)
285 fprintf(stderr
, "%s:%u: block allocated here.\n",
286 header
->file_name
, header
->line_num
);
292 PUB_FUNC
void *tcc_malloc_debug(unsigned long size
, const char *file
, int line
)
295 mem_debug_header_t
*header
;
297 header
= malloc(sizeof(mem_debug_header_t
) + size
);
299 tcc_error("memory full (malloc)");
301 header
->magic1
= MEM_DEBUG_MAGIC1
;
302 header
->magic2
= MEM_DEBUG_MAGIC2
;
304 MEM_DEBUG_CHECK3(header
) = MEM_DEBUG_MAGIC3
;
305 header
->line_num
= line
;
306 ofs
= strlen(file
) - MEM_DEBUG_FILE_LEN
;
307 strncpy(header
->file_name
, file
+ (ofs
> 0 ? ofs
: 0), MEM_DEBUG_FILE_LEN
);
308 header
->file_name
[MEM_DEBUG_FILE_LEN
] = 0;
310 header
->next
= mem_debug_chain
;
313 header
->next
->prev
= header
;
314 mem_debug_chain
= header
;
316 mem_cur_size
+= size
;
317 if (mem_cur_size
> mem_max_size
)
318 mem_max_size
= mem_cur_size
;
320 return MEM_USER_PTR(header
);
323 PUB_FUNC
void tcc_free_debug(void *ptr
)
325 mem_debug_header_t
*header
;
328 header
= malloc_check(ptr
, "tcc_free");
329 mem_cur_size
-= header
->size
;
330 header
->size
= (unsigned)-1;
332 header
->next
->prev
= header
->prev
;
334 header
->prev
->next
= header
->next
;
335 if (header
== mem_debug_chain
)
336 mem_debug_chain
= header
->next
;
340 PUB_FUNC
void *tcc_mallocz_debug(unsigned long size
, const char *file
, int line
)
343 ptr
= tcc_malloc_debug(size
,file
,line
);
344 memset(ptr
, 0, size
);
348 PUB_FUNC
void *tcc_realloc_debug(void *ptr
, unsigned long size
, const char *file
, int line
)
350 mem_debug_header_t
*header
;
351 int mem_debug_chain_update
= 0;
353 return tcc_malloc_debug(size
, file
, line
);
354 header
= malloc_check(ptr
, "tcc_realloc");
355 mem_cur_size
-= header
->size
;
356 mem_debug_chain_update
= (header
== mem_debug_chain
);
357 header
= realloc(header
, sizeof(mem_debug_header_t
) + size
);
359 tcc_error("memory full (realloc)");
361 MEM_DEBUG_CHECK3(header
) = MEM_DEBUG_MAGIC3
;
363 header
->next
->prev
= header
;
365 header
->prev
->next
= header
;
366 if (mem_debug_chain_update
)
367 mem_debug_chain
= header
;
368 mem_cur_size
+= size
;
369 if (mem_cur_size
> mem_max_size
)
370 mem_max_size
= mem_cur_size
;
371 return MEM_USER_PTR(header
);
374 PUB_FUNC
char *tcc_strdup_debug(const char *str
, const char *file
, int line
)
377 ptr
= tcc_malloc_debug(strlen(str
) + 1, file
, line
);
382 PUB_FUNC
void tcc_memcheck(void)
385 mem_debug_header_t
*header
= mem_debug_chain
;
386 fprintf(stderr
, "MEM_DEBUG: mem_leak= %d bytes, mem_max_size= %d bytes\n",
387 mem_cur_size
, mem_max_size
);
389 fprintf(stderr
, "%s:%u: error: %u bytes leaked\n",
390 header
->file_name
, header
->line_num
, header
->size
);
391 header
= header
->next
;
398 #endif /* MEM_DEBUG */
400 #define free(p) use_tcc_free(p)
401 #define malloc(s) use_tcc_malloc(s)
402 #define realloc(p, s) use_tcc_realloc(p, s)
404 /********************************************************/
407 ST_FUNC
void dynarray_add(void *ptab
, int *nb_ptr
, void *data
)
413 pp
= *(void ***)ptab
;
414 /* every power of two we double array size */
415 if ((nb
& (nb
- 1)) == 0) {
420 pp
= tcc_realloc(pp
, nb_alloc
* sizeof(void *));
427 ST_FUNC
void dynarray_reset(void *pp
, int *n
)
430 for (p
= *(void***)pp
; *n
; ++p
, --*n
)
433 tcc_free(*(void**)pp
);
437 static void tcc_split_path(TCCState
*s
, void *p_ary
, int *p_nb_ary
, const char *in
)
445 for (p
= in
; c
= *p
, c
!= '\0' && c
!= PATHSEP
; ++p
) {
446 if (c
== '{' && p
[1] && p
[2] == '}') {
449 cstr_cat(&str
, s
->tcc_lib_path
, -1);
455 cstr_ccat(&str
, '\0');
456 dynarray_add(p_ary
, p_nb_ary
, tcc_strdup(str
.data
));
463 /********************************************************/
465 static void strcat_vprintf(char *buf
, int buf_size
, const char *fmt
, va_list ap
)
469 vsnprintf(buf
+ len
, buf_size
- len
, fmt
, ap
);
472 static void strcat_printf(char *buf
, int buf_size
, const char *fmt
, ...)
476 strcat_vprintf(buf
, buf_size
, fmt
, ap
);
480 static void error1(TCCState
*s1
, int is_warning
, const char *fmt
, va_list ap
)
483 BufferedFile
**pf
, *f
;
486 /* use upper file if inline ":asm:" or token ":paste:" */
487 for (f
= file
; f
&& f
->filename
[0] == ':'; f
= f
->prev
)
490 for(pf
= s1
->include_stack
; pf
< s1
->include_stack_ptr
; pf
++)
491 strcat_printf(buf
, sizeof(buf
), "In file included from %s:%d:\n",
492 (*pf
)->filename
, (*pf
)->line_num
);
493 if (f
->line_num
> 0) {
494 strcat_printf(buf
, sizeof(buf
), "%s:%d: ",
495 f
->filename
, f
->line_num
- !!(tok_flags
& TOK_FLAG_BOL
));
497 strcat_printf(buf
, sizeof(buf
), "%s: ",
501 strcat_printf(buf
, sizeof(buf
), "tcc: ");
504 strcat_printf(buf
, sizeof(buf
), "warning: ");
506 strcat_printf(buf
, sizeof(buf
), "error: ");
507 strcat_vprintf(buf
, sizeof(buf
), fmt
, ap
);
509 if (!s1
->error_func
) {
510 /* default case: stderr */
511 if (s1
->ppfp
) /* print a newline during tcc -E */
512 fprintf(s1
->ppfp
, "\n"), fflush(s1
->ppfp
);
513 fflush(stdout
); /* flush -v output */
514 fprintf(stderr
, "%s\n", buf
);
515 fflush(stderr
); /* print error/warning now (win32) */
517 s1
->error_func(s1
->error_opaque
, buf
);
519 if (!is_warning
|| s1
->warn_error
)
523 LIBTCCAPI
void tcc_set_error_func(TCCState
*s
, void *error_opaque
,
524 void (*error_func
)(void *opaque
, const char *msg
))
526 s
->error_opaque
= error_opaque
;
527 s
->error_func
= error_func
;
530 /* error without aborting current compilation */
531 PUB_FUNC
void tcc_error_noabort(const char *fmt
, ...)
533 TCCState
*s1
= tcc_state
;
537 error1(s1
, 0, fmt
, ap
);
541 PUB_FUNC
void tcc_error(const char *fmt
, ...)
543 TCCState
*s1
= tcc_state
;
547 error1(s1
, 0, fmt
, ap
);
549 /* better than nothing: in some cases, we accept to handle errors */
550 if (s1
->error_set_jmp_enabled
) {
551 longjmp(s1
->error_jmp_buf
, 1);
553 /* XXX: eliminate this someday */
558 PUB_FUNC
void tcc_warning(const char *fmt
, ...)
560 TCCState
*s1
= tcc_state
;
567 error1(s1
, 1, fmt
, ap
);
571 /********************************************************/
574 ST_FUNC
void tcc_open_bf(TCCState
*s1
, const char *filename
, int initlen
)
577 int buflen
= initlen
? initlen
: IO_BUF_SIZE
;
579 bf
= tcc_mallocz(sizeof(BufferedFile
) + buflen
);
580 bf
->buf_ptr
= bf
->buffer
;
581 bf
->buf_end
= bf
->buffer
+ initlen
;
582 bf
->buf_end
[0] = CH_EOB
; /* put eob symbol */
583 pstrcpy(bf
->filename
, sizeof(bf
->filename
), filename
);
584 pstrcpy(bf
->filename2
, sizeof(bf
->filename2
), filename
);
586 normalize_slashes(bf
->filename
);
589 bf
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
595 ST_FUNC
void tcc_close(void)
597 BufferedFile
*bf
= file
;
600 total_lines
+= bf
->line_num
;
606 ST_FUNC
int tcc_open(TCCState
*s1
, const char *filename
)
609 if (strcmp(filename
, "-") == 0)
610 fd
= 0, filename
= "<stdin>";
612 fd
= open(filename
, O_RDONLY
| O_BINARY
);
613 if ((s1
->verbose
== 2 && fd
>= 0) || s1
->verbose
== 3)
614 printf("%s %*s%s\n", fd
< 0 ? "nf":"->",
615 (int)(s1
->include_stack_ptr
- s1
->include_stack
), "", filename
);
619 tcc_open_bf(s1
, filename
, 0);
624 /* compile the C file opened in 'file'. Return non zero if errors. */
625 static int tcc_compile(TCCState
*s1
)
629 define_start
= define_stack
;
630 if (setjmp(s1
->error_jmp_buf
) == 0) {
632 s1
->error_set_jmp_enabled
= 1;
634 preprocess_start(s1
);
638 printf("%s: **** new file\n", file
->filename
);
641 ch
= file
->buf_ptr
[0];
642 tok_flags
= TOK_FLAG_BOL
| TOK_FLAG_BOF
;
643 parse_flags
= PARSE_FLAG_PREPROCESS
| PARSE_FLAG_TOK_NUM
| PARSE_FLAG_TOK_STR
;
647 expect("declaration");
648 /* free defines here already on behalf of of M.M.'s possibly existing
649 experimental preprocessor implementation. The normal call below
650 is still there to free after error-longjmp */
651 free_defines(define_start
);
654 s1
->error_set_jmp_enabled
= 0;
656 free_inline_functions(s1
);
657 /* reset define stack, but keep -D and built-ins */
658 free_defines(define_start
);
659 sym_pop(&global_stack
, NULL
, 0);
660 sym_pop(&local_stack
, NULL
, 0);
661 return s1
->nb_errors
!= 0 ? -1 : 0;
664 LIBTCCAPI
int tcc_compile_string(TCCState
*s
, const char *str
)
669 tcc_open_bf(s
, "<string>", len
);
670 memcpy(file
->buffer
, str
, len
);
671 ret
= tcc_compile(s
);
676 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
677 LIBTCCAPI
void tcc_define_symbol(TCCState
*s1
, const char *sym
, const char *value
)
684 len2
= strlen(value
);
686 /* init file structure */
687 tcc_open_bf(s1
, "<define>", len1
+ len2
+ 1);
688 memcpy(file
->buffer
, sym
, len1
);
689 file
->buffer
[len1
] = ' ';
690 memcpy(file
->buffer
+ len1
+ 1, value
, len2
);
692 /* parse with define parser */
693 ch
= file
->buf_ptr
[0];
700 /* undefine a preprocessor symbol */
701 LIBTCCAPI
void tcc_undefine_symbol(TCCState
*s1
, const char *sym
)
705 ts
= tok_alloc(sym
, strlen(sym
));
706 s
= define_find(ts
->tok
);
707 /* undefine symbol by putting an invalid name */
712 /* cleanup all static data used during compilation */
713 static void tcc_cleanup(void)
715 if (NULL
== tcc_state
)
717 tccpp_delete(tcc_state
);
720 dynarray_reset(&sym_pools
, &nb_sym_pools
);
721 /* reset symbol stack */
722 sym_free_first
= NULL
;
725 LIBTCCAPI TCCState
*tcc_new(void)
731 s
= tcc_mallocz(sizeof(TCCState
));
737 s
->alacarte_link
= 1;
739 s
->warn_implicit_function_declaration
= 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
);
766 /* define __TINYC__ 92X */
767 char buffer
[32]; int a
,b
,c
;
768 sscanf(TCC_VERSION
, "%d.%d.%d", &a
, &b
, &c
);
769 sprintf(buffer
, "%d", a
*10000 + b
*100 + c
);
770 tcc_define_symbol(s
, "__TINYC__", buffer
);
773 /* standard defines */
774 tcc_define_symbol(s
, "__STDC__", NULL
);
775 tcc_define_symbol(s
, "__STDC_VERSION__", "199901L");
776 tcc_define_symbol(s
, "__STDC_HOSTED__", NULL
);
779 #if defined(TCC_TARGET_I386)
780 tcc_define_symbol(s
, "__i386__", NULL
);
781 tcc_define_symbol(s
, "__i386", NULL
);
782 tcc_define_symbol(s
, "i386", NULL
);
783 #elif defined(TCC_TARGET_X86_64)
784 tcc_define_symbol(s
, "__x86_64__", NULL
);
785 #elif defined(TCC_TARGET_ARM)
786 tcc_define_symbol(s
, "__ARM_ARCH_4__", NULL
);
787 tcc_define_symbol(s
, "__arm_elf__", NULL
);
788 tcc_define_symbol(s
, "__arm_elf", NULL
);
789 tcc_define_symbol(s
, "arm_elf", NULL
);
790 tcc_define_symbol(s
, "__arm__", NULL
);
791 tcc_define_symbol(s
, "__arm", NULL
);
792 tcc_define_symbol(s
, "arm", NULL
);
793 tcc_define_symbol(s
, "__APCS_32__", NULL
);
794 tcc_define_symbol(s
, "__ARMEL__", NULL
);
795 #if defined(TCC_ARM_EABI)
796 tcc_define_symbol(s
, "__ARM_EABI__", NULL
);
798 #if defined(TCC_ARM_HARDFLOAT)
799 s
->float_abi
= ARM_HARD_FLOAT
;
800 tcc_define_symbol(s
, "__ARM_PCS_VFP", NULL
);
802 s
->float_abi
= ARM_SOFTFP_FLOAT
;
804 #elif defined(TCC_TARGET_ARM64)
805 tcc_define_symbol(s
, "__aarch64__", NULL
);
809 tcc_define_symbol(s
, "_WIN32", NULL
);
810 # ifdef TCC_TARGET_X86_64
811 tcc_define_symbol(s
, "_WIN64", NULL
);
814 tcc_define_symbol(s
, "__unix__", NULL
);
815 tcc_define_symbol(s
, "__unix", NULL
);
816 tcc_define_symbol(s
, "unix", NULL
);
817 # if defined(__linux__)
818 tcc_define_symbol(s
, "__linux__", NULL
);
819 tcc_define_symbol(s
, "__linux", NULL
);
821 # if defined(__FreeBSD__)
822 tcc_define_symbol(s
, "__FreeBSD__", "__FreeBSD__");
823 /* No 'Thread Storage Local' on FreeBSD with tcc */
824 tcc_define_symbol(s
, "__NO_TLS", NULL
);
826 # if defined(__FreeBSD_kernel__)
827 tcc_define_symbol(s
, "__FreeBSD_kernel__", NULL
);
830 # if defined(__NetBSD__)
831 tcc_define_symbol(s
, "__NetBSD__", "__NetBSD__");
833 # if defined(__OpenBSD__)
834 tcc_define_symbol(s
, "__OpenBSD__", "__OpenBSD__");
837 /* TinyCC & gcc defines */
838 #if defined(TCC_TARGET_PE) && defined(TCC_TARGET_X86_64)
840 tcc_define_symbol(s
, "__SIZE_TYPE__", "unsigned long long");
841 tcc_define_symbol(s
, "__PTRDIFF_TYPE__", "long long");
842 tcc_define_symbol(s
, "__LLP64__", NULL
);
843 #elif defined(TCC_TARGET_X86_64) || defined(TCC_TARGET_ARM64)
844 /* Other 64bit systems. */
845 tcc_define_symbol(s
, "__SIZE_TYPE__", "unsigned long");
846 tcc_define_symbol(s
, "__PTRDIFF_TYPE__", "long");
847 tcc_define_symbol(s
, "__LP64__", NULL
);
849 /* Other 32bit systems. */
850 tcc_define_symbol(s
, "__SIZE_TYPE__", "unsigned long");
851 tcc_define_symbol(s
, "__PTRDIFF_TYPE__", "long");
852 tcc_define_symbol(s
, "__ILP32__", NULL
);
855 #if defined(TCC_MUSL)
856 tcc_define_symbol(s
, "__builtin_va_list", "void *");
857 #endif /* TCC_MUSL */
860 tcc_define_symbol(s
, "__WCHAR_TYPE__", "unsigned short");
861 tcc_define_symbol(s
, "__WINT_TYPE__", "unsigned short");
863 tcc_define_symbol(s
, "__WCHAR_TYPE__", "int");
864 /* wint_t is unsigned int by default, but (signed) int on BSDs
865 and unsigned short on windows. Other OSes might have still
866 other conventions, sigh. */
867 # if defined(__FreeBSD__) || defined (__FreeBSD_kernel__) \
868 || defined(__NetBSD__) || defined(__OpenBSD__)
869 tcc_define_symbol(s
, "__WINT_TYPE__", "int");
871 /* define __GNUC__ to have some useful stuff from sys/cdefs.h
872 that are unconditionally used in FreeBSDs other system headers :/ */
873 tcc_define_symbol(s
, "__GNUC__", "2");
874 tcc_define_symbol(s
, "__GNUC_MINOR__", "7");
875 tcc_define_symbol(s
, "__builtin_alloca", "alloca");
878 tcc_define_symbol(s
, "__WINT_TYPE__", "unsigned int");
880 tcc_define_symbol(s
, "__REDIRECT(name, proto, alias)",
881 "name proto __asm__ (#alias)");
882 tcc_define_symbol(s
, "__REDIRECT_NTH(name, proto, alias)",
883 "name proto __asm__ (#alias) __THROW");
885 /* Some GCC builtins that are simple to express as macros. */
886 tcc_define_symbol(s
, "__builtin_extract_return_addr(x)", "x");
887 #endif /* ndef TCC_TARGET_PE */
891 LIBTCCAPI
void tcc_delete(TCCState
*s1
)
898 /* free library paths */
899 dynarray_reset(&s1
->library_paths
, &s1
->nb_library_paths
);
900 dynarray_reset(&s1
->crt_paths
, &s1
->nb_crt_paths
);
902 /* free include paths */
903 dynarray_reset(&s1
->cached_includes
, &s1
->nb_cached_includes
);
904 dynarray_reset(&s1
->include_paths
, &s1
->nb_include_paths
);
905 dynarray_reset(&s1
->sysinclude_paths
, &s1
->nb_sysinclude_paths
);
906 dynarray_reset(&s1
->cmd_include_files
, &s1
->nb_cmd_include_files
);
908 tcc_free(s1
->tcc_lib_path
);
909 tcc_free(s1
->soname
);
911 tcc_free(s1
->init_symbol
);
912 tcc_free(s1
->fini_symbol
);
913 tcc_free(s1
->outfile
);
914 tcc_free(s1
->deps_outfile
);
915 dynarray_reset(&s1
->files
, &s1
->nb_files
);
916 dynarray_reset(&s1
->target_deps
, &s1
->nb_target_deps
);
917 dynarray_reset(&s1
->pragma_libs
, &s1
->nb_pragma_libs
);
918 dynarray_reset(&s1
->argv
, &s1
->argc
);
921 /* free runtime memory */
926 if (0 == --nb_states
)
930 LIBTCCAPI
int tcc_set_output_type(TCCState
*s
, int output_type
)
932 s
->output_type
= output_type
;
934 /* always elf for objects */
935 if (output_type
== TCC_OUTPUT_OBJ
)
936 s
->output_format
= TCC_OUTPUT_FORMAT_ELF
;
938 if (s
->char_is_unsigned
)
939 tcc_define_symbol(s
, "__CHAR_UNSIGNED__", NULL
);
942 /* default include paths */
943 /* -isystem paths have already been handled */
944 tcc_add_sysinclude_path(s
, CONFIG_TCC_SYSINCLUDEPATHS
);
947 #ifdef CONFIG_TCC_BCHECK
948 if (s
->do_bounds_check
) {
949 /* if bound checking, then add corresponding sections */
950 tccelf_bounds_new(s
);
952 tcc_define_symbol(s
, "__BOUNDS_CHECKING_ON", NULL
);
956 /* add debug sections */
960 tcc_add_library_path(s
, CONFIG_TCC_LIBPATHS
);
964 if (!s
->nostdlib
&& output_type
!= TCC_OUTPUT_OBJ
)
965 tcc_add_systemdir(s
);
968 /* paths for crt objects */
969 tcc_split_path(s
, &s
->crt_paths
, &s
->nb_crt_paths
, CONFIG_TCC_CRTPREFIX
);
970 /* add libc crt1/crti objects */
971 if ((output_type
== TCC_OUTPUT_EXE
|| output_type
== TCC_OUTPUT_DLL
) &&
973 if (output_type
!= TCC_OUTPUT_DLL
)
974 tcc_add_crt(s
, "crt1.o");
975 tcc_add_crt(s
, "crti.o");
981 LIBTCCAPI
int tcc_add_include_path(TCCState
*s
, const char *pathname
)
983 tcc_split_path(s
, &s
->include_paths
, &s
->nb_include_paths
, pathname
);
987 LIBTCCAPI
int tcc_add_sysinclude_path(TCCState
*s
, const char *pathname
)
989 tcc_split_path(s
, &s
->sysinclude_paths
, &s
->nb_sysinclude_paths
, pathname
);
993 ST_FUNC
int tcc_add_file_internal(TCCState
*s1
, const char *filename
, int flags
)
997 filetype
= flags
& 0x0F;
999 /* use a file extension to detect a filetype */
1000 const char *ext
= tcc_fileextension(filename
);
1003 if (!strcmp(ext
, "S"))
1004 filetype
= AFF_TYPE_ASMPP
;
1005 else if (!strcmp(ext
, "s"))
1006 filetype
= AFF_TYPE_ASM
;
1007 else if (!PATHCMP(ext
, "c") || !PATHCMP(ext
, "i"))
1008 filetype
= AFF_TYPE_C
;
1010 filetype
= AFF_TYPE_BIN
;
1012 filetype
= AFF_TYPE_C
;
1017 ret
= tcc_open(s1
, filename
);
1019 if (flags
& AFF_PRINT_ERROR
)
1020 tcc_error_noabort("file '%s' not found", filename
);
1024 /* update target deps */
1025 dynarray_add(&s1
->target_deps
, &s1
->nb_target_deps
,
1026 tcc_strdup(filename
));
1029 /* if .S file, define __ASSEMBLER__ like gcc does */
1030 if (filetype
== AFF_TYPE_ASM
|| filetype
== AFF_TYPE_ASMPP
) {
1031 tcc_define_symbol(s1
, "__ASSEMBLER__", NULL
);
1032 parse_flags
= PARSE_FLAG_ASM_FILE
;
1035 if (flags
& AFF_PREPROCESS
) {
1036 ret
= tcc_preprocess(s1
);
1037 } else if (filetype
== AFF_TYPE_C
) {
1038 ret
= tcc_compile(s1
);
1039 #ifdef CONFIG_TCC_ASM
1040 } else if (filetype
== AFF_TYPE_ASMPP
) {
1041 /* non preprocessed assembler */
1042 ret
= tcc_assemble(s1
, 1);
1043 } else if (filetype
== AFF_TYPE_ASM
) {
1044 /* preprocessed assembler */
1045 ret
= tcc_assemble(s1
, 0);
1052 obj_type
= tcc_object_type(fd
, &ehdr
);
1053 lseek(fd
, 0, SEEK_SET
);
1055 /* do not display line number if error */
1058 #ifdef TCC_TARGET_MACHO
1059 if (0 == obj_type
&& 0 == strcmp(tcc_fileextension(filename
), "dylib"))
1060 obj_type
= AFF_BINTYPE_DYN
;
1064 case AFF_BINTYPE_REL
:
1065 ret
= tcc_load_object_file(s1
, fd
, 0);
1067 #ifndef TCC_TARGET_PE
1068 case AFF_BINTYPE_DYN
:
1069 if (s1
->output_type
== TCC_OUTPUT_MEMORY
) {
1071 #ifdef TCC_IS_NATIVE
1072 if (NULL
== dlopen(filename
, RTLD_GLOBAL
| RTLD_LAZY
))
1076 ret
= tcc_load_dll(s1
, fd
, filename
,
1077 (flags
& AFF_REFERENCED_DLL
) != 0);
1081 case AFF_BINTYPE_AR
:
1082 ret
= tcc_load_archive(s1
, fd
);
1084 #ifdef TCC_TARGET_COFF
1085 case AFF_BINTYPE_C67
:
1086 ret
= tcc_load_coff(s1
, fd
);
1090 #ifdef TCC_TARGET_PE
1091 ret
= pe_load_file(s1
, filename
, fd
);
1093 /* as GNU ld, consider it is an ld script if not recognized */
1094 ret
= tcc_load_ldscript(s1
);
1097 tcc_error_noabort("unrecognized file type");
1105 LIBTCCAPI
int tcc_add_file(TCCState
*s
, const char *filename
)
1107 if (s
->output_type
== TCC_OUTPUT_PREPROCESS
)
1108 return tcc_add_file_internal(s
, filename
, AFF_PRINT_ERROR
| AFF_PREPROCESS
| s
->filetype
);
1110 return tcc_add_file_internal(s
, filename
, AFF_PRINT_ERROR
| s
->filetype
);
1113 LIBTCCAPI
int tcc_add_library_path(TCCState
*s
, const char *pathname
)
1115 tcc_split_path(s
, &s
->library_paths
, &s
->nb_library_paths
, pathname
);
1119 static int tcc_add_library_internal(TCCState
*s
, const char *fmt
,
1120 const char *filename
, int flags
, char **paths
, int nb_paths
)
1125 for(i
= 0; i
< nb_paths
; i
++) {
1126 snprintf(buf
, sizeof(buf
), fmt
, paths
[i
], filename
);
1127 if (tcc_add_file_internal(s
, buf
, flags
| AFF_TYPE_BIN
) == 0)
1133 /* find and load a dll. Return non zero if not found */
1134 /* XXX: add '-rpath' option support ? */
1135 ST_FUNC
int tcc_add_dll(TCCState
*s
, const char *filename
, int flags
)
1137 return tcc_add_library_internal(s
, "%s/%s", filename
, flags
,
1138 s
->library_paths
, s
->nb_library_paths
);
1141 ST_FUNC
int tcc_add_crt(TCCState
*s
, const char *filename
)
1143 if (-1 == tcc_add_library_internal(s
, "%s/%s",
1144 filename
, 0, s
->crt_paths
, s
->nb_crt_paths
))
1145 tcc_error_noabort("file '%s' not found", filename
);
1149 /* the library name is the same as the argument of the '-l' option */
1150 LIBTCCAPI
int tcc_add_library(TCCState
*s
, const char *libraryname
)
1152 #if defined TCC_TARGET_PE
1153 const char *libs
[] = { "%s/%s.def", "%s/lib%s.def", "%s/%s.dll", "%s/lib%s.dll", "%s/lib%s.a", NULL
};
1154 const char **pp
= s
->static_link
? libs
+ 4 : libs
;
1155 #elif defined TCC_TARGET_MACHO
1156 const char *libs
[] = { "%s/lib%s.dylib", "%s/lib%s.a", NULL
};
1157 const char **pp
= s
->static_link
? libs
+ 1 : libs
;
1159 const char *libs
[] = { "%s/lib%s.so", "%s/lib%s.a", NULL
};
1160 const char **pp
= s
->static_link
? libs
+ 1 : libs
;
1163 if (0 == tcc_add_library_internal(s
, *pp
,
1164 libraryname
, 0, s
->library_paths
, s
->nb_library_paths
))
1171 PUB_FUNC
int tcc_add_library_err(TCCState
*s
, const char *libname
)
1173 int ret
= tcc_add_library(s
, libname
);
1175 tcc_error_noabort("library '%s' not found", libname
);
1179 /* handle #pragma comment(lib,) */
1180 ST_FUNC
void tcc_add_pragma_libs(TCCState
*s1
)
1183 for (i
= 0; i
< s1
->nb_pragma_libs
; i
++)
1184 tcc_add_library_err(s1
, s1
->pragma_libs
[i
]);
1187 LIBTCCAPI
int tcc_add_symbol(TCCState
*s
, const char *name
, const void *val
)
1189 #ifdef TCC_TARGET_PE
1190 /* On x86_64 'val' might not be reachable with a 32bit offset.
1191 So it is handled here as if it were in a DLL. */
1192 pe_putimport(s
, 0, name
, (uintptr_t)val
);
1194 set_elf_sym(symtab_section
, (uintptr_t)val
, 0,
1195 ELFW(ST_INFO
)(STB_GLOBAL
, STT_NOTYPE
), 0,
1201 LIBTCCAPI
void tcc_set_lib_path(TCCState
*s
, const char *path
)
1203 tcc_free(s
->tcc_lib_path
);
1204 s
->tcc_lib_path
= tcc_strdup(path
);
1207 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
1208 #define FD_INVERT 0x0002 /* invert value before storing */
1210 typedef struct FlagDef
{
1216 static int no_flag(const char **pp
)
1218 const char *p
= *pp
;
1219 if (*p
!= 'n' || *++p
!= 'o' || *++p
!= '-')
1225 ST_FUNC
int set_flag(TCCState
*s
, const FlagDef
*flags
, const char *name
)
1236 for (ret
= -1, p
= flags
; p
->name
; ++p
) {
1238 if (strcmp(r
, p
->name
))
1241 if (0 == (p
->flags
& WD_ALL
))
1245 *(int*)((char *)s
+ p
->offset
) =
1246 p
->flags
& FD_INVERT
? !value
: value
;
1256 static int strstart(const char *val
, const char **str
)
1271 /* Like strstart, but automatically takes into account that ld options can
1273 * - start with double or single dash (e.g. '--soname' or '-soname')
1274 * - arguments can be given as separate or after '=' (e.g. '-Wl,-soname,x.so'
1275 * or '-Wl,-soname=x.so')
1277 * you provide `val` always in 'option[=]' form (no leading -)
1279 static int link_option(const char *str
, const char *val
, const char **ptr
)
1284 /* there should be 1 or 2 dashes */
1290 /* then str & val should match (potentially up to '=') */
1301 while (*q
!= '\0' && *q
!= '=') {
1308 /* '=' near eos means ',' or '=' is ok */
1312 if (*p
!= ',' && *p
!= '=')
1322 static const char *skip_linker_arg(const char **str
)
1324 const char *s1
= *str
;
1325 const char *s2
= strchr(s1
, ',');
1326 *str
= s2
? s2
++ : (s2
= s1
+ strlen(s1
));
1330 static void copy_linker_arg(char **pp
, const char *s
, int sep
)
1336 p
[l
= strlen(p
)] = sep
, ++l
;
1337 skip_linker_arg(&q
);
1338 pstrncpy(l
+ (*pp
= tcc_realloc(p
, q
- s
+ l
+ 1)), s
, q
- s
);
1341 /* set linker options */
1342 static int tcc_set_linker(TCCState
*s
, const char *option
)
1346 const char *p
= NULL
;
1351 if (link_option(option
, "Bsymbolic", &p
)) {
1353 } else if (link_option(option
, "nostdlib", &p
)) {
1355 } else if (link_option(option
, "fini=", &p
)) {
1356 copy_linker_arg(&s
->fini_symbol
, p
, 0);
1358 } else if (link_option(option
, "image-base=", &p
)
1359 || link_option(option
, "Ttext=", &p
)) {
1360 s
->text_addr
= strtoull(p
, &end
, 16);
1361 s
->has_text_addr
= 1;
1362 } else if (link_option(option
, "init=", &p
)) {
1363 copy_linker_arg(&s
->init_symbol
, p
, 0);
1365 } else if (link_option(option
, "oformat=", &p
)) {
1366 #if defined(TCC_TARGET_PE)
1367 if (strstart("pe-", &p
)) {
1368 #elif defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1369 if (strstart("elf64-", &p
)) {
1371 if (strstart("elf32-", &p
)) {
1373 s
->output_format
= TCC_OUTPUT_FORMAT_ELF
;
1374 } else if (!strcmp(p
, "binary")) {
1375 s
->output_format
= TCC_OUTPUT_FORMAT_BINARY
;
1376 #ifdef TCC_TARGET_COFF
1377 } else if (!strcmp(p
, "coff")) {
1378 s
->output_format
= TCC_OUTPUT_FORMAT_COFF
;
1383 } else if (link_option(option
, "as-needed", &p
)) {
1385 } else if (link_option(option
, "O", &p
)) {
1387 } else if (link_option(option
, "export-all-symbols", &p
)) {
1389 } else if (link_option(option
, "rpath=", &p
)) {
1390 copy_linker_arg(&s
->rpath
, p
, ':');
1391 } else if (link_option(option
, "enable-new-dtags", &p
)) {
1392 s
->enable_new_dtags
= 1;
1393 } else if (link_option(option
, "section-alignment=", &p
)) {
1394 s
->section_align
= strtoul(p
, &end
, 16);
1395 } else if (link_option(option
, "soname=", &p
)) {
1396 copy_linker_arg(&s
->soname
, p
, 0);
1397 #ifdef TCC_TARGET_PE
1398 } else if (link_option(option
, "large-address-aware", &p
)) {
1399 s
->pe_characteristics
|= 0x20;
1400 } else if (link_option(option
, "file-alignment=", &p
)) {
1401 s
->pe_file_align
= strtoul(p
, &end
, 16);
1402 } else if (link_option(option
, "stack=", &p
)) {
1403 s
->pe_stack_size
= strtoul(p
, &end
, 10);
1404 } else if (link_option(option
, "subsystem=", &p
)) {
1405 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1406 if (!strcmp(p
, "native")) {
1407 s
->pe_subsystem
= 1;
1408 } else if (!strcmp(p
, "console")) {
1409 s
->pe_subsystem
= 3;
1410 } else if (!strcmp(p
, "gui") || !strcmp(p
, "windows")) {
1411 s
->pe_subsystem
= 2;
1412 } else if (!strcmp(p
, "posix")) {
1413 s
->pe_subsystem
= 7;
1414 } else if (!strcmp(p
, "efiapp")) {
1415 s
->pe_subsystem
= 10;
1416 } else if (!strcmp(p
, "efiboot")) {
1417 s
->pe_subsystem
= 11;
1418 } else if (!strcmp(p
, "efiruntime")) {
1419 s
->pe_subsystem
= 12;
1420 } else if (!strcmp(p
, "efirom")) {
1421 s
->pe_subsystem
= 13;
1422 #elif defined(TCC_TARGET_ARM)
1423 if (!strcmp(p
, "wince")) {
1424 s
->pe_subsystem
= 9;
1429 } else if (ret
= link_option(option
, "?whole-archive", &p
), ret
) {
1430 s
->alacarte_link
= ret
< 0;
1435 tcc_error("unsupported linker option '%s'", option
);
1438 if (ignoring
&& s
->warn_unsupported
)
1439 tcc_warning("unsupported linker option '%s'", option
);
1441 option
= skip_linker_arg(&p
);
1446 typedef struct TCCOption
{
1468 TCC_OPTION_dumpversion
,
1477 TCC_OPTION_traditional
,
1482 TCC_OPTION_mfloat_abi
,
1486 TCC_OPTION_iwithprefix
,
1488 TCC_OPTION_nostdinc
,
1489 TCC_OPTION_nostdlib
,
1490 TCC_OPTION_print_search_dirs
,
1491 TCC_OPTION_rdynamic
,
1493 TCC_OPTION_pedantic
,
1506 #define TCC_OPTION_HAS_ARG 0x0001
1507 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
1509 static const TCCOption tcc_options
[] = {
1510 { "h", TCC_OPTION_HELP
, 0 },
1511 { "-help", TCC_OPTION_HELP
, 0 },
1512 { "?", TCC_OPTION_HELP
, 0 },
1513 { "hh", TCC_OPTION_HELP2
, 0 },
1514 { "v", TCC_OPTION_v
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1515 { "I", TCC_OPTION_I
, TCC_OPTION_HAS_ARG
},
1516 { "D", TCC_OPTION_D
, TCC_OPTION_HAS_ARG
},
1517 { "U", TCC_OPTION_U
, TCC_OPTION_HAS_ARG
},
1518 { "P", TCC_OPTION_P
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1519 { "L", TCC_OPTION_L
, TCC_OPTION_HAS_ARG
},
1520 { "B", TCC_OPTION_B
, TCC_OPTION_HAS_ARG
},
1521 { "l", TCC_OPTION_l
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1522 { "bench", TCC_OPTION_bench
, 0 },
1523 #ifdef CONFIG_TCC_BACKTRACE
1524 { "bt", TCC_OPTION_bt
, TCC_OPTION_HAS_ARG
},
1526 #ifdef CONFIG_TCC_BCHECK
1527 { "b", TCC_OPTION_b
, 0 },
1529 { "g", TCC_OPTION_g
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1530 { "c", TCC_OPTION_c
, 0 },
1531 { "dumpversion", TCC_OPTION_dumpversion
, 0},
1532 { "d", TCC_OPTION_d
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1533 { "static", TCC_OPTION_static
, 0 },
1534 { "std", TCC_OPTION_std
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1535 { "shared", TCC_OPTION_shared
, 0 },
1536 { "soname", TCC_OPTION_soname
, TCC_OPTION_HAS_ARG
},
1537 { "o", TCC_OPTION_o
, TCC_OPTION_HAS_ARG
},
1538 { "-param", TCC_OPTION_param
, TCC_OPTION_HAS_ARG
},
1539 { "pedantic", TCC_OPTION_pedantic
, 0},
1540 { "pthread", TCC_OPTION_pthread
, 0},
1541 { "run", TCC_OPTION_run
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1542 { "rdynamic", TCC_OPTION_rdynamic
, 0 },
1543 { "r", TCC_OPTION_r
, 0 },
1544 { "s", TCC_OPTION_s
, 0 },
1545 { "traditional", TCC_OPTION_traditional
, 0 },
1546 { "Wl,", TCC_OPTION_Wl
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1547 { "Wp,", TCC_OPTION_Wp
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1548 { "W", TCC_OPTION_W
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1549 { "O", TCC_OPTION_O
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1550 #ifdef TCC_TARGET_ARM
1551 { "mfloat-abi", TCC_OPTION_mfloat_abi
, TCC_OPTION_HAS_ARG
},
1553 { "m", TCC_OPTION_m
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1554 { "f", TCC_OPTION_f
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1555 { "isystem", TCC_OPTION_isystem
, TCC_OPTION_HAS_ARG
},
1556 { "iwithprefix", TCC_OPTION_iwithprefix
, TCC_OPTION_HAS_ARG
},
1557 { "include", TCC_OPTION_include
, TCC_OPTION_HAS_ARG
},
1558 { "nostdinc", TCC_OPTION_nostdinc
, 0 },
1559 { "nostdlib", TCC_OPTION_nostdlib
, 0 },
1560 { "print-search-dirs", TCC_OPTION_print_search_dirs
, 0 },
1561 { "w", TCC_OPTION_w
, 0 },
1562 { "pipe", TCC_OPTION_pipe
, 0},
1563 { "E", TCC_OPTION_E
, 0},
1564 { "MD", TCC_OPTION_MD
, 0},
1565 { "MF", TCC_OPTION_MF
, TCC_OPTION_HAS_ARG
},
1566 { "x", TCC_OPTION_x
, TCC_OPTION_HAS_ARG
},
1567 { "ar", TCC_OPTION_ar
, 0},
1568 #ifdef TCC_TARGET_PE
1569 { "impdef", TCC_OPTION_impdef
, 0},
1574 static const FlagDef options_W
[] = {
1576 { offsetof(TCCState
, warn_unsupported
), 0, "unsupported" },
1577 { offsetof(TCCState
, warn_write_strings
), 0, "write-strings" },
1578 { offsetof(TCCState
, warn_error
), 0, "error" },
1579 { offsetof(TCCState
, warn_implicit_function_declaration
), WD_ALL
,
1580 "implicit-function-declaration" },
1584 static const FlagDef options_f
[] = {
1585 { offsetof(TCCState
, char_is_unsigned
), 0, "unsigned-char" },
1586 { offsetof(TCCState
, char_is_unsigned
), FD_INVERT
, "signed-char" },
1587 { offsetof(TCCState
, nocommon
), FD_INVERT
, "common" },
1588 { offsetof(TCCState
, leading_underscore
), 0, "leading-underscore" },
1589 { offsetof(TCCState
, ms_extensions
), 0, "ms-extensions" },
1590 { offsetof(TCCState
, dollars_in_identifiers
), 0, "dollars-in-identifiers" },
1594 static const FlagDef options_m
[] = {
1595 { offsetof(TCCState
, ms_bitfields
), 0, "ms-bitfields" },
1596 #ifdef TCC_TARGET_X86_64
1597 { offsetof(TCCState
, nosse
), FD_INVERT
, "sse" },
1602 static void parse_option_D(TCCState
*s1
, const char *optarg
)
1604 char *sym
= tcc_strdup(optarg
);
1605 char *value
= strchr(sym
, '=');
1608 tcc_define_symbol(s1
, sym
, value
);
1612 static void args_parser_add_file(TCCState
*s
, const char* filename
, int filetype
)
1614 struct filespec
*f
= tcc_malloc(sizeof *f
+ strlen(filename
));
1616 f
->alacarte
= s
->alacarte_link
;
1617 strcpy(f
->name
, filename
);
1618 dynarray_add(&s
->files
, &s
->nb_files
, f
);
1621 static int args_parser_make_argv(const char *r
, int *argc
, char ***argv
)
1626 while (c
= (unsigned char)*r
, c
&& c
<= ' ')
1632 while (c
= (unsigned char)*r
, c
) {
1634 if (c
== '\\' && (*r
== '"' || *r
== '\\')) {
1636 } else if (c
== '"') {
1639 } else if (q
== 0 && c
<= ' ') {
1645 //printf("<%s>\n", str.data), fflush(stdout);
1646 dynarray_add(argv
, argc
, tcc_strdup(str
.data
));
1653 /* read list file */
1654 static void args_parser_listfile(TCCState
*s
,
1655 const char *filename
, int optind
, int *pargc
, char ***pargv
)
1663 fd
= open(filename
, O_RDONLY
| O_BINARY
);
1665 tcc_error("listfile '%s' not found", filename
);
1667 len
= lseek(fd
, 0, SEEK_END
);
1668 p
= tcc_malloc(len
+ 1), p
[len
] = 0;
1669 lseek(fd
, 0, SEEK_SET
), read(fd
, p
, len
), close(fd
);
1671 for (i
= 0; i
< *pargc
; ++i
)
1673 args_parser_make_argv(p
, &argc
, &argv
);
1675 dynarray_add(&argv
, &argc
, tcc_strdup((*pargv
)[i
]));
1678 dynarray_reset(&s
->argv
, &s
->argc
);
1679 *pargc
= s
->argc
= argc
, *pargv
= s
->argv
= argv
;
1682 PUB_FUNC
int tcc_parse_args(TCCState
*s
, int *pargc
, char ***pargv
, int optind
)
1684 const TCCOption
*popt
;
1685 const char *optarg
, *r
;
1686 const char *run
= NULL
;
1689 CString linker_arg
; /* collect -Wl options */
1691 int tool
= 0, arg_start
= 0, noaction
= optind
;
1692 char **argv
= *pargv
;
1695 cstr_new(&linker_arg
);
1697 while (optind
< argc
) {
1699 if (r
[0] == '@' && r
[1] != '\0') {
1700 args_parser_listfile(s
, r
+ 1, optind
, &argc
, &argv
);
1705 if (r
[0] == '-' && r
[1] == 'v' && r
[2] == 0)
1710 if (r
[0] != '-' || r
[1] == '\0') {
1711 if (r
[0] != '@') /* allow "tcc file(s) -run @ args ..." */
1712 args_parser_add_file(s
, r
, s
->filetype
);
1714 tcc_set_options(s
, run
);
1715 arg_start
= optind
- 1;
1721 /* find option in table */
1722 for(popt
= tcc_options
; ; ++popt
) {
1723 const char *p1
= popt
->name
;
1724 const char *r1
= r
+ 1;
1726 tcc_error("invalid option -- '%s'", r
);
1727 if (!strstart(p1
, &r1
))
1730 if (popt
->flags
& TCC_OPTION_HAS_ARG
) {
1731 if (*r1
== '\0' && !(popt
->flags
& TCC_OPTION_NOSEP
)) {
1734 tcc_error("argument to '%s' is missing", r
);
1735 optarg
= argv
[optind
++];
1737 } else if (*r1
!= '\0')
1742 switch(popt
->index
) {
1743 case TCC_OPTION_HELP
:
1745 case TCC_OPTION_HELP2
:
1748 tcc_add_include_path(s
, optarg
);
1751 parse_option_D(s
, optarg
);
1754 tcc_undefine_symbol(s
, optarg
);
1757 tcc_add_library_path(s
, optarg
);
1760 /* set tcc utilities path (mainly for tcc development) */
1761 tcc_set_lib_path(s
, optarg
);
1764 args_parser_add_file(s
, optarg
, AFF_TYPE_LIB
);
1767 case TCC_OPTION_pthread
:
1768 parse_option_D(s
, "_REENTRANT");
1769 s
->option_pthread
= 1;
1771 case TCC_OPTION_bench
:
1774 #ifdef CONFIG_TCC_BACKTRACE
1776 tcc_set_num_callers(atoi(optarg
));
1779 #ifdef CONFIG_TCC_BCHECK
1781 s
->do_bounds_check
= 1;
1792 tcc_warning("-%s: overriding compiler action already specified", popt
->name
);
1798 else if (*optarg
== 'M')
1801 goto unsupported_option
;
1803 case TCC_OPTION_static
:
1806 case TCC_OPTION_std
:
1807 /* silently ignore, a current purpose:
1808 allow to use a tcc as a reference compiler for "make test" */
1810 case TCC_OPTION_shared
:
1812 goto set_output_type
;
1813 case TCC_OPTION_soname
:
1814 s
->soname
= tcc_strdup(optarg
);
1818 tcc_warning("multiple -o option");
1819 tcc_free(s
->outfile
);
1821 s
->outfile
= tcc_strdup(optarg
);
1824 /* generate a .o merging several output files */
1827 goto set_output_type
;
1828 case TCC_OPTION_isystem
:
1829 tcc_add_sysinclude_path(s
, optarg
);
1831 case TCC_OPTION_iwithprefix
:
1832 snprintf(buf
, sizeof buf
, "{B}/%s", optarg
);
1833 tcc_add_sysinclude_path(s
, buf
);
1835 case TCC_OPTION_include
:
1836 dynarray_add(&s
->cmd_include_files
,
1837 &s
->nb_cmd_include_files
, tcc_strdup(optarg
));
1839 case TCC_OPTION_nostdinc
:
1842 case TCC_OPTION_nostdlib
:
1845 case TCC_OPTION_run
:
1846 #ifndef TCC_IS_NATIVE
1847 tcc_error("-run is not available in a cross compiler");
1850 x
= TCC_OUTPUT_MEMORY
;
1851 goto set_output_type
;
1853 do ++s
->verbose
; while (*optarg
++ == 'v');
1857 if (set_flag(s
, options_f
, optarg
) < 0)
1858 goto unsupported_option
;
1860 #ifdef TCC_TARGET_ARM
1861 case TCC_OPTION_mfloat_abi
:
1862 /* tcc doesn't support soft float yet */
1863 if (!strcmp(optarg
, "softfp")) {
1864 s
->float_abi
= ARM_SOFTFP_FLOAT
;
1865 tcc_undefine_symbol(s
, "__ARM_PCS_VFP");
1866 } else if (!strcmp(optarg
, "hard"))
1867 s
->float_abi
= ARM_HARD_FLOAT
;
1869 tcc_error("unsupported float abi '%s'", optarg
);
1873 if (set_flag(s
, options_m
, optarg
) < 0) {
1874 if (x
= atoi(optarg
), x
!= 32 && x
!= 64)
1875 goto unsupported_option
;
1876 if (PTR_SIZE
!= x
/8)
1882 if (set_flag(s
, options_W
, optarg
) < 0)
1883 goto unsupported_option
;
1888 case TCC_OPTION_rdynamic
:
1892 if (linker_arg
.size
)
1893 --linker_arg
.size
, cstr_ccat(&linker_arg
, ',');
1894 cstr_cat(&linker_arg
, optarg
, 0);
1895 if (tcc_set_linker(s
, linker_arg
.data
))
1896 cstr_free(&linker_arg
);
1902 x
= TCC_OUTPUT_PREPROCESS
;
1903 goto set_output_type
;
1905 s
->Pflag
= atoi(optarg
) + 1;
1911 s
->deps_outfile
= tcc_strdup(optarg
);
1913 case TCC_OPTION_dumpversion
:
1914 printf ("%s\n", TCC_VERSION
);
1919 s
->filetype
= AFF_TYPE_C
;
1920 else if (*optarg
== 'a')
1921 s
->filetype
= AFF_TYPE_ASMPP
;
1922 else if (*optarg
== 'n')
1923 s
->filetype
= AFF_TYPE_NONE
;
1925 tcc_warning("unsupported language '%s'", optarg
);
1928 last_o
= atoi(optarg
);
1930 case TCC_OPTION_print_search_dirs
:
1933 case TCC_OPTION_impdef
:
1939 arg_start
= optind
- 1;
1940 if (arg_start
!= noaction
)
1941 tcc_error("cannot parse %s here", r
);
1944 case TCC_OPTION_traditional
:
1945 case TCC_OPTION_pedantic
:
1946 case TCC_OPTION_pipe
:
1952 if (s
->warn_unsupported
)
1953 tcc_warning("unsupported option '%s'", r
);
1958 tcc_define_symbol(s
, "__OPTIMIZE__", NULL
);
1959 if (linker_arg
.size
) {
1960 r
= linker_arg
.data
;
1963 *pargc
= argc
- arg_start
;
1964 *pargv
= argv
+ arg_start
;
1967 if (optind
!= noaction
)
1969 if (s
->verbose
== 2)
1970 return OPT_PRINT_DIRS
;
1976 LIBTCCAPI
void tcc_set_options(TCCState
*s
, const char *r
)
1980 args_parser_make_argv(r
, &argc
, &argv
);
1981 tcc_parse_args(s
, &argc
, &argv
, 0);
1982 dynarray_reset(&argv
, &argc
);
1985 PUB_FUNC
void tcc_print_stats(TCCState
*s
, unsigned total_time
)
1989 if (total_bytes
< 1)
1991 fprintf(stderr
, "* %d idents, %d lines, %d bytes\n"
1992 "* %0.3f s, %u lines/s, %0.1f MB/s\n",
1993 tok_ident
- TOK_IDENT
, total_lines
, total_bytes
,
1994 (double)total_time
/1000,
1995 (unsigned)total_lines
*1000/total_time
,
1996 (double)total_bytes
/1000/total_time
);
1998 fprintf(stderr
, "* %d bytes memory used\n", mem_max_size
);