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
209209 #define IO_BUF_SIZE 8192
210210
211211 typedef struct BufferedFile {
212− unsigned char *buf_ptr;
213− unsigned char *buf_end;
212+ uint8_t *buf_ptr;
213+ uint8_t *buf_end;
214214 int fd;
215215 int line_num; /* current line number - here to simply code */
216216 int ifndef_macro; /* #ifndef macro / #endif search */
@@ -1501,7 +1501,7 @@ BufferedFile *tcc_open(TCCState *s1, const char *filename) · 1284 unmodified lines
15011501 bf->buf_end = bf->buffer;
15021502 bf->buffer[0] = CH_EOB; /* put eob symbol */
15031503 pstrcpy(bf->filename, sizeof(bf->filename), filename);
1504− bf->line_num = 0;
1504+ bf->line_num = 1;
15051505 bf->ifndef_macro = 0;
15061506 bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
15071507 // printf("opening '%s'\n", filename);
@@ -1515,8 +1515,8 @@ void tcc_close(BufferedFile *bf) · 7 unmodified lines
15151515 tcc_free(bf);
15161516 }
15171517
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)
15201520 {
15211521 int len;
15221522 /* only tries to read if really end of buffer */
@@ -1534,7 +1534,7 @@ int tcc_getc_slow(BufferedFile *bf) · 11 unmodified lines
15341534 *bf->buf_end = CH_EOB;
15351535 }
15361536 if (bf->buf_ptr < bf->buf_end) {
1537− return *bf->buf_ptr++;
1537+ return bf->buf_ptr[0];
15381538 } else {
15391539 bf->buf_ptr = bf->buf_end;
15401540 return CH_EOF;
@@ -1545,15 +1545,15 @@ int tcc_getc_slow(BufferedFile *bf) · 4 unmodified lines
15451545 void handle_eob(void)
15461546 {
15471547 /* 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)
15491549 return;
1550− ch = tcc_getc_slow(file);
1550+ ch = tcc_peekc_slow(file);
15511551 }
15521552
15531553 /* read next char from current input file and handle end of input buffer */
15541554 static inline void inp(void)
15551555 {
1556− ch = *(file->buf_ptr++);
1556+ ch = *(++(file->buf_ptr));
15571557 /* end of buffer/file handling */
15581558 if (ch == CH_EOB)
15591559 handle_eob();
@@ -1596,51 +1596,87 @@ static void parse_line_comment(void) · 36 unmodified lines
15961596 /* single line C++ comments */
15971597 /* XXX: accept '\\\n' ? */
15981598 inp();
1599− while (ch != '\n' && ch != TOK_EOF)
1599+ while (ch != '\n' && ch != CH_EOF)
16001600 inp();
16011601 }
16021602
16031603 static void parse_comment(void)
16041604 {
1605+ uint8_t *p;
1606+ int c;
1607+
16051608 /* C comments */
16061609 minp();
1610+ p = file->buf_ptr;
16071611 for(;;) {
16081612 /* 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+ }
16111623 /* now we can handle all the cases */
1612− if (ch == '\n') {
1624+ if (c == '\n') {
16131625 file->line_num++;
1614− inp();
1615− } else if (ch == '*') {
1626+ p++;
1627+ } else if (c == '*') {
1628+ p++;
16161629 for(;;) {
1617− inp();
1618− if (ch == '/') {
1630+ c = *p;
1631+ if (c == '/') {
16191632 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') {
16231645 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')
16281651 break;
16291652 file->line_num++;
1630− inp();
1653+ p++;
16311654 } else {
16321655 break;
16331656 }
1634− } else if (ch != '*') {
1657+ } else if (c == '*') {
1658+ p++;
1659+ } else {
16351660 break;
16361661 }
16371662 }
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+ }
16381671 } else {
1639− error("unexpected end of file in comment");
1672+ /* stray */
1673+ p++;
16401674 }
16411675 }
16421676 end_of_comment:
1643− inp();
1677+ p++;
1678+ file->buf_ptr = p;
1679+ ch = *p;
16441680 }
16451681
16461682 #define cinp minp
@@ -2127,6 +2163,7 @@ static void preprocess(int is_bof) · 480 unmodified lines
21272163 } else if (ch == '\"') {
21282164 c = ch;
21292165 read_name:
2166+ /* XXX: better stray handling */
21302167 minp();
21312168 q = buf;
21322169 while (ch != c && ch != '\n' && ch != CH_EOF) {
@@ -2135,6 +2172,7 @@ static void preprocess(int is_bof) · 2 unmodified lines
21352172 minp();
21362173 }
21372174 *q = '\0';
2175+ minp();
21382176 #if 0
21392177 /* eat all spaces and comments after include */
21402178 /* XXX: slightly incorrect */
@@ -2172,7 +2210,6 @@ static void preprocess(int is_bof) · 31 unmodified lines
21722210 }
21732211 }
21742212
2175− ch = '\n';
21762213 e = search_cached_include(s1, c, buf);
21772214 if (e && define_find(e->ifndef_macro)) {
21782215 /* no need to parse the include because the 'ifndef macro'
@@ -2229,7 +2266,8 @@ static void preprocess(int is_bof) · 50 unmodified lines
22292266 if (do_debug) {
22302267 put_stabs(file->filename, N_BINCL, 0, 0, 0);
22312268 }
2232− tok_flags |= TOK_FLAG_BOF;
2269+ tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
2270+ ch = file->buf_ptr[0];
22332271 goto the_end;
22342272 }
22352273 break;
@@ -2761,6 +2799,10 @@ static inline void next_nomacro1(void) · 525 unmodified lines
27612799 goto redo_no_start;
27622800
27632801 case '\\':
2802+ /* first look if it is in fact an end of buffer */
2803+ handle_eob();
2804+ if (ch != '\\')
2805+ goto redo_no_start;
27642806 handle_stray();
27652807 goto redo_no_start;
27662808
@@ -7641,7 +7683,7 @@ static int tcc_compile(TCCState *s1) · 4874 unmodified lines
76417683 s1->nb_errors = 0;
76427684 s1->error_set_jmp_enabled = 1;
76437685
7644− ch = '\n'; /* XXX: suppress that*/
7686+ ch = file->buf_ptr[0];
76457687 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
76467688 next();
76477689 decl(VT_CONST);
@@ -7719,7 +7761,7 @@ void tcc_define_symbol(TCCState *s1, const char *sym, const char *value) · 71 unmodified lines
77197761 s1->include_stack_ptr = s1->include_stack;
77207762
77217763 /* parse with define parser */
7722− ch = '\n'; /* needed to parse correctly first preprocessor command */
7764+ ch = file->buf_ptr[0];
77237765 next_nomacro();
77247766 parse_define();
77257767 file = NULL;
tccelf.c+2−1
@@ -1890,7 +1890,8 @@ static int tcc_load_ldscript(TCCState *s1) · 1889 unmodified lines
18901890 char filename[1024];
18911891 int t;
18921892
1893− inp();
1893+ ch = file->buf_ptr[0];
1894+ handle_eob();
18941895 for(;;) {
18951896 t = ld_next(s1, cmd, sizeof(cmd));
18961897 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