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
101101 static inline char *config_tccdir_w32(char *path)
102102 {
103103 char *p;
104− GetModuleFileName(tcc_module, path, MAX_PATH);
104+ GetModuleFileNameA(tcc_module, path, MAX_PATH);
105105 p = tcc_basename(normalize_slashes(strlwr(path)));
106106 if (p > path)
107107 --p;
@@ -115,7 +115,7 @@ static inline char *config_tccdir_w32(char *path) · 7 unmodified lines
115115 static void tcc_add_systemdir(TCCState *s)
116116 {
117117 char buf[1000];
118− GetSystemDirectory(buf, sizeof buf);
118+ GetSystemDirectoryA(buf, sizeof buf);
119119 tcc_add_library_path(s, normalize_slashes(buf));
120120 }
121121 #endif
@@ -801,14 +801,11 @@ LIBTCCAPI TCCState *tcc_new(void) · 679 unmodified lines
801801 TCCState *s;
802802
803803 s = tcc_mallocz(sizeof(TCCState));
804− if (!s)
805− return NULL;
806804 #ifdef MEM_DEBUG
807805 tcc_memcheck(1);
808806 #endif
809807
810808 #undef gnu_ext
811−
812809 s->gnu_ext = 1;
813810 s->tcc_ext = 1;
814811 s->nocommon = 1;
@@ -874,11 +871,13 @@ LIBTCCAPI void tcc_delete(TCCState *s1) · 59 unmodified lines
874871 cstr_free(&s1->cmdline_defs);
875872 cstr_free(&s1->cmdline_incl);
876873 cstr_free(&s1->linker_arg);
874+ tcc_free(s1->dState);
877875 #ifdef TCC_IS_NATIVE
878876 /* free runtime memory */
879877 tcc_run_free(s1);
880878 #endif
881− tcc_free(s1->dState);
879+ /* free loaded dlls array */
880+ dynarray_reset(&s1->loaded_dlls, &s1->nb_loaded_dlls);
882881 tcc_free(s1);
883882 #ifdef MEM_DEBUG
884883 tcc_memcheck(-1);
tccgen.c+25−11
@@ -157,6 +157,7 @@ static void gv_dup(void); · 156 unmodified lines
157157 static int get_temp_local_var(int size,int align);
158158 static void clear_temp_local_var_list();
159159 static void cast_error(CType *st, CType *dt);
160+static void end_switch(void);
160161
161162 /* ------------------------------------------------------------------------- */
162163 /* Automagical code suppression */
@@ -381,12 +382,10 @@ ST_FUNC void tccgen_init(TCCState *s1) · 218 unmodified lines
381382
382383 ST_FUNC int tccgen_compile(TCCState *s1)
383384 {
384− cur_text_section = NULL;
385385 funcname = "";
386386 func_ind = -1;
387387 anon_sym = SYM_FIRST_ANOM;
388388 nocode_wanted = DATA_ONLY_WANTED; /* no code outside of functions */
389− local_scope = 0;
390389 debug_modes = (s1->do_debug ? 1 : 0) | s1->test_coverage << 1;
391390
392391 tcc_debug_start(s1);
@@ -418,10 +417,19 @@ ST_FUNC void tccgen_finish(TCCState *s1) · 25 unmodified lines
418417 free_defines(NULL);
419418 /* free sym_pools */
420419 dynarray_reset(&sym_pools, &nb_sym_pools);
421− sym_free_first = NULL;
422− global_label_stack = local_label_stack = NULL;
423420 cstr_free(&initstr);
424421 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;
425433 }
426434
427435 /* ------------------------------------------------------------------------- */
@@ -6754,6 +6762,14 @@ static void gcase(struct case_t **base, int len, int *bsym) · 6326 unmodified lines
67546762 *bsym = gjmp(*bsym);
67556763 }
67566764
6765+static 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+
67576773 /* ------------------------------------------------------------------------- */
67586774 /* __attribute__((cleanup(fn))) */
67596775
@@ -7154,15 +7170,14 @@ again: · 394 unmodified lines
71547170 skip_switch:
71557171 /* break label */
71567172 gsym(a);
7157−
7158− dynarray_reset(&sw->p, &sw->n);
7159− cur_switch = sw->prev;
7160− tcc_free(sw);
7173+ end_switch();
71617174
71627175 } else if (t == TOK_CASE) {
7163− struct case_t *cr = tcc_malloc(sizeof(struct case_t));
7176+ struct case_t *cr;
71647177 if (!cur_switch)
71657178 expect("switch");
7179+ cr = tcc_malloc(sizeof(struct case_t));
7180+ dynarray_add(&cur_switch->p, &cur_switch->n, cr);
71667181 cr->v1 = cr->v2 = expr_const64();
71677182 if (gnu_ext && tok == TOK_DOTS) {
71687183 next();
@@ -7174,7 +7189,6 @@ again: · 5 unmodified lines
71747189 /* case and default are unreachable from a switch under nocode_wanted */
71757190 if (!cur_switch->nocode_wanted)
71767191 cr->sym = gind();
7177− dynarray_add(&cur_switch->p, &cur_switch->n, cr);
71787192 skip(':');
71797193 goto block_after_label;
71807194
@@ -8574,9 +8588,9 @@ static int decl(int l) · 1393 unmodified lines
85748588 fn = tcc_malloc(sizeof *fn + strlen(file->filename));
85758589 strcpy(fn->filename, file->filename);
85768590 fn->sym = sym;
8577− skip_or_save_block(&fn->func_str);
85788591 dynarray_add(&tcc_state->inline_fns,
85798592 &tcc_state->nb_inline_fns, fn);
8593+ skip_or_save_block(&fn->func_str);
85808594 } else {
85818595 /* compute text section */
85828596 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
921921 if (pe->type == PE_RUN) {
922922 if (dllref) {
923923 if ( !dllref->handle )
924− dllref->handle = LoadLibrary(dllref->name);
924+ dllref->handle = LoadLibraryA(dllref->name);
925925 v = (ADDR3264)GetProcAddress(dllref->handle, ordinal?(char*)0+ordinal:name);
926926 }
927927 if (!v)
@@ -1600,14 +1600,15 @@ static int read_mem(int fd, unsigned offset, void *buffer, unsigned len) · 672 unmodified lines
16001600
16011601 static int get_dllexports(int fd, char **pp)
16021602 {
1603− int l, i, n, n0, ret;
1603+ int i, k, l, n, n0, ret;
16041604 char *p;
16051605
16061606 IMAGE_SECTION_HEADER ish;
16071607 IMAGE_EXPORT_DIRECTORY ied;
16081608 IMAGE_DOS_HEADER dh;
16091609 IMAGE_FILE_HEADER ih;
1610− DWORD sig, ref, addr, ptr, namep;
1610+ DWORD sig, ref, addr;
1611+ DWORD *namep = NULL, p0 = 0, p1;
16111612
16121613 int pef_hdroffset, opt_hdroffset, sec_hdroffset;
16131614
@@ -1652,33 +1653,37 @@ static int get_dllexports(int fd, char **pp) · 38 unmodified lines
16521653 goto found;
16531654 }
16541655 goto the_end_0;
1655−
16561656 found:
16571657 ref = ish.VirtualAddress - ish.PointerToRawData;
16581658 if (!read_mem(fd, addr - ref, &ied, sizeof ied))
16591659 goto the_end;
1660−
1661− namep = ied.AddressOfNames - ref;
1662− for (i = 0; i < ied.NumberOfNames; ++i) {
1663− if (!read_mem(fd, namep, &ptr, sizeof ptr))
1660+ k = ied.NumberOfNames;
1661+ if (k) {
1662+ namep = tcc_malloc(l = k * sizeof *namep);
1663+ if (!read_mem(fd, ied.AddressOfNames - ref, namep, l))
16641664 goto the_end;
1665− namep += sizeof ptr;
1666− for (l = 0;;) {
1667− if (n+1 >= n0)
1668− p = tcc_realloc(p, n0 = n0 ? n0 * 2 : 256);
1669− if (!read_mem(fd, ptr - ref + l++, p + n, 1)) {
1670− tcc_free(p), p = NULL;
1671− goto the_end;
1672− }
1673− if (p[n++] == 0)
1674− break;
1665+ for (i = l = 0; i < k; ++i) {
1666+ p1 = namep[i] - ref;
1667+ if (p1 != p0)
1668+ lseek(fd, p0 = p1, SEEK_SET), l = 0;
1669+ do {
1670+ if (0 == l) {
1671+ if (n + 1000 >= n0)
1672+ p = tcc_realloc(p, n0 += 1000);
1673+ if ((l = read(fd, p + n, 1000 - 1)) <= 0)
1674+ goto the_end;
1675+ }
1676+ --l, ++p0;
1677+ } while (p[n++]);
16751678 }
1676− }
1677− if (p)
16781679 p[n] = 0;
1680+ }
16791681 the_end_0:
16801682 ret = 0;
16811683 the_end:
1684+ tcc_free(namep);
1685+ if (ret && p)
1686+ tcc_free(p), p = NULL;
16821687 *pp = p;
16831688 return ret;
16841689 }
@@ -1810,15 +1815,14 @@ quit: · 125 unmodified lines
18101815 static int pe_load_dll(TCCState *s1, int fd, const char *filename)
18111816 {
18121817 char *p, *q;
1813− int index, ret;
1814−
1815− ret = get_dllexports(fd, &p);
1816− if (ret) {
1818+ DLLReference *ref = tcc_add_dllref(s1, filename, 0);
1819+ if (ref->found)
1820+ return 0;
1821+ if (get_dllexports(fd, &p))
18171822 return -1;
1818− } else if (p) {
1819− index = tcc_add_dllref(s1, filename, 0)->index;
1823+ if (p) {
18201824 for (q = p; *q; q += 1 + strlen(q))
1821− pe_putimport(s1, index, q, 0);
1825+ pe_putimport(s1, ref->index, q, 0);
18221826 tcc_free(p);
18231827 }
18241828 return 0;
@@ -1941,15 +1945,20 @@ static void pe_add_runtime(TCCState *s1, struct pe_info *pe) · 116 unmodified lines
19411945 start_symbol = "__start";
19421946 run_symbol = "__runmain";
19431947 pe_type = PE_EXE;
1944−
1948+ if (s1->pe_subsystem == 2)
1949+ pe_type = PE_GUI;
19451950 }
1951+
19461952 if (TCC_OUTPUT_MEMORY == s1->output_type && !s1->nostdlib)
19471953 start_symbol = run_symbol;
19481954 }
1949−
1950− pe->start_symbol = start_symbol + 1;
1951− if (!s1->leading_underscore || strchr(start_symbol, '@'))
1952− ++start_symbol;
1955+ if (s1->elf_entryname) {
1956+ pe->start_symbol = start_symbol = s1->elf_entryname;
1957+ } else {
1958+ pe->start_symbol = start_symbol + 1;
1959+ if (!s1->leading_underscore || strchr(start_symbol, '@'))
1960+ ++start_symbol;
1961+ }
19531962
19541963 #ifdef CONFIG_TCC_BACKTRACE
19551964 if (s1->do_backtrace) {
@@ -2055,8 +2064,8 @@ ST_FUNC int pe_output_file(TCCState *s1, const char *filename) · 99 unmodified lines
20552064 #ifdef CONFIG_TCC_BCHECK
20562065 tcc_add_bcheck(s1);
20572066 #endif
2058− tcc_add_pragma_libs(s1);
20592067 pe_add_runtime(s1, &pe);
2068+ tcc_add_pragma_libs(s1);
20602069 resolve_common_syms(s1);
20612070 pe_set_options(s1, &pe);
20622071 pe_check_symbols(&pe);
tccpp.c+5−6
@@ -3547,11 +3547,7 @@ static void putdefs(CString *cs, const char *p) · 3546 unmodified lines
35473547
35483548 static void tcc_predefs(TCCState *s1, CString *cs, int is_asm)
35493549 {
3550− int a, b, c;
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−
3550+ cstr_printf(cs, "#define __TINYC__ 9%.2s\n", TCC_VERSION + 4);
35553551 putdefs(cs, target_machine_defs);
35563552 putdefs(cs, target_os_defs);
35573553
@@ -3742,7 +3738,10 @@ static void tok_print(const int *str, const char *msg, ...) · 184 unmodified lines
37423738 va_list ap;
37433739 int t, t0, s;
37443740 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);
37463745
37473746 s = t0 = 0;
37483747 while (str) {
tccrun.c+1−3
@@ -179,8 +179,6 @@ ST_FUNC void tcc_run_free(TCCState *s1) · 178 unmodified lines
179179 dlclose(ref->handle);
180180 #endif
181181 }
182− /* free loaded dlls array */
183− dynarray_reset(&s1->loaded_dlls, &s1->nb_loaded_dlls);
184182 /* unmap or unprotect and free memory */
185183 ptr = s1->run_ptr;
186184 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
854852 char *dirs[DIR_TABLE_SIZE];
855853 #endif
856854 unsigned int filename_size;
857− struct dwarf_filename_struct {
855+ struct /*dwarf_filename_struct*/ {
858856 unsigned int dir_entry;
859857 char *name;
860858 } filename_table[FILE_TABLE_SIZE];
tests/tcctest.c+2−3
@@ -3760,14 +3760,13 @@ void asm_dot_test(void) · 3759 unmodified lines
37603760
37613761 void asm_pcrel_test(void)
37623762 {
3763−#if defined(__i386__)
37643763 unsigned o1, o2;
37653764 /* subtract text-section label from forward or other-section label */
37663765 asm("1: mov $2f-1b,%%eax; mov %%eax,%0" : "=m"(o1));
37673766 /* 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));
37693769 printf("%s : %x\n", __FUNCTION__, o1 - o2); /* should be zero */
3770−#endif
37713770 }
37723771
37733772 void 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
137137 @if _%TCC_C%_==__ goto compiler_2parts
138138 @rem if TCC_C was defined then build only tcc.exe
139139 %CC% -o tcc.exe %TCC_C% %D%
140+@if errorlevel 1 goto :the_end
140141 @goto :compiler_done
141142
142143 :compiler_2parts
@@ -144,8 +145,10 @@ for %%f in (*tcc.exe *tcc.dll) do @del %%f · 1 unmodified line
144145 %CC% -o libtcc.dll -shared %LIBTCC_C% %D% -DLIBTCC_AS_DLL
145146 @if errorlevel 1 goto :the_end
146147 %CC% -o tcc.exe ..\tcc.c libtcc.dll %D% -DONE_SOURCE"=0"
148+@if errorlevel 1 goto :the_end
147149 if not _%XCC%_==_yes_ goto :compiler_done
148150 %CC% -o %PX%-tcc.exe ..\tcc.c %DX%
151+@if errorlevel 1 goto :the_end
149152 :compiler_done
150153 @if (%EXES_ONLY%)==(yes) goto :files_done
151154

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