Tidy new support for cleanups
[tinycc.git] / libtcc.c
blobbcccbd53754ba818f5602a5d031a030ef497b89b
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 #endif
49 #ifdef TCC_TARGET_ARM
50 #include "arm-gen.c"
51 #include "arm-link.c"
52 #include "arm-asm.c"
53 #endif
54 #ifdef TCC_TARGET_ARM64
55 #include "arm64-gen.c"
56 #include "arm64-link.c"
57 #endif
58 #ifdef TCC_TARGET_C67
59 #include "c67-gen.c"
60 #include "c67-link.c"
61 #include "tcccoff.c"
62 #endif
63 #ifdef TCC_TARGET_X86_64
64 #include "x86_64-gen.c"
65 #include "x86_64-link.c"
66 #include "i386-asm.c"
67 #endif
68 #ifdef CONFIG_TCC_ASM
69 #include "tccasm.c"
70 #endif
71 #ifdef TCC_TARGET_PE
72 #include "tccpe.c"
73 #endif
74 #endif /* ONE_SOURCE */
76 /********************************************************/
77 #ifndef CONFIG_TCC_ASM
78 ST_FUNC void asm_instr(void)
80 tcc_error("inline asm() not supported");
82 ST_FUNC void asm_global_instr(void)
84 tcc_error("inline asm() not supported");
86 #endif
88 /********************************************************/
89 #ifdef _WIN32
90 ST_FUNC char *normalize_slashes(char *path)
92 char *p;
93 for (p = path; *p; ++p)
94 if (*p == '\\')
95 *p = '/';
96 return path;
99 static HMODULE tcc_module;
101 /* on win32, we suppose the lib and includes are at the location of 'tcc.exe' */
102 static void tcc_set_lib_path_w32(TCCState *s)
104 char path[1024], *p;
105 GetModuleFileNameA(tcc_module, path, sizeof path);
106 p = tcc_basename(normalize_slashes(strlwr(path)));
107 if (p > path)
108 --p;
109 *p = 0;
110 tcc_set_lib_path(s, path);
113 #ifdef TCC_TARGET_PE
114 static void tcc_add_systemdir(TCCState *s)
116 char buf[1000];
117 GetSystemDirectory(buf, sizeof buf);
118 tcc_add_library_path(s, normalize_slashes(buf));
120 #endif
122 #ifdef LIBTCC_AS_DLL
123 BOOL WINAPI DllMain (HINSTANCE hDll, DWORD dwReason, LPVOID lpReserved)
125 if (DLL_PROCESS_ATTACH == dwReason)
126 tcc_module = hDll;
127 return TRUE;
129 #endif
130 #endif
132 /********************************************************/
133 /* copy a string and truncate it. */
134 ST_FUNC char *pstrcpy(char *buf, int buf_size, const char *s)
136 char *q, *q_end;
137 int c;
139 if (buf_size > 0) {
140 q = buf;
141 q_end = buf + buf_size - 1;
142 while (q < q_end) {
143 c = *s++;
144 if (c == '\0')
145 break;
146 *q++ = c;
148 *q = '\0';
150 return buf;
153 /* strcat and truncate. */
154 ST_FUNC char *pstrcat(char *buf, int buf_size, const char *s)
156 int len;
157 len = strlen(buf);
158 if (len < buf_size)
159 pstrcpy(buf + len, buf_size - len, s);
160 return buf;
163 ST_FUNC char *pstrncpy(char *out, const char *in, size_t num)
165 memcpy(out, in, num);
166 out[num] = '\0';
167 return out;
170 /* extract the basename of a file */
171 PUB_FUNC char *tcc_basename(const char *name)
173 char *p = strchr(name, 0);
174 while (p > name && !IS_DIRSEP(p[-1]))
175 --p;
176 return p;
179 /* extract extension part of a file
181 * (if no extension, return pointer to end-of-string)
183 PUB_FUNC char *tcc_fileextension (const char *name)
185 char *b = tcc_basename(name);
186 char *e = strrchr(b, '.');
187 return e ? e : strchr(b, 0);
190 /********************************************************/
191 /* memory management */
193 #undef free
194 #undef malloc
195 #undef realloc
197 #ifndef MEM_DEBUG
199 PUB_FUNC void tcc_free(void *ptr)
201 free(ptr);
204 PUB_FUNC void *tcc_malloc(unsigned long size)
206 void *ptr;
207 ptr = malloc(size);
208 if (!ptr && size)
209 tcc_error("memory full (malloc)");
210 return ptr;
213 PUB_FUNC void *tcc_mallocz(unsigned long size)
215 void *ptr;
216 ptr = tcc_malloc(size);
217 memset(ptr, 0, size);
218 return ptr;
221 PUB_FUNC void *tcc_realloc(void *ptr, unsigned long size)
223 void *ptr1;
224 ptr1 = realloc(ptr, size);
225 if (!ptr1 && size)
226 tcc_error("memory full (realloc)");
227 return ptr1;
230 PUB_FUNC char *tcc_strdup(const char *str)
232 char *ptr;
233 ptr = tcc_malloc(strlen(str) + 1);
234 strcpy(ptr, str);
235 return ptr;
238 PUB_FUNC void tcc_memcheck(void)
242 #else
244 #define MEM_DEBUG_MAGIC1 0xFEEDDEB1
245 #define MEM_DEBUG_MAGIC2 0xFEEDDEB2
246 #define MEM_DEBUG_MAGIC3 0xFEEDDEB3
247 #define MEM_DEBUG_FILE_LEN 40
248 #define MEM_DEBUG_CHECK3(header) \
249 ((mem_debug_header_t*)((char*)header + header->size))->magic3
250 #define MEM_USER_PTR(header) \
251 ((char *)header + offsetof(mem_debug_header_t, magic3))
252 #define MEM_HEADER_PTR(ptr) \
253 (mem_debug_header_t *)((char*)ptr - offsetof(mem_debug_header_t, magic3))
255 struct mem_debug_header {
256 unsigned magic1;
257 unsigned size;
258 struct mem_debug_header *prev;
259 struct mem_debug_header *next;
260 int line_num;
261 char file_name[MEM_DEBUG_FILE_LEN + 1];
262 unsigned magic2;
263 ALIGNED(16) unsigned magic3;
266 typedef struct mem_debug_header mem_debug_header_t;
268 static mem_debug_header_t *mem_debug_chain;
269 static unsigned mem_cur_size;
270 static unsigned mem_max_size;
272 static mem_debug_header_t *malloc_check(void *ptr, const char *msg)
274 mem_debug_header_t * header = MEM_HEADER_PTR(ptr);
275 if (header->magic1 != MEM_DEBUG_MAGIC1 ||
276 header->magic2 != MEM_DEBUG_MAGIC2 ||
277 MEM_DEBUG_CHECK3(header) != MEM_DEBUG_MAGIC3 ||
278 header->size == (unsigned)-1) {
279 fprintf(stderr, "%s check failed\n", msg);
280 if (header->magic1 == MEM_DEBUG_MAGIC1)
281 fprintf(stderr, "%s:%u: block allocated here.\n",
282 header->file_name, header->line_num);
283 exit(1);
285 return header;
288 PUB_FUNC void *tcc_malloc_debug(unsigned long size, const char *file, int line)
290 int ofs;
291 mem_debug_header_t *header;
293 header = malloc(sizeof(mem_debug_header_t) + size);
294 if (!header)
295 tcc_error("memory full (malloc)");
297 header->magic1 = MEM_DEBUG_MAGIC1;
298 header->magic2 = MEM_DEBUG_MAGIC2;
299 header->size = size;
300 MEM_DEBUG_CHECK3(header) = MEM_DEBUG_MAGIC3;
301 header->line_num = line;
302 ofs = strlen(file) - MEM_DEBUG_FILE_LEN;
303 strncpy(header->file_name, file + (ofs > 0 ? ofs : 0), MEM_DEBUG_FILE_LEN);
304 header->file_name[MEM_DEBUG_FILE_LEN] = 0;
306 header->next = mem_debug_chain;
307 header->prev = NULL;
308 if (header->next)
309 header->next->prev = header;
310 mem_debug_chain = header;
312 mem_cur_size += size;
313 if (mem_cur_size > mem_max_size)
314 mem_max_size = mem_cur_size;
316 return MEM_USER_PTR(header);
319 PUB_FUNC void tcc_free_debug(void *ptr)
321 mem_debug_header_t *header;
322 if (!ptr)
323 return;
324 header = malloc_check(ptr, "tcc_free");
325 mem_cur_size -= header->size;
326 header->size = (unsigned)-1;
327 if (header->next)
328 header->next->prev = header->prev;
329 if (header->prev)
330 header->prev->next = header->next;
331 if (header == mem_debug_chain)
332 mem_debug_chain = header->next;
333 free(header);
336 PUB_FUNC void *tcc_mallocz_debug(unsigned long size, const char *file, int line)
338 void *ptr;
339 ptr = tcc_malloc_debug(size,file,line);
340 memset(ptr, 0, size);
341 return ptr;
344 PUB_FUNC void *tcc_realloc_debug(void *ptr, unsigned long size, const char *file, int line)
346 mem_debug_header_t *header;
347 int mem_debug_chain_update = 0;
348 if (!ptr)
349 return tcc_malloc_debug(size, file, line);
350 header = malloc_check(ptr, "tcc_realloc");
351 mem_cur_size -= header->size;
352 mem_debug_chain_update = (header == mem_debug_chain);
353 header = realloc(header, sizeof(mem_debug_header_t) + size);
354 if (!header)
355 tcc_error("memory full (realloc)");
356 header->size = size;
357 MEM_DEBUG_CHECK3(header) = MEM_DEBUG_MAGIC3;
358 if (header->next)
359 header->next->prev = header;
360 if (header->prev)
361 header->prev->next = header;
362 if (mem_debug_chain_update)
363 mem_debug_chain = header;
364 mem_cur_size += size;
365 if (mem_cur_size > mem_max_size)
366 mem_max_size = mem_cur_size;
367 return MEM_USER_PTR(header);
370 PUB_FUNC char *tcc_strdup_debug(const char *str, const char *file, int line)
372 char *ptr;
373 ptr = tcc_malloc_debug(strlen(str) + 1, file, line);
374 strcpy(ptr, str);
375 return ptr;
378 PUB_FUNC void tcc_memcheck(void)
380 if (mem_cur_size) {
381 mem_debug_header_t *header = mem_debug_chain;
382 fprintf(stderr, "MEM_DEBUG: mem_leak= %d bytes, mem_max_size= %d bytes\n",
383 mem_cur_size, mem_max_size);
384 while (header) {
385 fprintf(stderr, "%s:%u: error: %u bytes leaked\n",
386 header->file_name, header->line_num, header->size);
387 header = header->next;
389 #if MEM_DEBUG-0 == 2
390 exit(2);
391 #endif
394 #endif /* MEM_DEBUG */
396 #define free(p) use_tcc_free(p)
397 #define malloc(s) use_tcc_malloc(s)
398 #define realloc(p, s) use_tcc_realloc(p, s)
400 /********************************************************/
401 /* dynarrays */
403 ST_FUNC void dynarray_add(void *ptab, int *nb_ptr, void *data)
405 int nb, nb_alloc;
406 void **pp;
408 nb = *nb_ptr;
409 pp = *(void ***)ptab;
410 /* every power of two we double array size */
411 if ((nb & (nb - 1)) == 0) {
412 if (!nb)
413 nb_alloc = 1;
414 else
415 nb_alloc = nb * 2;
416 pp = tcc_realloc(pp, nb_alloc * sizeof(void *));
417 *(void***)ptab = pp;
419 pp[nb++] = data;
420 *nb_ptr = nb;
423 ST_FUNC void dynarray_reset(void *pp, int *n)
425 void **p;
426 for (p = *(void***)pp; *n; ++p, --*n)
427 if (*p)
428 tcc_free(*p);
429 tcc_free(*(void**)pp);
430 *(void**)pp = NULL;
433 static void tcc_split_path(TCCState *s, void *p_ary, int *p_nb_ary, const char *in)
435 const char *p;
436 do {
437 int c;
438 CString str;
440 cstr_new(&str);
441 for (p = in; c = *p, c != '\0' && c != PATHSEP[0]; ++p) {
442 if (c == '{' && p[1] && p[2] == '}') {
443 c = p[1], p += 2;
444 if (c == 'B')
445 cstr_cat(&str, s->tcc_lib_path, -1);
446 } else {
447 cstr_ccat(&str, c);
450 if (str.size) {
451 cstr_ccat(&str, '\0');
452 dynarray_add(p_ary, p_nb_ary, tcc_strdup(str.data));
454 cstr_free(&str);
455 in = p+1;
456 } while (*p);
459 /********************************************************/
461 static void strcat_vprintf(char *buf, int buf_size, const char *fmt, va_list ap)
463 int len;
464 len = strlen(buf);
465 vsnprintf(buf + len, buf_size - len, fmt, ap);
468 static void strcat_printf(char *buf, int buf_size, const char *fmt, ...)
470 va_list ap;
471 va_start(ap, fmt);
472 strcat_vprintf(buf, buf_size, fmt, ap);
473 va_end(ap);
476 static void error1(TCCState *s1, int is_warning, const char *fmt, va_list ap)
478 char buf[2048];
479 BufferedFile **pf, *f;
481 buf[0] = '\0';
482 /* use upper file if inline ":asm:" or token ":paste:" */
483 for (f = file; f && f->filename[0] == ':'; f = f->prev)
485 if (f) {
486 for(pf = s1->include_stack; pf < s1->include_stack_ptr; pf++)
487 strcat_printf(buf, sizeof(buf), "In file included from %s:%d:\n",
488 (*pf)->filename, (*pf)->line_num);
489 if (s1->error_set_jmp_enabled) {
490 strcat_printf(buf, sizeof(buf), "%s:%d: ",
491 f->filename, f->line_num - !!(tok_flags & TOK_FLAG_BOL));
492 } else {
493 strcat_printf(buf, sizeof(buf), "%s: ",
494 f->filename);
496 } else {
497 strcat_printf(buf, sizeof(buf), "tcc: ");
499 if (is_warning)
500 strcat_printf(buf, sizeof(buf), "warning: ");
501 else
502 strcat_printf(buf, sizeof(buf), "error: ");
503 strcat_vprintf(buf, sizeof(buf), fmt, ap);
505 if (!s1->error_func) {
506 /* default case: stderr */
507 if (s1->output_type == TCC_OUTPUT_PREPROCESS && s1->ppfp == stdout)
508 /* print a newline during tcc -E */
509 printf("\n"), fflush(stdout);
510 fflush(stdout); /* flush -v output */
511 fprintf(stderr, "%s\n", buf);
512 fflush(stderr); /* print error/warning now (win32) */
513 } else {
514 s1->error_func(s1->error_opaque, buf);
516 if (!is_warning || s1->warn_error)
517 s1->nb_errors++;
520 LIBTCCAPI void tcc_set_error_func(TCCState *s, void *error_opaque,
521 void (*error_func)(void *opaque, const char *msg))
523 s->error_opaque = error_opaque;
524 s->error_func = error_func;
527 /* error without aborting current compilation */
528 PUB_FUNC void tcc_error_noabort(const char *fmt, ...)
530 TCCState *s1 = tcc_state;
531 va_list ap;
533 va_start(ap, fmt);
534 error1(s1, 0, fmt, ap);
535 va_end(ap);
538 PUB_FUNC void tcc_error(const char *fmt, ...)
540 TCCState *s1 = tcc_state;
541 va_list ap;
543 va_start(ap, fmt);
544 error1(s1, 0, fmt, ap);
545 va_end(ap);
546 /* better than nothing: in some cases, we accept to handle errors */
547 if (s1->error_set_jmp_enabled) {
548 longjmp(s1->error_jmp_buf, 1);
549 } else {
550 /* XXX: eliminate this someday */
551 exit(1);
555 PUB_FUNC void tcc_warning(const char *fmt, ...)
557 TCCState *s1 = tcc_state;
558 va_list ap;
560 if (s1->warn_none)
561 return;
563 va_start(ap, fmt);
564 error1(s1, 1, fmt, ap);
565 va_end(ap);
568 /********************************************************/
569 /* I/O layer */
571 ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen)
573 BufferedFile *bf;
574 int buflen = initlen ? initlen : IO_BUF_SIZE;
576 bf = tcc_mallocz(sizeof(BufferedFile) + buflen);
577 bf->buf_ptr = bf->buffer;
578 bf->buf_end = bf->buffer + initlen;
579 bf->buf_end[0] = CH_EOB; /* put eob symbol */
580 pstrcpy(bf->filename, sizeof(bf->filename), filename);
581 bf->true_filename = bf->filename;
582 bf->line_num = 1;
583 bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
584 bf->fd = -1;
585 bf->prev = file;
586 file = bf;
587 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
590 ST_FUNC void tcc_close(void)
592 BufferedFile *bf = file;
593 if (bf->fd > 0) {
594 close(bf->fd);
595 total_lines += bf->line_num;
597 if (bf->true_filename != bf->filename)
598 tcc_free(bf->true_filename);
599 file = bf->prev;
600 tcc_free(bf);
603 ST_FUNC int tcc_open(TCCState *s1, const char *filename)
605 int fd;
606 if (strcmp(filename, "-") == 0)
607 fd = 0, filename = "<stdin>";
608 else
609 fd = open(filename, O_RDONLY | O_BINARY);
610 if ((s1->verbose == 2 && fd >= 0) || s1->verbose == 3)
611 printf("%s %*s%s\n", fd < 0 ? "nf":"->",
612 (int)(s1->include_stack_ptr - s1->include_stack), "", filename);
613 if (fd < 0)
614 return -1;
615 tcc_open_bf(s1, filename, 0);
616 #ifdef _WIN32
617 normalize_slashes(file->filename);
618 #endif
619 file->fd = fd;
620 return fd;
623 /* compile the file opened in 'file'. Return non zero if errors. */
624 static int tcc_compile(TCCState *s1, int filetype)
626 Sym *define_start;
627 int is_asm;
629 define_start = define_stack;
630 is_asm = !!(filetype & (AFF_TYPE_ASM|AFF_TYPE_ASMPP));
631 tccelf_begin_file(s1);
633 if (setjmp(s1->error_jmp_buf) == 0) {
634 s1->nb_errors = 0;
635 s1->error_set_jmp_enabled = 1;
637 preprocess_start(s1, is_asm);
638 if (s1->output_type == TCC_OUTPUT_PREPROCESS) {
639 tcc_preprocess(s1);
640 } else if (is_asm) {
641 #ifdef CONFIG_TCC_ASM
642 tcc_assemble(s1, !!(filetype & AFF_TYPE_ASMPP));
643 #else
644 tcc_error_noabort("asm not supported");
645 #endif
646 } else {
647 tccgen_compile(s1);
650 s1->error_set_jmp_enabled = 0;
652 preprocess_end(s1);
653 free_inline_functions(s1);
654 /* reset define stack, but keep -D and built-ins */
655 free_defines(define_start);
656 sym_pop(&global_stack, NULL, 0);
657 sym_pop(&local_stack, NULL, 0);
658 tccelf_end_file(s1);
659 return s1->nb_errors != 0 ? -1 : 0;
662 LIBTCCAPI int tcc_compile_string(TCCState *s, const char *str)
664 int len, ret;
666 len = strlen(str);
667 tcc_open_bf(s, "<string>", len);
668 memcpy(file->buffer, str, len);
669 ret = tcc_compile(s, s->filetype);
670 tcc_close();
671 return ret;
674 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
675 LIBTCCAPI void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
677 int len1, len2;
678 /* default value */
679 if (!value)
680 value = "1";
681 len1 = strlen(sym);
682 len2 = strlen(value);
684 /* init file structure */
685 tcc_open_bf(s1, "<define>", len1 + len2 + 1);
686 memcpy(file->buffer, sym, len1);
687 file->buffer[len1] = ' ';
688 memcpy(file->buffer + len1 + 1, value, len2);
690 /* parse with define parser */
691 next_nomacro();
692 parse_define();
693 tcc_close();
696 /* undefine a preprocessor symbol */
697 LIBTCCAPI void tcc_undefine_symbol(TCCState *s1, const char *sym)
699 TokenSym *ts;
700 Sym *s;
701 ts = tok_alloc(sym, strlen(sym));
702 s = define_find(ts->tok);
703 /* undefine symbol by putting an invalid name */
704 if (s)
705 define_undef(s);
708 /* cleanup all static data used during compilation */
709 static void tcc_cleanup(void)
711 if (NULL == tcc_state)
712 return;
713 while (file)
714 tcc_close();
715 tccpp_delete(tcc_state);
716 tcc_state = NULL;
717 /* free sym_pools */
718 dynarray_reset(&sym_pools, &nb_sym_pools);
719 /* reset symbol stack */
720 sym_free_first = NULL;
723 LIBTCCAPI TCCState *tcc_new(void)
725 TCCState *s;
727 tcc_cleanup();
729 s = tcc_mallocz(sizeof(TCCState));
730 if (!s)
731 return NULL;
732 tcc_state = s;
733 ++nb_states;
735 s->nocommon = 1;
736 s->cversion = 199901; /* default unless -std=c11 is supplied */
737 s->warn_implicit_function_declaration = 1;
738 s->ms_extensions = 1;
740 #ifdef CHAR_IS_UNSIGNED
741 s->char_is_unsigned = 1;
742 #endif
743 #ifdef TCC_TARGET_I386
744 s->seg_size = 32;
745 #endif
746 /* enable this if you want symbols with leading underscore on windows: */
747 #if 0 /* def TCC_TARGET_PE */
748 s->leading_underscore = 1;
749 #endif
750 #ifdef _WIN32
751 tcc_set_lib_path_w32(s);
752 #else
753 tcc_set_lib_path(s, CONFIG_TCCDIR);
754 #endif
755 tccelf_new(s);
756 tccpp_new(s);
758 /* we add dummy defines for some special macros to speed up tests
759 and to have working defined() */
760 define_push(TOK___LINE__, MACRO_OBJ, NULL, NULL);
761 define_push(TOK___FILE__, MACRO_OBJ, NULL, NULL);
762 define_push(TOK___DATE__, MACRO_OBJ, NULL, NULL);
763 define_push(TOK___TIME__, MACRO_OBJ, NULL, NULL);
764 define_push(TOK___COUNTER__, MACRO_OBJ, NULL, NULL);
766 /* define __TINYC__ 92X */
767 char buffer[32]; int a,b,c;
768 sscanf(TCC_VERSION, "%d.%d.%d", &a, &b, &c);
769 sprintf(buffer, "%d", a*10000 + b*100 + c);
770 tcc_define_symbol(s, "__TINYC__", buffer);
773 /* standard defines */
774 tcc_define_symbol(s, "__STDC__", NULL);
775 tcc_define_symbol(s, "__STDC_VERSION__", "199901L");
776 tcc_define_symbol(s, "__STDC_HOSTED__", NULL);
778 /* target defines */
779 #if defined(TCC_TARGET_I386)
780 tcc_define_symbol(s, "__i386__", NULL);
781 tcc_define_symbol(s, "__i386", NULL);
782 tcc_define_symbol(s, "i386", NULL);
783 #elif defined(TCC_TARGET_X86_64)
784 tcc_define_symbol(s, "__x86_64__", NULL);
785 #elif defined(TCC_TARGET_ARM)
786 tcc_define_symbol(s, "__ARM_ARCH_4__", NULL);
787 tcc_define_symbol(s, "__arm_elf__", NULL);
788 tcc_define_symbol(s, "__arm_elf", NULL);
789 tcc_define_symbol(s, "arm_elf", NULL);
790 tcc_define_symbol(s, "__arm__", NULL);
791 tcc_define_symbol(s, "__arm", NULL);
792 tcc_define_symbol(s, "arm", NULL);
793 tcc_define_symbol(s, "__APCS_32__", NULL);
794 tcc_define_symbol(s, "__ARMEL__", NULL);
795 #if defined(TCC_ARM_EABI)
796 tcc_define_symbol(s, "__ARM_EABI__", NULL);
797 #endif
798 #if defined(TCC_ARM_HARDFLOAT)
799 s->float_abi = ARM_HARD_FLOAT;
800 tcc_define_symbol(s, "__ARM_PCS_VFP", NULL);
801 #else
802 s->float_abi = ARM_SOFTFP_FLOAT;
803 #endif
804 #elif defined(TCC_TARGET_ARM64)
805 tcc_define_symbol(s, "__aarch64__", NULL);
806 #elif defined TCC_TARGET_C67
807 tcc_define_symbol(s, "__C67__", NULL);
808 #endif
810 #ifdef TCC_TARGET_PE
811 tcc_define_symbol(s, "_WIN32", NULL);
812 # ifdef TCC_TARGET_X86_64
813 tcc_define_symbol(s, "_WIN64", NULL);
814 # endif
815 #else
816 tcc_define_symbol(s, "__unix__", NULL);
817 tcc_define_symbol(s, "__unix", NULL);
818 tcc_define_symbol(s, "unix", NULL);
819 # if defined(__linux__)
820 tcc_define_symbol(s, "__linux__", NULL);
821 tcc_define_symbol(s, "__linux", NULL);
822 # endif
823 # if defined(__FreeBSD__)
824 tcc_define_symbol(s, "__FreeBSD__", "__FreeBSD__");
825 /* No 'Thread Storage Local' on FreeBSD with tcc */
826 tcc_define_symbol(s, "__NO_TLS", NULL);
827 # endif
828 # if defined(__FreeBSD_kernel__)
829 tcc_define_symbol(s, "__FreeBSD_kernel__", NULL);
830 # endif
831 # if defined(__NetBSD__)
832 tcc_define_symbol(s, "__NetBSD__", "__NetBSD__");
833 # endif
834 # if defined(__OpenBSD__)
835 tcc_define_symbol(s, "__OpenBSD__", "__OpenBSD__");
836 # endif
837 #endif
839 /* TinyCC & gcc defines */
840 #if PTR_SIZE == 4
841 /* 32bit systems. */
842 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned int");
843 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "int");
844 tcc_define_symbol(s, "__ILP32__", NULL);
845 #elif LONG_SIZE == 4
846 /* 64bit Windows. */
847 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned long long");
848 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "long long");
849 tcc_define_symbol(s, "__LLP64__", NULL);
850 #else
851 /* Other 64bit systems. */
852 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned long");
853 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "long");
854 tcc_define_symbol(s, "__LP64__", NULL);
855 #endif
857 #ifdef TCC_TARGET_PE
858 tcc_define_symbol(s, "__WCHAR_TYPE__", "unsigned short");
859 tcc_define_symbol(s, "__WINT_TYPE__", "unsigned short");
860 #else
861 tcc_define_symbol(s, "__WCHAR_TYPE__", "int");
862 /* wint_t is unsigned int by default, but (signed) int on BSDs
863 and unsigned short on windows. Other OSes might have still
864 other conventions, sigh. */
865 # if defined(__FreeBSD__) || defined (__FreeBSD_kernel__) \
866 || defined(__NetBSD__) || defined(__OpenBSD__)
867 tcc_define_symbol(s, "__WINT_TYPE__", "int");
868 # ifdef __FreeBSD__
869 /* define __GNUC__ to have some useful stuff from sys/cdefs.h
870 that are unconditionally used in FreeBSDs other system headers :/ */
871 tcc_define_symbol(s, "__GNUC__", "2");
872 tcc_define_symbol(s, "__GNUC_MINOR__", "7");
873 tcc_define_symbol(s, "__builtin_alloca", "alloca");
874 # endif
875 # else
876 tcc_define_symbol(s, "__WINT_TYPE__", "unsigned int");
877 /* glibc defines */
878 tcc_define_symbol(s, "__REDIRECT(name, proto, alias)",
879 "name proto __asm__ (#alias)");
880 tcc_define_symbol(s, "__REDIRECT_NTH(name, proto, alias)",
881 "name proto __asm__ (#alias) __THROW");
882 # endif
883 # if defined(TCC_MUSL)
884 tcc_define_symbol(s, "__DEFINED_va_list", "");
885 tcc_define_symbol(s, "__DEFINED___isoc_va_list", "");
886 tcc_define_symbol(s, "__isoc_va_list", "void *");
887 # endif /* TCC_MUSL */
888 /* Some GCC builtins that are simple to express as macros. */
889 tcc_define_symbol(s, "__builtin_extract_return_addr(x)", "x");
890 #endif /* ndef TCC_TARGET_PE */
891 return s;
894 LIBTCCAPI void tcc_delete(TCCState *s1)
896 tcc_cleanup();
898 /* free sections */
899 tccelf_delete(s1);
901 /* free library paths */
902 dynarray_reset(&s1->library_paths, &s1->nb_library_paths);
903 dynarray_reset(&s1->crt_paths, &s1->nb_crt_paths);
905 /* free include paths */
906 dynarray_reset(&s1->cached_includes, &s1->nb_cached_includes);
907 dynarray_reset(&s1->include_paths, &s1->nb_include_paths);
908 dynarray_reset(&s1->sysinclude_paths, &s1->nb_sysinclude_paths);
909 dynarray_reset(&s1->cmd_include_files, &s1->nb_cmd_include_files);
911 tcc_free(s1->tcc_lib_path);
912 tcc_free(s1->soname);
913 tcc_free(s1->rpath);
914 tcc_free(s1->init_symbol);
915 tcc_free(s1->fini_symbol);
916 tcc_free(s1->outfile);
917 tcc_free(s1->deps_outfile);
918 dynarray_reset(&s1->files, &s1->nb_files);
919 dynarray_reset(&s1->target_deps, &s1->nb_target_deps);
920 dynarray_reset(&s1->pragma_libs, &s1->nb_pragma_libs);
921 dynarray_reset(&s1->argv, &s1->argc);
923 #ifdef TCC_IS_NATIVE
924 /* free runtime memory */
925 tcc_run_free(s1);
926 #endif
928 tcc_free(s1);
929 if (0 == --nb_states)
930 tcc_memcheck();
933 LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type)
935 s->output_type = output_type;
937 /* always elf for objects */
938 if (output_type == TCC_OUTPUT_OBJ)
939 s->output_format = TCC_OUTPUT_FORMAT_ELF;
941 if (s->char_is_unsigned)
942 tcc_define_symbol(s, "__CHAR_UNSIGNED__", NULL);
944 if (!s->nostdinc) {
945 /* default include paths */
946 /* -isystem paths have already been handled */
947 tcc_add_sysinclude_path(s, CONFIG_TCC_SYSINCLUDEPATHS);
950 #ifdef CONFIG_TCC_BCHECK
951 if (s->do_bounds_check) {
952 /* if bound checking, then add corresponding sections */
953 tccelf_bounds_new(s);
954 /* define symbol */
955 tcc_define_symbol(s, "__BOUNDS_CHECKING_ON", NULL);
957 #endif
958 if (s->do_debug) {
959 /* add debug sections */
960 tccelf_stab_new(s);
963 tcc_add_library_path(s, CONFIG_TCC_LIBPATHS);
965 #ifdef TCC_TARGET_PE
966 # ifdef _WIN32
967 if (!s->nostdlib && output_type != TCC_OUTPUT_OBJ)
968 tcc_add_systemdir(s);
969 # endif
970 #else
971 /* paths for crt objects */
972 tcc_split_path(s, &s->crt_paths, &s->nb_crt_paths, CONFIG_TCC_CRTPREFIX);
973 /* add libc crt1/crti objects */
974 if ((output_type == TCC_OUTPUT_EXE || output_type == TCC_OUTPUT_DLL) &&
975 !s->nostdlib) {
976 if (output_type != TCC_OUTPUT_DLL)
977 tcc_add_crt(s, "crt1.o");
978 tcc_add_crt(s, "crti.o");
980 #endif
981 return 0;
984 LIBTCCAPI int tcc_add_include_path(TCCState *s, const char *pathname)
986 tcc_split_path(s, &s->include_paths, &s->nb_include_paths, pathname);
987 return 0;
990 LIBTCCAPI int tcc_add_sysinclude_path(TCCState *s, const char *pathname)
992 tcc_split_path(s, &s->sysinclude_paths, &s->nb_sysinclude_paths, pathname);
993 return 0;
996 ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
998 int ret;
1000 /* open the file */
1001 ret = tcc_open(s1, filename);
1002 if (ret < 0) {
1003 if (flags & AFF_PRINT_ERROR)
1004 tcc_error_noabort("file '%s' not found", filename);
1005 return ret;
1008 /* update target deps */
1009 dynarray_add(&s1->target_deps, &s1->nb_target_deps,
1010 tcc_strdup(filename));
1012 if (flags & AFF_TYPE_BIN) {
1013 ElfW(Ehdr) ehdr;
1014 int fd, obj_type;
1016 fd = file->fd;
1017 obj_type = tcc_object_type(fd, &ehdr);
1018 lseek(fd, 0, SEEK_SET);
1020 #ifdef TCC_TARGET_MACHO
1021 if (0 == obj_type && 0 == strcmp(tcc_fileextension(filename), ".dylib"))
1022 obj_type = AFF_BINTYPE_DYN;
1023 #endif
1025 switch (obj_type) {
1026 case AFF_BINTYPE_REL:
1027 ret = tcc_load_object_file(s1, fd, 0);
1028 break;
1029 #ifndef TCC_TARGET_PE
1030 case AFF_BINTYPE_DYN:
1031 if (s1->output_type == TCC_OUTPUT_MEMORY) {
1032 ret = 0;
1033 #ifdef TCC_IS_NATIVE
1034 if (NULL == dlopen(filename, RTLD_GLOBAL | RTLD_LAZY))
1035 ret = -1;
1036 #endif
1037 } else {
1038 ret = tcc_load_dll(s1, fd, filename,
1039 (flags & AFF_REFERENCED_DLL) != 0);
1041 break;
1042 #endif
1043 case AFF_BINTYPE_AR:
1044 ret = tcc_load_archive(s1, fd, !(flags & AFF_WHOLE_ARCHIVE));
1045 break;
1046 #ifdef TCC_TARGET_COFF
1047 case AFF_BINTYPE_C67:
1048 ret = tcc_load_coff(s1, fd);
1049 break;
1050 #endif
1051 default:
1052 #ifdef TCC_TARGET_PE
1053 ret = pe_load_file(s1, filename, fd);
1054 #else
1055 /* as GNU ld, consider it is an ld script if not recognized */
1056 ret = tcc_load_ldscript(s1);
1057 #endif
1058 if (ret < 0)
1059 tcc_error_noabort("unrecognized file type");
1060 break;
1062 } else {
1063 ret = tcc_compile(s1, flags);
1065 tcc_close();
1066 return ret;
1069 LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename)
1071 int filetype = s->filetype;
1072 if (0 == (filetype & AFF_TYPE_MASK)) {
1073 /* use a file extension to detect a filetype */
1074 const char *ext = tcc_fileextension(filename);
1075 if (ext[0]) {
1076 ext++;
1077 if (!strcmp(ext, "S"))
1078 filetype = AFF_TYPE_ASMPP;
1079 else if (!strcmp(ext, "s"))
1080 filetype = AFF_TYPE_ASM;
1081 else if (!PATHCMP(ext, "c") || !PATHCMP(ext, "i"))
1082 filetype = AFF_TYPE_C;
1083 else
1084 filetype |= AFF_TYPE_BIN;
1085 } else {
1086 filetype = AFF_TYPE_C;
1089 return tcc_add_file_internal(s, filename, filetype | AFF_PRINT_ERROR);
1092 LIBTCCAPI int tcc_add_library_path(TCCState *s, const char *pathname)
1094 tcc_split_path(s, &s->library_paths, &s->nb_library_paths, pathname);
1095 return 0;
1098 static int tcc_add_library_internal(TCCState *s, const char *fmt,
1099 const char *filename, int flags, char **paths, int nb_paths)
1101 char buf[1024];
1102 int i;
1104 for(i = 0; i < nb_paths; i++) {
1105 snprintf(buf, sizeof(buf), fmt, paths[i], filename);
1106 if (tcc_add_file_internal(s, buf, flags | AFF_TYPE_BIN) == 0)
1107 return 0;
1109 return -1;
1112 /* find and load a dll. Return non zero if not found */
1113 /* XXX: add '-rpath' option support ? */
1114 ST_FUNC int tcc_add_dll(TCCState *s, const char *filename, int flags)
1116 return tcc_add_library_internal(s, "%s/%s", filename, flags,
1117 s->library_paths, s->nb_library_paths);
1120 ST_FUNC int tcc_add_crt(TCCState *s, const char *filename)
1122 if (-1 == tcc_add_library_internal(s, "%s/%s",
1123 filename, 0, s->crt_paths, s->nb_crt_paths))
1124 tcc_error_noabort("file '%s' not found", filename);
1125 return 0;
1128 /* the library name is the same as the argument of the '-l' option */
1129 LIBTCCAPI int tcc_add_library(TCCState *s, const char *libraryname)
1131 #if defined TCC_TARGET_PE
1132 const char *libs[] = { "%s/%s.def", "%s/lib%s.def", "%s/%s.dll", "%s/lib%s.dll", "%s/lib%s.a", NULL };
1133 const char **pp = s->static_link ? libs + 4 : libs;
1134 #elif defined TCC_TARGET_MACHO
1135 const char *libs[] = { "%s/lib%s.dylib", "%s/lib%s.a", NULL };
1136 const char **pp = s->static_link ? libs + 1 : libs;
1137 #else
1138 const char *libs[] = { "%s/lib%s.so", "%s/lib%s.a", NULL };
1139 const char **pp = s->static_link ? libs + 1 : libs;
1140 #endif
1141 int flags = s->filetype & AFF_WHOLE_ARCHIVE;
1142 while (*pp) {
1143 if (0 == tcc_add_library_internal(s, *pp,
1144 libraryname, flags, s->library_paths, s->nb_library_paths))
1145 return 0;
1146 ++pp;
1148 return -1;
1151 PUB_FUNC int tcc_add_library_err(TCCState *s, const char *libname)
1153 int ret = tcc_add_library(s, libname);
1154 if (ret < 0)
1155 tcc_error_noabort("library '%s' not found", libname);
1156 return ret;
1159 /* handle #pragma comment(lib,) */
1160 ST_FUNC void tcc_add_pragma_libs(TCCState *s1)
1162 int i;
1163 for (i = 0; i < s1->nb_pragma_libs; i++)
1164 tcc_add_library_err(s1, s1->pragma_libs[i]);
1167 LIBTCCAPI int tcc_add_symbol(TCCState *s, const char *name, const void *val)
1169 #ifdef TCC_TARGET_PE
1170 /* On x86_64 'val' might not be reachable with a 32bit offset.
1171 So it is handled here as if it were in a DLL. */
1172 pe_putimport(s, 0, name, (uintptr_t)val);
1173 #else
1174 set_elf_sym(symtab_section, (uintptr_t)val, 0,
1175 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1176 SHN_ABS, name);
1177 #endif
1178 return 0;
1181 LIBTCCAPI void tcc_set_lib_path(TCCState *s, const char *path)
1183 tcc_free(s->tcc_lib_path);
1184 s->tcc_lib_path = tcc_strdup(path);
1187 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
1188 #define FD_INVERT 0x0002 /* invert value before storing */
1190 typedef struct FlagDef {
1191 uint16_t offset;
1192 uint16_t flags;
1193 const char *name;
1194 } FlagDef;
1196 static int no_flag(const char **pp)
1198 const char *p = *pp;
1199 if (*p != 'n' || *++p != 'o' || *++p != '-')
1200 return 0;
1201 *pp = p + 1;
1202 return 1;
1205 ST_FUNC int set_flag(TCCState *s, const FlagDef *flags, const char *name)
1207 int value, ret;
1208 const FlagDef *p;
1209 const char *r;
1211 value = 1;
1212 r = name;
1213 if (no_flag(&r))
1214 value = 0;
1216 for (ret = -1, p = flags; p->name; ++p) {
1217 if (ret) {
1218 if (strcmp(r, p->name))
1219 continue;
1220 } else {
1221 if (0 == (p->flags & WD_ALL))
1222 continue;
1224 if (p->offset) {
1225 *(int*)((char *)s + p->offset) =
1226 p->flags & FD_INVERT ? !value : value;
1227 if (ret)
1228 return 0;
1229 } else {
1230 ret = 0;
1233 return ret;
1236 static int strstart(const char *val, const char **str)
1238 const char *p, *q;
1239 p = *str;
1240 q = val;
1241 while (*q) {
1242 if (*p != *q)
1243 return 0;
1244 p++;
1245 q++;
1247 *str = p;
1248 return 1;
1251 /* Like strstart, but automatically takes into account that ld options can
1253 * - start with double or single dash (e.g. '--soname' or '-soname')
1254 * - arguments can be given as separate or after '=' (e.g. '-Wl,-soname,x.so'
1255 * or '-Wl,-soname=x.so')
1257 * you provide `val` always in 'option[=]' form (no leading -)
1259 static int link_option(const char *str, const char *val, const char **ptr)
1261 const char *p, *q;
1262 int ret;
1264 /* there should be 1 or 2 dashes */
1265 if (*str++ != '-')
1266 return 0;
1267 if (*str == '-')
1268 str++;
1270 /* then str & val should match (potentially up to '=') */
1271 p = str;
1272 q = val;
1274 ret = 1;
1275 if (q[0] == '?') {
1276 ++q;
1277 if (no_flag(&p))
1278 ret = -1;
1281 while (*q != '\0' && *q != '=') {
1282 if (*p != *q)
1283 return 0;
1284 p++;
1285 q++;
1288 /* '=' near eos means ',' or '=' is ok */
1289 if (*q == '=') {
1290 if (*p == 0)
1291 *ptr = p;
1292 if (*p != ',' && *p != '=')
1293 return 0;
1294 p++;
1295 } else if (*p) {
1296 return 0;
1298 *ptr = p;
1299 return ret;
1302 static const char *skip_linker_arg(const char **str)
1304 const char *s1 = *str;
1305 const char *s2 = strchr(s1, ',');
1306 *str = s2 ? s2++ : (s2 = s1 + strlen(s1));
1307 return s2;
1310 static void copy_linker_arg(char **pp, const char *s, int sep)
1312 const char *q = s;
1313 char *p = *pp;
1314 int l = 0;
1315 if (p && sep)
1316 p[l = strlen(p)] = sep, ++l;
1317 skip_linker_arg(&q);
1318 pstrncpy(l + (*pp = tcc_realloc(p, q - s + l + 1)), s, q - s);
1321 /* set linker options */
1322 static int tcc_set_linker(TCCState *s, const char *option)
1324 while (*option) {
1326 const char *p = NULL;
1327 char *end = NULL;
1328 int ignoring = 0;
1329 int ret;
1331 if (link_option(option, "Bsymbolic", &p)) {
1332 s->symbolic = 1;
1333 } else if (link_option(option, "nostdlib", &p)) {
1334 s->nostdlib = 1;
1335 } else if (link_option(option, "fini=", &p)) {
1336 copy_linker_arg(&s->fini_symbol, p, 0);
1337 ignoring = 1;
1338 } else if (link_option(option, "image-base=", &p)
1339 || link_option(option, "Ttext=", &p)) {
1340 s->text_addr = strtoull(p, &end, 16);
1341 s->has_text_addr = 1;
1342 } else if (link_option(option, "init=", &p)) {
1343 copy_linker_arg(&s->init_symbol, p, 0);
1344 ignoring = 1;
1345 } else if (link_option(option, "oformat=", &p)) {
1346 #if defined(TCC_TARGET_PE)
1347 if (strstart("pe-", &p)) {
1348 #elif PTR_SIZE == 8
1349 if (strstart("elf64-", &p)) {
1350 #else
1351 if (strstart("elf32-", &p)) {
1352 #endif
1353 s->output_format = TCC_OUTPUT_FORMAT_ELF;
1354 } else if (!strcmp(p, "binary")) {
1355 s->output_format = TCC_OUTPUT_FORMAT_BINARY;
1356 #ifdef TCC_TARGET_COFF
1357 } else if (!strcmp(p, "coff")) {
1358 s->output_format = TCC_OUTPUT_FORMAT_COFF;
1359 #endif
1360 } else
1361 goto err;
1363 } else if (link_option(option, "as-needed", &p)) {
1364 ignoring = 1;
1365 } else if (link_option(option, "O", &p)) {
1366 ignoring = 1;
1367 } else if (link_option(option, "export-all-symbols", &p)) {
1368 s->rdynamic = 1;
1369 } else if (link_option(option, "export-dynamic", &p)) {
1370 s->rdynamic = 1;
1371 } else if (link_option(option, "rpath=", &p)) {
1372 copy_linker_arg(&s->rpath, p, ':');
1373 } else if (link_option(option, "enable-new-dtags", &p)) {
1374 s->enable_new_dtags = 1;
1375 } else if (link_option(option, "section-alignment=", &p)) {
1376 s->section_align = strtoul(p, &end, 16);
1377 } else if (link_option(option, "soname=", &p)) {
1378 copy_linker_arg(&s->soname, p, 0);
1379 #ifdef TCC_TARGET_PE
1380 } else if (link_option(option, "large-address-aware", &p)) {
1381 s->pe_characteristics |= 0x20;
1382 } else if (link_option(option, "file-alignment=", &p)) {
1383 s->pe_file_align = strtoul(p, &end, 16);
1384 } else if (link_option(option, "stack=", &p)) {
1385 s->pe_stack_size = strtoul(p, &end, 10);
1386 } else if (link_option(option, "subsystem=", &p)) {
1387 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1388 if (!strcmp(p, "native")) {
1389 s->pe_subsystem = 1;
1390 } else if (!strcmp(p, "console")) {
1391 s->pe_subsystem = 3;
1392 } else if (!strcmp(p, "gui") || !strcmp(p, "windows")) {
1393 s->pe_subsystem = 2;
1394 } else if (!strcmp(p, "posix")) {
1395 s->pe_subsystem = 7;
1396 } else if (!strcmp(p, "efiapp")) {
1397 s->pe_subsystem = 10;
1398 } else if (!strcmp(p, "efiboot")) {
1399 s->pe_subsystem = 11;
1400 } else if (!strcmp(p, "efiruntime")) {
1401 s->pe_subsystem = 12;
1402 } else if (!strcmp(p, "efirom")) {
1403 s->pe_subsystem = 13;
1404 #elif defined(TCC_TARGET_ARM)
1405 if (!strcmp(p, "wince")) {
1406 s->pe_subsystem = 9;
1407 #endif
1408 } else
1409 goto err;
1410 #endif
1411 } else if (ret = link_option(option, "?whole-archive", &p), ret) {
1412 if (ret > 0)
1413 s->filetype |= AFF_WHOLE_ARCHIVE;
1414 else
1415 s->filetype &= ~AFF_WHOLE_ARCHIVE;
1416 } else if (p) {
1417 return 0;
1418 } else {
1419 err:
1420 tcc_error("unsupported linker option '%s'", option);
1423 if (ignoring && s->warn_unsupported)
1424 tcc_warning("unsupported linker option '%s'", option);
1426 option = skip_linker_arg(&p);
1428 return 1;
1431 typedef struct TCCOption {
1432 const char *name;
1433 uint16_t index;
1434 uint16_t flags;
1435 } TCCOption;
1437 enum {
1438 TCC_OPTION_HELP,
1439 TCC_OPTION_HELP2,
1440 TCC_OPTION_v,
1441 TCC_OPTION_I,
1442 TCC_OPTION_D,
1443 TCC_OPTION_U,
1444 TCC_OPTION_P,
1445 TCC_OPTION_L,
1446 TCC_OPTION_B,
1447 TCC_OPTION_l,
1448 TCC_OPTION_bench,
1449 TCC_OPTION_bt,
1450 TCC_OPTION_b,
1451 TCC_OPTION_g,
1452 TCC_OPTION_c,
1453 TCC_OPTION_dumpversion,
1454 TCC_OPTION_d,
1455 TCC_OPTION_static,
1456 TCC_OPTION_std,
1457 TCC_OPTION_shared,
1458 TCC_OPTION_soname,
1459 TCC_OPTION_o,
1460 TCC_OPTION_r,
1461 TCC_OPTION_s,
1462 TCC_OPTION_traditional,
1463 TCC_OPTION_Wl,
1464 TCC_OPTION_Wp,
1465 TCC_OPTION_W,
1466 TCC_OPTION_O,
1467 TCC_OPTION_mfloat_abi,
1468 TCC_OPTION_m,
1469 TCC_OPTION_f,
1470 TCC_OPTION_isystem,
1471 TCC_OPTION_iwithprefix,
1472 TCC_OPTION_include,
1473 TCC_OPTION_nostdinc,
1474 TCC_OPTION_nostdlib,
1475 TCC_OPTION_print_search_dirs,
1476 TCC_OPTION_rdynamic,
1477 TCC_OPTION_param,
1478 TCC_OPTION_pedantic,
1479 TCC_OPTION_pthread,
1480 TCC_OPTION_run,
1481 TCC_OPTION_w,
1482 TCC_OPTION_pipe,
1483 TCC_OPTION_E,
1484 TCC_OPTION_MD,
1485 TCC_OPTION_MF,
1486 TCC_OPTION_x,
1487 TCC_OPTION_ar,
1488 TCC_OPTION_impdef
1491 #define TCC_OPTION_HAS_ARG 0x0001
1492 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
1494 static const TCCOption tcc_options[] = {
1495 { "h", TCC_OPTION_HELP, 0 },
1496 { "-help", TCC_OPTION_HELP, 0 },
1497 { "?", TCC_OPTION_HELP, 0 },
1498 { "hh", TCC_OPTION_HELP2, 0 },
1499 { "v", TCC_OPTION_v, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1500 { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
1501 { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
1502 { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
1503 { "P", TCC_OPTION_P, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1504 { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
1505 { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
1506 { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG },
1507 { "bench", TCC_OPTION_bench, 0 },
1508 #ifdef CONFIG_TCC_BACKTRACE
1509 { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG },
1510 #endif
1511 #ifdef CONFIG_TCC_BCHECK
1512 { "b", TCC_OPTION_b, 0 },
1513 #endif
1514 { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1515 { "c", TCC_OPTION_c, 0 },
1516 { "dumpversion", TCC_OPTION_dumpversion, 0},
1517 { "d", TCC_OPTION_d, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1518 { "static", TCC_OPTION_static, 0 },
1519 { "std", TCC_OPTION_std, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1520 { "shared", TCC_OPTION_shared, 0 },
1521 { "soname", TCC_OPTION_soname, TCC_OPTION_HAS_ARG },
1522 { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
1523 { "-param", TCC_OPTION_param, TCC_OPTION_HAS_ARG },
1524 { "pedantic", TCC_OPTION_pedantic, 0},
1525 { "pthread", TCC_OPTION_pthread, 0},
1526 { "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1527 { "rdynamic", TCC_OPTION_rdynamic, 0 },
1528 { "r", TCC_OPTION_r, 0 },
1529 { "s", TCC_OPTION_s, 0 },
1530 { "traditional", TCC_OPTION_traditional, 0 },
1531 { "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1532 { "Wp,", TCC_OPTION_Wp, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1533 { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1534 { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1535 #ifdef TCC_TARGET_ARM
1536 { "mfloat-abi", TCC_OPTION_mfloat_abi, TCC_OPTION_HAS_ARG },
1537 #endif
1538 { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1539 { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
1540 { "isystem", TCC_OPTION_isystem, TCC_OPTION_HAS_ARG },
1541 { "include", TCC_OPTION_include, TCC_OPTION_HAS_ARG },
1542 { "nostdinc", TCC_OPTION_nostdinc, 0 },
1543 { "nostdlib", TCC_OPTION_nostdlib, 0 },
1544 { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
1545 { "w", TCC_OPTION_w, 0 },
1546 { "pipe", TCC_OPTION_pipe, 0},
1547 { "E", TCC_OPTION_E, 0},
1548 { "MD", TCC_OPTION_MD, 0},
1549 { "MF", TCC_OPTION_MF, TCC_OPTION_HAS_ARG },
1550 { "x", TCC_OPTION_x, TCC_OPTION_HAS_ARG },
1551 { "ar", TCC_OPTION_ar, 0},
1552 #ifdef TCC_TARGET_PE
1553 { "impdef", TCC_OPTION_impdef, 0},
1554 #endif
1555 { NULL, 0, 0 },
1558 static const FlagDef options_W[] = {
1559 { 0, 0, "all" },
1560 { offsetof(TCCState, warn_unsupported), 0, "unsupported" },
1561 { offsetof(TCCState, warn_write_strings), 0, "write-strings" },
1562 { offsetof(TCCState, warn_error), 0, "error" },
1563 { offsetof(TCCState, warn_gcc_compat), 0, "gcc-compat" },
1564 { offsetof(TCCState, warn_implicit_function_declaration), WD_ALL,
1565 "implicit-function-declaration" },
1566 { 0, 0, NULL }
1569 static const FlagDef options_f[] = {
1570 { offsetof(TCCState, char_is_unsigned), 0, "unsigned-char" },
1571 { offsetof(TCCState, char_is_unsigned), FD_INVERT, "signed-char" },
1572 { offsetof(TCCState, nocommon), FD_INVERT, "common" },
1573 { offsetof(TCCState, leading_underscore), 0, "leading-underscore" },
1574 { offsetof(TCCState, ms_extensions), 0, "ms-extensions" },
1575 { offsetof(TCCState, dollars_in_identifiers), 0, "dollars-in-identifiers" },
1576 { 0, 0, NULL }
1579 static const FlagDef options_m[] = {
1580 { offsetof(TCCState, ms_bitfields), 0, "ms-bitfields" },
1581 #ifdef TCC_TARGET_X86_64
1582 { offsetof(TCCState, nosse), FD_INVERT, "sse" },
1583 #endif
1584 { 0, 0, NULL }
1587 static void parse_option_D(TCCState *s1, const char *optarg)
1589 char *sym = tcc_strdup(optarg);
1590 char *value = strchr(sym, '=');
1591 if (value)
1592 *value++ = '\0';
1593 tcc_define_symbol(s1, sym, value);
1594 tcc_free(sym);
1597 static void args_parser_add_file(TCCState *s, const char* filename, int filetype)
1599 struct filespec *f = tcc_malloc(sizeof *f + strlen(filename));
1600 f->type = filetype;
1601 strcpy(f->name, filename);
1602 dynarray_add(&s->files, &s->nb_files, f);
1605 static int args_parser_make_argv(const char *r, int *argc, char ***argv)
1607 int ret = 0, q, c;
1608 CString str;
1609 for(;;) {
1610 while (c = (unsigned char)*r, c && c <= ' ')
1611 ++r;
1612 if (c == 0)
1613 break;
1614 q = 0;
1615 cstr_new(&str);
1616 while (c = (unsigned char)*r, c) {
1617 ++r;
1618 if (c == '\\' && (*r == '"' || *r == '\\')) {
1619 c = *r++;
1620 } else if (c == '"') {
1621 q = !q;
1622 continue;
1623 } else if (q == 0 && c <= ' ') {
1624 break;
1626 cstr_ccat(&str, c);
1628 cstr_ccat(&str, 0);
1629 //printf("<%s>\n", str.data), fflush(stdout);
1630 dynarray_add(argv, argc, tcc_strdup(str.data));
1631 cstr_free(&str);
1632 ++ret;
1634 return ret;
1637 /* read list file */
1638 static void args_parser_listfile(TCCState *s,
1639 const char *filename, int optind, int *pargc, char ***pargv)
1641 int fd, i;
1642 size_t len;
1643 char *p;
1644 int argc = 0;
1645 char **argv = NULL;
1647 fd = open(filename, O_RDONLY | O_BINARY);
1648 if (fd < 0)
1649 tcc_error("listfile '%s' not found", filename);
1651 len = lseek(fd, 0, SEEK_END);
1652 p = tcc_malloc(len + 1), p[len] = 0;
1653 lseek(fd, 0, SEEK_SET), read(fd, p, len), close(fd);
1655 for (i = 0; i < *pargc; ++i)
1656 if (i == optind)
1657 args_parser_make_argv(p, &argc, &argv);
1658 else
1659 dynarray_add(&argv, &argc, tcc_strdup((*pargv)[i]));
1661 tcc_free(p);
1662 dynarray_reset(&s->argv, &s->argc);
1663 *pargc = s->argc = argc, *pargv = s->argv = argv;
1666 PUB_FUNC int tcc_parse_args(TCCState *s, int *pargc, char ***pargv, int optind)
1668 const TCCOption *popt;
1669 const char *optarg, *r;
1670 const char *run = NULL;
1671 int last_o = -1;
1672 int x;
1673 CString linker_arg; /* collect -Wl options */
1674 int tool = 0, arg_start = 0, noaction = optind;
1675 char **argv = *pargv;
1676 int argc = *pargc;
1678 cstr_new(&linker_arg);
1680 while (optind < argc) {
1681 r = argv[optind];
1682 if (r[0] == '@' && r[1] != '\0') {
1683 args_parser_listfile(s, r + 1, optind, &argc, &argv);
1684 continue;
1686 optind++;
1687 if (tool) {
1688 if (r[0] == '-' && r[1] == 'v' && r[2] == 0)
1689 ++s->verbose;
1690 continue;
1692 reparse:
1693 if (r[0] != '-' || r[1] == '\0') {
1694 if (r[0] != '@') /* allow "tcc file(s) -run @ args ..." */
1695 args_parser_add_file(s, r, s->filetype);
1696 if (run) {
1697 tcc_set_options(s, run);
1698 arg_start = optind - 1;
1699 break;
1701 continue;
1704 /* find option in table */
1705 for(popt = tcc_options; ; ++popt) {
1706 const char *p1 = popt->name;
1707 const char *r1 = r + 1;
1708 if (p1 == NULL)
1709 tcc_error("invalid option -- '%s'", r);
1710 if (!strstart(p1, &r1))
1711 continue;
1712 optarg = r1;
1713 if (popt->flags & TCC_OPTION_HAS_ARG) {
1714 if (*r1 == '\0' && !(popt->flags & TCC_OPTION_NOSEP)) {
1715 if (optind >= argc)
1716 arg_err:
1717 tcc_error("argument to '%s' is missing", r);
1718 optarg = argv[optind++];
1720 } else if (*r1 != '\0')
1721 continue;
1722 break;
1725 switch(popt->index) {
1726 case TCC_OPTION_HELP:
1727 return OPT_HELP;
1728 case TCC_OPTION_HELP2:
1729 return OPT_HELP2;
1730 case TCC_OPTION_I:
1731 tcc_add_include_path(s, optarg);
1732 break;
1733 case TCC_OPTION_D:
1734 parse_option_D(s, optarg);
1735 break;
1736 case TCC_OPTION_U:
1737 tcc_undefine_symbol(s, optarg);
1738 break;
1739 case TCC_OPTION_L:
1740 tcc_add_library_path(s, optarg);
1741 break;
1742 case TCC_OPTION_B:
1743 /* set tcc utilities path (mainly for tcc development) */
1744 tcc_set_lib_path(s, optarg);
1745 break;
1746 case TCC_OPTION_l:
1747 args_parser_add_file(s, optarg, AFF_TYPE_LIB | (s->filetype & ~AFF_TYPE_MASK));
1748 s->nb_libraries++;
1749 break;
1750 case TCC_OPTION_pthread:
1751 parse_option_D(s, "_REENTRANT");
1752 s->option_pthread = 1;
1753 break;
1754 case TCC_OPTION_bench:
1755 s->do_bench = 1;
1756 break;
1757 #ifdef CONFIG_TCC_BACKTRACE
1758 case TCC_OPTION_bt:
1759 tcc_set_num_callers(atoi(optarg));
1760 break;
1761 #endif
1762 #ifdef CONFIG_TCC_BCHECK
1763 case TCC_OPTION_b:
1764 s->do_bounds_check = 1;
1765 s->do_debug = 1;
1766 break;
1767 #endif
1768 case TCC_OPTION_g:
1769 s->do_debug = 1;
1770 break;
1771 case TCC_OPTION_c:
1772 x = TCC_OUTPUT_OBJ;
1773 set_output_type:
1774 if (s->output_type)
1775 tcc_warning("-%s: overriding compiler action already specified", popt->name);
1776 s->output_type = x;
1777 break;
1778 case TCC_OPTION_d:
1779 if (*optarg == 'D')
1780 s->dflag = 3;
1781 else if (*optarg == 'M')
1782 s->dflag = 7;
1783 else if (*optarg == 't')
1784 s->dflag = 16;
1785 else if (isnum(*optarg))
1786 g_debug = atoi(optarg);
1787 else
1788 goto unsupported_option;
1789 break;
1790 case TCC_OPTION_static:
1791 s->static_link = 1;
1792 break;
1793 case TCC_OPTION_std:
1794 if (*optarg == '=') {
1795 if (strcmp(optarg, "=c11") == 0) {
1796 tcc_undefine_symbol(s, "__STDC_VERSION__");
1797 tcc_define_symbol(s, "__STDC_VERSION__", "201112L");
1799 * The integer constant 1, intended to indicate
1800 * that the implementation does not support atomic
1801 * types (including the _Atomic type qualifier) and
1802 * the <stdatomic.h> header.
1804 tcc_define_symbol(s, "__STDC_NO_ATOMICS__", "1");
1806 * The integer constant 1, intended to indicate
1807 * that the implementation does not support complex
1808 * types or the <complex.h> header.
1810 tcc_define_symbol(s, "__STDC_NO_COMPLEX__", "1");
1812 * The integer constant 1, intended to indicate
1813 * that the implementation does not support the
1814 * <threads.h> header.
1816 tcc_define_symbol(s, "__STDC_NO_THREADS__", "1");
1818 * __STDC_NO_VLA__, tcc supports VLA.
1819 * The integer constant 1, intended to indicate
1820 * that the implementation does not support
1821 * variable length arrays or variably modified
1822 * types.
1824 #if !defined(TCC_TARGET_PE)
1826 * An integer constant of the form yyyymmL (for
1827 * example, 199712L). If this symbol is defined,
1828 * then every character in the Unicode required
1829 * set, when stored in an object of type
1830 * wchar_t, has the same value as the short
1831 * identifier of that character.
1833 tcc_define_symbol(s, "__STDC_ISO_10646__", "201605L");
1835 * The integer constant 1, intended to indicate
1836 * that values of type char16_t are UTF−16
1837 * encoded. If some other encoding is used, the
1838 * macro shall not be defined and the actual
1839 * encoding used is implementation defined.
1841 tcc_define_symbol(s, "__STDC_UTF_16__", "1");
1843 * The integer constant 1, intended to indicate
1844 * that values of type char32_t are UTF−32
1845 * encoded. If some other encoding is used, the
1846 * macro shall not be defined and the actual
1847 * encoding used is implementationdefined.
1849 tcc_define_symbol(s, "__STDC_UTF_32__", "1");
1850 #endif /* !TCC_TARGET_PE */
1851 s->cversion = 201112;
1855 * silently ignore other values, a current purpose:
1856 * allow to use a tcc as a reference compiler for "make test"
1858 break;
1859 case TCC_OPTION_shared:
1860 x = TCC_OUTPUT_DLL;
1861 goto set_output_type;
1862 case TCC_OPTION_soname:
1863 s->soname = tcc_strdup(optarg);
1864 break;
1865 case TCC_OPTION_o:
1866 if (s->outfile) {
1867 tcc_warning("multiple -o option");
1868 tcc_free(s->outfile);
1870 s->outfile = tcc_strdup(optarg);
1871 break;
1872 case TCC_OPTION_r:
1873 /* generate a .o merging several output files */
1874 s->option_r = 1;
1875 x = TCC_OUTPUT_OBJ;
1876 goto set_output_type;
1877 case TCC_OPTION_isystem:
1878 tcc_add_sysinclude_path(s, optarg);
1879 break;
1880 case TCC_OPTION_include:
1881 dynarray_add(&s->cmd_include_files,
1882 &s->nb_cmd_include_files, tcc_strdup(optarg));
1883 break;
1884 case TCC_OPTION_nostdinc:
1885 s->nostdinc = 1;
1886 break;
1887 case TCC_OPTION_nostdlib:
1888 s->nostdlib = 1;
1889 break;
1890 case TCC_OPTION_run:
1891 #ifndef TCC_IS_NATIVE
1892 tcc_error("-run is not available in a cross compiler");
1893 #endif
1894 run = optarg;
1895 x = TCC_OUTPUT_MEMORY;
1896 goto set_output_type;
1897 case TCC_OPTION_v:
1898 do ++s->verbose; while (*optarg++ == 'v');
1899 ++noaction;
1900 break;
1901 case TCC_OPTION_f:
1902 if (set_flag(s, options_f, optarg) < 0)
1903 goto unsupported_option;
1904 break;
1905 #ifdef TCC_TARGET_ARM
1906 case TCC_OPTION_mfloat_abi:
1907 /* tcc doesn't support soft float yet */
1908 if (!strcmp(optarg, "softfp")) {
1909 s->float_abi = ARM_SOFTFP_FLOAT;
1910 tcc_undefine_symbol(s, "__ARM_PCS_VFP");
1911 } else if (!strcmp(optarg, "hard"))
1912 s->float_abi = ARM_HARD_FLOAT;
1913 else
1914 tcc_error("unsupported float abi '%s'", optarg);
1915 break;
1916 #endif
1917 case TCC_OPTION_m:
1918 if (set_flag(s, options_m, optarg) < 0) {
1919 if (x = atoi(optarg), x != 32 && x != 64)
1920 goto unsupported_option;
1921 if (PTR_SIZE != x/8)
1922 return x;
1923 ++noaction;
1925 break;
1926 case TCC_OPTION_W:
1927 if (set_flag(s, options_W, optarg) < 0)
1928 goto unsupported_option;
1929 break;
1930 case TCC_OPTION_w:
1931 s->warn_none = 1;
1932 break;
1933 case TCC_OPTION_rdynamic:
1934 s->rdynamic = 1;
1935 break;
1936 case TCC_OPTION_Wl:
1937 if (linker_arg.size)
1938 --linker_arg.size, cstr_ccat(&linker_arg, ',');
1939 cstr_cat(&linker_arg, optarg, 0);
1940 if (tcc_set_linker(s, linker_arg.data))
1941 cstr_free(&linker_arg);
1942 break;
1943 case TCC_OPTION_Wp:
1944 r = optarg;
1945 goto reparse;
1946 case TCC_OPTION_E:
1947 x = TCC_OUTPUT_PREPROCESS;
1948 goto set_output_type;
1949 case TCC_OPTION_P:
1950 s->Pflag = atoi(optarg) + 1;
1951 break;
1952 case TCC_OPTION_MD:
1953 s->gen_deps = 1;
1954 break;
1955 case TCC_OPTION_MF:
1956 s->deps_outfile = tcc_strdup(optarg);
1957 break;
1958 case TCC_OPTION_dumpversion:
1959 printf ("%s\n", TCC_VERSION);
1960 exit(0);
1961 break;
1962 case TCC_OPTION_x:
1963 x = 0;
1964 if (*optarg == 'c')
1965 x = AFF_TYPE_C;
1966 else if (*optarg == 'a')
1967 x = AFF_TYPE_ASMPP;
1968 else if (*optarg == 'b')
1969 x = AFF_TYPE_BIN;
1970 else if (*optarg == 'n')
1971 x = AFF_TYPE_NONE;
1972 else
1973 tcc_warning("unsupported language '%s'", optarg);
1974 s->filetype = x | (s->filetype & ~AFF_TYPE_MASK);
1975 break;
1976 case TCC_OPTION_O:
1977 last_o = atoi(optarg);
1978 break;
1979 case TCC_OPTION_print_search_dirs:
1980 x = OPT_PRINT_DIRS;
1981 goto extra_action;
1982 case TCC_OPTION_impdef:
1983 x = OPT_IMPDEF;
1984 goto extra_action;
1985 case TCC_OPTION_ar:
1986 x = OPT_AR;
1987 extra_action:
1988 arg_start = optind - 1;
1989 if (arg_start != noaction)
1990 tcc_error("cannot parse %s here", r);
1991 tool = x;
1992 break;
1993 case TCC_OPTION_traditional:
1994 case TCC_OPTION_pedantic:
1995 case TCC_OPTION_pipe:
1996 case TCC_OPTION_s:
1997 /* ignored */
1998 break;
1999 default:
2000 unsupported_option:
2001 if (s->warn_unsupported)
2002 tcc_warning("unsupported option '%s'", r);
2003 break;
2006 if (last_o > 0)
2007 tcc_define_symbol(s, "__OPTIMIZE__", NULL);
2008 if (linker_arg.size) {
2009 r = linker_arg.data;
2010 goto arg_err;
2012 *pargc = argc - arg_start;
2013 *pargv = argv + arg_start;
2014 if (tool)
2015 return tool;
2016 if (optind != noaction)
2017 return 0;
2018 if (s->verbose == 2)
2019 return OPT_PRINT_DIRS;
2020 if (s->verbose)
2021 return OPT_V;
2022 return OPT_HELP;
2025 LIBTCCAPI void tcc_set_options(TCCState *s, const char *r)
2027 char **argv = NULL;
2028 int argc = 0;
2029 args_parser_make_argv(r, &argc, &argv);
2030 tcc_parse_args(s, &argc, &argv, 0);
2031 dynarray_reset(&argv, &argc);
2034 PUB_FUNC void tcc_print_stats(TCCState *s, unsigned total_time)
2036 if (total_time < 1)
2037 total_time = 1;
2038 if (total_bytes < 1)
2039 total_bytes = 1;
2040 fprintf(stderr, "* %d idents, %d lines, %d bytes\n"
2041 "* %0.3f s, %u lines/s, %0.1f MB/s\n",
2042 tok_ident - TOK_IDENT, total_lines, total_bytes,
2043 (double)total_time/1000,
2044 (unsigned)total_lines*1000/total_time,
2045 (double)total_bytes/1000/total_time);
2046 #ifdef MEM_DEBUG
2047 fprintf(stderr, "* %d bytes memory used\n", mem_max_size);
2048 #endif