macos: add __builtin_flt_rounds. Forced to 1 which means 'to nearest'
[tinycc.git] / libtcc.c
blob17dd33f626c02aa5fa2dd4efa1161dfc6edf2c4b
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 #include "arm-asm.c"
38 #elif defined(TCC_TARGET_C67)
39 #include "c67-gen.c"
40 #include "c67-link.c"
41 #include "tcccoff.c"
42 #elif defined(TCC_TARGET_X86_64)
43 #include "x86_64-gen.c"
44 #include "x86_64-link.c"
45 #include "i386-asm.c"
46 #elif defined(TCC_TARGET_RISCV64)
47 #include "riscv64-gen.c"
48 #include "riscv64-link.c"
49 #include "riscv64-asm.c"
50 #else
51 #error unknown target
52 #endif
53 #ifdef CONFIG_TCC_ASM
54 #include "tccasm.c"
55 #endif
56 #ifdef TCC_TARGET_PE
57 #include "tccpe.c"
58 #endif
59 #ifdef TCC_TARGET_MACHO
60 #include "tccmacho.c"
61 #endif
62 #endif /* ONE_SOURCE */
64 #include "tcc.h"
66 /********************************************************/
67 /* global variables */
69 /* XXX: get rid of this ASAP (or maybe not) */
70 ST_DATA struct TCCState *tcc_state;
72 #ifdef MEM_DEBUG
73 static int nb_states;
74 #endif
76 /********************************************************/
77 #ifdef _WIN32
78 ST_FUNC char *normalize_slashes(char *path)
80 char *p;
81 for (p = path; *p; ++p)
82 if (*p == '\\')
83 *p = '/';
84 return path;
87 static HMODULE tcc_module;
89 /* on win32, we suppose the lib and includes are at the location of 'tcc.exe' */
90 static void tcc_set_lib_path_w32(TCCState *s)
92 char path[1024], *p;
93 GetModuleFileNameA(tcc_module, path, sizeof path);
94 p = tcc_basename(normalize_slashes(strlwr(path)));
95 if (p > path)
96 --p;
97 *p = 0;
98 tcc_set_lib_path(s, path);
101 #ifdef TCC_TARGET_PE
102 static void tcc_add_systemdir(TCCState *s)
104 char buf[1000];
105 GetSystemDirectory(buf, sizeof buf);
106 tcc_add_library_path(s, normalize_slashes(buf));
108 #endif
110 #ifdef LIBTCC_AS_DLL
111 BOOL WINAPI DllMain (HINSTANCE hDll, DWORD dwReason, LPVOID lpReserved)
113 if (DLL_PROCESS_ATTACH == dwReason)
114 tcc_module = hDll;
115 return TRUE;
117 #endif
118 #endif
120 /********************************************************/
121 #ifndef CONFIG_TCC_SEMLOCK
122 #define WAIT_SEM()
123 #define POST_SEM()
124 #elif defined _WIN32
125 static int tcc_sem_init;
126 static CRITICAL_SECTION tcc_cr;
127 static void wait_sem(void)
129 if (!tcc_sem_init)
130 InitializeCriticalSection(&tcc_cr), tcc_sem_init = 1;
131 EnterCriticalSection(&tcc_cr);
133 #define WAIT_SEM() wait_sem()
134 #define POST_SEM() LeaveCriticalSection(&tcc_cr);
135 #elif defined __APPLE__
136 /* Half-compatible MacOS doesn't have non-shared (process local)
137 semaphores. Use the dispatch framework for lightweight locks. */
138 #include <dispatch/dispatch.h>
139 static int tcc_sem_init;
140 static dispatch_semaphore_t tcc_sem;
141 static void wait_sem(void)
143 if (!tcc_sem_init)
144 tcc_sem = dispatch_semaphore_create(1), tcc_sem_init = 1;
145 dispatch_semaphore_wait(tcc_sem, DISPATCH_TIME_FOREVER);
147 #define WAIT_SEM() wait_sem()
148 #define POST_SEM() dispatch_semaphore_signal(tcc_sem)
149 #else
150 #include <semaphore.h>
151 static int tcc_sem_init;
152 static sem_t tcc_sem;
153 static void wait_sem(void)
155 if (!tcc_sem_init)
156 sem_init(&tcc_sem, 0, 1), tcc_sem_init = 1;
157 while (sem_wait (&tcc_sem) < 0 && errno == EINTR);
159 #define WAIT_SEM() wait_sem()
160 #define POST_SEM() sem_post(&tcc_sem)
161 #endif
163 /********************************************************/
164 /* copy a string and truncate it. */
165 ST_FUNC char *pstrcpy(char *buf, size_t buf_size, const char *s)
167 char *q, *q_end;
168 int c;
170 if (buf_size > 0) {
171 q = buf;
172 q_end = buf + buf_size - 1;
173 while (q < q_end) {
174 c = *s++;
175 if (c == '\0')
176 break;
177 *q++ = c;
179 *q = '\0';
181 return buf;
184 /* strcat and truncate. */
185 ST_FUNC char *pstrcat(char *buf, size_t buf_size, const char *s)
187 size_t len;
188 len = strlen(buf);
189 if (len < buf_size)
190 pstrcpy(buf + len, buf_size - len, s);
191 return buf;
194 ST_FUNC char *pstrncpy(char *out, const char *in, size_t num)
196 memcpy(out, in, num);
197 out[num] = '\0';
198 return out;
201 /* extract the basename of a file */
202 PUB_FUNC char *tcc_basename(const char *name)
204 char *p = strchr(name, 0);
205 while (p > name && !IS_DIRSEP(p[-1]))
206 --p;
207 return p;
210 /* extract extension part of a file
212 * (if no extension, return pointer to end-of-string)
214 PUB_FUNC char *tcc_fileextension (const char *name)
216 char *b = tcc_basename(name);
217 char *e = strrchr(b, '.');
218 return e ? e : strchr(b, 0);
221 /********************************************************/
222 /* memory management */
224 #undef free
225 #undef malloc
226 #undef realloc
228 #ifndef MEM_DEBUG
230 PUB_FUNC void tcc_free(void *ptr)
232 free(ptr);
235 PUB_FUNC void *tcc_malloc(unsigned long size)
237 void *ptr;
238 ptr = malloc(size);
239 if (!ptr && size)
240 _tcc_error("memory full (malloc)");
241 return ptr;
244 PUB_FUNC void *tcc_mallocz(unsigned long size)
246 void *ptr;
247 ptr = tcc_malloc(size);
248 memset(ptr, 0, size);
249 return ptr;
252 PUB_FUNC void *tcc_realloc(void *ptr, unsigned long size)
254 void *ptr1;
255 ptr1 = realloc(ptr, size);
256 if (!ptr1 && size)
257 _tcc_error("memory full (realloc)");
258 return ptr1;
261 PUB_FUNC char *tcc_strdup(const char *str)
263 char *ptr;
264 ptr = tcc_malloc(strlen(str) + 1);
265 strcpy(ptr, str);
266 return ptr;
269 #else
271 #define MEM_DEBUG_MAGIC1 0xFEEDDEB1
272 #define MEM_DEBUG_MAGIC2 0xFEEDDEB2
273 #define MEM_DEBUG_MAGIC3 0xFEEDDEB3
274 #define MEM_DEBUG_FILE_LEN 40
275 #define MEM_DEBUG_CHECK3(header) \
276 ((mem_debug_header_t*)((char*)header + header->size))->magic3
277 #define MEM_USER_PTR(header) \
278 ((char *)header + offsetof(mem_debug_header_t, magic3))
279 #define MEM_HEADER_PTR(ptr) \
280 (mem_debug_header_t *)((char*)ptr - offsetof(mem_debug_header_t, magic3))
282 struct mem_debug_header {
283 unsigned magic1;
284 unsigned size;
285 struct mem_debug_header *prev;
286 struct mem_debug_header *next;
287 int line_num;
288 char file_name[MEM_DEBUG_FILE_LEN + 1];
289 unsigned magic2;
290 ALIGNED(16) unsigned magic3;
293 typedef struct mem_debug_header mem_debug_header_t;
295 static mem_debug_header_t *mem_debug_chain;
296 static unsigned mem_cur_size;
297 static unsigned mem_max_size;
299 static mem_debug_header_t *malloc_check(void *ptr, const char *msg)
301 mem_debug_header_t * header = MEM_HEADER_PTR(ptr);
302 if (header->magic1 != MEM_DEBUG_MAGIC1 ||
303 header->magic2 != MEM_DEBUG_MAGIC2 ||
304 MEM_DEBUG_CHECK3(header) != MEM_DEBUG_MAGIC3 ||
305 header->size == (unsigned)-1) {
306 fprintf(stderr, "%s check failed\n", msg);
307 if (header->magic1 == MEM_DEBUG_MAGIC1)
308 fprintf(stderr, "%s:%u: block allocated here.\n",
309 header->file_name, header->line_num);
310 exit(1);
312 return header;
315 PUB_FUNC void *tcc_malloc_debug(unsigned long size, const char *file, int line)
317 int ofs;
318 mem_debug_header_t *header;
320 header = malloc(sizeof(mem_debug_header_t) + size);
321 if (!header)
322 _tcc_error("memory full (malloc)");
324 header->magic1 = MEM_DEBUG_MAGIC1;
325 header->magic2 = MEM_DEBUG_MAGIC2;
326 header->size = size;
327 MEM_DEBUG_CHECK3(header) = MEM_DEBUG_MAGIC3;
328 header->line_num = line;
329 ofs = strlen(file) - MEM_DEBUG_FILE_LEN;
330 strncpy(header->file_name, file + (ofs > 0 ? ofs : 0), MEM_DEBUG_FILE_LEN);
331 header->file_name[MEM_DEBUG_FILE_LEN] = 0;
333 header->next = mem_debug_chain;
334 header->prev = NULL;
335 if (header->next)
336 header->next->prev = header;
337 mem_debug_chain = header;
339 mem_cur_size += size;
340 if (mem_cur_size > mem_max_size)
341 mem_max_size = mem_cur_size;
343 return MEM_USER_PTR(header);
346 PUB_FUNC void tcc_free_debug(void *ptr)
348 mem_debug_header_t *header;
349 if (!ptr)
350 return;
351 header = malloc_check(ptr, "tcc_free");
352 mem_cur_size -= header->size;
353 header->size = (unsigned)-1;
354 if (header->next)
355 header->next->prev = header->prev;
356 if (header->prev)
357 header->prev->next = header->next;
358 if (header == mem_debug_chain)
359 mem_debug_chain = header->next;
360 free(header);
363 PUB_FUNC void *tcc_mallocz_debug(unsigned long size, const char *file, int line)
365 void *ptr;
366 ptr = tcc_malloc_debug(size,file,line);
367 memset(ptr, 0, size);
368 return ptr;
371 PUB_FUNC void *tcc_realloc_debug(void *ptr, unsigned long size, const char *file, int line)
373 mem_debug_header_t *header;
374 int mem_debug_chain_update = 0;
375 if (!ptr)
376 return tcc_malloc_debug(size, file, line);
377 header = malloc_check(ptr, "tcc_realloc");
378 mem_cur_size -= header->size;
379 mem_debug_chain_update = (header == mem_debug_chain);
380 header = realloc(header, sizeof(mem_debug_header_t) + size);
381 if (!header)
382 _tcc_error("memory full (realloc)");
383 header->size = size;
384 MEM_DEBUG_CHECK3(header) = MEM_DEBUG_MAGIC3;
385 if (header->next)
386 header->next->prev = header;
387 if (header->prev)
388 header->prev->next = header;
389 if (mem_debug_chain_update)
390 mem_debug_chain = header;
391 mem_cur_size += size;
392 if (mem_cur_size > mem_max_size)
393 mem_max_size = mem_cur_size;
394 return MEM_USER_PTR(header);
397 PUB_FUNC char *tcc_strdup_debug(const char *str, const char *file, int line)
399 char *ptr;
400 ptr = tcc_malloc_debug(strlen(str) + 1, file, line);
401 strcpy(ptr, str);
402 return ptr;
405 PUB_FUNC void tcc_memcheck(void)
407 if (mem_cur_size) {
408 mem_debug_header_t *header = mem_debug_chain;
409 fprintf(stderr, "MEM_DEBUG: mem_leak= %d bytes, mem_max_size= %d bytes\n",
410 mem_cur_size, mem_max_size);
411 while (header) {
412 fprintf(stderr, "%s:%u: error: %u bytes leaked\n",
413 header->file_name, header->line_num, header->size);
414 header = header->next;
416 #if MEM_DEBUG-0 == 2
417 exit(2);
418 #endif
421 #endif /* MEM_DEBUG */
423 #define free(p) use_tcc_free(p)
424 #define malloc(s) use_tcc_malloc(s)
425 #define realloc(p, s) use_tcc_realloc(p, s)
427 /********************************************************/
428 /* dynarrays */
430 ST_FUNC void dynarray_add(void *ptab, int *nb_ptr, void *data)
432 int nb, nb_alloc;
433 void **pp;
435 nb = *nb_ptr;
436 pp = *(void ***)ptab;
437 /* every power of two we double array size */
438 if ((nb & (nb - 1)) == 0) {
439 if (!nb)
440 nb_alloc = 1;
441 else
442 nb_alloc = nb * 2;
443 pp = tcc_realloc(pp, nb_alloc * sizeof(void *));
444 *(void***)ptab = pp;
446 pp[nb++] = data;
447 *nb_ptr = nb;
450 ST_FUNC void dynarray_reset(void *pp, int *n)
452 void **p;
453 for (p = *(void***)pp; *n; ++p, --*n)
454 if (*p)
455 tcc_free(*p);
456 tcc_free(*(void**)pp);
457 *(void**)pp = NULL;
460 static void tcc_split_path(TCCState *s, void *p_ary, int *p_nb_ary, const char *in)
462 const char *p;
463 do {
464 int c;
465 CString str;
467 cstr_new(&str);
468 for (p = in; c = *p, c != '\0' && c != PATHSEP[0]; ++p) {
469 if (c == '{' && p[1] && p[2] == '}') {
470 c = p[1], p += 2;
471 if (c == 'B')
472 cstr_cat(&str, s->tcc_lib_path, -1);
473 if (c == 'f' && file) {
474 /* substitute current file's dir */
475 const char *f = file->true_filename;
476 const char *b = tcc_basename(f);
477 if (b > f)
478 cstr_cat(&str, f, b - f - 1);
479 else
480 cstr_cat(&str, ".", 1);
482 } else {
483 cstr_ccat(&str, c);
486 if (str.size) {
487 cstr_ccat(&str, '\0');
488 dynarray_add(p_ary, p_nb_ary, tcc_strdup(str.data));
490 cstr_free(&str);
491 in = p+1;
492 } while (*p);
495 /********************************************************/
497 static void strcat_vprintf(char *buf, int buf_size, const char *fmt, va_list ap)
499 int len;
500 len = strlen(buf);
501 vsnprintf(buf + len, buf_size - len, fmt, ap);
504 static void strcat_printf(char *buf, int buf_size, const char *fmt, ...)
506 va_list ap;
507 va_start(ap, fmt);
508 strcat_vprintf(buf, buf_size, fmt, ap);
509 va_end(ap);
512 #define ERROR_WARN 0
513 #define ERROR_NOABORT 1
514 #define ERROR_ERROR 2
516 PUB_FUNC void tcc_enter_state(TCCState *s1)
518 WAIT_SEM();
519 tcc_state = s1;
522 PUB_FUNC void tcc_exit_state(void)
524 tcc_state = NULL;
525 POST_SEM();
528 static void error1(int mode, const char *fmt, va_list ap)
530 char buf[2048];
531 BufferedFile **pf, *f;
532 TCCState *s1 = tcc_state;
534 buf[0] = '\0';
535 if (s1 == NULL)
536 /* can happen only if called from tcc_malloc(): 'out of memory' */
537 goto no_file;
539 if (s1 && !s1->error_set_jmp_enabled)
540 /* tcc_state just was set by tcc_enter_state() */
541 tcc_exit_state();
543 if (mode == ERROR_WARN) {
544 if (s1->warn_none)
545 return;
546 if (s1->warn_error)
547 mode = ERROR_ERROR;
550 f = NULL;
551 if (s1->error_set_jmp_enabled) { /* we're called while parsing a file */
552 /* use upper file if inline ":asm:" or token ":paste:" */
553 for (f = file; f && f->filename[0] == ':'; f = f->prev)
556 if (f) {
557 for(pf = s1->include_stack; pf < s1->include_stack_ptr; pf++)
558 strcat_printf(buf, sizeof(buf), "In file included from %s:%d:\n",
559 (*pf)->filename, (*pf)->line_num);
560 strcat_printf(buf, sizeof(buf), "%s:%d: ",
561 f->filename, f->line_num - !!(tok_flags & TOK_FLAG_BOL));
562 } else if (s1->current_filename) {
563 strcat_printf(buf, sizeof(buf), "%s: ", s1->current_filename);
566 no_file:
567 if (0 == buf[0])
568 strcat_printf(buf, sizeof(buf), "tcc: ");
569 if (mode == ERROR_WARN)
570 strcat_printf(buf, sizeof(buf), "warning: ");
571 else
572 strcat_printf(buf, sizeof(buf), "error: ");
573 strcat_vprintf(buf, sizeof(buf), fmt, ap);
574 if (!s1 || !s1->error_func) {
575 /* default case: stderr */
576 if (s1 && s1->output_type == TCC_OUTPUT_PREPROCESS && s1->ppfp == stdout)
577 /* print a newline during tcc -E */
578 printf("\n"), fflush(stdout);
579 fflush(stdout); /* flush -v output */
580 fprintf(stderr, "%s\n", buf);
581 fflush(stderr); /* print error/warning now (win32) */
582 } else {
583 s1->error_func(s1->error_opaque, buf);
585 if (s1) {
586 if (mode != ERROR_WARN)
587 s1->nb_errors++;
588 if (mode != ERROR_ERROR)
589 return;
590 if (s1->error_set_jmp_enabled)
591 longjmp(s1->error_jmp_buf, 1);
593 exit(1);
596 LIBTCCAPI void tcc_set_error_func(TCCState *s, void *error_opaque, TCCErrorFunc error_func)
598 s->error_opaque = error_opaque;
599 s->error_func = error_func;
602 LIBTCCAPI TCCErrorFunc tcc_get_error_func(TCCState *s)
604 return s->error_func;
607 LIBTCCAPI void *tcc_get_error_opaque(TCCState *s)
609 return s->error_opaque;
612 /* error without aborting current compilation */
613 PUB_FUNC void _tcc_error_noabort(const char *fmt, ...)
615 va_list ap;
616 va_start(ap, fmt);
617 error1(ERROR_NOABORT, fmt, ap);
618 va_end(ap);
621 PUB_FUNC void _tcc_error(const char *fmt, ...)
623 va_list ap;
624 va_start(ap, fmt);
625 for (;;) error1(ERROR_ERROR, fmt, ap);
628 PUB_FUNC void _tcc_warning(const char *fmt, ...)
630 va_list ap;
631 va_start(ap, fmt);
632 error1(ERROR_WARN, fmt, ap);
633 va_end(ap);
636 /********************************************************/
637 /* I/O layer */
639 ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen)
641 BufferedFile *bf;
642 int buflen = initlen ? initlen : IO_BUF_SIZE;
644 bf = tcc_mallocz(sizeof(BufferedFile) + buflen);
645 bf->buf_ptr = bf->buffer;
646 bf->buf_end = bf->buffer + initlen;
647 bf->buf_end[0] = CH_EOB; /* put eob symbol */
648 pstrcpy(bf->filename, sizeof(bf->filename), filename);
649 #ifdef _WIN32
650 normalize_slashes(bf->filename);
651 #endif
652 bf->true_filename = bf->filename;
653 bf->line_num = 1;
654 bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
655 bf->fd = -1;
656 bf->prev = file;
657 file = bf;
658 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
661 ST_FUNC void tcc_close(void)
663 TCCState *s1 = tcc_state;
664 BufferedFile *bf = file;
665 if (bf->fd > 0) {
666 close(bf->fd);
667 total_lines += bf->line_num;
669 if (bf->true_filename != bf->filename)
670 tcc_free(bf->true_filename);
671 file = bf->prev;
672 tcc_free(bf);
675 static int _tcc_open(TCCState *s1, const char *filename)
677 int fd;
678 if (strcmp(filename, "-") == 0)
679 fd = 0, filename = "<stdin>";
680 else
681 fd = open(filename, O_RDONLY | O_BINARY);
682 if ((s1->verbose == 2 && fd >= 0) || s1->verbose == 3)
683 printf("%s %*s%s\n", fd < 0 ? "nf":"->",
684 (int)(s1->include_stack_ptr - s1->include_stack), "", filename);
685 return fd;
688 ST_FUNC int tcc_open(TCCState *s1, const char *filename)
690 int fd = _tcc_open(s1, filename);
691 if (fd < 0)
692 return -1;
693 tcc_open_bf(s1, filename, 0);
694 file->fd = fd;
695 return 0;
698 /* compile the file opened in 'file'. Return non zero if errors. */
699 static int tcc_compile(TCCState *s1, int filetype, const char *str, int fd)
701 /* Here we enter the code section where we use the global variables for
702 parsing and code generation (tccpp.c, tccgen.c, <target>-gen.c).
703 Other threads need to wait until we're done.
705 Alternatively we could use thread local storage for those global
706 variables, which may or may not have advantages */
708 tcc_enter_state(s1);
710 if (setjmp(s1->error_jmp_buf) == 0) {
711 int is_asm;
712 s1->error_set_jmp_enabled = 1;
713 s1->nb_errors = 0;
715 if (fd == -1) {
716 int len = strlen(str);
717 tcc_open_bf(s1, "<string>", len);
718 memcpy(file->buffer, str, len);
719 } else {
720 tcc_open_bf(s1, str, 0);
721 file->fd = fd;
724 is_asm = !!(filetype & (AFF_TYPE_ASM|AFF_TYPE_ASMPP));
725 tccelf_begin_file(s1);
726 preprocess_start(s1, is_asm);
727 tccgen_init(s1);
728 if (s1->output_type == TCC_OUTPUT_PREPROCESS) {
729 tcc_preprocess(s1);
730 } else if (is_asm) {
731 #ifdef CONFIG_TCC_ASM
732 tcc_assemble(s1, !!(filetype & AFF_TYPE_ASMPP));
733 #else
734 tcc_error_noabort("asm not supported");
735 #endif
736 } else {
737 tccgen_compile(s1);
740 s1->error_set_jmp_enabled = 0;
741 tccgen_finish(s1);
742 preprocess_end(s1);
743 tcc_exit_state();
745 tccelf_end_file(s1);
746 return s1->nb_errors != 0 ? -1 : 0;
749 LIBTCCAPI int tcc_compile_string(TCCState *s, const char *str)
751 return tcc_compile(s, s->filetype, str, -1);
754 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
755 LIBTCCAPI void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
757 if (!value)
758 value = "1";
759 cstr_printf(&s1->cmdline_defs, "#define %s %s\n", sym, value);
762 /* undefine a preprocessor symbol */
763 LIBTCCAPI void tcc_undefine_symbol(TCCState *s1, const char *sym)
765 cstr_printf(&s1->cmdline_defs, "#undef %s\n", sym);
769 LIBTCCAPI TCCState *tcc_new(void)
771 TCCState *s;
773 s = tcc_mallocz(sizeof(TCCState));
774 if (!s)
775 return NULL;
776 #ifdef MEM_DEBUG
777 ++nb_states;
778 #endif
780 #undef gnu_ext
782 s->gnu_ext = 1;
783 s->tcc_ext = 1;
784 s->nocommon = 1;
785 s->dollars_in_identifiers = 1; /*on by default like in gcc/clang*/
786 s->cversion = 199901; /* default unless -std=c11 is supplied */
787 s->warn_implicit_function_declaration = 1;
788 s->ms_extensions = 1;
790 #ifdef CHAR_IS_UNSIGNED
791 s->char_is_unsigned = 1;
792 #endif
793 #ifdef TCC_TARGET_I386
794 s->seg_size = 32;
795 #endif
796 /* enable this if you want symbols with leading underscore on windows: */
797 #if defined TCC_TARGET_MACHO /* || defined TCC_TARGET_PE */
798 s->leading_underscore = 1;
799 #endif
800 s->ppfp = stdout;
801 /* might be used in error() before preprocess_start() */
802 s->include_stack_ptr = s->include_stack;
804 tccelf_new(s);
806 #ifdef _WIN32
807 tcc_set_lib_path_w32(s);
808 #else
809 tcc_set_lib_path(s, CONFIG_TCCDIR);
810 #endif
813 /* define __TINYC__ 92X */
814 char buffer[32]; int a,b,c;
815 sscanf(TCC_VERSION, "%d.%d.%d", &a, &b, &c);
816 sprintf(buffer, "%d", a*10000 + b*100 + c);
817 tcc_define_symbol(s, "__TINYC__", buffer);
820 /* standard defines */
821 tcc_define_symbol(s, "__STDC__", NULL);
822 tcc_define_symbol(s, "__STDC_VERSION__", "199901L");
823 tcc_define_symbol(s, "__STDC_HOSTED__", NULL);
825 /* target defines */
826 #if defined(TCC_TARGET_I386)
827 tcc_define_symbol(s, "__i386__", NULL);
828 tcc_define_symbol(s, "__i386", NULL);
829 tcc_define_symbol(s, "i386", NULL);
830 #elif defined(TCC_TARGET_X86_64)
831 tcc_define_symbol(s, "__x86_64__", NULL);
832 #elif defined(TCC_TARGET_ARM)
833 tcc_define_symbol(s, "__ARM_ARCH_4__", NULL);
834 tcc_define_symbol(s, "__arm_elf__", NULL);
835 tcc_define_symbol(s, "__arm_elf", NULL);
836 tcc_define_symbol(s, "arm_elf", NULL);
837 tcc_define_symbol(s, "__arm__", NULL);
838 tcc_define_symbol(s, "__arm", NULL);
839 tcc_define_symbol(s, "arm", NULL);
840 tcc_define_symbol(s, "__APCS_32__", NULL);
841 tcc_define_symbol(s, "__ARMEL__", NULL);
842 #if defined(TCC_ARM_EABI)
843 tcc_define_symbol(s, "__ARM_EABI__", NULL);
844 #endif
845 #if defined(TCC_ARM_HARDFLOAT)
846 s->float_abi = ARM_HARD_FLOAT;
847 tcc_define_symbol(s, "__ARM_PCS_VFP", NULL);
848 #else
849 s->float_abi = ARM_SOFTFP_FLOAT;
850 #endif
851 #elif defined(TCC_TARGET_ARM64)
852 tcc_define_symbol(s, "__aarch64__", NULL);
853 #elif defined TCC_TARGET_C67
854 tcc_define_symbol(s, "__C67__", NULL);
855 #elif defined TCC_TARGET_RISCV64
856 tcc_define_symbol(s, "__riscv", NULL);
857 tcc_define_symbol(s, "__riscv_xlen", "64");
858 tcc_define_symbol(s, "__riscv_flen", "64");
859 tcc_define_symbol(s, "__riscv_div", NULL);
860 tcc_define_symbol(s, "__riscv_mul", NULL);
861 tcc_define_symbol(s, "__riscv_fdiv", NULL);
862 tcc_define_symbol(s, "__riscv_fsqrt", NULL);
863 tcc_define_symbol(s, "__riscv_float_abi_double", NULL);
864 #endif
866 #ifdef TCC_TARGET_PE
867 tcc_define_symbol(s, "_WIN32", NULL);
868 tcc_define_symbol(s, "__declspec(x)", "__attribute__((x))");
869 tcc_define_symbol(s, "__cdecl", "");
870 # ifdef TCC_TARGET_X86_64
871 tcc_define_symbol(s, "_WIN64", NULL);
872 # endif
873 #else
874 tcc_define_symbol(s, "__unix__", NULL);
875 tcc_define_symbol(s, "__unix", NULL);
876 tcc_define_symbol(s, "unix", NULL);
877 # if defined(__linux__)
878 tcc_define_symbol(s, "__linux__", NULL);
879 tcc_define_symbol(s, "__linux", NULL);
880 # endif
881 # if defined(__FreeBSD__)
882 tcc_define_symbol(s, "__FreeBSD__", "__FreeBSD__");
883 /* No 'Thread Storage Local' on FreeBSD with tcc */
884 tcc_define_symbol(s, "__NO_TLS", NULL);
885 # endif
886 # if defined(__FreeBSD_kernel__)
887 tcc_define_symbol(s, "__FreeBSD_kernel__", NULL);
888 # endif
889 # if defined(__NetBSD__)
890 tcc_define_symbol(s, "__NetBSD__", "__NetBSD__");
891 # endif
892 # if defined(__OpenBSD__)
893 tcc_define_symbol(s, "__OpenBSD__", "__OpenBSD__");
894 # endif
895 #endif
897 /* TinyCC & gcc defines */
898 #if PTR_SIZE == 4
899 /* 32bit systems. */
900 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned int");
901 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "int");
902 tcc_define_symbol(s, "__ILP32__", NULL);
903 #elif LONG_SIZE == 4
904 /* 64bit Windows. */
905 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned long long");
906 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "long long");
907 tcc_define_symbol(s, "__LLP64__", NULL);
908 #else
909 /* Other 64bit systems. */
910 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned long");
911 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "long");
912 tcc_define_symbol(s, "__LP64__", NULL);
913 #endif
914 tcc_define_symbol(s, "__SIZEOF_POINTER__", PTR_SIZE == 4 ? "4" : "8");
916 #ifdef TCC_TARGET_PE
917 tcc_define_symbol(s, "__WCHAR_TYPE__", "unsigned short");
918 tcc_define_symbol(s, "__WINT_TYPE__", "unsigned short");
919 #else
920 tcc_define_symbol(s, "__WCHAR_TYPE__", "int");
921 /* wint_t is unsigned int by default, but (signed) int on BSDs
922 and unsigned short on windows. Other OSes might have still
923 other conventions, sigh. */
924 # if defined(__FreeBSD__) || defined (__FreeBSD_kernel__) \
925 || defined(__NetBSD__) || defined(__OpenBSD__)
926 tcc_define_symbol(s, "__WINT_TYPE__", "int");
927 # ifdef __FreeBSD__
928 /* define __GNUC__ to have some useful stuff from sys/cdefs.h
929 that are unconditionally used in FreeBSDs other system headers :/ */
930 tcc_define_symbol(s, "__GNUC__", "2");
931 tcc_define_symbol(s, "__GNUC_MINOR__", "7");
932 tcc_define_symbol(s, "__builtin_alloca", "alloca");
933 # endif
934 # else
935 tcc_define_symbol(s, "__WINT_TYPE__", "unsigned int");
936 /* glibc defines */
937 tcc_define_symbol(s, "__REDIRECT(name, proto, alias)",
938 "name proto __asm__ (#alias)");
939 tcc_define_symbol(s, "__REDIRECT_NTH(name, proto, alias)",
940 "name proto __asm__ (#alias) __THROW");
941 # endif
942 /* Some GCC builtins that are simple to express as macros. */
943 tcc_define_symbol(s, "__builtin_extract_return_addr(x)", "x");
944 #endif /* ndef TCC_TARGET_PE */
945 #ifdef TCC_TARGET_MACHO
946 /* emulate APPLE-GCC to make libc's headerfiles compile: */
947 tcc_define_symbol(s, "__APPLE__", "1");
948 tcc_define_symbol(s, "__GNUC__", "4"); /* darwin emits warning on GCC<4 */
949 tcc_define_symbol(s, "__APPLE_CC__", "1"); /* for <TargetConditionals.h> */
950 tcc_define_symbol(s, "_DONT_USE_CTYPE_INLINE_", "1");
951 tcc_define_symbol(s, "__builtin_alloca", "alloca"); /* as we claim GNUC */
952 /* used by math.h */
953 tcc_define_symbol(s, "__builtin_huge_val()", "1e500");
954 tcc_define_symbol(s, "__builtin_huge_valf()", "1e50f");
955 tcc_define_symbol(s, "__builtin_huge_vall()", "1e5000L");
956 tcc_define_symbol(s, "__builtin_nanf(ignored_string)", "__nan()");
957 /* used by _fd_def.h */
958 tcc_define_symbol(s, "__builtin_bzero(p, ignored_size)", "bzero(p, sizeof(*(p)))");
959 /* used by floats.h to implement FLT_ROUNDS C99 macro. 1 == to nearest */
960 tcc_define_symbol(s, "__builtin_flt_rounds()", "1");
962 /* avoids usage of GCC/clang specific builtins in libc-headerfiles: */
963 tcc_define_symbol(s, "__FINITE_MATH_ONLY__", "1");
964 tcc_define_symbol(s, "_FORTIFY_SOURCE", "0");
965 #endif /* ndef TCC_TARGET_MACHO */
966 return s;
969 LIBTCCAPI void tcc_delete(TCCState *s1)
971 /* free sections */
972 tccelf_delete(s1);
974 /* free library paths */
975 dynarray_reset(&s1->library_paths, &s1->nb_library_paths);
976 dynarray_reset(&s1->crt_paths, &s1->nb_crt_paths);
978 /* free include paths */
979 dynarray_reset(&s1->include_paths, &s1->nb_include_paths);
980 dynarray_reset(&s1->sysinclude_paths, &s1->nb_sysinclude_paths);
982 tcc_free(s1->tcc_lib_path);
983 tcc_free(s1->soname);
984 tcc_free(s1->rpath);
985 tcc_free(s1->init_symbol);
986 tcc_free(s1->fini_symbol);
987 tcc_free(s1->outfile);
988 tcc_free(s1->deps_outfile);
989 dynarray_reset(&s1->files, &s1->nb_files);
990 dynarray_reset(&s1->target_deps, &s1->nb_target_deps);
991 dynarray_reset(&s1->pragma_libs, &s1->nb_pragma_libs);
992 dynarray_reset(&s1->argv, &s1->argc);
994 cstr_free(&s1->cmdline_defs);
995 cstr_free(&s1->cmdline_incl);
996 #ifdef TCC_IS_NATIVE
997 /* free runtime memory */
998 tcc_run_free(s1);
999 #endif
1001 tcc_free(s1);
1002 #ifdef MEM_DEBUG
1003 if (0 == --nb_states)
1004 tcc_memcheck();
1005 #endif
1008 LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type)
1010 s->output_type = output_type;
1012 /* always elf for objects */
1013 if (output_type == TCC_OUTPUT_OBJ)
1014 s->output_format = TCC_OUTPUT_FORMAT_ELF;
1016 if (s->char_is_unsigned)
1017 tcc_define_symbol(s, "__CHAR_UNSIGNED__", NULL);
1019 if (s->cversion == 201112) {
1020 tcc_undefine_symbol(s, "__STDC_VERSION__");
1021 tcc_define_symbol(s, "__STDC_VERSION__", "201112L");
1022 tcc_define_symbol(s, "__STDC_NO_ATOMICS__", NULL);
1023 tcc_define_symbol(s, "__STDC_NO_COMPLEX__", NULL);
1024 tcc_define_symbol(s, "__STDC_NO_THREADS__", NULL);
1025 #ifndef TCC_TARGET_PE
1026 /* on Linux, this conflicts with a define introduced by
1027 /usr/include/stdc-predef.h included by glibc libs
1028 tcc_define_symbol(s, "__STDC_ISO_10646__", "201605L"); */
1029 tcc_define_symbol(s, "__STDC_UTF_16__", NULL);
1030 tcc_define_symbol(s, "__STDC_UTF_32__", NULL);
1031 #endif
1034 if (s->optimize > 0)
1035 tcc_define_symbol(s, "__OPTIMIZE__", NULL);
1037 if (s->option_pthread)
1038 tcc_define_symbol(s, "_REENTRANT", NULL);
1040 if (!s->nostdinc) {
1041 /* default include paths */
1042 /* -isystem paths have already been handled */
1043 tcc_add_sysinclude_path(s, CONFIG_TCC_SYSINCLUDEPATHS);
1046 #ifdef CONFIG_TCC_BCHECK
1047 if (s->do_bounds_check) {
1048 /* if bound checking, then add corresponding sections */
1049 tccelf_bounds_new(s);
1050 /* define symbol */
1051 tcc_define_symbol(s, "__BOUNDS_CHECKING_ON", NULL);
1053 #endif
1054 if (s->do_debug) {
1055 /* add debug sections */
1056 tccelf_stab_new(s);
1059 tcc_add_library_path(s, CONFIG_TCC_LIBPATHS);
1061 #ifdef TCC_TARGET_PE
1062 # ifdef _WIN32
1063 if (!s->nostdlib && output_type != TCC_OUTPUT_OBJ)
1064 tcc_add_systemdir(s);
1065 # endif
1066 #else
1067 /* paths for crt objects */
1068 tcc_split_path(s, &s->crt_paths, &s->nb_crt_paths, CONFIG_TCC_CRTPREFIX);
1069 /* add libc crt1/crti objects */
1070 if ((output_type == TCC_OUTPUT_EXE || output_type == TCC_OUTPUT_DLL) &&
1071 !s->nostdlib) {
1072 #ifndef TCC_TARGET_MACHO
1073 /* Mach-O with LC_MAIN doesn't need any crt startup code. */
1074 if (output_type != TCC_OUTPUT_DLL)
1075 tcc_add_crt(s, "crt1.o");
1076 tcc_add_crt(s, "crti.o");
1077 #endif
1079 #endif
1080 return 0;
1083 LIBTCCAPI int tcc_add_include_path(TCCState *s, const char *pathname)
1085 tcc_split_path(s, &s->include_paths, &s->nb_include_paths, pathname);
1086 return 0;
1089 LIBTCCAPI int tcc_add_sysinclude_path(TCCState *s, const char *pathname)
1091 tcc_split_path(s, &s->sysinclude_paths, &s->nb_sysinclude_paths, pathname);
1092 return 0;
1095 ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
1097 int fd, ret;
1099 /* open the file */
1100 fd = _tcc_open(s1, filename);
1101 if (fd < 0) {
1102 if (flags & AFF_PRINT_ERROR)
1103 tcc_error_noabort("file '%s' not found", filename);
1104 return -1;
1107 s1->current_filename = filename;
1108 if (flags & AFF_TYPE_BIN) {
1109 ElfW(Ehdr) ehdr;
1110 int obj_type;
1112 obj_type = tcc_object_type(fd, &ehdr);
1113 lseek(fd, 0, SEEK_SET);
1115 #ifdef TCC_TARGET_MACHO
1116 if (0 == obj_type && 0 == strcmp(tcc_fileextension(filename), ".dylib"))
1117 obj_type = AFF_BINTYPE_DYN;
1118 #endif
1120 switch (obj_type) {
1121 case AFF_BINTYPE_REL:
1122 ret = tcc_load_object_file(s1, fd, 0);
1123 break;
1124 #ifndef TCC_TARGET_PE
1125 case AFF_BINTYPE_DYN:
1126 if (s1->output_type == TCC_OUTPUT_MEMORY) {
1127 ret = 0;
1128 #ifdef TCC_IS_NATIVE
1129 if (NULL == dlopen(filename, RTLD_GLOBAL | RTLD_LAZY))
1130 ret = -1;
1131 #endif
1132 } else {
1133 #ifndef TCC_TARGET_MACHO
1134 ret = tcc_load_dll(s1, fd, filename,
1135 (flags & AFF_REFERENCED_DLL) != 0);
1136 #else
1137 ret = macho_load_dll(s1, fd, filename,
1138 (flags & AFF_REFERENCED_DLL) != 0);
1139 #endif
1141 break;
1142 #endif
1143 case AFF_BINTYPE_AR:
1144 ret = tcc_load_archive(s1, fd, !(flags & AFF_WHOLE_ARCHIVE));
1145 break;
1146 #ifdef TCC_TARGET_COFF
1147 case AFF_BINTYPE_C67:
1148 ret = tcc_load_coff(s1, fd);
1149 break;
1150 #endif
1151 default:
1152 #ifdef TCC_TARGET_PE
1153 ret = pe_load_file(s1, filename, fd);
1154 #elif defined(TCC_TARGET_MACHO)
1155 ret = -1;
1156 #else
1157 /* as GNU ld, consider it is an ld script if not recognized */
1158 ret = tcc_load_ldscript(s1, fd);
1159 #endif
1160 if (ret < 0)
1161 tcc_error_noabort("%s: unrecognized file type %d", filename,
1162 obj_type);
1163 break;
1165 close(fd);
1166 } else {
1167 /* update target deps */
1168 dynarray_add(&s1->target_deps, &s1->nb_target_deps, tcc_strdup(filename));
1169 ret = tcc_compile(s1, flags, filename, fd);
1171 s1->current_filename = NULL;
1172 return ret;
1175 LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename)
1177 int filetype = s->filetype;
1178 if (0 == (filetype & AFF_TYPE_MASK)) {
1179 /* use a file extension to detect a filetype */
1180 const char *ext = tcc_fileextension(filename);
1181 if (ext[0]) {
1182 ext++;
1183 if (!strcmp(ext, "S"))
1184 filetype = AFF_TYPE_ASMPP;
1185 else if (!strcmp(ext, "s"))
1186 filetype = AFF_TYPE_ASM;
1187 else if (!PATHCMP(ext, "c") || !PATHCMP(ext, "i"))
1188 filetype = AFF_TYPE_C;
1189 else
1190 filetype |= AFF_TYPE_BIN;
1191 } else {
1192 filetype = AFF_TYPE_C;
1195 return tcc_add_file_internal(s, filename, filetype | AFF_PRINT_ERROR);
1198 LIBTCCAPI int tcc_add_library_path(TCCState *s, const char *pathname)
1200 tcc_split_path(s, &s->library_paths, &s->nb_library_paths, pathname);
1201 return 0;
1204 static int tcc_add_library_internal(TCCState *s, const char *fmt,
1205 const char *filename, int flags, char **paths, int nb_paths)
1207 char buf[1024];
1208 int i;
1210 for(i = 0; i < nb_paths; i++) {
1211 snprintf(buf, sizeof(buf), fmt, paths[i], filename);
1212 if (tcc_add_file_internal(s, buf, flags | AFF_TYPE_BIN) == 0)
1213 return 0;
1215 return -1;
1218 /* find and load a dll. Return non zero if not found */
1219 /* XXX: add '-rpath' option support ? */
1220 ST_FUNC int tcc_add_dll(TCCState *s, const char *filename, int flags)
1222 return tcc_add_library_internal(s, "%s/%s", filename, flags,
1223 s->library_paths, s->nb_library_paths);
1226 #ifndef TCC_TARGET_PE
1227 ST_FUNC int tcc_add_crt(TCCState *s1, const char *filename)
1229 if (-1 == tcc_add_library_internal(s1, "%s/%s",
1230 filename, 0, s1->crt_paths, s1->nb_crt_paths))
1231 tcc_error_noabort("file '%s' not found", filename);
1232 return 0;
1234 #endif
1236 /* the library name is the same as the argument of the '-l' option */
1237 LIBTCCAPI int tcc_add_library(TCCState *s, const char *libraryname)
1239 #if defined TCC_TARGET_PE
1240 const char *libs[] = { "%s/%s.def", "%s/lib%s.def", "%s/%s.dll", "%s/lib%s.dll", "%s/lib%s.a", NULL };
1241 const char **pp = s->static_link ? libs + 4 : libs;
1242 #elif defined TCC_TARGET_MACHO
1243 const char *libs[] = { "%s/lib%s.dylib", "%s/lib%s.a", NULL };
1244 const char **pp = s->static_link ? libs + 1 : libs;
1245 #else
1246 const char *libs[] = { "%s/lib%s.so", "%s/lib%s.a", NULL };
1247 const char **pp = s->static_link ? libs + 1 : libs;
1248 #endif
1249 int flags = s->filetype & AFF_WHOLE_ARCHIVE;
1250 while (*pp) {
1251 if (0 == tcc_add_library_internal(s, *pp,
1252 libraryname, flags, s->library_paths, s->nb_library_paths))
1253 return 0;
1254 ++pp;
1256 return -1;
1259 PUB_FUNC int tcc_add_library_err(TCCState *s1, const char *libname)
1261 int ret = tcc_add_library(s1, libname);
1262 if (ret < 0)
1263 tcc_error_noabort("library '%s' not found", libname);
1264 return ret;
1267 /* handle #pragma comment(lib,) */
1268 ST_FUNC void tcc_add_pragma_libs(TCCState *s1)
1270 int i;
1271 for (i = 0; i < s1->nb_pragma_libs; i++)
1272 tcc_add_library_err(s1, s1->pragma_libs[i]);
1275 LIBTCCAPI int tcc_add_symbol(TCCState *s1, const char *name, const void *val)
1277 #ifdef TCC_TARGET_PE
1278 /* On x86_64 'val' might not be reachable with a 32bit offset.
1279 So it is handled here as if it were in a DLL. */
1280 pe_putimport(s1, 0, name, (uintptr_t)val);
1281 #else
1282 set_elf_sym(symtab_section, (uintptr_t)val, 0,
1283 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1284 SHN_ABS, name);
1285 #endif
1286 return 0;
1289 LIBTCCAPI void tcc_set_lib_path(TCCState *s, const char *path)
1291 tcc_free(s->tcc_lib_path);
1292 s->tcc_lib_path = tcc_strdup(path);
1295 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
1296 #define FD_INVERT 0x0002 /* invert value before storing */
1298 typedef struct FlagDef {
1299 uint16_t offset;
1300 uint16_t flags;
1301 const char *name;
1302 } FlagDef;
1304 static int no_flag(const char **pp)
1306 const char *p = *pp;
1307 if (*p != 'n' || *++p != 'o' || *++p != '-')
1308 return 0;
1309 *pp = p + 1;
1310 return 1;
1313 ST_FUNC int set_flag(TCCState *s, const FlagDef *flags, const char *name)
1315 int value, ret;
1316 const FlagDef *p;
1317 const char *r;
1319 value = 1;
1320 r = name;
1321 if (no_flag(&r))
1322 value = 0;
1324 for (ret = -1, p = flags; p->name; ++p) {
1325 if (ret) {
1326 if (strcmp(r, p->name))
1327 continue;
1328 } else {
1329 if (0 == (p->flags & WD_ALL))
1330 continue;
1332 if (p->offset) {
1333 *((unsigned char *)s + p->offset) =
1334 p->flags & FD_INVERT ? !value : value;
1335 if (ret)
1336 return 0;
1337 } else {
1338 ret = 0;
1341 return ret;
1344 static int strstart(const char *val, const char **str)
1346 const char *p, *q;
1347 p = *str;
1348 q = val;
1349 while (*q) {
1350 if (*p != *q)
1351 return 0;
1352 p++;
1353 q++;
1355 *str = p;
1356 return 1;
1359 /* Like strstart, but automatically takes into account that ld options can
1361 * - start with double or single dash (e.g. '--soname' or '-soname')
1362 * - arguments can be given as separate or after '=' (e.g. '-Wl,-soname,x.so'
1363 * or '-Wl,-soname=x.so')
1365 * you provide `val` always in 'option[=]' form (no leading -)
1367 static int link_option(const char *str, const char *val, const char **ptr)
1369 const char *p, *q;
1370 int ret;
1372 /* there should be 1 or 2 dashes */
1373 if (*str++ != '-')
1374 return 0;
1375 if (*str == '-')
1376 str++;
1378 /* then str & val should match (potentially up to '=') */
1379 p = str;
1380 q = val;
1382 ret = 1;
1383 if (q[0] == '?') {
1384 ++q;
1385 if (no_flag(&p))
1386 ret = -1;
1389 while (*q != '\0' && *q != '=') {
1390 if (*p != *q)
1391 return 0;
1392 p++;
1393 q++;
1396 /* '=' near eos means ',' or '=' is ok */
1397 if (*q == '=') {
1398 if (*p == 0)
1399 *ptr = p;
1400 if (*p != ',' && *p != '=')
1401 return 0;
1402 p++;
1403 } else if (*p) {
1404 return 0;
1406 *ptr = p;
1407 return ret;
1410 static const char *skip_linker_arg(const char **str)
1412 const char *s1 = *str;
1413 const char *s2 = strchr(s1, ',');
1414 *str = s2 ? s2++ : (s2 = s1 + strlen(s1));
1415 return s2;
1418 static void copy_linker_arg(char **pp, const char *s, int sep)
1420 const char *q = s;
1421 char *p = *pp;
1422 int l = 0;
1423 if (p && sep)
1424 p[l = strlen(p)] = sep, ++l;
1425 skip_linker_arg(&q);
1426 pstrncpy(l + (*pp = tcc_realloc(p, q - s + l + 1)), s, q - s);
1429 /* set linker options */
1430 static int tcc_set_linker(TCCState *s, const char *option)
1432 TCCState *s1 = s;
1433 while (*option) {
1435 const char *p = NULL;
1436 char *end = NULL;
1437 int ignoring = 0;
1438 int ret;
1440 if (link_option(option, "Bsymbolic", &p)) {
1441 s->symbolic = 1;
1442 } else if (link_option(option, "nostdlib", &p)) {
1443 s->nostdlib = 1;
1444 } else if (link_option(option, "fini=", &p)) {
1445 copy_linker_arg(&s->fini_symbol, p, 0);
1446 ignoring = 1;
1447 } else if (link_option(option, "image-base=", &p)
1448 || link_option(option, "Ttext=", &p)) {
1449 s->text_addr = strtoull(p, &end, 16);
1450 s->has_text_addr = 1;
1451 } else if (link_option(option, "init=", &p)) {
1452 copy_linker_arg(&s->init_symbol, p, 0);
1453 ignoring = 1;
1454 } else if (link_option(option, "oformat=", &p)) {
1455 #if defined(TCC_TARGET_PE)
1456 if (strstart("pe-", &p)) {
1457 #elif PTR_SIZE == 8
1458 if (strstart("elf64-", &p)) {
1459 #else
1460 if (strstart("elf32-", &p)) {
1461 #endif
1462 s->output_format = TCC_OUTPUT_FORMAT_ELF;
1463 } else if (!strcmp(p, "binary")) {
1464 s->output_format = TCC_OUTPUT_FORMAT_BINARY;
1465 #ifdef TCC_TARGET_COFF
1466 } else if (!strcmp(p, "coff")) {
1467 s->output_format = TCC_OUTPUT_FORMAT_COFF;
1468 #endif
1469 } else
1470 goto err;
1472 } else if (link_option(option, "as-needed", &p)) {
1473 ignoring = 1;
1474 } else if (link_option(option, "O", &p)) {
1475 ignoring = 1;
1476 } else if (link_option(option, "export-all-symbols", &p)) {
1477 s->rdynamic = 1;
1478 } else if (link_option(option, "export-dynamic", &p)) {
1479 s->rdynamic = 1;
1480 } else if (link_option(option, "rpath=", &p)) {
1481 copy_linker_arg(&s->rpath, p, ':');
1482 } else if (link_option(option, "enable-new-dtags", &p)) {
1483 s->enable_new_dtags = 1;
1484 } else if (link_option(option, "section-alignment=", &p)) {
1485 s->section_align = strtoul(p, &end, 16);
1486 } else if (link_option(option, "soname=", &p)) {
1487 copy_linker_arg(&s->soname, p, 0);
1488 #ifdef TCC_TARGET_PE
1489 } else if (link_option(option, "large-address-aware", &p)) {
1490 s->pe_characteristics |= 0x20;
1491 } else if (link_option(option, "file-alignment=", &p)) {
1492 s->pe_file_align = strtoul(p, &end, 16);
1493 } else if (link_option(option, "stack=", &p)) {
1494 s->pe_stack_size = strtoul(p, &end, 10);
1495 } else if (link_option(option, "subsystem=", &p)) {
1496 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1497 if (!strcmp(p, "native")) {
1498 s->pe_subsystem = 1;
1499 } else if (!strcmp(p, "console")) {
1500 s->pe_subsystem = 3;
1501 } else if (!strcmp(p, "gui") || !strcmp(p, "windows")) {
1502 s->pe_subsystem = 2;
1503 } else if (!strcmp(p, "posix")) {
1504 s->pe_subsystem = 7;
1505 } else if (!strcmp(p, "efiapp")) {
1506 s->pe_subsystem = 10;
1507 } else if (!strcmp(p, "efiboot")) {
1508 s->pe_subsystem = 11;
1509 } else if (!strcmp(p, "efiruntime")) {
1510 s->pe_subsystem = 12;
1511 } else if (!strcmp(p, "efirom")) {
1512 s->pe_subsystem = 13;
1513 #elif defined(TCC_TARGET_ARM)
1514 if (!strcmp(p, "wince")) {
1515 s->pe_subsystem = 9;
1516 #endif
1517 } else
1518 goto err;
1519 #endif
1520 } else if (ret = link_option(option, "?whole-archive", &p), ret) {
1521 if (ret > 0)
1522 s->filetype |= AFF_WHOLE_ARCHIVE;
1523 else
1524 s->filetype &= ~AFF_WHOLE_ARCHIVE;
1525 } else if (p) {
1526 return 0;
1527 } else {
1528 err:
1529 tcc_error("unsupported linker option '%s'", option);
1532 if (ignoring && s->warn_unsupported)
1533 tcc_warning("unsupported linker option '%s'", option);
1535 option = skip_linker_arg(&p);
1537 return 1;
1540 typedef struct TCCOption {
1541 const char *name;
1542 uint16_t index;
1543 uint16_t flags;
1544 } TCCOption;
1546 enum {
1547 TCC_OPTION_HELP,
1548 TCC_OPTION_HELP2,
1549 TCC_OPTION_v,
1550 TCC_OPTION_I,
1551 TCC_OPTION_D,
1552 TCC_OPTION_U,
1553 TCC_OPTION_P,
1554 TCC_OPTION_L,
1555 TCC_OPTION_B,
1556 TCC_OPTION_l,
1557 TCC_OPTION_bench,
1558 TCC_OPTION_bt,
1559 TCC_OPTION_b,
1560 TCC_OPTION_ba,
1561 TCC_OPTION_g,
1562 TCC_OPTION_c,
1563 TCC_OPTION_dumpversion,
1564 TCC_OPTION_d,
1565 TCC_OPTION_static,
1566 TCC_OPTION_std,
1567 TCC_OPTION_shared,
1568 TCC_OPTION_soname,
1569 TCC_OPTION_o,
1570 TCC_OPTION_r,
1571 TCC_OPTION_s,
1572 TCC_OPTION_traditional,
1573 TCC_OPTION_Wl,
1574 TCC_OPTION_Wp,
1575 TCC_OPTION_W,
1576 TCC_OPTION_O,
1577 TCC_OPTION_mfloat_abi,
1578 TCC_OPTION_m,
1579 TCC_OPTION_f,
1580 TCC_OPTION_isystem,
1581 TCC_OPTION_iwithprefix,
1582 TCC_OPTION_include,
1583 TCC_OPTION_nostdinc,
1584 TCC_OPTION_nostdlib,
1585 TCC_OPTION_print_search_dirs,
1586 TCC_OPTION_rdynamic,
1587 TCC_OPTION_param,
1588 TCC_OPTION_pedantic,
1589 TCC_OPTION_pthread,
1590 TCC_OPTION_run,
1591 TCC_OPTION_w,
1592 TCC_OPTION_pipe,
1593 TCC_OPTION_E,
1594 TCC_OPTION_MD,
1595 TCC_OPTION_MF,
1596 TCC_OPTION_x,
1597 TCC_OPTION_ar,
1598 TCC_OPTION_impdef
1601 #define TCC_OPTION_HAS_ARG 0x0001
1602 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
1604 static const TCCOption tcc_options[] = {
1605 { "h", TCC_OPTION_HELP, 0 },
1606 { "-help", TCC_OPTION_HELP, 0 },
1607 { "?", TCC_OPTION_HELP, 0 },
1608 { "hh", TCC_OPTION_HELP2, 0 },
1609 { "v", TCC_OPTION_v, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1610 { "-version", TCC_OPTION_v, 0 }, /* handle as verbose, also prints version*/
1611 { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
1612 { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
1613 { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
1614 { "P", TCC_OPTION_P, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1615 { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
1616 { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
1617 { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG },
1618 { "bench", TCC_OPTION_bench, 0 },
1619 #ifdef CONFIG_TCC_BACKTRACE
1620 { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1621 #endif
1622 #ifdef CONFIG_TCC_BCHECK
1623 { "b", TCC_OPTION_b, 0 },
1624 #endif
1625 { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1626 { "c", TCC_OPTION_c, 0 },
1627 { "dumpversion", TCC_OPTION_dumpversion, 0},
1628 { "d", TCC_OPTION_d, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1629 { "static", TCC_OPTION_static, 0 },
1630 { "std", TCC_OPTION_std, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1631 { "shared", TCC_OPTION_shared, 0 },
1632 { "soname", TCC_OPTION_soname, TCC_OPTION_HAS_ARG },
1633 { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
1634 { "-param", TCC_OPTION_param, TCC_OPTION_HAS_ARG },
1635 { "pedantic", TCC_OPTION_pedantic, 0},
1636 { "pthread", TCC_OPTION_pthread, 0},
1637 { "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1638 { "rdynamic", TCC_OPTION_rdynamic, 0 },
1639 { "r", TCC_OPTION_r, 0 },
1640 { "s", TCC_OPTION_s, 0 },
1641 { "traditional", TCC_OPTION_traditional, 0 },
1642 { "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1643 { "Wp,", TCC_OPTION_Wp, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1644 { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1645 { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1646 #ifdef TCC_TARGET_ARM
1647 { "mfloat-abi", TCC_OPTION_mfloat_abi, TCC_OPTION_HAS_ARG },
1648 #endif
1649 { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1650 { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1651 { "isystem", TCC_OPTION_isystem, TCC_OPTION_HAS_ARG },
1652 { "include", TCC_OPTION_include, TCC_OPTION_HAS_ARG },
1653 { "nostdinc", TCC_OPTION_nostdinc, 0 },
1654 { "nostdlib", TCC_OPTION_nostdlib, 0 },
1655 { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
1656 { "w", TCC_OPTION_w, 0 },
1657 { "pipe", TCC_OPTION_pipe, 0},
1658 { "E", TCC_OPTION_E, 0},
1659 { "MD", TCC_OPTION_MD, 0},
1660 { "MF", TCC_OPTION_MF, TCC_OPTION_HAS_ARG },
1661 { "x", TCC_OPTION_x, TCC_OPTION_HAS_ARG },
1662 { "ar", TCC_OPTION_ar, 0},
1663 #ifdef TCC_TARGET_PE
1664 { "impdef", TCC_OPTION_impdef, 0},
1665 #endif
1666 { NULL, 0, 0 },
1669 static const FlagDef options_W[] = {
1670 { 0, 0, "all" },
1671 { offsetof(TCCState, warn_unsupported), 0, "unsupported" },
1672 { offsetof(TCCState, warn_write_strings), 0, "write-strings" },
1673 { offsetof(TCCState, warn_error), 0, "error" },
1674 { offsetof(TCCState, warn_gcc_compat), 0, "gcc-compat" },
1675 { offsetof(TCCState, warn_implicit_function_declaration), WD_ALL,
1676 "implicit-function-declaration" },
1677 { 0, 0, NULL }
1680 static const FlagDef options_f[] = {
1681 { offsetof(TCCState, char_is_unsigned), 0, "unsigned-char" },
1682 { offsetof(TCCState, char_is_unsigned), FD_INVERT, "signed-char" },
1683 { offsetof(TCCState, nocommon), FD_INVERT, "common" },
1684 { offsetof(TCCState, leading_underscore), 0, "leading-underscore" },
1685 { offsetof(TCCState, ms_extensions), 0, "ms-extensions" },
1686 { offsetof(TCCState, dollars_in_identifiers), 0, "dollars-in-identifiers" },
1687 { 0, 0, NULL }
1690 static const FlagDef options_m[] = {
1691 { offsetof(TCCState, ms_bitfields), 0, "ms-bitfields" },
1692 #ifdef TCC_TARGET_X86_64
1693 { offsetof(TCCState, nosse), FD_INVERT, "sse" },
1694 #endif
1695 { 0, 0, NULL }
1698 static void parse_option_D(TCCState *s1, const char *optarg)
1700 char *sym = tcc_strdup(optarg);
1701 char *value = strchr(sym, '=');
1702 if (value)
1703 *value++ = '\0';
1704 tcc_define_symbol(s1, sym, value);
1705 tcc_free(sym);
1708 static void args_parser_add_file(TCCState *s, const char* filename, int filetype)
1710 struct filespec *f = tcc_malloc(sizeof *f + strlen(filename));
1711 f->type = filetype;
1712 strcpy(f->name, filename);
1713 dynarray_add(&s->files, &s->nb_files, f);
1716 static int args_parser_make_argv(const char *r, int *argc, char ***argv)
1718 int ret = 0, q, c;
1719 CString str;
1720 for(;;) {
1721 while (c = (unsigned char)*r, c && c <= ' ')
1722 ++r;
1723 if (c == 0)
1724 break;
1725 q = 0;
1726 cstr_new(&str);
1727 while (c = (unsigned char)*r, c) {
1728 ++r;
1729 if (c == '\\' && (*r == '"' || *r == '\\')) {
1730 c = *r++;
1731 } else if (c == '"') {
1732 q = !q;
1733 continue;
1734 } else if (q == 0 && c <= ' ') {
1735 break;
1737 cstr_ccat(&str, c);
1739 cstr_ccat(&str, 0);
1740 //printf("<%s>\n", str.data), fflush(stdout);
1741 dynarray_add(argv, argc, tcc_strdup(str.data));
1742 cstr_free(&str);
1743 ++ret;
1745 return ret;
1748 /* read list file */
1749 static void args_parser_listfile(TCCState *s,
1750 const char *filename, int optind, int *pargc, char ***pargv)
1752 TCCState *s1 = s;
1753 int fd, i;
1754 size_t len;
1755 char *p;
1756 int argc = 0;
1757 char **argv = NULL;
1759 fd = open(filename, O_RDONLY | O_BINARY);
1760 if (fd < 0)
1761 tcc_error("listfile '%s' not found", filename);
1763 len = lseek(fd, 0, SEEK_END);
1764 p = tcc_malloc(len + 1), p[len] = 0;
1765 lseek(fd, 0, SEEK_SET), read(fd, p, len), close(fd);
1767 for (i = 0; i < *pargc; ++i)
1768 if (i == optind)
1769 args_parser_make_argv(p, &argc, &argv);
1770 else
1771 dynarray_add(&argv, &argc, tcc_strdup((*pargv)[i]));
1773 tcc_free(p);
1774 dynarray_reset(&s->argv, &s->argc);
1775 *pargc = s->argc = argc, *pargv = s->argv = argv;
1778 PUB_FUNC int tcc_parse_args(TCCState *s, int *pargc, char ***pargv, int optind)
1780 TCCState *s1 = s;
1781 const TCCOption *popt;
1782 const char *optarg, *r;
1783 const char *run = NULL;
1784 int x;
1785 CString linker_arg; /* collect -Wl options */
1786 int tool = 0, arg_start = 0, noaction = optind;
1787 char **argv = *pargv;
1788 int argc = *pargc;
1790 cstr_new(&linker_arg);
1792 while (optind < argc) {
1793 r = argv[optind];
1794 if (r[0] == '@' && r[1] != '\0') {
1795 args_parser_listfile(s, r + 1, optind, &argc, &argv);
1796 continue;
1798 optind++;
1799 if (tool) {
1800 if (r[0] == '-' && r[1] == 'v' && r[2] == 0)
1801 ++s->verbose;
1802 continue;
1804 reparse:
1805 if (r[0] != '-' || r[1] == '\0') {
1806 if (r[0] != '@') /* allow "tcc file(s) -run @ args ..." */
1807 args_parser_add_file(s, r, s->filetype);
1808 if (run) {
1809 tcc_set_options(s, run);
1810 arg_start = optind - 1;
1811 break;
1813 continue;
1816 /* find option in table */
1817 for(popt = tcc_options; ; ++popt) {
1818 const char *p1 = popt->name;
1819 const char *r1 = r + 1;
1820 if (p1 == NULL)
1821 tcc_error("invalid option -- '%s'", r);
1822 if (!strstart(p1, &r1))
1823 continue;
1824 optarg = r1;
1825 if (popt->flags & TCC_OPTION_HAS_ARG) {
1826 if (*r1 == '\0' && !(popt->flags & TCC_OPTION_NOSEP)) {
1827 if (optind >= argc)
1828 arg_err:
1829 tcc_error("argument to '%s' is missing", r);
1830 optarg = argv[optind++];
1832 } else if (*r1 != '\0')
1833 continue;
1834 break;
1837 switch(popt->index) {
1838 case TCC_OPTION_HELP:
1839 x = OPT_HELP;
1840 goto extra_action;
1841 case TCC_OPTION_HELP2:
1842 x = OPT_HELP2;
1843 goto extra_action;
1844 case TCC_OPTION_I:
1845 tcc_add_include_path(s, optarg);
1846 break;
1847 case TCC_OPTION_D:
1848 parse_option_D(s, optarg);
1849 break;
1850 case TCC_OPTION_U:
1851 tcc_undefine_symbol(s, optarg);
1852 break;
1853 case TCC_OPTION_L:
1854 tcc_add_library_path(s, optarg);
1855 break;
1856 case TCC_OPTION_B:
1857 /* set tcc utilities path (mainly for tcc development) */
1858 tcc_set_lib_path(s, optarg);
1859 break;
1860 case TCC_OPTION_l:
1861 args_parser_add_file(s, optarg, AFF_TYPE_LIB | (s->filetype & ~AFF_TYPE_MASK));
1862 s->nb_libraries++;
1863 break;
1864 case TCC_OPTION_pthread:
1865 s->option_pthread = 1;
1866 break;
1867 case TCC_OPTION_bench:
1868 s->do_bench = 1;
1869 break;
1870 #ifdef CONFIG_TCC_BACKTRACE
1871 case TCC_OPTION_bt:
1872 s->rt_num_callers = atoi(optarg);
1873 s->do_backtrace = 1;
1874 s->do_debug = 1;
1875 break;
1876 #endif
1877 #ifdef CONFIG_TCC_BCHECK
1878 case TCC_OPTION_b:
1879 s->do_bounds_check = 1;
1880 s->do_backtrace = 1;
1881 s->do_debug = 1;
1882 break;
1883 #endif
1884 case TCC_OPTION_g:
1885 s->do_debug = 1;
1886 break;
1887 case TCC_OPTION_c:
1888 x = TCC_OUTPUT_OBJ;
1889 set_output_type:
1890 if (s->output_type)
1891 tcc_warning("-%s: overriding compiler action already specified", popt->name);
1892 s->output_type = x;
1893 break;
1894 case TCC_OPTION_d:
1895 if (*optarg == 'D')
1896 s->dflag = 3;
1897 else if (*optarg == 'M')
1898 s->dflag = 7;
1899 else if (*optarg == 't')
1900 s->dflag = 16;
1901 else if (isnum(*optarg))
1902 s->g_debug |= atoi(optarg);
1903 else
1904 goto unsupported_option;
1905 break;
1906 case TCC_OPTION_static:
1907 s->static_link = 1;
1908 break;
1909 case TCC_OPTION_std:
1910 if (strcmp(optarg, "=c11") == 0)
1911 s->cversion = 201112;
1912 break;
1913 case TCC_OPTION_shared:
1914 x = TCC_OUTPUT_DLL;
1915 goto set_output_type;
1916 case TCC_OPTION_soname:
1917 s->soname = tcc_strdup(optarg);
1918 break;
1919 case TCC_OPTION_o:
1920 if (s->outfile) {
1921 tcc_warning("multiple -o option");
1922 tcc_free(s->outfile);
1924 s->outfile = tcc_strdup(optarg);
1925 break;
1926 case TCC_OPTION_r:
1927 /* generate a .o merging several output files */
1928 s->option_r = 1;
1929 x = TCC_OUTPUT_OBJ;
1930 goto set_output_type;
1931 case TCC_OPTION_isystem:
1932 tcc_add_sysinclude_path(s, optarg);
1933 break;
1934 case TCC_OPTION_include:
1935 cstr_printf(&s->cmdline_incl, "#include \"%s\"\n", optarg);
1936 break;
1937 case TCC_OPTION_nostdinc:
1938 s->nostdinc = 1;
1939 break;
1940 case TCC_OPTION_nostdlib:
1941 s->nostdlib = 1;
1942 break;
1943 case TCC_OPTION_run:
1944 #ifndef TCC_IS_NATIVE
1945 tcc_error("-run is not available in a cross compiler");
1946 #endif
1947 run = optarg;
1948 x = TCC_OUTPUT_MEMORY;
1949 goto set_output_type;
1950 case TCC_OPTION_v:
1951 do ++s->verbose; while (*optarg++ == 'v');
1952 ++noaction;
1953 break;
1954 case TCC_OPTION_f:
1955 if (set_flag(s, options_f, optarg) < 0)
1956 goto unsupported_option;
1957 break;
1958 #ifdef TCC_TARGET_ARM
1959 case TCC_OPTION_mfloat_abi:
1960 /* tcc doesn't support soft float yet */
1961 if (!strcmp(optarg, "softfp")) {
1962 s->float_abi = ARM_SOFTFP_FLOAT;
1963 tcc_undefine_symbol(s, "__ARM_PCS_VFP");
1964 } else if (!strcmp(optarg, "hard"))
1965 s->float_abi = ARM_HARD_FLOAT;
1966 else
1967 tcc_error("unsupported float abi '%s'", optarg);
1968 break;
1969 #endif
1970 case TCC_OPTION_m:
1971 if (set_flag(s, options_m, optarg) < 0) {
1972 if (x = atoi(optarg), x != 32 && x != 64)
1973 goto unsupported_option;
1974 if (PTR_SIZE != x/8)
1975 return x;
1976 ++noaction;
1978 break;
1979 case TCC_OPTION_W:
1980 s->warn_none = 0;
1981 if (optarg[0] && set_flag(s, options_W, optarg) < 0)
1982 goto unsupported_option;
1983 break;
1984 case TCC_OPTION_w:
1985 s->warn_none = 1;
1986 break;
1987 case TCC_OPTION_rdynamic:
1988 s->rdynamic = 1;
1989 break;
1990 case TCC_OPTION_Wl:
1991 if (linker_arg.size)
1992 --linker_arg.size, cstr_ccat(&linker_arg, ',');
1993 cstr_cat(&linker_arg, optarg, 0);
1994 if (tcc_set_linker(s, linker_arg.data))
1995 cstr_free(&linker_arg);
1996 break;
1997 case TCC_OPTION_Wp:
1998 r = optarg;
1999 goto reparse;
2000 case TCC_OPTION_E:
2001 x = TCC_OUTPUT_PREPROCESS;
2002 goto set_output_type;
2003 case TCC_OPTION_P:
2004 s->Pflag = atoi(optarg) + 1;
2005 break;
2006 case TCC_OPTION_MD:
2007 s->gen_deps = 1;
2008 break;
2009 case TCC_OPTION_MF:
2010 s->deps_outfile = tcc_strdup(optarg);
2011 break;
2012 case TCC_OPTION_dumpversion:
2013 printf ("%s\n", TCC_VERSION);
2014 exit(0);
2015 break;
2016 case TCC_OPTION_x:
2017 x = 0;
2018 if (*optarg == 'c')
2019 x = AFF_TYPE_C;
2020 else if (*optarg == 'a')
2021 x = AFF_TYPE_ASMPP;
2022 else if (*optarg == 'b')
2023 x = AFF_TYPE_BIN;
2024 else if (*optarg == 'n')
2025 x = AFF_TYPE_NONE;
2026 else
2027 tcc_warning("unsupported language '%s'", optarg);
2028 s->filetype = x | (s->filetype & ~AFF_TYPE_MASK);
2029 break;
2030 case TCC_OPTION_O:
2031 s->optimize = atoi(optarg);
2032 break;
2033 case TCC_OPTION_print_search_dirs:
2034 x = OPT_PRINT_DIRS;
2035 goto extra_action;
2036 case TCC_OPTION_impdef:
2037 x = OPT_IMPDEF;
2038 goto extra_action;
2039 case TCC_OPTION_ar:
2040 x = OPT_AR;
2041 extra_action:
2042 arg_start = optind - 1;
2043 if (arg_start != noaction)
2044 tcc_error("cannot parse %s here", r);
2045 tool = x;
2046 break;
2047 case TCC_OPTION_traditional:
2048 case TCC_OPTION_pedantic:
2049 case TCC_OPTION_pipe:
2050 case TCC_OPTION_s:
2051 /* ignored */
2052 break;
2053 default:
2054 unsupported_option:
2055 if (s->warn_unsupported)
2056 tcc_warning("unsupported option '%s'", r);
2057 break;
2060 if (linker_arg.size) {
2061 r = linker_arg.data;
2062 goto arg_err;
2064 *pargc = argc - arg_start;
2065 *pargv = argv + arg_start;
2066 if (tool)
2067 return tool;
2068 if (optind != noaction)
2069 return 0;
2070 if (s->verbose == 2)
2071 return OPT_PRINT_DIRS;
2072 if (s->verbose)
2073 return OPT_V;
2074 return OPT_HELP;
2077 LIBTCCAPI void tcc_set_options(TCCState *s, const char *r)
2079 char **argv = NULL;
2080 int argc = 0;
2081 args_parser_make_argv(r, &argc, &argv);
2082 tcc_parse_args(s, &argc, &argv, 0);
2083 dynarray_reset(&argv, &argc);
2086 PUB_FUNC void tcc_print_stats(TCCState *s1, unsigned total_time)
2088 if (total_time < 1)
2089 total_time = 1;
2090 if (total_bytes < 1)
2091 total_bytes = 1;
2092 fprintf(stderr, "* %d idents, %d lines, %d bytes\n"
2093 "* %0.3f s, %u lines/s, %0.1f MB/s\n",
2094 total_idents, total_lines, total_bytes,
2095 (double)total_time/1000,
2096 (unsigned)total_lines*1000/total_time,
2097 (double)total_bytes/1000/total_time);
2098 #ifdef MEM_DEBUG
2099 fprintf(stderr, "* %d bytes memory used\n", mem_max_size);
2100 #endif