Skip to content

Commit 0061744

Browse files
author
grischka
committed
cleanups & stuff
libtcc.c: - free 'elfint' string - acceot -O and -Os - accept -gstabs (to override dwarf when default) - better -Wp,... tccpp.c: - #line cleanup also warn with "extra tokens after directive" tccgen.c & xxx_gen.c: - force CPU flags to register earlier tccelf.c: - tcc_load_object: align size only for code sections data/bss objects are always put with their specfic type align (in decl_initializer_alloc()) x86/64 doesn't need aligned code from c6afdff tccpe.c: - enable dllimport for "_imp__<sym>" also from assembler x86_64-gen.c & lib/libtcc1.c: - simpler fneg without libtcc1 reference tests2/134_double_to_signed.c: - a tcc compiled by msvc won't pass this test
1 parent f075851 commit 0061744

14 files changed

+65
-96
lines changed

arm-gen.c

+1-13
Original file line numberDiff line numberDiff line change
@@ -1332,7 +1332,7 @@ static int copy_params(int nb_args, struct plan *plan, int todo)
13321332
parameters and the function address. */
13331333
void gfunc_call(int nb_args)
13341334
{
1335-
int r, args_size;
1335+
int args_size;
13361336
int def_float_abi = float_abi;
13371337
int todo;
13381338
struct plan plan;
@@ -1352,12 +1352,6 @@ void gfunc_call(int nb_args)
13521352
float_abi = ARM_SOFTFP_FLOAT;
13531353
}
13541354
#endif
1355-
/* cannot let cpu flags if other instruction are generated. Also avoid leaving
1356-
VT_JMP anywhere except on the top of the stack because it would complicate
1357-
the code generator. */
1358-
r = vtop->r & VT_VALMASK;
1359-
if (r == VT_CMP || (r & ~1) == VT_JMP)
1360-
gv(RC_INT);
13611355

13621356
memset(&plan, 0, sizeof plan);
13631357
if (nb_args)
@@ -1716,9 +1710,6 @@ void gen_opi(int op)
17161710
opc|=2; // sub -> rsb
17171711
}
17181712
}
1719-
if ((vtop->r & VT_VALMASK) == VT_CMP ||
1720-
(vtop->r & (VT_VALMASK & ~1)) == VT_JMP)
1721-
gv(RC_INT);
17221713
vswap();
17231714
c=intr(gv(RC_INT));
17241715
vswap();
@@ -1757,9 +1748,6 @@ void gen_opi(int op)
17571748
break;
17581749
case 2:
17591750
opc=0xE1A00000|(opc<<5);
1760-
if ((vtop->r & VT_VALMASK) == VT_CMP ||
1761-
(vtop->r & (VT_VALMASK & ~1)) == VT_JMP)
1762-
gv(RC_INT);
17631751
vswap();
17641752
r=intr(gv(RC_INT));
17651753
vswap();

arm64-gen.c

-4
Original file line numberDiff line numberDiff line change
@@ -1029,10 +1029,6 @@ ST_FUNC void gfunc_call(int nb_args)
10291029

10301030
stack = (stack + 15) >> 4 << 4;
10311031

1032-
/* fetch cpu flag before generating any code */
1033-
if ((vtop->r & VT_VALMASK) == VT_CMP)
1034-
gv(RC_INT);
1035-
10361032
if (stack >= 0x1000000) // 16Mb
10371033
tcc_error("stack size too big %lu", stack);
10381034
if (stack & 0xfff)

i386-gen.c

-3
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,6 @@ ST_FUNC void gfunc_call(int nb_args)
409409
args_size = 0;
410410
for(i = 0;i < nb_args; i++) {
411411
if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
412-
/* fetch cpu flag before generating any code */
413-
if ((vtop->r & VT_VALMASK) == VT_CMP)
414-
gv(RC_INT);
415412
size = type_size(&vtop->type, &align);
416413
/* align to stack align size */
417414
size = (size + 3) & ~3;

lib/libtcc1.c

-6
Original file line numberDiff line numberDiff line change
@@ -626,12 +626,6 @@ long long __fixxfdi (long double a1)
626626
}
627627
#endif /* !ARM */
628628

629-
#if defined __x86_64__
630-
/* float constants used for unary minus operation */
631-
const float __mzerosf = -0.0;
632-
const double __mzerodf = -0.0;
633-
#endif
634-
635629
#if defined _WIN64
636630
/* MSVC x64 intrinsic */
637631
void __faststorefence(void)

libtcc.c

+28-16
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ TCC_SEM(static tcc_compile_sem);
7575
/* an array of pointers to memory to be free'd after errors */
7676
ST_DATA void** stk_data;
7777
ST_DATA int nb_stk_data;
78+
/* option -d<num> (for general development purposes) */
79+
ST_DATA int g_debug;
7880

7981
/********************************************************/
8082
#ifdef _WIN32
@@ -547,9 +549,8 @@ static void tcc_split_path(TCCState *s, void *p_ary, int *p_nb_ary, const char *
547549
}
548550
if (str.size) {
549551
cstr_ccat(&str, '\0');
550-
dynarray_add(p_ary, p_nb_ary, tcc_strdup(str.data));
552+
dynarray_add(p_ary, p_nb_ary, str.data);
551553
}
552-
cstr_free(&str);
553554
in = p+1;
554555
} while (*p);
555556
}
@@ -848,6 +849,9 @@ LIBTCCAPI TCCState *tcc_new(void)
848849
s->include_stack_ptr = s->include_stack;
849850

850851
tcc_set_lib_path(s, CONFIG_TCCDIR);
852+
#ifdef CONFIG_TCC_SWITCHES /* predefined options */
853+
tcc_set_options(s, CONFIG_TCC_SWITCHES);
854+
#endif
851855
return s;
852856
}
853857

@@ -867,6 +871,7 @@ LIBTCCAPI void tcc_delete(TCCState *s1)
867871
tcc_free(s1->tcc_lib_path);
868872
tcc_free(s1->soname);
869873
tcc_free(s1->rpath);
874+
tcc_free(s1->elfint);
870875
tcc_free(s1->elf_entryname);
871876
tcc_free(s1->init_symbol);
872877
tcc_free(s1->fini_symbol);
@@ -1225,17 +1230,15 @@ LIBTCCAPI int tcc_add_library(TCCState *s, const char *libraryname)
12251230
"%s/lib%s.a",
12261231
NULL
12271232
};
1228-
1229-
const char * const *pp = s->static_link
1230-
? libs + sizeof(libs) / sizeof(*libs) - 2
1231-
: libs;
1232-
12331233
/* if libraryname begins with a colon, it means search lib paths for
12341234
exactly the following file, without lib prefix or anything */
1235-
if (*libraryname == ':')
1235+
if (*libraryname == ':') {
12361236
libraryname++;
1237-
else {
1237+
} else {
12381238
int flags = s->filetype & AFF_WHOLE_ARCHIVE;
1239+
const char * const *pp = libs;
1240+
if (s->static_link)
1241+
pp += sizeof(libs) / sizeof(*libs) - 2; /* only "%s/lib%s.a" */
12391242
while (*pp) {
12401243
int ret = tcc_add_library_internal(s, *pp,
12411244
libraryname, flags, s->library_paths, s->nb_library_paths);
@@ -1341,7 +1344,7 @@ static int link_option(const char *str, const char *val, const char **ptr)
13411344
if (*p != ',' && *p != '=')
13421345
return 0;
13431346
p++;
1344-
} else if (*p) {
1347+
} else if (*p && *p != ',') {
13451348
return 0;
13461349
}
13471350
*ptr = p;
@@ -1890,7 +1893,6 @@ PUB_FUNC int tcc_parse_args(TCCState *s, int *pargc, char ***pargv, int optind)
18901893
++s->verbose;
18911894
continue;
18921895
}
1893-
reparse:
18941896
if (r[0] != '-' || r[1] == '\0') {
18951897
args_parser_add_file(s, r, s->filetype);
18961898
if (run) {
@@ -1968,7 +1970,8 @@ PUB_FUNC int tcc_parse_args(TCCState *s, int *pargc, char ***pargv, int optind)
19681970
goto enable_backtrace;
19691971
enable_backtrace:
19701972
s->do_backtrace = 1;
1971-
s->do_debug = s->do_debug ? s->do_debug : 1;
1973+
if (0 == s->do_debug)
1974+
s->do_debug = 1;
19721975
s->dwarf = CONFIG_DWARF_VERSION;
19731976
break;
19741977
#ifdef CONFIG_TCC_BCHECK
@@ -1982,6 +1985,8 @@ PUB_FUNC int tcc_parse_args(TCCState *s, int *pargc, char ***pargv, int optind)
19821985
s->dwarf = CONFIG_DWARF_VERSION;
19831986
if (strstart("dwarf", &optarg)) {
19841987
s->dwarf = (*optarg) ? (0 - atoi(optarg)) : DEFAULT_DWARF_VERSION;
1988+
} else if (0 == strcmp("stabs", optarg)) {
1989+
s->dwarf = 0;
19851990
} else if (isnum(*optarg)) {
19861991
x = *optarg - '0';
19871992
/* -g0 = no info, -g1 = lines/functions only, -g2 = full info */
@@ -2007,7 +2012,7 @@ PUB_FUNC int tcc_parse_args(TCCState *s, int *pargc, char ***pargv, int optind)
20072012
else if (*optarg == 't')
20082013
s->dflag = 16;
20092014
else if (isnum(*optarg))
2010-
s->g_debug |= atoi(optarg);
2015+
g_debug |= atoi(optarg);
20112016
else
20122017
goto unsupported_option;
20132018
break;
@@ -2106,8 +2111,15 @@ PUB_FUNC int tcc_parse_args(TCCState *s, int *pargc, char ***pargv, int optind)
21062111
return -1;
21072112
break;
21082113
case TCC_OPTION_Wp:
2109-
r = optarg;
2110-
goto reparse;
2114+
{
2115+
char *p = tcc_strdup(optarg), *q = p;
2116+
while (!!(q = strchr(q, ','))) *q++ = ' ';
2117+
x = tcc_set_options(s, p);
2118+
tcc_free(p);
2119+
if (x < 0)
2120+
return -1;
2121+
break;
2122+
}
21112123
case TCC_OPTION_E:
21122124
x = TCC_OUTPUT_PREPROCESS;
21132125
goto set_output_type;
@@ -2156,7 +2168,7 @@ PUB_FUNC int tcc_parse_args(TCCState *s, int *pargc, char ***pargv, int optind)
21562168
s->filetype = x | (s->filetype & ~AFF_TYPE_MASK);
21572169
break;
21582170
case TCC_OPTION_O:
2159-
s->optimize = atoi(optarg);
2171+
s->optimize = isnum(optarg[0]) ? optarg[0]-'0' : 1 /* -O -Os */;
21602172
break;
21612173
case TCC_OPTION_print_search_dirs:
21622174
x = OPT_PRINT_DIRS;

riscv64-gen.c

-5
Original file line numberDiff line numberDiff line change
@@ -628,11 +628,6 @@ ST_FUNC void gfunc_call(int nb_args)
628628
tempspace = (tempspace + 15) & -16;
629629
stack_add = stack_adj + tempspace;
630630

631-
/* fetch cpu flag before generating any code */
632-
if ((vtop->r & VT_VALMASK) == VT_CMP)
633-
gv(RC_INT);
634-
635-
636631
if (stack_add) {
637632
if (stack_add >= 0x800) {
638633
unsigned int bit11 = (((unsigned int)-stack_add) >> 11) & 1;

tcc.c

-3
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,6 @@ int main(int argc0, char **argv0)
293293
redo:
294294
argc = argc0, argv = argv0;
295295
s = s1 = tcc_new();
296-
#ifdef CONFIG_TCC_SWITCHES /* predefined options */
297-
tcc_set_options(s, CONFIG_TCC_SWITCHES);
298-
#endif
299296
opt = tcc_parse_args(s, &argc, &argv, 1);
300297
if (opt < 0)
301298
return 1;

tcc.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ struct TCCState {
803803
char *tcc_lib_path; /* CONFIG_TCCDIR or -B option */
804804
char *soname; /* as specified on the command line (-soname) */
805805
char *rpath; /* as specified on the command line (-Wl,-rpath=) */
806-
char *elfint; /* -Wl,-I on command line, LD_SO in environment, or DEFAULT_ELFINTERP(this) */
806+
char *elfint; /* -Wl,-I... on command line */
807807
char *elf_entryname; /* "_start" unless set */
808808
char *init_symbol; /* symbols to call at load-time (not used currently) */
809809
char *fini_symbol; /* symbols to call at unload-time (not used currently) */
@@ -987,9 +987,6 @@ struct TCCState {
987987
unsigned int total_bytes;
988988
unsigned int total_output[4];
989989

990-
/* option -dnum (for general development purposes) */
991-
int g_debug;
992-
993990
/* used by tcc_load_ldscript */
994991
int fd, cc;
995992

@@ -1193,6 +1190,7 @@ enum tcc_token {
11931190
ST_DATA struct TCCState *tcc_state;
11941191
ST_DATA void** stk_data;
11951192
ST_DATA int nb_stk_data;
1193+
ST_DATA int g_debug;
11961194

11971195
/* public functions currently used by the tcc main function */
11981196
ST_FUNC char *pstrcpy(char *buf, size_t buf_size, const char *s);

tccelf.c

+5-4
Original file line numberDiff line numberDiff line change
@@ -3247,11 +3247,12 @@ ST_FUNC int tcc_load_object_file(TCCState *s1,
32473247
} else {
32483248
s->data_offset += size;
32493249
}
3250-
/* align end of section */
3250+
#if defined TCC_TARGET_ARM || defined TCC_TARGET_ARM64 || defined TCC_TARGET_RISCV64
3251+
/* align code sections to instruction lenght */
32513252
/* This is needed if we compile a c file after this */
3252-
if (s == text_section || s == data_section || s == rodata_section ||
3253-
s == bss_section || s == common_section)
3254-
s->data_offset += -s->data_offset & (s->sh_addralign - 1);
3253+
if (s->sh_flags & SHF_EXECINSTR)
3254+
section_add(s, 0, 4);
3255+
#endif
32553256
next: ;
32563257
}
32573258

tccgen.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -6187,6 +6187,7 @@ ST_FUNC void unary(void)
61876187
}
61886188

61896189
next();
6190+
vcheck_cmp(); /* the generators don't like VT_CMP on vtop */
61906191
gfunc_call(nb_args);
61916192

61926193
if (ret_nregs < 0) {
@@ -8221,7 +8222,7 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
82218222
sec = rodata_section;
82228223
} else if (has_init) {
82238224
sec = data_section;
8224-
/*if (tcc_state->g_debug & 4)
8225+
/*if (g_debug & 4)
82258226
tcc_warning("rw data: %s", get_tok_str(v, 0));*/
82268227
} else if (tcc_state->nocommon)
82278228
sec = bss_section;

tccpe.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -1262,6 +1262,8 @@ static int pe_check_symbols(struct pe_info *pe)
12621262
const char *s, *p;
12631263

12641264
n = _imp_ = 0;
1265+
if (sym->st_other & ST_PE_IMPORT)
1266+
_imp_ = 1;
12651267
do {
12661268
s = pe_export_name(s1, sym);
12671269
if (n) {
@@ -1293,7 +1295,7 @@ static int pe_check_symbols(struct pe_info *pe)
12931295

12941296
if (type == STT_FUNC
12951297
/* symbols from assembler often have no type */
1296-
|| type == STT_NOTYPE) {
1298+
|| (type == STT_NOTYPE && 0 == _imp_)) {
12971299
unsigned offset = is->thk_offset;
12981300
if (offset) {
12991301
/* got aliased symbol, like stricmp and _stricmp */
@@ -1336,7 +1338,7 @@ static int pe_check_symbols(struct pe_info *pe)
13361338
sym->st_other &= ~ST_PE_EXPORT; /* do not export */
13371339

13381340
} else { /* STT_OBJECT */
1339-
if (0 == _imp_ && 0 == (sym->st_other & ST_PE_IMPORT))
1341+
if (0 == _imp_)
13401342
ret = tcc_error_noabort("symbol '%s' is missing __declspec(dllimport)", name);
13411343
/* original symbol will be patched later in pe_build_imports */
13421344
sym->st_value = is->iat_index; /* chain potential alias */
@@ -2015,7 +2017,7 @@ ST_FUNC int pe_output_file(TCCState *s1, const char *filename)
20152017
}
20162018
pe_free_imports(&pe);
20172019
#if PE_PRINT_SECTIONS
2018-
if (s1->g_debug & 8)
2020+
if (g_debug & 8)
20192021
pe_print_sections(s1, "tcc.log");
20202022
#endif
20212023
return s1->nb_errors ? -1 : 0;

tccpp.c

+13-12
Original file line numberDiff line numberDiff line change
@@ -1338,8 +1338,10 @@ ST_FUNC void skip_to_eol(int warn)
13381338
return;
13391339
if (warn)
13401340
tcc_warning("extra tokens after directive");
1341+
while (macro_stack)
1342+
end_macro();
13411343
file->buf_ptr = parse_line_comment(file->buf_ptr - 1);
1342-
tok = TOK_LINEFEED;
1344+
next_nomacro();
13431345
}
13441346

13451347
static CachedInclude *
@@ -1931,38 +1933,37 @@ ST_FUNC void preprocess(int is_bof)
19311933
case TOK_LINE:
19321934
parse_flags &= ~PARSE_FLAG_TOK_NUM;
19331935
next();
1934-
parse_flags |= PARSE_FLAG_TOK_NUM;
19351936
if (tok != TOK_PPNUM) {
19361937
_line_err:
19371938
tcc_error("wrong #line format");
19381939
}
1940+
c = 1;
19391941
goto _line_num;
19401942
case TOK_PPNUM:
19411943
if (parse_flags & PARSE_FLAG_ASM_FILE)
19421944
goto ignore;
1945+
c = 0; /* no error with extra tokens */
19431946
_line_num:
19441947
for (n = 0, q = tokc.str.data; *q; ++q) {
19451948
if (!isnum(*q))
19461949
goto _line_err;
19471950
n = n * 10 + *q - '0';
19481951
}
1949-
parse_flags &= ~PARSE_FLAG_TOK_STR;
1952+
parse_flags &= ~PARSE_FLAG_TOK_STR; /* don't parse escape sequences */
19501953
next();
1951-
parse_flags |= PARSE_FLAG_TOK_STR;
1952-
if (tok == TOK_PPSTR && tokc.str.data[0] == '"') {
1954+
if (tok != TOK_LINEFEED) {
1955+
if (tok != TOK_PPSTR || tokc.str.data[0] != '"')
1956+
goto _line_err;
19531957
tokc.str.data[tokc.str.size - 2] = 0;
19541958
tccpp_putfile(tokc.str.data + 1);
1955-
n--;
1956-
if (macro_ptr && *macro_ptr == 0)
1957-
macro_stack->save_line_num = n;
1959+
next();
1960+
/* skip optional level number & advance to next line */
1961+
skip_to_eol(c);
19581962
}
1959-
else if (tok != TOK_LINEFEED)
1960-
goto _line_err;
19611963
if (file->fd > 0)
19621964
total_lines += file->line_num - n;
1963-
file->line_ref += file->line_num - n;
19641965
file->line_num = n;
1965-
goto ignore; /* skip optional level number */
1966+
break;
19661967

19671968
case TOK_ERROR:
19681969
case TOK_WARNING:

tests/tests2/134_double_to_signed.c

+5
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,10 @@ int main() {
88
d = -2147483648.0;
99
printf("%d\n", (int)d);
1010

11+
#ifndef _WIN32
1112
printf("%llu\n", (unsigned long long)1e19);
13+
#else
14+
/* some msvc compiler won't compile tcc correctly in this ragard */
15+
printf("10000000000000000000\n");
16+
#endif
1217
}

0 commit comments

Comments
 (0)