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
;
35 /********************************************************/
42 #ifdef TCC_TARGET_I386
48 #ifdef TCC_TARGET_ARM64
49 #include "arm64-gen.c"
54 #ifdef TCC_TARGET_X86_64
55 #include "x86_64-gen.c"
59 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
63 #ifdef TCC_TARGET_COFF
69 #endif /* ONE_SOURCE */
71 /********************************************************/
72 #ifndef CONFIG_TCC_ASM
73 ST_FUNC
void asm_instr(void)
75 tcc_error("inline asm() not supported");
77 ST_FUNC
void asm_global_instr(void)
79 tcc_error("inline asm() not supported");
83 /********************************************************/
85 ST_FUNC
char *normalize_slashes(char *path
)
88 for (p
= path
; *p
; ++p
)
94 static HMODULE tcc_module
;
96 /* on win32, we suppose the lib and includes are at the location of 'tcc.exe' */
97 static void tcc_set_lib_path_w32(TCCState
*s
)
100 GetModuleFileNameA(tcc_module
, path
, sizeof path
);
101 p
= tcc_basename(normalize_slashes(strlwr(path
)));
102 if (p
- 5 > path
&& 0 == strncmp(p
- 5, "/bin/", 5))
107 tcc_set_lib_path(s
, path
);
111 static void tcc_add_systemdir(TCCState
*s
)
114 GetSystemDirectory(buf
, sizeof buf
);
115 tcc_add_library_path(s
, normalize_slashes(buf
));
120 BOOL WINAPI
DllMain (HINSTANCE hDll
, DWORD dwReason
, LPVOID lpReserved
)
122 if (DLL_PROCESS_ATTACH
== dwReason
)
129 /********************************************************/
130 /* copy a string and truncate it. */
131 PUB_FUNC
char *pstrcpy(char *buf
, int buf_size
, const char *s
)
138 q_end
= buf
+ buf_size
- 1;
150 /* strcat and truncate. */
151 PUB_FUNC
char *pstrcat(char *buf
, int buf_size
, const char *s
)
156 pstrcpy(buf
+ len
, buf_size
- len
, s
);
160 PUB_FUNC
char *pstrncpy(char *out
, const char *in
, size_t num
)
162 memcpy(out
, in
, num
);
167 /* extract the basename of a file */
168 PUB_FUNC
char *tcc_basename(const char *name
)
170 char *p
= strchr(name
, 0);
171 while (p
> name
&& !IS_DIRSEP(p
[-1]))
176 /* extract extension part of a file
178 * (if no extension, return pointer to end-of-string)
180 PUB_FUNC
char *tcc_fileextension (const char *name
)
182 char *b
= tcc_basename(name
);
183 char *e
= strrchr(b
, '.');
184 return e
? e
: strchr(b
, 0);
187 /********************************************************/
188 /* memory management */
196 PUB_FUNC
void tcc_free(void *ptr
)
201 PUB_FUNC
void *tcc_malloc(unsigned long size
)
206 tcc_error("memory full (malloc)");
210 PUB_FUNC
void *tcc_mallocz(unsigned long size
)
213 ptr
= tcc_malloc(size
);
214 memset(ptr
, 0, size
);
218 PUB_FUNC
void *tcc_realloc(void *ptr
, unsigned long size
)
221 ptr1
= realloc(ptr
, size
);
223 tcc_error("memory full (realloc)");
227 PUB_FUNC
char *tcc_strdup(const char *str
)
230 ptr
= tcc_malloc(strlen(str
) + 1);
235 PUB_FUNC
void tcc_memstats(int bench
)
241 #define MEM_DEBUG_MAGIC1 0xFEEDDEB1
242 #define MEM_DEBUG_MAGIC2 0xFEEDDEB2
243 #define MEM_DEBUG_FILE_LEN 15
245 struct mem_debug_header
{
248 struct mem_debug_header
*prev
;
249 struct mem_debug_header
*next
;
251 char file_name
[MEM_DEBUG_FILE_LEN
+ 1];
255 typedef struct mem_debug_header mem_debug_header_t
;
257 static mem_debug_header_t
*mem_debug_chain
;
258 static size_t mem_cur_size
;
259 static size_t mem_max_size
;
261 PUB_FUNC
void *tcc_malloc_debug(unsigned long size
, const char *file
, int line
)
266 mem_debug_header_t
*header
;
268 ptr
= malloc(sizeof(mem_debug_header_t
) + size
);
270 tcc_error("memory full (malloc)");
272 mem_cur_size
+= size
;
273 if (mem_cur_size
> mem_max_size
)
274 mem_max_size
= mem_cur_size
;
276 header
= (mem_debug_header_t
*)ptr
;
278 header
->magic1
= MEM_DEBUG_MAGIC1
;
279 header
->magic2
= MEM_DEBUG_MAGIC2
;
281 header
->line_num
= line
;
283 ofs
= strlen(file
) - MEM_DEBUG_FILE_LEN
;
284 strncpy(header
->file_name
, file
+ (ofs
> 0 ? ofs
: 0), MEM_DEBUG_FILE_LEN
);
285 header
->file_name
[MEM_DEBUG_FILE_LEN
] = 0;
287 header
->next
= mem_debug_chain
;
291 header
->next
->prev
= header
;
293 mem_debug_chain
= header
;
295 ptr
= (char *)ptr
+ sizeof(mem_debug_header_t
);
299 PUB_FUNC
void tcc_free_debug(void *ptr
)
301 mem_debug_header_t
*header
;
306 ptr
= (char *)ptr
- sizeof(mem_debug_header_t
);
307 header
= (mem_debug_header_t
*)ptr
;
308 if (header
->magic1
!= MEM_DEBUG_MAGIC1
||
309 header
->magic2
!= MEM_DEBUG_MAGIC2
||
310 header
->size
== (size_t)-1 )
312 tcc_error("tcc_free check failed");
315 mem_cur_size
-= header
->size
;
316 header
->size
= (size_t)-1;
319 header
->next
->prev
= header
->prev
;
322 header
->prev
->next
= header
->next
;
324 if (header
== mem_debug_chain
)
325 mem_debug_chain
= header
->next
;
331 PUB_FUNC
void *tcc_mallocz_debug(unsigned long size
, const char *file
, int line
)
334 ptr
= tcc_malloc_debug(size
,file
,line
);
335 memset(ptr
, 0, size
);
339 PUB_FUNC
void *tcc_realloc_debug(void *ptr
, unsigned long size
, const char *file
, int line
)
341 mem_debug_header_t
*header
;
342 int mem_debug_chain_update
= 0;
345 ptr
= tcc_malloc_debug(size
, file
, line
);
349 ptr
= (char *)ptr
- sizeof(mem_debug_header_t
);
350 header
= (mem_debug_header_t
*)ptr
;
351 if (header
->magic1
!= MEM_DEBUG_MAGIC1
||
352 header
->magic2
!= MEM_DEBUG_MAGIC2
||
353 header
->size
== (size_t)-1 )
356 tcc_error("tcc_realloc check failed");
359 mem_debug_chain_update
= (header
== mem_debug_chain
);
361 mem_cur_size
-= header
->size
;
362 ptr
= realloc(ptr
, sizeof(mem_debug_header_t
) + size
);
364 tcc_error("memory full (realloc)");
366 header
= (mem_debug_header_t
*)ptr
;
367 if (header
->magic1
!= MEM_DEBUG_MAGIC1
||
368 header
->magic2
!= MEM_DEBUG_MAGIC2
)
373 mem_cur_size
+= size
;
374 if (mem_cur_size
> mem_max_size
)
375 mem_max_size
= mem_cur_size
;
379 header
->next
->prev
= header
;
382 header
->prev
->next
= header
;
384 if (mem_debug_chain_update
)
385 mem_debug_chain
= header
;
387 ptr
= (char *)ptr
+ sizeof(mem_debug_header_t
);
391 PUB_FUNC
char *tcc_strdup_debug(const char *str
, const char *file
, int line
)
394 ptr
= tcc_malloc_debug(strlen(str
) + 1, file
, line
);
399 PUB_FUNC
void tcc_memstats(int bench
)
402 mem_debug_header_t
*header
= mem_debug_chain
;
404 fprintf(stderr
, "MEM_DEBUG: mem_leak= %d bytes, mem_max_size= %d bytes\n",
405 mem_cur_size
, mem_max_size
);
408 fprintf(stderr
, "%s:%u: error: %u bytes leaked\n",
409 header
->file_name
, header
->line_num
, header
->size
);
410 header
= header
->next
;
414 fprintf(stderr
, "mem_max_size= %d bytes\n", mem_max_size
);
417 #undef MEM_DEBUG_MAGIC1
418 #undef MEM_DEBUG_MAGIC2
419 #undef MEM_DEBUG_FILE_LEN
423 #define free(p) use_tcc_free(p)
424 #define malloc(s) use_tcc_malloc(s)
425 #define realloc(p, s) use_tcc_realloc(p, s)
427 /********************************************************/
430 ST_FUNC
void dynarray_add(void ***ptab
, int *nb_ptr
, void *data
)
437 /* every power of two we double array size */
438 if ((nb
& (nb
- 1)) == 0) {
443 pp
= tcc_realloc(pp
, nb_alloc
* sizeof(void *));
450 ST_FUNC
void dynarray_reset(void *pp
, int *n
)
453 for (p
= *(void***)pp
; *n
; ++p
, --*n
)
456 tcc_free(*(void**)pp
);
460 static void tcc_split_path(TCCState
*s
, void ***p_ary
, int *p_nb_ary
, const char *in
)
468 for (p
= in
; c
= *p
, c
!= '\0' && c
!= PATHSEP
; ++p
) {
469 if (c
== '{' && p
[1] && p
[2] == '}') {
472 cstr_cat(&str
, s
->tcc_lib_path
, -1);
478 cstr_ccat(&str
, '\0');
479 dynarray_add(p_ary
, p_nb_ary
, tcc_strdup(str
.data
));
486 /********************************************************/
488 static void strcat_vprintf(char *buf
, int buf_size
, const char *fmt
, va_list ap
)
492 vsnprintf(buf
+ len
, buf_size
- len
, fmt
, ap
);
495 static void strcat_printf(char *buf
, int buf_size
, const char *fmt
, ...)
499 strcat_vprintf(buf
, buf_size
, fmt
, ap
);
503 static void error1(TCCState
*s1
, int is_warning
, const char *fmt
, va_list ap
)
506 BufferedFile
**pf
, *f
;
509 /* use upper file if inline ":asm:" or token ":paste:" */
510 for (f
= file
; f
&& f
->filename
[0] == ':'; f
= f
->prev
)
513 for(pf
= s1
->include_stack
; pf
< s1
->include_stack_ptr
; pf
++)
514 strcat_printf(buf
, sizeof(buf
), "In file included from %s:%d:\n",
515 (*pf
)->filename
, (*pf
)->line_num
);
516 if (f
->line_num
> 0) {
517 strcat_printf(buf
, sizeof(buf
), "%s:%d: ",
518 f
->filename
, f
->line_num
- !!(tok_flags
& TOK_FLAG_BOL
));
520 strcat_printf(buf
, sizeof(buf
), "%s: ",
524 strcat_printf(buf
, sizeof(buf
), "tcc: ");
527 strcat_printf(buf
, sizeof(buf
), "warning: ");
529 strcat_printf(buf
, sizeof(buf
), "error: ");
530 strcat_vprintf(buf
, sizeof(buf
), fmt
, ap
);
532 if (!s1
->error_func
) {
533 /* default case: stderr */
534 if (s1
->ppfp
) /* print a newline during tcc -E */
535 fprintf(s1
->ppfp
, "\n"), fflush(s1
->ppfp
);
536 fprintf(stderr
, "%s\n", buf
);
537 fflush(stderr
); /* print error/warning now (win32) */
539 s1
->error_func(s1
->error_opaque
, buf
);
541 if (!is_warning
|| s1
->warn_error
)
545 LIBTCCAPI
void tcc_set_error_func(TCCState
*s
, void *error_opaque
,
546 void (*error_func
)(void *opaque
, const char *msg
))
548 s
->error_opaque
= error_opaque
;
549 s
->error_func
= error_func
;
552 /* error without aborting current compilation */
553 PUB_FUNC
void tcc_error_noabort(const char *fmt
, ...)
555 TCCState
*s1
= tcc_state
;
559 error1(s1
, 0, fmt
, ap
);
563 PUB_FUNC
void tcc_error(const char *fmt
, ...)
565 TCCState
*s1
= tcc_state
;
569 error1(s1
, 0, fmt
, ap
);
571 /* better than nothing: in some cases, we accept to handle errors */
572 if (s1
->error_set_jmp_enabled
) {
573 longjmp(s1
->error_jmp_buf
, 1);
575 /* XXX: eliminate this someday */
580 PUB_FUNC
void tcc_warning(const char *fmt
, ...)
582 TCCState
*s1
= tcc_state
;
589 error1(s1
, 1, fmt
, ap
);
593 /********************************************************/
596 ST_FUNC
void tcc_open_bf(TCCState
*s1
, const char *filename
, int initlen
)
599 int buflen
= initlen
? initlen
: IO_BUF_SIZE
;
601 bf
= tcc_mallocz(sizeof(BufferedFile
) + buflen
);
602 bf
->buf_ptr
= bf
->buffer
;
603 bf
->buf_end
= bf
->buffer
+ initlen
;
604 bf
->buf_end
[0] = CH_EOB
; /* put eob symbol */
605 pstrcpy(bf
->filename
, sizeof(bf
->filename
), filename
);
607 normalize_slashes(bf
->filename
);
610 bf
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
616 ST_FUNC
void tcc_close(void)
618 BufferedFile
*bf
= file
;
621 total_lines
+= bf
->line_num
;
627 ST_FUNC
int tcc_open(TCCState
*s1
, const char *filename
)
630 if (strcmp(filename
, "-") == 0)
631 fd
= 0, filename
= "<stdin>";
633 fd
= open(filename
, O_RDONLY
| O_BINARY
);
634 if ((s1
->verbose
== 2 && fd
>= 0) || s1
->verbose
== 3)
635 printf("%s %*s%s\n", fd
< 0 ? "nf":"->",
636 (int)(s1
->include_stack_ptr
- s1
->include_stack
), "", filename
);
640 tcc_open_bf(s1
, filename
, 0);
645 /* compile the C file opened in 'file'. Return non zero if errors. */
646 static int tcc_compile(TCCState
*s1
)
650 preprocess_start(s1
);
651 define_start
= define_stack
;
653 if (setjmp(s1
->error_jmp_buf
) == 0) {
655 s1
->error_set_jmp_enabled
= 1;
659 printf("%s: **** new file\n", file
->filename
);
661 ch
= file
->buf_ptr
[0];
662 tok_flags
= TOK_FLAG_BOL
| TOK_FLAG_BOF
;
663 parse_flags
= PARSE_FLAG_PREPROCESS
| PARSE_FLAG_TOK_NUM
| PARSE_FLAG_TOK_STR
;
667 expect("declaration");
670 s1
->error_set_jmp_enabled
= 0;
672 free_inline_functions(s1
);
673 /* reset define stack, but keep -D and built-ins */
674 free_defines(define_start
);
675 sym_pop(&global_stack
, NULL
);
676 sym_pop(&local_stack
, NULL
);
677 return s1
->nb_errors
!= 0 ? -1 : 0;
680 LIBTCCAPI
int tcc_compile_string(TCCState
*s
, const char *str
)
685 tcc_open_bf(s
, "<string>", len
);
686 memcpy(file
->buffer
, str
, len
);
687 ret
= tcc_compile(s
);
692 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
693 LIBTCCAPI
void tcc_define_symbol(TCCState
*s1
, const char *sym
, const char *value
)
700 len2
= strlen(value
);
702 /* init file structure */
703 tcc_open_bf(s1
, "<define>", len1
+ len2
+ 1);
704 memcpy(file
->buffer
, sym
, len1
);
705 file
->buffer
[len1
] = ' ';
706 memcpy(file
->buffer
+ len1
+ 1, value
, len2
);
708 /* parse with define parser */
709 ch
= file
->buf_ptr
[0];
716 /* undefine a preprocessor symbol */
717 LIBTCCAPI
void tcc_undefine_symbol(TCCState
*s1
, const char *sym
)
721 ts
= tok_alloc(sym
, strlen(sym
));
722 s
= define_find(ts
->tok
);
723 /* undefine symbol by putting an invalid name */
728 /* cleanup all static data used during compilation */
729 static void tcc_cleanup(void)
731 if (NULL
== tcc_state
)
733 tccpp_delete(tcc_state
);
736 dynarray_reset(&sym_pools
, &nb_sym_pools
);
737 /* reset symbol stack */
738 sym_free_first
= NULL
;
741 LIBTCCAPI TCCState
*tcc_new(void)
747 s
= tcc_mallocz(sizeof(TCCState
));
752 s
->alacarte_link
= 1;
754 s
->warn_implicit_function_declaration
= 1;
756 #ifdef CHAR_IS_UNSIGNED
757 s
->char_is_unsigned
= 1;
759 #ifdef TCC_TARGET_I386
763 s
->runtime_main
= "main";
765 /* enable this if you want symbols with leading underscore on windows: */
766 #if 0 /* def TCC_TARGET_PE */
767 s
->leading_underscore
= 1;
770 tcc_set_lib_path_w32(s
);
772 tcc_set_lib_path(s
, CONFIG_TCCDIR
);
777 /* we add dummy defines for some special macros to speed up tests
778 and to have working defined() */
779 define_push(TOK___LINE__
, MACRO_OBJ
, NULL
, NULL
);
780 define_push(TOK___FILE__
, MACRO_OBJ
, NULL
, NULL
);
781 define_push(TOK___DATE__
, MACRO_OBJ
, NULL
, NULL
);
782 define_push(TOK___TIME__
, MACRO_OBJ
, NULL
, NULL
);
784 /* define __TINYC__ 92X */
785 char buffer
[32]; int a
,b
,c
;
786 sscanf(TCC_VERSION
, "%d.%d.%d", &a
, &b
, &c
);
787 sprintf(buffer
, "%d", a
*10000 + b
*100 + c
);
788 tcc_define_symbol(s
, "__TINYC__", buffer
);
791 /* standard defines */
792 tcc_define_symbol(s
, "__STDC__", NULL
);
793 tcc_define_symbol(s
, "__STDC_VERSION__", "199901L");
794 tcc_define_symbol(s
, "__STDC_HOSTED__", NULL
);
797 #if defined(TCC_TARGET_I386)
798 tcc_define_symbol(s
, "__i386__", NULL
);
799 tcc_define_symbol(s
, "__i386", NULL
);
800 tcc_define_symbol(s
, "i386", NULL
);
801 #elif defined(TCC_TARGET_X86_64)
802 tcc_define_symbol(s
, "__x86_64__", NULL
);
803 #elif defined(TCC_TARGET_ARM)
804 tcc_define_symbol(s
, "__ARM_ARCH_4__", NULL
);
805 tcc_define_symbol(s
, "__arm_elf__", NULL
);
806 tcc_define_symbol(s
, "__arm_elf", NULL
);
807 tcc_define_symbol(s
, "arm_elf", NULL
);
808 tcc_define_symbol(s
, "__arm__", NULL
);
809 tcc_define_symbol(s
, "__arm", NULL
);
810 tcc_define_symbol(s
, "arm", NULL
);
811 tcc_define_symbol(s
, "__APCS_32__", NULL
);
812 tcc_define_symbol(s
, "__ARMEL__", NULL
);
813 #if defined(TCC_ARM_EABI)
814 tcc_define_symbol(s
, "__ARM_EABI__", NULL
);
816 #if defined(TCC_ARM_HARDFLOAT)
817 s
->float_abi
= ARM_HARD_FLOAT
;
818 tcc_define_symbol(s
, "__ARM_PCS_VFP", NULL
);
820 s
->float_abi
= ARM_SOFTFP_FLOAT
;
822 #elif defined(TCC_TARGET_ARM64)
823 tcc_define_symbol(s
, "__aarch64__", NULL
);
827 tcc_define_symbol(s
, "_WIN32", NULL
);
828 # ifdef TCC_TARGET_X86_64
829 tcc_define_symbol(s
, "_WIN64", NULL
);
832 tcc_define_symbol(s
, "__unix__", NULL
);
833 tcc_define_symbol(s
, "__unix", NULL
);
834 tcc_define_symbol(s
, "unix", NULL
);
835 # if defined(__linux__)
836 tcc_define_symbol(s
, "__linux__", NULL
);
837 tcc_define_symbol(s
, "__linux", NULL
);
839 # if defined(__FreeBSD__)
840 tcc_define_symbol(s
, "__FreeBSD__", "__FreeBSD__");
841 /* No 'Thread Storage Local' on FreeBSD with tcc */
842 tcc_define_symbol(s
, "__NO_TLS", NULL
);
844 # if defined(__FreeBSD_kernel__)
845 tcc_define_symbol(s
, "__FreeBSD_kernel__", NULL
);
848 # if defined(__NetBSD__)
849 tcc_define_symbol(s
, "__NetBSD__", "__NetBSD__");
851 # if defined(__OpenBSD__)
852 tcc_define_symbol(s
, "__OpenBSD__", "__OpenBSD__");
855 /* TinyCC & gcc defines */
856 #if defined(TCC_TARGET_PE) && defined(TCC_TARGET_X86_64)
858 tcc_define_symbol(s
, "__SIZE_TYPE__", "unsigned long long");
859 tcc_define_symbol(s
, "__PTRDIFF_TYPE__", "long long");
860 tcc_define_symbol(s
, "__LLP64__", NULL
);
861 #elif defined(TCC_TARGET_X86_64) || defined(TCC_TARGET_ARM64)
862 /* Other 64bit systems. */
863 tcc_define_symbol(s
, "__SIZE_TYPE__", "unsigned long");
864 tcc_define_symbol(s
, "__PTRDIFF_TYPE__", "long");
865 tcc_define_symbol(s
, "__LP64__", NULL
);
867 /* Other 32bit systems. */
868 tcc_define_symbol(s
, "__SIZE_TYPE__", "unsigned long");
869 tcc_define_symbol(s
, "__PTRDIFF_TYPE__", "long");
870 tcc_define_symbol(s
, "__ILP32__", NULL
);
874 tcc_define_symbol(s
, "__WCHAR_TYPE__", "unsigned short");
875 tcc_define_symbol(s
, "__WINT_TYPE__", "unsigned short");
877 tcc_define_symbol(s
, "__WCHAR_TYPE__", "int");
878 /* wint_t is unsigned int by default, but (signed) int on BSDs
879 and unsigned short on windows. Other OSes might have still
880 other conventions, sigh. */
881 # if defined(__FreeBSD__) || defined (__FreeBSD_kernel__) \
882 || defined(__NetBSD__) || defined(__OpenBSD__)
883 tcc_define_symbol(s
, "__WINT_TYPE__", "int");
885 /* define __GNUC__ to have some useful stuff from sys/cdefs.h
886 that are unconditionally used in FreeBSDs other system headers :/ */
887 tcc_define_symbol(s
, "__GNUC__", "2");
888 tcc_define_symbol(s
, "__GNUC_MINOR__", "7");
889 tcc_define_symbol(s
, "__builtin_alloca", "alloca");
892 tcc_define_symbol(s
, "__WINT_TYPE__", "unsigned int");
894 tcc_define_symbol(s
, "__REDIRECT(name, proto, alias)",
895 "name proto __asm__ (#alias)");
896 tcc_define_symbol(s
, "__REDIRECT_NTH(name, proto, alias)",
897 "name proto __asm__ (#alias) __THROW");
899 #endif /* ndef TCC_TARGET_PE */
904 LIBTCCAPI
void tcc_delete(TCCState
*s1
)
906 int bench
= s1
->do_bench
;
913 /* free library paths */
914 dynarray_reset(&s1
->library_paths
, &s1
->nb_library_paths
);
915 dynarray_reset(&s1
->crt_paths
, &s1
->nb_crt_paths
);
917 /* free include paths */
918 dynarray_reset(&s1
->cached_includes
, &s1
->nb_cached_includes
);
919 dynarray_reset(&s1
->include_paths
, &s1
->nb_include_paths
);
920 dynarray_reset(&s1
->sysinclude_paths
, &s1
->nb_sysinclude_paths
);
922 tcc_free(s1
->tcc_lib_path
);
923 tcc_free(s1
->soname
);
925 tcc_free(s1
->init_symbol
);
926 tcc_free(s1
->fini_symbol
);
927 tcc_free(s1
->outfile
);
928 tcc_free(s1
->deps_outfile
);
929 dynarray_reset(&s1
->files
, &s1
->nb_files
);
930 dynarray_reset(&s1
->target_deps
, &s1
->nb_target_deps
);
931 dynarray_reset(&s1
->pragma_libs
, &s1
->nb_pragma_libs
);
934 /* free runtime memory */
938 tcc_free(s1
->sym_attrs
);
943 LIBTCCAPI
int tcc_set_output_type(TCCState
*s
, int output_type
)
945 s
->output_type
= output_type
;
947 /* always elf for objects */
948 if (output_type
== TCC_OUTPUT_OBJ
)
949 s
->output_format
= TCC_OUTPUT_FORMAT_ELF
;
951 if (s
->char_is_unsigned
)
952 tcc_define_symbol(s
, "__CHAR_UNSIGNED__", NULL
);
955 /* default include paths */
956 /* -isystem paths have already been handled */
957 tcc_add_sysinclude_path(s
, CONFIG_TCC_SYSINCLUDEPATHS
);
960 #ifdef CONFIG_TCC_BCHECK
961 if (s
->do_bounds_check
) {
962 /* if bound checking, then add corresponding sections */
963 tccelf_bounds_new(s
);
965 tcc_define_symbol(s
, "__BOUNDS_CHECKING_ON", NULL
);
969 /* add debug sections */
973 tcc_add_library_path(s
, CONFIG_TCC_LIBPATHS
);
977 if (!s
->nostdlib
&& output_type
!= TCC_OUTPUT_OBJ
)
978 tcc_add_systemdir(s
);
981 /* paths for crt objects */
982 tcc_split_path(s
, (void ***)&s
->crt_paths
, &s
->nb_crt_paths
, CONFIG_TCC_CRTPREFIX
);
983 /* add libc crt1/crti objects */
984 if ((output_type
== TCC_OUTPUT_EXE
|| output_type
== TCC_OUTPUT_DLL
) &&
986 if (output_type
!= TCC_OUTPUT_DLL
)
987 tcc_add_crt(s
, "crt1.o");
988 tcc_add_crt(s
, "crti.o");
994 LIBTCCAPI
int tcc_add_include_path(TCCState
*s
, const char *pathname
)
996 tcc_split_path(s
, (void ***)&s
->include_paths
, &s
->nb_include_paths
, pathname
);
1000 LIBTCCAPI
int tcc_add_sysinclude_path(TCCState
*s
, const char *pathname
)
1002 tcc_split_path(s
, (void ***)&s
->sysinclude_paths
, &s
->nb_sysinclude_paths
, pathname
);
1006 ST_FUNC
int tcc_add_file_internal(TCCState
*s1
, const char *filename
, int flags
)
1010 filetype
= flags
& 0x0F;
1011 if (filetype
== 0) {
1012 /* use a file extension to detect a filetype */
1013 const char *ext
= tcc_fileextension(filename
);
1016 if (!strcmp(ext
, "S"))
1017 filetype
= AFF_TYPE_ASMPP
;
1018 else if (!strcmp(ext
, "s"))
1019 filetype
= AFF_TYPE_ASM
;
1020 else if (!PATHCMP(ext
, "c") || !PATHCMP(ext
, "i"))
1021 filetype
= AFF_TYPE_C
;
1023 filetype
= AFF_TYPE_BIN
;
1025 filetype
= AFF_TYPE_C
;
1030 ret
= tcc_open(s1
, filename
);
1032 if (flags
& AFF_PRINT_ERROR
)
1033 tcc_error_noabort("file '%s' not found", filename
);
1037 /* update target deps */
1038 dynarray_add((void ***)&s1
->target_deps
, &s1
->nb_target_deps
,
1039 tcc_strdup(filename
));
1042 /* if .S file, define __ASSEMBLER__ like gcc does */
1043 if (filetype
== AFF_TYPE_ASM
|| filetype
== AFF_TYPE_ASMPP
) {
1044 tcc_define_symbol(s1
, "__ASSEMBLER__", NULL
);
1045 parse_flags
= PARSE_FLAG_ASM_FILE
;
1048 if (flags
& AFF_PREPROCESS
) {
1049 ret
= tcc_preprocess(s1
);
1050 } else if (filetype
== AFF_TYPE_C
) {
1051 ret
= tcc_compile(s1
);
1052 #ifdef CONFIG_TCC_ASM
1053 } else if (filetype
== AFF_TYPE_ASMPP
) {
1054 /* non preprocessed assembler */
1055 ret
= tcc_assemble(s1
, 1);
1056 } else if (filetype
== AFF_TYPE_ASM
) {
1057 /* preprocessed assembler */
1058 ret
= tcc_assemble(s1
, 0);
1065 obj_type
= tcc_object_type(fd
, &ehdr
);
1066 lseek(fd
, 0, SEEK_SET
);
1068 /* do not display line number if error */
1072 case AFF_BINTYPE_REL
:
1073 ret
= tcc_load_object_file(s1
, fd
, 0);
1075 #ifndef TCC_TARGET_PE
1076 case AFF_BINTYPE_DYN
:
1077 if (s1
->output_type
== TCC_OUTPUT_MEMORY
) {
1079 #ifdef TCC_IS_NATIVE
1080 if (NULL
== dlopen(filename
, RTLD_GLOBAL
| RTLD_LAZY
))
1084 ret
= tcc_load_dll(s1
, fd
, filename
,
1085 (flags
& AFF_REFERENCED_DLL
) != 0);
1089 case AFF_BINTYPE_AR
:
1090 ret
= tcc_load_archive(s1
, fd
);
1092 #ifdef TCC_TARGET_COFF
1093 case AFF_BINTYPE_C67
:
1094 ret
= tcc_load_coff(s1
, fd
);
1098 #ifdef TCC_TARGET_PE
1099 ret
= pe_load_file(s1
, filename
, fd
);
1101 /* as GNU ld, consider it is an ld script if not recognized */
1102 ret
= tcc_load_ldscript(s1
);
1105 tcc_error_noabort("unrecognized file type");
1113 LIBTCCAPI
int tcc_add_file(TCCState
*s
, const char *filename
)
1115 if (s
->output_type
== TCC_OUTPUT_PREPROCESS
)
1116 return tcc_add_file_internal(s
, filename
, AFF_PRINT_ERROR
| AFF_PREPROCESS
| s
->filetype
);
1118 return tcc_add_file_internal(s
, filename
, AFF_PRINT_ERROR
| s
->filetype
);
1121 LIBTCCAPI
int tcc_add_library_path(TCCState
*s
, const char *pathname
)
1123 tcc_split_path(s
, (void ***)&s
->library_paths
, &s
->nb_library_paths
, pathname
);
1127 static int tcc_add_library_internal(TCCState
*s
, const char *fmt
,
1128 const char *filename
, int flags
, char **paths
, int nb_paths
)
1133 for(i
= 0; i
< nb_paths
; i
++) {
1134 snprintf(buf
, sizeof(buf
), fmt
, paths
[i
], filename
);
1135 if (tcc_add_file_internal(s
, buf
, flags
| AFF_TYPE_BIN
) == 0)
1141 #ifndef TCC_TARGET_PE
1142 /* find and load a dll. Return non zero if not found */
1143 /* XXX: add '-rpath' option support ? */
1144 ST_FUNC
int tcc_add_dll(TCCState
*s
, const char *filename
, int flags
)
1146 return tcc_add_library_internal(s
, "%s/%s", filename
, flags
,
1147 s
->library_paths
, s
->nb_library_paths
);
1151 ST_FUNC
int tcc_add_crt(TCCState
*s
, const char *filename
)
1153 if (-1 == tcc_add_library_internal(s
, "%s/%s",
1154 filename
, 0, s
->crt_paths
, s
->nb_crt_paths
))
1155 tcc_error_noabort("file '%s' not found", filename
);
1159 /* the library name is the same as the argument of the '-l' option */
1160 LIBTCCAPI
int tcc_add_library(TCCState
*s
, const char *libraryname
)
1162 #ifdef TCC_TARGET_PE
1163 const char *libs
[] = { "%s/%s.def", "%s/lib%s.def", "%s/%s.dll", "%s/lib%s.dll", "%s/lib%s.a", NULL
};
1164 const char **pp
= s
->static_link
? libs
+ 4 : libs
;
1166 const char *libs
[] = { "%s/lib%s.so", "%s/lib%s.a", NULL
};
1167 const char **pp
= s
->static_link
? libs
+ 1 : libs
;
1170 if (0 == tcc_add_library_internal(s
, *pp
,
1171 libraryname
, 0, s
->library_paths
, s
->nb_library_paths
))
1178 PUB_FUNC
int tcc_add_library_err(TCCState
*s
, const char *libname
)
1180 int ret
= tcc_add_library(s
, libname
);
1182 tcc_error_noabort("library 'lib%s' not found", libname
);
1186 /* habdle #pragma comment(lib,) */
1187 ST_FUNC
void tcc_add_pragma_libs(TCCState
*s1
)
1190 for (i
= 0; i
< s1
->nb_pragma_libs
; i
++)
1191 tcc_add_library_err(s1
, s1
->pragma_libs
[i
]);
1194 LIBTCCAPI
int tcc_add_symbol(TCCState
*s
, const char *name
, const void *val
)
1196 #ifdef TCC_TARGET_PE
1197 /* On x86_64 'val' might not be reachable with a 32bit offset.
1198 So it is handled here as if it were in a DLL. */
1199 pe_putimport(s
, 0, name
, (uintptr_t)val
);
1201 add_elf_sym(symtab_section
, (uintptr_t)val
, 0,
1202 ELFW(ST_INFO
)(STB_GLOBAL
, STT_NOTYPE
), 0,
1208 LIBTCCAPI
void tcc_set_lib_path(TCCState
*s
, const char *path
)
1210 tcc_free(s
->tcc_lib_path
);
1211 s
->tcc_lib_path
= tcc_strdup(path
);
1214 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
1215 #define FD_INVERT 0x0002 /* invert value before storing */
1217 typedef struct FlagDef
{
1223 static const FlagDef warning_defs
[] = {
1224 { offsetof(TCCState
, warn_unsupported
), 0, "unsupported" },
1225 { offsetof(TCCState
, warn_write_strings
), 0, "write-strings" },
1226 { offsetof(TCCState
, warn_error
), 0, "error" },
1227 { offsetof(TCCState
, warn_implicit_function_declaration
), WD_ALL
,
1228 "implicit-function-declaration" },
1231 static int no_flag(const char **pp
)
1233 const char *p
= *pp
;
1234 if (*p
!= 'n' || *++p
!= 'o' || *++p
!= '-')
1240 ST_FUNC
int set_flag(TCCState
*s
, const FlagDef
*flags
, int nb_flags
,
1241 const char *name
, int value
)
1251 for(i
= 0, p
= flags
; i
< nb_flags
; i
++, p
++) {
1252 if (!strcmp(r
, p
->name
))
1257 if (p
->flags
& FD_INVERT
)
1259 *(int *)((uint8_t *)s
+ p
->offset
) = value
;
1263 /* set/reset a warning */
1264 static int tcc_set_warning(TCCState
*s
, const char *warning_name
, int value
)
1269 if (!strcmp(warning_name
, "all")) {
1270 for(i
= 0, p
= warning_defs
; i
< countof(warning_defs
); i
++, p
++) {
1271 if (p
->flags
& WD_ALL
)
1272 *(int *)((uint8_t *)s
+ p
->offset
) = 1;
1276 return set_flag(s
, warning_defs
, countof(warning_defs
),
1277 warning_name
, value
);
1281 static const FlagDef flag_defs
[] = {
1282 { offsetof(TCCState
, char_is_unsigned
), 0, "unsigned-char" },
1283 { offsetof(TCCState
, char_is_unsigned
), FD_INVERT
, "signed-char" },
1284 { offsetof(TCCState
, nocommon
), FD_INVERT
, "common" },
1285 { offsetof(TCCState
, leading_underscore
), 0, "leading-underscore" },
1286 { offsetof(TCCState
, ms_extensions
), 0, "ms-extensions" },
1287 { offsetof(TCCState
, old_struct_init_code
), 0, "old-struct-init-code" },
1288 { offsetof(TCCState
, dollars_in_identifiers
), 0, "dollars-in-identifiers" },
1291 /* set/reset a flag */
1292 static int tcc_set_flag(TCCState
*s
, const char *flag_name
, int value
)
1294 return set_flag(s
, flag_defs
, countof(flag_defs
),
1299 static int strstart(const char *val
, const char **str
)
1314 /* Like strstart, but automatically takes into account that ld options can
1316 * - start with double or single dash (e.g. '--soname' or '-soname')
1317 * - arguments can be given as separate or after '=' (e.g. '-Wl,-soname,x.so'
1318 * or '-Wl,-soname=x.so')
1320 * you provide `val` always in 'option[=]' form (no leading -)
1322 static int link_option(const char *str
, const char *val
, const char **ptr
)
1327 /* there should be 1 or 2 dashes */
1333 /* then str & val should match (potentialy up to '=') */
1344 while (*q
!= '\0' && *q
!= '=') {
1351 /* '=' near eos means ',' or '=' is ok */
1355 if (*p
!= ',' && *p
!= '=')
1363 static const char *skip_linker_arg(const char **str
)
1365 const char *s1
= *str
;
1366 const char *s2
= strchr(s1
, ',');
1367 *str
= s2
? s2
++ : (s2
= s1
+ strlen(s1
));
1371 static char *copy_linker_arg(const char *p
)
1374 skip_linker_arg(&q
);
1375 return pstrncpy(tcc_malloc(q
- p
+ 1), p
, q
- p
);
1378 /* set linker options */
1379 static int tcc_set_linker(TCCState
*s
, const char *option
)
1383 const char *p
= NULL
;
1388 if (link_option(option
, "Bsymbolic", &p
)) {
1390 } else if (link_option(option
, "nostdlib", &p
)) {
1392 } else if (link_option(option
, "fini=", &p
)) {
1393 s
->fini_symbol
= copy_linker_arg(p
);
1395 } else if (link_option(option
, "image-base=", &p
)
1396 || link_option(option
, "Ttext=", &p
)) {
1397 s
->text_addr
= strtoull(p
, &end
, 16);
1398 s
->has_text_addr
= 1;
1399 } else if (link_option(option
, "init=", &p
)) {
1400 s
->init_symbol
= copy_linker_arg(p
);
1402 } else if (link_option(option
, "oformat=", &p
)) {
1403 #if defined(TCC_TARGET_PE)
1404 if (strstart("pe-", &p
)) {
1405 #elif defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1406 if (strstart("elf64-", &p
)) {
1408 if (strstart("elf32-", &p
)) {
1410 s
->output_format
= TCC_OUTPUT_FORMAT_ELF
;
1411 } else if (!strcmp(p
, "binary")) {
1412 s
->output_format
= TCC_OUTPUT_FORMAT_BINARY
;
1413 #ifdef TCC_TARGET_COFF
1414 } else if (!strcmp(p
, "coff")) {
1415 s
->output_format
= TCC_OUTPUT_FORMAT_COFF
;
1420 } else if (link_option(option
, "as-needed", &p
)) {
1422 } else if (link_option(option
, "O", &p
)) {
1424 } else if (link_option(option
, "rpath=", &p
)) {
1425 s
->rpath
= copy_linker_arg(p
);
1426 } else if (link_option(option
, "section-alignment=", &p
)) {
1427 s
->section_align
= strtoul(p
, &end
, 16);
1428 } else if (link_option(option
, "soname=", &p
)) {
1429 s
->soname
= copy_linker_arg(p
);
1430 #ifdef TCC_TARGET_PE
1431 } else if (link_option(option
, "file-alignment=", &p
)) {
1432 s
->pe_file_align
= strtoul(p
, &end
, 16);
1433 } else if (link_option(option
, "stack=", &p
)) {
1434 s
->pe_stack_size
= strtoul(p
, &end
, 10);
1435 } else if (link_option(option
, "subsystem=", &p
)) {
1436 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1437 if (!strcmp(p
, "native")) {
1438 s
->pe_subsystem
= 1;
1439 } else if (!strcmp(p
, "console")) {
1440 s
->pe_subsystem
= 3;
1441 } else if (!strcmp(p
, "gui")) {
1442 s
->pe_subsystem
= 2;
1443 } else if (!strcmp(p
, "posix")) {
1444 s
->pe_subsystem
= 7;
1445 } else if (!strcmp(p
, "efiapp")) {
1446 s
->pe_subsystem
= 10;
1447 } else if (!strcmp(p
, "efiboot")) {
1448 s
->pe_subsystem
= 11;
1449 } else if (!strcmp(p
, "efiruntime")) {
1450 s
->pe_subsystem
= 12;
1451 } else if (!strcmp(p
, "efirom")) {
1452 s
->pe_subsystem
= 13;
1453 #elif defined(TCC_TARGET_ARM)
1454 if (!strcmp(p
, "wince")) {
1455 s
->pe_subsystem
= 9;
1460 } else if (ret
= link_option(option
, "?whole-archive", &p
), ret
) {
1461 s
->alacarte_link
= ret
< 0;
1466 tcc_error("unsupported linker option '%s'", option
);
1469 if (ignoring
&& s
->warn_unsupported
)
1470 tcc_warning("unsupported linker option '%s'", option
);
1472 option
= skip_linker_arg(&p
);
1477 typedef struct TCCOption
{
1497 TCC_OPTION_dumpversion
,
1499 TCC_OPTION_float_abi
,
1507 TCC_OPTION_traditional
,
1514 TCC_OPTION_iwithprefix
,
1515 TCC_OPTION_nostdinc
,
1516 TCC_OPTION_nostdlib
,
1517 TCC_OPTION_print_search_dirs
,
1518 TCC_OPTION_rdynamic
,
1519 TCC_OPTION_pedantic
,
1531 #define TCC_OPTION_HAS_ARG 0x0001
1532 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
1534 static const TCCOption tcc_options
[] = {
1535 { "h", TCC_OPTION_HELP
, 0 },
1536 { "-help", TCC_OPTION_HELP
, 0 },
1537 { "?", TCC_OPTION_HELP
, 0 },
1538 { "I", TCC_OPTION_I
, TCC_OPTION_HAS_ARG
},
1539 { "D", TCC_OPTION_D
, TCC_OPTION_HAS_ARG
},
1540 { "U", TCC_OPTION_U
, TCC_OPTION_HAS_ARG
},
1541 { "P", TCC_OPTION_P
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1542 { "L", TCC_OPTION_L
, TCC_OPTION_HAS_ARG
},
1543 { "B", TCC_OPTION_B
, TCC_OPTION_HAS_ARG
},
1544 { "l", TCC_OPTION_l
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1545 { "bench", TCC_OPTION_bench
, 0 },
1546 #ifdef CONFIG_TCC_BACKTRACE
1547 { "bt", TCC_OPTION_bt
, TCC_OPTION_HAS_ARG
},
1549 #ifdef CONFIG_TCC_BCHECK
1550 { "b", TCC_OPTION_b
, 0 },
1552 { "g", TCC_OPTION_g
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1553 { "c", TCC_OPTION_c
, 0 },
1554 { "dumpversion", TCC_OPTION_dumpversion
, 0},
1555 { "d", TCC_OPTION_d
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1556 #ifdef TCC_TARGET_ARM
1557 { "mfloat-abi", TCC_OPTION_float_abi
, TCC_OPTION_HAS_ARG
},
1559 { "static", TCC_OPTION_static
, 0 },
1560 { "std", TCC_OPTION_std
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1561 { "shared", TCC_OPTION_shared
, 0 },
1562 { "soname", TCC_OPTION_soname
, TCC_OPTION_HAS_ARG
},
1563 { "o", TCC_OPTION_o
, TCC_OPTION_HAS_ARG
},
1564 { "pedantic", TCC_OPTION_pedantic
, 0},
1565 { "pthread", TCC_OPTION_pthread
, 0},
1566 { "run", TCC_OPTION_run
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1567 { "rdynamic", TCC_OPTION_rdynamic
, 0 },
1568 { "r", TCC_OPTION_r
, 0 },
1569 { "s", TCC_OPTION_s
, 0 },
1570 { "traditional", TCC_OPTION_traditional
, 0 },
1571 { "Wl,", TCC_OPTION_Wl
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1572 { "W", TCC_OPTION_W
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1573 { "O", TCC_OPTION_O
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1574 { "m", TCC_OPTION_m
, TCC_OPTION_HAS_ARG
},
1575 { "f", TCC_OPTION_f
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1576 { "isystem", TCC_OPTION_isystem
, TCC_OPTION_HAS_ARG
},
1577 { "iwithprefix", TCC_OPTION_iwithprefix
, TCC_OPTION_HAS_ARG
},
1578 { "nostdinc", TCC_OPTION_nostdinc
, 0 },
1579 { "nostdlib", TCC_OPTION_nostdlib
, 0 },
1580 { "print-search-dirs", TCC_OPTION_print_search_dirs
, 0 },
1581 { "v", TCC_OPTION_v
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
1582 { "w", TCC_OPTION_w
, 0 },
1583 { "pipe", TCC_OPTION_pipe
, 0},
1584 { "E", TCC_OPTION_E
, 0},
1585 { "MD", TCC_OPTION_MD
, 0},
1586 { "MF", TCC_OPTION_MF
, TCC_OPTION_HAS_ARG
},
1587 { "x", TCC_OPTION_x
, TCC_OPTION_HAS_ARG
},
1591 static void parse_option_D(TCCState
*s1
, const char *optarg
)
1593 char *sym
= tcc_strdup(optarg
);
1594 char *value
= strchr(sym
, '=');
1597 tcc_define_symbol(s1
, sym
, value
);
1601 static void args_parser_add_file(TCCState
*s
, const char* filename
, int filetype
)
1603 struct filespec
*f
= tcc_malloc(sizeof *f
+ strlen(filename
));
1605 strcpy(f
->name
, filename
);
1606 dynarray_add((void ***)&s
->files
, &s
->nb_files
, f
);
1609 /* read list file */
1610 static void args_parser_listfile(TCCState
*s
, const char *filename
)
1616 fd
= open(filename
, O_RDONLY
| O_BINARY
);
1618 tcc_error("file '%s' not found", filename
);
1620 len
= lseek(fd
, 0, SEEK_END
);
1621 p
= tcc_malloc(len
+ 1), p
[len
] = 0;
1622 lseek(fd
, 0, SEEK_SET
), read(fd
, p
, len
), close(fd
);
1623 tcc_set_options(s
, p
);
1627 PUB_FUNC
int tcc_parse_args(TCCState
*s
, int argc
, char **argv
)
1629 const TCCOption
*popt
;
1630 const char *optarg
, *r
;
1634 CString linker_arg
; /* collect -Wl options */
1637 cstr_new(&linker_arg
);
1639 while (optind
< argc
) {
1643 if (r
[0] == '@' && r
[1] != '\0') {
1644 args_parser_listfile(s
, r
+ 1);
1648 if (r
[0] != '-' || r
[1] == '\0') {
1649 args_parser_add_file(s
, r
, s
->filetype
);
1652 /* argv[0] will be this file */
1658 /* find option in table */
1659 for(popt
= tcc_options
; ; ++popt
) {
1660 const char *p1
= popt
->name
;
1661 const char *r1
= r
+ 1;
1663 tcc_error("invalid option -- '%s'", r
);
1664 if (!strstart(p1
, &r1
))
1667 if (popt
->flags
& TCC_OPTION_HAS_ARG
) {
1668 if (*r1
== '\0' && !(popt
->flags
& TCC_OPTION_NOSEP
)) {
1671 tcc_error("argument to '%s' is missing", r
);
1672 optarg
= argv
[optind
++];
1674 } else if (*r1
!= '\0')
1679 switch(popt
->index
) {
1680 case TCC_OPTION_HELP
:
1683 tcc_add_include_path(s
, optarg
);
1686 parse_option_D(s
, optarg
);
1689 tcc_undefine_symbol(s
, optarg
);
1692 tcc_add_library_path(s
, optarg
);
1695 /* set tcc utilities path (mainly for tcc development) */
1696 tcc_set_lib_path(s
, optarg
);
1699 args_parser_add_file(s
, optarg
, AFF_TYPE_LIBWH
- s
->alacarte_link
);
1702 case TCC_OPTION_pthread
:
1703 parse_option_D(s
, "_REENTRANT");
1704 s
->option_pthread
= 1;
1706 case TCC_OPTION_bench
:
1709 #ifdef CONFIG_TCC_BACKTRACE
1711 tcc_set_num_callers(atoi(optarg
));
1714 #ifdef CONFIG_TCC_BCHECK
1716 s
->do_bounds_check
= 1;
1727 tcc_warning("-%s: overriding compiler action already specified", popt
->name
);
1733 else if (*optarg
== 'M')
1736 goto unsupported_option
;
1738 #ifdef TCC_TARGET_ARM
1739 case TCC_OPTION_float_abi
:
1740 /* tcc doesn't support soft float yet */
1741 if (!strcmp(optarg
, "softfp")) {
1742 s
->float_abi
= ARM_SOFTFP_FLOAT
;
1743 tcc_undefine_symbol(s
, "__ARM_PCS_VFP");
1744 } else if (!strcmp(optarg
, "hard"))
1745 s
->float_abi
= ARM_HARD_FLOAT
;
1747 tcc_error("unsupported float abi '%s'", optarg
);
1750 case TCC_OPTION_static
:
1753 case TCC_OPTION_std
:
1754 /* silently ignore, a current purpose:
1755 allow to use a tcc as a reference compiler for "make test" */
1757 case TCC_OPTION_shared
:
1759 goto set_output_type
;
1760 case TCC_OPTION_soname
:
1761 s
->soname
= tcc_strdup(optarg
);
1764 s
->option_m
= tcc_strdup(optarg
);
1768 tcc_warning("multiple -o option");
1769 tcc_free(s
->outfile
);
1771 s
->outfile
= tcc_strdup(optarg
);
1774 /* generate a .o merging several output files */
1777 goto set_output_type
;
1778 case TCC_OPTION_isystem
:
1779 tcc_add_sysinclude_path(s
, optarg
);
1781 case TCC_OPTION_iwithprefix
:
1782 snprintf(buf
, sizeof buf
, "{B}/%s", optarg
);
1783 tcc_add_sysinclude_path(s
, buf
);
1785 case TCC_OPTION_nostdinc
:
1788 case TCC_OPTION_nostdlib
:
1791 case TCC_OPTION_print_search_dirs
:
1792 s
->print_search_dirs
= 1;
1794 case TCC_OPTION_run
:
1795 #ifndef TCC_IS_NATIVE
1796 tcc_error("-run is not available in a cross compiler");
1798 tcc_set_options(s
, optarg
);
1800 x
= TCC_OUTPUT_MEMORY
;
1801 goto set_output_type
;
1803 do ++s
->verbose
; while (*optarg
++ == 'v');
1806 if (tcc_set_flag(s
, optarg
, 1) < 0)
1807 goto unsupported_option
;
1810 if (tcc_set_warning(s
, optarg
, 1) < 0)
1811 goto unsupported_option
;
1816 case TCC_OPTION_rdynamic
:
1820 if (linker_arg
.size
)
1821 --linker_arg
.size
, cstr_ccat(&linker_arg
, ',');
1822 cstr_cat(&linker_arg
, optarg
, 0);
1823 if (tcc_set_linker(s
, linker_arg
.data
))
1824 cstr_free(&linker_arg
);
1827 x
= TCC_OUTPUT_PREPROCESS
;
1828 goto set_output_type
;
1830 s
->Pflag
= atoi(optarg
) + 1;
1836 s
->deps_outfile
= tcc_strdup(optarg
);
1838 case TCC_OPTION_dumpversion
:
1839 printf ("%s\n", TCC_VERSION
);
1844 s
->filetype
= AFF_TYPE_C
;
1845 else if (*optarg
== 'a')
1846 s
->filetype
= AFF_TYPE_ASMPP
;
1847 else if (*optarg
== 'n')
1848 s
->filetype
= AFF_TYPE_NONE
;
1850 tcc_warning("unsupported language '%s'", optarg
);
1855 tcc_define_symbol(s
, "__OPTIMIZE__", NULL
);
1857 case TCC_OPTION_traditional
:
1858 case TCC_OPTION_pedantic
:
1859 case TCC_OPTION_pipe
:
1865 if (s
->warn_unsupported
)
1866 tcc_warning("unsupported option '%s'", r
);
1871 if (linker_arg
.size
) {
1872 r
= linker_arg
.data
;
1879 LIBTCCAPI
int tcc_set_options(TCCState
*s
, const char *r
)
1886 argc
= 0, argv
= NULL
;
1888 while (c
= (unsigned char)*r
, c
&& c
<= ' ')
1894 while (c
= (unsigned char)*r
, c
) {
1896 if (c
== '\\' && (*r
== '"' || *r
== '\\')) {
1898 } else if (c
== '"') {
1901 } else if (q
== 0 && c
<= ' ') {
1907 //printf("<%s>\n", str.data), fflush(stdout);
1908 dynarray_add((void ***)&argv
, &argc
, tcc_strdup(str
.data
));
1911 ret
= tcc_parse_args(s
, argc
, argv
);
1912 dynarray_reset(&argv
, &argc
);
1916 PUB_FUNC
void tcc_print_stats(TCCState
*s
, unsigned total_time
)
1920 if (total_bytes
< 1)
1922 fprintf(stderr
, "%d idents, %d lines, %d bytes, %0.3f s, %u lines/s, %0.1f MB/s\n",
1923 tok_ident
- TOK_IDENT
, total_lines
, total_bytes
,
1924 (double)total_time
/1000,
1925 (unsigned)total_lines
*1000/total_time
,
1926 (double)total_bytes
/1000/total_time
);
1929 PUB_FUNC
void tcc_set_environment(TCCState
*s
)
1933 path
= getenv("C_INCLUDE_PATH");
1935 tcc_add_include_path(s
, path
);
1937 path
= getenv("CPATH");
1939 tcc_add_include_path(s
, path
);
1941 path
= getenv("LIBRARY_PATH");
1943 tcc_add_library_path(s
, path
);