Fix some string literal expressions in initializers
[tinycc.git] / libtcc.c
blob4e42c363c917b9177d483814715db3a9c31ed7b8
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 #endif /* ONE_SOURCE */
61 #include "tcc.h"
63 /********************************************************/
64 /* global variables */
66 /* XXX: get rid of this ASAP (or maybe not) */
67 ST_DATA struct TCCState *tcc_state;
69 #ifdef MEM_DEBUG
70 static int nb_states;
71 #endif
73 /********************************************************/
74 #ifdef _WIN32
75 ST_FUNC char *normalize_slashes(char *path)
77 char *p;
78 for (p = path; *p; ++p)
79 if (*p == '\\')
80 *p = '/';
81 return path;
84 static HMODULE tcc_module;
86 /* on win32, we suppose the lib and includes are at the location of 'tcc.exe' */
87 static void tcc_set_lib_path_w32(TCCState *s)
89 char path[1024], *p;
90 GetModuleFileNameA(tcc_module, path, sizeof path);
91 p = tcc_basename(normalize_slashes(strlwr(path)));
92 if (p > path)
93 --p;
94 *p = 0;
95 tcc_set_lib_path(s, path);
98 #ifdef TCC_TARGET_PE
99 static void tcc_add_systemdir(TCCState *s)
101 char buf[1000];
102 GetSystemDirectory(buf, sizeof buf);
103 tcc_add_library_path(s, normalize_slashes(buf));
105 #endif
107 #ifdef LIBTCC_AS_DLL
108 BOOL WINAPI DllMain (HINSTANCE hDll, DWORD dwReason, LPVOID lpReserved)
110 if (DLL_PROCESS_ATTACH == dwReason)
111 tcc_module = hDll;
112 return TRUE;
114 #endif
115 #endif
117 /********************************************************/
118 #ifndef CONFIG_TCC_SEMLOCK
119 #define WAIT_SEM()
120 #define POST_SEM()
121 #elif defined _WIN32
122 static int tcc_sem_init;
123 static CRITICAL_SECTION tcc_cr;
124 static void wait_sem(void)
126 if (!tcc_sem_init)
127 InitializeCriticalSection(&tcc_cr), tcc_sem_init = 1;
128 EnterCriticalSection(&tcc_cr);
130 #define WAIT_SEM() wait_sem()
131 #define POST_SEM() LeaveCriticalSection(&tcc_cr);
132 #else
133 #include <semaphore.h>
134 static int tcc_sem_init;
135 static sem_t tcc_sem;
136 static void wait_sem(void)
138 if (!tcc_sem_init)
139 sem_init(&tcc_sem, 0, 1), tcc_sem_init = 1;
140 while (sem_wait (&tcc_sem) < 0 && errno == EINTR);
142 #define WAIT_SEM() wait_sem()
143 #define POST_SEM() sem_post(&tcc_sem)
144 #endif
146 /********************************************************/
147 /* copy a string and truncate it. */
148 ST_FUNC char *pstrcpy(char *buf, size_t buf_size, const char *s)
150 char *q, *q_end;
151 int c;
153 if (buf_size > 0) {
154 q = buf;
155 q_end = buf + buf_size - 1;
156 while (q < q_end) {
157 c = *s++;
158 if (c == '\0')
159 break;
160 *q++ = c;
162 *q = '\0';
164 return buf;
167 /* strcat and truncate. */
168 ST_FUNC char *pstrcat(char *buf, size_t buf_size, const char *s)
170 size_t len;
171 len = strlen(buf);
172 if (len < buf_size)
173 pstrcpy(buf + len, buf_size - len, s);
174 return buf;
177 ST_FUNC char *pstrncpy(char *out, const char *in, size_t num)
179 memcpy(out, in, num);
180 out[num] = '\0';
181 return out;
184 /* extract the basename of a file */
185 PUB_FUNC char *tcc_basename(const char *name)
187 char *p = strchr(name, 0);
188 while (p > name && !IS_DIRSEP(p[-1]))
189 --p;
190 return p;
193 /* extract extension part of a file
195 * (if no extension, return pointer to end-of-string)
197 PUB_FUNC char *tcc_fileextension (const char *name)
199 char *b = tcc_basename(name);
200 char *e = strrchr(b, '.');
201 return e ? e : strchr(b, 0);
204 /********************************************************/
205 /* memory management */
207 #undef free
208 #undef malloc
209 #undef realloc
211 #ifndef MEM_DEBUG
213 PUB_FUNC void tcc_free(void *ptr)
215 free(ptr);
218 PUB_FUNC void *tcc_malloc(unsigned long size)
220 void *ptr;
221 ptr = malloc(size);
222 if (!ptr && size)
223 _tcc_error("memory full (malloc)");
224 return ptr;
227 PUB_FUNC void *tcc_mallocz(unsigned long size)
229 void *ptr;
230 ptr = tcc_malloc(size);
231 memset(ptr, 0, size);
232 return ptr;
235 PUB_FUNC void *tcc_realloc(void *ptr, unsigned long size)
237 void *ptr1;
238 ptr1 = realloc(ptr, size);
239 if (!ptr1 && size)
240 _tcc_error("memory full (realloc)");
241 return ptr1;
244 PUB_FUNC char *tcc_strdup(const char *str)
246 char *ptr;
247 ptr = tcc_malloc(strlen(str) + 1);
248 strcpy(ptr, str);
249 return ptr;
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 if (c == 'f' && file) {
457 /* substitute current file's dir */
458 const char *f = file->true_filename;
459 const char *b = tcc_basename(f);
460 if (b > f)
461 cstr_cat(&str, f, b - f - 1);
462 else
463 cstr_cat(&str, ".", 1);
465 } else {
466 cstr_ccat(&str, c);
469 if (str.size) {
470 cstr_ccat(&str, '\0');
471 dynarray_add(p_ary, p_nb_ary, tcc_strdup(str.data));
473 cstr_free(&str);
474 in = p+1;
475 } while (*p);
478 /********************************************************/
480 static void strcat_vprintf(char *buf, int buf_size, const char *fmt, va_list ap)
482 int len;
483 len = strlen(buf);
484 vsnprintf(buf + len, buf_size - len, fmt, ap);
487 static void strcat_printf(char *buf, int buf_size, const char *fmt, ...)
489 va_list ap;
490 va_start(ap, fmt);
491 strcat_vprintf(buf, buf_size, fmt, ap);
492 va_end(ap);
495 #define ERROR_WARN 0
496 #define ERROR_NOABORT 1
497 #define ERROR_ERROR 2
499 PUB_FUNC void tcc_enter_state(TCCState *s1)
501 WAIT_SEM();
502 tcc_state = s1;
505 static void error1(int mode, const char *fmt, va_list ap)
507 char buf[2048];
508 BufferedFile **pf, *f;
509 TCCState *s1 = tcc_state;
511 buf[0] = '\0';
512 if (s1 == NULL)
513 /* can happen only if called from tcc_malloc(): 'out of memory' */
514 goto no_file;
516 if (!s1->error_set_jmp_enabled) {
517 /* tcc_state just was set by tcc_enter_state() */
518 tcc_state = NULL;
519 POST_SEM();
522 if (mode == ERROR_WARN) {
523 if (s1->warn_none)
524 return;
525 if (s1->warn_error)
526 mode = ERROR_ERROR;
529 f = NULL;
530 if (s1->error_set_jmp_enabled) { /* we're called while parsing a file */
531 /* use upper file if inline ":asm:" or token ":paste:" */
532 for (f = file; f && f->filename[0] == ':'; f = f->prev)
535 if (f) {
536 for(pf = s1->include_stack; pf < s1->include_stack_ptr; pf++)
537 strcat_printf(buf, sizeof(buf), "In file included from %s:%d:\n",
538 (*pf)->filename, (*pf)->line_num);
539 strcat_printf(buf, sizeof(buf), "%s:%d: ",
540 f->filename, f->line_num - !!(tok_flags & TOK_FLAG_BOL));
541 } else if (s1->current_filename) {
542 strcat_printf(buf, sizeof(buf), "%s: ", s1->current_filename);
545 no_file:
546 if (0 == buf[0])
547 strcat_printf(buf, sizeof(buf), "tcc: ");
548 if (mode == ERROR_WARN)
549 strcat_printf(buf, sizeof(buf), "warning: ");
550 else
551 strcat_printf(buf, sizeof(buf), "error: ");
552 strcat_vprintf(buf, sizeof(buf), fmt, ap);
553 if (!s1 || !s1->error_func) {
554 /* default case: stderr */
555 if (s1 && s1->output_type == TCC_OUTPUT_PREPROCESS && s1->ppfp == stdout)
556 /* print a newline during tcc -E */
557 printf("\n"), fflush(stdout);
558 fflush(stdout); /* flush -v output */
559 fprintf(stderr, "%s\n", buf);
560 fflush(stderr); /* print error/warning now (win32) */
561 } else {
562 s1->error_func(s1->error_opaque, buf);
564 if (s1) {
565 if (mode != ERROR_WARN)
566 s1->nb_errors++;
567 if (mode != ERROR_ERROR)
568 return;
569 if (s1->error_set_jmp_enabled)
570 longjmp(s1->error_jmp_buf, 1);
572 exit(1);
575 LIBTCCAPI void tcc_set_error_func(TCCState *s, void *error_opaque, TCCErrorFunc error_func)
577 s->error_opaque = error_opaque;
578 s->error_func = error_func;
581 LIBTCCAPI TCCErrorFunc tcc_get_error_func(TCCState *s)
583 return s->error_func;
586 LIBTCCAPI void *tcc_get_error_opaque(TCCState *s)
588 return s->error_opaque;
591 /* error without aborting current compilation */
592 PUB_FUNC void _tcc_error_noabort(const char *fmt, ...)
594 va_list ap;
595 va_start(ap, fmt);
596 error1(ERROR_NOABORT, fmt, ap);
597 va_end(ap);
600 PUB_FUNC void _tcc_error(const char *fmt, ...)
602 va_list ap;
603 va_start(ap, fmt);
604 for (;;) error1(ERROR_ERROR, fmt, ap);
607 PUB_FUNC void _tcc_warning(const char *fmt, ...)
609 va_list ap;
610 va_start(ap, fmt);
611 error1(ERROR_WARN, fmt, ap);
612 va_end(ap);
615 /********************************************************/
616 /* I/O layer */
618 ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen)
620 BufferedFile *bf;
621 int buflen = initlen ? initlen : IO_BUF_SIZE;
623 bf = tcc_mallocz(sizeof(BufferedFile) + buflen);
624 bf->buf_ptr = bf->buffer;
625 bf->buf_end = bf->buffer + initlen;
626 bf->buf_end[0] = CH_EOB; /* put eob symbol */
627 pstrcpy(bf->filename, sizeof(bf->filename), filename);
628 #ifdef _WIN32
629 normalize_slashes(bf->filename);
630 #endif
631 bf->true_filename = bf->filename;
632 bf->line_num = 1;
633 bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
634 bf->fd = -1;
635 bf->prev = file;
636 file = bf;
637 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
640 ST_FUNC void tcc_close(void)
642 TCCState *s1 = tcc_state;
643 BufferedFile *bf = file;
644 if (bf->fd > 0) {
645 close(bf->fd);
646 total_lines += bf->line_num;
648 if (bf->true_filename != bf->filename)
649 tcc_free(bf->true_filename);
650 file = bf->prev;
651 tcc_free(bf);
654 static int _tcc_open(TCCState *s1, const char *filename)
656 int fd;
657 if (strcmp(filename, "-") == 0)
658 fd = 0, filename = "<stdin>";
659 else
660 fd = open(filename, O_RDONLY | O_BINARY);
661 if ((s1->verbose == 2 && fd >= 0) || s1->verbose == 3)
662 printf("%s %*s%s\n", fd < 0 ? "nf":"->",
663 (int)(s1->include_stack_ptr - s1->include_stack), "", filename);
664 return fd;
667 ST_FUNC int tcc_open(TCCState *s1, const char *filename)
669 int fd = _tcc_open(s1, filename);
670 if (fd < 0)
671 return -1;
672 tcc_open_bf(s1, filename, 0);
673 file->fd = fd;
674 return 0;
677 /* compile the file opened in 'file'. Return non zero if errors. */
678 static int tcc_compile(TCCState *s1, int filetype, const char *str, int fd)
680 /* Here we enter the code section where we use the global variables for
681 parsing and code generation (tccpp.c, tccgen.c, <target>-gen.c).
682 Other threads need to wait until we're done.
684 Alternatively we could use thread local storage for those global
685 variables, which may or may not have advantages */
687 WAIT_SEM();
688 tcc_state = s1;
690 if (setjmp(s1->error_jmp_buf) == 0) {
691 int is_asm;
692 s1->error_set_jmp_enabled = 1;
693 s1->nb_errors = 0;
695 if (fd == -1) {
696 int len = strlen(str);
697 tcc_open_bf(s1, "<string>", len);
698 memcpy(file->buffer, str, len);
699 } else {
700 tcc_open_bf(s1, str, 0);
701 file->fd = fd;
704 is_asm = !!(filetype & (AFF_TYPE_ASM|AFF_TYPE_ASMPP));
705 tccelf_begin_file(s1);
706 preprocess_start(s1, is_asm);
707 tccgen_init(s1);
708 if (s1->output_type == TCC_OUTPUT_PREPROCESS) {
709 tcc_preprocess(s1);
710 } else if (is_asm) {
711 #ifdef CONFIG_TCC_ASM
712 tcc_assemble(s1, !!(filetype & AFF_TYPE_ASMPP));
713 #else
714 tcc_error_noabort("asm not supported");
715 #endif
716 } else {
717 tccgen_compile(s1);
720 s1->error_set_jmp_enabled = 0;
721 tccgen_finish(s1);
722 preprocess_end(s1);
723 tccelf_end_file(s1);
725 tcc_state = NULL;
726 POST_SEM();
727 return s1->nb_errors != 0 ? -1 : 0;
730 LIBTCCAPI int tcc_compile_string(TCCState *s, const char *str)
732 return tcc_compile(s, s->filetype, str, -1);
735 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
736 LIBTCCAPI void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
738 if (!value)
739 value = "1";
740 cstr_printf(&s1->cmdline_defs, "#define %s %s\n", sym, value);
743 /* undefine a preprocessor symbol */
744 LIBTCCAPI void tcc_undefine_symbol(TCCState *s1, const char *sym)
746 cstr_printf(&s1->cmdline_defs, "#undef %s\n", sym);
750 LIBTCCAPI TCCState *tcc_new(void)
752 TCCState *s;
754 s = tcc_mallocz(sizeof(TCCState));
755 if (!s)
756 return NULL;
757 #ifdef MEM_DEBUG
758 ++nb_states;
759 #endif
761 #undef gnu_ext
763 s->gnu_ext = 1;
764 s->tcc_ext = 1;
765 s->nocommon = 1;
766 s->dollars_in_identifiers = 1; /*on by default like in gcc/clang*/
767 s->cversion = 199901; /* default unless -std=c11 is supplied */
768 s->warn_implicit_function_declaration = 1;
769 s->ms_extensions = 1;
771 #ifdef CHAR_IS_UNSIGNED
772 s->char_is_unsigned = 1;
773 #endif
774 #ifdef TCC_TARGET_I386
775 s->seg_size = 32;
776 #endif
777 /* enable this if you want symbols with leading underscore on windows: */
778 #if 0 /* def TCC_TARGET_PE */
779 s->leading_underscore = 1;
780 #endif
781 s->ppfp = stdout;
782 /* might be used in error() before preprocess_start() */
783 s->include_stack_ptr = s->include_stack;
785 tccelf_new(s);
787 #ifdef _WIN32
788 tcc_set_lib_path_w32(s);
789 #else
790 tcc_set_lib_path(s, CONFIG_TCCDIR);
791 #endif
794 /* define __TINYC__ 92X */
795 char buffer[32]; int a,b,c;
796 sscanf(TCC_VERSION, "%d.%d.%d", &a, &b, &c);
797 sprintf(buffer, "%d", a*10000 + b*100 + c);
798 tcc_define_symbol(s, "__TINYC__", buffer);
801 /* standard defines */
802 tcc_define_symbol(s, "__STDC__", NULL);
803 tcc_define_symbol(s, "__STDC_VERSION__", "199901L");
804 tcc_define_symbol(s, "__STDC_HOSTED__", NULL);
806 /* target defines */
807 #if defined(TCC_TARGET_I386)
808 tcc_define_symbol(s, "__i386__", NULL);
809 tcc_define_symbol(s, "__i386", NULL);
810 tcc_define_symbol(s, "i386", NULL);
811 #elif defined(TCC_TARGET_X86_64)
812 tcc_define_symbol(s, "__x86_64__", NULL);
813 #elif defined(TCC_TARGET_ARM)
814 tcc_define_symbol(s, "__ARM_ARCH_4__", NULL);
815 tcc_define_symbol(s, "__arm_elf__", NULL);
816 tcc_define_symbol(s, "__arm_elf", NULL);
817 tcc_define_symbol(s, "arm_elf", NULL);
818 tcc_define_symbol(s, "__arm__", NULL);
819 tcc_define_symbol(s, "__arm", NULL);
820 tcc_define_symbol(s, "arm", NULL);
821 tcc_define_symbol(s, "__APCS_32__", NULL);
822 tcc_define_symbol(s, "__ARMEL__", NULL);
823 #if defined(TCC_ARM_EABI)
824 tcc_define_symbol(s, "__ARM_EABI__", NULL);
825 #endif
826 #if defined(TCC_ARM_HARDFLOAT)
827 s->float_abi = ARM_HARD_FLOAT;
828 tcc_define_symbol(s, "__ARM_PCS_VFP", NULL);
829 #else
830 s->float_abi = ARM_SOFTFP_FLOAT;
831 #endif
832 #elif defined(TCC_TARGET_ARM64)
833 tcc_define_symbol(s, "__aarch64__", NULL);
834 #elif defined TCC_TARGET_C67
835 tcc_define_symbol(s, "__C67__", NULL);
836 #elif defined TCC_TARGET_RISCV64
837 tcc_define_symbol(s, "__riscv", NULL);
838 tcc_define_symbol(s, "__riscv_xlen", "64");
839 tcc_define_symbol(s, "__riscv_flen", "64");
840 tcc_define_symbol(s, "__riscv_div", NULL);
841 tcc_define_symbol(s, "__riscv_mul", NULL);
842 tcc_define_symbol(s, "__riscv_fdiv", NULL);
843 tcc_define_symbol(s, "__riscv_fsqrt", NULL);
844 tcc_define_symbol(s, "__riscv_float_abi_double", NULL);
845 #endif
847 #ifdef TCC_TARGET_PE
848 tcc_define_symbol(s, "_WIN32", NULL);
849 tcc_define_symbol(s, "__declspec(x)", "__attribute__((x))");
850 tcc_define_symbol(s, "__cdecl", "");
851 # ifdef TCC_TARGET_X86_64
852 tcc_define_symbol(s, "_WIN64", NULL);
853 # endif
854 #else
855 tcc_define_symbol(s, "__unix__", NULL);
856 tcc_define_symbol(s, "__unix", NULL);
857 tcc_define_symbol(s, "unix", NULL);
858 # if defined(__linux__)
859 tcc_define_symbol(s, "__linux__", NULL);
860 tcc_define_symbol(s, "__linux", NULL);
861 # endif
862 # if defined(__FreeBSD__)
863 tcc_define_symbol(s, "__FreeBSD__", "__FreeBSD__");
864 /* No 'Thread Storage Local' on FreeBSD with tcc */
865 tcc_define_symbol(s, "__NO_TLS", NULL);
866 # endif
867 # if defined(__FreeBSD_kernel__)
868 tcc_define_symbol(s, "__FreeBSD_kernel__", NULL);
869 # endif
870 # if defined(__NetBSD__)
871 tcc_define_symbol(s, "__NetBSD__", "__NetBSD__");
872 # endif
873 # if defined(__OpenBSD__)
874 tcc_define_symbol(s, "__OpenBSD__", "__OpenBSD__");
875 # endif
876 #endif
878 /* TinyCC & gcc defines */
879 #if PTR_SIZE == 4
880 /* 32bit systems. */
881 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned int");
882 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "int");
883 tcc_define_symbol(s, "__ILP32__", NULL);
884 #elif LONG_SIZE == 4
885 /* 64bit Windows. */
886 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned long long");
887 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "long long");
888 tcc_define_symbol(s, "__LLP64__", NULL);
889 #else
890 /* Other 64bit systems. */
891 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned long");
892 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "long");
893 tcc_define_symbol(s, "__LP64__", NULL);
894 #endif
895 tcc_define_symbol(s, "__SIZEOF_POINTER__", PTR_SIZE == 4 ? "4" : "8");
897 #ifdef TCC_TARGET_PE
898 tcc_define_symbol(s, "__WCHAR_TYPE__", "unsigned short");
899 tcc_define_symbol(s, "__WINT_TYPE__", "unsigned short");
900 #else
901 tcc_define_symbol(s, "__WCHAR_TYPE__", "int");
902 /* wint_t is unsigned int by default, but (signed) int on BSDs
903 and unsigned short on windows. Other OSes might have still
904 other conventions, sigh. */
905 # if defined(__FreeBSD__) || defined (__FreeBSD_kernel__) \
906 || defined(__NetBSD__) || defined(__OpenBSD__)
907 tcc_define_symbol(s, "__WINT_TYPE__", "int");
908 # ifdef __FreeBSD__
909 /* define __GNUC__ to have some useful stuff from sys/cdefs.h
910 that are unconditionally used in FreeBSDs other system headers :/ */
911 tcc_define_symbol(s, "__GNUC__", "2");
912 tcc_define_symbol(s, "__GNUC_MINOR__", "7");
913 tcc_define_symbol(s, "__builtin_alloca", "alloca");
914 # endif
915 # else
916 tcc_define_symbol(s, "__WINT_TYPE__", "unsigned int");
917 /* glibc defines */
918 tcc_define_symbol(s, "__REDIRECT(name, proto, alias)",
919 "name proto __asm__ (#alias)");
920 tcc_define_symbol(s, "__REDIRECT_NTH(name, proto, alias)",
921 "name proto __asm__ (#alias) __THROW");
922 # endif
923 /* Some GCC builtins that are simple to express as macros. */
924 tcc_define_symbol(s, "__builtin_extract_return_addr(x)", "x");
925 #endif /* ndef TCC_TARGET_PE */
926 #ifdef TCC_TARGET_MACHO
927 /* emulate APPLE-GCC to make libc's headerfiles compile: */
928 tcc_define_symbol(s, "__APPLE__", "1");
929 tcc_define_symbol(s, "__GNUC__", "4"); /* darwin emits warning on GCC<4 */
931 /* avoids usage of GCC/clang specific builtins in libc-headerfiles: */
932 tcc_define_symbol(s, "__FINITE_MATH_ONLY__", "1");
933 tcc_define_symbol(s, "_FORTIFY_SOURCE", "0");
934 #endif /* ndef TCC_TARGET_MACHO */
935 return s;
938 LIBTCCAPI void tcc_delete(TCCState *s1)
940 /* free sections */
941 tccelf_delete(s1);
943 /* free library paths */
944 dynarray_reset(&s1->library_paths, &s1->nb_library_paths);
945 dynarray_reset(&s1->crt_paths, &s1->nb_crt_paths);
947 /* free include paths */
948 dynarray_reset(&s1->include_paths, &s1->nb_include_paths);
949 dynarray_reset(&s1->sysinclude_paths, &s1->nb_sysinclude_paths);
951 tcc_free(s1->tcc_lib_path);
952 tcc_free(s1->soname);
953 tcc_free(s1->rpath);
954 tcc_free(s1->init_symbol);
955 tcc_free(s1->fini_symbol);
956 tcc_free(s1->outfile);
957 tcc_free(s1->deps_outfile);
958 dynarray_reset(&s1->files, &s1->nb_files);
959 dynarray_reset(&s1->target_deps, &s1->nb_target_deps);
960 dynarray_reset(&s1->pragma_libs, &s1->nb_pragma_libs);
961 dynarray_reset(&s1->argv, &s1->argc);
963 cstr_free(&s1->cmdline_defs);
964 cstr_free(&s1->cmdline_incl);
965 #ifdef TCC_IS_NATIVE
966 /* free runtime memory */
967 tcc_run_free(s1);
968 #endif
970 tcc_free(s1);
971 #ifdef MEM_DEBUG
972 if (0 == --nb_states)
973 tcc_memcheck();
974 #endif
977 LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type)
979 s->output_type = output_type;
981 /* always elf for objects */
982 if (output_type == TCC_OUTPUT_OBJ)
983 s->output_format = TCC_OUTPUT_FORMAT_ELF;
985 if (s->char_is_unsigned)
986 tcc_define_symbol(s, "__CHAR_UNSIGNED__", NULL);
988 if (s->cversion == 201112) {
989 tcc_undefine_symbol(s, "__STDC_VERSION__");
990 tcc_define_symbol(s, "__STDC_VERSION__", "201112L");
991 tcc_define_symbol(s, "__STDC_NO_ATOMICS__", NULL);
992 tcc_define_symbol(s, "__STDC_NO_COMPLEX__", NULL);
993 tcc_define_symbol(s, "__STDC_NO_THREADS__", NULL);
994 #ifndef TCC_TARGET_PE
995 /* on Linux, this conflicts with a define introduced by
996 /usr/include/stdc-predef.h included by glibc libs
997 tcc_define_symbol(s, "__STDC_ISO_10646__", "201605L"); */
998 tcc_define_symbol(s, "__STDC_UTF_16__", NULL);
999 tcc_define_symbol(s, "__STDC_UTF_32__", NULL);
1000 #endif
1003 if (s->optimize > 0)
1004 tcc_define_symbol(s, "__OPTIMIZE__", NULL);
1006 if (s->option_pthread)
1007 tcc_define_symbol(s, "_REENTRANT", NULL);
1009 if (!s->nostdinc) {
1010 /* default include paths */
1011 /* -isystem paths have already been handled */
1012 tcc_add_sysinclude_path(s, CONFIG_TCC_SYSINCLUDEPATHS);
1015 #ifdef CONFIG_TCC_BCHECK
1016 if (s->do_bounds_check) {
1017 /* if bound checking, then add corresponding sections */
1018 tccelf_bounds_new(s);
1019 /* define symbol */
1020 tcc_define_symbol(s, "__BOUNDS_CHECKING_ON", NULL);
1022 #endif
1023 if (s->do_debug) {
1024 /* add debug sections */
1025 tccelf_stab_new(s);
1028 tcc_add_library_path(s, CONFIG_TCC_LIBPATHS);
1030 #ifdef TCC_TARGET_PE
1031 # ifdef _WIN32
1032 if (!s->nostdlib && output_type != TCC_OUTPUT_OBJ)
1033 tcc_add_systemdir(s);
1034 # endif
1035 #else
1036 /* paths for crt objects */
1037 tcc_split_path(s, &s->crt_paths, &s->nb_crt_paths, CONFIG_TCC_CRTPREFIX);
1038 /* add libc crt1/crti objects */
1039 if ((output_type == TCC_OUTPUT_EXE || output_type == TCC_OUTPUT_DLL) &&
1040 !s->nostdlib) {
1041 if (output_type != TCC_OUTPUT_DLL)
1042 tcc_add_crt(s, "crt1.o");
1043 tcc_add_crt(s, "crti.o");
1045 #endif
1046 return 0;
1049 LIBTCCAPI int tcc_add_include_path(TCCState *s, const char *pathname)
1051 tcc_split_path(s, &s->include_paths, &s->nb_include_paths, pathname);
1052 return 0;
1055 LIBTCCAPI int tcc_add_sysinclude_path(TCCState *s, const char *pathname)
1057 tcc_split_path(s, &s->sysinclude_paths, &s->nb_sysinclude_paths, pathname);
1058 return 0;
1061 ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
1063 int fd, ret;
1065 /* open the file */
1066 fd = _tcc_open(s1, filename);
1067 if (fd < 0) {
1068 if (flags & AFF_PRINT_ERROR)
1069 tcc_error_noabort("file '%s' not found", filename);
1070 return -1;
1073 s1->current_filename = filename;
1074 if (flags & AFF_TYPE_BIN) {
1075 ElfW(Ehdr) ehdr;
1076 int obj_type;
1078 obj_type = tcc_object_type(fd, &ehdr);
1079 lseek(fd, 0, SEEK_SET);
1081 #ifdef TCC_TARGET_MACHO
1082 if (0 == obj_type && 0 == strcmp(tcc_fileextension(filename), ".dylib"))
1083 obj_type = AFF_BINTYPE_DYN;
1084 #endif
1086 switch (obj_type) {
1087 case AFF_BINTYPE_REL:
1088 ret = tcc_load_object_file(s1, fd, 0);
1089 break;
1090 #ifndef TCC_TARGET_PE
1091 case AFF_BINTYPE_DYN:
1092 if (s1->output_type == TCC_OUTPUT_MEMORY) {
1093 ret = 0;
1094 #ifdef TCC_IS_NATIVE
1095 if (NULL == dlopen(filename, RTLD_GLOBAL | RTLD_LAZY))
1096 ret = -1;
1097 #endif
1098 } else {
1099 ret = tcc_load_dll(s1, fd, filename,
1100 (flags & AFF_REFERENCED_DLL) != 0);
1102 break;
1103 #endif
1104 case AFF_BINTYPE_AR:
1105 ret = tcc_load_archive(s1, fd, !(flags & AFF_WHOLE_ARCHIVE));
1106 break;
1107 #ifdef TCC_TARGET_COFF
1108 case AFF_BINTYPE_C67:
1109 ret = tcc_load_coff(s1, fd);
1110 break;
1111 #endif
1112 default:
1113 #ifdef TCC_TARGET_PE
1114 ret = pe_load_file(s1, filename, fd);
1115 #else
1116 /* as GNU ld, consider it is an ld script if not recognized */
1117 ret = tcc_load_ldscript(s1, fd);
1118 #endif
1119 if (ret < 0)
1120 tcc_error_noabort("unrecognized file type");
1121 break;
1123 close(fd);
1124 } else {
1125 /* update target deps */
1126 dynarray_add(&s1->target_deps, &s1->nb_target_deps, tcc_strdup(filename));
1127 ret = tcc_compile(s1, flags, filename, fd);
1129 s1->current_filename = NULL;
1130 return ret;
1133 LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename)
1135 int filetype = s->filetype;
1136 if (0 == (filetype & AFF_TYPE_MASK)) {
1137 /* use a file extension to detect a filetype */
1138 const char *ext = tcc_fileextension(filename);
1139 if (ext[0]) {
1140 ext++;
1141 if (!strcmp(ext, "S"))
1142 filetype = AFF_TYPE_ASMPP;
1143 else if (!strcmp(ext, "s"))
1144 filetype = AFF_TYPE_ASM;
1145 else if (!PATHCMP(ext, "c") || !PATHCMP(ext, "i"))
1146 filetype = AFF_TYPE_C;
1147 else
1148 filetype |= AFF_TYPE_BIN;
1149 } else {
1150 filetype = AFF_TYPE_C;
1153 return tcc_add_file_internal(s, filename, filetype | AFF_PRINT_ERROR);
1156 LIBTCCAPI int tcc_add_library_path(TCCState *s, const char *pathname)
1158 tcc_split_path(s, &s->library_paths, &s->nb_library_paths, pathname);
1159 return 0;
1162 static int tcc_add_library_internal(TCCState *s, const char *fmt,
1163 const char *filename, int flags, char **paths, int nb_paths)
1165 char buf[1024];
1166 int i;
1168 for(i = 0; i < nb_paths; i++) {
1169 snprintf(buf, sizeof(buf), fmt, paths[i], filename);
1170 if (tcc_add_file_internal(s, buf, flags | AFF_TYPE_BIN) == 0)
1171 return 0;
1173 return -1;
1176 /* find and load a dll. Return non zero if not found */
1177 /* XXX: add '-rpath' option support ? */
1178 ST_FUNC int tcc_add_dll(TCCState *s, const char *filename, int flags)
1180 return tcc_add_library_internal(s, "%s/%s", filename, flags,
1181 s->library_paths, s->nb_library_paths);
1184 #ifndef TCC_TARGET_PE
1185 ST_FUNC int tcc_add_crt(TCCState *s1, const char *filename)
1187 if (-1 == tcc_add_library_internal(s1, "%s/%s",
1188 filename, 0, s1->crt_paths, s1->nb_crt_paths))
1189 tcc_error_noabort("file '%s' not found", filename);
1190 return 0;
1192 #endif
1194 /* the library name is the same as the argument of the '-l' option */
1195 LIBTCCAPI int tcc_add_library(TCCState *s, const char *libraryname)
1197 #if defined TCC_TARGET_PE
1198 const char *libs[] = { "%s/%s.def", "%s/lib%s.def", "%s/%s.dll", "%s/lib%s.dll", "%s/lib%s.a", NULL };
1199 const char **pp = s->static_link ? libs + 4 : libs;
1200 #elif defined TCC_TARGET_MACHO
1201 const char *libs[] = { "%s/lib%s.dylib", "%s/lib%s.a", NULL };
1202 const char **pp = s->static_link ? libs + 1 : libs;
1203 #else
1204 const char *libs[] = { "%s/lib%s.so", "%s/lib%s.a", NULL };
1205 const char **pp = s->static_link ? libs + 1 : libs;
1206 #endif
1207 int flags = s->filetype & AFF_WHOLE_ARCHIVE;
1208 while (*pp) {
1209 if (0 == tcc_add_library_internal(s, *pp,
1210 libraryname, flags, s->library_paths, s->nb_library_paths))
1211 return 0;
1212 ++pp;
1214 return -1;
1217 PUB_FUNC int tcc_add_library_err(TCCState *s1, const char *libname)
1219 int ret = tcc_add_library(s1, libname);
1220 if (ret < 0)
1221 tcc_error_noabort("library '%s' not found", libname);
1222 return ret;
1225 /* handle #pragma comment(lib,) */
1226 ST_FUNC void tcc_add_pragma_libs(TCCState *s1)
1228 int i;
1229 for (i = 0; i < s1->nb_pragma_libs; i++)
1230 tcc_add_library_err(s1, s1->pragma_libs[i]);
1233 LIBTCCAPI int tcc_add_symbol(TCCState *s1, const char *name, const void *val)
1235 #ifdef TCC_TARGET_PE
1236 /* On x86_64 'val' might not be reachable with a 32bit offset.
1237 So it is handled here as if it were in a DLL. */
1238 pe_putimport(s1, 0, name, (uintptr_t)val);
1239 #else
1240 set_elf_sym(symtab_section, (uintptr_t)val, 0,
1241 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1242 SHN_ABS, name);
1243 #endif
1244 return 0;
1247 LIBTCCAPI void tcc_set_lib_path(TCCState *s, const char *path)
1249 tcc_free(s->tcc_lib_path);
1250 s->tcc_lib_path = tcc_strdup(path);
1253 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
1254 #define FD_INVERT 0x0002 /* invert value before storing */
1256 typedef struct FlagDef {
1257 uint16_t offset;
1258 uint16_t flags;
1259 const char *name;
1260 } FlagDef;
1262 static int no_flag(const char **pp)
1264 const char *p = *pp;
1265 if (*p != 'n' || *++p != 'o' || *++p != '-')
1266 return 0;
1267 *pp = p + 1;
1268 return 1;
1271 ST_FUNC int set_flag(TCCState *s, const FlagDef *flags, const char *name)
1273 int value, ret;
1274 const FlagDef *p;
1275 const char *r;
1277 value = 1;
1278 r = name;
1279 if (no_flag(&r))
1280 value = 0;
1282 for (ret = -1, p = flags; p->name; ++p) {
1283 if (ret) {
1284 if (strcmp(r, p->name))
1285 continue;
1286 } else {
1287 if (0 == (p->flags & WD_ALL))
1288 continue;
1290 if (p->offset) {
1291 *((unsigned char *)s + p->offset) =
1292 p->flags & FD_INVERT ? !value : value;
1293 if (ret)
1294 return 0;
1295 } else {
1296 ret = 0;
1299 return ret;
1302 static int strstart(const char *val, const char **str)
1304 const char *p, *q;
1305 p = *str;
1306 q = val;
1307 while (*q) {
1308 if (*p != *q)
1309 return 0;
1310 p++;
1311 q++;
1313 *str = p;
1314 return 1;
1317 /* Like strstart, but automatically takes into account that ld options can
1319 * - start with double or single dash (e.g. '--soname' or '-soname')
1320 * - arguments can be given as separate or after '=' (e.g. '-Wl,-soname,x.so'
1321 * or '-Wl,-soname=x.so')
1323 * you provide `val` always in 'option[=]' form (no leading -)
1325 static int link_option(const char *str, const char *val, const char **ptr)
1327 const char *p, *q;
1328 int ret;
1330 /* there should be 1 or 2 dashes */
1331 if (*str++ != '-')
1332 return 0;
1333 if (*str == '-')
1334 str++;
1336 /* then str & val should match (potentially up to '=') */
1337 p = str;
1338 q = val;
1340 ret = 1;
1341 if (q[0] == '?') {
1342 ++q;
1343 if (no_flag(&p))
1344 ret = -1;
1347 while (*q != '\0' && *q != '=') {
1348 if (*p != *q)
1349 return 0;
1350 p++;
1351 q++;
1354 /* '=' near eos means ',' or '=' is ok */
1355 if (*q == '=') {
1356 if (*p == 0)
1357 *ptr = p;
1358 if (*p != ',' && *p != '=')
1359 return 0;
1360 p++;
1361 } else if (*p) {
1362 return 0;
1364 *ptr = p;
1365 return ret;
1368 static const char *skip_linker_arg(const char **str)
1370 const char *s1 = *str;
1371 const char *s2 = strchr(s1, ',');
1372 *str = s2 ? s2++ : (s2 = s1 + strlen(s1));
1373 return s2;
1376 static void copy_linker_arg(char **pp, const char *s, int sep)
1378 const char *q = s;
1379 char *p = *pp;
1380 int l = 0;
1381 if (p && sep)
1382 p[l = strlen(p)] = sep, ++l;
1383 skip_linker_arg(&q);
1384 pstrncpy(l + (*pp = tcc_realloc(p, q - s + l + 1)), s, q - s);
1387 /* set linker options */
1388 static int tcc_set_linker(TCCState *s, const char *option)
1390 TCCState *s1 = s;
1391 while (*option) {
1393 const char *p = NULL;
1394 char *end = NULL;
1395 int ignoring = 0;
1396 int ret;
1398 if (link_option(option, "Bsymbolic", &p)) {
1399 s->symbolic = 1;
1400 } else if (link_option(option, "nostdlib", &p)) {
1401 s->nostdlib = 1;
1402 } else if (link_option(option, "fini=", &p)) {
1403 copy_linker_arg(&s->fini_symbol, p, 0);
1404 ignoring = 1;
1405 } else if (link_option(option, "image-base=", &p)
1406 || link_option(option, "Ttext=", &p)) {
1407 s->text_addr = strtoull(p, &end, 16);
1408 s->has_text_addr = 1;
1409 } else if (link_option(option, "init=", &p)) {
1410 copy_linker_arg(&s->init_symbol, p, 0);
1411 ignoring = 1;
1412 } else if (link_option(option, "oformat=", &p)) {
1413 #if defined(TCC_TARGET_PE)
1414 if (strstart("pe-", &p)) {
1415 #elif PTR_SIZE == 8
1416 if (strstart("elf64-", &p)) {
1417 #else
1418 if (strstart("elf32-", &p)) {
1419 #endif
1420 s->output_format = TCC_OUTPUT_FORMAT_ELF;
1421 } else if (!strcmp(p, "binary")) {
1422 s->output_format = TCC_OUTPUT_FORMAT_BINARY;
1423 #ifdef TCC_TARGET_COFF
1424 } else if (!strcmp(p, "coff")) {
1425 s->output_format = TCC_OUTPUT_FORMAT_COFF;
1426 #endif
1427 } else
1428 goto err;
1430 } else if (link_option(option, "as-needed", &p)) {
1431 ignoring = 1;
1432 } else if (link_option(option, "O", &p)) {
1433 ignoring = 1;
1434 } else if (link_option(option, "export-all-symbols", &p)) {
1435 s->rdynamic = 1;
1436 } else if (link_option(option, "export-dynamic", &p)) {
1437 s->rdynamic = 1;
1438 } else if (link_option(option, "rpath=", &p)) {
1439 copy_linker_arg(&s->rpath, p, ':');
1440 } else if (link_option(option, "enable-new-dtags", &p)) {
1441 s->enable_new_dtags = 1;
1442 } else if (link_option(option, "section-alignment=", &p)) {
1443 s->section_align = strtoul(p, &end, 16);
1444 } else if (link_option(option, "soname=", &p)) {
1445 copy_linker_arg(&s->soname, p, 0);
1446 #ifdef TCC_TARGET_PE
1447 } else if (link_option(option, "large-address-aware", &p)) {
1448 s->pe_characteristics |= 0x20;
1449 } else if (link_option(option, "file-alignment=", &p)) {
1450 s->pe_file_align = strtoul(p, &end, 16);
1451 } else if (link_option(option, "stack=", &p)) {
1452 s->pe_stack_size = strtoul(p, &end, 10);
1453 } else if (link_option(option, "subsystem=", &p)) {
1454 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1455 if (!strcmp(p, "native")) {
1456 s->pe_subsystem = 1;
1457 } else if (!strcmp(p, "console")) {
1458 s->pe_subsystem = 3;
1459 } else if (!strcmp(p, "gui") || !strcmp(p, "windows")) {
1460 s->pe_subsystem = 2;
1461 } else if (!strcmp(p, "posix")) {
1462 s->pe_subsystem = 7;
1463 } else if (!strcmp(p, "efiapp")) {
1464 s->pe_subsystem = 10;
1465 } else if (!strcmp(p, "efiboot")) {
1466 s->pe_subsystem = 11;
1467 } else if (!strcmp(p, "efiruntime")) {
1468 s->pe_subsystem = 12;
1469 } else if (!strcmp(p, "efirom")) {
1470 s->pe_subsystem = 13;
1471 #elif defined(TCC_TARGET_ARM)
1472 if (!strcmp(p, "wince")) {
1473 s->pe_subsystem = 9;
1474 #endif
1475 } else
1476 goto err;
1477 #endif
1478 } else if (ret = link_option(option, "?whole-archive", &p), ret) {
1479 if (ret > 0)
1480 s->filetype |= AFF_WHOLE_ARCHIVE;
1481 else
1482 s->filetype &= ~AFF_WHOLE_ARCHIVE;
1483 } else if (p) {
1484 return 0;
1485 } else {
1486 err:
1487 tcc_error("unsupported linker option '%s'", option);
1490 if (ignoring && s->warn_unsupported)
1491 tcc_warning("unsupported linker option '%s'", option);
1493 option = skip_linker_arg(&p);
1495 return 1;
1498 typedef struct TCCOption {
1499 const char *name;
1500 uint16_t index;
1501 uint16_t flags;
1502 } TCCOption;
1504 enum {
1505 TCC_OPTION_HELP,
1506 TCC_OPTION_HELP2,
1507 TCC_OPTION_v,
1508 TCC_OPTION_I,
1509 TCC_OPTION_D,
1510 TCC_OPTION_U,
1511 TCC_OPTION_P,
1512 TCC_OPTION_L,
1513 TCC_OPTION_B,
1514 TCC_OPTION_l,
1515 TCC_OPTION_bench,
1516 TCC_OPTION_bt,
1517 TCC_OPTION_b,
1518 TCC_OPTION_ba,
1519 TCC_OPTION_g,
1520 TCC_OPTION_c,
1521 TCC_OPTION_dumpversion,
1522 TCC_OPTION_d,
1523 TCC_OPTION_static,
1524 TCC_OPTION_std,
1525 TCC_OPTION_shared,
1526 TCC_OPTION_soname,
1527 TCC_OPTION_o,
1528 TCC_OPTION_r,
1529 TCC_OPTION_s,
1530 TCC_OPTION_traditional,
1531 TCC_OPTION_Wl,
1532 TCC_OPTION_Wp,
1533 TCC_OPTION_W,
1534 TCC_OPTION_O,
1535 TCC_OPTION_mfloat_abi,
1536 TCC_OPTION_m,
1537 TCC_OPTION_f,
1538 TCC_OPTION_isystem,
1539 TCC_OPTION_iwithprefix,
1540 TCC_OPTION_include,
1541 TCC_OPTION_nostdinc,
1542 TCC_OPTION_nostdlib,
1543 TCC_OPTION_print_search_dirs,
1544 TCC_OPTION_rdynamic,
1545 TCC_OPTION_param,
1546 TCC_OPTION_pedantic,
1547 TCC_OPTION_pthread,
1548 TCC_OPTION_run,
1549 TCC_OPTION_w,
1550 TCC_OPTION_pipe,
1551 TCC_OPTION_E,
1552 TCC_OPTION_MD,
1553 TCC_OPTION_MF,
1554 TCC_OPTION_x,
1555 TCC_OPTION_ar,
1556 TCC_OPTION_impdef
1559 #define TCC_OPTION_HAS_ARG 0x0001
1560 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
1562 static const TCCOption tcc_options[] = {
1563 { "h", TCC_OPTION_HELP, 0 },
1564 { "-help", TCC_OPTION_HELP, 0 },
1565 { "?", TCC_OPTION_HELP, 0 },
1566 { "hh", TCC_OPTION_HELP2, 0 },
1567 { "v", TCC_OPTION_v, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1568 { "-version", TCC_OPTION_v, 0 }, /* handle as verbose, also prints version*/
1569 { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
1570 { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
1571 { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
1572 { "P", TCC_OPTION_P, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1573 { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
1574 { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
1575 { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG },
1576 { "bench", TCC_OPTION_bench, 0 },
1577 #ifdef CONFIG_TCC_BACKTRACE
1578 { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1579 #endif
1580 #ifdef CONFIG_TCC_BCHECK
1581 { "b", TCC_OPTION_b, 0 },
1582 #endif
1583 { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1584 { "c", TCC_OPTION_c, 0 },
1585 { "dumpversion", TCC_OPTION_dumpversion, 0},
1586 { "d", TCC_OPTION_d, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1587 { "static", TCC_OPTION_static, 0 },
1588 { "std", TCC_OPTION_std, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1589 { "shared", TCC_OPTION_shared, 0 },
1590 { "soname", TCC_OPTION_soname, TCC_OPTION_HAS_ARG },
1591 { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
1592 { "-param", TCC_OPTION_param, TCC_OPTION_HAS_ARG },
1593 { "pedantic", TCC_OPTION_pedantic, 0},
1594 { "pthread", TCC_OPTION_pthread, 0},
1595 { "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1596 { "rdynamic", TCC_OPTION_rdynamic, 0 },
1597 { "r", TCC_OPTION_r, 0 },
1598 { "s", TCC_OPTION_s, 0 },
1599 { "traditional", TCC_OPTION_traditional, 0 },
1600 { "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1601 { "Wp,", TCC_OPTION_Wp, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1602 { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1603 { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1604 #ifdef TCC_TARGET_ARM
1605 { "mfloat-abi", TCC_OPTION_mfloat_abi, TCC_OPTION_HAS_ARG },
1606 #endif
1607 { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1608 { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1609 { "isystem", TCC_OPTION_isystem, TCC_OPTION_HAS_ARG },
1610 { "include", TCC_OPTION_include, TCC_OPTION_HAS_ARG },
1611 { "nostdinc", TCC_OPTION_nostdinc, 0 },
1612 { "nostdlib", TCC_OPTION_nostdlib, 0 },
1613 { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
1614 { "w", TCC_OPTION_w, 0 },
1615 { "pipe", TCC_OPTION_pipe, 0},
1616 { "E", TCC_OPTION_E, 0},
1617 { "MD", TCC_OPTION_MD, 0},
1618 { "MF", TCC_OPTION_MF, TCC_OPTION_HAS_ARG },
1619 { "x", TCC_OPTION_x, TCC_OPTION_HAS_ARG },
1620 { "ar", TCC_OPTION_ar, 0},
1621 #ifdef TCC_TARGET_PE
1622 { "impdef", TCC_OPTION_impdef, 0},
1623 #endif
1624 { NULL, 0, 0 },
1627 static const FlagDef options_W[] = {
1628 { 0, 0, "all" },
1629 { offsetof(TCCState, warn_unsupported), 0, "unsupported" },
1630 { offsetof(TCCState, warn_write_strings), 0, "write-strings" },
1631 { offsetof(TCCState, warn_error), 0, "error" },
1632 { offsetof(TCCState, warn_gcc_compat), 0, "gcc-compat" },
1633 { offsetof(TCCState, warn_implicit_function_declaration), WD_ALL,
1634 "implicit-function-declaration" },
1635 { 0, 0, NULL }
1638 static const FlagDef options_f[] = {
1639 { offsetof(TCCState, char_is_unsigned), 0, "unsigned-char" },
1640 { offsetof(TCCState, char_is_unsigned), FD_INVERT, "signed-char" },
1641 { offsetof(TCCState, nocommon), FD_INVERT, "common" },
1642 { offsetof(TCCState, leading_underscore), 0, "leading-underscore" },
1643 { offsetof(TCCState, ms_extensions), 0, "ms-extensions" },
1644 { offsetof(TCCState, dollars_in_identifiers), 0, "dollars-in-identifiers" },
1645 { 0, 0, NULL }
1648 static const FlagDef options_m[] = {
1649 { offsetof(TCCState, ms_bitfields), 0, "ms-bitfields" },
1650 #ifdef TCC_TARGET_X86_64
1651 { offsetof(TCCState, nosse), FD_INVERT, "sse" },
1652 #endif
1653 { 0, 0, NULL }
1656 static void parse_option_D(TCCState *s1, const char *optarg)
1658 char *sym = tcc_strdup(optarg);
1659 char *value = strchr(sym, '=');
1660 if (value)
1661 *value++ = '\0';
1662 tcc_define_symbol(s1, sym, value);
1663 tcc_free(sym);
1666 static void args_parser_add_file(TCCState *s, const char* filename, int filetype)
1668 struct filespec *f = tcc_malloc(sizeof *f + strlen(filename));
1669 f->type = filetype;
1670 strcpy(f->name, filename);
1671 dynarray_add(&s->files, &s->nb_files, f);
1674 static int args_parser_make_argv(const char *r, int *argc, char ***argv)
1676 int ret = 0, q, c;
1677 CString str;
1678 for(;;) {
1679 while (c = (unsigned char)*r, c && c <= ' ')
1680 ++r;
1681 if (c == 0)
1682 break;
1683 q = 0;
1684 cstr_new(&str);
1685 while (c = (unsigned char)*r, c) {
1686 ++r;
1687 if (c == '\\' && (*r == '"' || *r == '\\')) {
1688 c = *r++;
1689 } else if (c == '"') {
1690 q = !q;
1691 continue;
1692 } else if (q == 0 && c <= ' ') {
1693 break;
1695 cstr_ccat(&str, c);
1697 cstr_ccat(&str, 0);
1698 //printf("<%s>\n", str.data), fflush(stdout);
1699 dynarray_add(argv, argc, tcc_strdup(str.data));
1700 cstr_free(&str);
1701 ++ret;
1703 return ret;
1706 /* read list file */
1707 static void args_parser_listfile(TCCState *s,
1708 const char *filename, int optind, int *pargc, char ***pargv)
1710 TCCState *s1 = s;
1711 int fd, i;
1712 size_t len;
1713 char *p;
1714 int argc = 0;
1715 char **argv = NULL;
1717 fd = open(filename, O_RDONLY | O_BINARY);
1718 if (fd < 0)
1719 tcc_error("listfile '%s' not found", filename);
1721 len = lseek(fd, 0, SEEK_END);
1722 p = tcc_malloc(len + 1), p[len] = 0;
1723 lseek(fd, 0, SEEK_SET), read(fd, p, len), close(fd);
1725 for (i = 0; i < *pargc; ++i)
1726 if (i == optind)
1727 args_parser_make_argv(p, &argc, &argv);
1728 else
1729 dynarray_add(&argv, &argc, tcc_strdup((*pargv)[i]));
1731 tcc_free(p);
1732 dynarray_reset(&s->argv, &s->argc);
1733 *pargc = s->argc = argc, *pargv = s->argv = argv;
1736 PUB_FUNC int tcc_parse_args(TCCState *s, int *pargc, char ***pargv, int optind)
1738 TCCState *s1 = s;
1739 const TCCOption *popt;
1740 const char *optarg, *r;
1741 const char *run = NULL;
1742 int x;
1743 CString linker_arg; /* collect -Wl options */
1744 int tool = 0, arg_start = 0, noaction = optind;
1745 char **argv = *pargv;
1746 int argc = *pargc;
1748 cstr_new(&linker_arg);
1750 while (optind < argc) {
1751 r = argv[optind];
1752 if (r[0] == '@' && r[1] != '\0') {
1753 args_parser_listfile(s, r + 1, optind, &argc, &argv);
1754 continue;
1756 optind++;
1757 if (tool) {
1758 if (r[0] == '-' && r[1] == 'v' && r[2] == 0)
1759 ++s->verbose;
1760 continue;
1762 reparse:
1763 if (r[0] != '-' || r[1] == '\0') {
1764 if (r[0] != '@') /* allow "tcc file(s) -run @ args ..." */
1765 args_parser_add_file(s, r, s->filetype);
1766 if (run) {
1767 tcc_set_options(s, run);
1768 arg_start = optind - 1;
1769 break;
1771 continue;
1774 /* find option in table */
1775 for(popt = tcc_options; ; ++popt) {
1776 const char *p1 = popt->name;
1777 const char *r1 = r + 1;
1778 if (p1 == NULL)
1779 tcc_error("invalid option -- '%s'", r);
1780 if (!strstart(p1, &r1))
1781 continue;
1782 optarg = r1;
1783 if (popt->flags & TCC_OPTION_HAS_ARG) {
1784 if (*r1 == '\0' && !(popt->flags & TCC_OPTION_NOSEP)) {
1785 if (optind >= argc)
1786 arg_err:
1787 tcc_error("argument to '%s' is missing", r);
1788 optarg = argv[optind++];
1790 } else if (*r1 != '\0')
1791 continue;
1792 break;
1795 switch(popt->index) {
1796 case TCC_OPTION_HELP:
1797 x = OPT_HELP;
1798 goto extra_action;
1799 case TCC_OPTION_HELP2:
1800 x = OPT_HELP2;
1801 goto extra_action;
1802 case TCC_OPTION_I:
1803 tcc_add_include_path(s, optarg);
1804 break;
1805 case TCC_OPTION_D:
1806 parse_option_D(s, optarg);
1807 break;
1808 case TCC_OPTION_U:
1809 tcc_undefine_symbol(s, optarg);
1810 break;
1811 case TCC_OPTION_L:
1812 tcc_add_library_path(s, optarg);
1813 break;
1814 case TCC_OPTION_B:
1815 /* set tcc utilities path (mainly for tcc development) */
1816 tcc_set_lib_path(s, optarg);
1817 break;
1818 case TCC_OPTION_l:
1819 args_parser_add_file(s, optarg, AFF_TYPE_LIB | (s->filetype & ~AFF_TYPE_MASK));
1820 s->nb_libraries++;
1821 break;
1822 case TCC_OPTION_pthread:
1823 s->option_pthread = 1;
1824 break;
1825 case TCC_OPTION_bench:
1826 s->do_bench = 1;
1827 break;
1828 #ifdef CONFIG_TCC_BACKTRACE
1829 case TCC_OPTION_bt:
1830 s->rt_num_callers = atoi(optarg);
1831 s->do_backtrace = 1;
1832 s->do_debug = 1;
1833 break;
1834 #endif
1835 #ifdef CONFIG_TCC_BCHECK
1836 case TCC_OPTION_b:
1837 s->do_bounds_check = 1;
1838 s->do_backtrace = 1;
1839 s->do_debug = 1;
1840 break;
1841 #endif
1842 case TCC_OPTION_g:
1843 s->do_debug = 1;
1844 break;
1845 case TCC_OPTION_c:
1846 x = TCC_OUTPUT_OBJ;
1847 set_output_type:
1848 if (s->output_type)
1849 tcc_warning("-%s: overriding compiler action already specified", popt->name);
1850 s->output_type = x;
1851 break;
1852 case TCC_OPTION_d:
1853 if (*optarg == 'D')
1854 s->dflag = 3;
1855 else if (*optarg == 'M')
1856 s->dflag = 7;
1857 else if (*optarg == 't')
1858 s->dflag = 16;
1859 else if (isnum(*optarg))
1860 s->g_debug |= atoi(optarg);
1861 else
1862 goto unsupported_option;
1863 break;
1864 case TCC_OPTION_static:
1865 s->static_link = 1;
1866 break;
1867 case TCC_OPTION_std:
1868 if (strcmp(optarg, "=c11") == 0)
1869 s->cversion = 201112;
1870 break;
1871 case TCC_OPTION_shared:
1872 x = TCC_OUTPUT_DLL;
1873 goto set_output_type;
1874 case TCC_OPTION_soname:
1875 s->soname = tcc_strdup(optarg);
1876 break;
1877 case TCC_OPTION_o:
1878 if (s->outfile) {
1879 tcc_warning("multiple -o option");
1880 tcc_free(s->outfile);
1882 s->outfile = tcc_strdup(optarg);
1883 break;
1884 case TCC_OPTION_r:
1885 /* generate a .o merging several output files */
1886 s->option_r = 1;
1887 x = TCC_OUTPUT_OBJ;
1888 goto set_output_type;
1889 case TCC_OPTION_isystem:
1890 tcc_add_sysinclude_path(s, optarg);
1891 break;
1892 case TCC_OPTION_include:
1893 cstr_printf(&s->cmdline_incl, "#include \"%s\"\n", optarg);
1894 break;
1895 case TCC_OPTION_nostdinc:
1896 s->nostdinc = 1;
1897 break;
1898 case TCC_OPTION_nostdlib:
1899 s->nostdlib = 1;
1900 break;
1901 case TCC_OPTION_run:
1902 #ifndef TCC_IS_NATIVE
1903 tcc_error("-run is not available in a cross compiler");
1904 #endif
1905 run = optarg;
1906 x = TCC_OUTPUT_MEMORY;
1907 goto set_output_type;
1908 case TCC_OPTION_v:
1909 do ++s->verbose; while (*optarg++ == 'v');
1910 ++noaction;
1911 break;
1912 case TCC_OPTION_f:
1913 if (set_flag(s, options_f, optarg) < 0)
1914 goto unsupported_option;
1915 break;
1916 #ifdef TCC_TARGET_ARM
1917 case TCC_OPTION_mfloat_abi:
1918 /* tcc doesn't support soft float yet */
1919 if (!strcmp(optarg, "softfp")) {
1920 s->float_abi = ARM_SOFTFP_FLOAT;
1921 tcc_undefine_symbol(s, "__ARM_PCS_VFP");
1922 } else if (!strcmp(optarg, "hard"))
1923 s->float_abi = ARM_HARD_FLOAT;
1924 else
1925 tcc_error("unsupported float abi '%s'", optarg);
1926 break;
1927 #endif
1928 case TCC_OPTION_m:
1929 if (set_flag(s, options_m, optarg) < 0) {
1930 if (x = atoi(optarg), x != 32 && x != 64)
1931 goto unsupported_option;
1932 if (PTR_SIZE != x/8)
1933 return x;
1934 ++noaction;
1936 break;
1937 case TCC_OPTION_W:
1938 s->warn_none = 0;
1939 if (optarg[0] && set_flag(s, options_W, optarg) < 0)
1940 goto unsupported_option;
1941 break;
1942 case TCC_OPTION_w:
1943 s->warn_none = 1;
1944 break;
1945 case TCC_OPTION_rdynamic:
1946 s->rdynamic = 1;
1947 break;
1948 case TCC_OPTION_Wl:
1949 if (linker_arg.size)
1950 --linker_arg.size, cstr_ccat(&linker_arg, ',');
1951 cstr_cat(&linker_arg, optarg, 0);
1952 if (tcc_set_linker(s, linker_arg.data))
1953 cstr_free(&linker_arg);
1954 break;
1955 case TCC_OPTION_Wp:
1956 r = optarg;
1957 goto reparse;
1958 case TCC_OPTION_E:
1959 x = TCC_OUTPUT_PREPROCESS;
1960 goto set_output_type;
1961 case TCC_OPTION_P:
1962 s->Pflag = atoi(optarg) + 1;
1963 break;
1964 case TCC_OPTION_MD:
1965 s->gen_deps = 1;
1966 break;
1967 case TCC_OPTION_MF:
1968 s->deps_outfile = tcc_strdup(optarg);
1969 break;
1970 case TCC_OPTION_dumpversion:
1971 printf ("%s\n", TCC_VERSION);
1972 exit(0);
1973 break;
1974 case TCC_OPTION_x:
1975 x = 0;
1976 if (*optarg == 'c')
1977 x = AFF_TYPE_C;
1978 else if (*optarg == 'a')
1979 x = AFF_TYPE_ASMPP;
1980 else if (*optarg == 'b')
1981 x = AFF_TYPE_BIN;
1982 else if (*optarg == 'n')
1983 x = AFF_TYPE_NONE;
1984 else
1985 tcc_warning("unsupported language '%s'", optarg);
1986 s->filetype = x | (s->filetype & ~AFF_TYPE_MASK);
1987 break;
1988 case TCC_OPTION_O:
1989 s->optimize = atoi(optarg);
1990 break;
1991 case TCC_OPTION_print_search_dirs:
1992 x = OPT_PRINT_DIRS;
1993 goto extra_action;
1994 case TCC_OPTION_impdef:
1995 x = OPT_IMPDEF;
1996 goto extra_action;
1997 case TCC_OPTION_ar:
1998 x = OPT_AR;
1999 extra_action:
2000 arg_start = optind - 1;
2001 if (arg_start != noaction)
2002 tcc_error("cannot parse %s here", r);
2003 tool = x;
2004 break;
2005 case TCC_OPTION_traditional:
2006 case TCC_OPTION_pedantic:
2007 case TCC_OPTION_pipe:
2008 case TCC_OPTION_s:
2009 /* ignored */
2010 break;
2011 default:
2012 unsupported_option:
2013 if (s->warn_unsupported)
2014 tcc_warning("unsupported option '%s'", r);
2015 break;
2018 if (linker_arg.size) {
2019 r = linker_arg.data;
2020 goto arg_err;
2022 *pargc = argc - arg_start;
2023 *pargv = argv + arg_start;
2024 if (tool)
2025 return tool;
2026 if (optind != noaction)
2027 return 0;
2028 if (s->verbose == 2)
2029 return OPT_PRINT_DIRS;
2030 if (s->verbose)
2031 return OPT_V;
2032 return OPT_HELP;
2035 LIBTCCAPI void tcc_set_options(TCCState *s, const char *r)
2037 char **argv = NULL;
2038 int argc = 0;
2039 args_parser_make_argv(r, &argc, &argv);
2040 tcc_parse_args(s, &argc, &argv, 0);
2041 dynarray_reset(&argv, &argc);
2044 PUB_FUNC void tcc_print_stats(TCCState *s1, unsigned total_time)
2046 if (total_time < 1)
2047 total_time = 1;
2048 if (total_bytes < 1)
2049 total_bytes = 1;
2050 fprintf(stderr, "* %d idents, %d lines, %d bytes\n"
2051 "* %0.3f s, %u lines/s, %0.1f MB/s\n",
2052 total_idents, total_lines, total_bytes,
2053 (double)total_time/1000,
2054 (unsigned)total_lines*1000/total_time,
2055 (double)total_bytes/1000/total_time);
2056 #ifdef MEM_DEBUG
2057 fprintf(stderr, "* %d bytes memory used\n", mem_max_size);
2058 #endif