| @@ -321,6 +321,7 @@ static int tok_flags; · 320 unmodified lines |
| 321 | 321 | #define TOK_FLAG_BOL 0x0001 /* beginning of line before */ |
| 322 | 322 | #define TOK_FLAG_BOF 0x0002 /* beginning of file before */ |
| 323 | 323 | #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */ |
| 324 | +#define TOK_FLAG_EOF 0x0008 /* end of file */ |
| 324 | 325 | |
| 325 | 326 | static int *macro_ptr, *macro_ptr_allocated; |
| 326 | 327 | static int *unget_saved_macro_ptr; |
| @@ -1032,6 +1033,32 @@ static int strstart(const char *str, const char *val, const char **ptr) · 705 unmodified lines |
| 1032 | 1033 | return 1; |
| 1033 | 1034 | } |
| 1034 | 1035 | |
| 1036 | +#ifdef WIN32 |
| 1037 | +char *normalize_slashes(char *path) |
| 1038 | +{ |
| 1039 | + char *p; |
| 1040 | + for (p = path; *p; ++p) |
| 1041 | + if (*p == '\\') |
| 1042 | + *p = '/'; |
| 1043 | + return path; |
| 1044 | +} |
| 1045 | + |
| 1046 | +char *w32_tcc_lib_path(void) |
| 1047 | +{ |
| 1048 | + /* on win32, we suppose the lib and includes are at the location |
| 1049 | + of 'tcc.exe' */ |
| 1050 | + char path[1024], *p; |
| 1051 | + GetModuleFileNameA(NULL, path, sizeof path); |
| 1052 | + p = tcc_basename(normalize_slashes(strlwr(path))); |
| 1053 | + if (p - 5 > path && 0 == strncmp(p - 5, "/bin/", 5)) |
| 1054 | + p -= 5; |
| 1055 | + else if (p > path) |
| 1056 | + p--; |
| 1057 | + *p = 0; |
| 1058 | + return strdup(path); |
| 1059 | +} |
| 1060 | +#endif |
| 1061 | + |
| 1035 | 1062 | /* memory management */ |
| 1036 | 1063 | #ifdef MEM_DEBUG |
| 1037 | 1064 | int mem_cur_size; |
| @@ -1844,7 +1871,6 @@ BufferedFile *tcc_open(TCCState *s1, const char *filename) · 806 unmodified lines |
| 1844 | 1871 | { |
| 1845 | 1872 | int fd; |
| 1846 | 1873 | BufferedFile *bf; |
| 1847 | | − int i, len; |
| 1848 | 1874 | |
| 1849 | 1875 | fd = open(filename, O_RDONLY | O_BINARY); |
| 1850 | 1876 | if (fd < 0) |
| @@ -1859,10 +1885,7 @@ BufferedFile *tcc_open(TCCState *s1, const char *filename) · 8 unmodified lines |
| 1859 | 1885 | bf->buf_end = bf->buffer; |
| 1860 | 1886 | bf->buffer[0] = CH_EOB; /* put eob symbol */ |
| 1861 | 1887 | pstrcpy(bf->filename, sizeof(bf->filename), filename); |
| 1862 | | − len = strlen(bf->filename); |
| 1863 | | − for (i = 0; i < len; i++) |
| 1864 | | − if (bf->filename[i] == '\\') |
| 1865 | | − bf->filename[i] = '/'; |
| 1888 | + normalize_slashes(bf->filename); |
| 1866 | 1889 | bf->line_num = 1; |
| 1867 | 1890 | bf->ifndef_macro = 0; |
| 1868 | 1891 | bf->ifdef_stack_ptr = s1->ifdef_stack_ptr; |
| @@ -3540,13 +3563,17 @@ static inline void next_nomacro1(void) · 1671 unmodified lines |
| 3540 | 3563 | parse_eof: |
| 3541 | 3564 | { |
| 3542 | 3565 | TCCState *s1 = tcc_state; |
| 3543 | | − if (parse_flags & PARSE_FLAG_LINEFEED) { |
| 3566 | + if ((parse_flags & PARSE_FLAG_LINEFEED) |
| 3567 | + && !(tok_flags & TOK_FLAG_EOF)) { |
| 3568 | + tok_flags |= TOK_FLAG_EOF; |
| 3544 | 3569 | tok = TOK_LINEFEED; |
| 3570 | + goto keep_tok_flags; |
| 3545 | 3571 | } else if (s1->include_stack_ptr == s1->include_stack || |
| 3546 | 3572 | !(parse_flags & PARSE_FLAG_PREPROCESS)) { |
| 3547 | 3573 | /* no include left : end of file. */ |
| 3548 | 3574 | tok = TOK_EOF; |
| 3549 | 3575 | } else { |
| 3576 | + tok_flags &= ~TOK_FLAG_EOF; |
| 3550 | 3577 | /* pop include file */ |
| 3551 | 3578 | |
| 3552 | 3579 | /* test if previous '#endif' was after a #ifdef at |
| @@ -3574,15 +3601,13 @@ static inline void next_nomacro1(void) · 21 unmodified lines |
| 3574 | 3601 | break; |
| 3575 | 3602 | |
| 3576 | 3603 | case '\n': |
| 3577 | | − if (parse_flags & PARSE_FLAG_LINEFEED) { |
| 3578 | | − tok = TOK_LINEFEED; |
| 3579 | | − } else { |
| 3580 | | − file->line_num++; |
| 3581 | | − tok_flags |= TOK_FLAG_BOL; |
| 3582 | | − p++; |
| 3604 | + file->line_num++; |
| 3605 | + tok_flags |= TOK_FLAG_BOL; |
| 3606 | + p++; |
| 3607 | + if (0 == (parse_flags & PARSE_FLAG_LINEFEED)) |
| 3583 | 3608 | goto redo_no_start; |
| 3584 | | − } |
| 3585 | | − break; |
| 3609 | + tok = TOK_LINEFEED; |
| 3610 | + goto keep_tok_flags; |
| 3586 | 3611 | |
| 3587 | 3612 | case '#': |
| 3588 | 3613 | /* XXX: simplify */ |
| @@ -3912,8 +3937,9 @@ static inline void next_nomacro1(void) · 323 unmodified lines |
| 3912 | 3937 | error("unrecognized character \\x%02x", c); |
| 3913 | 3938 | break; |
| 3914 | 3939 | } |
| 3915 | | − file->buf_ptr = p; |
| 3916 | 3940 | tok_flags = 0; |
| 3941 | +keep_tok_flags: |
| 3942 | + file->buf_ptr = p; |
| 3917 | 3943 | #if defined(PARSE_DEBUG) |
| 3918 | 3944 | printf("token = %s\n", get_tok_str(tok, &tokc)); |
| 3919 | 3945 | #endif |
| @@ -4140,7 +4166,8 @@ static int macro_subst_tok(TokenString *tok_str, · 220 unmodified lines |
| 4140 | 4166 | parlevel++; |
| 4141 | 4167 | else if (tok == ')') |
| 4142 | 4168 | parlevel--; |
| 4143 | | − tok_str_add2(&str, tok, &tokc); |
| 4169 | + if (tok != TOK_LINEFEED) |
| 4170 | + tok_str_add2(&str, tok, &tokc); |
| 4144 | 4171 | next_nomacro(); |
| 4145 | 4172 | } |
| 4146 | 4173 | tok_str_add(&str, 0); |
| @@ -8188,9 +8215,6 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, · 4041 unmodified lines |
| 8188 | 8215 | int v1, v2; |
| 8189 | 8216 | if (!case_sym) |
| 8190 | 8217 | expect("switch"); |
| 8191 | | − /* since a case is like a label, we must skip it with a jmp */ |
| 8192 | | − b = gjmp(0); |
| 8193 | | − next_case: |
| 8194 | 8218 | next(); |
| 8195 | 8219 | v1 = expr_const(); |
| 8196 | 8220 | v2 = v1; |
| @@ -8200,26 +8224,24 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, · 3 unmodified lines |
| 8200 | 8224 | if (v2 < v1) |
| 8201 | 8225 | warning("empty case range"); |
| 8202 | 8226 | } |
| 8227 | + /* since a case is like a label, we must skip it with a jmp */ |
| 8228 | + b = gjmp(0); |
| 8203 | 8229 | gsym(*case_sym); |
| 8204 | 8230 | vseti(case_reg, 0); |
| 8205 | 8231 | vpushi(v1); |
| 8206 | 8232 | if (v1 == v2) { |
| 8207 | 8233 | gen_op(TOK_EQ); |
| 8208 | | − *case_sym = 0; |
| 8234 | + *case_sym = gtst(1, 0); |
| 8209 | 8235 | } else { |
| 8210 | 8236 | gen_op(TOK_GE); |
| 8211 | 8237 | *case_sym = gtst(1, 0); |
| 8212 | 8238 | vseti(case_reg, 0); |
| 8213 | 8239 | vpushi(v2); |
| 8214 | 8240 | gen_op(TOK_LE); |
| 8241 | + *case_sym = gtst(1, *case_sym); |
| 8215 | 8242 | } |
| 8216 | | − skip(':'); |
| 8217 | | − if (tok == TOK_CASE) { |
| 8218 | | − b = gtst(0, b); |
| 8219 | | − goto next_case; |
| 8220 | | − } |
| 8221 | | − *case_sym = gtst(1, *case_sym); |
| 8222 | 8243 | gsym(b); |
| 8244 | + skip(':'); |
| 8223 | 8245 | is_expr = 0; |
| 8224 | 8246 | goto block_after_label; |
| 8225 | 8247 | } else |
| @@ -9015,6 +9037,8 @@ static void func_decl_list(Sym *func_sym) · 789 unmodified lines |
| 9015 | 9037 | 'cur_text_section' */ |
| 9016 | 9038 | static void gen_function(Sym *sym) |
| 9017 | 9039 | { |
| 9040 | + int saved_nocode_wanted = nocode_wanted; |
| 9041 | + nocode_wanted = 0; |
| 9018 | 9042 | ind = cur_text_section->data_offset; |
| 9019 | 9043 | /* NOTE: we patch the symbol size later */ |
| 9020 | 9044 | put_extern_sym(sym, cur_text_section, ind, 0); |
| @@ -9043,6 +9067,7 @@ static void gen_function(Sym *sym) · 22 unmodified lines |
| 9043 | 9067 | funcname = ""; /* for safety */ |
| 9044 | 9068 | func_vt.t = VT_VOID; /* for safety */ |
| 9045 | 9069 | ind = 0; /* for safety */ |
| 9070 | + nocode_wanted = saved_nocode_wanted; |
| 9046 | 9071 | } |
| 9047 | 9072 | |
| 9048 | 9073 | static void gen_inline_functions(void) |
| @@ -9350,6 +9375,7 @@ static int tcc_compile(TCCState *s1) · 301 unmodified lines |
| 9350 | 9375 | #endif |
| 9351 | 9376 | |
| 9352 | 9377 | define_start = define_stack; |
| 9378 | + nocode_wanted = 1; |
| 9353 | 9379 | |
| 9354 | 9380 | if (setjmp(s1->error_jmp_buf) == 0) { |
| 9355 | 9381 | s1->nb_errors = 0; |
| @@ -9400,26 +9426,19 @@ static int tcc_preprocess(TCCState *s1) · 44 unmodified lines |
| 9400 | 9426 | last_is_space = 1; |
| 9401 | 9427 | next(); |
| 9402 | 9428 | for(;;) { |
| 9403 | | − if (tok == TOK_EOF) |
| 9429 | + if (tok == TOK_EOF) { |
| 9404 | 9430 | break; |
| 9405 | | − if (!last_is_space) { |
| 9406 | | − fputc(' ', s1->outfile); |
| 9407 | | − } |
| 9408 | | − fputs(get_tok_str(tok, &tokc), s1->outfile); |
| 9409 | | − if (tok == TOK_LINEFEED) { |
| 9431 | + } else if (tok == TOK_LINEFEED) { |
| 9410 | 9432 | last_is_space = 1; |
| 9411 | | − /* XXX: suppress that hack */ |
| 9412 | | − parse_flags &= ~PARSE_FLAG_LINEFEED; |
| 9413 | | − next(); |
| 9414 | | − parse_flags |= PARSE_FLAG_LINEFEED; |
| 9415 | | − } else { |
| 9433 | + } else { |
| 9434 | + if (!last_is_space) |
| 9435 | + fputc(' ', s1->outfile); |
| 9416 | 9436 | last_is_space = 0; |
| 9417 | | − next(); |
| 9418 | 9437 | } |
| 9438 | + fputs(get_tok_str(tok, &tokc), s1->outfile); |
| 9439 | + next(); |
| 9419 | 9440 | } |
| 9420 | | − |
| 9421 | 9441 | free_defines(define_start); |
| 9422 | | − |
| 9423 | 9442 | return 0; |
| 9424 | 9443 | } |
| 9425 | 9444 | |
| @@ -10380,17 +10399,15 @@ int tcc_set_flag(TCCState *s, const char *flag_name, int value) · 954 unmodified lines |
| 10380 | 10399 | /* extract the basename of a file */ |
| 10381 | 10400 | static char *tcc_basename(const char *name) |
| 10382 | 10401 | { |
| 10383 | | − const char *p; |
| 10384 | | − p = strrchr(name, '/'); |
| 10402 | + char *p = strchr(name, 0); |
| 10403 | + while (p > name |
| 10404 | + && p[-1] != '/' |
| 10385 | 10405 | #ifdef WIN32 |
| 10386 | | − if (!p) |
| 10387 | | − p = strrchr(name, '\\'); |
| 10388 | | −#endif |
| 10389 | | − if (!p) |
| 10390 | | − p = name; |
| 10391 | | − else |
| 10392 | | − p++; |
| 10393 | | − return (char*)p; |
| 10406 | + && p[-1] != '\\' |
| 10407 | +#endif |
| 10408 | + ) |
| 10409 | + --p; |
| 10410 | + return p; |
| 10394 | 10411 | } |
| 10395 | 10412 | |
| 10396 | 10413 | #if !defined(LIBTCC) |
| @@ -10774,22 +10791,7 @@ int main(int argc, char **argv) · 377 unmodified lines |
| 10774 | 10791 | int64_t start_time = 0; |
| 10775 | 10792 | |
| 10776 | 10793 | #ifdef WIN32 |
| 10777 | | − /* on win32, we suppose the lib and includes are at the location |
| 10778 | | − of 'tcc.exe' */ |
| 10779 | | − { |
| 10780 | | − static char path[1024]; |
| 10781 | | − char *p, *d; |
| 10782 | | − |
| 10783 | | − GetModuleFileNameA(NULL, path, sizeof path); |
| 10784 | | − p = d = strlwr(path); |
| 10785 | | − while (*d) |
| 10786 | | − { |
| 10787 | | − if (*d == '\\') *d = '/', p = d; |
| 10788 | | − ++d; |
| 10789 | | − } |
| 10790 | | − *p = '\0'; |
| 10791 | | − tcc_lib_path = path; |
| 10792 | | − } |
| 10794 | + tcc_lib_path = w32_tcc_lib_path(); |
| 10793 | 10795 | #endif |
| 10794 | 10796 | |
| 10795 | 10797 | s = tcc_new(); |