Fix some races
[tinycc.git] / libtcc.c
blobe1bfe028c011014585183ff600a710e452ec7396
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 static int nb_states;
69 /********************************************************/
70 #ifdef _WIN32
71 ST_FUNC char *normalize_slashes(char *path)
73 char *p;
74 for (p = path; *p; ++p)
75 if (*p == '\\')
76 *p = '/';
77 return path;
80 static HMODULE tcc_module;
82 /* on win32, we suppose the lib and includes are at the location of 'tcc.exe' */
83 static void tcc_set_lib_path_w32(TCCState *s)
85 char path[1024], *p;
86 GetModuleFileNameA(tcc_module, path, sizeof path);
87 p = tcc_basename(normalize_slashes(strlwr(path)));
88 if (p > path)
89 --p;
90 *p = 0;
91 tcc_set_lib_path(s, path);
94 #ifdef TCC_TARGET_PE
95 static void tcc_add_systemdir(TCCState *s)
97 char buf[1000];
98 GetSystemDirectory(buf, sizeof buf);
99 tcc_add_library_path(s, normalize_slashes(buf));
101 #endif
103 #ifdef LIBTCC_AS_DLL
104 BOOL WINAPI DllMain (HINSTANCE hDll, DWORD dwReason, LPVOID lpReserved)
106 if (DLL_PROCESS_ATTACH == dwReason)
107 tcc_module = hDll;
108 return TRUE;
110 #endif
111 #endif
113 /********************************************************/
114 #ifndef CONFIG_TCC_SEMLOCK
115 #define WAIT_SEM()
116 #define POST_SEM()
117 #elif defined _WIN32
118 static int tcc_sem_init;
119 static CRITICAL_SECTION tcc_cr;
120 static void wait_sem(void)
122 if (!tcc_sem_init)
123 InitializeCriticalSection(&tcc_cr), tcc_sem_init = 1;
124 EnterCriticalSection(&tcc_cr);
126 #define WAIT_SEM() wait_sem()
127 #define POST_SEM() LeaveCriticalSection(&tcc_cr);
128 #else
129 #include <semaphore.h>
130 static int tcc_sem_init;
131 static sem_t tcc_sem;
132 static void wait_sem(void)
134 if (!tcc_sem_init)
135 sem_init(&tcc_sem, 0, 1), tcc_sem_init = 1;
136 while (sem_wait (&tcc_sem) < 0 && errno == EINTR);
138 #define WAIT_SEM() wait_sem()
139 #define POST_SEM() sem_post(&tcc_sem)
140 #endif
142 /********************************************************/
143 /* copy a string and truncate it. */
144 ST_FUNC char *pstrcpy(char *buf, size_t buf_size, const char *s)
146 char *q, *q_end;
147 int c;
149 if (buf_size > 0) {
150 q = buf;
151 q_end = buf + buf_size - 1;
152 while (q < q_end) {
153 c = *s++;
154 if (c == '\0')
155 break;
156 *q++ = c;
158 *q = '\0';
160 return buf;
163 /* strcat and truncate. */
164 ST_FUNC char *pstrcat(char *buf, size_t buf_size, const char *s)
166 size_t len;
167 len = strlen(buf);
168 if (len < buf_size)
169 pstrcpy(buf + len, buf_size - len, s);
170 return buf;
173 ST_FUNC char *pstrncpy(char *out, const char *in, size_t num)
175 memcpy(out, in, num);
176 out[num] = '\0';
177 return out;
180 /* extract the basename of a file */
181 PUB_FUNC char *tcc_basename(const char *name)
183 char *p = strchr(name, 0);
184 while (p > name && !IS_DIRSEP(p[-1]))
185 --p;
186 return p;
189 /* extract extension part of a file
191 * (if no extension, return pointer to end-of-string)
193 PUB_FUNC char *tcc_fileextension (const char *name)
195 char *b = tcc_basename(name);
196 char *e = strrchr(b, '.');
197 return e ? e : strchr(b, 0);
200 /********************************************************/
201 /* memory management */
203 #undef free
204 #undef malloc
205 #undef realloc
207 #ifndef MEM_DEBUG
209 PUB_FUNC void tcc_free(void *ptr)
211 free(ptr);
214 PUB_FUNC void *tcc_malloc(unsigned long size)
216 void *ptr;
217 ptr = malloc(size);
218 if (!ptr && size)
219 _tcc_error("memory full (malloc)");
220 return ptr;
223 PUB_FUNC void *tcc_mallocz(unsigned long size)
225 void *ptr;
226 ptr = tcc_malloc(size);
227 memset(ptr, 0, size);
228 return ptr;
231 PUB_FUNC void *tcc_realloc(void *ptr, unsigned long size)
233 void *ptr1;
234 ptr1 = realloc(ptr, size);
235 if (!ptr1 && size)
236 _tcc_error("memory full (realloc)");
237 return ptr1;
240 PUB_FUNC char *tcc_strdup(const char *str)
242 char *ptr;
243 ptr = tcc_malloc(strlen(str) + 1);
244 strcpy(ptr, str);
245 return ptr;
248 PUB_FUNC void tcc_memcheck(void)
252 #else
254 #define MEM_DEBUG_MAGIC1 0xFEEDDEB1
255 #define MEM_DEBUG_MAGIC2 0xFEEDDEB2
256 #define MEM_DEBUG_MAGIC3 0xFEEDDEB3
257 #define MEM_DEBUG_FILE_LEN 40
258 #define MEM_DEBUG_CHECK3(header) \
259 ((mem_debug_header_t*)((char*)header + header->size))->magic3
260 #define MEM_USER_PTR(header) \
261 ((char *)header + offsetof(mem_debug_header_t, magic3))
262 #define MEM_HEADER_PTR(ptr) \
263 (mem_debug_header_t *)((char*)ptr - offsetof(mem_debug_header_t, magic3))
265 struct mem_debug_header {
266 unsigned magic1;
267 unsigned size;
268 struct mem_debug_header *prev;
269 struct mem_debug_header *next;
270 int line_num;
271 char file_name[MEM_DEBUG_FILE_LEN + 1];
272 unsigned magic2;
273 ALIGNED(16) unsigned magic3;
276 typedef struct mem_debug_header mem_debug_header_t;
278 static mem_debug_header_t *mem_debug_chain;
279 static unsigned mem_cur_size;
280 static unsigned mem_max_size;
282 static mem_debug_header_t *malloc_check(void *ptr, const char *msg)
284 mem_debug_header_t * header = MEM_HEADER_PTR(ptr);
285 if (header->magic1 != MEM_DEBUG_MAGIC1 ||
286 header->magic2 != MEM_DEBUG_MAGIC2 ||
287 MEM_DEBUG_CHECK3(header) != MEM_DEBUG_MAGIC3 ||
288 header->size == (unsigned)-1) {
289 fprintf(stderr, "%s check failed\n", msg);
290 if (header->magic1 == MEM_DEBUG_MAGIC1)
291 fprintf(stderr, "%s:%u: block allocated here.\n",
292 header->file_name, header->line_num);
293 exit(1);
295 return header;
298 PUB_FUNC void *tcc_malloc_debug(unsigned long size, const char *file, int line)
300 int ofs;
301 mem_debug_header_t *header;
303 header = malloc(sizeof(mem_debug_header_t) + size);
304 if (!header)
305 _tcc_error("memory full (malloc)");
307 header->magic1 = MEM_DEBUG_MAGIC1;
308 header->magic2 = MEM_DEBUG_MAGIC2;
309 header->size = size;
310 MEM_DEBUG_CHECK3(header) = MEM_DEBUG_MAGIC3;
311 header->line_num = line;
312 ofs = strlen(file) - MEM_DEBUG_FILE_LEN;
313 strncpy(header->file_name, file + (ofs > 0 ? ofs : 0), MEM_DEBUG_FILE_LEN);
314 header->file_name[MEM_DEBUG_FILE_LEN] = 0;
316 header->next = mem_debug_chain;
317 header->prev = NULL;
318 if (header->next)
319 header->next->prev = header;
320 mem_debug_chain = header;
322 mem_cur_size += size;
323 if (mem_cur_size > mem_max_size)
324 mem_max_size = mem_cur_size;
326 return MEM_USER_PTR(header);
329 PUB_FUNC void tcc_free_debug(void *ptr)
331 mem_debug_header_t *header;
332 if (!ptr)
333 return;
334 header = malloc_check(ptr, "tcc_free");
335 mem_cur_size -= header->size;
336 header->size = (unsigned)-1;
337 if (header->next)
338 header->next->prev = header->prev;
339 if (header->prev)
340 header->prev->next = header->next;
341 if (header == mem_debug_chain)
342 mem_debug_chain = header->next;
343 free(header);
346 PUB_FUNC void *tcc_mallocz_debug(unsigned long size, const char *file, int line)
348 void *ptr;
349 ptr = tcc_malloc_debug(size,file,line);
350 memset(ptr, 0, size);
351 return ptr;
354 PUB_FUNC void *tcc_realloc_debug(void *ptr, unsigned long size, const char *file, int line)
356 mem_debug_header_t *header;
357 int mem_debug_chain_update = 0;
358 if (!ptr)
359 return tcc_malloc_debug(size, file, line);
360 header = malloc_check(ptr, "tcc_realloc");
361 mem_cur_size -= header->size;
362 mem_debug_chain_update = (header == mem_debug_chain);
363 header = realloc(header, sizeof(mem_debug_header_t) + size);
364 if (!header)
365 _tcc_error("memory full (realloc)");
366 header->size = size;
367 MEM_DEBUG_CHECK3(header) = MEM_DEBUG_MAGIC3;
368 if (header->next)
369 header->next->prev = header;
370 if (header->prev)
371 header->prev->next = header;
372 if (mem_debug_chain_update)
373 mem_debug_chain = header;
374 mem_cur_size += size;
375 if (mem_cur_size > mem_max_size)
376 mem_max_size = mem_cur_size;
377 return MEM_USER_PTR(header);
380 PUB_FUNC char *tcc_strdup_debug(const char *str, const char *file, int line)
382 char *ptr;
383 ptr = tcc_malloc_debug(strlen(str) + 1, file, line);
384 strcpy(ptr, str);
385 return ptr;
388 PUB_FUNC void tcc_memcheck(void)
390 if (mem_cur_size) {
391 mem_debug_header_t *header = mem_debug_chain;
392 fprintf(stderr, "MEM_DEBUG: mem_leak= %d bytes, mem_max_size= %d bytes\n",
393 mem_cur_size, mem_max_size);
394 while (header) {
395 fprintf(stderr, "%s:%u: error: %u bytes leaked\n",
396 header->file_name, header->line_num, header->size);
397 header = header->next;
399 #if MEM_DEBUG-0 == 2
400 exit(2);
401 #endif
404 #endif /* MEM_DEBUG */
406 #define free(p) use_tcc_free(p)
407 #define malloc(s) use_tcc_malloc(s)
408 #define realloc(p, s) use_tcc_realloc(p, s)
410 /********************************************************/
411 /* dynarrays */
413 ST_FUNC void dynarray_add(void *ptab, int *nb_ptr, void *data)
415 int nb, nb_alloc;
416 void **pp;
418 nb = *nb_ptr;
419 pp = *(void ***)ptab;
420 /* every power of two we double array size */
421 if ((nb & (nb - 1)) == 0) {
422 if (!nb)
423 nb_alloc = 1;
424 else
425 nb_alloc = nb * 2;
426 pp = tcc_realloc(pp, nb_alloc * sizeof(void *));
427 *(void***)ptab = pp;
429 pp[nb++] = data;
430 *nb_ptr = nb;
433 ST_FUNC void dynarray_reset(void *pp, int *n)
435 void **p;
436 for (p = *(void***)pp; *n; ++p, --*n)
437 if (*p)
438 tcc_free(*p);
439 tcc_free(*(void**)pp);
440 *(void**)pp = NULL;
443 static void tcc_split_path(TCCState *s, void *p_ary, int *p_nb_ary, const char *in)
445 const char *p;
446 do {
447 int c;
448 CString str;
450 cstr_new(&str);
451 for (p = in; c = *p, c != '\0' && c != PATHSEP[0]; ++p) {
452 if (c == '{' && p[1] && p[2] == '}') {
453 c = p[1], p += 2;
454 if (c == 'B')
455 cstr_cat(&str, s->tcc_lib_path, -1);
456 } else {
457 cstr_ccat(&str, c);
460 if (str.size) {
461 cstr_ccat(&str, '\0');
462 dynarray_add(p_ary, p_nb_ary, tcc_strdup(str.data));
464 cstr_free(&str);
465 in = p+1;
466 } while (*p);
469 /********************************************************/
471 static void strcat_vprintf(char *buf, int buf_size, const char *fmt, va_list ap)
473 int len;
474 len = strlen(buf);
475 vsnprintf(buf + len, buf_size - len, fmt, ap);
478 static void strcat_printf(char *buf, int buf_size, const char *fmt, ...)
480 va_list ap;
481 va_start(ap, fmt);
482 strcat_vprintf(buf, buf_size, fmt, ap);
483 va_end(ap);
486 #define ERROR_WARN 0
487 #define ERROR_NOABORT 1
488 #define ERROR_ERROR 2
490 PUB_FUNC void tcc_enter_state(TCCState *s1)
492 WAIT_SEM();
493 tcc_state = s1;
496 static void error1(int mode, const char *fmt, va_list ap)
498 char buf[2048];
499 BufferedFile **pf, *f;
500 TCCState *s1 = tcc_state;
502 /* 's1->error_set_jmp_enabled' means that we're called from
503 within the parser/generator and 'tcc_state' was already
504 set (i.e. not by the function above).
506 Otherwise, 's1 = NULL' means we're called because of severe
507 problems from tcc_malloc() which under normal conditions
508 should never happen. */
510 if (s1 && !s1->error_set_jmp_enabled) {
511 tcc_state = NULL;
512 POST_SEM();
515 if (mode == ERROR_WARN) {
516 if (s1->warn_none)
517 return;
518 if (s1->warn_error)
519 mode = ERROR_ERROR;
522 buf[0] = '\0';
523 /* use upper file if inline ":asm:" or token ":paste:" */
524 for (f = file; f && f->filename[0] == ':'; f = f->prev)
526 if (f) {
527 for(pf = s1->include_stack; pf < s1->include_stack_ptr; pf++)
528 strcat_printf(buf, sizeof(buf), "In file included from %s:%d:\n",
529 (*pf)->filename, (*pf)->line_num);
530 if (s1->error_set_jmp_enabled) {
531 strcat_printf(buf, sizeof(buf), "%s:%d: ",
532 f->filename, f->line_num - !!(tok_flags & TOK_FLAG_BOL));
533 } else {
534 strcat_printf(buf, sizeof(buf), "%s: ",
535 f->filename);
537 } else {
538 strcat_printf(buf, sizeof(buf), "tcc: ");
540 if (mode == ERROR_WARN)
541 strcat_printf(buf, sizeof(buf), "warning: ");
542 else
543 strcat_printf(buf, sizeof(buf), "error: ");
544 strcat_vprintf(buf, sizeof(buf), fmt, ap);
545 if (!s1 || !s1->error_func) {
546 /* default case: stderr */
547 if (s1 && s1->output_type == TCC_OUTPUT_PREPROCESS && s1->ppfp == stdout)
548 /* print a newline during tcc -E */
549 printf("\n"), fflush(stdout);
550 fflush(stdout); /* flush -v output */
551 fprintf(stderr, "%s\n", buf);
552 fflush(stderr); /* print error/warning now (win32) */
553 } else {
554 s1->error_func(s1->error_opaque, buf);
556 if (s1) {
557 if (mode != ERROR_WARN)
558 s1->nb_errors++;
559 if (mode != ERROR_ERROR)
560 return;
561 if (s1->error_set_jmp_enabled)
562 longjmp(s1->error_jmp_buf, 1);
564 exit(1);
567 LIBTCCAPI void tcc_set_error_func(TCCState *s, void *error_opaque, TCCErrorFunc error_func)
569 s->error_opaque = error_opaque;
570 s->error_func = error_func;
573 LIBTCCAPI TCCErrorFunc tcc_get_error_func(TCCState *s)
575 return s->error_func;
578 LIBTCCAPI void *tcc_get_error_opaque(TCCState *s)
580 return s->error_opaque;
583 /* error without aborting current compilation */
584 PUB_FUNC void _tcc_error_noabort(const char *fmt, ...)
586 va_list ap;
587 va_start(ap, fmt);
588 error1(ERROR_NOABORT, fmt, ap);
589 va_end(ap);
592 PUB_FUNC void _tcc_error(const char *fmt, ...)
594 va_list ap;
595 va_start(ap, fmt);
596 for (;;) error1(ERROR_ERROR, fmt, ap);
599 PUB_FUNC void _tcc_warning(const char *fmt, ...)
601 va_list ap;
602 va_start(ap, fmt);
603 error1(ERROR_WARN, fmt, ap);
604 va_end(ap);
607 /********************************************************/
608 /* I/O layer */
610 ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen)
612 BufferedFile *bf;
613 int buflen = initlen ? initlen : IO_BUF_SIZE;
615 bf = tcc_mallocz(sizeof(BufferedFile) + buflen);
616 bf->buf_ptr = bf->buffer;
617 bf->buf_end = bf->buffer + initlen;
618 bf->buf_end[0] = CH_EOB; /* put eob symbol */
619 pstrcpy(bf->filename, sizeof(bf->filename), filename);
620 #ifdef _WIN32
621 normalize_slashes(bf->filename);
622 #endif
623 bf->true_filename = bf->filename;
624 bf->line_num = 1;
625 bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
626 bf->fd = -1;
627 bf->prev = file;
628 file = bf;
629 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
632 ST_FUNC void tcc_close(void)
634 BufferedFile *bf = file;
635 if (bf->fd > 0) {
636 close(bf->fd);
637 total_lines += bf->line_num;
639 if (bf->true_filename != bf->filename)
640 tcc_free(bf->true_filename);
641 file = bf->prev;
642 tcc_free(bf);
645 static int _tcc_open(TCCState *s1, const char *filename)
647 int fd;
648 if (strcmp(filename, "-") == 0)
649 fd = 0, filename = "<stdin>";
650 else
651 fd = open(filename, O_RDONLY | O_BINARY);
652 if ((s1->verbose == 2 && fd >= 0) || s1->verbose == 3)
653 printf("%s %*s%s\n", fd < 0 ? "nf":"->",
654 (int)(s1->include_stack_ptr - s1->include_stack), "", filename);
655 return fd;
658 ST_FUNC int tcc_open(TCCState *s1, const char *filename)
660 int fd = _tcc_open(s1, filename);
661 if (fd < 0)
662 return -1;
663 tcc_open_bf(s1, filename, 0);
664 file->fd = fd;
665 return 0;
668 /* compile the file opened in 'file'. Return non zero if errors. */
669 static int tcc_compile(TCCState *s1, int filetype, const char *str, int fd)
671 tccelf_begin_file(s1);
673 /* Here we enter the code section where we use the global variables for
674 parsing and code generation (tccpp.c, tccgen.c, <target>-gen.c).
675 Other threads need to wait until we're done.
677 Alternatively we could of course pass TCCState *s1 everwhere
678 except that it would look extremly ugly.
680 Alternatively we could use thread local storage for those global
681 variables, which may or may not have advantages */
683 WAIT_SEM();
684 tcc_state = s1;
686 if (setjmp(s1->error_jmp_buf) == 0) {
687 int is_asm;
688 s1->error_set_jmp_enabled = 1;
689 s1->nb_errors = 0;
691 if (fd == -1) {
692 int len = strlen(str);
693 tcc_open_bf(s1, "<string>", len);
694 memcpy(file->buffer, str, len);
695 } else {
696 tcc_open_bf(s1, str, 0);
697 file->fd = fd;
700 is_asm = !!(filetype & (AFF_TYPE_ASM|AFF_TYPE_ASMPP));
701 preprocess_start(s1, is_asm);
702 if (s1->output_type == TCC_OUTPUT_PREPROCESS) {
703 tcc_preprocess(s1);
704 } else if (is_asm) {
705 #ifdef CONFIG_TCC_ASM
706 tcc_assemble(s1, !!(filetype & AFF_TYPE_ASMPP));
707 #else
708 tcc_error_noabort("asm not supported");
709 #endif
710 } else {
711 tccgen_compile(s1);
714 s1->error_set_jmp_enabled = 0;
715 tccgen_finish(s1, 1);
716 preprocess_end(s1);
717 tccgen_finish(s1, 2);
719 tcc_state = NULL;
720 POST_SEM();
721 tccelf_end_file(s1);
722 return s1->nb_errors != 0 ? -1 : 0;
725 LIBTCCAPI int tcc_compile_string(TCCState *s, const char *str)
727 return tcc_compile(s, s->filetype, str, -1);
730 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
731 LIBTCCAPI void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
733 if (!value)
734 value = "1";
735 cstr_printf(&s1->cmdline_defs, "#define %s %s\n", sym, value);
738 /* undefine a preprocessor symbol */
739 LIBTCCAPI void tcc_undefine_symbol(TCCState *s1, const char *sym)
741 cstr_printf(&s1->cmdline_defs, "#undef %s\n", sym);
745 LIBTCCAPI TCCState *tcc_new(void)
747 TCCState *s;
749 s = tcc_mallocz(sizeof(TCCState));
750 if (!s)
751 return NULL;
752 WAIT_SEM();
753 ++nb_states;
754 POST_SEM();
756 #undef gnu_ext
758 s->gnu_ext = 1;
759 s->tcc_ext = 1;
760 s->nocommon = 1;
761 s->dollars_in_identifiers = 1; /*on by default like in gcc/clang*/
762 s->cversion = 199901; /* default unless -std=c11 is supplied */
763 s->warn_implicit_function_declaration = 1;
764 s->ms_extensions = 1;
766 #ifdef CHAR_IS_UNSIGNED
767 s->char_is_unsigned = 1;
768 #endif
769 #ifdef TCC_TARGET_I386
770 s->seg_size = 32;
771 #endif
772 /* enable this if you want symbols with leading underscore on windows: */
773 #if 0 /* def TCC_TARGET_PE */
774 s->leading_underscore = 1;
775 #endif
776 #ifdef CONFIG_TCC_BACKTRACE
777 s->rt_num_callers = 6;
778 #endif
779 s->ppfp = stdout;
780 /* might be used in error() before preprocess_start() */
781 s->include_stack_ptr = s->include_stack;
783 tccelf_new(s);
785 #ifdef _WIN32
786 tcc_set_lib_path_w32(s);
787 #else
788 tcc_set_lib_path(s, CONFIG_TCCDIR);
789 #endif
792 /* define __TINYC__ 92X */
793 char buffer[32]; int a,b,c;
794 sscanf(TCC_VERSION, "%d.%d.%d", &a, &b, &c);
795 sprintf(buffer, "%d", a*10000 + b*100 + c);
796 tcc_define_symbol(s, "__TINYC__", buffer);
799 /* standard defines */
800 tcc_define_symbol(s, "__STDC__", NULL);
801 tcc_define_symbol(s, "__STDC_VERSION__", "199901L");
802 tcc_define_symbol(s, "__STDC_HOSTED__", NULL);
804 /* target defines */
805 #if defined(TCC_TARGET_I386)
806 tcc_define_symbol(s, "__i386__", NULL);
807 tcc_define_symbol(s, "__i386", NULL);
808 tcc_define_symbol(s, "i386", NULL);
809 #elif defined(TCC_TARGET_X86_64)
810 tcc_define_symbol(s, "__x86_64__", NULL);
811 #elif defined(TCC_TARGET_ARM)
812 tcc_define_symbol(s, "__ARM_ARCH_4__", NULL);
813 tcc_define_symbol(s, "__arm_elf__", NULL);
814 tcc_define_symbol(s, "__arm_elf", NULL);
815 tcc_define_symbol(s, "arm_elf", NULL);
816 tcc_define_symbol(s, "__arm__", NULL);
817 tcc_define_symbol(s, "__arm", NULL);
818 tcc_define_symbol(s, "arm", NULL);
819 tcc_define_symbol(s, "__APCS_32__", NULL);
820 tcc_define_symbol(s, "__ARMEL__", NULL);
821 #if defined(TCC_ARM_EABI)
822 tcc_define_symbol(s, "__ARM_EABI__", NULL);
823 #endif
824 #if defined(TCC_ARM_HARDFLOAT)
825 s->float_abi = ARM_HARD_FLOAT;
826 tcc_define_symbol(s, "__ARM_PCS_VFP", NULL);
827 #else
828 s->float_abi = ARM_SOFTFP_FLOAT;
829 #endif
830 #elif defined(TCC_TARGET_ARM64)
831 tcc_define_symbol(s, "__aarch64__", NULL);
832 #elif defined TCC_TARGET_C67
833 tcc_define_symbol(s, "__C67__", NULL);
834 #elif defined TCC_TARGET_RISCV64
835 tcc_define_symbol(s, "__riscv", NULL);
836 tcc_define_symbol(s, "__riscv_xlen", "64");
837 tcc_define_symbol(s, "__riscv_flen", "64");
838 tcc_define_symbol(s, "__riscv_div", NULL);
839 tcc_define_symbol(s, "__riscv_mul", NULL);
840 tcc_define_symbol(s, "__riscv_fdiv", NULL);
841 tcc_define_symbol(s, "__riscv_fsqrt", NULL);
842 tcc_define_symbol(s, "__riscv_float_abi_double", NULL);
843 #endif
845 #ifdef TCC_TARGET_PE
846 tcc_define_symbol(s, "_WIN32", NULL);
847 # ifdef TCC_TARGET_X86_64
848 tcc_define_symbol(s, "_WIN64", NULL);
849 # endif
850 #else
851 tcc_define_symbol(s, "__unix__", NULL);
852 tcc_define_symbol(s, "__unix", NULL);
853 tcc_define_symbol(s, "unix", NULL);
854 # if defined(__linux__)
855 tcc_define_symbol(s, "__linux__", NULL);
856 tcc_define_symbol(s, "__linux", NULL);
857 # endif
858 # if defined(__FreeBSD__)
859 tcc_define_symbol(s, "__FreeBSD__", "__FreeBSD__");
860 /* No 'Thread Storage Local' on FreeBSD with tcc */
861 tcc_define_symbol(s, "__NO_TLS", NULL);
862 # endif
863 # if defined(__FreeBSD_kernel__)
864 tcc_define_symbol(s, "__FreeBSD_kernel__", NULL);
865 # endif
866 # if defined(__NetBSD__)
867 tcc_define_symbol(s, "__NetBSD__", "__NetBSD__");
868 # endif
869 # if defined(__OpenBSD__)
870 tcc_define_symbol(s, "__OpenBSD__", "__OpenBSD__");
871 # endif
872 #endif
874 /* TinyCC & gcc defines */
875 #if PTR_SIZE == 4
876 /* 32bit systems. */
877 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned int");
878 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "int");
879 tcc_define_symbol(s, "__ILP32__", NULL);
880 #elif LONG_SIZE == 4
881 /* 64bit Windows. */
882 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned long long");
883 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "long long");
884 tcc_define_symbol(s, "__LLP64__", NULL);
885 #else
886 /* Other 64bit systems. */
887 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned long");
888 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "long");
889 tcc_define_symbol(s, "__LP64__", NULL);
890 #endif
891 tcc_define_symbol(s, "__SIZEOF_POINTER__", PTR_SIZE == 4 ? "4" : "8");
893 #ifdef TCC_TARGET_PE
894 tcc_define_symbol(s, "__WCHAR_TYPE__", "unsigned short");
895 tcc_define_symbol(s, "__WINT_TYPE__", "unsigned short");
896 #else
897 tcc_define_symbol(s, "__WCHAR_TYPE__", "int");
898 /* wint_t is unsigned int by default, but (signed) int on BSDs
899 and unsigned short on windows. Other OSes might have still
900 other conventions, sigh. */
901 # if defined(__FreeBSD__) || defined (__FreeBSD_kernel__) \
902 || defined(__NetBSD__) || defined(__OpenBSD__)
903 tcc_define_symbol(s, "__WINT_TYPE__", "int");
904 # ifdef __FreeBSD__
905 /* define __GNUC__ to have some useful stuff from sys/cdefs.h
906 that are unconditionally used in FreeBSDs other system headers :/ */
907 tcc_define_symbol(s, "__GNUC__", "2");
908 tcc_define_symbol(s, "__GNUC_MINOR__", "7");
909 tcc_define_symbol(s, "__builtin_alloca", "alloca");
910 # endif
911 # else
912 tcc_define_symbol(s, "__WINT_TYPE__", "unsigned int");
913 /* glibc defines */
914 tcc_define_symbol(s, "__REDIRECT(name, proto, alias)",
915 "name proto __asm__ (#alias)");
916 tcc_define_symbol(s, "__REDIRECT_NTH(name, proto, alias)",
917 "name proto __asm__ (#alias) __THROW");
918 # endif
919 # if defined(TCC_MUSL)
920 tcc_define_symbol(s, "__DEFINED_va_list", "");
921 tcc_define_symbol(s, "__DEFINED___isoc_va_list", "");
922 tcc_define_symbol(s, "__isoc_va_list", "void *");
923 # endif /* TCC_MUSL */
924 /* Some GCC builtins that are simple to express as macros. */
925 tcc_define_symbol(s, "__builtin_extract_return_addr(x)", "x");
926 #endif /* ndef TCC_TARGET_PE */
927 return s;
930 LIBTCCAPI void tcc_delete(TCCState *s1)
932 /* free sections */
933 tccelf_delete(s1);
935 /* free library paths */
936 dynarray_reset(&s1->library_paths, &s1->nb_library_paths);
937 dynarray_reset(&s1->crt_paths, &s1->nb_crt_paths);
939 /* free include paths */
940 dynarray_reset(&s1->include_paths, &s1->nb_include_paths);
941 dynarray_reset(&s1->sysinclude_paths, &s1->nb_sysinclude_paths);
943 tcc_free(s1->tcc_lib_path);
944 tcc_free(s1->soname);
945 tcc_free(s1->rpath);
946 tcc_free(s1->init_symbol);
947 tcc_free(s1->fini_symbol);
948 tcc_free(s1->outfile);
949 tcc_free(s1->deps_outfile);
950 dynarray_reset(&s1->files, &s1->nb_files);
951 dynarray_reset(&s1->target_deps, &s1->nb_target_deps);
952 dynarray_reset(&s1->pragma_libs, &s1->nb_pragma_libs);
953 dynarray_reset(&s1->argv, &s1->argc);
955 cstr_free(&s1->cmdline_defs);
956 cstr_free(&s1->cmdline_incl);
957 #ifdef TCC_IS_NATIVE
958 /* free runtime memory */
959 tcc_run_free(s1);
960 #endif
962 tcc_free(s1);
963 WAIT_SEM();
964 if (0 == --nb_states)
965 tcc_memcheck();
966 POST_SEM();
969 LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type)
971 s->output_type = output_type;
973 /* always elf for objects */
974 if (output_type == TCC_OUTPUT_OBJ)
975 s->output_format = TCC_OUTPUT_FORMAT_ELF;
977 if (s->char_is_unsigned)
978 tcc_define_symbol(s, "__CHAR_UNSIGNED__", NULL);
980 if (!s->nostdinc) {
981 /* default include paths */
982 /* -isystem paths have already been handled */
983 tcc_add_sysinclude_path(s, CONFIG_TCC_SYSINCLUDEPATHS);
986 #ifdef CONFIG_TCC_BCHECK
987 if (s->do_bounds_check) {
988 /* if bound checking, then add corresponding sections */
989 tccelf_bounds_new(s);
990 /* define symbol */
991 tcc_define_symbol(s, "__BOUNDS_CHECKING_ON", NULL);
993 #endif
994 if (s->do_debug) {
995 /* add debug sections */
996 tccelf_stab_new(s);
999 tcc_add_library_path(s, CONFIG_TCC_LIBPATHS);
1001 #ifdef TCC_TARGET_PE
1002 # ifdef _WIN32
1003 if (!s->nostdlib && output_type != TCC_OUTPUT_OBJ)
1004 tcc_add_systemdir(s);
1005 # endif
1006 #else
1007 /* paths for crt objects */
1008 tcc_split_path(s, &s->crt_paths, &s->nb_crt_paths, CONFIG_TCC_CRTPREFIX);
1009 /* add libc crt1/crti objects */
1010 if ((output_type == TCC_OUTPUT_EXE || output_type == TCC_OUTPUT_DLL) &&
1011 !s->nostdlib) {
1012 if (output_type != TCC_OUTPUT_DLL)
1013 tcc_add_crt(s, "crt1.o");
1014 tcc_add_crt(s, "crti.o");
1016 #endif
1017 return 0;
1020 LIBTCCAPI int tcc_add_include_path(TCCState *s, const char *pathname)
1022 tcc_split_path(s, &s->include_paths, &s->nb_include_paths, pathname);
1023 return 0;
1026 LIBTCCAPI int tcc_add_sysinclude_path(TCCState *s, const char *pathname)
1028 tcc_split_path(s, &s->sysinclude_paths, &s->nb_sysinclude_paths, pathname);
1029 return 0;
1032 ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
1034 int fd, ret;
1036 /* open the file */
1037 fd = _tcc_open(s1, filename);
1038 if (fd < 0) {
1039 if (flags & AFF_PRINT_ERROR)
1040 tcc_error_noabort("file '%s' not found", filename);
1041 return -1;
1044 /* update target deps */
1045 dynarray_add(&s1->target_deps, &s1->nb_target_deps,
1046 tcc_strdup(filename));
1048 if (flags & AFF_TYPE_BIN) {
1049 ElfW(Ehdr) ehdr;
1050 int obj_type;
1052 obj_type = tcc_object_type(fd, &ehdr);
1053 lseek(fd, 0, SEEK_SET);
1055 #ifdef TCC_TARGET_MACHO
1056 if (0 == obj_type && 0 == strcmp(tcc_fileextension(filename), ".dylib"))
1057 obj_type = AFF_BINTYPE_DYN;
1058 #endif
1060 switch (obj_type) {
1061 case AFF_BINTYPE_REL:
1062 ret = tcc_load_object_file(s1, fd, 0);
1063 break;
1064 #ifndef TCC_TARGET_PE
1065 case AFF_BINTYPE_DYN:
1066 if (s1->output_type == TCC_OUTPUT_MEMORY) {
1067 ret = 0;
1068 #ifdef TCC_IS_NATIVE
1069 if (NULL == dlopen(filename, RTLD_GLOBAL | RTLD_LAZY))
1070 ret = -1;
1071 #endif
1072 } else {
1073 ret = tcc_load_dll(s1, fd, filename,
1074 (flags & AFF_REFERENCED_DLL) != 0);
1076 break;
1077 #endif
1078 case AFF_BINTYPE_AR:
1079 ret = tcc_load_archive(s1, fd, !(flags & AFF_WHOLE_ARCHIVE));
1080 break;
1081 #ifdef TCC_TARGET_COFF
1082 case AFF_BINTYPE_C67:
1083 ret = tcc_load_coff(s1, fd);
1084 break;
1085 #endif
1086 default:
1087 #ifdef TCC_TARGET_PE
1088 ret = pe_load_file(s1, filename, fd);
1089 #else
1090 /* as GNU ld, consider it is an ld script if not recognized */
1091 ret = tcc_load_ldscript(s1, fd);
1092 #endif
1093 if (ret < 0)
1094 tcc_error_noabort("unrecognized file type");
1095 break;
1097 close(fd);
1098 } else {
1099 ret = tcc_compile(s1, flags, filename, fd);
1101 return ret;
1104 LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename)
1106 int filetype = s->filetype;
1107 if (0 == (filetype & AFF_TYPE_MASK)) {
1108 /* use a file extension to detect a filetype */
1109 const char *ext = tcc_fileextension(filename);
1110 if (ext[0]) {
1111 ext++;
1112 if (!strcmp(ext, "S"))
1113 filetype = AFF_TYPE_ASMPP;
1114 else if (!strcmp(ext, "s"))
1115 filetype = AFF_TYPE_ASM;
1116 else if (!PATHCMP(ext, "c") || !PATHCMP(ext, "i"))
1117 filetype = AFF_TYPE_C;
1118 else
1119 filetype |= AFF_TYPE_BIN;
1120 } else {
1121 filetype = AFF_TYPE_C;
1124 return tcc_add_file_internal(s, filename, filetype | AFF_PRINT_ERROR);
1127 LIBTCCAPI int tcc_add_library_path(TCCState *s, const char *pathname)
1129 tcc_split_path(s, &s->library_paths, &s->nb_library_paths, pathname);
1130 return 0;
1133 static int tcc_add_library_internal(TCCState *s, const char *fmt,
1134 const char *filename, int flags, char **paths, int nb_paths)
1136 char buf[1024];
1137 int i;
1139 for(i = 0; i < nb_paths; i++) {
1140 snprintf(buf, sizeof(buf), fmt, paths[i], filename);
1141 if (tcc_add_file_internal(s, buf, flags | AFF_TYPE_BIN) == 0)
1142 return 0;
1144 return -1;
1147 /* find and load a dll. Return non zero if not found */
1148 /* XXX: add '-rpath' option support ? */
1149 ST_FUNC int tcc_add_dll(TCCState *s, const char *filename, int flags)
1151 return tcc_add_library_internal(s, "%s/%s", filename, flags,
1152 s->library_paths, s->nb_library_paths);
1155 #ifndef TCC_TARGET_PE
1156 ST_FUNC int tcc_add_crt(TCCState *s1, const char *filename)
1158 if (-1 == tcc_add_library_internal(s1, "%s/%s",
1159 filename, 0, s1->crt_paths, s1->nb_crt_paths))
1160 tcc_error_noabort("file '%s' not found", filename);
1161 return 0;
1163 #endif
1165 /* the library name is the same as the argument of the '-l' option */
1166 LIBTCCAPI int tcc_add_library(TCCState *s, const char *libraryname)
1168 #if defined TCC_TARGET_PE
1169 const char *libs[] = { "%s/%s.def", "%s/lib%s.def", "%s/%s.dll", "%s/lib%s.dll", "%s/lib%s.a", NULL };
1170 const char **pp = s->static_link ? libs + 4 : libs;
1171 #elif defined TCC_TARGET_MACHO
1172 const char *libs[] = { "%s/lib%s.dylib", "%s/lib%s.a", NULL };
1173 const char **pp = s->static_link ? libs + 1 : libs;
1174 #else
1175 const char *libs[] = { "%s/lib%s.so", "%s/lib%s.a", NULL };
1176 const char **pp = s->static_link ? libs + 1 : libs;
1177 #endif
1178 int flags = s->filetype & AFF_WHOLE_ARCHIVE;
1179 while (*pp) {
1180 if (0 == tcc_add_library_internal(s, *pp,
1181 libraryname, flags, s->library_paths, s->nb_library_paths))
1182 return 0;
1183 ++pp;
1185 return -1;
1188 PUB_FUNC int tcc_add_library_err(TCCState *s1, const char *libname)
1190 int ret = tcc_add_library(s1, libname);
1191 if (ret < 0)
1192 tcc_error_noabort("library '%s' not found", libname);
1193 return ret;
1196 /* handle #pragma comment(lib,) */
1197 ST_FUNC void tcc_add_pragma_libs(TCCState *s1)
1199 int i;
1200 for (i = 0; i < s1->nb_pragma_libs; i++)
1201 tcc_add_library_err(s1, s1->pragma_libs[i]);
1204 LIBTCCAPI int tcc_add_symbol(TCCState *s1, const char *name, const void *val)
1206 #ifdef TCC_TARGET_PE
1207 /* On x86_64 'val' might not be reachable with a 32bit offset.
1208 So it is handled here as if it were in a DLL. */
1209 pe_putimport(s1, 0, name, (uintptr_t)val);
1210 #else
1211 set_elf_sym(symtab_section, (uintptr_t)val, 0,
1212 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1213 SHN_ABS, name);
1214 #endif
1215 return 0;
1218 LIBTCCAPI void tcc_set_lib_path(TCCState *s, const char *path)
1220 tcc_free(s->tcc_lib_path);
1221 s->tcc_lib_path = tcc_strdup(path);
1224 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
1225 #define FD_INVERT 0x0002 /* invert value before storing */
1227 typedef struct FlagDef {
1228 uint16_t offset;
1229 uint16_t flags;
1230 const char *name;
1231 } FlagDef;
1233 static int no_flag(const char **pp)
1235 const char *p = *pp;
1236 if (*p != 'n' || *++p != 'o' || *++p != '-')
1237 return 0;
1238 *pp = p + 1;
1239 return 1;
1242 ST_FUNC int set_flag(TCCState *s, const FlagDef *flags, const char *name)
1244 int value, ret;
1245 const FlagDef *p;
1246 const char *r;
1248 value = 1;
1249 r = name;
1250 if (no_flag(&r))
1251 value = 0;
1253 for (ret = -1, p = flags; p->name; ++p) {
1254 if (ret) {
1255 if (strcmp(r, p->name))
1256 continue;
1257 } else {
1258 if (0 == (p->flags & WD_ALL))
1259 continue;
1261 if (p->offset) {
1262 *((unsigned char *)s + p->offset) =
1263 p->flags & FD_INVERT ? !value : value;
1264 if (ret)
1265 return 0;
1266 } else {
1267 ret = 0;
1270 return ret;
1273 static int strstart(const char *val, const char **str)
1275 const char *p, *q;
1276 p = *str;
1277 q = val;
1278 while (*q) {
1279 if (*p != *q)
1280 return 0;
1281 p++;
1282 q++;
1284 *str = p;
1285 return 1;
1288 /* Like strstart, but automatically takes into account that ld options can
1290 * - start with double or single dash (e.g. '--soname' or '-soname')
1291 * - arguments can be given as separate or after '=' (e.g. '-Wl,-soname,x.so'
1292 * or '-Wl,-soname=x.so')
1294 * you provide `val` always in 'option[=]' form (no leading -)
1296 static int link_option(const char *str, const char *val, const char **ptr)
1298 const char *p, *q;
1299 int ret;
1301 /* there should be 1 or 2 dashes */
1302 if (*str++ != '-')
1303 return 0;
1304 if (*str == '-')
1305 str++;
1307 /* then str & val should match (potentially up to '=') */
1308 p = str;
1309 q = val;
1311 ret = 1;
1312 if (q[0] == '?') {
1313 ++q;
1314 if (no_flag(&p))
1315 ret = -1;
1318 while (*q != '\0' && *q != '=') {
1319 if (*p != *q)
1320 return 0;
1321 p++;
1322 q++;
1325 /* '=' near eos means ',' or '=' is ok */
1326 if (*q == '=') {
1327 if (*p == 0)
1328 *ptr = p;
1329 if (*p != ',' && *p != '=')
1330 return 0;
1331 p++;
1332 } else if (*p) {
1333 return 0;
1335 *ptr = p;
1336 return ret;
1339 static const char *skip_linker_arg(const char **str)
1341 const char *s1 = *str;
1342 const char *s2 = strchr(s1, ',');
1343 *str = s2 ? s2++ : (s2 = s1 + strlen(s1));
1344 return s2;
1347 static void copy_linker_arg(char **pp, const char *s, int sep)
1349 const char *q = s;
1350 char *p = *pp;
1351 int l = 0;
1352 if (p && sep)
1353 p[l = strlen(p)] = sep, ++l;
1354 skip_linker_arg(&q);
1355 pstrncpy(l + (*pp = tcc_realloc(p, q - s + l + 1)), s, q - s);
1358 /* set linker options */
1359 static int tcc_set_linker(TCCState *s, const char *option)
1361 TCCState *s1 = s;
1362 while (*option) {
1364 const char *p = NULL;
1365 char *end = NULL;
1366 int ignoring = 0;
1367 int ret;
1369 if (link_option(option, "Bsymbolic", &p)) {
1370 s->symbolic = 1;
1371 } else if (link_option(option, "nostdlib", &p)) {
1372 s->nostdlib = 1;
1373 } else if (link_option(option, "fini=", &p)) {
1374 copy_linker_arg(&s->fini_symbol, p, 0);
1375 ignoring = 1;
1376 } else if (link_option(option, "image-base=", &p)
1377 || link_option(option, "Ttext=", &p)) {
1378 s->text_addr = strtoull(p, &end, 16);
1379 s->has_text_addr = 1;
1380 } else if (link_option(option, "init=", &p)) {
1381 copy_linker_arg(&s->init_symbol, p, 0);
1382 ignoring = 1;
1383 } else if (link_option(option, "oformat=", &p)) {
1384 #if defined(TCC_TARGET_PE)
1385 if (strstart("pe-", &p)) {
1386 #elif PTR_SIZE == 8
1387 if (strstart("elf64-", &p)) {
1388 #else
1389 if (strstart("elf32-", &p)) {
1390 #endif
1391 s->output_format = TCC_OUTPUT_FORMAT_ELF;
1392 } else if (!strcmp(p, "binary")) {
1393 s->output_format = TCC_OUTPUT_FORMAT_BINARY;
1394 #ifdef TCC_TARGET_COFF
1395 } else if (!strcmp(p, "coff")) {
1396 s->output_format = TCC_OUTPUT_FORMAT_COFF;
1397 #endif
1398 } else
1399 goto err;
1401 } else if (link_option(option, "as-needed", &p)) {
1402 ignoring = 1;
1403 } else if (link_option(option, "O", &p)) {
1404 ignoring = 1;
1405 } else if (link_option(option, "export-all-symbols", &p)) {
1406 s->rdynamic = 1;
1407 } else if (link_option(option, "export-dynamic", &p)) {
1408 s->rdynamic = 1;
1409 } else if (link_option(option, "rpath=", &p)) {
1410 copy_linker_arg(&s->rpath, p, ':');
1411 } else if (link_option(option, "enable-new-dtags", &p)) {
1412 s->enable_new_dtags = 1;
1413 } else if (link_option(option, "section-alignment=", &p)) {
1414 s->section_align = strtoul(p, &end, 16);
1415 } else if (link_option(option, "soname=", &p)) {
1416 copy_linker_arg(&s->soname, p, 0);
1417 #ifdef TCC_TARGET_PE
1418 } else if (link_option(option, "large-address-aware", &p)) {
1419 s->pe_characteristics |= 0x20;
1420 } else if (link_option(option, "file-alignment=", &p)) {
1421 s->pe_file_align = strtoul(p, &end, 16);
1422 } else if (link_option(option, "stack=", &p)) {
1423 s->pe_stack_size = strtoul(p, &end, 10);
1424 } else if (link_option(option, "subsystem=", &p)) {
1425 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1426 if (!strcmp(p, "native")) {
1427 s->pe_subsystem = 1;
1428 } else if (!strcmp(p, "console")) {
1429 s->pe_subsystem = 3;
1430 } else if (!strcmp(p, "gui") || !strcmp(p, "windows")) {
1431 s->pe_subsystem = 2;
1432 } else if (!strcmp(p, "posix")) {
1433 s->pe_subsystem = 7;
1434 } else if (!strcmp(p, "efiapp")) {
1435 s->pe_subsystem = 10;
1436 } else if (!strcmp(p, "efiboot")) {
1437 s->pe_subsystem = 11;
1438 } else if (!strcmp(p, "efiruntime")) {
1439 s->pe_subsystem = 12;
1440 } else if (!strcmp(p, "efirom")) {
1441 s->pe_subsystem = 13;
1442 #elif defined(TCC_TARGET_ARM)
1443 if (!strcmp(p, "wince")) {
1444 s->pe_subsystem = 9;
1445 #endif
1446 } else
1447 goto err;
1448 #endif
1449 } else if (ret = link_option(option, "?whole-archive", &p), ret) {
1450 if (ret > 0)
1451 s->filetype |= AFF_WHOLE_ARCHIVE;
1452 else
1453 s->filetype &= ~AFF_WHOLE_ARCHIVE;
1454 } else if (p) {
1455 return 0;
1456 } else {
1457 err:
1458 tcc_error("unsupported linker option '%s'", option);
1461 if (ignoring && s->warn_unsupported)
1462 tcc_warning("unsupported linker option '%s'", option);
1464 option = skip_linker_arg(&p);
1466 return 1;
1469 typedef struct TCCOption {
1470 const char *name;
1471 uint16_t index;
1472 uint16_t flags;
1473 } TCCOption;
1475 enum {
1476 TCC_OPTION_HELP,
1477 TCC_OPTION_HELP2,
1478 TCC_OPTION_v,
1479 TCC_OPTION_I,
1480 TCC_OPTION_D,
1481 TCC_OPTION_U,
1482 TCC_OPTION_P,
1483 TCC_OPTION_L,
1484 TCC_OPTION_B,
1485 TCC_OPTION_l,
1486 TCC_OPTION_bench,
1487 TCC_OPTION_bt,
1488 TCC_OPTION_b,
1489 TCC_OPTION_ba,
1490 TCC_OPTION_g,
1491 TCC_OPTION_c,
1492 TCC_OPTION_dumpversion,
1493 TCC_OPTION_d,
1494 TCC_OPTION_static,
1495 TCC_OPTION_std,
1496 TCC_OPTION_shared,
1497 TCC_OPTION_soname,
1498 TCC_OPTION_o,
1499 TCC_OPTION_r,
1500 TCC_OPTION_s,
1501 TCC_OPTION_traditional,
1502 TCC_OPTION_Wl,
1503 TCC_OPTION_Wp,
1504 TCC_OPTION_W,
1505 TCC_OPTION_O,
1506 TCC_OPTION_mfloat_abi,
1507 TCC_OPTION_m,
1508 TCC_OPTION_f,
1509 TCC_OPTION_isystem,
1510 TCC_OPTION_iwithprefix,
1511 TCC_OPTION_include,
1512 TCC_OPTION_nostdinc,
1513 TCC_OPTION_nostdlib,
1514 TCC_OPTION_print_search_dirs,
1515 TCC_OPTION_rdynamic,
1516 TCC_OPTION_param,
1517 TCC_OPTION_pedantic,
1518 TCC_OPTION_pthread,
1519 TCC_OPTION_run,
1520 TCC_OPTION_w,
1521 TCC_OPTION_pipe,
1522 TCC_OPTION_E,
1523 TCC_OPTION_MD,
1524 TCC_OPTION_MF,
1525 TCC_OPTION_x,
1526 TCC_OPTION_ar,
1527 TCC_OPTION_impdef
1530 #define TCC_OPTION_HAS_ARG 0x0001
1531 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
1533 static const TCCOption tcc_options[] = {
1534 { "h", TCC_OPTION_HELP, 0 },
1535 { "-help", TCC_OPTION_HELP, 0 },
1536 { "?", TCC_OPTION_HELP, 0 },
1537 { "hh", TCC_OPTION_HELP2, 0 },
1538 { "v", TCC_OPTION_v, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1539 { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
1540 { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
1541 { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
1542 { "P", TCC_OPTION_P, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1543 { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
1544 { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
1545 { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG },
1546 { "bench", TCC_OPTION_bench, 0 },
1547 #ifdef CONFIG_TCC_BACKTRACE
1548 { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG },
1549 #endif
1550 #ifdef CONFIG_TCC_BCHECK
1551 { "b", TCC_OPTION_b, 0 },
1552 #endif
1553 { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1554 { "c", TCC_OPTION_c, 0 },
1555 { "dumpversion", TCC_OPTION_dumpversion, 0},
1556 { "d", TCC_OPTION_d, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1557 { "static", TCC_OPTION_static, 0 },
1558 { "std", TCC_OPTION_std, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1559 { "shared", TCC_OPTION_shared, 0 },
1560 { "soname", TCC_OPTION_soname, TCC_OPTION_HAS_ARG },
1561 { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
1562 { "-param", TCC_OPTION_param, TCC_OPTION_HAS_ARG },
1563 { "pedantic", TCC_OPTION_pedantic, 0},
1564 { "pthread", TCC_OPTION_pthread, 0},
1565 { "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1566 { "rdynamic", TCC_OPTION_rdynamic, 0 },
1567 { "r", TCC_OPTION_r, 0 },
1568 { "s", TCC_OPTION_s, 0 },
1569 { "traditional", TCC_OPTION_traditional, 0 },
1570 { "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1571 { "Wp,", TCC_OPTION_Wp, 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 #ifdef TCC_TARGET_ARM
1575 { "mfloat-abi", TCC_OPTION_mfloat_abi, TCC_OPTION_HAS_ARG },
1576 #endif
1577 { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1578 { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1579 { "isystem", TCC_OPTION_isystem, TCC_OPTION_HAS_ARG },
1580 { "include", TCC_OPTION_include, TCC_OPTION_HAS_ARG },
1581 { "nostdinc", TCC_OPTION_nostdinc, 0 },
1582 { "nostdlib", TCC_OPTION_nostdlib, 0 },
1583 { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
1584 { "w", TCC_OPTION_w, 0 },
1585 { "pipe", TCC_OPTION_pipe, 0},
1586 { "E", TCC_OPTION_E, 0},
1587 { "MD", TCC_OPTION_MD, 0},
1588 { "MF", TCC_OPTION_MF, TCC_OPTION_HAS_ARG },
1589 { "x", TCC_OPTION_x, TCC_OPTION_HAS_ARG },
1590 { "ar", TCC_OPTION_ar, 0},
1591 #ifdef TCC_TARGET_PE
1592 { "impdef", TCC_OPTION_impdef, 0},
1593 #endif
1594 { NULL, 0, 0 },
1597 static const FlagDef options_W[] = {
1598 { 0, 0, "all" },
1599 { offsetof(TCCState, warn_unsupported), 0, "unsupported" },
1600 { offsetof(TCCState, warn_write_strings), 0, "write-strings" },
1601 { offsetof(TCCState, warn_error), 0, "error" },
1602 { offsetof(TCCState, warn_gcc_compat), 0, "gcc-compat" },
1603 { offsetof(TCCState, warn_implicit_function_declaration), WD_ALL,
1604 "implicit-function-declaration" },
1605 { 0, 0, NULL }
1608 static const FlagDef options_f[] = {
1609 { offsetof(TCCState, char_is_unsigned), 0, "unsigned-char" },
1610 { offsetof(TCCState, char_is_unsigned), FD_INVERT, "signed-char" },
1611 { offsetof(TCCState, nocommon), FD_INVERT, "common" },
1612 { offsetof(TCCState, leading_underscore), 0, "leading-underscore" },
1613 { offsetof(TCCState, ms_extensions), 0, "ms-extensions" },
1614 { offsetof(TCCState, dollars_in_identifiers), 0, "dollars-in-identifiers" },
1615 { 0, 0, NULL }
1618 static const FlagDef options_m[] = {
1619 { offsetof(TCCState, ms_bitfields), 0, "ms-bitfields" },
1620 #ifdef TCC_TARGET_X86_64
1621 { offsetof(TCCState, nosse), FD_INVERT, "sse" },
1622 #endif
1623 { 0, 0, NULL }
1626 static void parse_option_D(TCCState *s1, const char *optarg)
1628 char *sym = tcc_strdup(optarg);
1629 char *value = strchr(sym, '=');
1630 if (value)
1631 *value++ = '\0';
1632 tcc_define_symbol(s1, sym, value);
1633 tcc_free(sym);
1636 static void args_parser_add_file(TCCState *s, const char* filename, int filetype)
1638 struct filespec *f = tcc_malloc(sizeof *f + strlen(filename));
1639 f->type = filetype;
1640 strcpy(f->name, filename);
1641 dynarray_add(&s->files, &s->nb_files, f);
1644 static int args_parser_make_argv(const char *r, int *argc, char ***argv)
1646 int ret = 0, q, c;
1647 CString str;
1648 for(;;) {
1649 while (c = (unsigned char)*r, c && c <= ' ')
1650 ++r;
1651 if (c == 0)
1652 break;
1653 q = 0;
1654 cstr_new(&str);
1655 while (c = (unsigned char)*r, c) {
1656 ++r;
1657 if (c == '\\' && (*r == '"' || *r == '\\')) {
1658 c = *r++;
1659 } else if (c == '"') {
1660 q = !q;
1661 continue;
1662 } else if (q == 0 && c <= ' ') {
1663 break;
1665 cstr_ccat(&str, c);
1667 cstr_ccat(&str, 0);
1668 //printf("<%s>\n", str.data), fflush(stdout);
1669 dynarray_add(argv, argc, tcc_strdup(str.data));
1670 cstr_free(&str);
1671 ++ret;
1673 return ret;
1676 /* read list file */
1677 static void args_parser_listfile(TCCState *s,
1678 const char *filename, int optind, int *pargc, char ***pargv)
1680 TCCState *s1 = s;
1681 int fd, i;
1682 size_t len;
1683 char *p;
1684 int argc = 0;
1685 char **argv = NULL;
1687 fd = open(filename, O_RDONLY | O_BINARY);
1688 if (fd < 0)
1689 tcc_error("listfile '%s' not found", filename);
1691 len = lseek(fd, 0, SEEK_END);
1692 p = tcc_malloc(len + 1), p[len] = 0;
1693 lseek(fd, 0, SEEK_SET), read(fd, p, len), close(fd);
1695 for (i = 0; i < *pargc; ++i)
1696 if (i == optind)
1697 args_parser_make_argv(p, &argc, &argv);
1698 else
1699 dynarray_add(&argv, &argc, tcc_strdup((*pargv)[i]));
1701 tcc_free(p);
1702 dynarray_reset(&s->argv, &s->argc);
1703 *pargc = s->argc = argc, *pargv = s->argv = argv;
1706 PUB_FUNC int tcc_parse_args(TCCState *s, int *pargc, char ***pargv, int optind)
1708 TCCState *s1 = s;
1709 const TCCOption *popt;
1710 const char *optarg, *r;
1711 const char *run = NULL;
1712 int last_o = -1;
1713 int x;
1714 CString linker_arg; /* collect -Wl options */
1715 int tool = 0, arg_start = 0, noaction = optind;
1716 char **argv = *pargv;
1717 int argc = *pargc;
1719 cstr_new(&linker_arg);
1721 while (optind < argc) {
1722 r = argv[optind];
1723 if (r[0] == '@' && r[1] != '\0') {
1724 args_parser_listfile(s, r + 1, optind, &argc, &argv);
1725 continue;
1727 optind++;
1728 if (tool) {
1729 if (r[0] == '-' && r[1] == 'v' && r[2] == 0)
1730 ++s->verbose;
1731 continue;
1733 reparse:
1734 if (r[0] != '-' || r[1] == '\0') {
1735 if (r[0] != '@') /* allow "tcc file(s) -run @ args ..." */
1736 args_parser_add_file(s, r, s->filetype);
1737 if (run) {
1738 tcc_set_options(s, run);
1739 arg_start = optind - 1;
1740 break;
1742 continue;
1745 /* find option in table */
1746 for(popt = tcc_options; ; ++popt) {
1747 const char *p1 = popt->name;
1748 const char *r1 = r + 1;
1749 if (p1 == NULL)
1750 tcc_error("invalid option -- '%s'", r);
1751 if (!strstart(p1, &r1))
1752 continue;
1753 optarg = r1;
1754 if (popt->flags & TCC_OPTION_HAS_ARG) {
1755 if (*r1 == '\0' && !(popt->flags & TCC_OPTION_NOSEP)) {
1756 if (optind >= argc)
1757 arg_err:
1758 tcc_error("argument to '%s' is missing", r);
1759 optarg = argv[optind++];
1761 } else if (*r1 != '\0')
1762 continue;
1763 break;
1766 switch(popt->index) {
1767 case TCC_OPTION_HELP:
1768 return OPT_HELP;
1769 case TCC_OPTION_HELP2:
1770 return OPT_HELP2;
1771 case TCC_OPTION_I:
1772 tcc_add_include_path(s, optarg);
1773 break;
1774 case TCC_OPTION_D:
1775 parse_option_D(s, optarg);
1776 break;
1777 case TCC_OPTION_U:
1778 tcc_undefine_symbol(s, optarg);
1779 break;
1780 case TCC_OPTION_L:
1781 tcc_add_library_path(s, optarg);
1782 break;
1783 case TCC_OPTION_B:
1784 /* set tcc utilities path (mainly for tcc development) */
1785 tcc_set_lib_path(s, optarg);
1786 break;
1787 case TCC_OPTION_l:
1788 args_parser_add_file(s, optarg, AFF_TYPE_LIB | (s->filetype & ~AFF_TYPE_MASK));
1789 s->nb_libraries++;
1790 break;
1791 case TCC_OPTION_pthread:
1792 parse_option_D(s, "_REENTRANT");
1793 s->option_pthread = 1;
1794 break;
1795 case TCC_OPTION_bench:
1796 s->do_bench = 1;
1797 break;
1798 #ifdef CONFIG_TCC_BACKTRACE
1799 case TCC_OPTION_bt:
1800 s->rt_num_callers = atoi(optarg);
1801 break;
1802 #endif
1803 #ifdef CONFIG_TCC_BCHECK
1804 case TCC_OPTION_b:
1805 s->do_bounds_check = 1;
1806 s->do_debug = 1;
1807 break;
1808 #endif
1809 case TCC_OPTION_g:
1810 s->do_debug = 1;
1811 break;
1812 case TCC_OPTION_c:
1813 x = TCC_OUTPUT_OBJ;
1814 set_output_type:
1815 if (s->output_type)
1816 tcc_warning("-%s: overriding compiler action already specified", popt->name);
1817 s->output_type = x;
1818 break;
1819 case TCC_OPTION_d:
1820 if (*optarg == 'D')
1821 s->dflag = 3;
1822 else if (*optarg == 'M')
1823 s->dflag = 7;
1824 else if (*optarg == 't')
1825 s->dflag = 16;
1826 else if (isnum(*optarg))
1827 g_debug = atoi(optarg);
1828 else
1829 goto unsupported_option;
1830 break;
1831 case TCC_OPTION_static:
1832 s->static_link = 1;
1833 break;
1834 case TCC_OPTION_std:
1835 if (*optarg == '=') {
1836 if (strcmp(optarg, "=c11") == 0) {
1837 tcc_undefine_symbol(s, "__STDC_VERSION__");
1838 tcc_define_symbol(s, "__STDC_VERSION__", "201112L");
1840 * The integer constant 1, intended to indicate
1841 * that the implementation does not support atomic
1842 * types (including the _Atomic type qualifier) and
1843 * the <stdatomic.h> header.
1845 tcc_define_symbol(s, "__STDC_NO_ATOMICS__", "1");
1847 * The integer constant 1, intended to indicate
1848 * that the implementation does not support complex
1849 * types or the <complex.h> header.
1851 tcc_define_symbol(s, "__STDC_NO_COMPLEX__", "1");
1853 * The integer constant 1, intended to indicate
1854 * that the implementation does not support the
1855 * <threads.h> header.
1857 tcc_define_symbol(s, "__STDC_NO_THREADS__", "1");
1859 * __STDC_NO_VLA__, tcc supports VLA.
1860 * The integer constant 1, intended to indicate
1861 * that the implementation does not support
1862 * variable length arrays or variably modified
1863 * types.
1865 #if !defined(TCC_TARGET_PE)
1867 * An integer constant of the form yyyymmL (for
1868 * example, 199712L). If this symbol is defined,
1869 * then every character in the Unicode required
1870 * set, when stored in an object of type
1871 * wchar_t, has the same value as the short
1872 * identifier of that character.
1874 #if 0
1875 /* on Linux, this conflicts with a define introduced by
1876 * /usr/include/stdc-predef.h included by glibc libs;
1877 * clang doesn't define it at all so it's probably not necessary
1879 tcc_define_symbol(s, "__STDC_ISO_10646__", "201605L");
1880 #endif
1882 * The integer constant 1, intended to indicate
1883 * that values of type char16_t are UTF−16
1884 * encoded. If some other encoding is used, the
1885 * macro shall not be defined and the actual
1886 * encoding used is implementation defined.
1888 tcc_define_symbol(s, "__STDC_UTF_16__", "1");
1890 * The integer constant 1, intended to indicate
1891 * that values of type char32_t are UTF−32
1892 * encoded. If some other encoding is used, the
1893 * macro shall not be defined and the actual
1894 * encoding used is implementationdefined.
1896 tcc_define_symbol(s, "__STDC_UTF_32__", "1");
1897 #endif /* !TCC_TARGET_PE */
1898 s->cversion = 201112;
1902 * silently ignore other values, a current purpose:
1903 * allow to use a tcc as a reference compiler for "make test"
1905 break;
1906 case TCC_OPTION_shared:
1907 x = TCC_OUTPUT_DLL;
1908 goto set_output_type;
1909 case TCC_OPTION_soname:
1910 s->soname = tcc_strdup(optarg);
1911 break;
1912 case TCC_OPTION_o:
1913 if (s->outfile) {
1914 tcc_warning("multiple -o option");
1915 tcc_free(s->outfile);
1917 s->outfile = tcc_strdup(optarg);
1918 break;
1919 case TCC_OPTION_r:
1920 /* generate a .o merging several output files */
1921 s->option_r = 1;
1922 x = TCC_OUTPUT_OBJ;
1923 goto set_output_type;
1924 case TCC_OPTION_isystem:
1925 tcc_add_sysinclude_path(s, optarg);
1926 break;
1927 case TCC_OPTION_include:
1928 cstr_printf(&s->cmdline_defs, "#include \"%s\"\n", optarg);
1929 break;
1930 case TCC_OPTION_nostdinc:
1931 s->nostdinc = 1;
1932 break;
1933 case TCC_OPTION_nostdlib:
1934 s->nostdlib = 1;
1935 break;
1936 case TCC_OPTION_run:
1937 #ifndef TCC_IS_NATIVE
1938 tcc_error("-run is not available in a cross compiler");
1939 #endif
1940 run = optarg;
1941 x = TCC_OUTPUT_MEMORY;
1942 goto set_output_type;
1943 case TCC_OPTION_v:
1944 do ++s->verbose; while (*optarg++ == 'v');
1945 ++noaction;
1946 break;
1947 case TCC_OPTION_f:
1948 if (set_flag(s, options_f, optarg) < 0)
1949 goto unsupported_option;
1950 break;
1951 #ifdef TCC_TARGET_ARM
1952 case TCC_OPTION_mfloat_abi:
1953 /* tcc doesn't support soft float yet */
1954 if (!strcmp(optarg, "softfp")) {
1955 s->float_abi = ARM_SOFTFP_FLOAT;
1956 tcc_undefine_symbol(s, "__ARM_PCS_VFP");
1957 } else if (!strcmp(optarg, "hard"))
1958 s->float_abi = ARM_HARD_FLOAT;
1959 else
1960 tcc_error("unsupported float abi '%s'", optarg);
1961 break;
1962 #endif
1963 case TCC_OPTION_m:
1964 if (set_flag(s, options_m, optarg) < 0) {
1965 if (x = atoi(optarg), x != 32 && x != 64)
1966 goto unsupported_option;
1967 if (PTR_SIZE != x/8)
1968 return x;
1969 ++noaction;
1971 break;
1972 case TCC_OPTION_W:
1973 if (set_flag(s, options_W, optarg) < 0)
1974 goto unsupported_option;
1975 break;
1976 case TCC_OPTION_w:
1977 s->warn_none = 1;
1978 break;
1979 case TCC_OPTION_rdynamic:
1980 s->rdynamic = 1;
1981 break;
1982 case TCC_OPTION_Wl:
1983 if (linker_arg.size)
1984 --linker_arg.size, cstr_ccat(&linker_arg, ',');
1985 cstr_cat(&linker_arg, optarg, 0);
1986 if (tcc_set_linker(s, linker_arg.data))
1987 cstr_free(&linker_arg);
1988 break;
1989 case TCC_OPTION_Wp:
1990 r = optarg;
1991 goto reparse;
1992 case TCC_OPTION_E:
1993 x = TCC_OUTPUT_PREPROCESS;
1994 goto set_output_type;
1995 case TCC_OPTION_P:
1996 s->Pflag = atoi(optarg) + 1;
1997 break;
1998 case TCC_OPTION_MD:
1999 s->gen_deps = 1;
2000 break;
2001 case TCC_OPTION_MF:
2002 s->deps_outfile = tcc_strdup(optarg);
2003 break;
2004 case TCC_OPTION_dumpversion:
2005 printf ("%s\n", TCC_VERSION);
2006 exit(0);
2007 break;
2008 case TCC_OPTION_x:
2009 x = 0;
2010 if (*optarg == 'c')
2011 x = AFF_TYPE_C;
2012 else if (*optarg == 'a')
2013 x = AFF_TYPE_ASMPP;
2014 else if (*optarg == 'b')
2015 x = AFF_TYPE_BIN;
2016 else if (*optarg == 'n')
2017 x = AFF_TYPE_NONE;
2018 else
2019 tcc_warning("unsupported language '%s'", optarg);
2020 s->filetype = x | (s->filetype & ~AFF_TYPE_MASK);
2021 break;
2022 case TCC_OPTION_O:
2023 last_o = atoi(optarg);
2024 break;
2025 case TCC_OPTION_print_search_dirs:
2026 x = OPT_PRINT_DIRS;
2027 goto extra_action;
2028 case TCC_OPTION_impdef:
2029 x = OPT_IMPDEF;
2030 goto extra_action;
2031 case TCC_OPTION_ar:
2032 x = OPT_AR;
2033 extra_action:
2034 arg_start = optind - 1;
2035 if (arg_start != noaction)
2036 tcc_error("cannot parse %s here", r);
2037 tool = x;
2038 break;
2039 case TCC_OPTION_traditional:
2040 case TCC_OPTION_pedantic:
2041 case TCC_OPTION_pipe:
2042 case TCC_OPTION_s:
2043 /* ignored */
2044 break;
2045 default:
2046 unsupported_option:
2047 if (s->warn_unsupported)
2048 tcc_warning("unsupported option '%s'", r);
2049 break;
2052 if (last_o > 0)
2053 tcc_define_symbol(s, "__OPTIMIZE__", NULL);
2054 if (linker_arg.size) {
2055 r = linker_arg.data;
2056 goto arg_err;
2058 *pargc = argc - arg_start;
2059 *pargv = argv + arg_start;
2060 if (tool)
2061 return tool;
2062 if (optind != noaction)
2063 return 0;
2064 if (s->verbose == 2)
2065 return OPT_PRINT_DIRS;
2066 if (s->verbose)
2067 return OPT_V;
2068 return OPT_HELP;
2071 LIBTCCAPI void tcc_set_options(TCCState *s, const char *r)
2073 char **argv = NULL;
2074 int argc = 0;
2075 args_parser_make_argv(r, &argc, &argv);
2076 tcc_parse_args(s, &argc, &argv, 0);
2077 dynarray_reset(&argv, &argc);
2080 PUB_FUNC void tcc_print_stats(TCCState *s, unsigned total_time)
2082 if (total_time < 1)
2083 total_time = 1;
2084 if (total_bytes < 1)
2085 total_bytes = 1;
2086 fprintf(stderr, "* %d idents, %d lines, %d bytes\n"
2087 "* %0.3f s, %u lines/s, %0.1f MB/s\n",
2088 tok_ident - TOK_IDENT, total_lines, total_bytes,
2089 (double)total_time/1000,
2090 (unsigned)total_lines*1000/total_time,
2091 (double)total_bytes/1000/total_time);
2092 #ifdef MEM_DEBUG
2093 fprintf(stderr, "* %d bytes memory used\n", mem_max_size);
2094 #endif