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
134134
135135 # cross compiler targets to build
136136 TCC_X = i386 x86_64 i386-win32 x86_64-win32 x86_64-osx arm arm64 arm-wince c67
137−TCC_X += riscv64
137+TCC_X += riscv64 arm64-osx
138138 # TCC_X += arm-fpa arm-fpa-ld arm-vfp arm-eabi
139139
140140 # cross libtcc1.a targets to build
@@ -222,10 +222,9 @@ $(TCC_FILES) : DEFINES += -DONE_SOURCE=0 · 81 unmodified lines
222222 $(X)tccpp.o : $(TCCDEFS_H)
223223 endif
224224
225−TCC_GIT_HASH=$(shell git rev-parse > /dev/null 2>&1 && git rev-parse --short HEAD || echo no)
226−ifneq ($(TCC_GIT_HASH),no)
227−MODIFIED = $(shell git diff | grep -q +++ && echo "modified ")
228−$(X)tcc.o : DEFINES += -DTCC_GIT_HASH="\"$(MODIFIED)$(TCC_GIT_HASH)\""
225+GITHASH := $(shell git rev-parse >/dev/null 2>&1 && git rev-parse --short HEAD || echo no)
226+ifneq ($(GITHASH),no)
227+DEF_GITHASH := -DTCC_GITHASH="\"$(GITHASH)$(shell git diff --quiet || echo '-mod')\""
229228 endif
230229
231230 ifeq ($(CONFIG_debug),yes)
@@ -247,6 +246,7 @@ $(X)%.o : %.c $(LIBTCC_INC) · 15 unmodified lines
247246
248247 # additional dependencies
249248 $(X)tcc.o : tcctools.c
249+$(X)tcc.o : DEFINES += $(DEF_GITHASH)
250250
251251 # Host Tiny C Compiler
252252 tcc$(EXESUF): tcc.o $(LIBTCC)
configure+2−2
@@ -238,7 +238,7 @@ if test "$mingw32" = "yes" ; then · 237 unmodified lines
238238 source_path="."
239239 fi
240240 test -z "$prefix" && prefix="C:/Program Files/tcc"
241− test -z "$tccdir" && tccdir="${prefix}"
241+ test -z "$tccdir" && tccdir="${prefix}" && tccdir_auto="yes"
242242 test -z "$bindir" && bindir="${tccdir}"
243243 test -z "$docdir" && docdir="${tccdir}/doc"
244244 test -z "$libdir" && libdir="${tccdir}/libtcc"
@@ -483,7 +483,7 @@ print_mak() { · 238 unmodified lines
483483 echo "/* Automatically generated by configure - do not modify */" > $TMPH
484484
485485 print_inc CONFIG_SYSROOT "$sysroot"
486−print_inc CONFIG_TCCDIR "$tccdir"
486+test "$tccdir_auto" = "yes" || print_inc CONFIG_TCCDIR "$tccdir"
487487 print_mak CONFIG_USR_INCLUDE "$tcc_usrinclude"
488488 print_mak CONFIG_TCC_SYSINCLUDEPATHS "$tcc_sysincludepaths"
489489 print_mak CONFIG_TCC_LIBPATHS "$tcc_libpaths"
i386-tok.h+76−0
@@ -1,6 +1,82 @@
11 /* ------------------------------------------------------------------ */
22 /* WARNING: relative order of tokens is important. */
33
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+/* ------------------------------------------------------------------ */
480 /* register */
581 DEF_ASM(al)
682 DEF_ASM(cl)
libtcc.c+81−98
@@ -21,6 +21,7 @@ · 20 unmodified lines
2121 #if !defined ONE_SOURCE || ONE_SOURCE
2222 #include "tccpp.c"
2323 #include "tccgen.c"
24+#include "tccasm.c"
2425 #include "tccelf.c"
2526 #include "tccrun.c"
2627 #ifdef TCC_TARGET_I386
@@ -50,9 +51,6 @@ · 23 unmodified lines
5051 #else
5152 #error unknown target
5253 #endif
53−#ifdef CONFIG_TCC_ASM
54−#include "tccasm.c"
55−#endif
5654 #ifdef TCC_TARGET_PE
5755 #include "tccpe.c"
5856 #endif
@@ -68,6 +66,7 @@ · 9 unmodified lines
6866
6967 /* XXX: get rid of this ASAP (or maybe not) */
7068 ST_DATA struct TCCState *tcc_state;
69+TCC_SEM(static tcc_compile_sem);
7170
7271 #ifdef MEM_DEBUG
7372 static int nb_states;
@@ -84,19 +83,23 @@ ST_FUNC char *normalize_slashes(char *path) · 10 unmodified lines
8483 return path;
8584 }
8685
86+/* NULL if this is tcc.exe, HINSTANCE if this is libtcc.dll */
8787 static HMODULE tcc_module;
8888
89+#ifndef CONFIG_TCCDIR
8990 /* on win32, we suppose the lib and includes are at the location of 'tcc.exe' */
90−static void tcc_set_lib_path_w32(TCCState *s)
91+static inline char *config_tccdir_w32(char *path)
9192 {
92− char path[1024], *p;
93− GetModuleFileNameA(tcc_module, path, sizeof path);
93+ char *p;
94+ GetModuleFileName(tcc_module, path, MAX_PATH);
9495 p = tcc_basename(normalize_slashes(strlwr(path)));
9596 if (p > path)
9697 --p;
9798 *p = 0;
98− tcc_set_lib_path(s, path);
99+ return path;
99100 }
101+#define CONFIG_TCCDIR config_tccdir_w32(alloca(MAX_PATH))
102+#endif
100103
101104 #ifdef TCC_TARGET_PE
102105 static void tcc_add_systemdir(TCCState *s)
@@ -118,47 +121,60 @@ BOOL WINAPI DllMain (HINSTANCE hDll, DWORD dwReason, LPVOID lpReserved) · 15 unmodified lines
118121 #endif
119122
120123 /********************************************************/
121−#if CONFIG_TCC_SEMLOCK == 0
122−#define WAIT_SEM()
123−#define POST_SEM()
124−#elif defined _WIN32
125−static int tcc_sem_init;
126−static CRITICAL_SECTION tcc_cr;
127−static void wait_sem(void)
128−{
129− if (!tcc_sem_init)
130− InitializeCriticalSection(&tcc_cr), tcc_sem_init = 1;
131− EnterCriticalSection(&tcc_cr);
132−}
133−#define WAIT_SEM() wait_sem()
134−#define POST_SEM() LeaveCriticalSection(&tcc_cr);
124+#if CONFIG_TCC_SEMLOCK
125+#if defined _WIN32
126+ST_FUNC void wait_sem(TCCSem *p)
127+{
128+ if (!p->init)
129+ InitializeCriticalSection(&p->cr), p->init = 1;
130+ EnterCriticalSection(&p->cr);
131+}
132+ST_FUNC void post_sem(TCCSem *p)
133+{
134+ LeaveCriticalSection(&p->cr);
135+}
135136 #elif defined __APPLE__
136137 /* Half-compatible MacOS doesn't have non-shared (process local)
137138 semaphores. Use the dispatch framework for lightweight locks. */
138−#include <dispatch/dispatch.h>
139−static int tcc_sem_init;
140−static dispatch_semaphore_t tcc_sem;
141−static void wait_sem(void)
139+ST_FUNC void wait_sem(TCCSem *p)
142140 {
143− if (!tcc_sem_init)
144− tcc_sem = dispatch_semaphore_create(1), tcc_sem_init = 1;
145− dispatch_semaphore_wait(tcc_sem, DISPATCH_TIME_FOREVER);
141+ if (!p->init)
142+ p->sem = dispatch_semaphore_create(1), p->init = 1;
143+ dispatch_semaphore_wait(p->sem, DISPATCH_TIME_FOREVER);
144+}
145+ST_FUNC void post_sem(TCCSem *p)
146+{
147+ dispatch_semaphore_signal(p->sem);
146148 }
147−#define WAIT_SEM() wait_sem()
148−#define POST_SEM() dispatch_semaphore_signal(tcc_sem)
149149 #else
150−#include <semaphore.h>
151−static int tcc_sem_init;
152−static sem_t tcc_sem;
153−static void wait_sem(void)
150+ST_FUNC void wait_sem(TCCSem *p)
151+{
152+ if (!p->init)
153+ sem_init(&p->sem, 0, 1), p->init = 1;
154+ while (sem_wait(&p->sem) < 0 && errno == EINTR);
155+}
156+ST_FUNC void post_sem(TCCSem *p)
154157 {
155− if (!tcc_sem_init)
156− sem_init(&tcc_sem, 0, 1), tcc_sem_init = 1;
157− while (sem_wait (&tcc_sem) < 0 && errno == EINTR);
158+ sem_post(&p->sem);
158159 }
159−#define WAIT_SEM() wait_sem()
160−#define POST_SEM() sem_post(&tcc_sem)
161160 #endif
161+#endif
162+
163+PUB_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+
171+PUB_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+}
162178
163179 /********************************************************/
164180 /* 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
494510 }
495511
496512 /********************************************************/
513+/* warning / error */
497514
498−static void strcat_vprintf(char *buf, int buf_size, const char *fmt, va_list ap)
499−{
500− int len;
501− len = strlen(buf);
502− vsnprintf(buf + len, buf_size - len, fmt, ap);
503−}
504−
505−static 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−}
515+/* warn_... option bits */
516+#define WARN_ON 1 /* warning is on (-Woption) */
517+#define WARN_ERR 2 /* warning is an error (-Werror=option) */
518+#define WARN_NOE 4 /* warning is not an error (-Wno-error=option) */
512519
513−PUB_FUNC void tcc_enter_state(TCCState *s1)
514−{
515− WAIT_SEM();
516− tcc_state = s1;
517−}
518−
519−PUB_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
520+/* error1() modes */
521+enum { ERROR_WARN, ERROR_NOABORT, ERROR_ERROR };
528522
529523 static void error1(int mode, const char *fmt, va_list ap)
530524 {
531− char buf[2048];
532525 BufferedFile **pf, *f;
533526 TCCState *s1 = tcc_state;
527+ CString cs;
528+
529+ cstr_new(&cs);
534530
535− buf[0] = '\0';
536531 if (s1 == NULL)
537532 /* can happen only if called from tcc_malloc(): 'out of memory' */
538533 goto no_file;
539534
540− if (!s1->error_set_jmp_enabled)
541− /* tcc_state just was set by tcc_enter_state() */
542− tcc_exit_state();
535+ tcc_exit_state(s1);
543536
544537 if (mode == ERROR_WARN) {
545538 if (s1->warn_error)
@@ -567,33 +560,30 @@ static void error1(int mode, const char *fmt, va_list ap) · 21 unmodified lines
567560 }
568561 if (f) {
569562 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",
571564 (*pf)->filename, (*pf)->line_num);
572− strcat_printf(buf, sizeof(buf), "%s:%d: ",
565+ cstr_printf(&cs, "%s:%d: ",
573566 f->filename, f->line_num - !!(tok_flags & TOK_FLAG_BOL));
574567 } else if (s1->current_filename) {
575− strcat_printf(buf, sizeof(buf), "%s: ", s1->current_filename);
568+ cstr_printf(&cs, "%s: ", s1->current_filename);
576569 }
577570
578571 no_file:
579− if (0 == buf[0])
580− strcat_printf(buf, sizeof(buf), "tcc: ");
581− if (mode == ERROR_WARN)
582− strcat_printf(buf, sizeof(buf), "warning: ");
583− else
584− strcat_printf(buf, sizeof(buf), "error: ");
585− strcat_vprintf(buf, sizeof(buf), fmt, ap);
572+ if (0 == cs.size)
573+ cstr_printf(&cs, "tcc: ");
574+ cstr_printf(&cs, mode == ERROR_WARN ? "warning: " : "error: ");
575+ cstr_vprintf(&cs, fmt, ap);
586576 if (!s1 || !s1->error_func) {
587577 /* default case: stderr */
588578 if (s1 && s1->output_type == TCC_OUTPUT_PREPROCESS && s1->ppfp == stdout)
589− /* print a newline during tcc -E */
590− printf("\n"), fflush(stdout);
579+ printf("\n"); /* print a newline during tcc -E */
591580 fflush(stdout); /* flush -v output */
592− fprintf(stderr, "%s\n", buf);
581+ fprintf(stderr, "%s\n", (char*)cs.data);
593582 fflush(stderr); /* print error/warning now (win32) */
594583 } else {
595− s1->error_func(s1->error_opaque, buf);
584+ s1->error_func(s1->error_opaque, (char*)cs.data);
596585 }
586+ cstr_free(&cs);
597587 if (s1) {
598588 if (mode != ERROR_WARN)
599589 s1->nb_errors++;
@@ -718,9 +708,9 @@ static int tcc_compile(TCCState *s1, int filetype, const char *str, int fd) · 118 unmodified lines
718708 variables, which may or may not have advantages */
719709
720710 tcc_enter_state(s1);
711+ s1->error_set_jmp_enabled = 1;
721712
722713 if (setjmp(s1->error_jmp_buf) == 0) {
723− s1->error_set_jmp_enabled = 1;
724714 s1->nb_errors = 0;
725715
726716 if (fd == -1) {
@@ -738,19 +728,16 @@ static int tcc_compile(TCCState *s1, int filetype, const char *str, int fd) · 11 unmodified lines
738728 if (s1->output_type == TCC_OUTPUT_PREPROCESS) {
739729 tcc_preprocess(s1);
740730 } else if (filetype & (AFF_TYPE_ASM | AFF_TYPE_ASMPP)) {
741−#ifdef CONFIG_TCC_ASM
742731 tcc_assemble(s1, !!(filetype & AFF_TYPE_ASMPP));
743−#else
744− tcc_error_noabort("asm not supported");
745−#endif
746732 } else {
747733 tccgen_compile(s1);
748734 }
749735 }
750− s1->error_set_jmp_enabled = 0;
751736 tccgen_finish(s1);
752737 preprocess_end(s1);
753− tcc_exit_state();
738+
739+ s1->error_set_jmp_enabled = 0;
740+ tcc_exit_state(s1);
754741
755742 tccelf_end_file(s1);
756743 return s1->nb_errors != 0 ? -1 : 0;
@@ -821,11 +808,7 @@ LIBTCCAPI TCCState *tcc_new(void) · 64 unmodified lines
821808
822809 tccelf_new(s);
823810
824−#ifdef _WIN32
825− tcc_set_lib_path_w32(s);
826−#else
827811 tcc_set_lib_path(s, CONFIG_TCCDIR);
828−#endif
829812 return s;
830813 }
831814
tcc.c+10−7
@@ -154,10 +154,8 @@ static const char help2[] = · 153 unmodified lines
154154
155155 static const char version[] =
156156 "tcc version "TCC_VERSION
157−#ifdef TCC_GIT_HASH
158− " - " TCC_GIT_HASH
159−#else
160− " - unknown hash"
157+#ifdef TCC_GITHASH
158+ " "TCC_GITHASH
161159 #endif
162160 " ("
163161 #ifdef TCC_TARGET_I386
@@ -276,7 +274,7 @@ int main(int argc0, char **argv0) · 112 unmodified lines
276274 {
277275 TCCState *s, *s1;
278276 int ret, opt, n = 0, t = 0, done;
279− unsigned start_time = 0;
277+ unsigned start_time = 0, end_time = 0;
280278 const char *first_file;
281279 int argc; char **argv;
282280 FILE *ppfp = stdout;
@@ -372,6 +370,9 @@ redo: · 89 unmodified lines
372370 done = ret || ++n >= s->nb_files;
373371 } while (!done && (s->output_type != TCC_OUTPUT_OBJ || s->option_r));
374372
373+ if (s->do_bench)
374+ end_time = getclock_ms();
375+
375376 if (s->run_test) {
376377 t = 0;
377378 } else if (s->output_type == TCC_OUTPUT_PREPROCESS) {
@@ -391,13 +392,15 @@ redo: · 13 unmodified lines
391392 }
392393 }
393394
394− if (s->do_bench && done && !(t | ret))
395− tcc_print_stats(s, getclock_ms() - start_time);
395+ if (done && 0 == t && 0 == ret && s->do_bench)
396+ tcc_print_stats(s, end_time - start_time);
397+
396398 tcc_delete(s);
397399 if (!done)
398400 goto redo; /* compile more files with -c */
399401 if (t)
400402 goto redo; /* run more tests with -dt -run */
403+
401404 if (ppfp && ppfp != stdout)
402405 fclose(ppfp);
403406 return ret;
tcc.h+78−132
@@ -55,6 +55,7 @@ extern long double strtold (const char *__nptr, char **__endptr); · 54 unmodified lines
5555 # include <windows.h>
5656 # include <io.h> /* open, close etc. */
5757 # include <direct.h> /* getcwd */
58+# include <malloc.h> /* alloca */
5859 # ifdef __GNUC__
5960 # include <stdint.h>
6061 # endif
@@ -91,6 +92,9 @@ extern long double strtold (const char *__nptr, char **__endptr); · 30 unmodified lines
9192 # define __x86_64__ 1
9293 # endif
9394 # endif
95+# ifndef va_copy
96+# define va_copy(a,b) a = b
97+# endif
9498 # undef CONFIG_TCC_STATIC
9599 #endif
96100
@@ -124,7 +128,6 @@ extern long double strtold (const char *__nptr, char **__endptr); · 27 unmodified lines
124128 # define PRINTF_LIKE(x,y) __attribute__ ((format (printf, (x), (y))))
125129 #endif
126130
127−
128131 #ifdef _WIN32
129132 # define IS_DIRSEP(c) (c == '/' || c == '\\')
130133 # 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
179182 # ifdef _WIN32
180183 # define TCC_TARGET_PE 1
181184 # endif
185+# ifdef __APPLE__
186+# define TCC_TARGET_MACHO 1
187+# endif
182188 #endif
183189
184190 /* only native compiler supports -run */
@@ -226,7 +232,7 @@ extern long double strtold (const char *__nptr, char **__endptr); · 41 unmodified lines
226232 /* No ten-byte long doubles on window and macos except in
227233 cross-compilers made by a mingw-GCC */
228234 #if defined TCC_TARGET_PE \
229− || (defined TCC_TARGET_MACHO && defined TCC_TARGET_X86_64) \
235+ || (defined TCC_TARGET_MACHO && defined TCC_TARGET_ARM64) \
230236 || (defined _WIN32 && !defined __GNUC__)
231237 # define TCC_USING_DOUBLE_FOR_LDOUBLE 1
232238 #endif
@@ -236,7 +242,7 @@ extern long double strtold (const char *__nptr, char **__endptr); · 3 unmodified lines
236242 #ifndef CONFIG_SYSROOT
237243 # define CONFIG_SYSROOT ""
238244 #endif
239−#ifndef CONFIG_TCCDIR
245+#if !defined CONFIG_TCCDIR && !defined _WIN32
240246 # define CONFIG_TCCDIR "/usr/local/lib/tcc"
241247 #endif
242248 #ifndef CONFIG_LDDIR
@@ -751,15 +757,6 @@ struct TCCState { · 508 unmodified lines
751757 unsigned char enable_new_dtags; /* -Wl,--enable-new-dtags */
752758 unsigned int cversion; /* supported C ISO version, 199901 (the default), 201112, ... */
753759
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−
763760 /* C language options */
764761 unsigned char char_is_unsigned;
765762 unsigned char leading_underscore;
@@ -776,10 +773,14 @@ struct TCCState { · 10 unmodified lines
776773 unsigned char warn_implicit_function_declaration;
777774 unsigned char warn_discarded_qualifiers;
778775 #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) */
781776 unsigned char warn_num; /* temp var for tcc_warning_c() */
782777
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+
783784 /* compile with debug symbol (and use them if error during execution) */
784785 unsigned char do_debug;
785786 unsigned char do_backtrace;
@@ -789,30 +790,41 @@ struct TCCState { · 3 unmodified lines
789790 #endif
790791 unsigned char test_coverage; /* generate test coverage code */
791792
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
792804 #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*/
794806 #endif
795− int run_test; /* nth test to run with -dt -run */
796807
797− addr_t text_addr; /* address of text section */
798808 unsigned char has_text_addr;
799−
809+ addr_t text_addr; /* address of text section */
800810 unsigned section_align; /* section alignment */
811+#ifdef TCC_TARGET_I386
812+ int seg_size; /* 32. Can be 16 with i386 assembler (.code16) */
813+#endif
801814
802− /* use GNU C extensions */
803− unsigned char gnu_ext;
804− /* use TinyCC extensions */
805− unsigned char tcc_ext;
815+ char *tcc_lib_path; /* CONFIG_TCCDIR or -B option */
816+ char *soname; /* as specified on the command line (-soname) */
817+ char *rpath; /* as specified on the command line (-Wl,-rpath=) */
806818
807819 char *init_symbol; /* symbols to call at load-time (not used currently) */
808820 char *fini_symbol; /* symbols to call at unload-time (not used currently) */
809821
810−#ifdef TCC_TARGET_I386
811− int seg_size; /* 32. Can be 16 with i386 assembler (.code16) */
812−#endif
813−#ifdef TCC_TARGET_X86_64
814− unsigned char nosse; /* For -mno-sse support. */
815−#endif
822+ /* output type, see TCC_OUTPUT_XXX */
823+ int output_type;
824+ /* output format, see TCC_OUTPUT_FORMAT_xxx */
825+ int output_format;
826+ /* nth test to run with -dt -run */
827+ int run_test;
816828
817829 /* array of all loaded dlls (including those referenced by loaded dlls) */
818830 DLLReference **loaded_dlls;
@@ -847,13 +859,6 @@ struct TCCState { · 28 unmodified lines
847859
848860 /* output file for preprocessing (-E) */
849861 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 */
857862
858863 /* for -MD/-MF: collected dependencies for this compilation */
859864 char **target_deps;
@@ -922,11 +927,11 @@ struct TCCState { · 62 unmodified lines
922927 int nb_sym_attrs;
923928 /* ptr to next reloc entry reused */
924929 ElfW_Rel *qrel;
925−# define qrel s1->qrel
930+ #define qrel s1->qrel
926931
927932 #ifdef TCC_TARGET_RISCV64
928933 struct pcrel_hi { addr_t addr, val; } last_hi;
929−# define last_hi s1->last_hi
934+ #define last_hi s1->last_hi
930935 #endif
931936
932937 #ifdef TCC_TARGET_PE
@@ -963,8 +968,6 @@ struct TCCState { · 30 unmodified lines
963968 int rt_num_callers;
964969 #endif
965970
966− int fd, cc; /* used by tcc_load_ldscript */
967−
968971 /* benchmark info */
969972 int total_idents;
970973 int total_lines;
@@ -974,7 +977,10 @@ struct TCCState { · 3 unmodified lines
974977 /* option -dnum (for general development purposes) */
975978 int g_debug;
976979
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 */
978984 const char *current_filename;
979985
980986 /* used by main and tcc_parse_args only */
@@ -982,11 +988,6 @@ struct TCCState { · 1 unmodified line
982988 int nb_files; /* number thereof */
983989 int nb_libraries; /* number of libs thereof */
984990 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 */
990991 char *deps_outfile; /* option -MF */
991992 int argc;
992993 char **argv;
@@ -1161,91 +1162,6 @@ struct filespec { · 168 unmodified lines
11611162 /* all identifiers and strings have token above that */
11621163 #define TOK_IDENT 256
11631164
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−
12491165 enum tcc_token {
12501166 TOK_LAST = TOK_IDENT - 1
12511167 #define DEF(id, str) ,id
@@ -1306,6 +1222,7 @@ ST_FUNC void cstr_wccat(CString *cstr, int ch); · 54 unmodified lines
13061222 ST_FUNC void cstr_new(CString *cstr);
13071223 ST_FUNC void cstr_free(CString *cstr);
13081224 ST_FUNC int cstr_printf(CString *cs, const char *fmt, ...) PRINTF_LIKE(2,3);
1225+ST_FUNC int cstr_vprintf(CString *cstr, const char *fmt, va_list ap);
13091226 ST_FUNC void cstr_reset(CString *cstr);
13101227
13111228 ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen);
@@ -1397,6 +1314,13 @@ ST_DATA TokenSym **table_ident; · 85 unmodified lines
13971314 #define IS_ID 2
13981315 #define IS_NUM 4
13991316
1317+enum 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+
14001324 ST_FUNC TokenSym *tok_alloc(const char *str, int len);
14011325 ST_FUNC int tok_alloc_const(const char *str);
14021326 ST_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
17701694 /* ------------ tccasm.c ------------ */
17711695 ST_FUNC void asm_instr(void);
17721696 ST_FUNC void asm_global_instr(void);
1697+ST_FUNC int tcc_assemble(TCCState *s1, int do_preprocess);
17731698 #ifdef CONFIG_TCC_ASM
17741699 ST_FUNC int find_constraint(ASMOperand *operands, int nb_operands, const char *name, const char **pp);
17751700 ST_FUNC Sym* get_asm_sym(int name, Sym *csym);
17761701 ST_FUNC void asm_expr(TCCState *s1, ExprValue *pe);
17771702 ST_FUNC int asm_int_expr(TCCState *s1);
1778−ST_FUNC int tcc_assemble(TCCState *s1, int do_preprocess);
17791703 /* ------------ i386-asm.c ------------ */
17801704 ST_FUNC void gen_expr32(ExprValue *pe);
17811705 #ifdef TCC_TARGET_X86_64
@@ -1844,6 +1768,28 @@ ST_FUNC void tcc_tool_cross(TCCState *s, char **argv, int option); · 62 unmodified lines
18441768 ST_FUNC void gen_makedeps(TCCState *s, const char *target, const char *filename);
18451769 #endif
18461770
1771+/********************************************************/
1772+#if CONFIG_TCC_SEMLOCK
1773+#if defined _WIN32
1774+typedef struct { int init; CRITICAL_SECTION cr; } TCCSem;
1775+#elif defined __APPLE__
1776+#include <dispatch/dispatch.h>
1777+typedef struct { int init; dispatch_semaphore_t sem; } TCCSem;
1778+#else
1779+#include <semaphore.h>
1780+typedef struct { int init; sem_t sem; } TCCSem;
1781+#endif
1782+ST_FUNC void wait_sem(TCCSem *p);
1783+ST_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+
18471793 /********************************************************/
18481794 #undef ST_DATA
18491795 #if ONE_SOURCE
@@ -1875,7 +1821,7 @@ ST_FUNC void gen_makedeps(TCCState *s, const char *target, const char *filename) · 25 unmodified lines
18751821 #define total_bytes TCC_STATE_VAR(total_bytes)
18761822
18771823 PUB_FUNC void tcc_enter_state(TCCState *s1);
1878−PUB_FUNC void tcc_exit_state(void);
1824+PUB_FUNC void tcc_exit_state(TCCState *s1);
18791825
18801826 /* conditional warning depending on switch */
18811827 #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
12981298 cstr_free(&astr);
12991299 nocode_wanted = saved_nocode_wanted;
13001300 }
1301+
1302+/********************************************************/
1303+#else
1304+ST_FUNC int tcc_assemble(TCCState *s1, int do_preprocess)
1305+{
1306+ tcc_error("asm not supported");
1307+}
1308+
1309+ST_FUNC void asm_instr(void)
1310+{
1311+ tcc_error("inline asm() not supported");
1312+}
1313+
1314+ST_FUNC void asm_global_instr(void)
1315+{
1316+ tcc_error("inline asm() not supported");
1317+}
13011318 #endif /* CONFIG_TCC_ASM */
tccgen.c+11−23
@@ -97,7 +97,7 @@ static CString initstr; · 96 unmodified lines
9797 #define VT_PTRDIFF_T (VT_LONG | VT_LLONG)
9898 #endif
9999
100−ST_DATA struct switch_t {
100+static struct switch_t {
101101 struct case_t {
102102 int64_t v1, v2;
103103 int sym;
@@ -111,12 +111,12 @@ ST_DATA struct switch_t { · 7 unmodified lines
111111
112112 #define MAX_TEMP_LOCAL_VARIABLE_NUMBER 8
113113 /*list of temporary local variables on the stack in current function. */
114−ST_DATA struct temp_local_variable {
114+static struct temp_local_variable {
115115 int location; //offset on stack. Svalue.c.i
116116 short size;
117117 short align;
118118 } arr_temp_local_vars[MAX_TEMP_LOCAL_VARIABLE_NUMBER];
119−short nb_temp_local_vars;
119+static int nb_temp_local_vars;
120120
121121 static struct scope {
122122 struct scope *prev;
@@ -132,6 +132,11 @@ typedef struct { · 9 unmodified lines
132132 Sym *flex_array_ref;
133133 } init_params;
134134
135+#if 1
136+#define precedence_parser
137+static void init_prec(void);
138+#endif
139+
135140 /********************************************************/
136141 /* stab debug support */
137142
@@ -215,23 +220,6 @@ static struct { · 77 unmodified lines
215220 } tcov_data;
216221
217222 /********************************************************/
218−#if 1
219−#define precedence_parser
220−static void init_prec(void);
221−#endif
222−/********************************************************/
223−#ifndef CONFIG_TCC_ASM
224−ST_FUNC void asm_instr(void)
225−{
226− tcc_error("inline asm() not supported");
227−}
228−ST_FUNC void asm_global_instr(void)
229−{
230− tcc_error("inline asm() not supported");
231−}
232−#endif
233−
234−/* ------------------------------------------------------------------------- */
235223 static void gen_cast(CType *type);
236224 static void gen_cast_s(int t);
237225 static inline CType *pointed_type(CType *type);
@@ -7247,7 +7235,7 @@ static void vla_leave(struct scope *o) · 7009 unmodified lines
72477235 /* ------------------------------------------------------------------------- */
72487236 /* local scopes */
72497237
7250−void new_scope(struct scope *o)
7238+static void new_scope(struct scope *o)
72517239 {
72527240 /* copy and link previous scope */
72537241 *o = *cur_scope;
@@ -7264,7 +7252,7 @@ void new_scope(struct scope *o) · 10 unmodified lines
72647252 tcc_debug_stabn(tcc_state, N_LBRAC, ind - func_ind);
72657253 }
72667254
7267−void prev_scope(struct scope *o, int is_expr)
7255+static void prev_scope(struct scope *o, int is_expr)
72687256 {
72697257 vla_leave(o->prev);
72707258
@@ -7292,7 +7280,7 @@ void prev_scope(struct scope *o, int is_expr) · 21 unmodified lines
72927280 }
72937281
72947282 /* leave a scope via break/continue(/goto) */
7295−void leave_scope(struct scope *o)
7283+static void leave_scope(struct scope *o)
72967284 {
72977285 if (!o)
72987286 return;
tccmacho.c+1−1
@@ -517,7 +517,7 @@ static void create_symtab(TCCState *s1, struct macho *mo) · 516 unmodified lines
517517 }
518518 tcc_enter_state(s1); /* qsort needs global state */
519519 qsort(pn, sym_end - 1, sizeof(*pn), machosymcmp);
520− tcc_exit_state();
520+ tcc_exit_state(s1);
521521 mo->e2msym = tcc_malloc(sym_end * sizeof(*mo->e2msym));
522522 mo->e2msym[0] = -1;
523523 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
15431543 return s1->nb_loaded_dlls;
15441544 }
15451545
1546−/* ------------------------------------------------------------- */
1547−
15481546 static int read_mem(int fd, unsigned offset, void *buffer, unsigned len)
15491547 {
15501548 lseek(fd, offset, SEEK_SET);
@@ -1553,11 +1551,10 @@ static int read_mem(int fd, unsigned offset, void *buffer, unsigned len) · 2 unmodified lines
15531551
15541552 /* ------------------------------------------------------------- */
15551553
1556−PUB_FUNC int tcc_get_dllexports(const char *filename, char **pp)
1554+static int get_dllexports(int fd, char **pp)
15571555 {
15581556 int l, i, n, n0, ret;
15591557 char *p;
1560− int fd;
15611558
15621559 IMAGE_SECTION_HEADER ish;
15631560 IMAGE_EXPORT_DIRECTORY ied;
@@ -1569,11 +1566,6 @@ PUB_FUNC int tcc_get_dllexports(const char *filename, char **pp) · 5 unmodified lines
15691566
15701567 n = n0 = 0;
15711568 p = NULL;
1572− ret = -1;
1573−
1574− fd = open(filename, O_RDONLY | O_BINARY);
1575− if (fd < 0)
1576− goto the_end_1;
15771569 ret = 1;
15781570 if (!read_mem(fd, 0, &dh, sizeof dh))
15791571 goto the_end;
@@ -1640,8 +1632,6 @@ found: · 60 unmodified lines
16401632 the_end_0:
16411633 ret = 0;
16421634 the_end:
1643− close(fd);
1644−the_end_1:
16451635 *pp = p;
16461636 return ret;
16471637 }
@@ -1694,47 +1684,53 @@ quit: · 46 unmodified lines
16941684
16951685 static char *trimfront(char *p)
16961686 {
1697− while (*p && (unsigned char)*p <= ' ')
1687+ while ((unsigned char)*p <= ' ' && *p && *p != '\n')
16981688 ++p;
16991689 return p;
17001690 }
17011691
1692+/*
17021693 static char *trimback(char *a, char *e)
17031694 {
17041695 while (e > a && (unsigned char)e[-1] <= ' ')
17051696 --e;
17061697 *e = 0;;
17071698 return a;
1699+}*/
1700+
1701+static 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;
17081710 }
17091711
1710−/* ------------------------------------------------------------- */
17111712 static int pe_load_def(TCCState *s1, int fd)
17121713 {
17131714 int state = 0, ret = -1, dllindex = 0, ord;
1714− char line[400], dllname[80], *p, *x;
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;
1715+ char dllname[80], *buf, *line, *p, *x, next;
17231716
1717+ buf = tcc_load_text(fd);
1718+ for (line = buf;; ++line) {
1719+ p = get_token(&line, &next);
1720+ if (!(*p && *p != ';'))
1721+ goto skip;
17241722 switch (state) {
17251723 case 0:
1726− if (0 != strnicmp(p, "LIBRARY", 7))
1724+ if (0 != stricmp(p, "LIBRARY") || next == '\n')
17271725 goto quit;
1728− pstrcpy(dllname, sizeof dllname, trimfront(p+7));
1726+ pstrcpy(dllname, sizeof dllname, get_token(&line, &next));
17291727 ++state;
1730− continue;
1731−
1728+ break;
17321729 case 1:
17331730 if (0 != stricmp(p, "EXPORTS"))
17341731 goto quit;
17351732 ++state;
1736− continue;
1737−
1733+ break;
17381734 case 2:
17391735 dllindex = pe_add_dllref(s1, dllname);
17401736 ++state;
@@ -1742,33 +1738,34 @@ static int pe_load_def(TCCState *s1, int fd) · 1 unmodified line
17421738 default:
17431739 /* get ordinal and will store in sym->st_value */
17441740 ord = 0;
1745− x = strchr(p, ' ');
1746− if (x) {
1747− *x = 0, x = strrchr(x + 1, '@');
1748− if (x) {
1749− char *d;
1750− ord = (int)strtol(x + 1, &d, 10);
1751− if (*d)
1752− ord = 0;
1753− }
1741+ if (next == '@') {
1742+ x = get_token(&line, &next);
1743+ ord = (int)strtol(x + 1, &x, 10);
17541744 }
1745+ //printf("token %s ; %s : %d\n", dllname, p, ord);
17551746 pe_putimport(s1, dllindex, p, ord);
1756− continue;
1747+ break;
17571748 }
1749+skip:
1750+ while ((unsigned char)next > ' ')
1751+ get_token(&line, &next);
1752+ if (next != '\n')
1753+ break;
17581754 }
17591755 ret = 0;
17601756 quit:
1761− fclose(fp);
1757+ tcc_free(buf);
17621758 return ret;
17631759 }
17641760
17651761 /* ------------------------------------------------------------- */
1766−static int pe_load_dll(TCCState *s1, const char *filename)
1762+
1763+static int pe_load_dll(TCCState *s1, int fd, const char *filename)
17671764 {
17681765 char *p, *q;
17691766 int index, ret;
17701767
1771− ret = tcc_get_dllexports(filename, &p);
1768+ ret = get_dllexports(fd, &p);
17721769 if (ret) {
17731770 return -1;
17741771 } else if (p) {
@@ -1780,7 +1777,6 @@ static int pe_load_dll(TCCState *s1, const char *filename) · 5 unmodified lines
17801777 return 0;
17811778 }
17821779
1783−/* ------------------------------------------------------------- */
17841780 ST_FUNC int pe_load_file(struct TCCState *s1, int fd, const char *filename)
17851781 {
17861782 int ret = -1;
@@ -1790,7 +1786,17 @@ ST_FUNC int pe_load_file(struct TCCState *s1, int fd, const char *filename) · 3 unmodified lines
17901786 else if (pe_load_res(s1, fd) == 0)
17911787 ret = 0;
17921788 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+
1793+PUB_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);
17941800 return ret;
17951801 }
17961802
tccpp.c+11−2
@@ -404,7 +404,7 @@ ST_FUNC void cstr_reset(CString *cstr) · 403 unmodified lines
404404 cstr->size = 0;
405405 }
406406
407−ST_FUNC int cstr_printf(CString *cstr, const char *fmt, ...)
407+ST_FUNC int cstr_vprintf(CString *cstr, const char *fmt, va_list ap)
408408 {
409409 va_list v;
410410 int len, size = 80;
@@ -413,7 +413,7 @@ ST_FUNC int cstr_printf(CString *cstr, const char *fmt, ...) · 2 unmodified lines
413413 if (size > cstr->size_allocated)
414414 cstr_realloc(cstr, size);
415415 size = cstr->size_allocated - cstr->size;
416− va_start(v, fmt);
416+ va_copy(v, ap);
417417 len = vsnprintf((char*)cstr->data + cstr->size, size, fmt, v);
418418 va_end(v);
419419 if (len > 0 && len < size)
@@ -424,6 +424,15 @@ ST_FUNC int cstr_printf(CString *cstr, const char *fmt, ...) · 4 unmodified lines
424424 return len;
425425 }
426426
427+ST_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+
427436 /* XXX: unicode ? */
428437 static void add_char(CString *cstr, int c)
429438 {
tcctok.h+14−2
@@ -1,3 +1,4 @@
1+/*********************************************************************/
12 /* keywords */
23 DEF(TOK_INT, "int")
34 DEF(TOK_VOID, "void")
@@ -343,8 +344,17 @@ · 339 unmodified lines
343344 DEF(TOK_longjmp, "longjmp")
344345 #endif
345346
347+
348+/*********************************************************************/
346349 /* 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 */
348358 DEF_ASMDIR(word)
349359 DEF_ASMDIR(align)
350360 DEF_ASMDIR(balign)
@@ -383,14 +393,16 @@ · 32 unmodified lines
383393 DEF_ASMDIR(short)
384394 DEF_ASMDIR(long)
385395 DEF_ASMDIR(int)
386− DEF_ASMDIR(section) /* must be last directive */
396+ DEF_ASMDIR(section) /* must be last directive */
387397
388398 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
389399 #include "i386-tok.h"
390400 #endif
401+
391402 #if defined TCC_TARGET_ARM || defined TCC_TARGET_ARM64
392403 #include "arm-tok.h"
393404 #endif
405+
394406 #if defined TCC_TARGET_RISCV64
395407 #include "riscv64-tok.h"
396408 #endif
tcctools.c+2−4
@@ -385,10 +385,8 @@ usage: · 384 unmodified lines
385385 ret = 0;
386386
387387 the_end:
388− /* cannot free memory received from tcc_get_dllexports
389− if it came from a dll */
390− /* if (p)
391− tcc_free(p); */
388+ if (p)
389+ tcc_free(p);
392390 if (fp)
393391 fclose(fp);
394392 if (op)
tests/libtcc_test_mt.c+5−4
@@ -220,9 +220,10 @@ TF_TYPE(thread_test_complex, vn) · 219 unmodified lines
220220 void time_tcc(int n, const char *src)
221221 {
222222 TCCState *s;
223− int ret;
224− while (--n >= 0) {
223+ int ret, i = 0;
224+ while (i++ < n) {
225225 s = new_state(1);
226+ printf(" %d", i), fflush(stdout);
226227 ret = tcc_add_file(s, src);
227228 tcc_delete(s);
228229 if (ret < 0)
@@ -277,10 +278,10 @@ int main(int argc, char **argv) · 48 unmodified lines
277278 printf("\n (%u ms)\n", getclock_ms() - t);
278279 #endif
279280 #if 1
280− printf("compiling tcc.c 10 times\n"), fflush(stdout);
281+ printf("compiling tcc.c 10 times\n "), fflush(stdout);
281282 t = getclock_ms();
282283 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);
284285 #endif
285286 return 0;
286287 }

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