History

prepared parser for direct use of file->buf_ptr - faster comment parsing

bellard committed 23 years ago

Commit b81d4ba1 parent cdcfed92 files · +76 −33Browse files

Unified · Split

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

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