| @@ -209,8 +209,8 @@ typedef struct AttributeDef { · 208 unmodified lines |
| 209 | 209 | #define IO_BUF_SIZE 8192 |
| 210 | 210 | |
| 211 | 211 | typedef struct BufferedFile { |
| 212 | | − unsigned char *buf_ptr; |
| 213 | | − unsigned char *buf_end; |
| 212 | + uint8_t *buf_ptr; |
| 213 | + uint8_t *buf_end; |
| 214 | 214 | int fd; |
| 215 | 215 | int line_num; /* current line number - here to simply code */ |
| 216 | 216 | int ifndef_macro; /* #ifndef macro / #endif search */ |
| @@ -1501,7 +1501,7 @@ BufferedFile *tcc_open(TCCState *s1, const char *filename) · 1284 unmodified lines |
| 1501 | 1501 | bf->buf_end = bf->buffer; |
| 1502 | 1502 | bf->buffer[0] = CH_EOB; /* put eob symbol */ |
| 1503 | 1503 | pstrcpy(bf->filename, sizeof(bf->filename), filename); |
| 1504 | | − bf->line_num = 0; |
| 1504 | + bf->line_num = 1; |
| 1505 | 1505 | bf->ifndef_macro = 0; |
| 1506 | 1506 | bf->ifdef_stack_ptr = s1->ifdef_stack_ptr; |
| 1507 | 1507 | // printf("opening '%s'\n", filename); |
| @@ -1515,8 +1515,8 @@ void tcc_close(BufferedFile *bf) · 7 unmodified lines |
| 1515 | 1515 | tcc_free(bf); |
| 1516 | 1516 | } |
| 1517 | 1517 | |
| 1518 | | −/* fill input buffer and return next char */ |
| 1519 | | −int tcc_getc_slow(BufferedFile *bf) |
| 1518 | +/* fill input buffer and peek next char */ |
| 1519 | +static int tcc_peekc_slow(BufferedFile *bf) |
| 1520 | 1520 | { |
| 1521 | 1521 | int len; |
| 1522 | 1522 | /* only tries to read if really end of buffer */ |
| @@ -1534,7 +1534,7 @@ int tcc_getc_slow(BufferedFile *bf) · 11 unmodified lines |
| 1534 | 1534 | *bf->buf_end = CH_EOB; |
| 1535 | 1535 | } |
| 1536 | 1536 | if (bf->buf_ptr < bf->buf_end) { |
| 1537 | | − return *bf->buf_ptr++; |
| 1537 | + return bf->buf_ptr[0]; |
| 1538 | 1538 | } else { |
| 1539 | 1539 | bf->buf_ptr = bf->buf_end; |
| 1540 | 1540 | return CH_EOF; |
| @@ -1545,15 +1545,15 @@ int tcc_getc_slow(BufferedFile *bf) · 4 unmodified lines |
| 1545 | 1545 | void handle_eob(void) |
| 1546 | 1546 | { |
| 1547 | 1547 | /* no need to do anything if not at EOB */ |
| 1548 | | − if (file->buf_ptr <= file->buf_end) |
| 1548 | + if (file->buf_ptr < file->buf_end) |
| 1549 | 1549 | return; |
| 1550 | | − ch = tcc_getc_slow(file); |
| 1550 | + ch = tcc_peekc_slow(file); |
| 1551 | 1551 | } |
| 1552 | 1552 | |
| 1553 | 1553 | /* read next char from current input file and handle end of input buffer */ |
| 1554 | 1554 | static inline void inp(void) |
| 1555 | 1555 | { |
| 1556 | | − ch = *(file->buf_ptr++); |
| 1556 | + ch = *(++(file->buf_ptr)); |
| 1557 | 1557 | /* end of buffer/file handling */ |
| 1558 | 1558 | if (ch == CH_EOB) |
| 1559 | 1559 | handle_eob(); |
| @@ -1596,51 +1596,87 @@ static void parse_line_comment(void) · 36 unmodified lines |
| 1596 | 1596 | /* single line C++ comments */ |
| 1597 | 1597 | /* XXX: accept '\\\n' ? */ |
| 1598 | 1598 | inp(); |
| 1599 | | − while (ch != '\n' && ch != TOK_EOF) |
| 1599 | + while (ch != '\n' && ch != CH_EOF) |
| 1600 | 1600 | inp(); |
| 1601 | 1601 | } |
| 1602 | 1602 | |
| 1603 | 1603 | static void parse_comment(void) |
| 1604 | 1604 | { |
| 1605 | + uint8_t *p; |
| 1606 | + int c; |
| 1607 | + |
| 1605 | 1608 | /* C comments */ |
| 1606 | 1609 | minp(); |
| 1610 | + p = file->buf_ptr; |
| 1607 | 1611 | for(;;) { |
| 1608 | 1612 | /* fast skip loop */ |
| 1609 | | − while (ch != '\n' && ch != '*' && ch != TOK_EOF) |
| 1610 | | − inp(); |
| 1613 | + for(;;) { |
| 1614 | + c = *p; |
| 1615 | + if (c == '\n' || c == '*' || c == '\\') |
| 1616 | + break; |
| 1617 | + p++; |
| 1618 | + c = *p; |
| 1619 | + if (c == '\n' || c == '*' || c == '\\') |
| 1620 | + break; |
| 1621 | + p++; |
| 1622 | + } |
| 1611 | 1623 | /* now we can handle all the cases */ |
| 1612 | | − if (ch == '\n') { |
| 1624 | + if (c == '\n') { |
| 1613 | 1625 | file->line_num++; |
| 1614 | | − inp(); |
| 1615 | | − } else if (ch == '*') { |
| 1626 | + p++; |
| 1627 | + } else if (c == '*') { |
| 1628 | + p++; |
| 1616 | 1629 | for(;;) { |
| 1617 | | − inp(); |
| 1618 | | − if (ch == '/') { |
| 1630 | + c = *p; |
| 1631 | + if (c == '/') { |
| 1619 | 1632 | goto end_of_comment; |
| 1620 | | − } else if (ch == '\\') { |
| 1621 | | − inp(); |
| 1622 | | − if (ch == '\n') { |
| 1633 | + } else if (c == '\\') { |
| 1634 | + if (p >= file->buf_end) { |
| 1635 | + file->buf_ptr = p; |
| 1636 | + handle_eob(); |
| 1637 | + p = file->buf_ptr; |
| 1638 | + if (p >= file->buf_end) |
| 1639 | + goto eof_found; |
| 1640 | + continue; |
| 1641 | + } |
| 1642 | + p++; |
| 1643 | + c = *p; |
| 1644 | + if (c == '\n') { |
| 1623 | 1645 | file->line_num++; |
| 1624 | | − inp(); |
| 1625 | | − } else if (ch == '\r') { |
| 1626 | | − inp(); |
| 1627 | | − if (ch != '\n') |
| 1646 | + p++; |
| 1647 | + } else if (c == '\r') { |
| 1648 | + p++; |
| 1649 | + c = *p; |
| 1650 | + if (c != '\n') |
| 1628 | 1651 | break; |
| 1629 | 1652 | file->line_num++; |
| 1630 | | − inp(); |
| 1653 | + p++; |
| 1631 | 1654 | } else { |
| 1632 | 1655 | break; |
| 1633 | 1656 | } |
| 1634 | | − } else if (ch != '*') { |
| 1657 | + } else if (c == '*') { |
| 1658 | + p++; |
| 1659 | + } else { |
| 1635 | 1660 | break; |
| 1636 | 1661 | } |
| 1637 | 1662 | } |
| 1663 | + } else if (p >= file->buf_end) { |
| 1664 | + file->buf_ptr = p; |
| 1665 | + handle_eob(); |
| 1666 | + p = file->buf_ptr; |
| 1667 | + if (p >= file->buf_end) { |
| 1668 | + eof_found: |
| 1669 | + error("unexpected end of file in comment"); |
| 1670 | + } |
| 1638 | 1671 | } else { |
| 1639 | | − error("unexpected end of file in comment"); |
| 1672 | + /* stray */ |
| 1673 | + p++; |
| 1640 | 1674 | } |
| 1641 | 1675 | } |
| 1642 | 1676 | end_of_comment: |
| 1643 | | − inp(); |
| 1677 | + p++; |
| 1678 | + file->buf_ptr = p; |
| 1679 | + ch = *p; |
| 1644 | 1680 | } |
| 1645 | 1681 | |
| 1646 | 1682 | #define cinp minp |
| @@ -2127,6 +2163,7 @@ static void preprocess(int is_bof) · 480 unmodified lines |
| 2127 | 2163 | } else if (ch == '\"') { |
| 2128 | 2164 | c = ch; |
| 2129 | 2165 | read_name: |
| 2166 | + /* XXX: better stray handling */ |
| 2130 | 2167 | minp(); |
| 2131 | 2168 | q = buf; |
| 2132 | 2169 | while (ch != c && ch != '\n' && ch != CH_EOF) { |
| @@ -2135,6 +2172,7 @@ static void preprocess(int is_bof) · 2 unmodified lines |
| 2135 | 2172 | minp(); |
| 2136 | 2173 | } |
| 2137 | 2174 | *q = '\0'; |
| 2175 | + minp(); |
| 2138 | 2176 | #if 0 |
| 2139 | 2177 | /* eat all spaces and comments after include */ |
| 2140 | 2178 | /* XXX: slightly incorrect */ |
| @@ -2172,7 +2210,6 @@ static void preprocess(int is_bof) · 31 unmodified lines |
| 2172 | 2210 | } |
| 2173 | 2211 | } |
| 2174 | 2212 | |
| 2175 | | − ch = '\n'; |
| 2176 | 2213 | e = search_cached_include(s1, c, buf); |
| 2177 | 2214 | if (e && define_find(e->ifndef_macro)) { |
| 2178 | 2215 | /* no need to parse the include because the 'ifndef macro' |
| @@ -2229,7 +2266,8 @@ static void preprocess(int is_bof) · 50 unmodified lines |
| 2229 | 2266 | if (do_debug) { |
| 2230 | 2267 | put_stabs(file->filename, N_BINCL, 0, 0, 0); |
| 2231 | 2268 | } |
| 2232 | | − tok_flags |= TOK_FLAG_BOF; |
| 2269 | + tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL; |
| 2270 | + ch = file->buf_ptr[0]; |
| 2233 | 2271 | goto the_end; |
| 2234 | 2272 | } |
| 2235 | 2273 | break; |
| @@ -2761,6 +2799,10 @@ static inline void next_nomacro1(void) · 525 unmodified lines |
| 2761 | 2799 | goto redo_no_start; |
| 2762 | 2800 | |
| 2763 | 2801 | case '\\': |
| 2802 | + /* first look if it is in fact an end of buffer */ |
| 2803 | + handle_eob(); |
| 2804 | + if (ch != '\\') |
| 2805 | + goto redo_no_start; |
| 2764 | 2806 | handle_stray(); |
| 2765 | 2807 | goto redo_no_start; |
| 2766 | 2808 | |
| @@ -7641,7 +7683,7 @@ static int tcc_compile(TCCState *s1) · 4874 unmodified lines |
| 7641 | 7683 | s1->nb_errors = 0; |
| 7642 | 7684 | s1->error_set_jmp_enabled = 1; |
| 7643 | 7685 | |
| 7644 | | − ch = '\n'; /* XXX: suppress that*/ |
| 7686 | + ch = file->buf_ptr[0]; |
| 7645 | 7687 | tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF; |
| 7646 | 7688 | next(); |
| 7647 | 7689 | decl(VT_CONST); |
| @@ -7719,7 +7761,7 @@ void tcc_define_symbol(TCCState *s1, const char *sym, const char *value) · 71 unmodified lines |
| 7719 | 7761 | s1->include_stack_ptr = s1->include_stack; |
| 7720 | 7762 | |
| 7721 | 7763 | /* parse with define parser */ |
| 7722 | | − ch = '\n'; /* needed to parse correctly first preprocessor command */ |
| 7764 | + ch = file->buf_ptr[0]; |
| 7723 | 7765 | next_nomacro(); |
| 7724 | 7766 | parse_define(); |
| 7725 | 7767 | file = NULL; |