riscv: fix more sign/zero-extension problems
[tinycc.git] / libtcc.c
blobb3c4fc8e24ecb8758bf2c7df884b13cad359583b
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 #include "tcc.h"
23 /********************************************************/
24 /* global variables */
26 /* use GNU C extensions */
27 ST_DATA int gnu_ext = 1;
29 /* use TinyCC extensions */
30 ST_DATA int tcc_ext = 1;
32 /* XXX: get rid of this ASAP */
33 ST_DATA struct TCCState *tcc_state;
35 static int nb_states;
37 /********************************************************/
39 #if ONE_SOURCE
40 #include "tccpp.c"
41 #include "tccgen.c"
42 #include "tccelf.c"
43 #include "tccrun.c"
44 #ifdef TCC_TARGET_I386
45 #include "i386-gen.c"
46 #include "i386-link.c"
47 #include "i386-asm.c"
48 #elif defined(TCC_TARGET_ARM)
49 #include "arm-gen.c"
50 #include "arm-link.c"
51 #include "arm-asm.c"
52 #elif defined(TCC_TARGET_ARM64)
53 #include "arm64-gen.c"
54 #include "arm64-link.c"
55 #elif defined(TCC_TARGET_C67)
56 #include "c67-gen.c"
57 #include "c67-link.c"
58 #include "tcccoff.c"
59 #elif defined(TCC_TARGET_X86_64)
60 #include "x86_64-gen.c"
61 #include "x86_64-link.c"
62 #include "i386-asm.c"
63 #elif defined(TCC_TARGET_RISCV64)
64 #include "riscv64-gen.c"
65 #include "riscv64-link.c"
66 #else
67 #error unknown target
68 #endif
69 #ifdef CONFIG_TCC_ASM
70 #include "tccasm.c"
71 #endif
72 #ifdef TCC_TARGET_PE
73 #include "tccpe.c"
74 #endif
75 #endif /* ONE_SOURCE */
77 /********************************************************/
78 #ifndef CONFIG_TCC_ASM
79 ST_FUNC void asm_instr(void)
81 tcc_error("inline asm() not supported");
83 ST_FUNC void asm_global_instr(void)
85 tcc_error("inline asm() not supported");
87 #endif
89 /********************************************************/
90 #ifdef _WIN32
91 ST_FUNC char *normalize_slashes(char *path)
93 char *p;
94 for (p = path; *p; ++p)
95 if (*p == '\\')
96 *p = '/';
97 return path;
100 static HMODULE tcc_module;
102 /* on win32, we suppose the lib and includes are at the location of 'tcc.exe' */
103 static void tcc_set_lib_path_w32(TCCState *s)
105 char path[1024], *p;
106 GetModuleFileNameA(tcc_module, path, sizeof path);
107 p = tcc_basename(normalize_slashes(strlwr(path)));
108 if (p > path)
109 --p;
110 *p = 0;
111 tcc_set_lib_path(s, path);
114 #ifdef TCC_TARGET_PE
115 static void tcc_add_systemdir(TCCState *s)
117 char buf[1000];
118 GetSystemDirectory(buf, sizeof buf);
119 tcc_add_library_path(s, normalize_slashes(buf));
121 #endif
123 #ifdef LIBTCC_AS_DLL
124 BOOL WINAPI DllMain (HINSTANCE hDll, DWORD dwReason, LPVOID lpReserved)
126 if (DLL_PROCESS_ATTACH == dwReason)
127 tcc_module = hDll;
128 return TRUE;
130 #endif
131 #endif
133 /********************************************************/
134 /* copy a string and truncate it. */
135 ST_FUNC char *pstrcpy(char *buf, int buf_size, const char *s)
137 char *q, *q_end;
138 int c;
140 if (buf_size > 0) {
141 q = buf;
142 q_end = buf + buf_size - 1;
143 while (q < q_end) {
144 c = *s++;
145 if (c == '\0')
146 break;
147 *q++ = c;
149 *q = '\0';
151 return buf;
154 /* strcat and truncate. */
155 ST_FUNC char *pstrcat(char *buf, int buf_size, const char *s)
157 int len;
158 len = strlen(buf);
159 if (len < buf_size)
160 pstrcpy(buf + len, buf_size - len, s);
161 return buf;
164 ST_FUNC char *pstrncpy(char *out, const char *in, size_t num)
166 memcpy(out, in, num);
167 out[num] = '\0';
168 return out;
171 /* extract the basename of a file */
172 PUB_FUNC char *tcc_basename(const char *name)
174 char *p = strchr(name, 0);
175 while (p > name && !IS_DIRSEP(p[-1]))
176 --p;
177 return p;
180 /* extract extension part of a file
182 * (if no extension, return pointer to end-of-string)
184 PUB_FUNC char *tcc_fileextension (const char *name)
186 char *b = tcc_basename(name);
187 char *e = strrchr(b, '.');
188 return e ? e : strchr(b, 0);
191 /********************************************************/
192 /* memory management */
194 #undef free
195 #undef malloc
196 #undef realloc
198 #ifndef MEM_DEBUG
200 PUB_FUNC void tcc_free(void *ptr)
202 free(ptr);
205 PUB_FUNC void *tcc_malloc(unsigned long size)
207 void *ptr;
208 ptr = malloc(size);
209 if (!ptr && size)
210 tcc_error("memory full (malloc)");
211 return ptr;
214 PUB_FUNC void *tcc_mallocz(unsigned long size)
216 void *ptr;
217 ptr = tcc_malloc(size);
218 memset(ptr, 0, size);
219 return ptr;
222 PUB_FUNC void *tcc_realloc(void *ptr, unsigned long size)
224 void *ptr1;
225 ptr1 = realloc(ptr, size);
226 if (!ptr1 && size)
227 tcc_error("memory full (realloc)");
228 return ptr1;
231 PUB_FUNC char *tcc_strdup(const char *str)
233 char *ptr;
234 ptr = tcc_malloc(strlen(str) + 1);
235 strcpy(ptr, str);
236 return ptr;
239 PUB_FUNC void tcc_memcheck(void)
243 #else
245 #define MEM_DEBUG_MAGIC1 0xFEEDDEB1
246 #define MEM_DEBUG_MAGIC2 0xFEEDDEB2
247 #define MEM_DEBUG_MAGIC3 0xFEEDDEB3
248 #define MEM_DEBUG_FILE_LEN 40
249 #define MEM_DEBUG_CHECK3(header) \
250 ((mem_debug_header_t*)((char*)header + header->size))->magic3
251 #define MEM_USER_PTR(header) \
252 ((char *)header + offsetof(mem_debug_header_t, magic3))
253 #define MEM_HEADER_PTR(ptr) \
254 (mem_debug_header_t *)((char*)ptr - offsetof(mem_debug_header_t, magic3))
256 struct mem_debug_header {
257 unsigned magic1;
258 unsigned size;
259 struct mem_debug_header *prev;
260 struct mem_debug_header *next;
261 int line_num;
262 char file_name[MEM_DEBUG_FILE_LEN + 1];
263 unsigned magic2;
264 ALIGNED(16) unsigned magic3;
267 typedef struct mem_debug_header mem_debug_header_t;
269 static mem_debug_header_t *mem_debug_chain;
270 static unsigned mem_cur_size;
271 static unsigned mem_max_size;
273 static mem_debug_header_t *malloc_check(void *ptr, const char *msg)
275 mem_debug_header_t * header = MEM_HEADER_PTR(ptr);
276 if (header->magic1 != MEM_DEBUG_MAGIC1 ||
277 header->magic2 != MEM_DEBUG_MAGIC2 ||
278 MEM_DEBUG_CHECK3(header) != MEM_DEBUG_MAGIC3 ||
279 header->size == (unsigned)-1) {
280 fprintf(stderr, "%s check failed\n", msg);
281 if (header->magic1 == MEM_DEBUG_MAGIC1)
282 fprintf(stderr, "%s:%u: block allocated here.\n",
283 header->file_name, header->line_num);
284 exit(1);
286 return header;
289 PUB_FUNC void *tcc_malloc_debug(unsigned long size, const char *file, int line)
291 int ofs;
292 mem_debug_header_t *header;
294 header = malloc(sizeof(mem_debug_header_t) + size);
295 if (!header)
296 tcc_error("memory full (malloc)");
298 header->magic1 = MEM_DEBUG_MAGIC1;
299 header->magic2 = MEM_DEBUG_MAGIC2;
300 header->size = size;
301 MEM_DEBUG_CHECK3(header) = MEM_DEBUG_MAGIC3;
302 header->line_num = line;
303 ofs = strlen(file) - MEM_DEBUG_FILE_LEN;
304 strncpy(header->file_name, file + (ofs > 0 ? ofs : 0), MEM_DEBUG_FILE_LEN);
305 header->file_name[MEM_DEBUG_FILE_LEN] = 0;
307 header->next = mem_debug_chain;
308 header->prev = NULL;
309 if (header->next)
310 header->next->prev = header;
311 mem_debug_chain = header;
313 mem_cur_size += size;
314 if (mem_cur_size > mem_max_size)
315 mem_max_size = mem_cur_size;
317 return MEM_USER_PTR(header);
320 PUB_FUNC void tcc_free_debug(void *ptr)
322 mem_debug_header_t *header;
323 if (!ptr)
324 return;
325 header = malloc_check(ptr, "tcc_free");
326 mem_cur_size -= header->size;
327 header->size = (unsigned)-1;
328 if (header->next)
329 header->next->prev = header->prev;
330 if (header->prev)
331 header->prev->next = header->next;
332 if (header == mem_debug_chain)
333 mem_debug_chain = header->next;
334 free(header);
337 PUB_FUNC void *tcc_mallocz_debug(unsigned long size, const char *file, int line)
339 void *ptr;
340 ptr = tcc_malloc_debug(size,file,line);
341 memset(ptr, 0, size);
342 return ptr;
345 PUB_FUNC void *tcc_realloc_debug(void *ptr, unsigned long size, const char *file, int line)
347 mem_debug_header_t *header;
348 int mem_debug_chain_update = 0;
349 if (!ptr)
350 return tcc_malloc_debug(size, file, line);
351 header = malloc_check(ptr, "tcc_realloc");
352 mem_cur_size -= header->size;
353 mem_debug_chain_update = (header == mem_debug_chain);
354 header = realloc(header, sizeof(mem_debug_header_t) + size);
355 if (!header)
356 tcc_error("memory full (realloc)");
357 header->size = size;
358 MEM_DEBUG_CHECK3(header) = MEM_DEBUG_MAGIC3;
359 if (header->next)
360 header->next->prev = header;
361 if (header->prev)
362 header->prev->next = header;
363 if (mem_debug_chain_update)
364 mem_debug_chain = header;
365 mem_cur_size += size;
366 if (mem_cur_size > mem_max_size)
367 mem_max_size = mem_cur_size;
368 return MEM_USER_PTR(header);
371 PUB_FUNC char *tcc_strdup_debug(const char *str, const char *file, int line)
373 char *ptr;
374 ptr = tcc_malloc_debug(strlen(str) + 1, file, line);
375 strcpy(ptr, str);
376 return ptr;
379 PUB_FUNC void tcc_memcheck(void)
381 if (mem_cur_size) {
382 mem_debug_header_t *header = mem_debug_chain;
383 fprintf(stderr, "MEM_DEBUG: mem_leak= %d bytes, mem_max_size= %d bytes\n",
384 mem_cur_size, mem_max_size);
385 while (header) {
386 fprintf(stderr, "%s:%u: error: %u bytes leaked\n",
387 header->file_name, header->line_num, header->size);
388 header = header->next;
390 #if MEM_DEBUG-0 == 2
391 exit(2);
392 #endif
395 #endif /* MEM_DEBUG */
397 #define free(p) use_tcc_free(p)
398 #define malloc(s) use_tcc_malloc(s)
399 #define realloc(p, s) use_tcc_realloc(p, s)
401 /********************************************************/
402 /* dynarrays */
404 ST_FUNC void dynarray_add(void *ptab, int *nb_ptr, void *data)
406 int nb, nb_alloc;
407 void **pp;
409 nb = *nb_ptr;
410 pp = *(void ***)ptab;
411 /* every power of two we double array size */
412 if ((nb & (nb - 1)) == 0) {
413 if (!nb)
414 nb_alloc = 1;
415 else
416 nb_alloc = nb * 2;
417 pp = tcc_realloc(pp, nb_alloc * sizeof(void *));
418 *(void***)ptab = pp;
420 pp[nb++] = data;
421 *nb_ptr = nb;
424 ST_FUNC void dynarray_reset(void *pp, int *n)
426 void **p;
427 for (p = *(void***)pp; *n; ++p, --*n)
428 if (*p)
429 tcc_free(*p);
430 tcc_free(*(void**)pp);
431 *(void**)pp = NULL;
434 static void tcc_split_path(TCCState *s, void *p_ary, int *p_nb_ary, const char *in)
436 const char *p;
437 do {
438 int c;
439 CString str;
441 cstr_new(&str);
442 for (p = in; c = *p, c != '\0' && c != PATHSEP[0]; ++p) {
443 if (c == '{' && p[1] && p[2] == '}') {
444 c = p[1], p += 2;
445 if (c == 'B')
446 cstr_cat(&str, s->tcc_lib_path, -1);
447 } else {
448 cstr_ccat(&str, c);
451 if (str.size) {
452 cstr_ccat(&str, '\0');
453 dynarray_add(p_ary, p_nb_ary, tcc_strdup(str.data));
455 cstr_free(&str);
456 in = p+1;
457 } while (*p);
460 /********************************************************/
462 static void strcat_vprintf(char *buf, int buf_size, const char *fmt, va_list ap)
464 int len;
465 len = strlen(buf);
466 vsnprintf(buf + len, buf_size - len, fmt, ap);
469 static void strcat_printf(char *buf, int buf_size, const char *fmt, ...)
471 va_list ap;
472 va_start(ap, fmt);
473 strcat_vprintf(buf, buf_size, fmt, ap);
474 va_end(ap);
477 static void error1(TCCState *s1, int is_warning, const char *fmt, va_list ap)
479 char buf[2048];
480 BufferedFile **pf, *f;
482 buf[0] = '\0';
483 /* use upper file if inline ":asm:" or token ":paste:" */
484 for (f = file; f && f->filename[0] == ':'; f = f->prev)
486 if (f) {
487 for(pf = s1->include_stack; pf < s1->include_stack_ptr; pf++)
488 strcat_printf(buf, sizeof(buf), "In file included from %s:%d:\n",
489 (*pf)->filename, (*pf)->line_num);
490 if (s1->error_set_jmp_enabled) {
491 strcat_printf(buf, sizeof(buf), "%s:%d: ",
492 f->filename, f->line_num - !!(tok_flags & TOK_FLAG_BOL));
493 } else {
494 strcat_printf(buf, sizeof(buf), "%s: ",
495 f->filename);
497 } else {
498 strcat_printf(buf, sizeof(buf), "tcc: ");
500 if (is_warning)
501 strcat_printf(buf, sizeof(buf), "warning: ");
502 else
503 strcat_printf(buf, sizeof(buf), "error: ");
504 strcat_vprintf(buf, sizeof(buf), fmt, ap);
506 if (!s1->error_func) {
507 /* default case: stderr */
508 if (s1->output_type == TCC_OUTPUT_PREPROCESS && s1->ppfp == stdout)
509 /* print a newline during tcc -E */
510 printf("\n"), fflush(stdout);
511 fflush(stdout); /* flush -v output */
512 fprintf(stderr, "%s\n", buf);
513 fflush(stderr); /* print error/warning now (win32) */
514 } else {
515 s1->error_func(s1->error_opaque, buf);
517 if (!is_warning || s1->warn_error)
518 s1->nb_errors++;
521 LIBTCCAPI void tcc_set_error_func(TCCState *s, void *error_opaque,
522 void (*error_func)(void *opaque, const char *msg))
524 s->error_opaque = error_opaque;
525 s->error_func = error_func;
528 /* error without aborting current compilation */
529 PUB_FUNC void tcc_error_noabort(const char *fmt, ...)
531 TCCState *s1 = tcc_state;
532 va_list ap;
534 va_start(ap, fmt);
535 error1(s1, 0, fmt, ap);
536 va_end(ap);
539 PUB_FUNC void tcc_error(const char *fmt, ...)
541 TCCState *s1 = tcc_state;
542 va_list ap;
544 va_start(ap, fmt);
545 error1(s1, 0, fmt, ap);
546 va_end(ap);
547 /* better than nothing: in some cases, we accept to handle errors */
548 if (s1->error_set_jmp_enabled) {
549 longjmp(s1->error_jmp_buf, 1);
550 } else {
551 /* XXX: eliminate this someday */
552 exit(1);
556 PUB_FUNC void tcc_warning(const char *fmt, ...)
558 TCCState *s1 = tcc_state;
559 va_list ap;
561 if (s1->warn_none)
562 return;
564 va_start(ap, fmt);
565 error1(s1, 1, fmt, ap);
566 va_end(ap);
569 /********************************************************/
570 /* I/O layer */
572 ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen)
574 BufferedFile *bf;
575 int buflen = initlen ? initlen : IO_BUF_SIZE;
577 bf = tcc_mallocz(sizeof(BufferedFile) + buflen);
578 bf->buf_ptr = bf->buffer;
579 bf->buf_end = bf->buffer + initlen;
580 bf->buf_end[0] = CH_EOB; /* put eob symbol */
581 pstrcpy(bf->filename, sizeof(bf->filename), filename);
582 bf->true_filename = bf->filename;
583 bf->line_num = 1;
584 bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
585 bf->fd = -1;
586 bf->prev = file;
587 file = bf;
588 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
591 ST_FUNC void tcc_close(void)
593 BufferedFile *bf = file;
594 if (bf->fd > 0) {
595 close(bf->fd);
596 total_lines += bf->line_num;
598 if (bf->true_filename != bf->filename)
599 tcc_free(bf->true_filename);
600 file = bf->prev;
601 tcc_free(bf);
604 ST_FUNC int tcc_open(TCCState *s1, const char *filename)
606 int fd;
607 if (strcmp(filename, "-") == 0)
608 fd = 0, filename = "<stdin>";
609 else
610 fd = open(filename, O_RDONLY | O_BINARY);
611 if ((s1->verbose == 2 && fd >= 0) || s1->verbose == 3)
612 printf("%s %*s%s\n", fd < 0 ? "nf":"->",
613 (int)(s1->include_stack_ptr - s1->include_stack), "", filename);
614 if (fd < 0)
615 return -1;
616 tcc_open_bf(s1, filename, 0);
617 #ifdef _WIN32
618 normalize_slashes(file->filename);
619 #endif
620 file->fd = fd;
621 return fd;
624 /* compile the file opened in 'file'. Return non zero if errors. */
625 static int tcc_compile(TCCState *s1, int filetype)
627 Sym *define_start;
628 int is_asm;
630 define_start = define_stack;
631 is_asm = !!(filetype & (AFF_TYPE_ASM|AFF_TYPE_ASMPP));
632 tccelf_begin_file(s1);
634 if (setjmp(s1->error_jmp_buf) == 0) {
635 s1->nb_errors = 0;
636 s1->error_set_jmp_enabled = 1;
638 preprocess_start(s1, is_asm);
639 if (s1->output_type == TCC_OUTPUT_PREPROCESS) {
640 tcc_preprocess(s1);
641 } else if (is_asm) {
642 #ifdef CONFIG_TCC_ASM
643 tcc_assemble(s1, !!(filetype & AFF_TYPE_ASMPP));
644 #else
645 tcc_error_noabort("asm not supported");
646 #endif
647 } else {
648 tccgen_compile(s1);
651 s1->error_set_jmp_enabled = 0;
653 preprocess_end(s1);
654 free_inline_functions(s1);
655 /* reset define stack, but keep -D and built-ins */
656 free_defines(define_start);
657 sym_pop(&global_stack, NULL, 0);
658 sym_pop(&local_stack, NULL, 0);
659 tccelf_end_file(s1);
660 return s1->nb_errors != 0 ? -1 : 0;
663 LIBTCCAPI int tcc_compile_string(TCCState *s, const char *str)
665 int len, ret;
667 len = strlen(str);
668 tcc_open_bf(s, "<string>", len);
669 memcpy(file->buffer, str, len);
670 ret = tcc_compile(s, s->filetype);
671 tcc_close();
672 return ret;
675 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
676 LIBTCCAPI void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
678 int len1, len2;
679 /* default value */
680 if (!value)
681 value = "1";
682 len1 = strlen(sym);
683 len2 = strlen(value);
685 /* init file structure */
686 tcc_open_bf(s1, "<define>", len1 + len2 + 1);
687 memcpy(file->buffer, sym, len1);
688 file->buffer[len1] = ' ';
689 memcpy(file->buffer + len1 + 1, value, len2);
691 /* parse with define parser */
692 next_nomacro();
693 parse_define();
694 tcc_close();
697 /* undefine a preprocessor symbol */
698 LIBTCCAPI void tcc_undefine_symbol(TCCState *s1, const char *sym)
700 TokenSym *ts;
701 Sym *s;
702 ts = tok_alloc(sym, strlen(sym));
703 s = define_find(ts->tok);
704 if (s) {
705 define_undef(s);
706 tok_str_free_str(s->d);
707 s->d = NULL;
711 /* cleanup all static data used during compilation */
712 static void tcc_cleanup(void)
714 if (NULL == tcc_state)
715 return;
716 while (file)
717 tcc_close();
718 tccpp_delete(tcc_state);
719 tcc_state = NULL;
720 /* free sym_pools */
721 dynarray_reset(&sym_pools, &nb_sym_pools);
722 /* reset symbol stack */
723 sym_free_first = NULL;
726 LIBTCCAPI TCCState *tcc_new(void)
728 TCCState *s;
730 tcc_cleanup();
732 s = tcc_mallocz(sizeof(TCCState));
733 if (!s)
734 return NULL;
735 tcc_state = s;
736 ++nb_states;
738 s->nocommon = 1;
739 s->dollars_in_identifiers = 1; /*on by default like in gcc/clang*/
740 s->cversion = 199901; /* default unless -std=c11 is supplied */
741 s->warn_implicit_function_declaration = 1;
742 s->ms_extensions = 1;
744 #ifdef CHAR_IS_UNSIGNED
745 s->char_is_unsigned = 1;
746 #endif
747 #ifdef TCC_TARGET_I386
748 s->seg_size = 32;
749 #endif
750 /* enable this if you want symbols with leading underscore on windows: */
751 #if 0 /* def TCC_TARGET_PE */
752 s->leading_underscore = 1;
753 #endif
754 #ifdef _WIN32
755 tcc_set_lib_path_w32(s);
756 #else
757 tcc_set_lib_path(s, CONFIG_TCCDIR);
758 #endif
759 tccelf_new(s);
760 tccpp_new(s);
762 /* we add dummy defines for some special macros to speed up tests
763 and to have working defined() */
764 define_push(TOK___LINE__, MACRO_OBJ, NULL, NULL);
765 define_push(TOK___FILE__, MACRO_OBJ, NULL, NULL);
766 define_push(TOK___DATE__, MACRO_OBJ, NULL, NULL);
767 define_push(TOK___TIME__, MACRO_OBJ, NULL, NULL);
768 define_push(TOK___COUNTER__, MACRO_OBJ, NULL, NULL);
770 /* define __TINYC__ 92X */
771 char buffer[32]; int a,b,c;
772 sscanf(TCC_VERSION, "%d.%d.%d", &a, &b, &c);
773 sprintf(buffer, "%d", a*10000 + b*100 + c);
774 tcc_define_symbol(s, "__TINYC__", buffer);
777 /* standard defines */
778 tcc_define_symbol(s, "__STDC__", NULL);
779 tcc_define_symbol(s, "__STDC_VERSION__", "199901L");
780 tcc_define_symbol(s, "__STDC_HOSTED__", NULL);
782 /* target defines */
783 #if defined(TCC_TARGET_I386)
784 tcc_define_symbol(s, "__i386__", NULL);
785 tcc_define_symbol(s, "__i386", NULL);
786 tcc_define_symbol(s, "i386", NULL);
787 #elif defined(TCC_TARGET_X86_64)
788 tcc_define_symbol(s, "__x86_64__", NULL);
789 #elif defined(TCC_TARGET_ARM)
790 tcc_define_symbol(s, "__ARM_ARCH_4__", NULL);
791 tcc_define_symbol(s, "__arm_elf__", NULL);
792 tcc_define_symbol(s, "__arm_elf", NULL);
793 tcc_define_symbol(s, "arm_elf", NULL);
794 tcc_define_symbol(s, "__arm__", NULL);
795 tcc_define_symbol(s, "__arm", NULL);
796 tcc_define_symbol(s, "arm", NULL);
797 tcc_define_symbol(s, "__APCS_32__", NULL);
798 tcc_define_symbol(s, "__ARMEL__", NULL);
799 #if defined(TCC_ARM_EABI)
800 tcc_define_symbol(s, "__ARM_EABI__", NULL);
801 #endif
802 #if defined(TCC_ARM_HARDFLOAT)
803 s->float_abi = ARM_HARD_FLOAT;
804 tcc_define_symbol(s, "__ARM_PCS_VFP", NULL);
805 #else
806 s->float_abi = ARM_SOFTFP_FLOAT;
807 #endif
808 #elif defined(TCC_TARGET_ARM64)
809 tcc_define_symbol(s, "__aarch64__", NULL);
810 #elif defined TCC_TARGET_C67
811 tcc_define_symbol(s, "__C67__", NULL);
812 #elif defined TCC_TARGET_RISCV64
813 tcc_define_symbol(s, "__riscv", NULL);
814 tcc_define_symbol(s, "__riscv_xlen", "64");
815 tcc_define_symbol(s, "__riscv_flen", "64");
816 tcc_define_symbol(s, "__riscv_div", NULL);
817 tcc_define_symbol(s, "__riscv_mul", NULL);
818 tcc_define_symbol(s, "__riscv_fdiv", NULL);
819 tcc_define_symbol(s, "__riscv_fsqrt", NULL);
820 tcc_define_symbol(s, "__riscv_float_abi_double", NULL);
821 #endif
823 #ifdef TCC_TARGET_PE
824 tcc_define_symbol(s, "_WIN32", NULL);
825 # ifdef TCC_TARGET_X86_64
826 tcc_define_symbol(s, "_WIN64", NULL);
827 # endif
828 #else
829 tcc_define_symbol(s, "__unix__", NULL);
830 tcc_define_symbol(s, "__unix", NULL);
831 tcc_define_symbol(s, "unix", NULL);
832 # if defined(__linux__)
833 tcc_define_symbol(s, "__linux__", NULL);
834 tcc_define_symbol(s, "__linux", NULL);
835 # endif
836 # if defined(__FreeBSD__)
837 tcc_define_symbol(s, "__FreeBSD__", "__FreeBSD__");
838 /* No 'Thread Storage Local' on FreeBSD with tcc */
839 tcc_define_symbol(s, "__NO_TLS", NULL);
840 # endif
841 # if defined(__FreeBSD_kernel__)
842 tcc_define_symbol(s, "__FreeBSD_kernel__", NULL);
843 # endif
844 # if defined(__NetBSD__)
845 tcc_define_symbol(s, "__NetBSD__", "__NetBSD__");
846 # endif
847 # if defined(__OpenBSD__)
848 tcc_define_symbol(s, "__OpenBSD__", "__OpenBSD__");
849 # endif
850 #endif
852 /* TinyCC & gcc defines */
853 #if PTR_SIZE == 4
854 /* 32bit systems. */
855 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned int");
856 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "int");
857 tcc_define_symbol(s, "__ILP32__", NULL);
858 #elif LONG_SIZE == 4
859 /* 64bit Windows. */
860 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned long long");
861 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "long long");
862 tcc_define_symbol(s, "__LLP64__", NULL);
863 #else
864 /* Other 64bit systems. */
865 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned long");
866 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "long");
867 tcc_define_symbol(s, "__LP64__", NULL);
868 #endif
869 tcc_define_symbol(s, "__SIZEOF_POINTER__", PTR_SIZE == 4 ? "4" : "8");
871 #ifdef TCC_TARGET_PE
872 tcc_define_symbol(s, "__WCHAR_TYPE__", "unsigned short");
873 tcc_define_symbol(s, "__WINT_TYPE__", "unsigned short");
874 #else
875 tcc_define_symbol(s, "__WCHAR_TYPE__", "int");
876 /* wint_t is unsigned int by default, but (signed) int on BSDs
877 and unsigned short on windows. Other OSes might have still
878 other conventions, sigh. */
879 # if defined(__FreeBSD__) || defined (__FreeBSD_kernel__) \
880 || defined(__NetBSD__) || defined(__OpenBSD__)
881 tcc_define_symbol(s, "__WINT_TYPE__", "int");
882 # ifdef __FreeBSD__
883 /* define __GNUC__ to have some useful stuff from sys/cdefs.h
884 that are unconditionally used in FreeBSDs other system headers :/ */
885 tcc_define_symbol(s, "__GNUC__", "2");
886 tcc_define_symbol(s, "__GNUC_MINOR__", "7");
887 tcc_define_symbol(s, "__builtin_alloca", "alloca");
888 # endif
889 # else
890 tcc_define_symbol(s, "__WINT_TYPE__", "unsigned int");
891 /* glibc defines */
892 tcc_define_symbol(s, "__REDIRECT(name, proto, alias)",
893 "name proto __asm__ (#alias)");
894 tcc_define_symbol(s, "__REDIRECT_NTH(name, proto, alias)",
895 "name proto __asm__ (#alias) __THROW");
896 # endif
897 # if defined(TCC_MUSL)
898 tcc_define_symbol(s, "__DEFINED_va_list", "");
899 tcc_define_symbol(s, "__DEFINED___isoc_va_list", "");
900 tcc_define_symbol(s, "__isoc_va_list", "void *");
901 # endif /* TCC_MUSL */
902 /* Some GCC builtins that are simple to express as macros. */
903 tcc_define_symbol(s, "__builtin_extract_return_addr(x)", "x");
904 #endif /* ndef TCC_TARGET_PE */
905 return s;
908 LIBTCCAPI void tcc_delete(TCCState *s1)
910 tcc_cleanup();
912 /* free sections */
913 tccelf_delete(s1);
915 /* free library paths */
916 dynarray_reset(&s1->library_paths, &s1->nb_library_paths);
917 dynarray_reset(&s1->crt_paths, &s1->nb_crt_paths);
919 /* free include paths */
920 dynarray_reset(&s1->cached_includes, &s1->nb_cached_includes);
921 dynarray_reset(&s1->include_paths, &s1->nb_include_paths);
922 dynarray_reset(&s1->sysinclude_paths, &s1->nb_sysinclude_paths);
923 dynarray_reset(&s1->cmd_include_files, &s1->nb_cmd_include_files);
925 tcc_free(s1->tcc_lib_path);
926 tcc_free(s1->soname);
927 tcc_free(s1->rpath);
928 tcc_free(s1->init_symbol);
929 tcc_free(s1->fini_symbol);
930 tcc_free(s1->outfile);
931 tcc_free(s1->deps_outfile);
932 dynarray_reset(&s1->files, &s1->nb_files);
933 dynarray_reset(&s1->target_deps, &s1->nb_target_deps);
934 dynarray_reset(&s1->pragma_libs, &s1->nb_pragma_libs);
935 dynarray_reset(&s1->argv, &s1->argc);
937 #ifdef TCC_IS_NATIVE
938 /* free runtime memory */
939 tcc_run_free(s1);
940 #endif
942 tcc_free(s1);
943 if (0 == --nb_states)
944 tcc_memcheck();
947 LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type)
949 s->output_type = output_type;
951 /* always elf for objects */
952 if (output_type == TCC_OUTPUT_OBJ)
953 s->output_format = TCC_OUTPUT_FORMAT_ELF;
955 if (s->char_is_unsigned)
956 tcc_define_symbol(s, "__CHAR_UNSIGNED__", NULL);
958 if (!s->nostdinc) {
959 /* default include paths */
960 /* -isystem paths have already been handled */
961 tcc_add_sysinclude_path(s, CONFIG_TCC_SYSINCLUDEPATHS);
964 #ifdef CONFIG_TCC_BCHECK
965 if (s->do_bounds_check) {
966 /* if bound checking, then add corresponding sections */
967 tccelf_bounds_new(s);
968 /* define symbol */
969 tcc_define_symbol(s, "__BOUNDS_CHECKING_ON", NULL);
971 #endif
972 if (s->do_debug) {
973 /* add debug sections */
974 tccelf_stab_new(s);
977 tcc_add_library_path(s, CONFIG_TCC_LIBPATHS);
979 #ifdef TCC_TARGET_PE
980 # ifdef _WIN32
981 if (!s->nostdlib && output_type != TCC_OUTPUT_OBJ)
982 tcc_add_systemdir(s);
983 # endif
984 #else
985 /* paths for crt objects */
986 tcc_split_path(s, &s->crt_paths, &s->nb_crt_paths, CONFIG_TCC_CRTPREFIX);
987 /* add libc crt1/crti objects */
988 if ((output_type == TCC_OUTPUT_EXE || output_type == TCC_OUTPUT_DLL) &&
989 !s->nostdlib) {
990 if (output_type != TCC_OUTPUT_DLL)
991 tcc_add_crt(s, "crt1.o");
992 tcc_add_crt(s, "crti.o");
994 #endif
995 return 0;
998 LIBTCCAPI int tcc_add_include_path(TCCState *s, const char *pathname)
1000 tcc_split_path(s, &s->include_paths, &s->nb_include_paths, pathname);
1001 return 0;
1004 LIBTCCAPI int tcc_add_sysinclude_path(TCCState *s, const char *pathname)
1006 tcc_split_path(s, &s->sysinclude_paths, &s->nb_sysinclude_paths, pathname);
1007 return 0;
1010 ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
1012 int ret;
1014 /* open the file */
1015 ret = tcc_open(s1, filename);
1016 if (ret < 0) {
1017 if (flags & AFF_PRINT_ERROR)
1018 tcc_error_noabort("file '%s' not found", filename);
1019 return ret;
1022 /* update target deps */
1023 dynarray_add(&s1->target_deps, &s1->nb_target_deps,
1024 tcc_strdup(filename));
1026 if (flags & AFF_TYPE_BIN) {
1027 ElfW(Ehdr) ehdr;
1028 int fd, obj_type;
1030 fd = file->fd;
1031 obj_type = tcc_object_type(fd, &ehdr);
1032 lseek(fd, 0, SEEK_SET);
1034 #ifdef TCC_TARGET_MACHO
1035 if (0 == obj_type && 0 == strcmp(tcc_fileextension(filename), ".dylib"))
1036 obj_type = AFF_BINTYPE_DYN;
1037 #endif
1039 switch (obj_type) {
1040 case AFF_BINTYPE_REL:
1041 ret = tcc_load_object_file(s1, fd, 0);
1042 break;
1043 #ifndef TCC_TARGET_PE
1044 case AFF_BINTYPE_DYN:
1045 if (s1->output_type == TCC_OUTPUT_MEMORY) {
1046 ret = 0;
1047 #ifdef TCC_IS_NATIVE
1048 if (NULL == dlopen(filename, RTLD_GLOBAL | RTLD_LAZY))
1049 ret = -1;
1050 #endif
1051 } else {
1052 ret = tcc_load_dll(s1, fd, filename,
1053 (flags & AFF_REFERENCED_DLL) != 0);
1055 break;
1056 #endif
1057 case AFF_BINTYPE_AR:
1058 ret = tcc_load_archive(s1, fd, !(flags & AFF_WHOLE_ARCHIVE));
1059 break;
1060 #ifdef TCC_TARGET_COFF
1061 case AFF_BINTYPE_C67:
1062 ret = tcc_load_coff(s1, fd);
1063 break;
1064 #endif
1065 default:
1066 #ifdef TCC_TARGET_PE
1067 ret = pe_load_file(s1, filename, fd);
1068 #else
1069 /* as GNU ld, consider it is an ld script if not recognized */
1070 ret = tcc_load_ldscript(s1);
1071 #endif
1072 if (ret < 0)
1073 tcc_error_noabort("unrecognized file type");
1074 break;
1076 } else {
1077 ret = tcc_compile(s1, flags);
1079 tcc_close();
1080 return ret;
1083 LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename)
1085 int filetype = s->filetype;
1086 if (0 == (filetype & AFF_TYPE_MASK)) {
1087 /* use a file extension to detect a filetype */
1088 const char *ext = tcc_fileextension(filename);
1089 if (ext[0]) {
1090 ext++;
1091 if (!strcmp(ext, "S"))
1092 filetype = AFF_TYPE_ASMPP;
1093 else if (!strcmp(ext, "s"))
1094 filetype = AFF_TYPE_ASM;
1095 else if (!PATHCMP(ext, "c") || !PATHCMP(ext, "i"))
1096 filetype = AFF_TYPE_C;
1097 else
1098 filetype |= AFF_TYPE_BIN;
1099 } else {
1100 filetype = AFF_TYPE_C;
1103 return tcc_add_file_internal(s, filename, filetype | AFF_PRINT_ERROR);
1106 LIBTCCAPI int tcc_add_library_path(TCCState *s, const char *pathname)
1108 tcc_split_path(s, &s->library_paths, &s->nb_library_paths, pathname);
1109 return 0;
1112 static int tcc_add_library_internal(TCCState *s, const char *fmt,
1113 const char *filename, int flags, char **paths, int nb_paths)
1115 char buf[1024];
1116 int i;
1118 for(i = 0; i < nb_paths; i++) {
1119 snprintf(buf, sizeof(buf), fmt, paths[i], filename);
1120 if (tcc_add_file_internal(s, buf, flags | AFF_TYPE_BIN) == 0)
1121 return 0;
1123 return -1;
1126 /* find and load a dll. Return non zero if not found */
1127 /* XXX: add '-rpath' option support ? */
1128 ST_FUNC int tcc_add_dll(TCCState *s, const char *filename, int flags)
1130 return tcc_add_library_internal(s, "%s/%s", filename, flags,
1131 s->library_paths, s->nb_library_paths);
1134 #ifndef TCC_TARGET_PE
1135 ST_FUNC int tcc_add_crt(TCCState *s, const char *filename)
1137 if (-1 == tcc_add_library_internal(s, "%s/%s",
1138 filename, 0, s->crt_paths, s->nb_crt_paths))
1139 tcc_error_noabort("file '%s' not found", filename);
1140 return 0;
1142 #endif
1144 /* the library name is the same as the argument of the '-l' option */
1145 LIBTCCAPI int tcc_add_library(TCCState *s, const char *libraryname)
1147 #if defined TCC_TARGET_PE
1148 const char *libs[] = { "%s/%s.def", "%s/lib%s.def", "%s/%s.dll", "%s/lib%s.dll", "%s/lib%s.a", NULL };
1149 const char **pp = s->static_link ? libs + 4 : libs;
1150 #elif defined TCC_TARGET_MACHO
1151 const char *libs[] = { "%s/lib%s.dylib", "%s/lib%s.a", NULL };
1152 const char **pp = s->static_link ? libs + 1 : libs;
1153 #else
1154 const char *libs[] = { "%s/lib%s.so", "%s/lib%s.a", NULL };
1155 const char **pp = s->static_link ? libs + 1 : libs;
1156 #endif
1157 int flags = s->filetype & AFF_WHOLE_ARCHIVE;
1158 while (*pp) {
1159 if (0 == tcc_add_library_internal(s, *pp,
1160 libraryname, flags, s->library_paths, s->nb_library_paths))
1161 return 0;
1162 ++pp;
1164 return -1;
1167 PUB_FUNC int tcc_add_library_err(TCCState *s, const char *libname)
1169 int ret = tcc_add_library(s, libname);
1170 if (ret < 0)
1171 tcc_error_noabort("library '%s' not found", libname);
1172 return ret;
1175 /* handle #pragma comment(lib,) */
1176 ST_FUNC void tcc_add_pragma_libs(TCCState *s1)
1178 int i;
1179 for (i = 0; i < s1->nb_pragma_libs; i++)
1180 tcc_add_library_err(s1, s1->pragma_libs[i]);
1183 LIBTCCAPI int tcc_add_symbol(TCCState *s, const char *name, const void *val)
1185 #ifdef TCC_TARGET_PE
1186 /* On x86_64 'val' might not be reachable with a 32bit offset.
1187 So it is handled here as if it were in a DLL. */
1188 pe_putimport(s, 0, name, (uintptr_t)val);
1189 #else
1190 set_elf_sym(symtab_section, (uintptr_t)val, 0,
1191 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1192 SHN_ABS, name);
1193 #endif
1194 return 0;
1197 LIBTCCAPI void tcc_set_lib_path(TCCState *s, const char *path)
1199 tcc_free(s->tcc_lib_path);
1200 s->tcc_lib_path = tcc_strdup(path);
1203 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
1204 #define FD_INVERT 0x0002 /* invert value before storing */
1206 typedef struct FlagDef {
1207 uint16_t offset;
1208 uint16_t flags;
1209 const char *name;
1210 } FlagDef;
1212 static int no_flag(const char **pp)
1214 const char *p = *pp;
1215 if (*p != 'n' || *++p != 'o' || *++p != '-')
1216 return 0;
1217 *pp = p + 1;
1218 return 1;
1221 ST_FUNC int set_flag(TCCState *s, const FlagDef *flags, const char *name)
1223 int value, ret;
1224 const FlagDef *p;
1225 const char *r;
1227 value = 1;
1228 r = name;
1229 if (no_flag(&r))
1230 value = 0;
1232 for (ret = -1, p = flags; p->name; ++p) {
1233 if (ret) {
1234 if (strcmp(r, p->name))
1235 continue;
1236 } else {
1237 if (0 == (p->flags & WD_ALL))
1238 continue;
1240 if (p->offset) {
1241 *(int*)((char *)s + p->offset) =
1242 p->flags & FD_INVERT ? !value : value;
1243 if (ret)
1244 return 0;
1245 } else {
1246 ret = 0;
1249 return ret;
1252 static int strstart(const char *val, const char **str)
1254 const char *p, *q;
1255 p = *str;
1256 q = val;
1257 while (*q) {
1258 if (*p != *q)
1259 return 0;
1260 p++;
1261 q++;
1263 *str = p;
1264 return 1;
1267 /* Like strstart, but automatically takes into account that ld options can
1269 * - start with double or single dash (e.g. '--soname' or '-soname')
1270 * - arguments can be given as separate or after '=' (e.g. '-Wl,-soname,x.so'
1271 * or '-Wl,-soname=x.so')
1273 * you provide `val` always in 'option[=]' form (no leading -)
1275 static int link_option(const char *str, const char *val, const char **ptr)
1277 const char *p, *q;
1278 int ret;
1280 /* there should be 1 or 2 dashes */
1281 if (*str++ != '-')
1282 return 0;
1283 if (*str == '-')
1284 str++;
1286 /* then str & val should match (potentially up to '=') */
1287 p = str;
1288 q = val;
1290 ret = 1;
1291 if (q[0] == '?') {
1292 ++q;
1293 if (no_flag(&p))
1294 ret = -1;
1297 while (*q != '\0' && *q != '=') {
1298 if (*p != *q)
1299 return 0;
1300 p++;
1301 q++;
1304 /* '=' near eos means ',' or '=' is ok */
1305 if (*q == '=') {
1306 if (*p == 0)
1307 *ptr = p;
1308 if (*p != ',' && *p != '=')
1309 return 0;
1310 p++;
1311 } else if (*p) {
1312 return 0;
1314 *ptr = p;
1315 return ret;
1318 static const char *skip_linker_arg(const char **str)
1320 const char *s1 = *str;
1321 const char *s2 = strchr(s1, ',');
1322 *str = s2 ? s2++ : (s2 = s1 + strlen(s1));
1323 return s2;
1326 static void copy_linker_arg(char **pp, const char *s, int sep)
1328 const char *q = s;
1329 char *p = *pp;
1330 int l = 0;
1331 if (p && sep)
1332 p[l = strlen(p)] = sep, ++l;
1333 skip_linker_arg(&q);
1334 pstrncpy(l + (*pp = tcc_realloc(p, q - s + l + 1)), s, q - s);
1337 /* set linker options */
1338 static int tcc_set_linker(TCCState *s, const char *option)
1340 while (*option) {
1342 const char *p = NULL;
1343 char *end = NULL;
1344 int ignoring = 0;
1345 int ret;
1347 if (link_option(option, "Bsymbolic", &p)) {
1348 s->symbolic = 1;
1349 } else if (link_option(option, "nostdlib", &p)) {
1350 s->nostdlib = 1;
1351 } else if (link_option(option, "fini=", &p)) {
1352 copy_linker_arg(&s->fini_symbol, p, 0);
1353 ignoring = 1;
1354 } else if (link_option(option, "image-base=", &p)
1355 || link_option(option, "Ttext=", &p)) {
1356 s->text_addr = strtoull(p, &end, 16);
1357 s->has_text_addr = 1;
1358 } else if (link_option(option, "init=", &p)) {
1359 copy_linker_arg(&s->init_symbol, p, 0);
1360 ignoring = 1;
1361 } else if (link_option(option, "oformat=", &p)) {
1362 #if defined(TCC_TARGET_PE)
1363 if (strstart("pe-", &p)) {
1364 #elif PTR_SIZE == 8
1365 if (strstart("elf64-", &p)) {
1366 #else
1367 if (strstart("elf32-", &p)) {
1368 #endif
1369 s->output_format = TCC_OUTPUT_FORMAT_ELF;
1370 } else if (!strcmp(p, "binary")) {
1371 s->output_format = TCC_OUTPUT_FORMAT_BINARY;
1372 #ifdef TCC_TARGET_COFF
1373 } else if (!strcmp(p, "coff")) {
1374 s->output_format = TCC_OUTPUT_FORMAT_COFF;
1375 #endif
1376 } else
1377 goto err;
1379 } else if (link_option(option, "as-needed", &p)) {
1380 ignoring = 1;
1381 } else if (link_option(option, "O", &p)) {
1382 ignoring = 1;
1383 } else if (link_option(option, "export-all-symbols", &p)) {
1384 s->rdynamic = 1;
1385 } else if (link_option(option, "export-dynamic", &p)) {
1386 s->rdynamic = 1;
1387 } else if (link_option(option, "rpath=", &p)) {
1388 copy_linker_arg(&s->rpath, p, ':');
1389 } else if (link_option(option, "enable-new-dtags", &p)) {
1390 s->enable_new_dtags = 1;
1391 } else if (link_option(option, "section-alignment=", &p)) {
1392 s->section_align = strtoul(p, &end, 16);
1393 } else if (link_option(option, "soname=", &p)) {
1394 copy_linker_arg(&s->soname, p, 0);
1395 #ifdef TCC_TARGET_PE
1396 } else if (link_option(option, "large-address-aware", &p)) {
1397 s->pe_characteristics |= 0x20;
1398 } else if (link_option(option, "file-alignment=", &p)) {
1399 s->pe_file_align = strtoul(p, &end, 16);
1400 } else if (link_option(option, "stack=", &p)) {
1401 s->pe_stack_size = strtoul(p, &end, 10);
1402 } else if (link_option(option, "subsystem=", &p)) {
1403 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1404 if (!strcmp(p, "native")) {
1405 s->pe_subsystem = 1;
1406 } else if (!strcmp(p, "console")) {
1407 s->pe_subsystem = 3;
1408 } else if (!strcmp(p, "gui") || !strcmp(p, "windows")) {
1409 s->pe_subsystem = 2;
1410 } else if (!strcmp(p, "posix")) {
1411 s->pe_subsystem = 7;
1412 } else if (!strcmp(p, "efiapp")) {
1413 s->pe_subsystem = 10;
1414 } else if (!strcmp(p, "efiboot")) {
1415 s->pe_subsystem = 11;
1416 } else if (!strcmp(p, "efiruntime")) {
1417 s->pe_subsystem = 12;
1418 } else if (!strcmp(p, "efirom")) {
1419 s->pe_subsystem = 13;
1420 #elif defined(TCC_TARGET_ARM)
1421 if (!strcmp(p, "wince")) {
1422 s->pe_subsystem = 9;
1423 #endif
1424 } else
1425 goto err;
1426 #endif
1427 } else if (ret = link_option(option, "?whole-archive", &p), ret) {
1428 if (ret > 0)
1429 s->filetype |= AFF_WHOLE_ARCHIVE;
1430 else
1431 s->filetype &= ~AFF_WHOLE_ARCHIVE;
1432 } else if (p) {
1433 return 0;
1434 } else {
1435 err:
1436 tcc_error("unsupported linker option '%s'", option);
1439 if (ignoring && s->warn_unsupported)
1440 tcc_warning("unsupported linker option '%s'", option);
1442 option = skip_linker_arg(&p);
1444 return 1;
1447 typedef struct TCCOption {
1448 const char *name;
1449 uint16_t index;
1450 uint16_t flags;
1451 } TCCOption;
1453 enum {
1454 TCC_OPTION_HELP,
1455 TCC_OPTION_HELP2,
1456 TCC_OPTION_v,
1457 TCC_OPTION_I,
1458 TCC_OPTION_D,
1459 TCC_OPTION_U,
1460 TCC_OPTION_P,
1461 TCC_OPTION_L,
1462 TCC_OPTION_B,
1463 TCC_OPTION_l,
1464 TCC_OPTION_bench,
1465 TCC_OPTION_bt,
1466 TCC_OPTION_b,
1467 TCC_OPTION_g,
1468 TCC_OPTION_c,
1469 TCC_OPTION_dumpversion,
1470 TCC_OPTION_d,
1471 TCC_OPTION_static,
1472 TCC_OPTION_std,
1473 TCC_OPTION_shared,
1474 TCC_OPTION_soname,
1475 TCC_OPTION_o,
1476 TCC_OPTION_r,
1477 TCC_OPTION_s,
1478 TCC_OPTION_traditional,
1479 TCC_OPTION_Wl,
1480 TCC_OPTION_Wp,
1481 TCC_OPTION_W,
1482 TCC_OPTION_O,
1483 TCC_OPTION_mfloat_abi,
1484 TCC_OPTION_m,
1485 TCC_OPTION_f,
1486 TCC_OPTION_isystem,
1487 TCC_OPTION_iwithprefix,
1488 TCC_OPTION_include,
1489 TCC_OPTION_nostdinc,
1490 TCC_OPTION_nostdlib,
1491 TCC_OPTION_print_search_dirs,
1492 TCC_OPTION_rdynamic,
1493 TCC_OPTION_param,
1494 TCC_OPTION_pedantic,
1495 TCC_OPTION_pthread,
1496 TCC_OPTION_run,
1497 TCC_OPTION_w,
1498 TCC_OPTION_pipe,
1499 TCC_OPTION_E,
1500 TCC_OPTION_MD,
1501 TCC_OPTION_MF,
1502 TCC_OPTION_x,
1503 TCC_OPTION_ar,
1504 TCC_OPTION_impdef
1507 #define TCC_OPTION_HAS_ARG 0x0001
1508 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
1510 static const TCCOption tcc_options[] = {
1511 { "h", TCC_OPTION_HELP, 0 },
1512 { "-help", TCC_OPTION_HELP, 0 },
1513 { "?", TCC_OPTION_HELP, 0 },
1514 { "hh", TCC_OPTION_HELP2, 0 },
1515 { "v", TCC_OPTION_v, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1516 { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
1517 { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
1518 { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
1519 { "P", TCC_OPTION_P, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1520 { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
1521 { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
1522 { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG },
1523 { "bench", TCC_OPTION_bench, 0 },
1524 #ifdef CONFIG_TCC_BACKTRACE
1525 { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG },
1526 #endif
1527 #ifdef CONFIG_TCC_BCHECK
1528 { "b", TCC_OPTION_b, 0 },
1529 #endif
1530 { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1531 { "c", TCC_OPTION_c, 0 },
1532 { "dumpversion", TCC_OPTION_dumpversion, 0},
1533 { "d", TCC_OPTION_d, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1534 { "static", TCC_OPTION_static, 0 },
1535 { "std", TCC_OPTION_std, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1536 { "shared", TCC_OPTION_shared, 0 },
1537 { "soname", TCC_OPTION_soname, TCC_OPTION_HAS_ARG },
1538 { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
1539 { "-param", TCC_OPTION_param, TCC_OPTION_HAS_ARG },
1540 { "pedantic", TCC_OPTION_pedantic, 0},
1541 { "pthread", TCC_OPTION_pthread, 0},
1542 { "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1543 { "rdynamic", TCC_OPTION_rdynamic, 0 },
1544 { "r", TCC_OPTION_r, 0 },
1545 { "s", TCC_OPTION_s, 0 },
1546 { "traditional", TCC_OPTION_traditional, 0 },
1547 { "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1548 { "Wp,", TCC_OPTION_Wp, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1549 { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1550 { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1551 #ifdef TCC_TARGET_ARM
1552 { "mfloat-abi", TCC_OPTION_mfloat_abi, TCC_OPTION_HAS_ARG },
1553 #endif
1554 { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1555 { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1556 { "isystem", TCC_OPTION_isystem, TCC_OPTION_HAS_ARG },
1557 { "include", TCC_OPTION_include, TCC_OPTION_HAS_ARG },
1558 { "nostdinc", TCC_OPTION_nostdinc, 0 },
1559 { "nostdlib", TCC_OPTION_nostdlib, 0 },
1560 { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
1561 { "w", TCC_OPTION_w, 0 },
1562 { "pipe", TCC_OPTION_pipe, 0},
1563 { "E", TCC_OPTION_E, 0},
1564 { "MD", TCC_OPTION_MD, 0},
1565 { "MF", TCC_OPTION_MF, TCC_OPTION_HAS_ARG },
1566 { "x", TCC_OPTION_x, TCC_OPTION_HAS_ARG },
1567 { "ar", TCC_OPTION_ar, 0},
1568 #ifdef TCC_TARGET_PE
1569 { "impdef", TCC_OPTION_impdef, 0},
1570 #endif
1571 { NULL, 0, 0 },
1574 static const FlagDef options_W[] = {
1575 { 0, 0, "all" },
1576 { offsetof(TCCState, warn_unsupported), 0, "unsupported" },
1577 { offsetof(TCCState, warn_write_strings), 0, "write-strings" },
1578 { offsetof(TCCState, warn_error), 0, "error" },
1579 { offsetof(TCCState, warn_gcc_compat), 0, "gcc-compat" },
1580 { offsetof(TCCState, warn_implicit_function_declaration), WD_ALL,
1581 "implicit-function-declaration" },
1582 { 0, 0, NULL }
1585 static const FlagDef options_f[] = {
1586 { offsetof(TCCState, char_is_unsigned), 0, "unsigned-char" },
1587 { offsetof(TCCState, char_is_unsigned), FD_INVERT, "signed-char" },
1588 { offsetof(TCCState, nocommon), FD_INVERT, "common" },
1589 { offsetof(TCCState, leading_underscore), 0, "leading-underscore" },
1590 { offsetof(TCCState, ms_extensions), 0, "ms-extensions" },
1591 { offsetof(TCCState, dollars_in_identifiers), 0, "dollars-in-identifiers" },
1592 { 0, 0, NULL }
1595 static const FlagDef options_m[] = {
1596 { offsetof(TCCState, ms_bitfields), 0, "ms-bitfields" },
1597 #ifdef TCC_TARGET_X86_64
1598 { offsetof(TCCState, nosse), FD_INVERT, "sse" },
1599 #endif
1600 { 0, 0, NULL }
1603 static void parse_option_D(TCCState *s1, const char *optarg)
1605 char *sym = tcc_strdup(optarg);
1606 char *value = strchr(sym, '=');
1607 if (value)
1608 *value++ = '\0';
1609 tcc_define_symbol(s1, sym, value);
1610 tcc_free(sym);
1613 static void args_parser_add_file(TCCState *s, const char* filename, int filetype)
1615 struct filespec *f = tcc_malloc(sizeof *f + strlen(filename));
1616 f->type = filetype;
1617 strcpy(f->name, filename);
1618 dynarray_add(&s->files, &s->nb_files, f);
1621 static int args_parser_make_argv(const char *r, int *argc, char ***argv)
1623 int ret = 0, q, c;
1624 CString str;
1625 for(;;) {
1626 while (c = (unsigned char)*r, c && c <= ' ')
1627 ++r;
1628 if (c == 0)
1629 break;
1630 q = 0;
1631 cstr_new(&str);
1632 while (c = (unsigned char)*r, c) {
1633 ++r;
1634 if (c == '\\' && (*r == '"' || *r == '\\')) {
1635 c = *r++;
1636 } else if (c == '"') {
1637 q = !q;
1638 continue;
1639 } else if (q == 0 && c <= ' ') {
1640 break;
1642 cstr_ccat(&str, c);
1644 cstr_ccat(&str, 0);
1645 //printf("<%s>\n", str.data), fflush(stdout);
1646 dynarray_add(argv, argc, tcc_strdup(str.data));
1647 cstr_free(&str);
1648 ++ret;
1650 return ret;
1653 /* read list file */
1654 static void args_parser_listfile(TCCState *s,
1655 const char *filename, int optind, int *pargc, char ***pargv)
1657 int fd, i;
1658 size_t len;
1659 char *p;
1660 int argc = 0;
1661 char **argv = NULL;
1663 fd = open(filename, O_RDONLY | O_BINARY);
1664 if (fd < 0)
1665 tcc_error("listfile '%s' not found", filename);
1667 len = lseek(fd, 0, SEEK_END);
1668 p = tcc_malloc(len + 1), p[len] = 0;
1669 lseek(fd, 0, SEEK_SET), read(fd, p, len), close(fd);
1671 for (i = 0; i < *pargc; ++i)
1672 if (i == optind)
1673 args_parser_make_argv(p, &argc, &argv);
1674 else
1675 dynarray_add(&argv, &argc, tcc_strdup((*pargv)[i]));
1677 tcc_free(p);
1678 dynarray_reset(&s->argv, &s->argc);
1679 *pargc = s->argc = argc, *pargv = s->argv = argv;
1682 PUB_FUNC int tcc_parse_args(TCCState *s, int *pargc, char ***pargv, int optind)
1684 const TCCOption *popt;
1685 const char *optarg, *r;
1686 const char *run = NULL;
1687 int last_o = -1;
1688 int x;
1689 CString linker_arg; /* collect -Wl options */
1690 int tool = 0, arg_start = 0, noaction = optind;
1691 char **argv = *pargv;
1692 int argc = *pargc;
1694 cstr_new(&linker_arg);
1696 while (optind < argc) {
1697 r = argv[optind];
1698 if (r[0] == '@' && r[1] != '\0') {
1699 args_parser_listfile(s, r + 1, optind, &argc, &argv);
1700 continue;
1702 optind++;
1703 if (tool) {
1704 if (r[0] == '-' && r[1] == 'v' && r[2] == 0)
1705 ++s->verbose;
1706 continue;
1708 reparse:
1709 if (r[0] != '-' || r[1] == '\0') {
1710 if (r[0] != '@') /* allow "tcc file(s) -run @ args ..." */
1711 args_parser_add_file(s, r, s->filetype);
1712 if (run) {
1713 tcc_set_options(s, run);
1714 arg_start = optind - 1;
1715 break;
1717 continue;
1720 /* find option in table */
1721 for(popt = tcc_options; ; ++popt) {
1722 const char *p1 = popt->name;
1723 const char *r1 = r + 1;
1724 if (p1 == NULL)
1725 tcc_error("invalid option -- '%s'", r);
1726 if (!strstart(p1, &r1))
1727 continue;
1728 optarg = r1;
1729 if (popt->flags & TCC_OPTION_HAS_ARG) {
1730 if (*r1 == '\0' && !(popt->flags & TCC_OPTION_NOSEP)) {
1731 if (optind >= argc)
1732 arg_err:
1733 tcc_error("argument to '%s' is missing", r);
1734 optarg = argv[optind++];
1736 } else if (*r1 != '\0')
1737 continue;
1738 break;
1741 switch(popt->index) {
1742 case TCC_OPTION_HELP:
1743 return OPT_HELP;
1744 case TCC_OPTION_HELP2:
1745 return OPT_HELP2;
1746 case TCC_OPTION_I:
1747 tcc_add_include_path(s, optarg);
1748 break;
1749 case TCC_OPTION_D:
1750 parse_option_D(s, optarg);
1751 break;
1752 case TCC_OPTION_U:
1753 tcc_undefine_symbol(s, optarg);
1754 break;
1755 case TCC_OPTION_L:
1756 tcc_add_library_path(s, optarg);
1757 break;
1758 case TCC_OPTION_B:
1759 /* set tcc utilities path (mainly for tcc development) */
1760 tcc_set_lib_path(s, optarg);
1761 break;
1762 case TCC_OPTION_l:
1763 args_parser_add_file(s, optarg, AFF_TYPE_LIB | (s->filetype & ~AFF_TYPE_MASK));
1764 s->nb_libraries++;
1765 break;
1766 case TCC_OPTION_pthread:
1767 parse_option_D(s, "_REENTRANT");
1768 s->option_pthread = 1;
1769 break;
1770 case TCC_OPTION_bench:
1771 s->do_bench = 1;
1772 break;
1773 #ifdef CONFIG_TCC_BACKTRACE
1774 case TCC_OPTION_bt:
1775 tcc_set_num_callers(atoi(optarg));
1776 break;
1777 #endif
1778 #ifdef CONFIG_TCC_BCHECK
1779 case TCC_OPTION_b:
1780 s->do_bounds_check = 1;
1781 s->do_debug = 1;
1782 break;
1783 #endif
1784 case TCC_OPTION_g:
1785 s->do_debug = 1;
1786 break;
1787 case TCC_OPTION_c:
1788 x = TCC_OUTPUT_OBJ;
1789 set_output_type:
1790 if (s->output_type)
1791 tcc_warning("-%s: overriding compiler action already specified", popt->name);
1792 s->output_type = x;
1793 break;
1794 case TCC_OPTION_d:
1795 if (*optarg == 'D')
1796 s->dflag = 3;
1797 else if (*optarg == 'M')
1798 s->dflag = 7;
1799 else if (*optarg == 't')
1800 s->dflag = 16;
1801 else if (isnum(*optarg))
1802 g_debug = atoi(optarg);
1803 else
1804 goto unsupported_option;
1805 break;
1806 case TCC_OPTION_static:
1807 s->static_link = 1;
1808 break;
1809 case TCC_OPTION_std:
1810 if (*optarg == '=') {
1811 if (strcmp(optarg, "=c11") == 0) {
1812 tcc_undefine_symbol(s, "__STDC_VERSION__");
1813 tcc_define_symbol(s, "__STDC_VERSION__", "201112L");
1815 * The integer constant 1, intended to indicate
1816 * that the implementation does not support atomic
1817 * types (including the _Atomic type qualifier) and
1818 * the <stdatomic.h> header.
1820 tcc_define_symbol(s, "__STDC_NO_ATOMICS__", "1");
1822 * The integer constant 1, intended to indicate
1823 * that the implementation does not support complex
1824 * types or the <complex.h> header.
1826 tcc_define_symbol(s, "__STDC_NO_COMPLEX__", "1");
1828 * The integer constant 1, intended to indicate
1829 * that the implementation does not support the
1830 * <threads.h> header.
1832 tcc_define_symbol(s, "__STDC_NO_THREADS__", "1");
1834 * __STDC_NO_VLA__, tcc supports VLA.
1835 * The integer constant 1, intended to indicate
1836 * that the implementation does not support
1837 * variable length arrays or variably modified
1838 * types.
1840 #if !defined(TCC_TARGET_PE)
1842 * An integer constant of the form yyyymmL (for
1843 * example, 199712L). If this symbol is defined,
1844 * then every character in the Unicode required
1845 * set, when stored in an object of type
1846 * wchar_t, has the same value as the short
1847 * identifier of that character.
1849 #if 0
1850 /* on Linux, this conflicts with a define introduced by
1851 * /usr/include/stdc-predef.h included by glibc libs;
1852 * clang doesn't define it at all so it's probably not necessary
1854 tcc_define_symbol(s, "__STDC_ISO_10646__", "201605L");
1855 #endif
1857 * The integer constant 1, intended to indicate
1858 * that values of type char16_t are UTF−16
1859 * encoded. If some other encoding is used, the
1860 * macro shall not be defined and the actual
1861 * encoding used is implementation defined.
1863 tcc_define_symbol(s, "__STDC_UTF_16__", "1");
1865 * The integer constant 1, intended to indicate
1866 * that values of type char32_t are UTF−32
1867 * encoded. If some other encoding is used, the
1868 * macro shall not be defined and the actual
1869 * encoding used is implementationdefined.
1871 tcc_define_symbol(s, "__STDC_UTF_32__", "1");
1872 #endif /* !TCC_TARGET_PE */
1873 s->cversion = 201112;
1877 * silently ignore other values, a current purpose:
1878 * allow to use a tcc as a reference compiler for "make test"
1880 break;
1881 case TCC_OPTION_shared:
1882 x = TCC_OUTPUT_DLL;
1883 goto set_output_type;
1884 case TCC_OPTION_soname:
1885 s->soname = tcc_strdup(optarg);
1886 break;
1887 case TCC_OPTION_o:
1888 if (s->outfile) {
1889 tcc_warning("multiple -o option");
1890 tcc_free(s->outfile);
1892 s->outfile = tcc_strdup(optarg);
1893 break;
1894 case TCC_OPTION_r:
1895 /* generate a .o merging several output files */
1896 s->option_r = 1;
1897 x = TCC_OUTPUT_OBJ;
1898 goto set_output_type;
1899 case TCC_OPTION_isystem:
1900 tcc_add_sysinclude_path(s, optarg);
1901 break;
1902 case TCC_OPTION_include:
1903 dynarray_add(&s->cmd_include_files,
1904 &s->nb_cmd_include_files, tcc_strdup(optarg));
1905 break;
1906 case TCC_OPTION_nostdinc:
1907 s->nostdinc = 1;
1908 break;
1909 case TCC_OPTION_nostdlib:
1910 s->nostdlib = 1;
1911 break;
1912 case TCC_OPTION_run:
1913 #ifndef TCC_IS_NATIVE
1914 tcc_error("-run is not available in a cross compiler");
1915 #endif
1916 run = optarg;
1917 x = TCC_OUTPUT_MEMORY;
1918 goto set_output_type;
1919 case TCC_OPTION_v:
1920 do ++s->verbose; while (*optarg++ == 'v');
1921 ++noaction;
1922 break;
1923 case TCC_OPTION_f:
1924 if (set_flag(s, options_f, optarg) < 0)
1925 goto unsupported_option;
1926 break;
1927 #ifdef TCC_TARGET_ARM
1928 case TCC_OPTION_mfloat_abi:
1929 /* tcc doesn't support soft float yet */
1930 if (!strcmp(optarg, "softfp")) {
1931 s->float_abi = ARM_SOFTFP_FLOAT;
1932 tcc_undefine_symbol(s, "__ARM_PCS_VFP");
1933 } else if (!strcmp(optarg, "hard"))
1934 s->float_abi = ARM_HARD_FLOAT;
1935 else
1936 tcc_error("unsupported float abi '%s'", optarg);
1937 break;
1938 #endif
1939 case TCC_OPTION_m:
1940 if (set_flag(s, options_m, optarg) < 0) {
1941 if (x = atoi(optarg), x != 32 && x != 64)
1942 goto unsupported_option;
1943 if (PTR_SIZE != x/8)
1944 return x;
1945 ++noaction;
1947 break;
1948 case TCC_OPTION_W:
1949 if (set_flag(s, options_W, optarg) < 0)
1950 goto unsupported_option;
1951 break;
1952 case TCC_OPTION_w:
1953 s->warn_none = 1;
1954 break;
1955 case TCC_OPTION_rdynamic:
1956 s->rdynamic = 1;
1957 break;
1958 case TCC_OPTION_Wl:
1959 if (linker_arg.size)
1960 --linker_arg.size, cstr_ccat(&linker_arg, ',');
1961 cstr_cat(&linker_arg, optarg, 0);
1962 if (tcc_set_linker(s, linker_arg.data))
1963 cstr_free(&linker_arg);
1964 break;
1965 case TCC_OPTION_Wp:
1966 r = optarg;
1967 goto reparse;
1968 case TCC_OPTION_E:
1969 x = TCC_OUTPUT_PREPROCESS;
1970 goto set_output_type;
1971 case TCC_OPTION_P:
1972 s->Pflag = atoi(optarg) + 1;
1973 break;
1974 case TCC_OPTION_MD:
1975 s->gen_deps = 1;
1976 break;
1977 case TCC_OPTION_MF:
1978 s->deps_outfile = tcc_strdup(optarg);
1979 break;
1980 case TCC_OPTION_dumpversion:
1981 printf ("%s\n", TCC_VERSION);
1982 exit(0);
1983 break;
1984 case TCC_OPTION_x:
1985 x = 0;
1986 if (*optarg == 'c')
1987 x = AFF_TYPE_C;
1988 else if (*optarg == 'a')
1989 x = AFF_TYPE_ASMPP;
1990 else if (*optarg == 'b')
1991 x = AFF_TYPE_BIN;
1992 else if (*optarg == 'n')
1993 x = AFF_TYPE_NONE;
1994 else
1995 tcc_warning("unsupported language '%s'", optarg);
1996 s->filetype = x | (s->filetype & ~AFF_TYPE_MASK);
1997 break;
1998 case TCC_OPTION_O:
1999 last_o = atoi(optarg);
2000 break;
2001 case TCC_OPTION_print_search_dirs:
2002 x = OPT_PRINT_DIRS;
2003 goto extra_action;
2004 case TCC_OPTION_impdef:
2005 x = OPT_IMPDEF;
2006 goto extra_action;
2007 case TCC_OPTION_ar:
2008 x = OPT_AR;
2009 extra_action:
2010 arg_start = optind - 1;
2011 if (arg_start != noaction)
2012 tcc_error("cannot parse %s here", r);
2013 tool = x;
2014 break;
2015 case TCC_OPTION_traditional:
2016 case TCC_OPTION_pedantic:
2017 case TCC_OPTION_pipe:
2018 case TCC_OPTION_s:
2019 /* ignored */
2020 break;
2021 default:
2022 unsupported_option:
2023 if (s->warn_unsupported)
2024 tcc_warning("unsupported option '%s'", r);
2025 break;
2028 if (last_o > 0)
2029 tcc_define_symbol(s, "__OPTIMIZE__", NULL);
2030 if (linker_arg.size) {
2031 r = linker_arg.data;
2032 goto arg_err;
2034 *pargc = argc - arg_start;
2035 *pargv = argv + arg_start;
2036 if (tool)
2037 return tool;
2038 if (optind != noaction)
2039 return 0;
2040 if (s->verbose == 2)
2041 return OPT_PRINT_DIRS;
2042 if (s->verbose)
2043 return OPT_V;
2044 return OPT_HELP;
2047 LIBTCCAPI void tcc_set_options(TCCState *s, const char *r)
2049 char **argv = NULL;
2050 int argc = 0;
2051 args_parser_make_argv(r, &argc, &argv);
2052 tcc_parse_args(s, &argc, &argv, 0);
2053 dynarray_reset(&argv, &argc);
2056 PUB_FUNC void tcc_print_stats(TCCState *s, unsigned total_time)
2058 if (total_time < 1)
2059 total_time = 1;
2060 if (total_bytes < 1)
2061 total_bytes = 1;
2062 fprintf(stderr, "* %d idents, %d lines, %d bytes\n"
2063 "* %0.3f s, %u lines/s, %0.1f MB/s\n",
2064 tok_ident - TOK_IDENT, total_lines, total_bytes,
2065 (double)total_time/1000,
2066 (unsigned)total_lines*1000/total_time,
2067 (double)total_bytes/1000/total_time);
2068 #ifdef MEM_DEBUG
2069 fprintf(stderr, "* %d bytes memory used\n", mem_max_size);
2070 #endif