From: Peter Johnson Date: Sat, 28 Oct 2006 20:34:03 +0000 (-0000) Subject: Make common scanner fill function, as we're using it in two places already. X-Git-Tag: v0.6.0~113 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1b8ca788489e34787b679e2c22ad5f848458e2ab;p=yasm Make common scanner fill function, as we're using it in two places already. Actually, GAS parser doesn't use this yet, as it still contains the rept functionality that will move to GAS preproc at some point. When GAS preproc implements rept, we can switch to the common implementation. svn path=/trunk/yasm/; revision=1668 --- diff --git a/libyasm/file.c b/libyasm/file.c index c605137f..94b61c6a 100644 --- a/libyasm/file.c +++ b/libyasm/file.c @@ -44,6 +44,77 @@ #include "file.h" +#define BSIZE 8192 /* Fill block size */ + + +void +yasm_scanner_initialize(yasm_scanner *s) +{ + s->bot = NULL; + s->tok = NULL; + s->ptr = NULL; + s->cur = NULL; + s->pos = NULL; + s->lim = NULL; + s->top = NULL; + s->eof = NULL; + s->tchar = 0; + s->tline = 0; + s->cline = 1; +} + +void +yasm_scanner_delete(yasm_scanner *s) +{ + if (s->bot) { + yasm_xfree(s->bot); + s->bot = NULL; + } +} + +int +yasm_fill_helper(yasm_scanner *s, unsigned char **cursor, + size_t (*input_func) (void *d, unsigned char *buf, + size_t max), + void *input_func_data) +{ + size_t cnt; + int first = 0; + + if (s->eof) + return 0; + + cnt = s->tok - s->bot; + if (cnt > 0) { + memmove(s->bot, s->tok, (size_t)(s->lim - s->tok)); + s->tok = s->bot; + s->ptr -= cnt; + *cursor -= cnt; + s->pos -= cnt; + s->lim -= cnt; + } + if (!s->bot) + first = 1; + if ((s->top - s->lim) < BSIZE) { + unsigned char *buf = yasm_xmalloc((size_t)(s->lim - s->bot) + BSIZE); + memcpy(buf, s->tok, (size_t)(s->lim - s->tok)); + s->tok = buf; + s->ptr = &buf[s->ptr - s->bot]; + *cursor = &buf[*cursor - s->bot]; + s->pos = &buf[s->pos - s->bot]; + s->lim = &buf[s->lim - s->bot]; + s->top = &s->lim[BSIZE]; + if (s->bot) + yasm_xfree(s->bot); + s->bot = buf; + } + if ((cnt = input_func(input_func_data, s->lim, BSIZE)) == 0) { + s->eof = &s->lim[cnt]; + *s->eof++ = '\n'; + } + s->lim += cnt; + return first; +} size_t yasm__splitpath_unix(const char *path, /*@out@*/ const char **tail) diff --git a/libyasm/file.h b/libyasm/file.h index 2e0e5953..594f5d0c 100644 --- a/libyasm/file.h +++ b/libyasm/file.h @@ -34,6 +34,36 @@ #ifndef YASM_FILE_H #define YASM_FILE_H +/** Re2c scanner state. */ +typedef struct yasm_scanner { + unsigned char *bot, *tok, *ptr, *cur, *pos, *lim, *top, *eof; + unsigned int tchar, tline, cline; +} yasm_scanner; + +/** Initialize scanner state. + * \param scanner Re2c scanner state + */ +void yasm_scanner_initialize(yasm_scanner *scanner); + +/** Frees any memory used by scanner state; does not free state itself. + * \param scanner Re2c scanner state + */ +void yasm_scanner_delete(yasm_scanner *scanner); + +/** Fill a scanner state structure with data coming from an input function. + * \param scanner Re2c scanner state + * \param cursor Re2c scan cursor + * \param input_func Input function to read data; takes buffer and maximum + * number of bytes, returns number of bytes read. + * \param input_func_data Data to pass as the first parameter to input_func + * \return 1 if this was the first time this function was called on this + * scanner state, 0 otherwise. + */ +int yasm_fill_helper + (yasm_scanner *scanner, unsigned char **cursor, + size_t (*input_func) (void *d, unsigned char *buf, size_t max), + void *input_func_data); + /** Split a UNIX pathname into head (directory) and tail (base filename) * portions. * \internal diff --git a/modules/parsers/gas/gas-parser.c b/modules/parsers/gas/gas-parser.c index f70e70ab..a6c9574a 100644 --- a/modules/parsers/gas/gas-parser.c +++ b/modules/parsers/gas/gas-parser.c @@ -66,17 +66,7 @@ gas_parser_do_parse(yasm_object *object, yasm_preproc *pp, yasm_arch *a, parser_gas.save_last = 0; /* initialize scanner structure */ - parser_gas.s.bot = NULL; - parser_gas.s.tok = NULL; - parser_gas.s.ptr = NULL; - parser_gas.s.cur = NULL; - parser_gas.s.pos = NULL; - parser_gas.s.lim = NULL; - parser_gas.s.top = NULL; - parser_gas.s.eof = NULL; - parser_gas.s.tchar = 0; - parser_gas.s.tline = 0; - parser_gas.s.cline = 1; + yasm_scanner_initialize(&parser_gas.s); parser_gas.state = INITIAL; @@ -103,7 +93,7 @@ gas_parser_do_parse(yasm_object *object, yasm_preproc *pp, yasm_arch *a, yasm_linemap_get_current(parser_gas.linemap)-2); } - gas_parser_cleanup(&parser_gas); + yasm_scanner_delete(&parser_gas.s); /* Free locallabel base if necessary */ if (parser_gas.locallabel_base) diff --git a/modules/parsers/gas/gas-parser.h b/modules/parsers/gas/gas-parser.h index f736bb28..a74dade1 100644 --- a/modules/parsers/gas/gas-parser.h +++ b/modules/parsers/gas/gas-parser.h @@ -33,10 +33,6 @@ #include "gas-bison.h" #define YYCTYPE unsigned char -typedef struct Scanner { - YYCTYPE *bot, *tok, *ptr, *cur, *pos, *lim, *top, *eof; - unsigned int tchar, tline, cline; -} Scanner; #define MAX_SAVED_LINE_LEN 80 @@ -87,7 +83,7 @@ typedef struct yasm_parser_gas { YYCTYPE save_line[2][MAX_SAVED_LINE_LEN]; int save_last; - Scanner s; + yasm_scanner s; enum { INITIAL, COMMENT, diff --git a/modules/parsers/gas/gas-token.re b/modules/parsers/gas/gas-token.re index 08e4d946..db08de4a 100644 --- a/modules/parsers/gas/gas-token.re +++ b/modules/parsers/gas/gas-token.re @@ -131,10 +131,16 @@ rept_input(yasm_parser_gas *parser_gas, /*@out@*/ YYCTYPE *buf, return (max_size-numleft); } +static size_t +fill_input(void *d, unsigned char *buf, size_t max) +{ + return yasm_preproc_input((yasm_preproc *)d, (char *)buf, max); +} + static YYCTYPE * fill(yasm_parser_gas *parser_gas, YYCTYPE *cursor) { - Scanner *s = &parser_gas->s; + yasm_scanner *s = &parser_gas->s; int first = 0; if(!s->eof){ size_t cnt = s->tok - s->bot; @@ -186,7 +192,7 @@ fill(yasm_parser_gas *parser_gas, YYCTYPE *cursor) static YYCTYPE * save_line(yasm_parser_gas *parser_gas, YYCTYPE *cursor) { - Scanner *s = &parser_gas->s; + yasm_scanner *s = &parser_gas->s; int i = 0; YYCTYPE *saveline; @@ -202,13 +208,6 @@ save_line(yasm_parser_gas *parser_gas, YYCTYPE *cursor) return cursor; } -void -gas_parser_cleanup(yasm_parser_gas *parser_gas) -{ - if (parser_gas->s.bot) - yasm_xfree(parser_gas->s.bot); -} - /* starting size of string buffer */ #define STRBUF_ALLOC_SIZE 128 @@ -219,8 +218,8 @@ static YYCTYPE *strbuf = NULL; static size_t strbuf_size = 0; static void -strbuf_append(size_t count, YYCTYPE *cursor, Scanner *s, unsigned long line, - int ch) +strbuf_append(size_t count, YYCTYPE *cursor, yasm_scanner *s, + unsigned long line, int ch) { if (cursor == s->eof) yasm_error_set(YASM_ERROR_SYNTAX, @@ -249,7 +248,7 @@ int gas_parser_lex(YYSTYPE *lvalp, yasm_parser_gas *parser_gas) { /*@null@*/ gas_rept *rept = parser_gas->rept; - Scanner *s = &parser_gas->s; + yasm_scanner *s = &parser_gas->s; YYCTYPE *cursor = s->cur; size_t count; YYCTYPE savech; diff --git a/modules/parsers/nasm/nasm-parser.c b/modules/parsers/nasm/nasm-parser.c index ade9a076..22387720 100644 --- a/modules/parsers/nasm/nasm-parser.c +++ b/modules/parsers/nasm/nasm-parser.c @@ -63,17 +63,7 @@ nasm_parser_do_parse(yasm_object *object, yasm_preproc *pp, yasm_arch *a, parser_nasm.save_last = 0; /* initialize scanner structure */ - parser_nasm.s.bot = NULL; - parser_nasm.s.tok = NULL; - parser_nasm.s.ptr = NULL; - parser_nasm.s.cur = NULL; - parser_nasm.s.pos = NULL; - parser_nasm.s.lim = NULL; - parser_nasm.s.top = NULL; - parser_nasm.s.eof = NULL; - parser_nasm.s.tchar = 0; - parser_nasm.s.tline = 0; - parser_nasm.s.cline = 1; + yasm_scanner_initialize(&parser_nasm.s); parser_nasm.state = INITIAL; @@ -82,7 +72,7 @@ nasm_parser_do_parse(yasm_object *object, yasm_preproc *pp, yasm_arch *a, nasm_parser_parse(&parser_nasm); - nasm_parser_cleanup(&parser_nasm); + yasm_scanner_delete(&parser_nasm.s); /* Free locallabel base if necessary */ if (parser_nasm.locallabel_base) diff --git a/modules/parsers/nasm/nasm-parser.h b/modules/parsers/nasm/nasm-parser.h index a8a4a616..4b569553 100644 --- a/modules/parsers/nasm/nasm-parser.h +++ b/modules/parsers/nasm/nasm-parser.h @@ -30,10 +30,6 @@ #include "nasm-bison.h" #define YYCTYPE unsigned char -typedef struct Scanner { - YYCTYPE *bot, *tok, *ptr, *cur, *pos, *lim, *top, *eof; - unsigned int tchar, tline, cline; -} Scanner; #define MAX_SAVED_LINE_LEN 80 @@ -64,7 +60,7 @@ typedef struct yasm_parser_nasm { YYCTYPE save_line[2][MAX_SAVED_LINE_LEN]; int save_last; - Scanner s; + yasm_scanner s; enum { INITIAL, DIRECTIVE, diff --git a/modules/parsers/nasm/nasm-token.re b/modules/parsers/nasm/nasm-token.re index ebb28061..d2879a88 100644 --- a/modules/parsers/nasm/nasm-token.re +++ b/modules/parsers/nasm/nasm-token.re @@ -41,7 +41,7 @@ RCSID("$Id$"); #define YYCURSOR cursor #define YYLIMIT (s->lim) #define YYMARKER (s->ptr) -#define YYFILL(n) {cursor = fill(parser_nasm, cursor);} +#define YYFILL(n) {fill(parser_nasm, &cursor);} #define RETURN(i) {s->cur = cursor; return i;} @@ -55,59 +55,33 @@ RCSID("$Id$"); #define TOKLEN (size_t)(cursor-s->tok) -static YYCTYPE * -fill(yasm_parser_nasm *parser_nasm, YYCTYPE *cursor) +static size_t +fill_input(void *d, unsigned char *buf, size_t max) { - Scanner *s = &parser_nasm->s; - int first = 0; - if(!s->eof){ - size_t cnt = s->tok - s->bot; - if(cnt){ - memmove(s->bot, s->tok, (size_t)(s->lim - s->tok)); - s->tok = s->bot; - s->ptr -= cnt; - cursor -= cnt; - s->pos -= cnt; - s->lim -= cnt; - } - if (!s->bot) - first = 1; - if((s->top - s->lim) < BSIZE){ - YYCTYPE *buf = yasm_xmalloc((size_t)(s->lim - s->bot) + BSIZE); - memcpy(buf, s->tok, (size_t)(s->lim - s->tok)); - s->tok = buf; - s->ptr = &buf[s->ptr - s->bot]; - cursor = &buf[cursor - s->bot]; - s->pos = &buf[s->pos - s->bot]; - s->lim = &buf[s->lim - s->bot]; - s->top = &s->lim[BSIZE]; - if (s->bot) - yasm_xfree(s->bot); - s->bot = buf; - } - if((cnt = yasm_preproc_input(parser_nasm->preproc, (char *)s->lim, - BSIZE)) == 0) { - s->eof = &s->lim[cnt]; *s->eof++ = '\n'; - } - s->lim += cnt; - if (first && parser_nasm->save_input) { - int i; - YYCTYPE *saveline; - parser_nasm->save_last ^= 1; - saveline = parser_nasm->save_line[parser_nasm->save_last]; - /* save next line into cur_line */ - for (i=0; i<79 && &s->tok[i] < s->lim && s->tok[i] != '\n'; i++) - saveline[i] = s->tok[i]; - saveline[i] = '\0'; - } + return yasm_preproc_input((yasm_preproc *)d, (char *)buf, max); +} + +static void +fill(yasm_parser_nasm *parser_nasm, YYCTYPE **cursor) +{ + yasm_scanner *s = &parser_nasm->s; + if (yasm_fill_helper(s, cursor, fill_input, parser_nasm->preproc) + && parser_nasm->save_input) { + int i; + YYCTYPE *saveline; + parser_nasm->save_last ^= 1; + saveline = parser_nasm->save_line[parser_nasm->save_last]; + /* save next line into cur_line */ + for (i=0; i<79 && &s->tok[i] < s->lim && s->tok[i] != '\n'; i++) + saveline[i] = s->tok[i]; + saveline[i] = '\0'; } - return cursor; } static YYCTYPE * save_line(yasm_parser_nasm *parser_nasm, YYCTYPE *cursor) { - Scanner *s = &parser_nasm->s; + yasm_scanner *s = &parser_nasm->s; int i = 0; YYCTYPE *saveline; @@ -123,13 +97,6 @@ save_line(yasm_parser_nasm *parser_nasm, YYCTYPE *cursor) return cursor; } -void -nasm_parser_cleanup(yasm_parser_nasm *parser_nasm) -{ - if (parser_nasm->s.bot) - yasm_xfree(parser_nasm->s.bot); -} - /* starting size of string buffer */ #define STRBUF_ALLOC_SIZE 128 @@ -156,7 +123,7 @@ static int linechg_numcount; int nasm_parser_lex(YYSTYPE *lvalp, yasm_parser_nasm *parser_nasm) { - Scanner *s = &parser_nasm->s; + yasm_scanner *s = &parser_nasm->s; YYCTYPE *cursor = s->cur; YYCTYPE endch; size_t count, len;