update gen_cast
[tinycc.git] / libtcc.c
blob608e515a1d4cacf814c04f568888b811542253ee
1 /*
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
22 #include "tccpp.c"
23 #include "tccgen.c"
24 #include "tccelf.c"
25 #include "tccrun.c"
26 #ifdef TCC_TARGET_I386
27 #include "i386-gen.c"
28 #include "i386-link.c"
29 #include "i386-asm.c"
30 #elif defined(TCC_TARGET_ARM)
31 #include "arm-gen.c"
32 #include "arm-link.c"
33 #include "arm-asm.c"
34 #elif defined(TCC_TARGET_ARM64)
35 #include "arm64-gen.c"
36 #include "arm64-link.c"
37 #elif defined(TCC_TARGET_C67)
38 #include "c67-gen.c"
39 #include "c67-link.c"
40 #include "tcccoff.c"
41 #elif defined(TCC_TARGET_X86_64)
42 #include "x86_64-gen.c"
43 #include "x86_64-link.c"
44 #include "i386-asm.c"
45 #elif defined(TCC_TARGET_RISCV64)
46 #include "riscv64-gen.c"
47 #include "riscv64-link.c"
48 #else
49 #error unknown target
50 #endif
51 #ifdef CONFIG_TCC_ASM
52 #include "tccasm.c"
53 #endif
54 #ifdef TCC_TARGET_PE
55 #include "tccpe.c"
56 #endif
57 #endif /* ONE_SOURCE */
59 #include "tcc.h"
61 /********************************************************/
62 /* global variables */
64 /* XXX: get rid of this ASAP (or maybe not) */
65 ST_DATA struct TCCState *tcc_state;
67 #ifdef MEM_DEBUG
68 static int nb_states;
69 #endif
71 /********************************************************/
72 #ifdef _WIN32
73 ST_FUNC char *normalize_slashes(char *path)
75 char *p;
76 for (p = path; *p; ++p)
77 if (*p == '\\')
78 *p = '/';
79 return path;
82 static HMODULE tcc_module;
84 /* on win32, we suppose the lib and includes are at the location of 'tcc.exe' */
85 static void tcc_set_lib_path_w32(TCCState *s)
87 char path[1024], *p;
88 GetModuleFileNameA(tcc_module, path, sizeof path);
89 p = tcc_basename(normalize_slashes(strlwr(path)));
90 if (p > path)
91 --p;
92 *p = 0;
93 tcc_set_lib_path(s, path);
96 #ifdef TCC_TARGET_PE
97 static void tcc_add_systemdir(TCCState *s)
99 char buf[1000];
100 GetSystemDirectory(buf, sizeof buf);
101 tcc_add_library_path(s, normalize_slashes(buf));
103 #endif
105 #ifdef LIBTCC_AS_DLL
106 BOOL WINAPI DllMain (HINSTANCE hDll, DWORD dwReason, LPVOID lpReserved)
108 if (DLL_PROCESS_ATTACH == dwReason)
109 tcc_module = hDll;
110 return TRUE;
112 #endif
113 #endif
115 /********************************************************/
116 #ifndef CONFIG_TCC_SEMLOCK
117 #define WAIT_SEM()
118 #define POST_SEM()
119 #elif defined _WIN32
120 static int tcc_sem_init;
121 static CRITICAL_SECTION tcc_cr;
122 static void wait_sem(void)
124 if (!tcc_sem_init)
125 InitializeCriticalSection(&tcc_cr), tcc_sem_init = 1;
126 EnterCriticalSection(&tcc_cr);
128 #define WAIT_SEM() wait_sem()
129 #define POST_SEM() LeaveCriticalSection(&tcc_cr);
130 #else
131 #include <semaphore.h>
132 static int tcc_sem_init;
133 static sem_t tcc_sem;
134 static void wait_sem(void)
136 if (!tcc_sem_init)
137 sem_init(&tcc_sem, 0, 1), tcc_sem_init = 1;
138 while (sem_wait (&tcc_sem) < 0 && errno == EINTR);
140 #define WAIT_SEM() wait_sem()
141 #define POST_SEM() sem_post(&tcc_sem)
142 #endif
144 /********************************************************/
145 /* copy a string and truncate it. */
146 ST_FUNC char *pstrcpy(char *buf, size_t buf_size, const char *s)
148 char *q, *q_end;
149 int c;
151 if (buf_size > 0) {
152 q = buf;
153 q_end = buf + buf_size - 1;
154 while (q < q_end) {
155 c = *s++;
156 if (c == '\0')
157 break;
158 *q++ = c;
160 *q = '\0';
162 return buf;
165 /* strcat and truncate. */
166 ST_FUNC char *pstrcat(char *buf, size_t buf_size, const char *s)
168 size_t len;
169 len = strlen(buf);
170 if (len < buf_size)
171 pstrcpy(buf + len, buf_size - len, s);
172 return buf;
175 ST_FUNC char *pstrncpy(char *out, const char *in, size_t num)
177 memcpy(out, in, num);
178 out[num] = '\0';
179 return out;
182 /* extract the basename of a file */
183 PUB_FUNC char *tcc_basename(const char *name)
185 char *p = strchr(name, 0);
186 while (p > name && !IS_DIRSEP(p[-1]))
187 --p;
188 return p;
191 /* extract extension part of a file
193 * (if no extension, return pointer to end-of-string)
195 PUB_FUNC char *tcc_fileextension (const char *name)
197 char *b = tcc_basename(name);
198 char *e = strrchr(b, '.');
199 return e ? e : strchr(b, 0);
202 /********************************************************/
203 /* memory management */
205 #undef free
206 #undef malloc
207 #undef realloc
209 #ifndef MEM_DEBUG
211 PUB_FUNC void tcc_free(void *ptr)
213 free(ptr);
216 PUB_FUNC void *tcc_malloc(unsigned long size)
218 void *ptr;
219 ptr = malloc(size);
220 if (!ptr && size)
221 _tcc_error("memory full (malloc)");
222 return ptr;
225 PUB_FUNC void *tcc_mallocz(unsigned long size)
227 void *ptr;
228 ptr = tcc_malloc(size);
229 memset(ptr, 0, size);
230 return ptr;
233 PUB_FUNC void *tcc_realloc(void *ptr, unsigned long size)
235 void *ptr1;
236 ptr1 = realloc(ptr, size);
237 if (!ptr1 && size)
238 _tcc_error("memory full (realloc)");
239 return ptr1;
242 PUB_FUNC char *tcc_strdup(const char *str)
244 char *ptr;
245 ptr = tcc_malloc(strlen(str) + 1);
246 strcpy(ptr, str);
247 return ptr;
250 #else
252 #define MEM_DEBUG_MAGIC1 0xFEEDDEB1
253 #define MEM_DEBUG_MAGIC2 0xFEEDDEB2
254 #define MEM_DEBUG_MAGIC3 0xFEEDDEB3
255 #define MEM_DEBUG_FILE_LEN 40
256 #define MEM_DEBUG_CHECK3(header) \
257 ((mem_debug_header_t*)((char*)header + header->size))->magic3
258 #define MEM_USER_PTR(header) \
259 ((char *)header + offsetof(mem_debug_header_t, magic3))
260 #define MEM_HEADER_PTR(ptr) \
261 (mem_debug_header_t *)((char*)ptr - offsetof(mem_debug_header_t, magic3))
263 struct mem_debug_header {
264 unsigned magic1;
265 unsigned size;
266 struct mem_debug_header *prev;
267 struct mem_debug_header *next;
268 int line_num;
269 char file_name[MEM_DEBUG_FILE_LEN + 1];
270 unsigned magic2;
271 ALIGNED(16) unsigned magic3;
274 typedef struct mem_debug_header mem_debug_header_t;
276 static mem_debug_header_t *mem_debug_chain;
277 static unsigned mem_cur_size;
278 static unsigned mem_max_size;
280 static mem_debug_header_t *malloc_check(void *ptr, const char *msg)
282 mem_debug_header_t * header = MEM_HEADER_PTR(ptr);
283 if (header->magic1 != MEM_DEBUG_MAGIC1 ||
284 header->magic2 != MEM_DEBUG_MAGIC2 ||
285 MEM_DEBUG_CHECK3(header) != MEM_DEBUG_MAGIC3 ||
286 header->size == (unsigned)-1) {
287 fprintf(stderr, "%s check failed\n", msg);
288 if (header->magic1 == MEM_DEBUG_MAGIC1)
289 fprintf(stderr, "%s:%u: block allocated here.\n",
290 header->file_name, header->line_num);
291 exit(1);
293 return header;
296 PUB_FUNC void *tcc_malloc_debug(unsigned long size, const char *file, int line)
298 int ofs;
299 mem_debug_header_t *header;
301 header = malloc(sizeof(mem_debug_header_t) + size);
302 if (!header)
303 _tcc_error("memory full (malloc)");
305 header->magic1 = MEM_DEBUG_MAGIC1;
306 header->magic2 = MEM_DEBUG_MAGIC2;
307 header->size = size;
308 MEM_DEBUG_CHECK3(header) = MEM_DEBUG_MAGIC3;
309 header->line_num = line;
310 ofs = strlen(file) - MEM_DEBUG_FILE_LEN;
311 strncpy(header->file_name, file + (ofs > 0 ? ofs : 0), MEM_DEBUG_FILE_LEN);
312 header->file_name[MEM_DEBUG_FILE_LEN] = 0;
314 header->next = mem_debug_chain;
315 header->prev = NULL;
316 if (header->next)
317 header->next->prev = header;
318 mem_debug_chain = header;
320 mem_cur_size += size;
321 if (mem_cur_size > mem_max_size)
322 mem_max_size = mem_cur_size;
324 return MEM_USER_PTR(header);
327 PUB_FUNC void tcc_free_debug(void *ptr)
329 mem_debug_header_t *header;
330 if (!ptr)
331 return;
332 header = malloc_check(ptr, "tcc_free");
333 mem_cur_size -= header->size;
334 header->size = (unsigned)-1;
335 if (header->next)
336 header->next->prev = header->prev;
337 if (header->prev)
338 header->prev->next = header->next;
339 if (header == mem_debug_chain)
340 mem_debug_chain = header->next;
341 free(header);
344 PUB_FUNC void *tcc_mallocz_debug(unsigned long size, const char *file, int line)
346 void *ptr;
347 ptr = tcc_malloc_debug(size,file,line);
348 memset(ptr, 0, size);
349 return ptr;
352 PUB_FUNC void *tcc_realloc_debug(void *ptr, unsigned long size, const char *file, int line)
354 mem_debug_header_t *header;
355 int mem_debug_chain_update = 0;
356 if (!ptr)
357 return tcc_malloc_debug(size, file, line);
358 header = malloc_check(ptr, "tcc_realloc");
359 mem_cur_size -= header->size;
360 mem_debug_chain_update = (header == mem_debug_chain);
361 header = realloc(header, sizeof(mem_debug_header_t) + size);
362 if (!header)
363 _tcc_error("memory full (realloc)");
364 header->size = size;
365 MEM_DEBUG_CHECK3(header) = MEM_DEBUG_MAGIC3;
366 if (header->next)
367 header->next->prev = header;
368 if (header->prev)
369 header->prev->next = header;
370 if (mem_debug_chain_update)
371 mem_debug_chain = header;
372 mem_cur_size += size;
373 if (mem_cur_size > mem_max_size)
374 mem_max_size = mem_cur_size;
375 return MEM_USER_PTR(header);
378 PUB_FUNC char *tcc_strdup_debug(const char *str, const char *file, int line)
380 char *ptr;
381 ptr = tcc_malloc_debug(strlen(str) + 1, file, line);
382 strcpy(ptr, str);
383 return ptr;
386 PUB_FUNC void tcc_memcheck(void)
388 if (mem_cur_size) {
389 mem_debug_header_t *header = mem_debug_chain;
390 fprintf(stderr, "MEM_DEBUG: mem_leak= %d bytes, mem_max_size= %d bytes\n",
391 mem_cur_size, mem_max_size);
392 while (header) {
393 fprintf(stderr, "%s:%u: error: %u bytes leaked\n",
394 header->file_name, header->line_num, header->size);
395 header = header->next;
397 #if MEM_DEBUG-0 == 2
398 exit(2);
399 #endif
402 #endif /* MEM_DEBUG */
404 #define free(p) use_tcc_free(p)
405 #define malloc(s) use_tcc_malloc(s)
406 #define realloc(p, s) use_tcc_realloc(p, s)
408 /********************************************************/
409 /* dynarrays */
411 ST_FUNC void dynarray_add(void *ptab, int *nb_ptr, void *data)
413 int nb, nb_alloc;
414 void **pp;
416 nb = *nb_ptr;
417 pp = *(void ***)ptab;
418 /* every power of two we double array size */
419 if ((nb & (nb - 1)) == 0) {
420 if (!nb)
421 nb_alloc = 1;
422 else
423 nb_alloc = nb * 2;
424 pp = tcc_realloc(pp, nb_alloc * sizeof(void *));
425 *(void***)ptab = pp;
427 pp[nb++] = data;
428 *nb_ptr = nb;
431 ST_FUNC void dynarray_reset(void *pp, int *n)
433 void **p;
434 for (p = *(void***)pp; *n; ++p, --*n)
435 if (*p)
436 tcc_free(*p);
437 tcc_free(*(void**)pp);
438 *(void**)pp = NULL;
441 static void tcc_split_path(TCCState *s, void *p_ary, int *p_nb_ary, const char *in)
443 const char *p;
444 do {
445 int c;
446 CString str;
448 cstr_new(&str);
449 for (p = in; c = *p, c != '\0' && c != PATHSEP[0]; ++p) {
450 if (c == '{' && p[1] && p[2] == '}') {
451 c = p[1], p += 2;
452 if (c == 'B')
453 cstr_cat(&str, s->tcc_lib_path, -1);
454 } else {
455 cstr_ccat(&str, c);
458 if (str.size) {
459 cstr_ccat(&str, '\0');
460 dynarray_add(p_ary, p_nb_ary, tcc_strdup(str.data));
462 cstr_free(&str);
463 in = p+1;
464 } while (*p);
467 /********************************************************/
469 static void strcat_vprintf(char *buf, int buf_size, const char *fmt, va_list ap)
471 int len;
472 len = strlen(buf);
473 vsnprintf(buf + len, buf_size - len, fmt, ap);
476 static void strcat_printf(char *buf, int buf_size, const char *fmt, ...)
478 va_list ap;
479 va_start(ap, fmt);
480 strcat_vprintf(buf, buf_size, fmt, ap);
481 va_end(ap);
484 #define ERROR_WARN 0
485 #define ERROR_NOABORT 1
486 #define ERROR_ERROR 2
488 PUB_FUNC void tcc_enter_state(TCCState *s1)
490 WAIT_SEM();
491 tcc_state = s1;
494 static void error1(int mode, const char *fmt, va_list ap)
496 char buf[2048];
497 BufferedFile **pf, *f;
498 TCCState *s1 = tcc_state;
500 /* 's1->error_set_jmp_enabled' means that we're called from
501 within the parser/generator and 'tcc_state' was already
502 set (i.e. not by the function above).
504 Otherwise, 's1 = NULL' means we're called because of severe
505 problems from tcc_malloc() which under normal conditions
506 should never happen. */
508 if (s1 && !s1->error_set_jmp_enabled) {
509 tcc_state = NULL;
510 POST_SEM();
513 if (mode == ERROR_WARN) {
514 if (s1->warn_none)
515 return;
516 if (s1->warn_error)
517 mode = ERROR_ERROR;
520 buf[0] = '\0';
521 /* use upper file if inline ":asm:" or token ":paste:" */
522 for (f = file; f && f->filename[0] == ':'; f = f->prev)
524 if (f) {
525 for(pf = s1->include_stack; pf < s1->include_stack_ptr; pf++)
526 strcat_printf(buf, sizeof(buf), "In file included from %s:%d:\n",
527 (*pf)->filename, (*pf)->line_num);
528 if (s1->error_set_jmp_enabled) {
529 strcat_printf(buf, sizeof(buf), "%s:%d: ",
530 f->filename, f->line_num - !!(tok_flags & TOK_FLAG_BOL));
531 } else {
532 strcat_printf(buf, sizeof(buf), "%s: ",
533 f->filename);
535 } else {
536 strcat_printf(buf, sizeof(buf), "tcc: ");
538 if (mode == ERROR_WARN)
539 strcat_printf(buf, sizeof(buf), "warning: ");
540 else
541 strcat_printf(buf, sizeof(buf), "error: ");
542 strcat_vprintf(buf, sizeof(buf), fmt, ap);
543 if (!s1 || !s1->error_func) {
544 /* default case: stderr */
545 if (s1 && s1->output_type == TCC_OUTPUT_PREPROCESS && s1->ppfp == stdout)
546 /* print a newline during tcc -E */
547 printf("\n"), fflush(stdout);
548 fflush(stdout); /* flush -v output */
549 fprintf(stderr, "%s\n", buf);
550 fflush(stderr); /* print error/warning now (win32) */
551 } else {
552 s1->error_func(s1->error_opaque, buf);
554 if (s1) {
555 if (mode != ERROR_WARN)
556 s1->nb_errors++;
557 if (mode != ERROR_ERROR)
558 return;
559 if (s1->error_set_jmp_enabled)
560 longjmp(s1->error_jmp_buf, 1);
562 exit(1);
565 LIBTCCAPI void tcc_set_error_func(TCCState *s, void *error_opaque, TCCErrorFunc error_func)
567 s->error_opaque = error_opaque;
568 s->error_func = error_func;
571 LIBTCCAPI TCCErrorFunc tcc_get_error_func(TCCState *s)
573 return s->error_func;
576 LIBTCCAPI void *tcc_get_error_opaque(TCCState *s)
578 return s->error_opaque;
581 /* error without aborting current compilation */
582 PUB_FUNC void _tcc_error_noabort(const char *fmt, ...)
584 va_list ap;
585 va_start(ap, fmt);
586 error1(ERROR_NOABORT, fmt, ap);
587 va_end(ap);
590 PUB_FUNC void _tcc_error(const char *fmt, ...)
592 va_list ap;
593 va_start(ap, fmt);
594 for (;;) error1(ERROR_ERROR, fmt, ap);
597 PUB_FUNC void _tcc_warning(const char *fmt, ...)
599 va_list ap;
600 va_start(ap, fmt);
601 error1(ERROR_WARN, fmt, ap);
602 va_end(ap);
605 /********************************************************/
606 /* I/O layer */
608 ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen)
610 BufferedFile *bf;
611 int buflen = initlen ? initlen : IO_BUF_SIZE;
613 bf = tcc_mallocz(sizeof(BufferedFile) + buflen);
614 bf->buf_ptr = bf->buffer;
615 bf->buf_end = bf->buffer + initlen;
616 bf->buf_end[0] = CH_EOB; /* put eob symbol */
617 pstrcpy(bf->filename, sizeof(bf->filename), filename);
618 #ifdef _WIN32
619 normalize_slashes(bf->filename);
620 #endif
621 bf->true_filename = bf->filename;
622 bf->line_num = 1;
623 bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
624 bf->fd = -1;
625 bf->prev = file;
626 file = bf;
627 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
630 ST_FUNC void tcc_close(void)
632 TCCState *s1 = tcc_state;
633 BufferedFile *bf = file;
634 if (bf->fd > 0) {
635 close(bf->fd);
636 total_lines += bf->line_num;
638 if (bf->true_filename != bf->filename)
639 tcc_free(bf->true_filename);
640 file = bf->prev;
641 tcc_free(bf);
644 static int _tcc_open(TCCState *s1, const char *filename)
646 int fd;
647 if (strcmp(filename, "-") == 0)
648 fd = 0, filename = "<stdin>";
649 else
650 fd = open(filename, O_RDONLY | O_BINARY);
651 if ((s1->verbose == 2 && fd >= 0) || s1->verbose == 3)
652 printf("%s %*s%s\n", fd < 0 ? "nf":"->",
653 (int)(s1->include_stack_ptr - s1->include_stack), "", filename);
654 return fd;
657 ST_FUNC int tcc_open(TCCState *s1, const char *filename)
659 int fd = _tcc_open(s1, filename);
660 if (fd < 0)
661 return -1;
662 tcc_open_bf(s1, filename, 0);
663 file->fd = fd;
664 return 0;
667 /* compile the file opened in 'file'. Return non zero if errors. */
668 static int tcc_compile(TCCState *s1, int filetype, const char *str, int fd)
670 /* Here we enter the code section where we use the global variables for
671 parsing and code generation (tccpp.c, tccgen.c, <target>-gen.c).
672 Other threads need to wait until we're done.
674 Alternatively we could use thread local storage for those global
675 variables, which may or may not have advantages */
677 WAIT_SEM();
678 tcc_state = s1;
680 if (setjmp(s1->error_jmp_buf) == 0) {
681 int is_asm;
682 s1->error_set_jmp_enabled = 1;
683 s1->nb_errors = 0;
685 if (fd == -1) {
686 int len = strlen(str);
687 tcc_open_bf(s1, "<string>", len);
688 memcpy(file->buffer, str, len);
689 } else {
690 tcc_open_bf(s1, str, 0);
691 file->fd = fd;
694 is_asm = !!(filetype & (AFF_TYPE_ASM|AFF_TYPE_ASMPP));
695 tccelf_begin_file(s1);
696 preprocess_start(s1, is_asm);
697 tccgen_init(s1);
698 if (s1->output_type == TCC_OUTPUT_PREPROCESS) {
699 tcc_preprocess(s1);
700 } else if (is_asm) {
701 #ifdef CONFIG_TCC_ASM
702 tcc_assemble(s1, !!(filetype & AFF_TYPE_ASMPP));
703 #else
704 tcc_error_noabort("asm not supported");
705 #endif
706 } else {
707 tccgen_compile(s1);
710 s1->error_set_jmp_enabled = 0;
711 tccgen_finish(s1);
712 preprocess_end(s1);
713 tccelf_end_file(s1);
715 tcc_state = NULL;
716 POST_SEM();
717 return s1->nb_errors != 0 ? -1 : 0;
720 LIBTCCAPI int tcc_compile_string(TCCState *s, const char *str)
722 return tcc_compile(s, s->filetype, str, -1);
725 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
726 LIBTCCAPI void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
728 if (!value)
729 value = "1";
730 cstr_printf(&s1->cmdline_defs, "#define %s %s\n", sym, value);
733 /* undefine a preprocessor symbol */
734 LIBTCCAPI void tcc_undefine_symbol(TCCState *s1, const char *sym)
736 cstr_printf(&s1->cmdline_defs, "#undef %s\n", sym);
740 LIBTCCAPI TCCState *tcc_new(void)
742 TCCState *s;
744 s = tcc_mallocz(sizeof(TCCState));
745 if (!s)
746 return NULL;
747 #ifdef MEM_DEBUG
748 ++nb_states;
749 #endif
751 #undef gnu_ext
753 s->gnu_ext = 1;
754 s->tcc_ext = 1;
755 s->nocommon = 1;
756 s->dollars_in_identifiers = 1; /*on by default like in gcc/clang*/
757 s->cversion = 199901; /* default unless -std=c11 is supplied */
758 s->warn_implicit_function_declaration = 1;
759 s->ms_extensions = 1;
761 #ifdef CHAR_IS_UNSIGNED
762 s->char_is_unsigned = 1;
763 #endif
764 #ifdef TCC_TARGET_I386
765 s->seg_size = 32;
766 #endif
767 /* enable this if you want symbols with leading underscore on windows: */
768 #if 0 /* def TCC_TARGET_PE */
769 s->leading_underscore = 1;
770 #endif
771 #ifdef CONFIG_TCC_BACKTRACE
772 s->rt_num_callers = 6;
773 #endif
774 s->ppfp = stdout;
775 /* might be used in error() before preprocess_start() */
776 s->include_stack_ptr = s->include_stack;
778 tccelf_new(s);
780 #ifdef _WIN32
781 tcc_set_lib_path_w32(s);
782 #else
783 tcc_set_lib_path(s, CONFIG_TCCDIR);
784 #endif
787 /* define __TINYC__ 92X */
788 char buffer[32]; int a,b,c;
789 sscanf(TCC_VERSION, "%d.%d.%d", &a, &b, &c);
790 sprintf(buffer, "%d", a*10000 + b*100 + c);
791 tcc_define_symbol(s, "__TINYC__", buffer);
794 /* standard defines */
795 tcc_define_symbol(s, "__STDC__", NULL);
796 tcc_define_symbol(s, "__STDC_VERSION__", "199901L");
797 tcc_define_symbol(s, "__STDC_HOSTED__", NULL);
799 /* target defines */
800 #if defined(TCC_TARGET_I386)
801 tcc_define_symbol(s, "__i386__", NULL);
802 tcc_define_symbol(s, "__i386", NULL);
803 tcc_define_symbol(s, "i386", NULL);
804 #elif defined(TCC_TARGET_X86_64)
805 tcc_define_symbol(s, "__x86_64__", NULL);
806 #elif defined(TCC_TARGET_ARM)
807 tcc_define_symbol(s, "__ARM_ARCH_4__", NULL);
808 tcc_define_symbol(s, "__arm_elf__", NULL);
809 tcc_define_symbol(s, "__arm_elf", NULL);
810 tcc_define_symbol(s, "arm_elf", NULL);
811 tcc_define_symbol(s, "__arm__", NULL);
812 tcc_define_symbol(s, "__arm", NULL);
813 tcc_define_symbol(s, "arm", NULL);
814 tcc_define_symbol(s, "__APCS_32__", NULL);
815 tcc_define_symbol(s, "__ARMEL__", NULL);
816 #if defined(TCC_ARM_EABI)
817 tcc_define_symbol(s, "__ARM_EABI__", NULL);
818 #endif
819 #if defined(TCC_ARM_HARDFLOAT)
820 s->float_abi = ARM_HARD_FLOAT;
821 tcc_define_symbol(s, "__ARM_PCS_VFP", NULL);
822 #else
823 s->float_abi = ARM_SOFTFP_FLOAT;
824 #endif
825 #elif defined(TCC_TARGET_ARM64)
826 tcc_define_symbol(s, "__aarch64__", NULL);
827 #elif defined TCC_TARGET_C67
828 tcc_define_symbol(s, "__C67__", NULL);
829 #elif defined TCC_TARGET_RISCV64
830 tcc_define_symbol(s, "__riscv", NULL);
831 tcc_define_symbol(s, "__riscv_xlen", "64");
832 tcc_define_symbol(s, "__riscv_flen", "64");
833 tcc_define_symbol(s, "__riscv_div", NULL);
834 tcc_define_symbol(s, "__riscv_mul", NULL);
835 tcc_define_symbol(s, "__riscv_fdiv", NULL);
836 tcc_define_symbol(s, "__riscv_fsqrt", NULL);
837 tcc_define_symbol(s, "__riscv_float_abi_double", NULL);
838 #endif
840 #ifdef TCC_TARGET_PE
841 tcc_define_symbol(s, "_WIN32", NULL);
842 # ifdef TCC_TARGET_X86_64
843 tcc_define_symbol(s, "_WIN64", NULL);
844 # endif
845 #else
846 tcc_define_symbol(s, "__unix__", NULL);
847 tcc_define_symbol(s, "__unix", NULL);
848 tcc_define_symbol(s, "unix", NULL);
849 # if defined(__linux__)
850 tcc_define_symbol(s, "__linux__", NULL);
851 tcc_define_symbol(s, "__linux", NULL);
852 # endif
853 # if defined(__FreeBSD__)
854 tcc_define_symbol(s, "__FreeBSD__", "__FreeBSD__");
855 /* No 'Thread Storage Local' on FreeBSD with tcc */
856 tcc_define_symbol(s, "__NO_TLS", NULL);
857 # endif
858 # if defined(__FreeBSD_kernel__)
859 tcc_define_symbol(s, "__FreeBSD_kernel__", NULL);
860 # endif
861 # if defined(__NetBSD__)
862 tcc_define_symbol(s, "__NetBSD__", "__NetBSD__");
863 # endif
864 # if defined(__OpenBSD__)
865 tcc_define_symbol(s, "__OpenBSD__", "__OpenBSD__");
866 # endif
867 #endif
869 /* TinyCC & gcc defines */
870 #if PTR_SIZE == 4
871 /* 32bit systems. */
872 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned int");
873 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "int");
874 tcc_define_symbol(s, "__ILP32__", NULL);
875 #elif LONG_SIZE == 4
876 /* 64bit Windows. */
877 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned long long");
878 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "long long");
879 tcc_define_symbol(s, "__LLP64__", NULL);
880 #else
881 /* Other 64bit systems. */
882 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned long");
883 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "long");
884 tcc_define_symbol(s, "__LP64__", NULL);
885 #endif
886 tcc_define_symbol(s, "__SIZEOF_POINTER__", PTR_SIZE == 4 ? "4" : "8");
888 #ifdef TCC_TARGET_PE
889 tcc_define_symbol(s, "__WCHAR_TYPE__", "unsigned short");
890 tcc_define_symbol(s, "__WINT_TYPE__", "unsigned short");
891 #else
892 tcc_define_symbol(s, "__WCHAR_TYPE__", "int");
893 /* wint_t is unsigned int by default, but (signed) int on BSDs
894 and unsigned short on windows. Other OSes might have still
895 other conventions, sigh. */
896 # if defined(__FreeBSD__) || defined (__FreeBSD_kernel__) \
897 || defined(__NetBSD__) || defined(__OpenBSD__)
898 tcc_define_symbol(s, "__WINT_TYPE__", "int");
899 # ifdef __FreeBSD__
900 /* define __GNUC__ to have some useful stuff from sys/cdefs.h
901 that are unconditionally used in FreeBSDs other system headers :/ */
902 tcc_define_symbol(s, "__GNUC__", "2");
903 tcc_define_symbol(s, "__GNUC_MINOR__", "7");
904 tcc_define_symbol(s, "__builtin_alloca", "alloca");
905 # endif
906 # else
907 tcc_define_symbol(s, "__WINT_TYPE__", "unsigned int");
908 /* glibc defines */
909 tcc_define_symbol(s, "__REDIRECT(name, proto, alias)",
910 "name proto __asm__ (#alias)");
911 tcc_define_symbol(s, "__REDIRECT_NTH(name, proto, alias)",
912 "name proto __asm__ (#alias) __THROW");
913 # endif
914 # if defined(TCC_MUSL)
915 tcc_define_symbol(s, "__DEFINED_va_list", "");
916 tcc_define_symbol(s, "__DEFINED___isoc_va_list", "");
917 tcc_define_symbol(s, "__isoc_va_list", "void *");
918 # endif /* TCC_MUSL */
919 /* Some GCC builtins that are simple to express as macros. */
920 tcc_define_symbol(s, "__builtin_extract_return_addr(x)", "x");
921 #endif /* ndef TCC_TARGET_PE */
922 return s;
925 LIBTCCAPI void tcc_delete(TCCState *s1)
927 /* free sections */
928 tccelf_delete(s1);
930 /* free library paths */
931 dynarray_reset(&s1->library_paths, &s1->nb_library_paths);
932 dynarray_reset(&s1->crt_paths, &s1->nb_crt_paths);
934 /* free include paths */
935 dynarray_reset(&s1->include_paths, &s1->nb_include_paths);
936 dynarray_reset(&s1->sysinclude_paths, &s1->nb_sysinclude_paths);
938 tcc_free(s1->tcc_lib_path);
939 tcc_free(s1->soname);
940 tcc_free(s1->rpath);
941 tcc_free(s1->init_symbol);
942 tcc_free(s1->fini_symbol);
943 tcc_free(s1->outfile);
944 tcc_free(s1->deps_outfile);
945 dynarray_reset(&s1->files, &s1->nb_files);
946 dynarray_reset(&s1->target_deps, &s1->nb_target_deps);
947 dynarray_reset(&s1->pragma_libs, &s1->nb_pragma_libs);
948 dynarray_reset(&s1->argv, &s1->argc);
950 cstr_free(&s1->cmdline_defs);
951 cstr_free(&s1->cmdline_incl);
952 #ifdef TCC_IS_NATIVE
953 /* free runtime memory */
954 tcc_run_free(s1);
955 #endif
957 tcc_free(s1);
958 #ifdef MEM_DEBUG
959 if (0 == --nb_states)
960 tcc_memcheck();
961 #endif
964 LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type)
966 s->output_type = output_type;
968 /* always elf for objects */
969 if (output_type == TCC_OUTPUT_OBJ)
970 s->output_format = TCC_OUTPUT_FORMAT_ELF;
972 if (s->char_is_unsigned)
973 tcc_define_symbol(s, "__CHAR_UNSIGNED__", NULL);
975 if (!s->nostdinc) {
976 /* default include paths */
977 /* -isystem paths have already been handled */
978 tcc_add_sysinclude_path(s, CONFIG_TCC_SYSINCLUDEPATHS);
981 #ifdef CONFIG_TCC_BCHECK
982 if (s->do_bounds_check) {
983 /* if bound checking, then add corresponding sections */
984 tccelf_bounds_new(s);
985 /* define symbol */
986 tcc_define_symbol(s, "__BOUNDS_CHECKING_ON", NULL);
988 #endif
989 if (s->do_debug) {
990 /* add debug sections */
991 tccelf_stab_new(s);
994 tcc_add_library_path(s, CONFIG_TCC_LIBPATHS);
996 #ifdef TCC_TARGET_PE
997 # ifdef _WIN32
998 if (!s->nostdlib && output_type != TCC_OUTPUT_OBJ)
999 tcc_add_systemdir(s);
1000 # endif
1001 #else
1002 /* paths for crt objects */
1003 tcc_split_path(s, &s->crt_paths, &s->nb_crt_paths, CONFIG_TCC_CRTPREFIX);
1004 /* add libc crt1/crti objects */
1005 if ((output_type == TCC_OUTPUT_EXE || output_type == TCC_OUTPUT_DLL) &&
1006 !s->nostdlib) {
1007 if (output_type != TCC_OUTPUT_DLL)
1008 tcc_add_crt(s, "crt1.o");
1009 tcc_add_crt(s, "crti.o");
1011 #endif
1012 return 0;
1015 LIBTCCAPI int tcc_add_include_path(TCCState *s, const char *pathname)
1017 tcc_split_path(s, &s->include_paths, &s->nb_include_paths, pathname);
1018 return 0;
1021 LIBTCCAPI int tcc_add_sysinclude_path(TCCState *s, const char *pathname)
1023 tcc_split_path(s, &s->sysinclude_paths, &s->nb_sysinclude_paths, pathname);
1024 return 0;
1027 ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
1029 int fd, ret;
1031 /* open the file */
1032 fd = _tcc_open(s1, filename);
1033 if (fd < 0) {
1034 if (flags & AFF_PRINT_ERROR)
1035 tcc_error_noabort("file '%s' not found", filename);
1036 return -1;
1039 /* update target deps */
1040 dynarray_add(&s1->target_deps, &s1->nb_target_deps,
1041 tcc_strdup(filename));
1043 if (flags & AFF_TYPE_BIN) {
1044 ElfW(Ehdr) ehdr;
1045 int obj_type;
1047 obj_type = tcc_object_type(fd, &ehdr);
1048 lseek(fd, 0, SEEK_SET);
1050 #ifdef TCC_TARGET_MACHO
1051 if (0 == obj_type && 0 == strcmp(tcc_fileextension(filename), ".dylib"))
1052 obj_type = AFF_BINTYPE_DYN;
1053 #endif
1055 switch (obj_type) {
1056 case AFF_BINTYPE_REL:
1057 ret = tcc_load_object_file(s1, fd, 0);
1058 break;
1059 #ifndef TCC_TARGET_PE
1060 case AFF_BINTYPE_DYN:
1061 if (s1->output_type == TCC_OUTPUT_MEMORY) {
1062 ret = 0;
1063 #ifdef TCC_IS_NATIVE
1064 if (NULL == dlopen(filename, RTLD_GLOBAL | RTLD_LAZY))
1065 ret = -1;
1066 #endif
1067 } else {
1068 ret = tcc_load_dll(s1, fd, filename,
1069 (flags & AFF_REFERENCED_DLL) != 0);
1071 break;
1072 #endif
1073 case AFF_BINTYPE_AR:
1074 ret = tcc_load_archive(s1, fd, !(flags & AFF_WHOLE_ARCHIVE));
1075 break;
1076 #ifdef TCC_TARGET_COFF
1077 case AFF_BINTYPE_C67:
1078 ret = tcc_load_coff(s1, fd);
1079 break;
1080 #endif
1081 default:
1082 #ifdef TCC_TARGET_PE
1083 ret = pe_load_file(s1, filename, fd);
1084 #else
1085 /* as GNU ld, consider it is an ld script if not recognized */
1086 ret = tcc_load_ldscript(s1, fd);
1087 #endif
1088 if (ret < 0)
1089 tcc_error_noabort("unrecognized file type");
1090 break;
1092 close(fd);
1093 } else {
1094 ret = tcc_compile(s1, flags, filename, fd);
1096 return ret;
1099 LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename)
1101 int filetype = s->filetype;
1102 if (0 == (filetype & AFF_TYPE_MASK)) {
1103 /* use a file extension to detect a filetype */
1104 const char *ext = tcc_fileextension(filename);
1105 if (ext[0]) {
1106 ext++;
1107 if (!strcmp(ext, "S"))
1108 filetype = AFF_TYPE_ASMPP;
1109 else if (!strcmp(ext, "s"))
1110 filetype = AFF_TYPE_ASM;
1111 else if (!PATHCMP(ext, "c") || !PATHCMP(ext, "i"))
1112 filetype = AFF_TYPE_C;
1113 else
1114 filetype |= AFF_TYPE_BIN;
1115 } else {
1116 filetype = AFF_TYPE_C;
1119 return tcc_add_file_internal(s, filename, filetype | AFF_PRINT_ERROR);
1122 LIBTCCAPI int tcc_add_library_path(TCCState *s, const char *pathname)
1124 tcc_split_path(s, &s->library_paths, &s->nb_library_paths, pathname);
1125 return 0;
1128 static int tcc_add_library_internal(TCCState *s, const char *fmt,
1129 const char *filename, int flags, char **paths, int nb_paths)
1131 char buf[1024];
1132 int i;
1134 for(i = 0; i < nb_paths; i++) {
1135 snprintf(buf, sizeof(buf), fmt, paths[i], filename);
1136 if (tcc_add_file_internal(s, buf, flags | AFF_TYPE_BIN) == 0)
1137 return 0;
1139 return -1;
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);
1150 #ifndef TCC_TARGET_PE
1151 ST_FUNC int tcc_add_crt(TCCState *s1, const char *filename)
1153 if (-1 == tcc_add_library_internal(s1, "%s/%s",
1154 filename, 0, s1->crt_paths, s1->nb_crt_paths))
1155 tcc_error_noabort("file '%s' not found", filename);
1156 return 0;
1158 #endif
1160 /* the library name is the same as the argument of the '-l' option */
1161 LIBTCCAPI int tcc_add_library(TCCState *s, const char *libraryname)
1163 #if defined TCC_TARGET_PE
1164 const char *libs[] = { "%s/%s.def", "%s/lib%s.def", "%s/%s.dll", "%s/lib%s.dll", "%s/lib%s.a", NULL };
1165 const char **pp = s->static_link ? libs + 4 : libs;
1166 #elif defined TCC_TARGET_MACHO
1167 const char *libs[] = { "%s/lib%s.dylib", "%s/lib%s.a", NULL };
1168 const char **pp = s->static_link ? libs + 1 : libs;
1169 #else
1170 const char *libs[] = { "%s/lib%s.so", "%s/lib%s.a", NULL };
1171 const char **pp = s->static_link ? libs + 1 : libs;
1172 #endif
1173 int flags = s->filetype & AFF_WHOLE_ARCHIVE;
1174 while (*pp) {
1175 if (0 == tcc_add_library_internal(s, *pp,
1176 libraryname, flags, s->library_paths, s->nb_library_paths))
1177 return 0;
1178 ++pp;
1180 return -1;
1183 PUB_FUNC int tcc_add_library_err(TCCState *s1, const char *libname)
1185 int ret = tcc_add_library(s1, libname);
1186 if (ret < 0)
1187 tcc_error_noabort("library '%s' not found", libname);
1188 return ret;
1191 /* handle #pragma comment(lib,) */
1192 ST_FUNC void tcc_add_pragma_libs(TCCState *s1)
1194 int i;
1195 for (i = 0; i < s1->nb_pragma_libs; i++)
1196 tcc_add_library_err(s1, s1->pragma_libs[i]);
1199 LIBTCCAPI int tcc_add_symbol(TCCState *s1, const char *name, const void *val)
1201 #ifdef TCC_TARGET_PE
1202 /* On x86_64 'val' might not be reachable with a 32bit offset.
1203 So it is handled here as if it were in a DLL. */
1204 pe_putimport(s1, 0, name, (uintptr_t)val);
1205 #else
1206 set_elf_sym(symtab_section, (uintptr_t)val, 0,
1207 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1208 SHN_ABS, name);
1209 #endif
1210 return 0;
1213 LIBTCCAPI void tcc_set_lib_path(TCCState *s, const char *path)
1215 tcc_free(s->tcc_lib_path);
1216 s->tcc_lib_path = tcc_strdup(path);
1219 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
1220 #define FD_INVERT 0x0002 /* invert value before storing */
1222 typedef struct FlagDef {
1223 uint16_t offset;
1224 uint16_t flags;
1225 const char *name;
1226 } FlagDef;
1228 static int no_flag(const char **pp)
1230 const char *p = *pp;
1231 if (*p != 'n' || *++p != 'o' || *++p != '-')
1232 return 0;
1233 *pp = p + 1;
1234 return 1;
1237 ST_FUNC int set_flag(TCCState *s, const FlagDef *flags, const char *name)
1239 int value, ret;
1240 const FlagDef *p;
1241 const char *r;
1243 value = 1;
1244 r = name;
1245 if (no_flag(&r))
1246 value = 0;
1248 for (ret = -1, p = flags; p->name; ++p) {
1249 if (ret) {
1250 if (strcmp(r, p->name))
1251 continue;
1252 } else {
1253 if (0 == (p->flags & WD_ALL))
1254 continue;
1256 if (p->offset) {
1257 *((unsigned char *)s + p->offset) =
1258 p->flags & FD_INVERT ? !value : value;
1259 if (ret)
1260 return 0;
1261 } else {
1262 ret = 0;
1265 return ret;
1268 static int strstart(const char *val, const char **str)
1270 const char *p, *q;
1271 p = *str;
1272 q = val;
1273 while (*q) {
1274 if (*p != *q)
1275 return 0;
1276 p++;
1277 q++;
1279 *str = p;
1280 return 1;
1283 /* Like strstart, but automatically takes into account that ld options can
1285 * - start with double or single dash (e.g. '--soname' or '-soname')
1286 * - arguments can be given as separate or after '=' (e.g. '-Wl,-soname,x.so'
1287 * or '-Wl,-soname=x.so')
1289 * you provide `val` always in 'option[=]' form (no leading -)
1291 static int link_option(const char *str, const char *val, const char **ptr)
1293 const char *p, *q;
1294 int ret;
1296 /* there should be 1 or 2 dashes */
1297 if (*str++ != '-')
1298 return 0;
1299 if (*str == '-')
1300 str++;
1302 /* then str & val should match (potentially up to '=') */
1303 p = str;
1304 q = val;
1306 ret = 1;
1307 if (q[0] == '?') {
1308 ++q;
1309 if (no_flag(&p))
1310 ret = -1;
1313 while (*q != '\0' && *q != '=') {
1314 if (*p != *q)
1315 return 0;
1316 p++;
1317 q++;
1320 /* '=' near eos means ',' or '=' is ok */
1321 if (*q == '=') {
1322 if (*p == 0)
1323 *ptr = p;
1324 if (*p != ',' && *p != '=')
1325 return 0;
1326 p++;
1327 } else if (*p) {
1328 return 0;
1330 *ptr = p;
1331 return ret;
1334 static const char *skip_linker_arg(const char **str)
1336 const char *s1 = *str;
1337 const char *s2 = strchr(s1, ',');
1338 *str = s2 ? s2++ : (s2 = s1 + strlen(s1));
1339 return s2;
1342 static void copy_linker_arg(char **pp, const char *s, int sep)
1344 const char *q = s;
1345 char *p = *pp;
1346 int l = 0;
1347 if (p && sep)
1348 p[l = strlen(p)] = sep, ++l;
1349 skip_linker_arg(&q);
1350 pstrncpy(l + (*pp = tcc_realloc(p, q - s + l + 1)), s, q - s);
1353 /* set linker options */
1354 static int tcc_set_linker(TCCState *s, const char *option)
1356 TCCState *s1 = s;
1357 while (*option) {
1359 const char *p = NULL;
1360 char *end = NULL;
1361 int ignoring = 0;
1362 int ret;
1364 if (link_option(option, "Bsymbolic", &p)) {
1365 s->symbolic = 1;
1366 } else if (link_option(option, "nostdlib", &p)) {
1367 s->nostdlib = 1;
1368 } else if (link_option(option, "fini=", &p)) {
1369 copy_linker_arg(&s->fini_symbol, p, 0);
1370 ignoring = 1;
1371 } else if (link_option(option, "image-base=", &p)
1372 || link_option(option, "Ttext=", &p)) {
1373 s->text_addr = strtoull(p, &end, 16);
1374 s->has_text_addr = 1;
1375 } else if (link_option(option, "init=", &p)) {
1376 copy_linker_arg(&s->init_symbol, p, 0);
1377 ignoring = 1;
1378 } else if (link_option(option, "oformat=", &p)) {
1379 #if defined(TCC_TARGET_PE)
1380 if (strstart("pe-", &p)) {
1381 #elif PTR_SIZE == 8
1382 if (strstart("elf64-", &p)) {
1383 #else
1384 if (strstart("elf32-", &p)) {
1385 #endif
1386 s->output_format = TCC_OUTPUT_FORMAT_ELF;
1387 } else if (!strcmp(p, "binary")) {
1388 s->output_format = TCC_OUTPUT_FORMAT_BINARY;
1389 #ifdef TCC_TARGET_COFF
1390 } else if (!strcmp(p, "coff")) {
1391 s->output_format = TCC_OUTPUT_FORMAT_COFF;
1392 #endif
1393 } else
1394 goto err;
1396 } else if (link_option(option, "as-needed", &p)) {
1397 ignoring = 1;
1398 } else if (link_option(option, "O", &p)) {
1399 ignoring = 1;
1400 } else if (link_option(option, "export-all-symbols", &p)) {
1401 s->rdynamic = 1;
1402 } else if (link_option(option, "export-dynamic", &p)) {
1403 s->rdynamic = 1;
1404 } else if (link_option(option, "rpath=", &p)) {
1405 copy_linker_arg(&s->rpath, p, ':');
1406 } else if (link_option(option, "enable-new-dtags", &p)) {
1407 s->enable_new_dtags = 1;
1408 } else if (link_option(option, "section-alignment=", &p)) {
1409 s->section_align = strtoul(p, &end, 16);
1410 } else if (link_option(option, "soname=", &p)) {
1411 copy_linker_arg(&s->soname, p, 0);
1412 #ifdef TCC_TARGET_PE
1413 } else if (link_option(option, "large-address-aware", &p)) {
1414 s->pe_characteristics |= 0x20;
1415 } else if (link_option(option, "file-alignment=", &p)) {
1416 s->pe_file_align = strtoul(p, &end, 16);
1417 } else if (link_option(option, "stack=", &p)) {
1418 s->pe_stack_size = strtoul(p, &end, 10);
1419 } else if (link_option(option, "subsystem=", &p)) {
1420 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1421 if (!strcmp(p, "native")) {
1422 s->pe_subsystem = 1;
1423 } else if (!strcmp(p, "console")) {
1424 s->pe_subsystem = 3;
1425 } else if (!strcmp(p, "gui") || !strcmp(p, "windows")) {
1426 s->pe_subsystem = 2;
1427 } else if (!strcmp(p, "posix")) {
1428 s->pe_subsystem = 7;
1429 } else if (!strcmp(p, "efiapp")) {
1430 s->pe_subsystem = 10;
1431 } else if (!strcmp(p, "efiboot")) {
1432 s->pe_subsystem = 11;
1433 } else if (!strcmp(p, "efiruntime")) {
1434 s->pe_subsystem = 12;
1435 } else if (!strcmp(p, "efirom")) {
1436 s->pe_subsystem = 13;
1437 #elif defined(TCC_TARGET_ARM)
1438 if (!strcmp(p, "wince")) {
1439 s->pe_subsystem = 9;
1440 #endif
1441 } else
1442 goto err;
1443 #endif
1444 } else if (ret = link_option(option, "?whole-archive", &p), ret) {
1445 if (ret > 0)
1446 s->filetype |= AFF_WHOLE_ARCHIVE;
1447 else
1448 s->filetype &= ~AFF_WHOLE_ARCHIVE;
1449 } else if (p) {
1450 return 0;
1451 } else {
1452 err:
1453 tcc_error("unsupported linker option '%s'", option);
1456 if (ignoring && s->warn_unsupported)
1457 tcc_warning("unsupported linker option '%s'", option);
1459 option = skip_linker_arg(&p);
1461 return 1;
1464 typedef struct TCCOption {
1465 const char *name;
1466 uint16_t index;
1467 uint16_t flags;
1468 } TCCOption;
1470 enum {
1471 TCC_OPTION_HELP,
1472 TCC_OPTION_HELP2,
1473 TCC_OPTION_v,
1474 TCC_OPTION_I,
1475 TCC_OPTION_D,
1476 TCC_OPTION_U,
1477 TCC_OPTION_P,
1478 TCC_OPTION_L,
1479 TCC_OPTION_B,
1480 TCC_OPTION_l,
1481 TCC_OPTION_bench,
1482 TCC_OPTION_bt,
1483 TCC_OPTION_b,
1484 TCC_OPTION_ba,
1485 TCC_OPTION_g,
1486 TCC_OPTION_c,
1487 TCC_OPTION_dumpversion,
1488 TCC_OPTION_d,
1489 TCC_OPTION_static,
1490 TCC_OPTION_std,
1491 TCC_OPTION_shared,
1492 TCC_OPTION_soname,
1493 TCC_OPTION_o,
1494 TCC_OPTION_r,
1495 TCC_OPTION_s,
1496 TCC_OPTION_traditional,
1497 TCC_OPTION_Wl,
1498 TCC_OPTION_Wp,
1499 TCC_OPTION_W,
1500 TCC_OPTION_O,
1501 TCC_OPTION_mfloat_abi,
1502 TCC_OPTION_m,
1503 TCC_OPTION_f,
1504 TCC_OPTION_isystem,
1505 TCC_OPTION_iwithprefix,
1506 TCC_OPTION_include,
1507 TCC_OPTION_nostdinc,
1508 TCC_OPTION_nostdlib,
1509 TCC_OPTION_print_search_dirs,
1510 TCC_OPTION_rdynamic,
1511 TCC_OPTION_param,
1512 TCC_OPTION_pedantic,
1513 TCC_OPTION_pthread,
1514 TCC_OPTION_run,
1515 TCC_OPTION_w,
1516 TCC_OPTION_pipe,
1517 TCC_OPTION_E,
1518 TCC_OPTION_MD,
1519 TCC_OPTION_MF,
1520 TCC_OPTION_x,
1521 TCC_OPTION_ar,
1522 TCC_OPTION_impdef
1525 #define TCC_OPTION_HAS_ARG 0x0001
1526 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
1528 static const TCCOption tcc_options[] = {
1529 { "h", TCC_OPTION_HELP, 0 },
1530 { "-help", TCC_OPTION_HELP, 0 },
1531 { "?", TCC_OPTION_HELP, 0 },
1532 { "hh", TCC_OPTION_HELP2, 0 },
1533 { "v", TCC_OPTION_v, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1534 { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
1535 { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
1536 { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
1537 { "P", TCC_OPTION_P, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1538 { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
1539 { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
1540 { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG },
1541 { "bench", TCC_OPTION_bench, 0 },
1542 #ifdef CONFIG_TCC_BACKTRACE
1543 { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG },
1544 #endif
1545 #ifdef CONFIG_TCC_BCHECK
1546 { "b", TCC_OPTION_b, 0 },
1547 #endif
1548 { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1549 { "c", TCC_OPTION_c, 0 },
1550 { "dumpversion", TCC_OPTION_dumpversion, 0},
1551 { "d", TCC_OPTION_d, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1552 { "static", TCC_OPTION_static, 0 },
1553 { "std", TCC_OPTION_std, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1554 { "shared", TCC_OPTION_shared, 0 },
1555 { "soname", TCC_OPTION_soname, TCC_OPTION_HAS_ARG },
1556 { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
1557 { "-param", TCC_OPTION_param, TCC_OPTION_HAS_ARG },
1558 { "pedantic", TCC_OPTION_pedantic, 0},
1559 { "pthread", TCC_OPTION_pthread, 0},
1560 { "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1561 { "rdynamic", TCC_OPTION_rdynamic, 0 },
1562 { "r", TCC_OPTION_r, 0 },
1563 { "s", TCC_OPTION_s, 0 },
1564 { "traditional", TCC_OPTION_traditional, 0 },
1565 { "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1566 { "Wp,", TCC_OPTION_Wp, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1567 { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1568 { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1569 #ifdef TCC_TARGET_ARM
1570 { "mfloat-abi", TCC_OPTION_mfloat_abi, TCC_OPTION_HAS_ARG },
1571 #endif
1572 { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1573 { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1574 { "isystem", TCC_OPTION_isystem, TCC_OPTION_HAS_ARG },
1575 { "include", TCC_OPTION_include, TCC_OPTION_HAS_ARG },
1576 { "nostdinc", TCC_OPTION_nostdinc, 0 },
1577 { "nostdlib", TCC_OPTION_nostdlib, 0 },
1578 { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
1579 { "w", TCC_OPTION_w, 0 },
1580 { "pipe", TCC_OPTION_pipe, 0},
1581 { "E", TCC_OPTION_E, 0},
1582 { "MD", TCC_OPTION_MD, 0},
1583 { "MF", TCC_OPTION_MF, TCC_OPTION_HAS_ARG },
1584 { "x", TCC_OPTION_x, TCC_OPTION_HAS_ARG },
1585 { "ar", TCC_OPTION_ar, 0},
1586 #ifdef TCC_TARGET_PE
1587 { "impdef", TCC_OPTION_impdef, 0},
1588 #endif
1589 { NULL, 0, 0 },
1592 static const FlagDef options_W[] = {
1593 { 0, 0, "all" },
1594 { offsetof(TCCState, warn_unsupported), 0, "unsupported" },
1595 { offsetof(TCCState, warn_write_strings), 0, "write-strings" },
1596 { offsetof(TCCState, warn_error), 0, "error" },
1597 { offsetof(TCCState, warn_gcc_compat), 0, "gcc-compat" },
1598 { offsetof(TCCState, warn_implicit_function_declaration), WD_ALL,
1599 "implicit-function-declaration" },
1600 { 0, 0, NULL }
1603 static const FlagDef options_f[] = {
1604 { offsetof(TCCState, char_is_unsigned), 0, "unsigned-char" },
1605 { offsetof(TCCState, char_is_unsigned), FD_INVERT, "signed-char" },
1606 { offsetof(TCCState, nocommon), FD_INVERT, "common" },
1607 { offsetof(TCCState, leading_underscore), 0, "leading-underscore" },
1608 { offsetof(TCCState, ms_extensions), 0, "ms-extensions" },
1609 { offsetof(TCCState, dollars_in_identifiers), 0, "dollars-in-identifiers" },
1610 { 0, 0, NULL }
1613 static const FlagDef options_m[] = {
1614 { offsetof(TCCState, ms_bitfields), 0, "ms-bitfields" },
1615 #ifdef TCC_TARGET_X86_64
1616 { offsetof(TCCState, nosse), FD_INVERT, "sse" },
1617 #endif
1618 { 0, 0, NULL }
1621 static void parse_option_D(TCCState *s1, const char *optarg)
1623 char *sym = tcc_strdup(optarg);
1624 char *value = strchr(sym, '=');
1625 if (value)
1626 *value++ = '\0';
1627 tcc_define_symbol(s1, sym, value);
1628 tcc_free(sym);
1631 static void args_parser_add_file(TCCState *s, const char* filename, int filetype)
1633 struct filespec *f = tcc_malloc(sizeof *f + strlen(filename));
1634 f->type = filetype;
1635 strcpy(f->name, filename);
1636 dynarray_add(&s->files, &s->nb_files, f);
1639 static int args_parser_make_argv(const char *r, int *argc, char ***argv)
1641 int ret = 0, q, c;
1642 CString str;
1643 for(;;) {
1644 while (c = (unsigned char)*r, c && c <= ' ')
1645 ++r;
1646 if (c == 0)
1647 break;
1648 q = 0;
1649 cstr_new(&str);
1650 while (c = (unsigned char)*r, c) {
1651 ++r;
1652 if (c == '\\' && (*r == '"' || *r == '\\')) {
1653 c = *r++;
1654 } else if (c == '"') {
1655 q = !q;
1656 continue;
1657 } else if (q == 0 && c <= ' ') {
1658 break;
1660 cstr_ccat(&str, c);
1662 cstr_ccat(&str, 0);
1663 //printf("<%s>\n", str.data), fflush(stdout);
1664 dynarray_add(argv, argc, tcc_strdup(str.data));
1665 cstr_free(&str);
1666 ++ret;
1668 return ret;
1671 /* read list file */
1672 static void args_parser_listfile(TCCState *s,
1673 const char *filename, int optind, int *pargc, char ***pargv)
1675 TCCState *s1 = s;
1676 int fd, i;
1677 size_t len;
1678 char *p;
1679 int argc = 0;
1680 char **argv = NULL;
1682 fd = open(filename, O_RDONLY | O_BINARY);
1683 if (fd < 0)
1684 tcc_error("listfile '%s' not found", filename);
1686 len = lseek(fd, 0, SEEK_END);
1687 p = tcc_malloc(len + 1), p[len] = 0;
1688 lseek(fd, 0, SEEK_SET), read(fd, p, len), close(fd);
1690 for (i = 0; i < *pargc; ++i)
1691 if (i == optind)
1692 args_parser_make_argv(p, &argc, &argv);
1693 else
1694 dynarray_add(&argv, &argc, tcc_strdup((*pargv)[i]));
1696 tcc_free(p);
1697 dynarray_reset(&s->argv, &s->argc);
1698 *pargc = s->argc = argc, *pargv = s->argv = argv;
1701 PUB_FUNC int tcc_parse_args(TCCState *s, int *pargc, char ***pargv, int optind)
1703 TCCState *s1 = s;
1704 const TCCOption *popt;
1705 const char *optarg, *r;
1706 const char *run = NULL;
1707 int last_o = -1;
1708 int x;
1709 CString linker_arg; /* collect -Wl options */
1710 int tool = 0, arg_start = 0, noaction = optind;
1711 char **argv = *pargv;
1712 int argc = *pargc;
1714 cstr_new(&linker_arg);
1716 while (optind < argc) {
1717 r = argv[optind];
1718 if (r[0] == '@' && r[1] != '\0') {
1719 args_parser_listfile(s, r + 1, optind, &argc, &argv);
1720 continue;
1722 optind++;
1723 if (tool) {
1724 if (r[0] == '-' && r[1] == 'v' && r[2] == 0)
1725 ++s->verbose;
1726 continue;
1728 reparse:
1729 if (r[0] != '-' || r[1] == '\0') {
1730 if (r[0] != '@') /* allow "tcc file(s) -run @ args ..." */
1731 args_parser_add_file(s, r, s->filetype);
1732 if (run) {
1733 tcc_set_options(s, run);
1734 arg_start = optind - 1;
1735 break;
1737 continue;
1740 /* find option in table */
1741 for(popt = tcc_options; ; ++popt) {
1742 const char *p1 = popt->name;
1743 const char *r1 = r + 1;
1744 if (p1 == NULL)
1745 tcc_error("invalid option -- '%s'", r);
1746 if (!strstart(p1, &r1))
1747 continue;
1748 optarg = r1;
1749 if (popt->flags & TCC_OPTION_HAS_ARG) {
1750 if (*r1 == '\0' && !(popt->flags & TCC_OPTION_NOSEP)) {
1751 if (optind >= argc)
1752 arg_err:
1753 tcc_error("argument to '%s' is missing", r);
1754 optarg = argv[optind++];
1756 } else if (*r1 != '\0')
1757 continue;
1758 break;
1761 switch(popt->index) {
1762 case TCC_OPTION_HELP:
1763 return OPT_HELP;
1764 case TCC_OPTION_HELP2:
1765 return OPT_HELP2;
1766 case TCC_OPTION_I:
1767 tcc_add_include_path(s, optarg);
1768 break;
1769 case TCC_OPTION_D:
1770 parse_option_D(s, optarg);
1771 break;
1772 case TCC_OPTION_U:
1773 tcc_undefine_symbol(s, optarg);
1774 break;
1775 case TCC_OPTION_L:
1776 tcc_add_library_path(s, optarg);
1777 break;
1778 case TCC_OPTION_B:
1779 /* set tcc utilities path (mainly for tcc development) */
1780 tcc_set_lib_path(s, optarg);
1781 break;
1782 case TCC_OPTION_l:
1783 args_parser_add_file(s, optarg, AFF_TYPE_LIB | (s->filetype & ~AFF_TYPE_MASK));
1784 s->nb_libraries++;
1785 break;
1786 case TCC_OPTION_pthread:
1787 parse_option_D(s, "_REENTRANT");
1788 s->option_pthread = 1;
1789 break;
1790 case TCC_OPTION_bench:
1791 s->do_bench = 1;
1792 break;
1793 #ifdef CONFIG_TCC_BACKTRACE
1794 case TCC_OPTION_bt:
1795 s->rt_num_callers = atoi(optarg);
1796 break;
1797 #endif
1798 #ifdef CONFIG_TCC_BCHECK
1799 case TCC_OPTION_b:
1800 s->do_bounds_check = 1;
1801 s->do_debug = 1;
1802 break;
1803 #endif
1804 case TCC_OPTION_g:
1805 s->do_debug = 1;
1806 break;
1807 case TCC_OPTION_c:
1808 x = TCC_OUTPUT_OBJ;
1809 set_output_type:
1810 if (s->output_type)
1811 tcc_warning("-%s: overriding compiler action already specified", popt->name);
1812 s->output_type = x;
1813 break;
1814 case TCC_OPTION_d:
1815 if (*optarg == 'D')
1816 s->dflag = 3;
1817 else if (*optarg == 'M')
1818 s->dflag = 7;
1819 else if (*optarg == 't')
1820 s->dflag = 16;
1821 else if (isnum(*optarg))
1822 s->g_debug |= atoi(optarg);
1823 else
1824 goto unsupported_option;
1825 break;
1826 case TCC_OPTION_static:
1827 s->static_link = 1;
1828 break;
1829 case TCC_OPTION_std:
1830 if (*optarg == '=') {
1831 if (strcmp(optarg, "=c11") == 0) {
1832 tcc_undefine_symbol(s, "__STDC_VERSION__");
1833 tcc_define_symbol(s, "__STDC_VERSION__", "201112L");
1835 * The integer constant 1, intended to indicate
1836 * that the implementation does not support atomic
1837 * types (including the _Atomic type qualifier) and
1838 * the <stdatomic.h> header.
1840 tcc_define_symbol(s, "__STDC_NO_ATOMICS__", "1");
1842 * The integer constant 1, intended to indicate
1843 * that the implementation does not support complex
1844 * types or the <complex.h> header.
1846 tcc_define_symbol(s, "__STDC_NO_COMPLEX__", "1");
1848 * The integer constant 1, intended to indicate
1849 * that the implementation does not support the
1850 * <threads.h> header.
1852 tcc_define_symbol(s, "__STDC_NO_THREADS__", "1");
1854 * __STDC_NO_VLA__, tcc supports VLA.
1855 * The integer constant 1, intended to indicate
1856 * that the implementation does not support
1857 * variable length arrays or variably modified
1858 * types.
1860 #if !defined(TCC_TARGET_PE)
1862 * An integer constant of the form yyyymmL (for
1863 * example, 199712L). If this symbol is defined,
1864 * then every character in the Unicode required
1865 * set, when stored in an object of type
1866 * wchar_t, has the same value as the short
1867 * identifier of that character.
1869 #if 0
1870 /* on Linux, this conflicts with a define introduced by
1871 * /usr/include/stdc-predef.h included by glibc libs;
1872 * clang doesn't define it at all so it's probably not necessary
1874 tcc_define_symbol(s, "__STDC_ISO_10646__", "201605L");
1875 #endif
1877 * The integer constant 1, intended to indicate
1878 * that values of type char16_t are UTF−16
1879 * encoded. If some other encoding is used, the
1880 * macro shall not be defined and the actual
1881 * encoding used is implementation defined.
1883 tcc_define_symbol(s, "__STDC_UTF_16__", "1");
1885 * The integer constant 1, intended to indicate
1886 * that values of type char32_t are UTF−32
1887 * encoded. If some other encoding is used, the
1888 * macro shall not be defined and the actual
1889 * encoding used is implementationdefined.
1891 tcc_define_symbol(s, "__STDC_UTF_32__", "1");
1892 #endif /* !TCC_TARGET_PE */
1893 s->cversion = 201112;
1897 * silently ignore other values, a current purpose:
1898 * allow to use a tcc as a reference compiler for "make test"
1900 break;
1901 case TCC_OPTION_shared:
1902 x = TCC_OUTPUT_DLL;
1903 goto set_output_type;
1904 case TCC_OPTION_soname:
1905 s->soname = tcc_strdup(optarg);
1906 break;
1907 case TCC_OPTION_o:
1908 if (s->outfile) {
1909 tcc_warning("multiple -o option");
1910 tcc_free(s->outfile);
1912 s->outfile = tcc_strdup(optarg);
1913 break;
1914 case TCC_OPTION_r:
1915 /* generate a .o merging several output files */
1916 s->option_r = 1;
1917 x = TCC_OUTPUT_OBJ;
1918 goto set_output_type;
1919 case TCC_OPTION_isystem:
1920 tcc_add_sysinclude_path(s, optarg);
1921 break;
1922 case TCC_OPTION_include:
1923 cstr_printf(&s->cmdline_incl, "#include \"%s\"\n", optarg);
1924 break;
1925 case TCC_OPTION_nostdinc:
1926 s->nostdinc = 1;
1927 break;
1928 case TCC_OPTION_nostdlib:
1929 s->nostdlib = 1;
1930 break;
1931 case TCC_OPTION_run:
1932 #ifndef TCC_IS_NATIVE
1933 tcc_error("-run is not available in a cross compiler");
1934 #endif
1935 run = optarg;
1936 x = TCC_OUTPUT_MEMORY;
1937 goto set_output_type;
1938 case TCC_OPTION_v:
1939 do ++s->verbose; while (*optarg++ == 'v');
1940 ++noaction;
1941 break;
1942 case TCC_OPTION_f:
1943 if (set_flag(s, options_f, optarg) < 0)
1944 goto unsupported_option;
1945 break;
1946 #ifdef TCC_TARGET_ARM
1947 case TCC_OPTION_mfloat_abi:
1948 /* tcc doesn't support soft float yet */
1949 if (!strcmp(optarg, "softfp")) {
1950 s->float_abi = ARM_SOFTFP_FLOAT;
1951 tcc_undefine_symbol(s, "__ARM_PCS_VFP");
1952 } else if (!strcmp(optarg, "hard"))
1953 s->float_abi = ARM_HARD_FLOAT;
1954 else
1955 tcc_error("unsupported float abi '%s'", optarg);
1956 break;
1957 #endif
1958 case TCC_OPTION_m:
1959 if (set_flag(s, options_m, optarg) < 0) {
1960 if (x = atoi(optarg), x != 32 && x != 64)
1961 goto unsupported_option;
1962 if (PTR_SIZE != x/8)
1963 return x;
1964 ++noaction;
1966 break;
1967 case TCC_OPTION_W:
1968 s->warn_none = 0;
1969 if (optarg[0] && set_flag(s, options_W, optarg) < 0)
1970 goto unsupported_option;
1971 break;
1972 case TCC_OPTION_w:
1973 s->warn_none = 1;
1974 break;
1975 case TCC_OPTION_rdynamic:
1976 s->rdynamic = 1;
1977 break;
1978 case TCC_OPTION_Wl:
1979 if (linker_arg.size)
1980 --linker_arg.size, cstr_ccat(&linker_arg, ',');
1981 cstr_cat(&linker_arg, optarg, 0);
1982 if (tcc_set_linker(s, linker_arg.data))
1983 cstr_free(&linker_arg);
1984 break;
1985 case TCC_OPTION_Wp:
1986 r = optarg;
1987 goto reparse;
1988 case TCC_OPTION_E:
1989 x = TCC_OUTPUT_PREPROCESS;
1990 goto set_output_type;
1991 case TCC_OPTION_P:
1992 s->Pflag = atoi(optarg) + 1;
1993 break;
1994 case TCC_OPTION_MD:
1995 s->gen_deps = 1;
1996 break;
1997 case TCC_OPTION_MF:
1998 s->deps_outfile = tcc_strdup(optarg);
1999 break;
2000 case TCC_OPTION_dumpversion:
2001 printf ("%s\n", TCC_VERSION);
2002 exit(0);
2003 break;
2004 case TCC_OPTION_x:
2005 x = 0;
2006 if (*optarg == 'c')
2007 x = AFF_TYPE_C;
2008 else if (*optarg == 'a')
2009 x = AFF_TYPE_ASMPP;
2010 else if (*optarg == 'b')
2011 x = AFF_TYPE_BIN;
2012 else if (*optarg == 'n')
2013 x = AFF_TYPE_NONE;
2014 else
2015 tcc_warning("unsupported language '%s'", optarg);
2016 s->filetype = x | (s->filetype & ~AFF_TYPE_MASK);
2017 break;
2018 case TCC_OPTION_O:
2019 last_o = atoi(optarg);
2020 break;
2021 case TCC_OPTION_print_search_dirs:
2022 x = OPT_PRINT_DIRS;
2023 goto extra_action;
2024 case TCC_OPTION_impdef:
2025 x = OPT_IMPDEF;
2026 goto extra_action;
2027 case TCC_OPTION_ar:
2028 x = OPT_AR;
2029 extra_action:
2030 arg_start = optind - 1;
2031 if (arg_start != noaction)
2032 tcc_error("cannot parse %s here", r);
2033 tool = x;
2034 break;
2035 case TCC_OPTION_traditional:
2036 case TCC_OPTION_pedantic:
2037 case TCC_OPTION_pipe:
2038 case TCC_OPTION_s:
2039 /* ignored */
2040 break;
2041 default:
2042 unsupported_option:
2043 if (s->warn_unsupported)
2044 tcc_warning("unsupported option '%s'", r);
2045 break;
2048 if (last_o > 0)
2049 tcc_define_symbol(s, "__OPTIMIZE__", NULL);
2050 if (linker_arg.size) {
2051 r = linker_arg.data;
2052 goto arg_err;
2054 *pargc = argc - arg_start;
2055 *pargv = argv + arg_start;
2056 if (tool)
2057 return tool;
2058 if (optind != noaction)
2059 return 0;
2060 if (s->verbose == 2)
2061 return OPT_PRINT_DIRS;
2062 if (s->verbose)
2063 return OPT_V;
2064 return OPT_HELP;
2067 LIBTCCAPI void tcc_set_options(TCCState *s, const char *r)
2069 char **argv = NULL;
2070 int argc = 0;
2071 args_parser_make_argv(r, &argc, &argv);
2072 tcc_parse_args(s, &argc, &argv, 0);
2073 dynarray_reset(&argv, &argc);
2076 PUB_FUNC void tcc_print_stats(TCCState *s1, unsigned total_time)
2078 if (total_time < 1)
2079 total_time = 1;
2080 if (total_bytes < 1)
2081 total_bytes = 1;
2082 fprintf(stderr, "* %d idents, %d lines, %d bytes\n"
2083 "* %0.3f s, %u lines/s, %0.1f MB/s\n",
2084 total_idents, total_lines, total_bytes,
2085 (double)total_time/1000,
2086 (unsigned)total_lines*1000/total_time,
2087 (double)total_bytes/1000/total_time);
2088 #ifdef MEM_DEBUG
2089 fprintf(stderr, "* %d bytes memory used\n", mem_max_size);
2090 #endif