do_preproc_only(void)
{
yasm_linemap *linemap;
- char *preproc_buf = yasm_xmalloc(PREPROC_BUF_SIZE);
+ char *preproc_buf;
size_t got;
const char *base_filename;
FILE *out = NULL;
} else {
/* Open output (object) file */
out = open_file(obj_filename, "wt");
- if (!out) {
- yasm_xfree(preproc_buf);
+ if (!out)
return EXIT_FAILURE;
- }
}
/* Pre-process until done */
if (generate_make_dependencies) {
size_t totlen;
+ preproc_buf = yasm_xmalloc(PREPROC_BUF_SIZE);
+
fprintf(stdout, "%s: %s", obj_filename, in_filename);
totlen = strlen(obj_filename)+2+strlen(in_filename);
fwrite(preproc_buf, got, 1, stdout);
}
fputc('\n', stdout);
+ yasm_xfree(preproc_buf);
} else {
- while ((got = yasm_preproc_input(cur_preproc, preproc_buf,
- PREPROC_BUF_SIZE)) != 0)
- fwrite(preproc_buf, got, 1, out);
+ while ((preproc_buf = yasm_preproc_get_line(cur_preproc)) != NULL) {
+ fputs(preproc_buf, out);
+ fputc('\n', out);
+ yasm_xfree(preproc_buf);
+ }
}
if (out != stdout)
print_yasm_error, print_yasm_warning);
if (out != stdout)
remove(obj_filename);
- yasm_xfree(preproc_buf);
yasm_linemap_destroy(linemap);
yasm_errwarns_destroy(errwarns);
cleanup(NULL);
yasm_errwarns_output_all(errwarns, linemap, warning_error,
print_yasm_error, print_yasm_warning);
- yasm_xfree(preproc_buf);
yasm_linemap_destroy(linemap);
yasm_errwarns_destroy(errwarns);
cleanup(NULL);
*/
void (*destroy) (/*@only@*/ yasm_preproc *preproc);
- /** Module-level implementation of yasm_preproc_input().
- * Call yasm_preproc_input() instead of calling this function.
+ /** Module-level implementation of yasm_preproc_get_line().
+ * Call yasm_preproc_get_line() instead of calling this function.
*/
- size_t (*input) (yasm_preproc *preproc, /*@out@*/ char *buf,
- size_t max_size);
+ char * (*get_line) (yasm_preproc *preproc);
/** Module-level implementation of yasm_preproc_get_included_file().
* Call yasm_preproc_get_included_file() instead of calling this function.
*/
void yasm_preproc_destroy(/*@only@*/ yasm_preproc *preproc);
-/** Gets more preprocessed source code (up to max_size bytes) into buf.
- * More than a single line may be returned in buf.
+/** Gets a single line of preprocessed source code.
* \param preproc preprocessor
- * \param buf destination buffer for preprocessed source
- * \param max_size maximum number of bytes that can be returned in buf
- * \return Actual number of bytes returned in buf.
+ * \return Allocated line of code, without the trailing \n.
*/
-size_t yasm_preproc_input(yasm_preproc *preproc, /*@out@*/ char *buf,
- size_t max_size);
+char *yasm_preproc_get_line(yasm_preproc *preproc);
/** Get the next filename included by the source code.
* \param preproc preprocessor
#define yasm_preproc_destroy(preproc) \
((yasm_preproc_base *)preproc)->module->destroy(preproc)
-#define yasm_preproc_input(preproc, buf, max_size) \
- ((yasm_preproc_base *)preproc)->module->input(preproc, buf, max_size)
+#define yasm_preproc_get_line(preproc) \
+ ((yasm_preproc_base *)preproc)->module->get_line(preproc)
#define yasm_preproc_get_included_file(preproc, buf, max_size) \
((yasm_preproc_base *)preproc)->module->get_included_file(preproc, buf, max_size)
#define yasm_preproc_add_include_file(preproc, filename) \
parser_gas.peek_token = NONE;
+ parser_gas.line = NULL;
+
/* initialize scanner structure */
yasm_scanner_initialize(&parser_gas.s);
YYCTYPE save_line[2][MAX_SAVED_LINE_LEN];
int save_last;
+ /* Line data storage used in preproc_input(). */
+ char *line, *linepos;
+ size_t lineleft;
+
yasm_scanner s;
enum gas_parser_state state;
return (max_size-numleft);
}
+
+/* Bridge function to convert byte-oriented parser with line-oriented
+ * preprocessor.
+ */
+static size_t
+preproc_input(yasm_parser_gas *parser_gas, /*@out@*/ YYCTYPE *buf,
+ size_t max_size)
+{
+ size_t tot=0;
+ while (max_size > 0) {
+ size_t n;
+
+ if (!parser_gas->line) {
+ parser_gas->line = yasm_preproc_get_line(parser_gas->preproc);
+ if (!parser_gas->line)
+ return tot; /* EOF */
+ parser_gas->linepos = parser_gas->line;
+ parser_gas->lineleft = strlen(parser_gas->line) + 1;
+ parser_gas->line[parser_gas->lineleft-1] = '\n';
+ }
+
+ n = parser_gas->lineleft<max_size ? parser_gas->lineleft : max_size;
+ strncpy((char *)buf+tot, parser_gas->linepos, n);
+
+ if (n == parser_gas->lineleft) {
+ yasm_xfree(parser_gas->line);
+ parser_gas->line = NULL;
+ } else {
+ parser_gas->lineleft -= n;
+ parser_gas->linepos += n;
+ }
+
+ tot += n;
+ max_size -= n;
+ }
+ return tot;
+}
#if 0
static size_t
fill_input(void *d, unsigned char *buf, size_t max)
if (parser_gas->rept && parser_gas->rept->ended) {
/* Pull from rept lines instead of preproc */
cnt = rept_input(parser_gas, s->lim, BSIZE);
- } else if((cnt = yasm_preproc_input(parser_gas->preproc,
- (char *)s->lim, BSIZE)) == 0) {
+ } else if((cnt = preproc_input(parser_gas, s->lim, BSIZE)) == 0) {
s->eof = &s->lim[cnt]; *s->eof++ = '\n';
}
s->lim += cnt;
static void define_label(yasm_parser_nasm *parser_nasm, /*@only@*/ char *name,
int local);
-#define is_eol_tok(tok) ((tok) == '\n' || (tok) == 0)
+#define is_eol_tok(tok) ((tok) == 0)
#define is_eol() is_eol_tok(curtok)
#define get_next_token() (curtok = nasm_parser_lex(&curval, parser_nasm))
void
nasm_parser_parse(yasm_parser_nasm *parser_nasm)
{
- while (get_next_token() != 0) {
+ unsigned char *line;
+ while ((line = (unsigned char *)
+ yasm_preproc_get_line(parser_nasm->preproc)) != NULL) {
yasm_bytecode *bc = NULL, *temp_bc;
-
+
+ parser_nasm->s.bot = line;
+ parser_nasm->s.tok = line;
+ parser_nasm->s.ptr = line;
+ parser_nasm->s.cur = line;
+ parser_nasm->s.lim = line + strlen((char *)line)+1;
+ parser_nasm->s.top = parser_nasm->s.lim;
+
+ get_next_token();
if (!is_eol()) {
bc = parse_line(parser_nasm);
demand_eol();
yasm_errwarn_propagate(parser_nasm->errwarns, cur_line);
if (parser_nasm->save_input)
- yasm_linemap_add_source(parser_nasm->linemap,
- temp_bc,
- (char *)parser_nasm->save_line[parser_nasm->save_last ^ 1]);
+ yasm_linemap_add_source(parser_nasm->linemap, temp_bc,
+ (char *)line);
yasm_linemap_goto_next(parser_nasm->linemap);
+ yasm_xfree(line);
}
}
parser_nasm.prev_bc = yasm_section_bcs_first(object->cur_section);
parser_nasm.save_input = save_input;
- parser_nasm.save_last = 0;
parser_nasm.peek_token = NONE;
nasm_parser_parse(&parser_nasm);
- yasm_scanner_delete(&parser_nasm.s);
+ /*yasm_scanner_delete(&parser_nasm.s);*/
/* Free locallabel base if necessary */
if (parser_nasm.locallabel_base)
/*@null@*/ yasm_bytecode *prev_bc;
int save_input;
- YYCTYPE save_line[2][MAX_SAVED_LINE_LEN];
- int save_last;
yasm_scanner s;
enum {
#include "modules/parsers/nasm/nasm-parser.h"
-#define BSIZE 8192
-
#define YYCURSOR cursor
#define YYLIMIT (s->lim)
#define YYMARKER (s->ptr)
-#define YYFILL(n) {fill(parser_nasm, &cursor);}
+#define YYFILL(n) {}
#define RETURN(i) {s->cur = cursor; parser_nasm->tokch = s->tok[0]; \
return i;}
#define TOKLEN (size_t)(cursor-s->tok)
-static size_t
-fill_input(void *d, unsigned char *buf, size_t max)
-{
- 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';
- }
-}
-
-static YYCTYPE *
-save_line(yasm_parser_nasm *parser_nasm, YYCTYPE *cursor)
-{
- yasm_scanner *s = &parser_nasm->s;
- int i = 0;
- YYCTYPE *saveline;
-
- parser_nasm->save_last ^= 1;
- saveline = parser_nasm->save_line[parser_nasm->save_last];
-
- /* save next line into cur_line */
- if ((YYLIMIT - YYCURSOR) < 80)
- YYFILL(80);
- for (i=0; i<79 && &cursor[i] < s->lim && cursor[i] != '\n'; i++)
- saveline[i] = cursor[i];
- saveline[i] = '\0';
- return cursor;
-}
-
/* starting size of string buffer */
#define STRBUF_ALLOC_SIZE 128
static int linechg_numcount;
/*!re2c
- any = [\000-\377];
+ any = [\001-\377];
digit = [0-9];
iletter = [a-zA-Z];
bindigit = [01];
return tok;
}
- /* Catch EOF */
+ /* Catch EOL (EOF from the scanner perspective) */
if (s->eof && cursor == s->eof)
return 0;
ws+ { goto scan; }
- "\n" {
- if (parser_nasm->save_input)
- cursor = save_line(parser_nasm, cursor);
+ [\000] {
parser_nasm->state = INITIAL;
RETURN(s->tok[0]);
}
RETURN(INTNUM);
}
- "\n" {
- if (parser_nasm->save_input)
- cursor = save_line(parser_nasm, cursor);
+ [\000] {
parser_nasm->state = INITIAL;
RETURN(s->tok[0]);
}
SCANINIT();
/*!re2c
- "\n" {
- if (parser_nasm->save_input)
- cursor = save_line(parser_nasm, cursor);
+ [\000] {
parser_nasm->state = INITIAL;
RETURN(s->tok[0]);
}
SCANINIT();
/*!re2c
- [\]\n] {
- if (parser_nasm->save_input)
- cursor = save_line(parser_nasm, cursor);
+ [\]\000] {
parser_nasm->state = INITIAL;
RETURN(s->tok[0]);
}
RETURN(s->tok[0]);
}
- "\n" {
- if (parser_nasm->save_input)
- cursor = save_line(parser_nasm, cursor);
+ [\000] {
parser_nasm->state = INITIAL;
RETURN(s->tok[0]);
}
ws+ { goto directive2; }
- "\n" {
- if (parser_nasm->save_input)
- cursor = save_line(parser_nasm, cursor);
+ [\000] {
parser_nasm->state = INITIAL;
RETURN(s->tok[0]);
}
SCANINIT();
/*!re2c
- "\n" {
- if (cursor == s->eof)
- yasm_error_set(YASM_ERROR_SYNTAX,
- N_("unexpected end of file in string"));
- else
- yasm_error_set(YASM_ERROR_SYNTAX, N_("unterminated string"));
+ [\000] {
+ yasm_error_set(YASM_ERROR_SYNTAX, N_("unterminated string"));
strbuf[count] = '\0';
lvalp->str.contents = (char *)strbuf;
lvalp->str.len = count;
- if (parser_nasm->save_input)
- cursor = save_line(parser_nasm, cursor);
RETURN(STRING);
}
/* TODO: Use autoconf to get the limit on the command line length. */
#define CMDLINE_SIZE 32770
+#define BSIZE 512
+
/* Pre-declare the preprocessor module object. */
yasm_preproc_module yasm_cpp_LTX_preproc;
yasm_xfree(pp);
}
-static size_t
-cpp_preproc_input(yasm_preproc *preproc, char *buf, size_t max_size)
+static char *
+cpp_preproc_get_line(yasm_preproc *preproc)
{
- size_t n;
yasm_preproc_cpp *pp = (yasm_preproc_cpp *)preproc;
+ int bufsize = BSIZE;
+ char *buf, *p;
if (! (pp->flags & CPP_HAS_BEEN_INVOKED) ) {
pp->flags |= CPP_HAS_BEEN_INVOKED;
Once the preprocessor has been run, we're just dealing with a normal
file.
*/
- if (((n = fread(buf, 1, max_size, pp->f)) == 0) &&
- ferror(pp->f)) {
- yasm_error_set(YASM_ERROR_IO, N_("error reading from pipe"));
- yasm_errwarn_propagate(pp->errwarns,
- yasm_linemap_get_current(pp->cur_lm));
+
+ /* Loop to ensure entire line is read (don't want to limit line length). */
+ buf = yasm_xmalloc((size_t)bufsize);
+ p = buf;
+ for (;;) {
+ if (!fgets(p, bufsize-(p-buf), pp->f)) {
+ if (ferror(pp->f)) {
+ yasm_error_set(YASM_ERROR_IO,
+ N_("error when reading from file"));
+ yasm_errwarn_propagate(pp->errwarns,
+ yasm_linemap_get_current(pp->cur_lm));
+ }
+ break;
+ }
+ p += strlen(p);
+ if (p > buf && p[-1] == '\n')
+ break;
+ if ((p-buf) >= bufsize) {
+ /* Increase size of buffer */
+ char *oldbuf = buf;
+ bufsize *= 2;
+ buf = yasm_xrealloc(buf, (size_t)bufsize);
+ p = buf + (p-oldbuf);
+ }
}
- return n;
+ if (p == buf) {
+ /* No data; must be at EOF */
+ yasm_xfree(buf);
+ return NULL;
+ }
+
+ /* Strip the line ending */
+ buf[strcspn(buf, "\r\n")] = '\0';
+
+ return buf;
}
static size_t
"cpp",
cpp_preproc_create,
cpp_preproc_destroy,
- cpp_preproc_input,
+ cpp_preproc_get_line,
cpp_preproc_get_included_file,
cpp_preproc_add_include_file,
cpp_preproc_predefine_macro,
yasm_preproc_base preproc; /* Base structure */
FILE *in;
- char *line, *linepos;
- size_t lineleft;
+ char *line;
char *file_name;
long prior_linnum;
int lineinc;
static yasm_preproc *
nasm_preproc_create(const char *in_filename, yasm_linemap *lm,
- yasm_errwarns *errwarns)
+ yasm_errwarns *errwarns)
{
FILE *f;
yasm_preproc_nasm *preproc_nasm = yasm_xmalloc(sizeof(yasm_preproc_nasm));
{
yasm_preproc_nasm *preproc_nasm = (yasm_preproc_nasm *)preproc;
nasmpp.cleanup(0);
+ if (preproc_nasm->line)
+ yasm_xfree(preproc_nasm->line);
if (preproc_nasm->file_name)
yasm_xfree(preproc_nasm->file_name);
yasm_xfree(preproc);
yasm_xfree(preproc_deps);
}
-static size_t
-nasm_preproc_input(yasm_preproc *preproc, char *buf, size_t max_size)
+static char *
+nasm_preproc_get_line(yasm_preproc *preproc)
{
yasm_preproc_nasm *preproc_nasm = (yasm_preproc_nasm *)preproc;
- size_t tot = 0, n;
- long linnum = preproc_nasm->prior_linnum += preproc_nasm->lineinc;
+ long linnum;
int altline;
+ char *line;
- if (!preproc_nasm->line) {
- preproc_nasm->line = nasmpp.getline();
- if (!preproc_nasm->line)
- return 0;
- preproc_nasm->linepos = preproc_nasm->line;
- preproc_nasm->lineleft = strlen(preproc_nasm->line) + 1;
- preproc_nasm->line[preproc_nasm->lineleft-1] = '\n';
-
- altline = nasm_src_get(&linnum, &preproc_nasm->file_name);
- if (altline) {
- if (altline == 1 && preproc_nasm->lineinc == 1) {
- *buf++ = '\n';
- max_size--;
- tot++;
- } else {
- preproc_nasm->lineinc =
- (altline != -1 || preproc_nasm->lineinc != 1);
- n = sprintf(buf, "%%line %ld+%d %s\n", linnum,
- preproc_nasm->lineinc, preproc_nasm->file_name);
- buf += n;
- max_size -= n;
- tot += n;
- }
- preproc_nasm->prior_linnum = linnum;
- }
+ if (preproc_nasm->line) {
+ char *retval = preproc_nasm->line;
+ preproc_nasm->line = NULL;
+ return retval;
}
- n = preproc_nasm->lineleft<max_size?preproc_nasm->lineleft:max_size;
- strncpy(buf, preproc_nasm->linepos, n);
- tot += n;
-
- if (n == preproc_nasm->lineleft) {
- yasm_xfree(preproc_nasm->line);
- preproc_nasm->line = NULL;
- } else {
- preproc_nasm->lineleft -= n;
- preproc_nasm->linepos += n;
+ line = nasmpp.getline();
+ if (!line)
+ return NULL; /* EOF */
+
+ linnum = preproc_nasm->prior_linnum += preproc_nasm->lineinc;
+ altline = nasm_src_get(&linnum, &preproc_nasm->file_name);
+ if (altline != 0) {
+ preproc_nasm->lineinc =
+ (altline != -1 || preproc_nasm->lineinc != 1);
+ preproc_nasm->line = line;
+ line = yasm_xmalloc(40+strlen(preproc_nasm->file_name));
+ sprintf(line, "%%line %ld+%d %s", linnum,
+ preproc_nasm->lineinc, preproc_nasm->file_name);
+ preproc_nasm->prior_linnum = linnum;
}
- return tot;
+ return line;
}
void
"nasm",
nasm_preproc_create,
nasm_preproc_destroy,
- nasm_preproc_input,
+ nasm_preproc_get_line,
nasm_preproc_get_included_file,
nasm_preproc_add_include_file,
nasm_preproc_predefine_macro,
#include <libyasm.h>
+#define BSIZE 512
+
typedef struct yasm_preproc_raw {
yasm_preproc_base preproc; /* base structure */
- int is_interactive;
FILE *in;
yasm_linemap *cur_lm;
yasm_errwarns *errwarns;
yasm_preproc_module yasm_raw_LTX_preproc;
-int isatty(int);
-
static yasm_preproc *
raw_preproc_create(const char *in_filename, yasm_linemap *lm,
yasm_errwarns *errwarns)
preproc_raw->in = f;
preproc_raw->cur_lm = lm;
preproc_raw->errwarns = errwarns;
- /*@-unrecog@*/
- preproc_raw->is_interactive = f ? (isatty(fileno(f)) > 0) : 0;
- /*@=unrecog@*/
return (yasm_preproc *)preproc_raw;
}
yasm_xfree(preproc);
}
-static size_t
-raw_preproc_input(yasm_preproc *preproc, char *buf, size_t max_size)
+static char *
+raw_preproc_get_line(yasm_preproc *preproc)
{
yasm_preproc_raw *preproc_raw = (yasm_preproc_raw *)preproc;
- int c = '*';
- size_t n;
-
- if (preproc_raw->is_interactive) {
- for (n = 0; n < max_size && (c = getc(preproc_raw->in)) != EOF &&
- c != '\n'; n++)
- buf[n] = (char)c;
- if (c == '\n')
- buf[n++] = (char)c;
- if (c == EOF && ferror(preproc_raw->in)) {
- yasm_error_set(YASM_ERROR_IO, N_("error when reading from file"));
- yasm_errwarn_propagate(preproc_raw->errwarns,
- yasm_linemap_get_current(preproc_raw->cur_lm));
+ int bufsize = BSIZE;
+ char *buf = yasm_xmalloc((size_t)bufsize);
+ char *p;
+
+ /* Loop to ensure entire line is read (don't want to limit line length). */
+ p = buf;
+ for (;;) {
+ if (!fgets(p, bufsize-(p-buf), preproc_raw->in)) {
+ if (ferror(preproc_raw->in)) {
+ yasm_error_set(YASM_ERROR_IO,
+ N_("error when reading from file"));
+ yasm_errwarn_propagate(preproc_raw->errwarns,
+ yasm_linemap_get_current(preproc_raw->cur_lm));
+ }
+ break;
+ }
+ p += strlen(p);
+ if (p > buf && p[-1] == '\n')
+ break;
+ if ((p-buf) >= bufsize) {
+ /* Increase size of buffer */
+ char *oldbuf = buf;
+ bufsize *= 2;
+ buf = yasm_xrealloc(buf, (size_t)bufsize);
+ p = buf + (p-oldbuf);
}
- } else if (((n = fread(buf, 1, max_size, preproc_raw->in)) == 0) &&
- ferror(preproc_raw->in)) {
- yasm_error_set(YASM_ERROR_IO, N_("error when reading from file"));
- yasm_errwarn_propagate(preproc_raw->errwarns,
- yasm_linemap_get_current(preproc_raw->cur_lm));
}
- return n;
+ if (p == buf) {
+ /* No data; must be at EOF */
+ yasm_xfree(buf);
+ return NULL;
+ }
+
+ /* Strip the line ending */
+ buf[strcspn(buf, "\r\n")] = '\0';
+
+ return buf;
}
static size_t
"raw",
raw_preproc_create,
raw_preproc_destroy,
- raw_preproc_input,
+ raw_preproc_get_line,
raw_preproc_get_included_file,
raw_preproc_add_include_file,
raw_preproc_predefine_macro,