History

libtcc usability improvements

- tccgen.c: cleanup switch data etc. after errors (*) - tccpe.c: faster get_dllexports (*) - tccpe.c: support -Wl,-e[ntry]=... (*) - libtcc.c: win32: use ANSI functions (GetModuleFileNameA etc.) - tccrun.c: be nice to tcc-0.9.26 ("struct/enum already defined") - tccpp.c: be nice to tcc-0.9.27's va_start/end macros (*) suggested by Robert Schlicht https://lists.gnu.org/archive/html/tinycc-devel/2024-03/msg00012.html

grischka committed 2 years ago

Commit 2b0a6631 parent 9d2068c7 files · +83 −62Browse files

Unified · Split

7 files
  1. libtcc.c +5 −6
  2. tccgen.c +25 −11
  3. tccpe.c +42 −33
  4. tccpp.c +5 −6
  5. tccrun.c +1 −3
  6. tests/tcctest.c +2 −3
  7. win32/build-tcc.bat +3 −0
libtcc.c+5−6
@@ -101,7 +101,7 @@ BOOL WINAPI DllMain (HINSTANCE hDll, DWORD dwReason, LPVOID lpReserved) · 100 unmodified lines
101static inline char *config_tccdir_w32(char *path)101static inline char *config_tccdir_w32(char *path)
102{102{
103 char *p;103 char *p;
104 GetModuleFileName(tcc_module, path, MAX_PATH);104 GetModuleFileNameA(tcc_module, path, MAX_PATH);
105 p = tcc_basename(normalize_slashes(strlwr(path)));105 p = tcc_basename(normalize_slashes(strlwr(path)));
106 if (p > path)106 if (p > path)
107 --p;107 --p;
@@ -115,7 +115,7 @@ static inline char *config_tccdir_w32(char *path) · 7 unmodified lines
115static void tcc_add_systemdir(TCCState *s)115static void tcc_add_systemdir(TCCState *s)
116{116{
117 char buf[1000];117 char buf[1000];
118 GetSystemDirectory(buf, sizeof buf);118 GetSystemDirectoryA(buf, sizeof buf);
119 tcc_add_library_path(s, normalize_slashes(buf));119 tcc_add_library_path(s, normalize_slashes(buf));
120}120}
121#endif121#endif
@@ -801,14 +801,11 @@ LIBTCCAPI TCCState *tcc_new(void) · 679 unmodified lines
801 TCCState *s;801 TCCState *s;
802 802
803 s = tcc_mallocz(sizeof(TCCState));803 s = tcc_mallocz(sizeof(TCCState));
804 if (!s)
805 return NULL;
806#ifdef MEM_DEBUG804#ifdef MEM_DEBUG
807 tcc_memcheck(1);805 tcc_memcheck(1);
808#endif806#endif
809 807
810#undef gnu_ext808#undef gnu_ext
811
812 s->gnu_ext = 1;809 s->gnu_ext = 1;
813 s->tcc_ext = 1;810 s->tcc_ext = 1;
814 s->nocommon = 1;811 s->nocommon = 1;
@@ -874,11 +871,13 @@ LIBTCCAPI void tcc_delete(TCCState *s1) · 59 unmodified lines
874 cstr_free(&s1->cmdline_defs);871 cstr_free(&s1->cmdline_defs);
875 cstr_free(&s1->cmdline_incl);872 cstr_free(&s1->cmdline_incl);
876 cstr_free(&s1->linker_arg);873 cstr_free(&s1->linker_arg);
874 tcc_free(s1->dState);
877#ifdef TCC_IS_NATIVE875#ifdef TCC_IS_NATIVE
878 /* free runtime memory */876 /* free runtime memory */
879 tcc_run_free(s1);877 tcc_run_free(s1);
880#endif878#endif
881 tcc_free(s1->dState);879 /* free loaded dlls array */
880 dynarray_reset(&s1->loaded_dlls, &s1->nb_loaded_dlls);
882 tcc_free(s1);881 tcc_free(s1);
883#ifdef MEM_DEBUG882#ifdef MEM_DEBUG
884 tcc_memcheck(-1);883 tcc_memcheck(-1);
tccgen.c+25−11
@@ -157,6 +157,7 @@ static void gv_dup(void); · 156 unmodified lines
157static int get_temp_local_var(int size,int align);157static int get_temp_local_var(int size,int align);
158static void clear_temp_local_var_list();158static void clear_temp_local_var_list();
159static void cast_error(CType *st, CType *dt);159static void cast_error(CType *st, CType *dt);
160static void end_switch(void);
160 161
161/* ------------------------------------------------------------------------- */162/* ------------------------------------------------------------------------- */
162/* Automagical code suppression */163/* Automagical code suppression */
@@ -381,12 +382,10 @@ ST_FUNC void tccgen_init(TCCState *s1) · 218 unmodified lines
381 382
382ST_FUNC int tccgen_compile(TCCState *s1)383ST_FUNC int tccgen_compile(TCCState *s1)
383{384{
384 cur_text_section = NULL;
385 funcname = "";385 funcname = "";
386 func_ind = -1;386 func_ind = -1;
387 anon_sym = SYM_FIRST_ANOM;387 anon_sym = SYM_FIRST_ANOM;
388 nocode_wanted = DATA_ONLY_WANTED; /* no code outside of functions */388 nocode_wanted = DATA_ONLY_WANTED; /* no code outside of functions */
389 local_scope = 0;
390 debug_modes = (s1->do_debug ? 1 : 0) | s1->test_coverage << 1;389 debug_modes = (s1->do_debug ? 1 : 0) | s1->test_coverage << 1;
391 390
392 tcc_debug_start(s1);391 tcc_debug_start(s1);
@@ -418,10 +417,19 @@ ST_FUNC void tccgen_finish(TCCState *s1) · 25 unmodified lines
418 free_defines(NULL);417 free_defines(NULL);
419 /* free sym_pools */418 /* free sym_pools */
420 dynarray_reset(&sym_pools, &nb_sym_pools);419 dynarray_reset(&sym_pools, &nb_sym_pools);
421 sym_free_first = NULL;
422 global_label_stack = local_label_stack = NULL;
423 cstr_free(&initstr);420 cstr_free(&initstr);
424 dynarray_reset(&stk_data, &nb_stk_data);421 dynarray_reset(&stk_data, &nb_stk_data);
422 while (cur_switch)
423 end_switch();
424 local_scope = 0;
425 loop_scope = NULL;
426 all_cleanups = NULL;
427 pending_gotos = NULL;
428 nb_temp_local_vars = 0;
429 global_label_stack = NULL;
430 local_label_stack = NULL;
431 cur_text_section = NULL;
432 sym_free_first = NULL;
425}433}
426 434
427/* ------------------------------------------------------------------------- */435/* ------------------------------------------------------------------------- */
@@ -6754,6 +6762,14 @@ static void gcase(struct case_t **base, int len, int *bsym) · 6326 unmodified lines
6754 *bsym = gjmp(*bsym);6762 *bsym = gjmp(*bsym);
6755}6763}
6756 6764
6765static void end_switch(void)
6766{
6767 struct switch_t *sw = cur_switch;
6768 dynarray_reset(&sw->p, &sw->n);
6769 cur_switch = sw->prev;
6770 tcc_free(sw);
6771}
6772
6757/* ------------------------------------------------------------------------- */6773/* ------------------------------------------------------------------------- */
6758/* __attribute__((cleanup(fn))) */6774/* __attribute__((cleanup(fn))) */
6759 6775
@@ -7154,15 +7170,14 @@ again: · 394 unmodified lines
7154 skip_switch:7170 skip_switch:
7155 /* break label */7171 /* break label */
7156 gsym(a);7172 gsym(a);
7157 7173 end_switch();
7158 dynarray_reset(&sw->p, &sw->n);
7159 cur_switch = sw->prev;
7160 tcc_free(sw);
7161 7174
7162 } else if (t == TOK_CASE) {7175 } else if (t == TOK_CASE) {
7163 struct case_t *cr = tcc_malloc(sizeof(struct case_t));7176 struct case_t *cr;
7164 if (!cur_switch)7177 if (!cur_switch)
7165 expect("switch");7178 expect("switch");
7179 cr = tcc_malloc(sizeof(struct case_t));
7180 dynarray_add(&cur_switch->p, &cur_switch->n, cr);
7166 cr->v1 = cr->v2 = expr_const64();7181 cr->v1 = cr->v2 = expr_const64();
7167 if (gnu_ext && tok == TOK_DOTS) {7182 if (gnu_ext && tok == TOK_DOTS) {
7168 next();7183 next();
@@ -7174,7 +7189,6 @@ again: · 5 unmodified lines
7174 /* case and default are unreachable from a switch under nocode_wanted */7189 /* case and default are unreachable from a switch under nocode_wanted */
7175 if (!cur_switch->nocode_wanted)7190 if (!cur_switch->nocode_wanted)
7176 cr->sym = gind();7191 cr->sym = gind();
7177 dynarray_add(&cur_switch->p, &cur_switch->n, cr);
7178 skip(':');7192 skip(':');
7179 goto block_after_label;7193 goto block_after_label;
7180 7194
@@ -8574,9 +8588,9 @@ static int decl(int l) · 1393 unmodified lines
8574 fn = tcc_malloc(sizeof *fn + strlen(file->filename));8588 fn = tcc_malloc(sizeof *fn + strlen(file->filename));
8575 strcpy(fn->filename, file->filename);8589 strcpy(fn->filename, file->filename);
8576 fn->sym = sym;8590 fn->sym = sym;
8577 skip_or_save_block(&fn->func_str);
8578 dynarray_add(&tcc_state->inline_fns,8591 dynarray_add(&tcc_state->inline_fns,
8579 &tcc_state->nb_inline_fns, fn);8592 &tcc_state->nb_inline_fns, fn);
8593 skip_or_save_block(&fn->func_str);
8580 } else {8594 } else {
8581 /* compute text section */8595 /* compute text section */
8582 cur_text_section = ad.section;8596 cur_text_section = ad.section;
tccpe.c+42−33
@@ -921,7 +921,7 @@ static void pe_build_imports(struct pe_info *pe) · 920 unmodified lines
921 if (pe->type == PE_RUN) {921 if (pe->type == PE_RUN) {
922 if (dllref) {922 if (dllref) {
923 if ( !dllref->handle )923 if ( !dllref->handle )
924 dllref->handle = LoadLibrary(dllref->name);924 dllref->handle = LoadLibraryA(dllref->name);
925 v = (ADDR3264)GetProcAddress(dllref->handle, ordinal?(char*)0+ordinal:name);925 v = (ADDR3264)GetProcAddress(dllref->handle, ordinal?(char*)0+ordinal:name);
926 }926 }
927 if (!v)927 if (!v)
@@ -1600,14 +1600,15 @@ static int read_mem(int fd, unsigned offset, void *buffer, unsigned len) · 672 unmodified lines
1600 1600
1601static int get_dllexports(int fd, char **pp)1601static int get_dllexports(int fd, char **pp)
1602{1602{
1603 int l, i, n, n0, ret;1603 int i, k, l, n, n0, ret;
1604 char *p;1604 char *p;
1605 1605
1606 IMAGE_SECTION_HEADER ish;1606 IMAGE_SECTION_HEADER ish;
1607 IMAGE_EXPORT_DIRECTORY ied;1607 IMAGE_EXPORT_DIRECTORY ied;
1608 IMAGE_DOS_HEADER dh;1608 IMAGE_DOS_HEADER dh;
1609 IMAGE_FILE_HEADER ih;1609 IMAGE_FILE_HEADER ih;
1610 DWORD sig, ref, addr, ptr, namep;1610 DWORD sig, ref, addr;
1611 DWORD *namep = NULL, p0 = 0, p1;
1611 1612
1612 int pef_hdroffset, opt_hdroffset, sec_hdroffset;1613 int pef_hdroffset, opt_hdroffset, sec_hdroffset;
1613 1614
@@ -1652,33 +1653,37 @@ static int get_dllexports(int fd, char **pp) · 38 unmodified lines
1652 goto found;1653 goto found;
1653 }1654 }
1654 goto the_end_0;1655 goto the_end_0;
1655
1656found:1656found:
1657 ref = ish.VirtualAddress - ish.PointerToRawData;1657 ref = ish.VirtualAddress - ish.PointerToRawData;
1658 if (!read_mem(fd, addr - ref, &ied, sizeof ied))1658 if (!read_mem(fd, addr - ref, &ied, sizeof ied))
1659 goto the_end;1659 goto the_end;
1660 1660 k = ied.NumberOfNames;
1661 namep = ied.AddressOfNames - ref;1661 if (k) {
1662 for (i = 0; i < ied.NumberOfNames; ++i) {1662 namep = tcc_malloc(l = k * sizeof *namep);
1663 if (!read_mem(fd, namep, &ptr, sizeof ptr))1663 if (!read_mem(fd, ied.AddressOfNames - ref, namep, l))
1664 goto the_end;1664 goto the_end;
1665 namep += sizeof ptr;1665 for (i = l = 0; i < k; ++i) {
1666 for (l = 0;;) {1666 p1 = namep[i] - ref;
1667 if (n+1 >= n0)1667 if (p1 != p0)
1668 p = tcc_realloc(p, n0 = n0 ? n0 * 2 : 256);1668 lseek(fd, p0 = p1, SEEK_SET), l = 0;
1669 if (!read_mem(fd, ptr - ref + l++, p + n, 1)) {1669 do {
1670 tcc_free(p), p = NULL;1670 if (0 == l) {
1671 goto the_end;1671 if (n + 1000 >= n0)
1672 }1672 p = tcc_realloc(p, n0 += 1000);
1673 if (p[n++] == 0)1673 if ((l = read(fd, p + n, 1000 - 1)) <= 0)
1674 break;1674 goto the_end;
1675 }
1676 --l, ++p0;
1677 } while (p[n++]);
1675 }1678 }
1676 }
1677 if (p)
1678 p[n] = 0;1679 p[n] = 0;
1680 }
1679the_end_0:1681the_end_0:
1680 ret = 0;1682 ret = 0;
1681the_end:1683the_end:
1684 tcc_free(namep);
1685 if (ret && p)
1686 tcc_free(p), p = NULL;
1682 *pp = p;1687 *pp = p;
1683 return ret;1688 return ret;
1684}1689}
@@ -1810,15 +1815,14 @@ quit: · 125 unmodified lines
1810static int pe_load_dll(TCCState *s1, int fd, const char *filename)1815static int pe_load_dll(TCCState *s1, int fd, const char *filename)
1811{1816{
1812 char *p, *q;1817 char *p, *q;
1813 int index, ret;1818 DLLReference *ref = tcc_add_dllref(s1, filename, 0);
1814 1819 if (ref->found)
1815 ret = get_dllexports(fd, &p);1820 return 0;
1816 if (ret) {1821 if (get_dllexports(fd, &p))
1817 return -1;1822 return -1;
1818 } else if (p) {1823 if (p) {
1819 index = tcc_add_dllref(s1, filename, 0)->index;
1820 for (q = p; *q; q += 1 + strlen(q))1824 for (q = p; *q; q += 1 + strlen(q))
1821 pe_putimport(s1, index, q, 0);1825 pe_putimport(s1, ref->index, q, 0);
1822 tcc_free(p);1826 tcc_free(p);
1823 }1827 }
1824 return 0;1828 return 0;
@@ -1941,15 +1945,20 @@ static void pe_add_runtime(TCCState *s1, struct pe_info *pe) · 116 unmodified lines
1941 start_symbol = "__start";1945 start_symbol = "__start";
1942 run_symbol = "__runmain";1946 run_symbol = "__runmain";
1943 pe_type = PE_EXE;1947 pe_type = PE_EXE;
1944 1948 if (s1->pe_subsystem == 2)
1949 pe_type = PE_GUI;
1945 }1950 }
1951
1946 if (TCC_OUTPUT_MEMORY == s1->output_type && !s1->nostdlib)1952 if (TCC_OUTPUT_MEMORY == s1->output_type && !s1->nostdlib)
1947 start_symbol = run_symbol;1953 start_symbol = run_symbol;
1948 }1954 }
1949 1955 if (s1->elf_entryname) {
1950 pe->start_symbol = start_symbol + 1;1956 pe->start_symbol = start_symbol = s1->elf_entryname;
1951 if (!s1->leading_underscore || strchr(start_symbol, '@'))1957 } else {
1952 ++start_symbol;1958 pe->start_symbol = start_symbol + 1;
1959 if (!s1->leading_underscore || strchr(start_symbol, '@'))
1960 ++start_symbol;
1961 }
1953 1962
1954#ifdef CONFIG_TCC_BACKTRACE1963#ifdef CONFIG_TCC_BACKTRACE
1955 if (s1->do_backtrace) {1964 if (s1->do_backtrace) {
@@ -2055,8 +2064,8 @@ ST_FUNC int pe_output_file(TCCState *s1, const char *filename) · 99 unmodified lines
2055#ifdef CONFIG_TCC_BCHECK2064#ifdef CONFIG_TCC_BCHECK
2056 tcc_add_bcheck(s1);2065 tcc_add_bcheck(s1);
2057#endif2066#endif
2058 tcc_add_pragma_libs(s1);
2059 pe_add_runtime(s1, &pe);2067 pe_add_runtime(s1, &pe);
2068 tcc_add_pragma_libs(s1);
2060 resolve_common_syms(s1);2069 resolve_common_syms(s1);
2061 pe_set_options(s1, &pe);2070 pe_set_options(s1, &pe);
2062 pe_check_symbols(&pe);2071 pe_check_symbols(&pe);
tccpp.c+5−6
@@ -3547,11 +3547,7 @@ static void putdefs(CString *cs, const char *p) · 3546 unmodified lines
3547 3547
3548static void tcc_predefs(TCCState *s1, CString *cs, int is_asm)3548static void tcc_predefs(TCCState *s1, CString *cs, int is_asm)
3549{3549{
3550 int a, b, c;3550 cstr_printf(cs, "#define __TINYC__ 9%.2s\n", TCC_VERSION + 4);
3551
3552 sscanf(TCC_VERSION, "%d.%d.%d", &a, &b, &c);
3553 cstr_printf(cs, "#define __TINYC__ %d\n", a*10000 + b*100 + c);
3554
3555 putdefs(cs, target_machine_defs);3551 putdefs(cs, target_machine_defs);
3556 putdefs(cs, target_os_defs);3552 putdefs(cs, target_os_defs);
3557 3553
@@ -3742,7 +3738,10 @@ static void tok_print(const int *str, const char *msg, ...) · 184 unmodified lines
3742 va_list ap;3738 va_list ap;
3743 int t, t0, s;3739 int t, t0, s;
3744 CValue cval;3740 CValue cval;
3745 va_start(ap, msg), vfprintf(fp, msg, ap), va_end(ap);3741
3742 va_start(ap, msg);
3743 vfprintf(fp, msg, ap);
3744 va_end(ap);
3746 3745
3747 s = t0 = 0;3746 s = t0 = 0;
3748 while (str) {3747 while (str) {
tccrun.c+1−3
@@ -179,8 +179,6 @@ ST_FUNC void tcc_run_free(TCCState *s1) · 178 unmodified lines
179 dlclose(ref->handle);179 dlclose(ref->handle);
180#endif180#endif
181 }181 }
182 /* free loaded dlls array */
183 dynarray_reset(&s1->loaded_dlls, &s1->nb_loaded_dlls);
184 /* unmap or unprotect and free memory */182 /* unmap or unprotect and free memory */
185 ptr = s1->run_ptr;183 ptr = s1->run_ptr;
186 if (NULL == ptr)184 if (NULL == ptr)
@@ -854,7 +852,7 @@ static addr_t rt_printline_dwarf (rt_context *rc, addr_t wanted_pc, bt_info *bi) · 667 unmodified lines
854 char *dirs[DIR_TABLE_SIZE];852 char *dirs[DIR_TABLE_SIZE];
855#endif853#endif
856 unsigned int filename_size;854 unsigned int filename_size;
857 struct dwarf_filename_struct {855 struct /*dwarf_filename_struct*/ {
858 unsigned int dir_entry;856 unsigned int dir_entry;
859 char *name;857 char *name;
860 } filename_table[FILE_TABLE_SIZE];858 } filename_table[FILE_TABLE_SIZE];
tests/tcctest.c+2−3
@@ -3760,14 +3760,13 @@ void asm_dot_test(void) · 3759 unmodified lines
3760 3760
3761void asm_pcrel_test(void)3761void asm_pcrel_test(void)
3762{3762{
3763#if defined(__i386__)
3764 unsigned o1, o2;3763 unsigned o1, o2;
3765 /* subtract text-section label from forward or other-section label */3764 /* subtract text-section label from forward or other-section label */
3766 asm("1: mov $2f-1b,%%eax; mov %%eax,%0" : "=m"(o1));3765 asm("1: mov $2f-1b,%%eax; mov %%eax,%0" : "=m"(o1));
3767 /* verify ... */3766 /* verify ... */
3768 asm("2: mov $2b,%%eax; sub $1b,%%eax; mov %%eax,%0" : "=m"(o2));3767 asm("2: lea 2b"RX",%eax; lea 1b"RX",%ecx; sub %ecx,%eax");
3768 asm("mov %%eax,%0" : "=m"(o2));
3769 printf("%s : %x\n", __FUNCTION__, o1 - o2); /* should be zero */3769 printf("%s : %x\n", __FUNCTION__, o1 - o2); /* should be zero */
3770#endif
3771}3770}
3772 3771
3773void asm_test(void)3772void asm_test(void)
win32/build-tcc.bat+3−0
@@ -137,6 +137,7 @@ for %%f in (*tcc.exe *tcc.dll) do @del %%f · 136 unmodified lines
137@if _%TCC_C%_==__ goto compiler_2parts137@if _%TCC_C%_==__ goto compiler_2parts
138@rem if TCC_C was defined then build only tcc.exe138@rem if TCC_C was defined then build only tcc.exe
139%CC% -o tcc.exe %TCC_C% %D%139%CC% -o tcc.exe %TCC_C% %D%
140@if errorlevel 1 goto :the_end
140@goto :compiler_done141@goto :compiler_done
141 142
142:compiler_2parts143:compiler_2parts
@@ -144,8 +145,10 @@ for %%f in (*tcc.exe *tcc.dll) do @del %%f · 1 unmodified line
144%CC% -o libtcc.dll -shared %LIBTCC_C% %D% -DLIBTCC_AS_DLL145%CC% -o libtcc.dll -shared %LIBTCC_C% %D% -DLIBTCC_AS_DLL
145@if errorlevel 1 goto :the_end146@if errorlevel 1 goto :the_end
146%CC% -o tcc.exe ..\tcc.c libtcc.dll %D% -DONE_SOURCE"=0"147%CC% -o tcc.exe ..\tcc.c libtcc.dll %D% -DONE_SOURCE"=0"
148@if errorlevel 1 goto :the_end
147if not _%XCC%_==_yes_ goto :compiler_done149if not _%XCC%_==_yes_ goto :compiler_done
148%CC% -o %PX%-tcc.exe ..\tcc.c %DX%150%CC% -o %PX%-tcc.exe ..\tcc.c %DX%
151@if errorlevel 1 goto :the_end
149:compiler_done152:compiler_done
150@if (%EXES_ONLY%)==(yes) goto :files_done153@if (%EXES_ONLY%)==(yes) goto :files_done
151 154

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