From 15bdc51187d32b8dd34e7809bc6944ff4778d1bb Mon Sep 17 00:00:00 2001 From: Cyrill Gorcunov Date: Tue, 13 Jul 2010 11:27:41 +0400 Subject: preproc: Extract reading line from predefined macros from read_line It makes read_line less complex Signed-off-by: Cyrill Gorcunov --- preproc.c | 143 ++++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 83 insertions(+), 60 deletions(-) diff --git a/preproc.c b/preproc.c index 9d560b9d..be321f33 100644 --- a/preproc.c +++ b/preproc.c @@ -680,6 +680,80 @@ hash_findix(struct hash_table *hash, const char *str) return p ? *p : NULL; } +/* + * read line from standart macros set, + * if there no more left -- return NULL + */ +static char *line_from_stdmac(void) +{ + unsigned char c; + const unsigned char *p = stdmacpos; + char *line, *q; + size_t len = 0; + + if (!stdmacpos) + return NULL; + + while ((c = *p++)) { + if (c >= 0x80) + len += pp_directives_len[c - 0x80] + 1; + else + len++; + } + + line = nasm_malloc(len + 1); + q = line; + while ((c = *stdmacpos++)) { + if (c >= 0x80) { + memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]); + q += pp_directives_len[c - 0x80]; + *q++ = ' '; + } else { + *q++ = c; + } + } + stdmacpos = p; + *q = '\0'; + + if (!*stdmacpos) { + /* This was the last of the standard macro chain... */ + stdmacpos = NULL; + if (any_extrastdmac) { + stdmacpos = extrastdmac; + any_extrastdmac = false; + } else if (do_predef) { + Line *pd, *l; + Token *head, **tail, *t; + + /* + * Nasty hack: here we push the contents of + * `predef' on to the top-level expansion stack, + * since this is the most convenient way to + * implement the pre-include and pre-define + * features. + */ + list_for_each(pd, predef) { + head = NULL; + tail = &head; + list_for_each(t, pd->first) { + *tail = new_Token(NULL, t->type, t->text, 0); + tail = &(*tail)->next; + } + + l = nasm_malloc(sizeof(Line)); + l->next = istk->expansion; + l->first = head; + l->finishes = NULL; + + istk->expansion = l; + } + do_predef = false; + } + } + + return line; +} + #define BUF_DELTA 512 /* * Read a line from the top file in istk, handling multiple CR/LFs @@ -692,67 +766,16 @@ static char *read_line(void) char *buffer, *p, *q; int bufsize, continued_count; - if (stdmacpos) { - unsigned char c; - const unsigned char *p = stdmacpos; - char *ret, *q; - size_t len = 0; - while ((c = *p++)) { - if (c >= 0x80) - len += pp_directives_len[c-0x80]+1; - else - len++; - } - ret = nasm_malloc(len+1); - q = ret; - while ((c = *stdmacpos++)) { - if (c >= 0x80) { - memcpy(q, pp_directives[c-0x80], pp_directives_len[c-0x80]); - q += pp_directives_len[c-0x80]; - *q++ = ' '; - } else { - *q++ = c; - } - } - stdmacpos = p; - *q = '\0'; - - if (!*stdmacpos) { - /* This was the last of the standard macro chain... */ - stdmacpos = NULL; - if (any_extrastdmac) { - stdmacpos = extrastdmac; - any_extrastdmac = false; - } else if (do_predef) { - Line *pd, *l; - Token *head, **tail, *t; - - /* - * Nasty hack: here we push the contents of - * `predef' on to the top-level expansion stack, - * since this is the most convenient way to - * implement the pre-include and pre-define - * features. - */ - list_for_each(pd, predef) { - head = NULL; - tail = &head; - list_for_each(t, pd->first) { - *tail = new_Token(NULL, t->type, t->text, 0); - tail = &(*tail)->next; - } - l = nasm_malloc(sizeof(Line)); - l->next = istk->expansion; - l->first = head; - l->finishes = NULL; - istk->expansion = l; - } - do_predef = false; - } - } - return ret; - } + /* + * standart macros set (predefined) goes first + */ + p = line_from_stdmac(); + if (p) + return p; + /* + * regular read from a file + */ bufsize = BUF_DELTA; buffer = nasm_malloc(BUF_DELTA); p = buffer; -- cgit v1.2.1