History

WAIT/POST_SEM(): generalize interface (and more)

Currently used only with 'tcc_compile_sem' to protect tcc_compile(), but can be used with other semaphores Also fix deadlock when tcc_enter_state() is called recursively for the same state, for example with tcc_warning() from #pragma comment(option,"...") Also: - libtcc.c: error1(): use cstr_[v]printf() - tcc.h: set TCC_USING_DOUBLE_FOR_LDOUBLE for macho-arm64 (rather than for macho-X86_64) - tcc.h: define TCC_TARGET_MACHO on __APPLE__ by default - tcc.h: cleanup TCCState, move DEFASM token stuff to tcctok.h - tccgen.c: more static - Makefile/tcc.c: review githash - tccpe/tcctools: use read() instead of fgets() in pe_load_def() (all files opened by tcc for reading are now read via 'int fd') - configure/win32: don't preset CONFIG_TCCDIR (to allow to override it) - tcc.c -bench: do not include output/run-time

grischka committed 5 years ago

Commit dda95e91 parent 4b2c6cf14 files · +362 −323Browse files

Unified · Split

14 files
  1. Makefile +5 −5
  2. configure +2 −2
  3. i386-tok.h +76 −0
  4. libtcc.c +81 −98
  5. tcc.c +10 −7
  6. tcc.h +78 −132
  7. tccasm.c +17 −0
  8. tccgen.c +11 −23
  9. tccmacho.c +1 −1
  10. tccpe.c +49 −43
  11. tccpp.c +11 −2
  12. tcctok.h +14 −2
  13. tcctools.c +2 −4
  14. tests/libtcc_test_mt.c +5 −4
Makefile+5−5
@@ -134,7 +134,7 @@ all: $(PROGS) $(TCCLIBS) $(TCCDOCS) · 133 unmodified lines
134 134
135# cross compiler targets to build135# cross compiler targets to build
136TCC_X = i386 x86_64 i386-win32 x86_64-win32 x86_64-osx arm arm64 arm-wince c67136TCC_X = i386 x86_64 i386-win32 x86_64-win32 x86_64-osx arm arm64 arm-wince c67
137TCC_X += riscv64137TCC_X += riscv64 arm64-osx
138# TCC_X += arm-fpa arm-fpa-ld arm-vfp arm-eabi138# TCC_X += arm-fpa arm-fpa-ld arm-vfp arm-eabi
139 139
140# cross libtcc1.a targets to build140# cross libtcc1.a targets to build
@@ -222,10 +222,9 @@ $(TCC_FILES) : DEFINES += -DONE_SOURCE=0 · 81 unmodified lines
222$(X)tccpp.o : $(TCCDEFS_H)222$(X)tccpp.o : $(TCCDEFS_H)
223endif223endif
224 224
225TCC_GIT_HASH=$(shell git rev-parse > /dev/null 2>&1 && git rev-parse --short HEAD || echo no)225GITHASH := $(shell git rev-parse >/dev/null 2>&1 && git rev-parse --short HEAD || echo no)
226ifneq ($(TCC_GIT_HASH),no)226ifneq ($(GITHASH),no)
227MODIFIED = $(shell git diff | grep -q +++ && echo "modified ")227DEF_GITHASH := -DTCC_GITHASH="\"$(GITHASH)$(shell git diff --quiet || echo '-mod')\""
228$(X)tcc.o : DEFINES += -DTCC_GIT_HASH="\"$(MODIFIED)$(TCC_GIT_HASH)\""
229endif228endif
230 229
231ifeq ($(CONFIG_debug),yes)230ifeq ($(CONFIG_debug),yes)
@@ -247,6 +246,7 @@ $(X)%.o : %.c $(LIBTCC_INC) · 15 unmodified lines
247 246
248# additional dependencies247# additional dependencies
249$(X)tcc.o : tcctools.c248$(X)tcc.o : tcctools.c
249$(X)tcc.o : DEFINES += $(DEF_GITHASH)
250 250
251# Host Tiny C Compiler251# Host Tiny C Compiler
252tcc$(EXESUF): tcc.o $(LIBTCC)252tcc$(EXESUF): tcc.o $(LIBTCC)
configure+2−2
@@ -238,7 +238,7 @@ if test "$mingw32" = "yes" ; then · 237 unmodified lines
238 source_path="."238 source_path="."
239 fi239 fi
240 test -z "$prefix" && prefix="C:/Program Files/tcc"240 test -z "$prefix" && prefix="C:/Program Files/tcc"
241 test -z "$tccdir" && tccdir="${prefix}"241 test -z "$tccdir" && tccdir="${prefix}" && tccdir_auto="yes"
242 test -z "$bindir" && bindir="${tccdir}"242 test -z "$bindir" && bindir="${tccdir}"
243 test -z "$docdir" && docdir="${tccdir}/doc"243 test -z "$docdir" && docdir="${tccdir}/doc"
244 test -z "$libdir" && libdir="${tccdir}/libtcc"244 test -z "$libdir" && libdir="${tccdir}/libtcc"
@@ -483,7 +483,7 @@ print_mak() { · 238 unmodified lines
483echo "/* Automatically generated by configure - do not modify */" > $TMPH483echo "/* Automatically generated by configure - do not modify */" > $TMPH
484 484
485print_inc CONFIG_SYSROOT "$sysroot"485print_inc CONFIG_SYSROOT "$sysroot"
486print_inc CONFIG_TCCDIR "$tccdir"486test "$tccdir_auto" = "yes" || print_inc CONFIG_TCCDIR "$tccdir"
487print_mak CONFIG_USR_INCLUDE "$tcc_usrinclude"487print_mak CONFIG_USR_INCLUDE "$tcc_usrinclude"
488print_mak CONFIG_TCC_SYSINCLUDEPATHS "$tcc_sysincludepaths"488print_mak CONFIG_TCC_SYSINCLUDEPATHS "$tcc_sysincludepaths"
489print_mak CONFIG_TCC_LIBPATHS "$tcc_libpaths"489print_mak CONFIG_TCC_LIBPATHS "$tcc_libpaths"
i386-tok.h+76−0
@@ -1,6 +1,82 @@
1/* ------------------------------------------------------------------ */1/* ------------------------------------------------------------------ */
2/* WARNING: relative order of tokens is important. */2/* WARNING: relative order of tokens is important. */
3 3
4#define DEF_BWL(x) \
5 DEF(TOK_ASM_ ## x ## b, #x "b") \
6 DEF(TOK_ASM_ ## x ## w, #x "w") \
7 DEF(TOK_ASM_ ## x ## l, #x "l") \
8 DEF(TOK_ASM_ ## x, #x)
9#define DEF_WL(x) \
10 DEF(TOK_ASM_ ## x ## w, #x "w") \
11 DEF(TOK_ASM_ ## x ## l, #x "l") \
12 DEF(TOK_ASM_ ## x, #x)
13#ifdef TCC_TARGET_X86_64
14# define DEF_BWLQ(x) \
15 DEF(TOK_ASM_ ## x ## b, #x "b") \
16 DEF(TOK_ASM_ ## x ## w, #x "w") \
17 DEF(TOK_ASM_ ## x ## l, #x "l") \
18 DEF(TOK_ASM_ ## x ## q, #x "q") \
19 DEF(TOK_ASM_ ## x, #x)
20# define DEF_WLQ(x) \
21 DEF(TOK_ASM_ ## x ## w, #x "w") \
22 DEF(TOK_ASM_ ## x ## l, #x "l") \
23 DEF(TOK_ASM_ ## x ## q, #x "q") \
24 DEF(TOK_ASM_ ## x, #x)
25# define DEF_BWLX DEF_BWLQ
26# define DEF_WLX DEF_WLQ
27/* number of sizes + 1 */
28# define NBWLX 5
29#else
30# define DEF_BWLX DEF_BWL
31# define DEF_WLX DEF_WL
32/* number of sizes + 1 */
33# define NBWLX 4
34#endif
35
36#define DEF_FP1(x) \
37 DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
38 DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
39 DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
40 DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
41
42#define DEF_FP(x) \
43 DEF(TOK_ASM_ ## f ## x, "f" #x ) \
44 DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
45 DEF_FP1(x)
46
47#define DEF_ASMTEST(x,suffix) \
48 DEF_ASM(x ## o ## suffix) \
49 DEF_ASM(x ## no ## suffix) \
50 DEF_ASM(x ## b ## suffix) \
51 DEF_ASM(x ## c ## suffix) \
52 DEF_ASM(x ## nae ## suffix) \
53 DEF_ASM(x ## nb ## suffix) \
54 DEF_ASM(x ## nc ## suffix) \
55 DEF_ASM(x ## ae ## suffix) \
56 DEF_ASM(x ## e ## suffix) \
57 DEF_ASM(x ## z ## suffix) \
58 DEF_ASM(x ## ne ## suffix) \
59 DEF_ASM(x ## nz ## suffix) \
60 DEF_ASM(x ## be ## suffix) \
61 DEF_ASM(x ## na ## suffix) \
62 DEF_ASM(x ## nbe ## suffix) \
63 DEF_ASM(x ## a ## suffix) \
64 DEF_ASM(x ## s ## suffix) \
65 DEF_ASM(x ## ns ## suffix) \
66 DEF_ASM(x ## p ## suffix) \
67 DEF_ASM(x ## pe ## suffix) \
68 DEF_ASM(x ## np ## suffix) \
69 DEF_ASM(x ## po ## suffix) \
70 DEF_ASM(x ## l ## suffix) \
71 DEF_ASM(x ## nge ## suffix) \
72 DEF_ASM(x ## nl ## suffix) \
73 DEF_ASM(x ## ge ## suffix) \
74 DEF_ASM(x ## le ## suffix) \
75 DEF_ASM(x ## ng ## suffix) \
76 DEF_ASM(x ## nle ## suffix) \
77 DEF_ASM(x ## g ## suffix)
78
79/* ------------------------------------------------------------------ */
4/* register */80/* register */
5 DEF_ASM(al)81 DEF_ASM(al)
6 DEF_ASM(cl)82 DEF_ASM(cl)
libtcc.c+81−98
@@ -21,6 +21,7 @@ · 20 unmodified lines
21#if !defined ONE_SOURCE || ONE_SOURCE21#if !defined ONE_SOURCE || ONE_SOURCE
22#include "tccpp.c"22#include "tccpp.c"
23#include "tccgen.c"23#include "tccgen.c"
24#include "tccasm.c"
24#include "tccelf.c"25#include "tccelf.c"
25#include "tccrun.c"26#include "tccrun.c"
26#ifdef TCC_TARGET_I38627#ifdef TCC_TARGET_I386
@@ -50,9 +51,6 @@ · 23 unmodified lines
50#else51#else
51#error unknown target52#error unknown target
52#endif53#endif
53#ifdef CONFIG_TCC_ASM
54#include "tccasm.c"
55#endif
56#ifdef TCC_TARGET_PE54#ifdef TCC_TARGET_PE
57#include "tccpe.c"55#include "tccpe.c"
58#endif56#endif
@@ -68,6 +66,7 @@ · 9 unmodified lines
68 66
69/* XXX: get rid of this ASAP (or maybe not) */67/* XXX: get rid of this ASAP (or maybe not) */
70ST_DATA struct TCCState *tcc_state;68ST_DATA struct TCCState *tcc_state;
69TCC_SEM(static tcc_compile_sem);
71 70
72#ifdef MEM_DEBUG71#ifdef MEM_DEBUG
73static int nb_states;72static int nb_states;
@@ -84,19 +83,23 @@ ST_FUNC char *normalize_slashes(char *path) · 10 unmodified lines
84 return path;83 return path;
85}84}
86 85
86/* NULL if this is tcc.exe, HINSTANCE if this is libtcc.dll */
87static HMODULE tcc_module;87static HMODULE tcc_module;
88 88
89#ifndef CONFIG_TCCDIR
89/* on win32, we suppose the lib and includes are at the location of 'tcc.exe' */90/* on win32, we suppose the lib and includes are at the location of 'tcc.exe' */
90static void tcc_set_lib_path_w32(TCCState *s)91static inline char *config_tccdir_w32(char *path)
91{92{
92 char path[1024], *p;93 char *p;
93 GetModuleFileNameA(tcc_module, path, sizeof path);94 GetModuleFileName(tcc_module, path, MAX_PATH);
94 p = tcc_basename(normalize_slashes(strlwr(path)));95 p = tcc_basename(normalize_slashes(strlwr(path)));
95 if (p > path)96 if (p > path)
96 --p;97 --p;
97 *p = 0;98 *p = 0;
98 tcc_set_lib_path(s, path);99 return path;
99}100}
101#define CONFIG_TCCDIR config_tccdir_w32(alloca(MAX_PATH))
102#endif
100 103
101#ifdef TCC_TARGET_PE104#ifdef TCC_TARGET_PE
102static void tcc_add_systemdir(TCCState *s)105static void tcc_add_systemdir(TCCState *s)
@@ -118,47 +121,60 @@ BOOL WINAPI DllMain (HINSTANCE hDll, DWORD dwReason, LPVOID lpReserved) · 15 unmodified lines
118#endif121#endif
119 122
120/********************************************************/123/********************************************************/
121#if CONFIG_TCC_SEMLOCK == 0124#if CONFIG_TCC_SEMLOCK
122#define WAIT_SEM()125#if defined _WIN32
123#define POST_SEM()126ST_FUNC void wait_sem(TCCSem *p)
124#elif defined _WIN32127{
125static int tcc_sem_init;128 if (!p->init)
126static CRITICAL_SECTION tcc_cr;129 InitializeCriticalSection(&p->cr), p->init = 1;
127static void wait_sem(void)130 EnterCriticalSection(&p->cr);
128{131}
129 if (!tcc_sem_init)132ST_FUNC void post_sem(TCCSem *p)
130 InitializeCriticalSection(&tcc_cr), tcc_sem_init = 1;133{
131 EnterCriticalSection(&tcc_cr);134 LeaveCriticalSection(&p->cr);
132}135}
133#define WAIT_SEM() wait_sem()
134#define POST_SEM() LeaveCriticalSection(&tcc_cr);
135#elif defined __APPLE__136#elif defined __APPLE__
136/* Half-compatible MacOS doesn't have non-shared (process local)137/* Half-compatible MacOS doesn't have non-shared (process local)
137 semaphores. Use the dispatch framework for lightweight locks. */138 semaphores. Use the dispatch framework for lightweight locks. */
138#include <dispatch/dispatch.h>139ST_FUNC void wait_sem(TCCSem *p)
139static int tcc_sem_init;
140static dispatch_semaphore_t tcc_sem;
141static void wait_sem(void)
142{140{
143 if (!tcc_sem_init)141 if (!p->init)
144 tcc_sem = dispatch_semaphore_create(1), tcc_sem_init = 1;142 p->sem = dispatch_semaphore_create(1), p->init = 1;
145 dispatch_semaphore_wait(tcc_sem, DISPATCH_TIME_FOREVER);143 dispatch_semaphore_wait(p->sem, DISPATCH_TIME_FOREVER);
144}
145ST_FUNC void post_sem(TCCSem *p)
146{
147 dispatch_semaphore_signal(p->sem);
146}148}
147#define WAIT_SEM() wait_sem()
148#define POST_SEM() dispatch_semaphore_signal(tcc_sem)
149#else149#else
150#include <semaphore.h>150ST_FUNC void wait_sem(TCCSem *p)
151static int tcc_sem_init;151{
152static sem_t tcc_sem;152 if (!p->init)
153static void wait_sem(void)153 sem_init(&p->sem, 0, 1), p->init = 1;
154 while (sem_wait(&p->sem) < 0 && errno == EINTR);
155}
156ST_FUNC void post_sem(TCCSem *p)
154{157{
155 if (!tcc_sem_init)158 sem_post(&p->sem);
156 sem_init(&tcc_sem, 0, 1), tcc_sem_init = 1;
157 while (sem_wait (&tcc_sem) < 0 && errno == EINTR);
158}159}
159#define WAIT_SEM() wait_sem()
160#define POST_SEM() sem_post(&tcc_sem)
161#endif160#endif
161#endif
162
163PUB_FUNC void tcc_enter_state(TCCState *s1)
164{
165 if (s1->error_set_jmp_enabled)
166 return;
167 WAIT_SEM(&tcc_compile_sem);
168 tcc_state = s1;
169}
170
171PUB_FUNC void tcc_exit_state(TCCState *s1)
172{
173 if (s1->error_set_jmp_enabled)
174 return;
175 tcc_state = NULL;
176 POST_SEM(&tcc_compile_sem);
177}
162 178
163/********************************************************/179/********************************************************/
164/* copy a string and truncate it. */180/* copy a string and truncate it. */
@@ -494,52 +510,29 @@ static void tcc_split_path(TCCState *s, void *p_ary, int *p_nb_ary, const char * · 329 unmodified lines
494}510}
495 511
496/********************************************************/512/********************************************************/
513/* warning / error */
497 514
498static void strcat_vprintf(char *buf, int buf_size, const char *fmt, va_list ap)515/* warn_... option bits */
499{516#define WARN_ON 1 /* warning is on (-Woption) */
500 int len;517#define WARN_ERR 2 /* warning is an error (-Werror=option) */
501 len = strlen(buf);518#define WARN_NOE 4 /* warning is not an error (-Wno-error=option) */
502 vsnprintf(buf + len, buf_size - len, fmt, ap);
503}
504
505static void strcat_printf(char *buf, int buf_size, const char *fmt, ...)
506{
507 va_list ap;
508 va_start(ap, fmt);
509 strcat_vprintf(buf, buf_size, fmt, ap);
510 va_end(ap);
511}
512 519
513PUB_FUNC void tcc_enter_state(TCCState *s1)520/* error1() modes */
514{521enum { ERROR_WARN, ERROR_NOABORT, ERROR_ERROR };
515 WAIT_SEM();
516 tcc_state = s1;
517}
518
519PUB_FUNC void tcc_exit_state(void)
520{
521 tcc_state = NULL;
522 POST_SEM();
523}
524
525#define ERROR_WARN 0
526#define ERROR_NOABORT 1
527#define ERROR_ERROR 2
528 522
529static void error1(int mode, const char *fmt, va_list ap)523static void error1(int mode, const char *fmt, va_list ap)
530{524{
531 char buf[2048];
532 BufferedFile **pf, *f;525 BufferedFile **pf, *f;
533 TCCState *s1 = tcc_state;526 TCCState *s1 = tcc_state;
527 CString cs;
528
529 cstr_new(&cs);
534 530
535 buf[0] = '\0';
536 if (s1 == NULL)531 if (s1 == NULL)
537 /* can happen only if called from tcc_malloc(): 'out of memory' */532 /* can happen only if called from tcc_malloc(): 'out of memory' */
538 goto no_file;533 goto no_file;
539 534
540 if (!s1->error_set_jmp_enabled)535 tcc_exit_state(s1);
541 /* tcc_state just was set by tcc_enter_state() */
542 tcc_exit_state();
543 536
544 if (mode == ERROR_WARN) {537 if (mode == ERROR_WARN) {
545 if (s1->warn_error)538 if (s1->warn_error)
@@ -567,33 +560,30 @@ static void error1(int mode, const char *fmt, va_list ap) · 21 unmodified lines
567 }560 }
568 if (f) {561 if (f) {
569 for(pf = s1->include_stack; pf < s1->include_stack_ptr; pf++)562 for(pf = s1->include_stack; pf < s1->include_stack_ptr; pf++)
570 strcat_printf(buf, sizeof(buf), "In file included from %s:%d:\n",563 cstr_printf(&cs, "In file included from %s:%d:\n",
571 (*pf)->filename, (*pf)->line_num);564 (*pf)->filename, (*pf)->line_num);
572 strcat_printf(buf, sizeof(buf), "%s:%d: ",565 cstr_printf(&cs, "%s:%d: ",
573 f->filename, f->line_num - !!(tok_flags & TOK_FLAG_BOL));566 f->filename, f->line_num - !!(tok_flags & TOK_FLAG_BOL));
574 } else if (s1->current_filename) {567 } else if (s1->current_filename) {
575 strcat_printf(buf, sizeof(buf), "%s: ", s1->current_filename);568 cstr_printf(&cs, "%s: ", s1->current_filename);
576 }569 }
577 570
578no_file:571no_file:
579 if (0 == buf[0])572 if (0 == cs.size)
580 strcat_printf(buf, sizeof(buf), "tcc: ");573 cstr_printf(&cs, "tcc: ");
581 if (mode == ERROR_WARN)574 cstr_printf(&cs, mode == ERROR_WARN ? "warning: " : "error: ");
582 strcat_printf(buf, sizeof(buf), "warning: ");575 cstr_vprintf(&cs, fmt, ap);
583 else
584 strcat_printf(buf, sizeof(buf), "error: ");
585 strcat_vprintf(buf, sizeof(buf), fmt, ap);
586 if (!s1 || !s1->error_func) {576 if (!s1 || !s1->error_func) {
587 /* default case: stderr */577 /* default case: stderr */
588 if (s1 && s1->output_type == TCC_OUTPUT_PREPROCESS && s1->ppfp == stdout)578 if (s1 && s1->output_type == TCC_OUTPUT_PREPROCESS && s1->ppfp == stdout)
589 /* print a newline during tcc -E */579 printf("\n"); /* print a newline during tcc -E */
590 printf("\n"), fflush(stdout);
591 fflush(stdout); /* flush -v output */580 fflush(stdout); /* flush -v output */
592 fprintf(stderr, "%s\n", buf);581 fprintf(stderr, "%s\n", (char*)cs.data);
593 fflush(stderr); /* print error/warning now (win32) */582 fflush(stderr); /* print error/warning now (win32) */
594 } else {583 } else {
595 s1->error_func(s1->error_opaque, buf);584 s1->error_func(s1->error_opaque, (char*)cs.data);
596 }585 }
586 cstr_free(&cs);
597 if (s1) {587 if (s1) {
598 if (mode != ERROR_WARN)588 if (mode != ERROR_WARN)
599 s1->nb_errors++;589 s1->nb_errors++;
@@ -718,9 +708,9 @@ static int tcc_compile(TCCState *s1, int filetype, const char *str, int fd) · 118 unmodified lines
718 variables, which may or may not have advantages */708 variables, which may or may not have advantages */
719 709
720 tcc_enter_state(s1);710 tcc_enter_state(s1);
711 s1->error_set_jmp_enabled = 1;
721 712
722 if (setjmp(s1->error_jmp_buf) == 0) {713 if (setjmp(s1->error_jmp_buf) == 0) {
723 s1->error_set_jmp_enabled = 1;
724 s1->nb_errors = 0;714 s1->nb_errors = 0;
725 715
726 if (fd == -1) {716 if (fd == -1) {
@@ -738,19 +728,16 @@ static int tcc_compile(TCCState *s1, int filetype, const char *str, int fd) · 11 unmodified lines
738 if (s1->output_type == TCC_OUTPUT_PREPROCESS) {728 if (s1->output_type == TCC_OUTPUT_PREPROCESS) {
739 tcc_preprocess(s1);729 tcc_preprocess(s1);
740 } else if (filetype & (AFF_TYPE_ASM | AFF_TYPE_ASMPP)) {730 } else if (filetype & (AFF_TYPE_ASM | AFF_TYPE_ASMPP)) {
741#ifdef CONFIG_TCC_ASM
742 tcc_assemble(s1, !!(filetype & AFF_TYPE_ASMPP));731 tcc_assemble(s1, !!(filetype & AFF_TYPE_ASMPP));
743#else
744 tcc_error_noabort("asm not supported");
745#endif
746 } else {732 } else {
747 tccgen_compile(s1);733 tccgen_compile(s1);
748 }734 }
749 }735 }
750 s1->error_set_jmp_enabled = 0;
751 tccgen_finish(s1);736 tccgen_finish(s1);
752 preprocess_end(s1);737 preprocess_end(s1);
753 tcc_exit_state();738
739 s1->error_set_jmp_enabled = 0;
740 tcc_exit_state(s1);
754 741
755 tccelf_end_file(s1);742 tccelf_end_file(s1);
756 return s1->nb_errors != 0 ? -1 : 0;743 return s1->nb_errors != 0 ? -1 : 0;
@@ -821,11 +808,7 @@ LIBTCCAPI TCCState *tcc_new(void) · 64 unmodified lines
821 808
822 tccelf_new(s);809 tccelf_new(s);
823 810
824#ifdef _WIN32
825 tcc_set_lib_path_w32(s);
826#else
827 tcc_set_lib_path(s, CONFIG_TCCDIR);811 tcc_set_lib_path(s, CONFIG_TCCDIR);
828#endif
829 return s;812 return s;
830}813}
831 814
tcc.c+10−7
@@ -154,10 +154,8 @@ static const char help2[] = · 153 unmodified lines
154 154
155static const char version[] =155static const char version[] =
156 "tcc version "TCC_VERSION156 "tcc version "TCC_VERSION
157#ifdef TCC_GIT_HASH157#ifdef TCC_GITHASH
158 " - " TCC_GIT_HASH158 " "TCC_GITHASH
159#else
160 " - unknown hash"
161#endif159#endif
162 " ("160 " ("
163#ifdef TCC_TARGET_I386161#ifdef TCC_TARGET_I386
@@ -276,7 +274,7 @@ int main(int argc0, char **argv0) · 112 unmodified lines
276{274{
277 TCCState *s, *s1;275 TCCState *s, *s1;
278 int ret, opt, n = 0, t = 0, done;276 int ret, opt, n = 0, t = 0, done;
279 unsigned start_time = 0;277 unsigned start_time = 0, end_time = 0;
280 const char *first_file;278 const char *first_file;
281 int argc; char **argv;279 int argc; char **argv;
282 FILE *ppfp = stdout;280 FILE *ppfp = stdout;
@@ -372,6 +370,9 @@ redo: · 89 unmodified lines
372 done = ret || ++n >= s->nb_files;370 done = ret || ++n >= s->nb_files;
373 } while (!done && (s->output_type != TCC_OUTPUT_OBJ || s->option_r));371 } while (!done && (s->output_type != TCC_OUTPUT_OBJ || s->option_r));
374 372
373 if (s->do_bench)
374 end_time = getclock_ms();
375
375 if (s->run_test) {376 if (s->run_test) {
376 t = 0;377 t = 0;
377 } else if (s->output_type == TCC_OUTPUT_PREPROCESS) {378 } else if (s->output_type == TCC_OUTPUT_PREPROCESS) {
@@ -391,13 +392,15 @@ redo: · 13 unmodified lines
391 }392 }
392 }393 }
393 394
394 if (s->do_bench && done && !(t | ret))395 if (done && 0 == t && 0 == ret && s->do_bench)
395 tcc_print_stats(s, getclock_ms() - start_time);396 tcc_print_stats(s, end_time - start_time);
397
396 tcc_delete(s);398 tcc_delete(s);
397 if (!done)399 if (!done)
398 goto redo; /* compile more files with -c */400 goto redo; /* compile more files with -c */
399 if (t)401 if (t)
400 goto redo; /* run more tests with -dt -run */402 goto redo; /* run more tests with -dt -run */
403
401 if (ppfp && ppfp != stdout)404 if (ppfp && ppfp != stdout)
402 fclose(ppfp);405 fclose(ppfp);
403 return ret;406 return ret;
tcc.h+78−132
@@ -55,6 +55,7 @@ extern long double strtold (const char *__nptr, char **__endptr); · 54 unmodified lines
55# include <windows.h>55# include <windows.h>
56# include <io.h> /* open, close etc. */56# include <io.h> /* open, close etc. */
57# include <direct.h> /* getcwd */57# include <direct.h> /* getcwd */
58# include <malloc.h> /* alloca */
58# ifdef __GNUC__59# ifdef __GNUC__
59# include <stdint.h>60# include <stdint.h>
60# endif61# endif
@@ -91,6 +92,9 @@ extern long double strtold (const char *__nptr, char **__endptr); · 30 unmodified lines
91# define __x86_64__ 192# define __x86_64__ 1
92# endif93# endif
93# endif94# endif
95# ifndef va_copy
96# define va_copy(a,b) a = b
97# endif
94# undef CONFIG_TCC_STATIC98# undef CONFIG_TCC_STATIC
95#endif99#endif
96 100
@@ -124,7 +128,6 @@ extern long double strtold (const char *__nptr, char **__endptr); · 27 unmodified lines
124# define PRINTF_LIKE(x,y) __attribute__ ((format (printf, (x), (y))))128# define PRINTF_LIKE(x,y) __attribute__ ((format (printf, (x), (y))))
125#endif129#endif
126 130
127
128#ifdef _WIN32131#ifdef _WIN32
129# define IS_DIRSEP(c) (c == '/' || c == '\\')132# define IS_DIRSEP(c) (c == '/' || c == '\\')
130# define IS_ABSPATH(p) (IS_DIRSEP(p[0]) || (p[0] && p[1] == ':' && IS_DIRSEP(p[2])))133# define IS_ABSPATH(p) (IS_DIRSEP(p[0]) || (p[0] && p[1] == ':' && IS_DIRSEP(p[2])))
@@ -179,6 +182,9 @@ extern long double strtold (const char *__nptr, char **__endptr); · 48 unmodified lines
179# ifdef _WIN32182# ifdef _WIN32
180# define TCC_TARGET_PE 1183# define TCC_TARGET_PE 1
181# endif184# endif
185# ifdef __APPLE__
186# define TCC_TARGET_MACHO 1
187# endif
182#endif188#endif
183 189
184/* only native compiler supports -run */190/* only native compiler supports -run */
@@ -226,7 +232,7 @@ extern long double strtold (const char *__nptr, char **__endptr); · 41 unmodified lines
226/* No ten-byte long doubles on window and macos except in232/* No ten-byte long doubles on window and macos except in
227 cross-compilers made by a mingw-GCC */233 cross-compilers made by a mingw-GCC */
228#if defined TCC_TARGET_PE \234#if defined TCC_TARGET_PE \
229 || (defined TCC_TARGET_MACHO && defined TCC_TARGET_X86_64) \235 || (defined TCC_TARGET_MACHO && defined TCC_TARGET_ARM64) \
230 || (defined _WIN32 && !defined __GNUC__)236 || (defined _WIN32 && !defined __GNUC__)
231# define TCC_USING_DOUBLE_FOR_LDOUBLE 1237# define TCC_USING_DOUBLE_FOR_LDOUBLE 1
232#endif238#endif
@@ -236,7 +242,7 @@ extern long double strtold (const char *__nptr, char **__endptr); · 3 unmodified lines
236#ifndef CONFIG_SYSROOT242#ifndef CONFIG_SYSROOT
237# define CONFIG_SYSROOT ""243# define CONFIG_SYSROOT ""
238#endif244#endif
239#ifndef CONFIG_TCCDIR245#if !defined CONFIG_TCCDIR && !defined _WIN32
240# define CONFIG_TCCDIR "/usr/local/lib/tcc"246# define CONFIG_TCCDIR "/usr/local/lib/tcc"
241#endif247#endif
242#ifndef CONFIG_LDDIR248#ifndef CONFIG_LDDIR
@@ -751,15 +757,6 @@ struct TCCState { · 508 unmodified lines
751 unsigned char enable_new_dtags; /* -Wl,--enable-new-dtags */757 unsigned char enable_new_dtags; /* -Wl,--enable-new-dtags */
752 unsigned int cversion; /* supported C ISO version, 199901 (the default), 201112, ... */758 unsigned int cversion; /* supported C ISO version, 199901 (the default), 201112, ... */
753 759
754 char *tcc_lib_path; /* CONFIG_TCCDIR or -B option */
755 char *soname; /* as specified on the command line (-soname) */
756 char *rpath; /* as specified on the command line (-Wl,-rpath=) */
757
758 /* output type, see TCC_OUTPUT_XXX */
759 int output_type;
760 /* output format, see TCC_OUTPUT_FORMAT_xxx */
761 int output_format;
762
763 /* C language options */760 /* C language options */
764 unsigned char char_is_unsigned;761 unsigned char char_is_unsigned;
765 unsigned char leading_underscore;762 unsigned char leading_underscore;
@@ -776,10 +773,14 @@ struct TCCState { · 10 unmodified lines
776 unsigned char warn_implicit_function_declaration;773 unsigned char warn_implicit_function_declaration;
777 unsigned char warn_discarded_qualifiers;774 unsigned char warn_discarded_qualifiers;
778 #define WARN_ON 1 /* warning is on (-Woption) */775 #define WARN_ON 1 /* warning is on (-Woption) */
779 #define WARN_ERR 2 /* warning is an error (-Werror=option) */
780 #define WARN_NOE 4 /* warning is not an error (-Wno-error=option) */
781 unsigned char warn_num; /* temp var for tcc_warning_c() */776 unsigned char warn_num; /* temp var for tcc_warning_c() */
782 777
778 unsigned char option_r; /* option -r */
779 unsigned char do_bench; /* option -bench */
780 unsigned char just_deps; /* option -M */
781 unsigned char gen_deps; /* option -MD */
782 unsigned char include_sys_deps; /* option -MD */
783
783 /* compile with debug symbol (and use them if error during execution) */784 /* compile with debug symbol (and use them if error during execution) */
784 unsigned char do_debug;785 unsigned char do_debug;
785 unsigned char do_backtrace;786 unsigned char do_backtrace;
@@ -789,30 +790,41 @@ struct TCCState { · 3 unmodified lines
789#endif790#endif
790 unsigned char test_coverage; /* generate test coverage code */791 unsigned char test_coverage; /* generate test coverage code */
791 792
793 /* use GNU C extensions */
794 unsigned char gnu_ext;
795 /* use TinyCC extensions */
796 unsigned char tcc_ext;
797
798 unsigned char dflag; /* -dX value */
799 unsigned char Pflag; /* -P switch (LINE_MACRO_OUTPUT_FORMAT) */
800
801#ifdef TCC_TARGET_X86_64
802 unsigned char nosse; /* For -mno-sse support. */
803#endif
792#ifdef TCC_TARGET_ARM804#ifdef TCC_TARGET_ARM
793 enum float_abi float_abi; /* float ABI of the generated code*/805 unsigned char float_abi; /* float ABI of the generated code*/
794#endif806#endif
795 int run_test; /* nth test to run with -dt -run */
796 807
797 addr_t text_addr; /* address of text section */
798 unsigned char has_text_addr;808 unsigned char has_text_addr;
799 809 addr_t text_addr; /* address of text section */
800 unsigned section_align; /* section alignment */810 unsigned section_align; /* section alignment */
811#ifdef TCC_TARGET_I386
812 int seg_size; /* 32. Can be 16 with i386 assembler (.code16) */
813#endif
801 814
802 /* use GNU C extensions */815 char *tcc_lib_path; /* CONFIG_TCCDIR or -B option */
803 unsigned char gnu_ext;816 char *soname; /* as specified on the command line (-soname) */
804 /* use TinyCC extensions */817 char *rpath; /* as specified on the command line (-Wl,-rpath=) */
805 unsigned char tcc_ext;
806 818
807 char *init_symbol; /* symbols to call at load-time (not used currently) */819 char *init_symbol; /* symbols to call at load-time (not used currently) */
808 char *fini_symbol; /* symbols to call at unload-time (not used currently) */820 char *fini_symbol; /* symbols to call at unload-time (not used currently) */
809 821
810#ifdef TCC_TARGET_I386822 /* output type, see TCC_OUTPUT_XXX */
811 int seg_size; /* 32. Can be 16 with i386 assembler (.code16) */823 int output_type;
812#endif824 /* output format, see TCC_OUTPUT_FORMAT_xxx */
813#ifdef TCC_TARGET_X86_64825 int output_format;
814 unsigned char nosse; /* For -mno-sse support. */826 /* nth test to run with -dt -run */
815#endif827 int run_test;
816 828
817 /* array of all loaded dlls (including those referenced by loaded dlls) */829 /* array of all loaded dlls (including those referenced by loaded dlls) */
818 DLLReference **loaded_dlls;830 DLLReference **loaded_dlls;
@@ -847,13 +859,6 @@ struct TCCState { · 28 unmodified lines
847 859
848 /* output file for preprocessing (-E) */860 /* output file for preprocessing (-E) */
849 FILE *ppfp;861 FILE *ppfp;
850 enum {
851 LINE_MACRO_OUTPUT_FORMAT_GCC,
852 LINE_MACRO_OUTPUT_FORMAT_NONE,
853 LINE_MACRO_OUTPUT_FORMAT_STD,
854 LINE_MACRO_OUTPUT_FORMAT_P10 = 11
855 } Pflag; /* -P switch */
856 char dflag; /* -dX value */
857 862
858 /* for -MD/-MF: collected dependencies for this compilation */863 /* for -MD/-MF: collected dependencies for this compilation */
859 char **target_deps;864 char **target_deps;
@@ -922,11 +927,11 @@ struct TCCState { · 62 unmodified lines
922 int nb_sym_attrs;927 int nb_sym_attrs;
923 /* ptr to next reloc entry reused */928 /* ptr to next reloc entry reused */
924 ElfW_Rel *qrel;929 ElfW_Rel *qrel;
925# define qrel s1->qrel930 #define qrel s1->qrel
926 931
927#ifdef TCC_TARGET_RISCV64932#ifdef TCC_TARGET_RISCV64
928 struct pcrel_hi { addr_t addr, val; } last_hi;933 struct pcrel_hi { addr_t addr, val; } last_hi;
929# define last_hi s1->last_hi934 #define last_hi s1->last_hi
930#endif935#endif
931 936
932#ifdef TCC_TARGET_PE937#ifdef TCC_TARGET_PE
@@ -963,8 +968,6 @@ struct TCCState { · 30 unmodified lines
963 int rt_num_callers;968 int rt_num_callers;
964#endif969#endif
965 970
966 int fd, cc; /* used by tcc_load_ldscript */
967
968 /* benchmark info */971 /* benchmark info */
969 int total_idents;972 int total_idents;
970 int total_lines;973 int total_lines;
@@ -974,7 +977,10 @@ struct TCCState { · 3 unmodified lines
974 /* option -dnum (for general development purposes) */977 /* option -dnum (for general development purposes) */
975 int g_debug;978 int g_debug;
976 979
977 /* for warnings/errors for object files*/980 /* used by tcc_load_ldscript */
981 int fd, cc;
982
983 /* for warnings/errors for object files */
978 const char *current_filename;984 const char *current_filename;
979 985
980 /* used by main and tcc_parse_args only */986 /* used by main and tcc_parse_args only */
@@ -982,11 +988,6 @@ struct TCCState { · 1 unmodified line
982 int nb_files; /* number thereof */988 int nb_files; /* number thereof */
983 int nb_libraries; /* number of libs thereof */989 int nb_libraries; /* number of libs thereof */
984 char *outfile; /* output filename */990 char *outfile; /* output filename */
985 unsigned char option_r; /* option -r */
986 unsigned char do_bench; /* option -bench */
987 int just_deps; /* option -M */
988 int gen_deps; /* option -MD */
989 int include_sys_deps; /* option -MD */
990 char *deps_outfile; /* option -MF */991 char *deps_outfile; /* option -MF */
991 int argc;992 int argc;
992 char **argv;993 char **argv;
@@ -1161,91 +1162,6 @@ struct filespec { · 168 unmodified lines
1161/* all identifiers and strings have token above that */1162/* all identifiers and strings have token above that */
1162#define TOK_IDENT 2561163#define TOK_IDENT 256
1163 1164
1164#define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
1165#define TOK_ASM_int TOK_INT
1166#define DEF_ASMDIR(x) DEF(TOK_ASMDIR_ ## x, "." #x)
1167#define TOK_ASMDIR_FIRST TOK_ASMDIR_byte
1168#define TOK_ASMDIR_LAST TOK_ASMDIR_section
1169
1170#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
1171/* only used for i386 asm opcodes definitions */
1172#define DEF_BWL(x) \
1173 DEF(TOK_ASM_ ## x ## b, #x "b") \
1174 DEF(TOK_ASM_ ## x ## w, #x "w") \
1175 DEF(TOK_ASM_ ## x ## l, #x "l") \
1176 DEF(TOK_ASM_ ## x, #x)
1177#define DEF_WL(x) \
1178 DEF(TOK_ASM_ ## x ## w, #x "w") \
1179 DEF(TOK_ASM_ ## x ## l, #x "l") \
1180 DEF(TOK_ASM_ ## x, #x)
1181#ifdef TCC_TARGET_X86_64
1182# define DEF_BWLQ(x) \
1183 DEF(TOK_ASM_ ## x ## b, #x "b") \
1184 DEF(TOK_ASM_ ## x ## w, #x "w") \
1185 DEF(TOK_ASM_ ## x ## l, #x "l") \
1186 DEF(TOK_ASM_ ## x ## q, #x "q") \
1187 DEF(TOK_ASM_ ## x, #x)
1188# define DEF_WLQ(x) \
1189 DEF(TOK_ASM_ ## x ## w, #x "w") \
1190 DEF(TOK_ASM_ ## x ## l, #x "l") \
1191 DEF(TOK_ASM_ ## x ## q, #x "q") \
1192 DEF(TOK_ASM_ ## x, #x)
1193# define DEF_BWLX DEF_BWLQ
1194# define DEF_WLX DEF_WLQ
1195/* number of sizes + 1 */
1196# define NBWLX 5
1197#else
1198# define DEF_BWLX DEF_BWL
1199# define DEF_WLX DEF_WL
1200/* number of sizes + 1 */
1201# define NBWLX 4
1202#endif
1203
1204#define DEF_FP1(x) \
1205 DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
1206 DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
1207 DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
1208 DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
1209
1210#define DEF_FP(x) \
1211 DEF(TOK_ASM_ ## f ## x, "f" #x ) \
1212 DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
1213 DEF_FP1(x)
1214
1215#define DEF_ASMTEST(x,suffix) \
1216 DEF_ASM(x ## o ## suffix) \
1217 DEF_ASM(x ## no ## suffix) \
1218 DEF_ASM(x ## b ## suffix) \
1219 DEF_ASM(x ## c ## suffix) \
1220 DEF_ASM(x ## nae ## suffix) \
1221 DEF_ASM(x ## nb ## suffix) \
1222 DEF_ASM(x ## nc ## suffix) \
1223 DEF_ASM(x ## ae ## suffix) \
1224 DEF_ASM(x ## e ## suffix) \
1225 DEF_ASM(x ## z ## suffix) \
1226 DEF_ASM(x ## ne ## suffix) \
1227 DEF_ASM(x ## nz ## suffix) \
1228 DEF_ASM(x ## be ## suffix) \
1229 DEF_ASM(x ## na ## suffix) \
1230 DEF_ASM(x ## nbe ## suffix) \
1231 DEF_ASM(x ## a ## suffix) \
1232 DEF_ASM(x ## s ## suffix) \
1233 DEF_ASM(x ## ns ## suffix) \
1234 DEF_ASM(x ## p ## suffix) \
1235 DEF_ASM(x ## pe ## suffix) \
1236 DEF_ASM(x ## np ## suffix) \
1237 DEF_ASM(x ## po ## suffix) \
1238 DEF_ASM(x ## l ## suffix) \
1239 DEF_ASM(x ## nge ## suffix) \
1240 DEF_ASM(x ## nl ## suffix) \
1241 DEF_ASM(x ## ge ## suffix) \
1242 DEF_ASM(x ## le ## suffix) \
1243 DEF_ASM(x ## ng ## suffix) \
1244 DEF_ASM(x ## nle ## suffix) \
1245 DEF_ASM(x ## g ## suffix)
1246
1247#endif /* defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64 */
1248
1249enum tcc_token {1165enum tcc_token {
1250 TOK_LAST = TOK_IDENT - 11166 TOK_LAST = TOK_IDENT - 1
1251#define DEF(id, str) ,id1167#define DEF(id, str) ,id
@@ -1306,6 +1222,7 @@ ST_FUNC void cstr_wccat(CString *cstr, int ch); · 54 unmodified lines
1306ST_FUNC void cstr_new(CString *cstr);1222ST_FUNC void cstr_new(CString *cstr);
1307ST_FUNC void cstr_free(CString *cstr);1223ST_FUNC void cstr_free(CString *cstr);
1308ST_FUNC int cstr_printf(CString *cs, const char *fmt, ...) PRINTF_LIKE(2,3);1224ST_FUNC int cstr_printf(CString *cs, const char *fmt, ...) PRINTF_LIKE(2,3);
1225ST_FUNC int cstr_vprintf(CString *cstr, const char *fmt, va_list ap);
1309ST_FUNC void cstr_reset(CString *cstr);1226ST_FUNC void cstr_reset(CString *cstr);
1310 1227
1311ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen);1228ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen);
@@ -1397,6 +1314,13 @@ ST_DATA TokenSym **table_ident; · 85 unmodified lines
1397#define IS_ID 21314#define IS_ID 2
1398#define IS_NUM 41315#define IS_NUM 4
1399 1316
1317enum line_macro_output_format {
1318 LINE_MACRO_OUTPUT_FORMAT_GCC,
1319 LINE_MACRO_OUTPUT_FORMAT_NONE,
1320 LINE_MACRO_OUTPUT_FORMAT_STD,
1321 LINE_MACRO_OUTPUT_FORMAT_P10 = 11
1322};
1323
1400ST_FUNC TokenSym *tok_alloc(const char *str, int len);1324ST_FUNC TokenSym *tok_alloc(const char *str, int len);
1401ST_FUNC int tok_alloc_const(const char *str);1325ST_FUNC int tok_alloc_const(const char *str);
1402ST_FUNC const char *get_tok_str(int v, CValue *cv);1326ST_FUNC const char *get_tok_str(int v, CValue *cv);
@@ -1770,12 +1694,12 @@ ST_FUNC int tcc_load_coff(TCCState * s1, int fd); · 367 unmodified lines
1770/* ------------ tccasm.c ------------ */1694/* ------------ tccasm.c ------------ */
1771ST_FUNC void asm_instr(void);1695ST_FUNC void asm_instr(void);
1772ST_FUNC void asm_global_instr(void);1696ST_FUNC void asm_global_instr(void);
1697ST_FUNC int tcc_assemble(TCCState *s1, int do_preprocess);
1773#ifdef CONFIG_TCC_ASM1698#ifdef CONFIG_TCC_ASM
1774ST_FUNC int find_constraint(ASMOperand *operands, int nb_operands, const char *name, const char **pp);1699ST_FUNC int find_constraint(ASMOperand *operands, int nb_operands, const char *name, const char **pp);
1775ST_FUNC Sym* get_asm_sym(int name, Sym *csym);1700ST_FUNC Sym* get_asm_sym(int name, Sym *csym);
1776ST_FUNC void asm_expr(TCCState *s1, ExprValue *pe);1701ST_FUNC void asm_expr(TCCState *s1, ExprValue *pe);
1777ST_FUNC int asm_int_expr(TCCState *s1);1702ST_FUNC int asm_int_expr(TCCState *s1);
1778ST_FUNC int tcc_assemble(TCCState *s1, int do_preprocess);
1779/* ------------ i386-asm.c ------------ */1703/* ------------ i386-asm.c ------------ */
1780ST_FUNC void gen_expr32(ExprValue *pe);1704ST_FUNC void gen_expr32(ExprValue *pe);
1781#ifdef TCC_TARGET_X86_641705#ifdef TCC_TARGET_X86_64
@@ -1844,6 +1768,28 @@ ST_FUNC void tcc_tool_cross(TCCState *s, char **argv, int option); · 62 unmodified lines
1844ST_FUNC void gen_makedeps(TCCState *s, const char *target, const char *filename);1768ST_FUNC void gen_makedeps(TCCState *s, const char *target, const char *filename);
1845#endif1769#endif
1846 1770
1771/********************************************************/
1772#if CONFIG_TCC_SEMLOCK
1773#if defined _WIN32
1774typedef struct { int init; CRITICAL_SECTION cr; } TCCSem;
1775#elif defined __APPLE__
1776#include <dispatch/dispatch.h>
1777typedef struct { int init; dispatch_semaphore_t sem; } TCCSem;
1778#else
1779#include <semaphore.h>
1780typedef struct { int init; sem_t sem; } TCCSem;
1781#endif
1782ST_FUNC void wait_sem(TCCSem *p);
1783ST_FUNC void post_sem(TCCSem *p);
1784#define TCC_SEM(s) TCCSem s
1785#define WAIT_SEM wait_sem
1786#define POST_SEM post_sem
1787#else
1788#define TCC_SEM(s)
1789#define WAIT_SEM(p)
1790#define POST_SEM(p)
1791#endif
1792
1847/********************************************************/1793/********************************************************/
1848#undef ST_DATA1794#undef ST_DATA
1849#if ONE_SOURCE1795#if ONE_SOURCE
@@ -1875,7 +1821,7 @@ ST_FUNC void gen_makedeps(TCCState *s, const char *target, const char *filename) · 25 unmodified lines
1875#define total_bytes TCC_STATE_VAR(total_bytes)1821#define total_bytes TCC_STATE_VAR(total_bytes)
1876 1822
1877PUB_FUNC void tcc_enter_state(TCCState *s1);1823PUB_FUNC void tcc_enter_state(TCCState *s1);
1878PUB_FUNC void tcc_exit_state(void);1824PUB_FUNC void tcc_exit_state(TCCState *s1);
1879 1825
1880/* conditional warning depending on switch */1826/* conditional warning depending on switch */
1881#define tcc_warning_c(sw) TCC_SET_STATE((\1827#define tcc_warning_c(sw) TCC_SET_STATE((\
tccasm.c+17−0
@@ -1298,4 +1298,21 @@ ST_FUNC void asm_global_instr(void) · 1297 unmodified lines
1298 cstr_free(&astr);1298 cstr_free(&astr);
1299 nocode_wanted = saved_nocode_wanted;1299 nocode_wanted = saved_nocode_wanted;
1300}1300}
1301
1302/********************************************************/
1303#else
1304ST_FUNC int tcc_assemble(TCCState *s1, int do_preprocess)
1305{
1306 tcc_error("asm not supported");
1307}
1308
1309ST_FUNC void asm_instr(void)
1310{
1311 tcc_error("inline asm() not supported");
1312}
1313
1314ST_FUNC void asm_global_instr(void)
1315{
1316 tcc_error("inline asm() not supported");
1317}
1301#endif /* CONFIG_TCC_ASM */1318#endif /* CONFIG_TCC_ASM */
tccgen.c+11−23
@@ -97,7 +97,7 @@ static CString initstr; · 96 unmodified lines
97#define VT_PTRDIFF_T (VT_LONG | VT_LLONG)97#define VT_PTRDIFF_T (VT_LONG | VT_LLONG)
98#endif98#endif
99 99
100ST_DATA struct switch_t {100static struct switch_t {
101 struct case_t {101 struct case_t {
102 int64_t v1, v2;102 int64_t v1, v2;
103 int sym;103 int sym;
@@ -111,12 +111,12 @@ ST_DATA struct switch_t { · 7 unmodified lines
111 111
112#define MAX_TEMP_LOCAL_VARIABLE_NUMBER 8112#define MAX_TEMP_LOCAL_VARIABLE_NUMBER 8
113/*list of temporary local variables on the stack in current function. */113/*list of temporary local variables on the stack in current function. */
114ST_DATA struct temp_local_variable {114static struct temp_local_variable {
115 int location; //offset on stack. Svalue.c.i115 int location; //offset on stack. Svalue.c.i
116 short size;116 short size;
117 short align;117 short align;
118} arr_temp_local_vars[MAX_TEMP_LOCAL_VARIABLE_NUMBER];118} arr_temp_local_vars[MAX_TEMP_LOCAL_VARIABLE_NUMBER];
119short nb_temp_local_vars;119static int nb_temp_local_vars;
120 120
121static struct scope {121static struct scope {
122 struct scope *prev;122 struct scope *prev;
@@ -132,6 +132,11 @@ typedef struct { · 9 unmodified lines
132 Sym *flex_array_ref;132 Sym *flex_array_ref;
133} init_params;133} init_params;
134 134
135#if 1
136#define precedence_parser
137static void init_prec(void);
138#endif
139
135/********************************************************/140/********************************************************/
136/* stab debug support */141/* stab debug support */
137 142
@@ -215,23 +220,6 @@ static struct { · 77 unmodified lines
215} tcov_data;220} tcov_data;
216 221
217/********************************************************/222/********************************************************/
218#if 1
219#define precedence_parser
220static void init_prec(void);
221#endif
222/********************************************************/
223#ifndef CONFIG_TCC_ASM
224ST_FUNC void asm_instr(void)
225{
226 tcc_error("inline asm() not supported");
227}
228ST_FUNC void asm_global_instr(void)
229{
230 tcc_error("inline asm() not supported");
231}
232#endif
233
234/* ------------------------------------------------------------------------- */
235static void gen_cast(CType *type);223static void gen_cast(CType *type);
236static void gen_cast_s(int t);224static void gen_cast_s(int t);
237static inline CType *pointed_type(CType *type);225static inline CType *pointed_type(CType *type);
@@ -7247,7 +7235,7 @@ static void vla_leave(struct scope *o) · 7009 unmodified lines
7247/* ------------------------------------------------------------------------- */7235/* ------------------------------------------------------------------------- */
7248/* local scopes */7236/* local scopes */
7249 7237
7250void new_scope(struct scope *o)7238static void new_scope(struct scope *o)
7251{7239{
7252 /* copy and link previous scope */7240 /* copy and link previous scope */
7253 *o = *cur_scope;7241 *o = *cur_scope;
@@ -7264,7 +7252,7 @@ void new_scope(struct scope *o) · 10 unmodified lines
7264 tcc_debug_stabn(tcc_state, N_LBRAC, ind - func_ind);7252 tcc_debug_stabn(tcc_state, N_LBRAC, ind - func_ind);
7265}7253}
7266 7254
7267void prev_scope(struct scope *o, int is_expr)7255static void prev_scope(struct scope *o, int is_expr)
7268{7256{
7269 vla_leave(o->prev);7257 vla_leave(o->prev);
7270 7258
@@ -7292,7 +7280,7 @@ void prev_scope(struct scope *o, int is_expr) · 21 unmodified lines
7292}7280}
7293 7281
7294/* leave a scope via break/continue(/goto) */7282/* leave a scope via break/continue(/goto) */
7295void leave_scope(struct scope *o)7283static void leave_scope(struct scope *o)
7296{7284{
7297 if (!o)7285 if (!o)
7298 return;7286 return;
tccmacho.c+1−1
@@ -517,7 +517,7 @@ static void create_symtab(TCCState *s1, struct macho *mo) · 516 unmodified lines
517 }517 }
518 tcc_enter_state(s1); /* qsort needs global state */518 tcc_enter_state(s1); /* qsort needs global state */
519 qsort(pn, sym_end - 1, sizeof(*pn), machosymcmp);519 qsort(pn, sym_end - 1, sizeof(*pn), machosymcmp);
520 tcc_exit_state();520 tcc_exit_state(s1);
521 mo->e2msym = tcc_malloc(sym_end * sizeof(*mo->e2msym));521 mo->e2msym = tcc_malloc(sym_end * sizeof(*mo->e2msym));
522 mo->e2msym[0] = -1;522 mo->e2msym[0] = -1;
523 for (sym_index = 1; sym_index < sym_end; ++sym_index) {523 for (sym_index = 1; sym_index < sym_end; ++sym_index) {
tccpe.c+49−43
@@ -1543,8 +1543,6 @@ static int pe_add_dllref(TCCState *s1, const char *dllname) · 1542 unmodified lines
1543 return s1->nb_loaded_dlls;1543 return s1->nb_loaded_dlls;
1544}1544}
1545 1545
1546/* ------------------------------------------------------------- */
1547
1548static int read_mem(int fd, unsigned offset, void *buffer, unsigned len)1546static int read_mem(int fd, unsigned offset, void *buffer, unsigned len)
1549{1547{
1550 lseek(fd, offset, SEEK_SET);1548 lseek(fd, offset, SEEK_SET);
@@ -1553,11 +1551,10 @@ static int read_mem(int fd, unsigned offset, void *buffer, unsigned len) · 2 unmodified lines
1553 1551
1554/* ------------------------------------------------------------- */1552/* ------------------------------------------------------------- */
1555 1553
1556PUB_FUNC int tcc_get_dllexports(const char *filename, char **pp)1554static int get_dllexports(int fd, char **pp)
1557{1555{
1558 int l, i, n, n0, ret;1556 int l, i, n, n0, ret;
1559 char *p;1557 char *p;
1560 int fd;
1561 1558
1562 IMAGE_SECTION_HEADER ish;1559 IMAGE_SECTION_HEADER ish;
1563 IMAGE_EXPORT_DIRECTORY ied;1560 IMAGE_EXPORT_DIRECTORY ied;
@@ -1569,11 +1566,6 @@ PUB_FUNC int tcc_get_dllexports(const char *filename, char **pp) · 5 unmodified lines
1569 1566
1570 n = n0 = 0;1567 n = n0 = 0;
1571 p = NULL;1568 p = NULL;
1572 ret = -1;
1573
1574 fd = open(filename, O_RDONLY | O_BINARY);
1575 if (fd < 0)
1576 goto the_end_1;
1577 ret = 1;1569 ret = 1;
1578 if (!read_mem(fd, 0, &dh, sizeof dh))1570 if (!read_mem(fd, 0, &dh, sizeof dh))
1579 goto the_end;1571 goto the_end;
@@ -1640,8 +1632,6 @@ found: · 60 unmodified lines
1640the_end_0:1632the_end_0:
1641 ret = 0;1633 ret = 0;
1642the_end:1634the_end:
1643 close(fd);
1644the_end_1:
1645 *pp = p;1635 *pp = p;
1646 return ret;1636 return ret;
1647}1637}
@@ -1694,47 +1684,53 @@ quit: · 46 unmodified lines
1694 1684
1695static char *trimfront(char *p)1685static char *trimfront(char *p)
1696{1686{
1697 while (*p && (unsigned char)*p <= ' ')1687 while ((unsigned char)*p <= ' ' && *p && *p != '\n')
1698 ++p;1688 ++p;
1699 return p;1689 return p;
1700}1690}
1701 1691
1692/*
1702static char *trimback(char *a, char *e)1693static char *trimback(char *a, char *e)
1703{1694{
1704 while (e > a && (unsigned char)e[-1] <= ' ')1695 while (e > a && (unsigned char)e[-1] <= ' ')
1705 --e;1696 --e;
1706 *e = 0;;1697 *e = 0;;
1707 return a;1698 return a;
1699}*/
1700
1701static char *get_token(char **s, char *f)
1702{
1703 char *p = *s, *e;
1704 p = e = trimfront(p);
1705 while ((unsigned char)*e > ' ')
1706 ++e;
1707 *s = trimfront(e);
1708 *f = **s; *e = 0;
1709 return p;
1708}1710}
1709 1711
1710/* ------------------------------------------------------------- */
1711static int pe_load_def(TCCState *s1, int fd)1712static int pe_load_def(TCCState *s1, int fd)
1712{1713{
1713 int state = 0, ret = -1, dllindex = 0, ord;1714 int state = 0, ret = -1, dllindex = 0, ord;
1714 char line[400], dllname[80], *p, *x;1715 char dllname[80], *buf, *line, *p, *x, next;
1715 FILE *fp;
1716
1717 fp = fdopen(dup(fd), "rb");
1718 while (fgets(line, sizeof line, fp))
1719 {
1720 p = trimfront(trimback(line, strchr(line, 0)));
1721 if (0 == *p || ';' == *p)
1722 continue;
1723 1716
1717 buf = tcc_load_text(fd);
1718 for (line = buf;; ++line) {
1719 p = get_token(&line, &next);
1720 if (!(*p && *p != ';'))
1721 goto skip;
1724 switch (state) {1722 switch (state) {
1725 case 0:1723 case 0:
1726 if (0 != strnicmp(p, "LIBRARY", 7))1724 if (0 != stricmp(p, "LIBRARY") || next == '\n')
1727 goto quit;1725 goto quit;
1728 pstrcpy(dllname, sizeof dllname, trimfront(p+7));1726 pstrcpy(dllname, sizeof dllname, get_token(&line, &next));
1729 ++state;1727 ++state;
1730 continue;1728 break;
1731
1732 case 1:1729 case 1:
1733 if (0 != stricmp(p, "EXPORTS"))1730 if (0 != stricmp(p, "EXPORTS"))
1734 goto quit;1731 goto quit;
1735 ++state;1732 ++state;
1736 continue;1733 break;
1737
1738 case 2:1734 case 2:
1739 dllindex = pe_add_dllref(s1, dllname);1735 dllindex = pe_add_dllref(s1, dllname);
1740 ++state;1736 ++state;
@@ -1742,33 +1738,34 @@ static int pe_load_def(TCCState *s1, int fd) · 1 unmodified line
1742 default:1738 default:
1743 /* get ordinal and will store in sym->st_value */1739 /* get ordinal and will store in sym->st_value */
1744 ord = 0;1740 ord = 0;
1745 x = strchr(p, ' ');1741 if (next == '@') {
1746 if (x) {1742 x = get_token(&line, &next);
1747 *x = 0, x = strrchr(x + 1, '@');1743 ord = (int)strtol(x + 1, &x, 10);
1748 if (x) {
1749 char *d;
1750 ord = (int)strtol(x + 1, &d, 10);
1751 if (*d)
1752 ord = 0;
1753 }
1754 }1744 }
1745 //printf("token %s ; %s : %d\n", dllname, p, ord);
1755 pe_putimport(s1, dllindex, p, ord);1746 pe_putimport(s1, dllindex, p, ord);
1756 continue;1747 break;
1757 }1748 }
1749skip:
1750 while ((unsigned char)next > ' ')
1751 get_token(&line, &next);
1752 if (next != '\n')
1753 break;
1758 }1754 }
1759 ret = 0;1755 ret = 0;
1760quit:1756quit:
1761 fclose(fp);1757 tcc_free(buf);
1762 return ret;1758 return ret;
1763}1759}
1764 1760
1765/* ------------------------------------------------------------- */1761/* ------------------------------------------------------------- */
1766static int pe_load_dll(TCCState *s1, const char *filename)1762
1763static int pe_load_dll(TCCState *s1, int fd, const char *filename)
1767{1764{
1768 char *p, *q;1765 char *p, *q;
1769 int index, ret;1766 int index, ret;
1770 1767
1771 ret = tcc_get_dllexports(filename, &p);1768 ret = get_dllexports(fd, &p);
1772 if (ret) {1769 if (ret) {
1773 return -1;1770 return -1;
1774 } else if (p) {1771 } else if (p) {
@@ -1780,7 +1777,6 @@ static int pe_load_dll(TCCState *s1, const char *filename) · 5 unmodified lines
1780 return 0;1777 return 0;
1781}1778}
1782 1779
1783/* ------------------------------------------------------------- */
1784ST_FUNC int pe_load_file(struct TCCState *s1, int fd, const char *filename)1780ST_FUNC int pe_load_file(struct TCCState *s1, int fd, const char *filename)
1785{1781{
1786 int ret = -1;1782 int ret = -1;
@@ -1790,7 +1786,17 @@ ST_FUNC int pe_load_file(struct TCCState *s1, int fd, const char *filename) · 3 unmodified lines
1790 else if (pe_load_res(s1, fd) == 0)1786 else if (pe_load_res(s1, fd) == 0)
1791 ret = 0;1787 ret = 0;
1792 else if (read_mem(fd, 0, buf, 4) && 0 == memcmp(buf, "MZ", 2))1788 else if (read_mem(fd, 0, buf, 4) && 0 == memcmp(buf, "MZ", 2))
1793 ret = pe_load_dll(s1, filename);1789 ret = pe_load_dll(s1, fd, filename);
1790 return ret;
1791}
1792
1793PUB_FUNC int tcc_get_dllexports(const char *filename, char **pp)
1794{
1795 int ret, fd = open(filename, O_RDONLY | O_BINARY);
1796 if (fd < 0)
1797 return -1;
1798 ret = get_dllexports(fd, pp);
1799 close(fd);
1794 return ret;1800 return ret;
1795}1801}
1796 1802
tccpp.c+11−2
@@ -404,7 +404,7 @@ ST_FUNC void cstr_reset(CString *cstr) · 403 unmodified lines
404 cstr->size = 0;404 cstr->size = 0;
405}405}
406 406
407ST_FUNC int cstr_printf(CString *cstr, const char *fmt, ...)407ST_FUNC int cstr_vprintf(CString *cstr, const char *fmt, va_list ap)
408{408{
409 va_list v;409 va_list v;
410 int len, size = 80;410 int len, size = 80;
@@ -413,7 +413,7 @@ ST_FUNC int cstr_printf(CString *cstr, const char *fmt, ...) · 2 unmodified lines
413 if (size > cstr->size_allocated)413 if (size > cstr->size_allocated)
414 cstr_realloc(cstr, size);414 cstr_realloc(cstr, size);
415 size = cstr->size_allocated - cstr->size;415 size = cstr->size_allocated - cstr->size;
416 va_start(v, fmt);416 va_copy(v, ap);
417 len = vsnprintf((char*)cstr->data + cstr->size, size, fmt, v);417 len = vsnprintf((char*)cstr->data + cstr->size, size, fmt, v);
418 va_end(v);418 va_end(v);
419 if (len > 0 && len < size)419 if (len > 0 && len < size)
@@ -424,6 +424,15 @@ ST_FUNC int cstr_printf(CString *cstr, const char *fmt, ...) · 4 unmodified lines
424 return len;424 return len;
425}425}
426 426
427ST_FUNC int cstr_printf(CString *cstr, const char *fmt, ...)
428{
429 va_list ap; int len;
430 va_start(ap, fmt);
431 len = cstr_vprintf(cstr, fmt, ap);
432 va_end(ap);
433 return len;
434}
435
427/* XXX: unicode ? */436/* XXX: unicode ? */
428static void add_char(CString *cstr, int c)437static void add_char(CString *cstr, int c)
429{438{
tcctok.h+14−2
@@ -1,3 +1,4 @@
1/*********************************************************************/
1/* keywords */2/* keywords */
2 DEF(TOK_INT, "int")3 DEF(TOK_INT, "int")
3 DEF(TOK_VOID, "void")4 DEF(TOK_VOID, "void")
@@ -343,8 +344,17 @@ · 339 unmodified lines
343 DEF(TOK_longjmp, "longjmp")344 DEF(TOK_longjmp, "longjmp")
344#endif345#endif
345 346
347
348/*********************************************************************/
346/* Tiny Assembler */349/* Tiny Assembler */
347 DEF_ASMDIR(byte) /* must be first directive */350#define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
351#define DEF_ASMDIR(x) DEF(TOK_ASMDIR_ ## x, "." #x)
352#define TOK_ASM_int TOK_INT
353
354#define TOK_ASMDIR_FIRST TOK_ASMDIR_byte
355#define TOK_ASMDIR_LAST TOK_ASMDIR_section
356
357 DEF_ASMDIR(byte) /* must be first directive */
348 DEF_ASMDIR(word)358 DEF_ASMDIR(word)
349 DEF_ASMDIR(align)359 DEF_ASMDIR(align)
350 DEF_ASMDIR(balign)360 DEF_ASMDIR(balign)
@@ -383,14 +393,16 @@ · 32 unmodified lines
383 DEF_ASMDIR(short)393 DEF_ASMDIR(short)
384 DEF_ASMDIR(long)394 DEF_ASMDIR(long)
385 DEF_ASMDIR(int)395 DEF_ASMDIR(int)
386 DEF_ASMDIR(section) /* must be last directive */396 DEF_ASMDIR(section) /* must be last directive */
387 397
388#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64398#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
389#include "i386-tok.h"399#include "i386-tok.h"
390#endif400#endif
401
391#if defined TCC_TARGET_ARM || defined TCC_TARGET_ARM64402#if defined TCC_TARGET_ARM || defined TCC_TARGET_ARM64
392#include "arm-tok.h"403#include "arm-tok.h"
393#endif404#endif
405
394#if defined TCC_TARGET_RISCV64406#if defined TCC_TARGET_RISCV64
395#include "riscv64-tok.h"407#include "riscv64-tok.h"
396#endif408#endif
tcctools.c+2−4
@@ -385,10 +385,8 @@ usage: · 384 unmodified lines
385 ret = 0;385 ret = 0;
386 386
387the_end:387the_end:
388 /* cannot free memory received from tcc_get_dllexports388 if (p)
389 if it came from a dll */389 tcc_free(p);
390 /* if (p)
391 tcc_free(p); */
392 if (fp)390 if (fp)
393 fclose(fp);391 fclose(fp);
394 if (op)392 if (op)
tests/libtcc_test_mt.c+5−4
@@ -220,9 +220,10 @@ TF_TYPE(thread_test_complex, vn) · 219 unmodified lines
220void time_tcc(int n, const char *src)220void time_tcc(int n, const char *src)
221{221{
222 TCCState *s;222 TCCState *s;
223 int ret;223 int ret, i = 0;
224 while (--n >= 0) {224 while (i++ < n) {
225 s = new_state(1);225 s = new_state(1);
226 printf(" %d", i), fflush(stdout);
226 ret = tcc_add_file(s, src);227 ret = tcc_add_file(s, src);
227 tcc_delete(s);228 tcc_delete(s);
228 if (ret < 0)229 if (ret < 0)
@@ -277,10 +278,10 @@ int main(int argc, char **argv) · 48 unmodified lines
277 printf("\n (%u ms)\n", getclock_ms() - t);278 printf("\n (%u ms)\n", getclock_ms() - t);
278#endif279#endif
279#if 1280#if 1
280 printf("compiling tcc.c 10 times\n"), fflush(stdout);281 printf("compiling tcc.c 10 times\n "), fflush(stdout);
281 t = getclock_ms();282 t = getclock_ms();
282 time_tcc(10, argv[1]);283 time_tcc(10, argv[1]);
283 printf(" (%u ms)\n", getclock_ms() - t), fflush(stdout);284 printf("\n (%u ms)\n", getclock_ms() - t), fflush(stdout);
284#endif285#endif
285 return 0;286 return 0;
286}287}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t