From: Peter Johnson Date: Sun, 27 Oct 2002 09:21:39 +0000 (-0000) Subject: Delete last of global variables by making a line manager and passing around X-Git-Tag: v0.2.0~67 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2f19ce4ecdfc03df71ba623549df2efff14adbd8;p=yasm Delete last of global variables by making a line manager and passing around the line index. Fixes some minor line number/error message nits due to incorrect usage of line_index in old global variable method. svn path=/trunk/yasm/; revision=787 --- diff --git a/frontends/yasm/Makefile.inc b/frontends/yasm/Makefile.inc index 5eca5ae8..1926b5ae 100644 --- a/frontends/yasm/Makefile.inc +++ b/frontends/yasm/Makefile.inc @@ -11,8 +11,7 @@ libyasm_la_SOURCES = \ src/expr-int.h \ src/symrec.c \ src/symrec.h \ - src/globals.c \ - src/globals.h \ + src/linemgr.h \ src/util.h \ src/coretype.h \ src/file.c \ @@ -47,7 +46,8 @@ yasm_SOURCES += \ src/objfmt.c \ src/parser.c \ src/module.h \ - src/module.c + src/module.c \ + src/linemgr.c EXTRA_DIST += \ diff --git a/frontends/yasm/yasm-module.c b/frontends/yasm/yasm-module.c index ba31ebd3..8b1e7410 100644 --- a/frontends/yasm/yasm-module.c +++ b/frontends/yasm/yasm-module.c @@ -25,7 +25,6 @@ #include "ltdl.h" #include "module.h" -#include "globals.h" typedef struct module { diff --git a/frontends/yasm/yasm.c b/frontends/yasm/yasm.c index dc240b68..1c7d7e17 100644 --- a/frontends/yasm/yasm.c +++ b/frontends/yasm/yasm.c @@ -28,8 +28,8 @@ #include "bitvect.h" #include "file.h" -#include "globals.h" #include "options.h" +#include "linemgr.h" #include "errwarn.h" #include "intnum.h" #include "floatnum.h" @@ -47,6 +47,9 @@ #include "arch.h" +/* YASM's line manager (for parse stage). */ +extern linemgr yasm_linemgr; + /* Extra path to search for our modules. */ #ifndef YASM_MODULE_PATH_ENV # define YASM_MODULE_PATH_ENV "YASM_MODULE_PATH" @@ -204,8 +207,9 @@ main(int argc, char *argv[]) in_filename = xstrdup("-"); } - /* Initialize line info */ - line_set(in_filename, 1, 1); + /* Initialize line manager */ + yasm_linemgr.initialize(); + yasm_linemgr.set(in_filename, 1, 1); /* handle preproc-only case here */ if (preproc_only) { @@ -236,7 +240,8 @@ main(int argc, char *argv[]) /* Pre-process until done */ cur_preproc->initialize(in, in_filename); - while ((got = cur_preproc->input(preproc_buf, PREPROC_BUF_SIZE)) != 0) + while ((got = cur_preproc->input(preproc_buf, PREPROC_BUF_SIZE, + &yasm_linemgr)) != 0) fwrite(preproc_buf, got, 1, obj); if (in != stdin) @@ -246,7 +251,7 @@ main(int argc, char *argv[]) fclose(obj); if (GetNumErrors() > 0) { - OutputAllErrorWarning(); + OutputAllErrorWarning(&yasm_linemgr); if (obj != stdout) remove(obj_filename); xfree(preproc_buf); @@ -370,15 +375,15 @@ main(int argc, char *argv[]) } /* Parse! */ - sections = cur_parser->do_parse(cur_preproc, cur_arch, cur_objfmt, in, - in_filename); + sections = cur_parser->do_parse(cur_preproc, cur_arch, cur_objfmt, + &yasm_linemgr, in, in_filename); /* Close input file */ if (in != stdin) fclose(in); if (GetNumErrors() > 0) { - OutputAllErrorWarning(); + OutputAllErrorWarning(&yasm_linemgr); cleanup(sections); return EXIT_FAILURE; } @@ -387,7 +392,7 @@ main(int argc, char *argv[]) cur_optimizer->optimize(sections); if (GetNumErrors() > 0) { - OutputAllErrorWarning(); + OutputAllErrorWarning(&yasm_linemgr); cleanup(sections); return EXIT_FAILURE; } @@ -412,13 +417,13 @@ main(int argc, char *argv[]) * object file (to make sure it's not left newer than the source). */ if (GetNumErrors() > 0) { - OutputAllErrorWarning(); + OutputAllErrorWarning(&yasm_linemgr); remove(obj_filename); cleanup(sections); return EXIT_FAILURE; } - OutputAllErrorWarning(); + OutputAllErrorWarning(&yasm_linemgr); cleanup(sections); return EXIT_SUCCESS; @@ -448,7 +453,7 @@ cleanup(sectionhead *sections) if (sections) sections_delete(sections); symrec_delete_all(); - line_shutdown(); + yasm_linemgr.cleanup(); floatnum_shutdown(); intnum_shutdown(); diff --git a/libyasm/Makefile.inc b/libyasm/Makefile.inc index 5eca5ae8..1926b5ae 100644 --- a/libyasm/Makefile.inc +++ b/libyasm/Makefile.inc @@ -11,8 +11,7 @@ libyasm_la_SOURCES = \ src/expr-int.h \ src/symrec.c \ src/symrec.h \ - src/globals.c \ - src/globals.h \ + src/linemgr.h \ src/util.h \ src/coretype.h \ src/file.c \ @@ -47,7 +46,8 @@ yasm_SOURCES += \ src/objfmt.c \ src/parser.c \ src/module.h \ - src/module.c + src/module.c \ + src/linemgr.c EXTRA_DIST += \ diff --git a/libyasm/arch.c b/libyasm/arch.c index bf46b3c0..d26fc917 100644 --- a/libyasm/arch.c +++ b/libyasm/arch.c @@ -22,7 +22,6 @@ #include "util.h" /*@unused@*/ RCSID("$IdPath$"); -#include "globals.h" #include "expr.h" #include "bytecode.h" diff --git a/libyasm/arch.h b/libyasm/arch.h index 02f93db6..3d23ea8c 100644 --- a/libyasm/arch.h +++ b/libyasm/arch.h @@ -69,7 +69,7 @@ struct arch { * parse functions! The bytecode and output functions should be able * to handle any CPU. */ - void (*switch_cpu) (const char *cpuid); + void (*switch_cpu) (const char *cpuid, unsigned long lindex); /* Checks an generic identifier to see if it matches architecture * specific names for instructions, registers, etc (see the @@ -82,7 +82,8 @@ struct arch { * used for TARGETMOD, REG, and SEGREG return values. */ arch_check_id_retval (*check_identifier) (unsigned long data[4], - const char *id); + const char *id, + unsigned long lindex); /* Architecture-specific directive support. Returns 1 if directive was * not recognized. Returns 0 if directive was recognized, even if it @@ -91,7 +92,7 @@ struct arch { */ int (*directive) (const char *name, valparamhead *valparams, /*@null@*/ valparamhead *objext_valparams, - sectionhead *headp); + sectionhead *headp, unsigned long lindex); /* Creates an instruction. Creates a bytecode by matching the * instruction data and the parameters given with a valid instruction. @@ -102,20 +103,24 @@ struct arch { int num_operands, /*@null@*/ insn_operandhead *operands, section *cur_section, - /*@null@*/ bytecode *prev_bc); + /*@null@*/ bytecode *prev_bc, + unsigned long lindex); /* Handle an instruction prefix by modifying bc as necessary. */ - void (*handle_prefix) (bytecode *bc, const unsigned long data[4]); + void (*handle_prefix) (bytecode *bc, const unsigned long data[4], + unsigned long lindex); /* Handle an segment register instruction prefix by modifying bc as * necessary. */ - void (*handle_seg_prefix) (bytecode *bc, unsigned long segreg); + void (*handle_seg_prefix) (bytecode *bc, unsigned long segreg, + unsigned long lindex); /* Handle memory expression segment overrides by modifying ea as * necessary. */ - void (*handle_seg_override) (effaddr *ea, unsigned long segreg); + void (*handle_seg_override) (effaddr *ea, unsigned long segreg, + unsigned long lindex); /* Convert an expression into an effective address. */ effaddr * (*ea_new_expr) (/*@keep@*/ expr *e); diff --git a/libyasm/bytecode.c b/libyasm/bytecode.c index 0aedd6d0..3a1132d3 100644 --- a/libyasm/bytecode.c +++ b/libyasm/bytecode.c @@ -24,7 +24,6 @@ #include "file.h" -#include "globals.h" #include "errwarn.h" #include "intnum.h" #include "expr.h" @@ -106,9 +105,10 @@ bc_initialize(arch *a) } immval * -imm_new_int(unsigned long int_val) +imm_new_int(unsigned long int_val, unsigned long lindex) { - return imm_new_expr(expr_new_ident(ExprInt(intnum_new_uint(int_val)))); + return imm_new_expr(expr_new_ident(ExprInt(intnum_new_uint(int_val)), + lindex)); } immval * @@ -179,13 +179,13 @@ void bc_set_multiple(bytecode *bc, expr *e) { if (bc->multiple) - bc->multiple = expr_new_tree(bc->multiple, EXPR_MUL, e); + bc->multiple = expr_new_tree(bc->multiple, EXPR_MUL, e, e->line); else bc->multiple = e; } bytecode * -bc_new_common(bytecode_type type, size_t size) +bc_new_common(bytecode_type type, size_t size, unsigned long lindex) { bytecode *bc = xmalloc(size); @@ -194,7 +194,7 @@ bc_new_common(bytecode_type type, size_t size) bc->multiple = (expr *)NULL; bc->len = 0; - bc->line = line_index; + bc->line = lindex; bc->offset = 0; @@ -204,11 +204,12 @@ bc_new_common(bytecode_type type, size_t size) } bytecode * -bc_new_data(datavalhead *datahead, unsigned char size) +bc_new_data(datavalhead *datahead, unsigned char size, unsigned long lindex) { bytecode_data *data; - data = (bytecode_data *)bc_new_common(BC_DATA, sizeof(bytecode_data)); + data = (bytecode_data *)bc_new_common(BC_DATA, sizeof(bytecode_data), + lindex); data->datahead = *datahead; data->size = size; @@ -217,12 +218,13 @@ bc_new_data(datavalhead *datahead, unsigned char size) } bytecode * -bc_new_reserve(expr *numitems, unsigned char itemsize) +bc_new_reserve(expr *numitems, unsigned char itemsize, unsigned long lindex) { bytecode_reserve *reserve; reserve = (bytecode_reserve *)bc_new_common(BC_RESERVE, - sizeof(bytecode_reserve)); + sizeof(bytecode_reserve), + lindex); /*@-mustfree@*/ reserve->numitems = numitems; @@ -233,12 +235,13 @@ bc_new_reserve(expr *numitems, unsigned char itemsize) } bytecode * -bc_new_incbin(char *filename, expr *start, expr *maxlen) +bc_new_incbin(char *filename, expr *start, expr *maxlen, unsigned long lindex) { bytecode_incbin *incbin; incbin = (bytecode_incbin *)bc_new_common(BC_INCBIN, - sizeof(bytecode_incbin)); + sizeof(bytecode_incbin), + lindex); /*@-mustfree@*/ incbin->filename = filename; @@ -250,11 +253,12 @@ bc_new_incbin(char *filename, expr *start, expr *maxlen) } bytecode * -bc_new_align(unsigned long boundary) +bc_new_align(unsigned long boundary, unsigned long lindex) { bytecode_align *align; - align = (bytecode_align *)bc_new_common(BC_ALIGN, sizeof(bytecode_align)); + align = (bytecode_align *)bc_new_common(BC_ALIGN, sizeof(bytecode_align), + lindex); align->boundary = boundary; @@ -263,13 +267,14 @@ bc_new_align(unsigned long boundary) bytecode * bc_new_objfmt_data(unsigned int type, unsigned long len, objfmt *of, - void *data) + void *data, unsigned long lindex) { bytecode_objfmt_data *objfmt_data; objfmt_data = (bytecode_objfmt_data *)bc_new_common(BC_ALIGN, - sizeof(bytecode_objfmt_data)); + sizeof(bytecode_objfmt_data), + lindex); objfmt_data->type = type; objfmt_data->of = of; @@ -346,8 +351,6 @@ bc_print(FILE *f, int indent_level, const bytecode *bc) const bytecode_incbin *incbin; const bytecode_align *align; const bytecode_objfmt_data *objfmt_data; - const char *filename; - unsigned long line; switch (bc->type) { case BC_EMPTY: @@ -414,9 +417,7 @@ bc_print(FILE *f, int indent_level, const bytecode *bc) else expr_print(f, bc->multiple); fprintf(f, "\n%*sLength=%lu\n", indent_level, "", bc->len); - line_lookup(bc->line, &filename, &line); - fprintf(f, "%*sFilename=\"%s\" Line Number=%lu\n", indent_level, "", - filename, line); + fprintf(f, "%*sLine Index=%lu\n", indent_level, "", bc->line); fprintf(f, "%*sOffset=%lx\n", indent_level, "", bc->offset); } @@ -499,11 +500,10 @@ bc_resolve_reserve(bytecode_reserve *reserve, unsigned long *len, int save, * the circular reference error to filter through. */ if (temp && expr_contains(temp, EXPR_FLOAT)) - ErrorAt(line, - _("expression must not contain floating point value")); + Error(line, _("expression must not contain floating point value")); else - ErrorAt(line, - _("attempt to reserve non-constant quantity of space")); + Error(line, + _("attempt to reserve non-constant quantity of space")); retval = BC_RESOLVE_ERROR | BC_RESOLVE_UNKNOWN_LEN; } else *len += intnum_get_uint(num)*reserve->itemsize; @@ -565,13 +565,12 @@ bc_resolve_incbin(bytecode_incbin *incbin, unsigned long *len, int save, /* Open file and determine its length */ f = fopen(incbin->filename, "rb"); if (!f) { - ErrorAt(line, _("`incbin': unable to open file `%s'"), - incbin->filename); + Error(line, _("`incbin': unable to open file `%s'"), incbin->filename); return BC_RESOLVE_ERROR | BC_RESOLVE_UNKNOWN_LEN; } if (fseek(f, 0L, SEEK_END) < 0) { - ErrorAt(line, _("`incbin': unable to seek on file `%s'"), - incbin->filename); + Error(line, _("`incbin': unable to seek on file `%s'"), + incbin->filename); return BC_RESOLVE_ERROR | BC_RESOLVE_UNKNOWN_LEN; } flen = (unsigned long)ftell(f); @@ -579,8 +578,8 @@ bc_resolve_incbin(bytecode_incbin *incbin, unsigned long *len, int save, /* Compute length of incbin from start, maxlen, and len */ if (start > flen) { - WarningAt(line, _("`incbin': start past end of file `%s'"), - incbin->filename); + Warning(line, _("`incbin': start past end of file `%s'"), + incbin->filename); start = flen; } flen -= start; @@ -645,8 +644,8 @@ bc_resolve(bytecode *bc, int save, const section *sect, if (!num) { retval = BC_RESOLVE_UNKNOWN_LEN; if (temp && expr_contains(temp, EXPR_FLOAT)) { - ErrorAt(bc->line, - _("expression must not contain floating point value")); + Error(bc->line, + _("expression must not contain floating point value")); retval |= BC_RESOLVE_ERROR; } } else @@ -719,23 +718,22 @@ bc_tobytes_incbin(bytecode_incbin *incbin, unsigned char **bufp, /* Open file */ f = fopen(incbin->filename, "rb"); if (!f) { - ErrorAt(line, _("`incbin': unable to open file `%s'"), - incbin->filename); + Error(line, _("`incbin': unable to open file `%s'"), incbin->filename); return 1; } /* Seek to start of data */ if (fseek(f, (long)start, SEEK_SET) < 0) { - ErrorAt(line, _("`incbin': unable to seek on file `%s'"), - incbin->filename); + Error(line, _("`incbin': unable to seek on file `%s'"), + incbin->filename); fclose(f); return 1; } /* Read len bytes */ if (fread(*bufp, (size_t)len, 1, f) < (size_t)len) { - ErrorAt(line, _("`incbin': unable to read %lu bytes from file `%s'"), - len, incbin->filename); + Error(line, _("`incbin': unable to read %lu bytes from file `%s'"), + len, incbin->filename); fclose(f); return 1; } diff --git a/libyasm/bytecode.h b/libyasm/bytecode.h index 6ad5bb38..f2938ef4 100644 --- a/libyasm/bytecode.h +++ b/libyasm/bytecode.h @@ -42,7 +42,7 @@ typedef enum { void bc_initialize(arch *a); -/*@only@*/ immval *imm_new_int(unsigned long int_val); +/*@only@*/ immval *imm_new_int(unsigned long int_val, unsigned long lindex); /*@only@*/ immval *imm_new_expr(/*@keep@*/ expr *e); /*@observer@*/ const expr *ea_get_disp(const effaddr *ea); @@ -53,16 +53,22 @@ void ea_print(FILE *f, int indent_level, const effaddr *ea); void bc_set_multiple(bytecode *bc, /*@keep@*/ expr *e); -/*@only@*/ bytecode *bc_new_common(bytecode_type type, size_t datasize); -/*@only@*/ bytecode *bc_new_data(datavalhead *datahead, unsigned char size); +/*@only@*/ bytecode *bc_new_common(bytecode_type type, size_t datasize, + unsigned long lindex); +/*@only@*/ bytecode *bc_new_data(datavalhead *datahead, unsigned char size, + unsigned long lindex); /*@only@*/ bytecode *bc_new_reserve(/*@only@*/ expr *numitems, - unsigned char itemsize); + unsigned char itemsize, + unsigned long lindex); /*@only@*/ bytecode *bc_new_incbin(/*@only@*/ char *filename, /*@only@*/ /*@null@*/ expr *start, - /*@only@*/ /*@null@*/ expr *maxlen); -/*@only@*/ bytecode *bc_new_align(unsigned long boundary); + /*@only@*/ /*@null@*/ expr *maxlen, + unsigned long lindex); +/*@only@*/ bytecode *bc_new_align(unsigned long boundary, + unsigned long lindex); /*@only@*/ bytecode *bc_new_objfmt_data(unsigned int type, unsigned long len, - objfmt *of, /*@only@*/ void *data); + objfmt *of, /*@only@*/ void *data, + unsigned long lindex); void bc_delete(/*@only@*/ /*@null@*/ bytecode *bc); diff --git a/libyasm/coretype.h b/libyasm/coretype.h index 29f2709d..d1585fa9 100644 --- a/libyasm/coretype.h +++ b/libyasm/coretype.h @@ -42,6 +42,8 @@ typedef struct expr expr; typedef struct intnum intnum; typedef struct floatnum floatnum; +typedef struct linemgr linemgr; + typedef enum { EXPR_ADD, EXPR_SUB, diff --git a/libyasm/errwarn.c b/libyasm/errwarn.c index 47d72b66..c74d5337 100644 --- a/libyasm/errwarn.c +++ b/libyasm/errwarn.c @@ -28,7 +28,7 @@ # include #endif -#include "globals.h" +#include "linemgr.h" #include "errwarn.h" @@ -235,29 +235,17 @@ warning_common(unsigned long lindex, const char *fmt, va_list va) * system. */ void -ParserError(const char *s) +ParserError(unsigned long lindex, const char *s) { - Error("%s %s", _("parser error:"), s); + Error(lindex, "%s %s", _("parser error:"), s); previous_we->type = WE_PARSERERROR; } -/* Register an error during the parser stage (uses global line_index). Does - * not print the error, only stores it for OutputAllErrorWarning() to print. - */ -void -Error(const char *fmt, ...) -{ - va_list va; - va_start(va, fmt); - error_common(line_index, fmt, va); - va_end(va); -} - /* Register an error at line lindex. Does not print the error, only stores it * for OutputAllErrorWarning() to print. */ void -ErrorAt(unsigned long lindex, const char *fmt, ...) +Error(unsigned long lindex, const char *fmt, ...) { va_list va; va_start(va, fmt); @@ -265,23 +253,11 @@ ErrorAt(unsigned long lindex, const char *fmt, ...) va_end(va); } -/* Register a warning during the parser stage (uses global line_index). Does - * not print the warning, only stores it for OutputAllErrorWarning() to print. - */ -void -Warning(const char *fmt, ...) -{ - va_list va; - va_start(va, fmt); - warning_common(line_index, fmt, va); - va_end(va); -} - /* Register an warning at line lindex. Does not print the warning, only stores * it for OutputAllErrorWarning() to print. */ void -WarningAt(unsigned long lindex, const char *fmt, ...) +Warning(unsigned long lindex, const char *fmt, ...) { va_list va; va_start(va, fmt); @@ -329,7 +305,7 @@ GetNumErrors(void) /* Output all previously stored errors and warnings to stderr. */ void -OutputAllErrorWarning(void) +OutputAllErrorWarning(linemgr *lm) { errwarn *we; const char *filename; @@ -345,7 +321,7 @@ OutputAllErrorWarning(void) while (!SLIST_EMPTY(&errwarns)) { we = SLIST_FIRST(&errwarns); /* Output error/warning */ - line_lookup(we->line, &filename, &line); + lm->lookup(we->line, &filename, &line); if (we->type == WE_ERROR) fprintf(stderr, "%s:%lu: %s\n", filename, line, we->msg); else diff --git a/libyasm/errwarn.h b/libyasm/errwarn.h index bc95e328..75ee91a3 100644 --- a/libyasm/errwarn.h +++ b/libyasm/errwarn.h @@ -60,21 +60,16 @@ typedef enum { /*@shared@*/ char *conv_unprint(char ch); -void ParserError(const char *); +void ParserError(unsigned long lindex, const char *); /*@exits@*/ void InternalError_(const char *file, unsigned int line, const char *message); #define InternalError(msg) InternalError_(__FILE__, __LINE__, msg) /*@exits@*/ void Fatal(fatal_num); -void Error(const char *, ...) /*@printflike@*/; -void Warning(const char *, ...) /*@printflike@*/; -/* Use Error() and Warning() instead of ErrorAt() and WarningAt() when being - * called in line order from a parser. - */ -void ErrorAt(unsigned long lindex, const char *, ...) /*@printflike@*/; -void WarningAt(unsigned long lindex, const char *, ...) /*@printflike@*/; +void Error(unsigned long lindex, const char *, ...) /*@printflike@*/; +void Warning(unsigned long lindex, const char *, ...) /*@printflike@*/; /* These two functions immediately output the error or warning, with no file * or line information. They should be used for errors and warnings outside @@ -87,6 +82,6 @@ void WarningNow(const char *, ...) /*@printflike@*/; unsigned int GetNumErrors(void); /* Outputs all errors/warnings to standard error. */ -void OutputAllErrorWarning(void); +void OutputAllErrorWarning(linemgr *lm); #endif diff --git a/libyasm/expr.c b/libyasm/expr.c index c3458698..a56762aa 100644 --- a/libyasm/expr.c +++ b/libyasm/expr.c @@ -24,7 +24,6 @@ #include "bitvect.h" -#include "globals.h" #include "errwarn.h" #include "intnum.h" #include "floatnum.h" @@ -56,7 +55,7 @@ expr_initialize(arch *a) * If it's a unary operator, put the element in left and set right=NULL. */ /*@-compmempass@*/ expr * -expr_new(ExprOp op, ExprItem *left, ExprItem *right) +expr_new(ExprOp op, ExprItem *left, ExprItem *right, unsigned long lindex) { expr *ptr, *sube; ptr = xmalloc(sizeof(expr)); @@ -103,7 +102,7 @@ expr_new(ExprOp op, ExprItem *left, ExprItem *right) } } - ptr->line = line_index; + ptr->line = lindex; return ptr; } @@ -177,11 +176,11 @@ expr_xform_bc_dist(/*@returned@*/ /*@only@*/ expr *e, symrec_get_label(e->terms[i].data.sym, §, &precbc) && section_is_absolute(sect) && (dist = calc_bc_dist(sect, NULL, precbc))) { + const expr *start = section_get_start(sect); e->terms[i].type = EXPR_EXPR; e->terms[i].data.expn = - expr_new(EXPR_ADD, - ExprExpr(expr_copy(section_get_start(sect))), - ExprInt(dist)); + expr_new(EXPR_ADD, ExprExpr(expr_copy(start)), ExprInt(dist), + start->line); } } @@ -316,7 +315,7 @@ expr_xform_neg_helper(/*@returned@*/ /*@only@*/ expr *e) * floatnums present below; if there ARE floatnums, recurse. */ if (e->terms[0].type == EXPR_FLOAT) - floatnum_calc(e->terms[0].data.flt, EXPR_NEG, NULL); + floatnum_calc(e->terms[0].data.flt, EXPR_NEG, NULL, e->line); else if (e->terms[0].type == EXPR_EXPR && expr_contains(e->terms[0].data.expn, EXPR_FLOAT)) expr_xform_neg_helper(e->terms[0].data.expn); @@ -701,7 +700,7 @@ expr_level_tree(expr *e, int fold_const, int simplify_ident, /* Check for circular reference */ SLIST_FOREACH(np, eh, next) { if (np->e == equ_expr) { - ErrorAt(e->line, _("circular reference detected.")); + Error(e->line, _("circular reference detected.")); return e; } } diff --git a/libyasm/expr.h b/libyasm/expr.h index 06680bda..36e53972 100644 --- a/libyasm/expr.h +++ b/libyasm/expr.h @@ -27,7 +27,8 @@ typedef struct ExprItem ExprItem; void expr_initialize(arch *a); /*@only@*/ expr *expr_new(ExprOp, /*@only@*/ ExprItem *, - /*@only@*/ /*@null@*/ ExprItem *); + /*@only@*/ /*@null@*/ ExprItem *, + unsigned long lindex); /*@only@*/ ExprItem *ExprSym(/*@keep@*/ symrec *); /*@only@*/ ExprItem *ExprExpr(/*@keep@*/ expr *); @@ -35,12 +36,12 @@ void expr_initialize(arch *a); /*@only@*/ ExprItem *ExprFloat(/*@keep@*/ floatnum *); /*@only@*/ ExprItem *ExprReg(unsigned long reg); -#define expr_new_tree(l,o,r) \ - expr_new ((o), ExprExpr(l), ExprExpr(r)) -#define expr_new_branch(o,r) \ - expr_new ((o), ExprExpr(r), (ExprItem *)NULL) -#define expr_new_ident(r) \ - expr_new (EXPR_IDENT, (r), (ExprItem *)NULL) +#define expr_new_tree(l,o,r,i) \ + expr_new ((o), ExprExpr(l), ExprExpr(r), i) +#define expr_new_branch(o,r,i) \ + expr_new ((o), ExprExpr(r), (ExprItem *)NULL, i) +#define expr_new_ident(r,i) \ + expr_new (EXPR_IDENT, (r), (ExprItem *)NULL, i) /* allocates and makes an exact duplicate of e */ expr *expr_copy(const expr *e); diff --git a/libyasm/floatnum.c b/libyasm/floatnum.c index 6f98dc26..f7b4fe60 100644 --- a/libyasm/floatnum.c +++ b/libyasm/floatnum.c @@ -511,10 +511,11 @@ floatnum_delete(floatnum *flt) } void -floatnum_calc(floatnum *acc, ExprOp op, /*@unused@*/ floatnum *operand) +floatnum_calc(floatnum *acc, ExprOp op, /*@unused@*/ floatnum *operand, + unsigned long lindex) { if (op != EXPR_NEG) - Error(_("Unsupported floating-point arithmetic operation")); + Error(lindex, _("Unsupported floating-point arithmetic operation")); else acc->sign ^= 1; } diff --git a/libyasm/floatnum.h b/libyasm/floatnum.h index c699e317..a5d05bba 100644 --- a/libyasm/floatnum.h +++ b/libyasm/floatnum.h @@ -32,7 +32,8 @@ void floatnum_shutdown(void); void floatnum_delete(/*@only@*/ floatnum *flt); /* calculation function: acc = acc op operand */ -void floatnum_calc(floatnum *acc, ExprOp op, floatnum *operand); +void floatnum_calc(floatnum *acc, ExprOp op, floatnum *operand, + unsigned long lindex); /* The get functions return nonzero if flt can't fit into that size format: * -1 if underflow occurred, 1 if overflow occurred. diff --git a/libyasm/intnum.c b/libyasm/intnum.c index b066c712..3ca4775d 100644 --- a/libyasm/intnum.c +++ b/libyasm/intnum.c @@ -56,7 +56,7 @@ intnum_shutdown(void) } intnum * -intnum_new_dec(char *str) +intnum_new_dec(char *str, unsigned long lindex) { intnum *intn = xmalloc(sizeof(intnum)); @@ -68,7 +68,7 @@ intnum_new_dec(char *str) } if (BitVector_from_Dec_static(conv_bv, (unsigned char *)str) == ErrCode_Ovfl) - Warning(_("Numeric constant too large for internal format")); + Warning(lindex, _("Numeric constant too large for internal format")); if (Set_Max(conv_bv) < 32) { intn->type = INTNUM_UL; intn->val.ul = BitVector_Chunk_Read(conv_bv, 32, 0); @@ -81,14 +81,14 @@ intnum_new_dec(char *str) } intnum * -intnum_new_bin(char *str) +intnum_new_bin(char *str, unsigned long lindex) { intnum *intn = xmalloc(sizeof(intnum)); intn->origsize = (unsigned char)strlen(str); if(intn->origsize > BITVECT_ALLOC_SIZE) - Warning(_("Numeric constant too large for internal format")); + Warning(lindex, _("Numeric constant too large for internal format")); if (!conv_bv) { conv_bv = BitVector_Create(BITVECT_ALLOC_SIZE, FALSE); @@ -107,14 +107,14 @@ intnum_new_bin(char *str) } intnum * -intnum_new_oct(char *str) +intnum_new_oct(char *str, unsigned long lindex) { intnum *intn = xmalloc(sizeof(intnum)); intn->origsize = strlen(str)*3; if(intn->origsize > BITVECT_ALLOC_SIZE) - Warning(_("Numeric constant too large for internal format")); + Warning(lindex, _("Numeric constant too large for internal format")); if (!conv_bv) { conv_bv = BitVector_Create(BITVECT_ALLOC_SIZE, FALSE); @@ -133,14 +133,14 @@ intnum_new_oct(char *str) } intnum * -intnum_new_hex(char *str) +intnum_new_hex(char *str, unsigned long lindex) { intnum *intn = xmalloc(sizeof(intnum)); intn->origsize = strlen(str)*4; if(intn->origsize > BITVECT_ALLOC_SIZE) - Warning(_("Numeric constant too large for internal format")); + Warning(lindex, _("Numeric constant too large for internal format")); if (!conv_bv) { conv_bv = BitVector_Create(BITVECT_ALLOC_SIZE, FALSE); @@ -160,13 +160,14 @@ intnum_new_hex(char *str) /*@-usedef -compdef -uniondef@*/ intnum * -intnum_new_charconst_nasm(const char *str) +intnum_new_charconst_nasm(const char *str, unsigned long lindex) { intnum *intn = xmalloc(sizeof(intnum)); size_t len = strlen(str); if (len > 4) - Warning(_("character constant too large, ignoring trailing characters")); + Warning(lindex, + _("character constant too large, ignoring trailing characters")); intn->val.ul = 0; intn->type = INTNUM_UL; diff --git a/libyasm/intnum.h b/libyasm/intnum.h index 0352b441..99f1a105 100644 --- a/libyasm/intnum.h +++ b/libyasm/intnum.h @@ -25,12 +25,13 @@ /* Clean up internal allocations */ void intnum_shutdown(void); -/*@only@*/ intnum *intnum_new_dec(char *str); -/*@only@*/ intnum *intnum_new_bin(char *str); -/*@only@*/ intnum *intnum_new_oct(char *str); -/*@only@*/ intnum *intnum_new_hex(char *str); +/*@only@*/ intnum *intnum_new_dec(char *str, unsigned long lindex); +/*@only@*/ intnum *intnum_new_bin(char *str, unsigned long lindex); +/*@only@*/ intnum *intnum_new_oct(char *str, unsigned long lindex); +/*@only@*/ intnum *intnum_new_hex(char *str, unsigned long lindex); /* convert character constant to integer value, using NASM rules */ -/*@only@*/ intnum *intnum_new_charconst_nasm(const char *str); +/*@only@*/ intnum *intnum_new_charconst_nasm(const char *str, + unsigned long lindex); /*@only@*/ intnum *intnum_new_uint(unsigned long i); /*@only@*/ intnum *intnum_new_int(long i); /*@only@*/ intnum *intnum_copy(const intnum *intn); diff --git a/libyasm/linemgr.c b/libyasm/linemgr.c index 61763af5..718b01b9 100644 --- a/libyasm/linemgr.c +++ b/libyasm/linemgr.c @@ -1,7 +1,7 @@ /* - * Global variables + * YASM assembler line manager (for parse stage) * - * Copyright (C) 2001 Peter Johnson + * Copyright (C) 2002 Peter Johnson * * This file is part of YASM. * @@ -24,7 +24,7 @@ #include "hamt.h" -#include "globals.h" +#include "linemgr.h" /* Source lines tracking */ @@ -48,14 +48,11 @@ typedef struct line_index_mapping { } line_index_mapping; /* Shared storage for filenames */ -static /*@only@*/ /*@null@*/ HAMT *filename_table = NULL; +static /*@only@*/ /*@null@*/ HAMT *filename_table; /* Virtual line number. Uniquely specifies every line read by the parser. */ -unsigned long line_index = 1; -static /*@only@*/ /*@null@*/ line_index_mapping_head *line_index_map = NULL; - -/* Global assembler options. */ -unsigned int asm_options = 0; +static unsigned long line_index; +static /*@only@*/ /*@null@*/ line_index_mapping_head *line_index_map; static void filename_delete_one(/*@only@*/ void *d) @@ -63,21 +60,15 @@ filename_delete_one(/*@only@*/ void *d) xfree(d); } -void -line_set(const char *filename, unsigned long line, unsigned long line_inc) +static void +yasm_linemgr_set(const char *filename, unsigned long line, + unsigned long line_inc) { char *copy; int replace = 0; line_index_mapping *mapping; /* Create a new mapping in the map */ - if (!line_index_map) { - /* initialize vector */ - line_index_map = xmalloc(sizeof(line_index_mapping_head)); - line_index_map->vector = xmalloc(8*sizeof(line_index_mapping)); - line_index_map->size = 0; - line_index_map->allocated = 8; - } if (line_index_map->size >= line_index_map->allocated) { /* allocate another size bins when full for 2x space */ line_index_map->vector = xrealloc(line_index_map->vector, @@ -91,8 +82,6 @@ line_set(const char *filename, unsigned long line, unsigned long line_inc) /* Copy the filename (via shared storage) */ copy = xstrdup(filename); - if (!filename_table) - filename_table = HAMT_new(); /*@-aliasunique@*/ mapping->filename = HAMT_insert(filename_table, copy, copy, &replace, filename_delete_one); @@ -103,8 +92,22 @@ line_set(const char *filename, unsigned long line, unsigned long line_inc) mapping->line_inc = line_inc; } -void -line_shutdown(void) +static void +yasm_linemgr_initialize(void) +{ + filename_table = HAMT_new(); + + line_index = 1; + + /* initialize mapping vector */ + line_index_map = xmalloc(sizeof(line_index_mapping_head)); + line_index_map->vector = xmalloc(8*sizeof(line_index_mapping)); + line_index_map->size = 0; + line_index_map->allocated = 8; +} + +static void +yasm_linemgr_cleanup(void) { if (line_index_map) { xfree(line_index_map->vector); @@ -118,8 +121,21 @@ line_shutdown(void) } } -void -line_lookup(unsigned long lindex, const char **filename, unsigned long *line) +static unsigned long +yasm_linemgr_get_current(void) +{ + return line_index; +} + +static unsigned long +yasm_linemgr_goto_next(void) +{ + return ++line_index; +} + +static void +yasm_linemgr_lookup(unsigned long lindex, const char **filename, + unsigned long *line) { line_index_mapping *mapping; unsigned long vindex, step; @@ -144,3 +160,12 @@ line_lookup(unsigned long lindex, const char **filename, unsigned long *line) *filename = mapping->filename; *line = mapping->line+mapping->line_inc*(lindex-mapping->index); } + +linemgr yasm_linemgr = { + yasm_linemgr_initialize, + yasm_linemgr_cleanup, + yasm_linemgr_get_current, + yasm_linemgr_goto_next, + yasm_linemgr_set, + yasm_linemgr_lookup +}; diff --git a/libyasm/linemgr.h b/libyasm/linemgr.h index a38ed352..85d474e0 100644 --- a/libyasm/linemgr.h +++ b/libyasm/linemgr.h @@ -1,7 +1,7 @@ /* $IdPath$ - * Globals header file + * Line manager (for parse stage) header file * - * Copyright (C) 2001 Peter Johnson + * Copyright (C) 2002 Peter Johnson * * This file is part of YASM. * @@ -19,19 +19,34 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#ifndef YASM_GLOBALS_H -#define YASM_GLOBALS_H +#ifndef YASM_LINEMGR_H +#define YASM_LINEMGR_H -/* Virtual line number. Uniquely specifies every line read by the parser. */ -extern unsigned long line_index; +struct linemgr { + /* Initialize cur_lindex and any manager internal data structures. */ + void (*initialize) (void); -/* Global assembler options. */ -extern unsigned int asm_options; + /* Cleans up any memory allocated by initialize. */ + void (*cleanup) (void); -void line_set(const char *filename, unsigned long line, - unsigned long line_inc); -void line_shutdown(void); -void line_lookup(unsigned long lindex, /*@out@*/ const char **filename, - /*@out@*/ unsigned long *line); + /* Returns the current line index. */ + unsigned long (*get_current) (void); + + /* Goes to the next line (increments the current line index), returns + * the current (new) line index. + */ + unsigned long (*goto_next) (void); + + /* Sets a new file/line association starting point at the current line + * index. line_inc indicates how much the "real" line is incremented by + * for each line index increment (0 is perfectly legal). + */ + void (*set) (const char *filename, unsigned long line, + unsigned long line_inc); + + /* Look up the associated actual file and line for a line index. */ + void (*lookup) (unsigned long lindex, /*@out@*/ const char **filename, + /*@out@*/ unsigned long *line); +}; #endif diff --git a/libyasm/objfmt.h b/libyasm/objfmt.h index 24f9b935..55f7b9cc 100644 --- a/libyasm/objfmt.h +++ b/libyasm/objfmt.h @@ -76,7 +76,8 @@ struct objfmt { */ /*@observer@*/ /*@null@*/ section * (*sections_switch)(sectionhead *headp, valparamhead *valparams, - /*@null@*/ valparamhead *objext_valparams); + /*@null@*/ valparamhead *objext_valparams, + unsigned long lindex); /* Object format-specific data handling functions for sections. * May be NULL if no data is ever allocated in sections_switch(). @@ -88,11 +89,14 @@ struct objfmt { * May be NULL if objfmt doesn't care about such declarations. */ void (*extern_declare)(symrec *sym, - /*@null@*/ valparamhead *objext_valparams); + /*@null@*/ valparamhead *objext_valparams, + unsigned long lindex); void (*global_declare)(symrec *sym, - /*@null@*/ valparamhead *objext_valparams); + /*@null@*/ valparamhead *objext_valparams, + unsigned long lindex); void (*common_declare)(symrec *sym, /*@only@*/ expr *size, - /*@null@*/ valparamhead *objext_valparams); + /*@null@*/ valparamhead *objext_valparams, + unsigned long lindex); /* May be NULL if symrec_set_of_data() is never called. */ void (*symrec_data_delete)(/*@only@*/ void *data); @@ -104,7 +108,7 @@ struct objfmt { */ int (*directive)(const char *name, valparamhead *valparams, /*@null@*/ valparamhead *objext_valparams, - sectionhead *headp); + sectionhead *headp, unsigned long lindex); /* Bytecode objfmt data (BC_OBJFMT_DATA) handling functions. * May be NULL if no BC_OBJFMT_DATA is ever allocated by the object format. diff --git a/libyasm/parser.h b/libyasm/parser.h index 4c934bec..01b0db32 100644 --- a/libyasm/parser.h +++ b/libyasm/parser.h @@ -50,8 +50,8 @@ struct parser { * This function returns the starting section of a linked list of sections * (whatever was in the file). */ - sectionhead *(*do_parse) (preproc *pp, arch *a, objfmt *of, FILE *f, - const char *in_filename); + sectionhead *(*do_parse) (preproc *pp, arch *a, objfmt *of, linemgr *lm, + FILE *f, const char *in_filename); }; /* Generic functions for all parsers - implemented in src/parser.c */ diff --git a/libyasm/preproc.h b/libyasm/preproc.h index 7b0f1d0a..a1e2b9fe 100644 --- a/libyasm/preproc.h +++ b/libyasm/preproc.h @@ -43,7 +43,7 @@ struct preproc { /* Gets more preprocessed source code (up to max_size bytes) into buf. * Note that more than a single line may be returned in buf. */ - size_t (*input) (/*@out@*/ char *buf, size_t max_size); + size_t (*input) (/*@out@*/ char *buf, size_t max_size, linemgr *lm); }; #endif diff --git a/libyasm/section.c b/libyasm/section.c index 88d10fff..430c73bf 100644 --- a/libyasm/section.c +++ b/libyasm/section.c @@ -22,7 +22,6 @@ #include "util.h" /*@unused@*/ RCSID("$IdPath$"); -#include "globals.h" #include "errwarn.h" #include "intnum.h" #include "expr.h" @@ -72,7 +71,7 @@ sections_initialize(sectionhead *headp, objfmt *of) vp_new(vp, xstrdup(of->default_section_name), NULL); vps_initialize(&vps); vps_append(&vps, vp); - s = of->sections_switch(headp, &vps, NULL); + s = of->sections_switch(headp, &vps, NULL, 0); vps_delete(&vps); return s; @@ -82,7 +81,8 @@ sections_initialize(sectionhead *headp, objfmt *of) /*@-onlytrans@*/ section * sections_switch_general(sectionhead *headp, const char *name, - unsigned long start, int res_only, int *isnew) + unsigned long start, int res_only, int *isnew, + unsigned long lindex) { section *s; @@ -107,7 +107,7 @@ sections_switch_general(sectionhead *headp, const char *name, s->data.general.name = xstrdup(name); s->data.general.of = NULL; s->data.general.of_data = NULL; - s->start = expr_new_ident(ExprInt(intnum_new_uint(start))); + s->start = expr_new_ident(ExprInt(intnum_new_uint(start)), lindex); bcs_initialize(&s->bc); s->opt_flags = 0; @@ -260,10 +260,10 @@ section_get_name(const section *sect) } void -section_set_start(section *sect, unsigned long start) +section_set_start(section *sect, unsigned long start, unsigned long lindex) { expr_delete(sect->start); - sect->start = expr_new_ident(ExprInt(intnum_new_uint(start))); + sect->start = expr_new_ident(ExprInt(intnum_new_uint(start)), lindex); } const expr * diff --git a/libyasm/section.h b/libyasm/section.h index 7645e40c..c7f5e954 100644 --- a/libyasm/section.h +++ b/libyasm/section.h @@ -28,7 +28,8 @@ const char *name, unsigned long start, int res_only, - /*@out@*/ int *isnew); + /*@out@*/ int *isnew, + unsigned long lindex); /*@dependent@*/ section *sections_switch_absolute(sectionhead *headp, /*@keep@*/ expr *start); @@ -63,7 +64,8 @@ int sections_traverse(sectionhead *headp, /*@null@*/ void *d, /*@observer@*/ /*@null@*/ const char *section_get_name(const section *sect); -void section_set_start(section *sect, unsigned long start); +void section_set_start(section *sect, unsigned long start, + unsigned long lindex); /*@observer@*/ const expr *section_get_start(const section *sect); void section_delete(/*@only@*/ section *sect); diff --git a/libyasm/symrec.c b/libyasm/symrec.c index 876c1c32..fe32457b 100644 --- a/libyasm/symrec.c +++ b/libyasm/symrec.c @@ -28,7 +28,6 @@ #include "hamt.h" -#include "globals.h" #include "errwarn.h" #include "floatnum.h" #include "expr.h" @@ -177,27 +176,29 @@ symrec_traverse(void *d, int (*func) (symrec *sym, void *d)) } symrec * -symrec_use(const char *name) +symrec_use(const char *name, unsigned long lindex) { symrec *rec = symrec_get_or_new(name, 1); if (rec->line == 0) - rec->line = line_index; /* set line number of first use */ + rec->line = lindex; /* set line number of first use */ rec->status |= SYM_USED; return rec; } static /*@dependent@*/ symrec * -symrec_define(const char *name, SymType type, int in_table) +symrec_define(const char *name, SymType type, int in_table, + unsigned long lindex) { symrec *rec = symrec_get_or_new(name, in_table); /* Has it been defined before (either by DEFINED or COMMON/EXTERN)? */ if ((rec->status & SYM_DEFINED) || (rec->visibility & (SYM_COMMON | SYM_EXTERN))) { - Error(_("duplicate definition of `%s'; first defined on line %lu"), + Error(lindex, + _("duplicate definition of `%s'; first defined on line %lu"), name, rec->line); } else { - rec->line = line_index; /* set line number of definition */ + rec->line = lindex; /* set line number of definition */ rec->type = type; rec->status |= SYM_DEFINED; } @@ -205,9 +206,9 @@ symrec_define(const char *name, SymType type, int in_table) } symrec * -symrec_define_equ(const char *name, expr *e) +symrec_define_equ(const char *name, expr *e, unsigned long lindex) { - symrec *rec = symrec_define(name, SYM_EQU, 1); + symrec *rec = symrec_define(name, SYM_EQU, 1, lindex); rec->value.expn = e; rec->status |= SYM_VALUED; return rec; @@ -215,16 +216,16 @@ symrec_define_equ(const char *name, expr *e) symrec * symrec_define_label(const char *name, section *sect, bytecode *precbc, - int in_table) + int in_table, unsigned long lindex) { - symrec *rec = symrec_define(name, SYM_LABEL, in_table); + symrec *rec = symrec_define(name, SYM_LABEL, in_table, lindex); rec->value.label.sect = sect; rec->value.label.bc = precbc; return rec; } symrec * -symrec_declare(const char *name, SymVisibility vis) +symrec_declare(const char *name, SymVisibility vis, unsigned long lindex) { symrec *rec = symrec_get_or_new(name, 1); @@ -246,7 +247,8 @@ symrec_declare(const char *name, SymVisibility vis) ((rec->visibility & SYM_EXTERN) && (vis == SYM_EXTERN))))) rec->visibility |= vis; else - Error(_("duplicate definition of `%s'; first defined on line %lu"), + Error(lindex, + _("duplicate definition of `%s'; first defined on line %lu"), name, rec->line); return rec; } @@ -323,7 +325,7 @@ symrec_parser_finalize_checksym(symrec *sym, /*@unused@*/ /*@null@*/ void *d) /* error if a symbol is used but never defined or extern/common declared */ if ((sym->status & SYM_USED) && !(sym->status & SYM_DEFINED) && !(sym->visibility & (SYM_EXTERN | SYM_COMMON))) { - ErrorAt(sym->line, _("undefined symbol `%s' (first use)"), sym->name); + Error(sym->line, _("undefined symbol `%s' (first use)"), sym->name); if (sym->line < firstundef_line) firstundef_line = sym->line; } @@ -337,8 +339,8 @@ symrec_parser_finalize(void) firstundef_line = ULONG_MAX; symrec_traverse(NULL, symrec_parser_finalize_checksym); if (firstundef_line < ULONG_MAX) - ErrorAt(firstundef_line, - _(" (Each undefined symbol is reported only once.)")); + Error(firstundef_line, + _(" (Each undefined symbol is reported only once.)")); } void @@ -389,9 +391,6 @@ symrec_print_all(FILE *f, int indent_level) void symrec_print(FILE *f, int indent_level, const symrec *sym) { - const char *filename; - unsigned long line; - switch (sym->type) { case SYM_UNKNOWN: fprintf(f, "%*s-Unknown (Common/Extern)-\n", indent_level, ""); @@ -451,7 +450,5 @@ symrec_print(FILE *f, int indent_level, const symrec *sym) fprintf(f, "%*sUNKNOWN\n", indent_level+1, ""); } - line_lookup(sym->line, &filename, &line); - fprintf(f, "%*sFilename=\"%s\" Line Number=%lu\n", indent_level, "", - filename, line); + fprintf(f, "%*sLine Index=%lu\n", indent_level, "", sym->line); } diff --git a/libyasm/symrec.h b/libyasm/symrec.h index 49ac2b91..47d148e8 100644 --- a/libyasm/symrec.h +++ b/libyasm/symrec.h @@ -22,9 +22,9 @@ #ifndef YASM_SYMREC_H #define YASM_SYMREC_H -/*@dependent@*/ symrec *symrec_use(const char *name); -/*@dependent@*/ symrec *symrec_define_equ(const char *name, - /*@keep@*/ expr *e); +/*@dependent@*/ symrec *symrec_use(const char *name, unsigned long lindex); +/*@dependent@*/ symrec *symrec_define_equ(const char *name, /*@keep@*/ expr *e, + unsigned long lindex); /* in_table specifies if the label should be inserted into the symbol table. * All labels are memory managed internally. */ @@ -32,8 +32,10 @@ /*@dependent@*/ /*@null@*/ section *sect, /*@dependent@*/ /*@null@*/ - bytecode *precbc, int in_table); -/*@dependent@*/ symrec *symrec_declare(const char *name, SymVisibility vis); + bytecode *precbc, int in_table, + unsigned long lindex); +/*@dependent@*/ symrec *symrec_declare(const char *name, SymVisibility vis, + unsigned long lindex); /*@observer@*/ const char *symrec_get_name(const symrec *sym); SymVisibility symrec_get_visibility(const symrec *sym); diff --git a/libyasm/valparam.c b/libyasm/valparam.c index 2339af9b..ddae81d2 100644 --- a/libyasm/valparam.c +++ b/libyasm/valparam.c @@ -22,7 +22,6 @@ #include "util.h" /*@unused@*/ RCSID("$IdPath$"); -#include "globals.h" #include "expr.h" diff --git a/modules/Makefile.inc b/modules/Makefile.inc index 5eca5ae8..1926b5ae 100644 --- a/modules/Makefile.inc +++ b/modules/Makefile.inc @@ -11,8 +11,7 @@ libyasm_la_SOURCES = \ src/expr-int.h \ src/symrec.c \ src/symrec.h \ - src/globals.c \ - src/globals.h \ + src/linemgr.h \ src/util.h \ src/coretype.h \ src/file.c \ @@ -47,7 +46,8 @@ yasm_SOURCES += \ src/objfmt.c \ src/parser.c \ src/module.h \ - src/module.c + src/module.c \ + src/linemgr.c EXTRA_DIST += \ diff --git a/modules/arch/x86/x86arch.c b/modules/arch/x86/x86arch.c index bf962947..36d3db9f 100644 --- a/modules/arch/x86/x86arch.c +++ b/modules/arch/x86/x86arch.c @@ -24,7 +24,6 @@ #include "file.h" -#include "globals.h" #include "errwarn.h" #include "intnum.h" #include "floatnum.h" @@ -42,7 +41,7 @@ unsigned char yasm_x86_LTX_mode_bits = 0; int x86_directive(const char *name, valparamhead *valparams, /*@unused@*/ /*@null@*/ valparamhead *objext_valparams, - /*@unused@*/ sectionhead *headp) + /*@unused@*/ sectionhead *headp, unsigned long lindex) { valparam *vp; const intnum *intn; @@ -54,7 +53,7 @@ x86_directive(const char *name, valparamhead *valparams, (lval = intnum_get_int(intn)) && (lval == 16 || lval == 32)) yasm_x86_LTX_mode_bits = (unsigned char)lval; else - Error(_("invalid argument to [%s]"), "BITS"); + Error(lindex, _("invalid argument to [%s]"), "BITS"); return 0; } else return 1; @@ -132,11 +131,12 @@ x86_segreg_print(FILE *f, unsigned long segreg) } void -x86_handle_prefix(bytecode *bc, const unsigned long data[4]) +x86_handle_prefix(bytecode *bc, const unsigned long data[4], + unsigned long lindex) { switch((x86_parse_insn_prefix)data[0]) { case X86_LOCKREP: - x86_bc_insn_set_lockrep_prefix(bc, (unsigned char)data[1]); + x86_bc_insn_set_lockrep_prefix(bc, (unsigned char)data[1], lindex); break; case X86_ADDRSIZE: x86_bc_insn_addrsize_override(bc, (unsigned char)data[1]); @@ -148,15 +148,17 @@ x86_handle_prefix(bytecode *bc, const unsigned long data[4]) } void -x86_handle_seg_prefix(bytecode *bc, unsigned long segreg) +x86_handle_seg_prefix(bytecode *bc, unsigned long segreg, unsigned long lindex) { - x86_ea_set_segment(x86_bc_insn_get_ea(bc), (unsigned char)(segreg>>8)); + x86_ea_set_segment(x86_bc_insn_get_ea(bc), (unsigned char)(segreg>>8), + lindex); } void -x86_handle_seg_override(effaddr *ea, unsigned long segreg) +x86_handle_seg_override(effaddr *ea, unsigned long segreg, + unsigned long lindex) { - x86_ea_set_segment(ea, (unsigned char)(segreg>>8)); + x86_ea_set_segment(ea, (unsigned char)(segreg>>8), lindex); } /* Define arch structure -- see arch.h for details */ diff --git a/modules/arch/x86/x86arch.h b/modules/arch/x86/x86arch.h index 824912b0..4674070b 100644 --- a/modules/arch/x86/x86arch.h +++ b/modules/arch/x86/x86arch.h @@ -62,7 +62,8 @@ typedef enum { JR_NEAR_FORCED } x86_jmprel_opcode_sel; -void x86_ea_set_segment(/*@null@*/ effaddr *ea, unsigned char segment); +void x86_ea_set_segment(/*@null@*/ effaddr *ea, unsigned char segment, + unsigned long lindex); void x86_ea_set_disponly(effaddr *ea); effaddr *x86_ea_new_reg(unsigned char reg); effaddr *x86_ea_new_imm(/*@keep@*/expr *imm, unsigned char im_len); @@ -72,16 +73,15 @@ effaddr *x86_ea_new_expr(/*@keep@*/ expr *e); void x86_bc_insn_opersize_override(bytecode *bc, unsigned char opersize); void x86_bc_insn_addrsize_override(bytecode *bc, unsigned char addrsize); -void x86_bc_insn_set_lockrep_prefix(bytecode *bc, unsigned char prefix); - -void x86_set_jmprel_opcode_sel(x86_jmprel_opcode_sel *old_sel, - x86_jmprel_opcode_sel new_sel); +void x86_bc_insn_set_lockrep_prefix(bytecode *bc, unsigned char prefix, + unsigned long lindex); /* Structure with *all* inputs passed to x86_bytecode_new_insn(). * IMPORTANT: ea_ptr and im_ptr cannot be reused or freed after calling the * function (it doesn't make a copy). */ typedef struct x86_new_insn_data { + unsigned long lindex; /*@keep@*/ /*@null@*/ effaddr *ea; /*@keep@*/ /*@null@*/ expr *imm; unsigned char opersize; @@ -100,6 +100,7 @@ bytecode *x86_bc_new_insn(x86_new_insn_data *d); * Pass 0 for the opcode_len if that version of the opcode doesn't exist. */ typedef struct x86_new_jmprel_data { + unsigned long lindex; /*@keep@*/ expr *target; x86_jmprel_opcode_sel op_sel; unsigned char short_op_len; @@ -128,26 +129,31 @@ int x86_expr_checkea(expr **ep, unsigned char *addrsize, unsigned char bits, unsigned char *v_sib, unsigned char *n_sib, calc_bc_dist_func calc_bc_dist); -void x86_switch_cpu(const char *cpuid); +void x86_switch_cpu(const char *cpuid, unsigned long lindex); arch_check_id_retval x86_check_identifier(unsigned long data[2], - const char *id); + const char *id, + unsigned long lindex); int x86_directive(const char *name, valparamhead *valparams, /*@null@*/ valparamhead *objext_valparams, - sectionhead *headp); + sectionhead *headp, unsigned long lindex); /*@null@*/ bytecode *x86_new_insn(const unsigned long data[2], int num_operands, /*@null@*/ insn_operandhead *operands, section *cur_section, - /*@null@*/ bytecode *prev_bc); + /*@null@*/ bytecode *prev_bc, + unsigned long lindex); -void x86_handle_prefix(bytecode *bc, const unsigned long data[4]); +void x86_handle_prefix(bytecode *bc, const unsigned long data[4], + unsigned long lindex); -void x86_handle_seg_prefix(bytecode *bc, unsigned long segreg); +void x86_handle_seg_prefix(bytecode *bc, unsigned long segreg, + unsigned long lindex); -void x86_handle_seg_override(effaddr *ea, unsigned long segreg); +void x86_handle_seg_override(effaddr *ea, unsigned long segreg, + unsigned long lindex); int x86_floatnum_tobytes(const floatnum *flt, unsigned char **bufp, unsigned long valsize, const expr *e); diff --git a/modules/arch/x86/x86bc.c b/modules/arch/x86/x86bc.c index d6aa652c..26d76055 100644 --- a/modules/arch/x86/x86bc.c +++ b/modules/arch/x86/x86bc.c @@ -24,7 +24,6 @@ #include "file.h" -#include "globals.h" #include "errwarn.h" #include "intnum.h" #include "expr.h" @@ -125,7 +124,7 @@ x86_bc_new_insn(x86_new_insn_data *d) x86_insn *insn; insn = (x86_insn *)bc_new_common((bytecode_type)X86_BC_INSN, - sizeof(x86_insn)); + sizeof(x86_insn), d->lindex); insn->ea = (x86_effaddr *)d->ea; if (d->ea) { @@ -165,15 +164,15 @@ x86_bc_new_jmprel(x86_new_jmprel_data *d) x86_jmprel *jmprel; jmprel = (x86_jmprel *)bc_new_common((bytecode_type)X86_BC_JMPREL, - sizeof(x86_jmprel)); + sizeof(x86_jmprel), d->lindex); jmprel->target = d->target; jmprel->op_sel = d->op_sel; if ((d->op_sel == JR_SHORT_FORCED) && (d->near_op_len == 0)) - Error(_("no SHORT form of that jump instruction exists")); + Error(d->lindex, _("no SHORT form of that jump instruction exists")); if ((d->op_sel == JR_NEAR_FORCED) && (d->short_op_len == 0)) - Error(_("no NEAR form of that jump instruction exists")); + Error(d->lindex, _("no NEAR form of that jump instruction exists")); jmprel->shortop.opcode[0] = d->short_op[0]; jmprel->shortop.opcode[1] = d->short_op[1]; @@ -196,7 +195,7 @@ x86_bc_new_jmprel(x86_new_jmprel_data *d) /*@=compmempass =mustfree@*/ void -x86_ea_set_segment(effaddr *ea, unsigned char segment) +x86_ea_set_segment(effaddr *ea, unsigned char segment, unsigned long lindex) { x86_effaddr *x86_ea = (x86_effaddr *)ea; @@ -204,7 +203,7 @@ x86_ea_set_segment(effaddr *ea, unsigned char segment) return; if (segment != 0 && x86_ea->segment != 0) - Warning(_("multiple segment overrides, using leftmost")); + Warning(lindex, _("multiple segment overrides, using leftmost")); x86_ea->segment = segment; } @@ -347,7 +346,8 @@ x86_bc_insn_addrsize_override(bytecode *bc, unsigned char addrsize) } void -x86_bc_insn_set_lockrep_prefix(bytecode *bc, unsigned char prefix) +x86_bc_insn_set_lockrep_prefix(bytecode *bc, unsigned char prefix, + unsigned long lindex) { x86_insn *insn; x86_jmprel *jmprel; @@ -370,24 +370,11 @@ x86_bc_insn_set_lockrep_prefix(bytecode *bc, unsigned char prefix) } if (*lockrep_pre != 0) - Warning(_("multiple LOCK or REP prefixes, using leftmost")); + Warning(lindex, _("multiple LOCK or REP prefixes, using leftmost")); *lockrep_pre = prefix; } -void -x86_set_jmprel_opcode_sel(x86_jmprel_opcode_sel *old_sel, - x86_jmprel_opcode_sel new_sel) -{ - if (!old_sel) - return; - - if (new_sel != JR_NONE && ((*old_sel == JR_SHORT_FORCED) || - (*old_sel == JR_NEAR_FORCED))) - Warning(_("multiple SHORT or NEAR specifiers, using leftmost")); - *old_sel = new_sel; -} - void x86_bc_delete(bytecode *bc) { @@ -667,20 +654,20 @@ x86_bc_resolve_jmprel(x86_jmprel *jmprel, unsigned long *len, int save, temp = expr_copy(jmprel->target); num = expr_get_intnum(&temp, calc_bc_dist); if (!num) { - ErrorAt(bc->line, - _("short jump target external or out of segment")); + Error(bc->line, + _("short jump target external or out of segment")); return BC_RESOLVE_ERROR | BC_RESOLVE_UNKNOWN_LEN; } else { rel = intnum_get_int(num); rel -= jmprel->shortop.opcode_len+1; /* does a short form exist? */ if (jmprel->shortop.opcode_len == 0) { - ErrorAt(bc->line, _("short jump does not exist")); + Error(bc->line, _("short jump does not exist")); return BC_RESOLVE_ERROR | BC_RESOLVE_UNKNOWN_LEN; } /* short displacement must fit in -128 <= rel <= +127 */ if (rel < -128 || rel > 127) { - ErrorAt(bc->line, _("short jump out of range")); + Error(bc->line, _("short jump out of range")); return BC_RESOLVE_ERROR | BC_RESOLVE_UNKNOWN_LEN; } } @@ -691,7 +678,7 @@ x86_bc_resolve_jmprel(x86_jmprel *jmprel, unsigned long *len, int save, jrshort = 0; if (save) { if (jmprel->nearop.opcode_len == 0) { - ErrorAt(bc->line, _("near jump does not exist")); + Error(bc->line, _("near jump does not exist")); return BC_RESOLVE_ERROR | BC_RESOLVE_UNKNOWN_LEN; } } @@ -726,7 +713,8 @@ x86_bc_resolve_jmprel(x86_jmprel *jmprel, unsigned long *len, int save, * it to actually be within short range). */ if (save) { - ErrorAt(bc->line, _("short jump out of range (near jump does not exist)")); + Error(bc->line, + _("short jump out of range (near jump does not exist)")); return BC_RESOLVE_ERROR | BC_RESOLVE_UNKNOWN_LEN; } jrshort = 1; @@ -742,8 +730,8 @@ x86_bc_resolve_jmprel(x86_jmprel *jmprel, unsigned long *len, int save, jrshort = 0; } else { if (save) { - ErrorAt(bc->line, - _("short jump out of range (near jump does not exist)")); + Error(bc->line, + _("short jump out of range (near jump does not exist)")); return BC_RESOLVE_ERROR | BC_RESOLVE_UNKNOWN_LEN; } jrshort = 1; @@ -928,7 +916,7 @@ x86_bc_tobytes_jmprel(x86_jmprel *jmprel, unsigned char **bufp, case JR_NEAR: /* 2/4 byte relative displacement (depending on operand size) */ if (jmprel->nearop.opcode_len == 0) { - ErrorAt(bc->line, _("near jump does not exist")); + Error(bc->line, _("near jump does not exist")); return 1; } diff --git a/modules/arch/x86/x86expr.c b/modules/arch/x86/x86expr.c index 20c1b677..ea022f6d 100644 --- a/modules/arch/x86/x86expr.c +++ b/modules/arch/x86/x86expr.c @@ -24,7 +24,6 @@ #include "bitvect.h" -#include "globals.h" #include "errwarn.h" #include "intnum.h" #include "floatnum.h" @@ -379,7 +378,7 @@ x86_checkea_calc_displen(expr **ep, unsigned int wordsize, int noreg, */ if (!intnum_check_size(intn, (size_t)wordsize, 0) && !intnum_check_size(intn, 1, 1)) { - ErrorAt(e->line, _("invalid effective address")); + Error(e->line, _("invalid effective address")); return 0; } @@ -443,8 +442,8 @@ x86_checkea_calc_displen(expr **ep, unsigned int wordsize, int noreg, case 2: case 4: if (wordsize != *displen) { - ErrorAt(e->line, - _("invalid effective address (displacement size)")); + Error(e->line, + _("invalid effective address (displacement size)")); return 0; } /* TODO: Add optional warning here about 2/4 not being valid @@ -536,7 +535,7 @@ x86_expr_checkea(expr **ep, unsigned char *addrsize, unsigned char bits, calc_bc_dist)) { case 0: e = *ep; - ErrorAt(e->line, _("invalid effective address")); + Error(e->line, _("invalid effective address")); return 0; case 1: return 1; @@ -558,7 +557,7 @@ x86_expr_checkea(expr **ep, unsigned char *addrsize, unsigned char bits, */ for (i=0; i<8; i++) { if (reg32mult[i] < 0) { - ErrorAt(e->line, _("invalid effective address")); + Error(e->line, _("invalid effective address")); return 0; } if (i != indexreg && reg32mult[i] == 1 && basereg == REG32_NONE) @@ -599,7 +598,7 @@ x86_expr_checkea(expr **ep, unsigned char *addrsize, unsigned char bits, */ for (i=0; i<8; i++) if (i != basereg && i != indexreg && reg32mult[i] != 0) { - ErrorAt(e->line, _("invalid effective address")); + Error(e->line, _("invalid effective address")); return 0; } @@ -607,7 +606,7 @@ x86_expr_checkea(expr **ep, unsigned char *addrsize, unsigned char bits, if (indexreg != REG32_NONE && reg32mult[indexreg] != 1 && reg32mult[indexreg] != 2 && reg32mult[indexreg] != 4 && reg32mult[indexreg] != 8) { - ErrorAt(e->line, _("invalid effective address")); + Error(e->line, _("invalid effective address")); return 0; } @@ -617,7 +616,7 @@ x86_expr_checkea(expr **ep, unsigned char *addrsize, unsigned char bits, * legal. */ if (reg32mult[REG32_ESP] > 1 || basereg == REG32_ESP) { - ErrorAt(e->line, _("invalid effective address")); + Error(e->line, _("invalid effective address")); return 0; } /* If mult==1 and basereg is not ESP, swap indexreg w/basereg. */ @@ -721,7 +720,7 @@ x86_expr_checkea(expr **ep, unsigned char *addrsize, unsigned char bits, calc_bc_dist)) { case 0: e = *ep; - ErrorAt(e->line, _("invalid effective address")); + Error(e->line, _("invalid effective address")); return 0; case 1: return 1; @@ -733,7 +732,7 @@ x86_expr_checkea(expr **ep, unsigned char *addrsize, unsigned char bits, /* reg multipliers not 0 or 1 are illegal. */ if (reg16mult.bx & ~1 || reg16mult.si & ~1 || reg16mult.di & ~1 || reg16mult.bp & ~1) { - ErrorAt(e->line, _("invalid effective address")); + Error(e->line, _("invalid effective address")); return 0; } @@ -749,7 +748,7 @@ x86_expr_checkea(expr **ep, unsigned char *addrsize, unsigned char bits, /* Check the modrm value for invalid combinations. */ if (modrm16[havereg] & 0070) { - ErrorAt(e->line, _("invalid effective address")); + Error(e->line, _("invalid effective address")); return 0; } @@ -777,17 +776,17 @@ x86_floatnum_tobytes(const floatnum *flt, unsigned char **bufp, int fltret; if (!floatnum_check_size(flt, (size_t)valsize)) { - ErrorAt(e->line, _("invalid floating point constant size")); + Error(e->line, _("invalid floating point constant size")); return 1; } fltret = floatnum_get_sized(flt, *bufp, (size_t)valsize); if (fltret < 0) { - ErrorAt(e->line, _("underflow in floating point expression")); + Error(e->line, _("underflow in floating point expression")); return 1; } if (fltret > 0) { - ErrorAt(e->line, _("overflow in floating point expression")); + Error(e->line, _("overflow in floating point expression")); return 1; } *bufp += valsize; diff --git a/modules/arch/x86/x86id.re b/modules/arch/x86/x86id.re index cd729306..4cee4eed 100644 --- a/modules/arch/x86/x86id.re +++ b/modules/arch/x86/x86id.re @@ -24,7 +24,6 @@ RCSID("$IdPath$"); #include "bitvect.h" -#include "globals.h" #include "errwarn.h" #include "intnum.h" #include "floatnum.h" @@ -1308,7 +1307,8 @@ static const x86_insn_info xbts_insn[] = { static bytecode * x86_new_jmprel(const unsigned long data[4], int num_operands, insn_operandhead *operands, x86_insn_info *jrinfo, - section *cur_section, /*@null@*/ bytecode *prev_bc) + section *cur_section, /*@null@*/ bytecode *prev_bc, + unsigned long lindex) { x86_new_jmprel_data d; int num_info = (int)(data[1]&0xFF); @@ -1317,13 +1317,15 @@ x86_new_jmprel(const unsigned long data[4], int num_operands, insn_operand *op; static const unsigned char size_lookup[] = {0, 8, 16, 32, 64, 80, 128, 0}; + d.lindex = lindex; + /* We know the target is in operand 0, but sanity check for Imm. */ op = ops_first(operands); if (op->type != INSN_OPERAND_IMM) InternalError(_("invalid operand conversion")); d.target = expr_new(EXPR_SUB, ExprExpr(op->data.val), ExprSym(symrec_define_label("$", cur_section, prev_bc, - 0))); + 0, lindex)), lindex); /* See if the user explicitly specified short/near. */ switch (jrinfo->operands[0] & OPTM_MASK) { @@ -1398,7 +1400,7 @@ x86_new_jmprel(const unsigned long data[4], int num_operands, bytecode * x86_new_insn(const unsigned long data[4], int num_operands, insn_operandhead *operands, section *cur_section, - /*@null@*/ bytecode *prev_bc) + /*@null@*/ bytecode *prev_bc, unsigned long lindex) { x86_new_insn_data d; int num_info = (int)(data[1]&0xFF); @@ -1616,7 +1618,7 @@ x86_new_insn(const unsigned long data[4], int num_operands, if (!found) { /* Didn't find a matching one */ - Error(_("invalid combination of opcode and operands")); + Error(lindex, _("invalid combination of opcode and operands")); return NULL; } @@ -1628,10 +1630,10 @@ x86_new_insn(const unsigned long data[4], int num_operands, case MOD_ExtErr: switch ((info->modifiers & MOD_ExtIndex_MASK)>>MOD_ExtIndex_SHIFT) { case 0: - Error(_("mismatch in operand sizes")); + Error(lindex, _("mismatch in operand sizes")); break; case 1: - Error(_("operand size not specified")); + Error(lindex, _("operand size not specified")); break; default: InternalError(_("unrecognized x86 ext mod index")); @@ -1650,9 +1652,10 @@ x86_new_insn(const unsigned long data[4], int num_operands, /* Shortcut to JmpRel */ if (operands && (info->operands[0] & OPA_MASK) == OPA_JmpRel) return x86_new_jmprel(data, num_operands, operands, info, cur_section, - prev_bc); + prev_bc, lindex); /* Copy what we can from info */ + d.lindex = lindex; d.ea = NULL; d.imm = NULL; d.opersize = info->opersize; @@ -1692,7 +1695,8 @@ x86_new_insn(const unsigned long data[4], int num_operands, mod_data >>= 8; } if (info->modifiers & MOD_Imm8) { - d.imm = expr_new_ident(ExprInt(intnum_new_int(mod_data & 0xFF))); + d.imm = expr_new_ident(ExprInt(intnum_new_int(mod_data & 0xFF)), + lindex); d.im_len = 1; /*mod_data >>= 8;*/ } @@ -1840,7 +1844,7 @@ x86_new_insn(const unsigned long data[4], int num_operands, */ void -x86_switch_cpu(const char *id) +x86_switch_cpu(const char *id, unsigned long lindex) { /*const char *marker;*/ @@ -1949,18 +1953,19 @@ x86_switch_cpu(const char *id) /* catchalls */ [\001-\377]+ { - Warning(_("unrecognized CPU identifier `%s'"), id); + Warning(lindex, _("unrecognized CPU identifier `%s'"), id); return; } [\000] { - Warning(_("unrecognized CPU identifier `%s'"), id); + Warning(lindex, _("unrecognized CPU identifier `%s'"), id); return; } */ } arch_check_id_retval -x86_check_identifier(unsigned long data[4], const char *id) +x86_check_identifier(unsigned long data[4], const char *id, + /*@unused@*/ unsigned long lindex) { const char *oid = id; /*const char *marker;*/ diff --git a/modules/objfmts/bin/bin-objfmt.c b/modules/objfmts/bin/bin-objfmt.c index 2e5f0ed9..1ff0534c 100644 --- a/modules/objfmts/bin/bin-objfmt.c +++ b/modules/objfmts/bin/bin-objfmt.c @@ -24,7 +24,6 @@ #include "file.h" -#include "globals.h" #include "errwarn.h" #include "intnum.h" #include "floatnum.h" @@ -122,11 +121,11 @@ bin_objfmt_expr_xform(/*@returned@*/ /*@only@*/ expr *e, if (e->terms[i].type == EXPR_SYM && symrec_get_label(e->terms[i].data.sym, §, &precbc) && (dist = common_calc_bc_dist(sect, NULL, precbc))) { + const expr *start = section_get_start(sect); e->terms[i].type = EXPR_EXPR; e->terms[i].data.expn = - expr_new(EXPR_ADD, - ExprExpr(expr_copy(section_get_start(sect))), - ExprInt(dist)); + expr_new(EXPR_ADD, ExprExpr(expr_copy(start)), ExprInt(dist), + start->line); } } @@ -164,13 +163,13 @@ bin_objfmt_output_expr(expr **ep, unsigned char **bufp, unsigned long valsize, /* Check for complex float expressions */ if (expr_contains(*ep, EXPR_FLOAT)) { - ErrorAt((*ep)->line, _("floating point expression too complex")); + Error((*ep)->line, _("floating point expression too complex")); return 1; } /* Couldn't output, assume it contains an external reference. */ - ErrorAt((*ep)->line, - _("binary object format does not support external references")); + Error((*ep)->line, + _("binary object format does not support external references")); return 1; } @@ -199,8 +198,8 @@ bin_objfmt_output_bytecode(bytecode *bc, /*@null@*/ void *d) /* Warn that gaps are converted to 0 and write out the 0's. */ if (gap) { unsigned long left; - WarningAt(bc->line, - _("uninitialized space declared in code/data section: zeroing")); + Warning(bc->line, + _("uninitialized space declared in code/data section: zeroing")); /* Write out in chunks */ memset(info->buf, 0, REGULAR_OUTBUF_SIZE); left = multiple*size; @@ -268,7 +267,7 @@ bin_objfmt_output(FILE *f, sectionhead *sections) if (data) { start = bin_objfmt_align_section(data, prevsect, start, 4, prevsectlenptr, prevsectpadptr); - section_set_start(data, start); + section_set_start(data, start, 0); datastart = start; prevsect = data; prevsectlenptr = &datalen; @@ -277,7 +276,7 @@ bin_objfmt_output(FILE *f, sectionhead *sections) if (bss) { start = bin_objfmt_align_section(bss, prevsect, start, 4, prevsectlenptr, prevsectpadptr); - section_set_start(bss, start); + section_set_start(bss, start, 0); } /* Output .text first. */ @@ -315,7 +314,8 @@ bin_objfmt_cleanup(void) static /*@observer@*/ /*@null@*/ section * bin_objfmt_sections_switch(sectionhead *headp, valparamhead *valparams, /*@unused@*/ /*@null@*/ - valparamhead *objext_valparams) + valparamhead *objext_valparams, + unsigned long lindex) { valparam *vp; section *retval; @@ -340,7 +340,7 @@ bin_objfmt_sections_switch(sectionhead *headp, valparamhead *valparams, resonly = 1; } else { /* other section names not recognized. */ - Error(_("segment name `%s' not recognized"), sectname); + Error(lindex, _("segment name `%s' not recognized"), sectname); return NULL; } @@ -351,14 +351,15 @@ bin_objfmt_sections_switch(sectionhead *headp, valparamhead *valparams, unsigned long bitcnt; if (strcmp(sectname, ".text") == 0) { - Error(_("cannot specify an alignment to the `%s' section"), + Error(lindex, + _("cannot specify an alignment to the `%s' section"), sectname); return NULL; } align = expr_get_intnum(&vp->param, NULL); if (!align) { - Error(_("argument to `%s' is not a power of two"), + Error(lindex, _("argument to `%s' is not a power of two"), vp->val); return NULL; } @@ -369,7 +370,7 @@ bin_objfmt_sections_switch(sectionhead *headp, valparamhead *valparams, */ BitCount(bitcnt, alignval); if (bitcnt > 1) { - Error(_("argument to `%s' is not a power of two"), + Error(lindex, _("argument to `%s' is not a power of two"), vp->val); return NULL; } @@ -379,7 +380,7 @@ bin_objfmt_sections_switch(sectionhead *headp, valparamhead *valparams, } retval = sections_switch_general(headp, sectname, start, resonly, - &isnew); + &isnew, lindex); if (isnew) { if (have_alignval) { @@ -388,9 +389,10 @@ bin_objfmt_sections_switch(sectionhead *headp, valparamhead *valparams, section_set_of_data(retval, &yasm_bin_LTX_objfmt, data); } - symrec_define_label(sectname, retval, (bytecode *)NULL, 1); + symrec_define_label(sectname, retval, (bytecode *)NULL, 1, lindex); } else if (have_alignval) - Warning(_("alignment value ignored on section redeclaration")); + Warning(lindex, + _("alignment value ignored on section redeclaration")); return retval; } else @@ -406,16 +408,16 @@ bin_objfmt_section_data_delete(/*@only@*/ void *d) static void bin_objfmt_common_declare(/*@unused@*/ symrec *sym, /*@only@*/ expr *size, /*@unused@*/ /*@null@*/ - valparamhead *objext_valparams) + valparamhead *objext_valparams, unsigned long lindex) { expr_delete(size); - Error(_("binary object format does not support common variables")); + Error(lindex, _("binary object format does not support common variables")); } static int bin_objfmt_directive(const char *name, valparamhead *valparams, /*@unused@*/ /*@null@*/ valparamhead *objext_valparams, - sectionhead *headp) + sectionhead *headp, unsigned long lindex) { section *sect; valparam *vp; @@ -426,13 +428,13 @@ bin_objfmt_directive(const char *name, valparamhead *valparams, /* ORG takes just a simple integer as param */ vp = vps_first(valparams); if (vp->val) { - Error(_("argument to ORG should be numeric")); + Error(lindex, _("argument to ORG should be numeric")); return 0; } else if (vp->param) start = expr_get_intnum(&vp->param, NULL); if (!start) { - Error(_("argument to ORG should be numeric")); + Error(lindex, _("argument to ORG should be numeric")); return 0; } @@ -440,7 +442,7 @@ bin_objfmt_directive(const char *name, valparamhead *valparams, sect = sections_find_general(headp, ".text"); if (!sect) InternalError(_("bin objfmt: .text section does not exist before ORG is called?")); - section_set_start(sect, intnum_get_uint(start)); + section_set_start(sect, intnum_get_uint(start), lindex); return 0; /* directive recognized */ } else diff --git a/modules/objfmts/coff/coff-objfmt.c b/modules/objfmts/coff/coff-objfmt.c index 135d46fb..db6d3075 100644 --- a/modules/objfmts/coff/coff-objfmt.c +++ b/modules/objfmts/coff/coff-objfmt.c @@ -24,7 +24,6 @@ #include "file.h" -#include "globals.h" #include "errwarn.h" #include "intnum.h" #include "floatnum.h" @@ -210,7 +209,7 @@ coff_objfmt_initialize(const char *in_filename, data->index = 0; data->sclass = COFF_SCL_FILE; data->size = NULL; - filesym = symrec_define_label(".file", NULL, NULL, 0); + filesym = symrec_define_label(".file", NULL, NULL, 0, 0); symrec_set_of_data(filesym, &yasm_coff_LTX_objfmt, data); entry = xmalloc(sizeof(coff_symtab_entry)); @@ -274,7 +273,7 @@ coff_objfmt_output_expr(expr **ep, unsigned char **bufp, unsigned long valsize, SymVisibility vis; if (valsize != 4) { - ErrorAt((*ep)->line, _("coff: invalid relocation size")); + Error((*ep)->line, _("coff: invalid relocation size")); return 1; } @@ -291,7 +290,8 @@ coff_objfmt_output_expr(expr **ep, unsigned char **bufp, unsigned long valsize, csymd = symrec_get_of_data(sym); assert(csymd != NULL); *ep = expr_new(EXPR_ADD, ExprExpr(*ep), - ExprExpr(expr_copy(csymd->size))); + ExprExpr(expr_copy(csymd->size)), + csymd->size->line); *ep = expr_simplify(*ep, common_calc_bc_dist); } else if (!(vis & SYM_EXTERN)) { /* Local symbols need relocation to their section's start */ @@ -302,7 +302,8 @@ coff_objfmt_output_expr(expr **ep, unsigned char **bufp, unsigned long valsize, reloc->sym = label_csd->sym; if (COFF_SET_VMA) *ep = expr_new(EXPR_ADD, ExprExpr(*ep), - ExprInt(intnum_new_uint(label_csd->addr))); + ExprInt(intnum_new_uint(label_csd->addr)), + (*ep)->line); } } @@ -311,7 +312,8 @@ coff_objfmt_output_expr(expr **ep, unsigned char **bufp, unsigned long valsize, /* Need to reference to start of section, so add $$ in. */ *ep = expr_new(EXPR_ADD, ExprExpr(*ep), ExprSym(symrec_define_label("$$", info->sect, NULL, - 0))); + 0, (*ep)->line)), + (*ep)->line); *ep = expr_simplify(*ep, common_calc_bc_dist); } else reloc->type = COFF_RELOC_ADDR32; @@ -324,11 +326,11 @@ coff_objfmt_output_expr(expr **ep, unsigned char **bufp, unsigned long valsize, /* Check for complex float expressions */ if (expr_contains(*ep, EXPR_FLOAT)) { - ErrorAt((*ep)->line, _("floating point expression too complex")); + Error((*ep)->line, _("floating point expression too complex")); return 1; } - ErrorAt((*ep)->line, _("coff: relocation too complex")); + Error((*ep)->line, _("coff: relocation too complex")); return 1; } @@ -359,8 +361,8 @@ coff_objfmt_output_bytecode(bytecode *bc, /*@null@*/ void *d) /* Warn that gaps are converted to 0 and write out the 0's. */ if (gap) { unsigned long left; - WarningAt(bc->line, - _("uninitialized space declared in code/data section: zeroing")); + Warning(bc->line, + _("uninitialized space declared in code/data section: zeroing")); /* Write out in chunks */ memset(info->buf, 0, REGULAR_OUTBUF_SIZE); left = multiple*size; @@ -578,8 +580,8 @@ coff_objfmt_output(FILE *f, sectionhead *sections) const intnum *intn; intn = expr_get_intnum(&csymd->size, common_calc_bc_dist); if (!intn) - ErrorAt(csymd->size->line, - _("COMMON data size not an integer expression")); + Error(csymd->size->line, + _("COMMON data size not an integer expression")); else value = intnum_get_uint(intn); scnum = 0; @@ -695,7 +697,8 @@ coff_objfmt_cleanup(void) static /*@observer@*/ /*@null@*/ section * coff_objfmt_sections_switch(sectionhead *headp, valparamhead *valparams, /*@unused@*/ /*@null@*/ - valparamhead *objext_valparams) + valparamhead *objext_valparams, + unsigned long lindex) { valparam *vp = vps_first(valparams); section *retval; @@ -710,7 +713,8 @@ coff_objfmt_sections_switch(sectionhead *headp, valparamhead *valparams, sectname = vp->val; if (strlen(sectname) > 8) { - Warning(_("COFF section names limited to 8 characters: truncating")); + Warning(lindex, + _("COFF section names limited to 8 characters: truncating")); sectname[8] = '\0'; } @@ -737,7 +741,8 @@ coff_objfmt_sections_switch(sectionhead *headp, valparamhead *valparams, } } - retval = sections_switch_general(headp, sectname, 0, resonly, &isnew); + retval = sections_switch_general(headp, sectname, 0, resonly, &isnew, + lindex); if (isnew) { coff_section_data *data; @@ -754,12 +759,13 @@ coff_objfmt_sections_switch(sectionhead *headp, valparamhead *valparams, STAILQ_INIT(&data->relocs); section_set_of_data(retval, &yasm_coff_LTX_objfmt, data); - sym = symrec_define_label(sectname, retval, (bytecode *)NULL, 1); + sym = symrec_define_label(sectname, retval, (bytecode *)NULL, 1, + lindex); coff_objfmt_symtab_append(sym, COFF_SCL_STAT, NULL, 1, COFF_SYMTAB_AUX_SECT); data->sym = sym; } else if (flags_override) - Warning(_("section flags ignored on section redeclaration")); + Warning(lindex, _("section flags ignored on section redeclaration")); return retval; } @@ -826,7 +832,8 @@ coff_objfmt_section_data_print(FILE *f, int indent_level, void *data) static void coff_objfmt_extglob_declare(symrec *sym, /*@unused@*/ - /*@null@*/ valparamhead *objext_valparams) + /*@null@*/ valparamhead *objext_valparams, + /*@unused@*/ unsigned long lindex) { coff_objfmt_symtab_append(sym, COFF_SCL_EXT, NULL, 0, COFF_SYMTAB_AUX_NONE); @@ -834,7 +841,8 @@ coff_objfmt_extglob_declare(symrec *sym, /*@unused@*/ static void coff_objfmt_common_declare(symrec *sym, /*@only@*/ expr *size, /*@unused@*/ - /*@null@*/ valparamhead *objext_valparams) + /*@null@*/ valparamhead *objext_valparams, + /*@unused@*/ unsigned long lindex) { coff_objfmt_symtab_append(sym, COFF_SCL_EXT, size, 0, COFF_SYMTAB_AUX_NONE); @@ -868,7 +876,8 @@ static int coff_objfmt_directive(/*@unused@*/ const char *name, /*@unused@*/ valparamhead *valparams, /*@unused@*/ /*@null@*/ valparamhead *objext_valparams, - /*@unused@*/ sectionhead *headp) + /*@unused@*/ sectionhead *headp, + /*@unused@*/ unsigned long lindex) { return 1; /* no objfmt directives */ } diff --git a/modules/objfmts/dbg/dbg-objfmt.c b/modules/objfmts/dbg/dbg-objfmt.c index a50d5f49..3b10a442 100644 --- a/modules/objfmts/dbg/dbg-objfmt.c +++ b/modules/objfmts/dbg/dbg-objfmt.c @@ -22,7 +22,6 @@ #include "util.h" /*@unused@*/ RCSID("$IdPath$"); -#include "globals.h" #include "errwarn.h" #include "expr.h" #include "symrec.h" @@ -81,7 +80,8 @@ dbg_objfmt_cleanup(void) static /*@observer@*/ /*@null@*/ section * dbg_objfmt_sections_switch(sectionhead *headp, valparamhead *valparams, /*@unused@*/ /*@null@*/ - valparamhead *objext_valparams) + valparamhead *objext_valparams, + unsigned long lindex) { valparam *vp; section *retval; @@ -91,13 +91,14 @@ dbg_objfmt_sections_switch(sectionhead *headp, valparamhead *valparams, vps_print(dbg_objfmt_file, valparams); fprintf(dbg_objfmt_file, ", "); vps_print(dbg_objfmt_file, objext_valparams); - fprintf(dbg_objfmt_file, "), returning "); + fprintf(dbg_objfmt_file, ", %lu), returning ", lindex); if ((vp = vps_first(valparams)) && !vp->param && vp->val != NULL) { - retval = sections_switch_general(headp, vp->val, 200, 0, &isnew); + retval = sections_switch_general(headp, vp->val, 200, 0, &isnew, + lindex); if (isnew) { fprintf(dbg_objfmt_file, "(new) "); - symrec_define_label(vp->val, retval, (bytecode *)NULL, 1); + symrec_define_label(vp->val, retval, (bytecode *)NULL, 1, lindex); } fprintf(dbg_objfmt_file, "\"%s\" section\n", vp->val); return retval; @@ -125,34 +126,35 @@ dbg_objfmt_section_data_print(FILE *f, int indent_level, /*@null@*/ void *data) static void dbg_objfmt_extern_declare(symrec *sym, /*@unused@*/ /*@null@*/ - valparamhead *objext_valparams) + valparamhead *objext_valparams, unsigned long lindex) { fprintf(dbg_objfmt_file, "extern_declare(\"%s\", ", symrec_get_name(sym)); vps_print(dbg_objfmt_file, objext_valparams); - fprintf(dbg_objfmt_file, "), setting of_data=NULL\n"); + fprintf(dbg_objfmt_file, ", %lu), setting of_data=NULL\n", lindex); symrec_set_of_data(sym, &yasm_dbg_LTX_objfmt, NULL); } static void dbg_objfmt_global_declare(symrec *sym, /*@unused@*/ /*@null@*/ - valparamhead *objext_valparams) + valparamhead *objext_valparams, unsigned long lindex) { fprintf(dbg_objfmt_file, "global_declare(\"%s\", ", symrec_get_name(sym)); vps_print(dbg_objfmt_file, objext_valparams); - fprintf(dbg_objfmt_file, "), setting of_data=NULL\n"); + fprintf(dbg_objfmt_file, ", %lu), setting of_data=NULL\n", lindex); symrec_set_of_data(sym, &yasm_dbg_LTX_objfmt, NULL); } static void dbg_objfmt_common_declare(symrec *sym, /*@only@*/ expr *size, /*@unused@*/ - /*@null@*/ valparamhead *objext_valparams) + /*@null@*/ valparamhead *objext_valparams, + unsigned long lindex) { assert(dbg_objfmt_file != NULL); fprintf(dbg_objfmt_file, "common_declare(\"%s\", ", symrec_get_name(sym)); expr_print(dbg_objfmt_file, size); fprintf(dbg_objfmt_file, ", "); vps_print(dbg_objfmt_file, objext_valparams); - fprintf(dbg_objfmt_file, "), setting of_data="); + fprintf(dbg_objfmt_file, ", %lu), setting of_data=", lindex); expr_print(dbg_objfmt_file, size); symrec_set_of_data(sym, &yasm_dbg_LTX_objfmt, size); fprintf(dbg_objfmt_file, "\n"); @@ -184,13 +186,13 @@ dbg_objfmt_symrec_data_print(FILE *f, int indent_level, /*@null@*/ void *data) static int dbg_objfmt_directive(const char *name, valparamhead *valparams, /*@null@*/ valparamhead *objext_valparams, - /*@unused@*/ sectionhead *headp) + /*@unused@*/ sectionhead *headp, unsigned long lindex) { fprintf(dbg_objfmt_file, "directive(\"%s\", ", name); vps_print(dbg_objfmt_file, valparams); fprintf(dbg_objfmt_file, ", "); vps_print(dbg_objfmt_file, objext_valparams); - fprintf(dbg_objfmt_file, "), returning 0 (recognized)\n"); + fprintf(dbg_objfmt_file, ", %lu), returning 0 (recognized)\n", lindex); return 0; /* dbg format "recognizes" all directives */ } diff --git a/modules/optimizers/basic/basic-optimizer.c b/modules/optimizers/basic/basic-optimizer.c index cc6a903c..5c654b7d 100644 --- a/modules/optimizers/basic/basic-optimizer.c +++ b/modules/optimizers/basic/basic-optimizer.c @@ -129,7 +129,7 @@ basic_optimize_bytecode_1(/*@observer@*/ bytecode *bc, void *d) bcr_retval = bc_resolve(bc, 0, data->sect, basic_optimize_calc_bc_dist_1); if (bcr_retval & BC_RESOLVE_UNKNOWN_LEN) { if (!(bcr_retval & BC_RESOLVE_ERROR)) - ErrorAt(bc->line, _("circular reference detected.")); + Error(bc->line, _("circular reference detected.")); data->saw_unknown = -1; return 0; } diff --git a/modules/parsers/nasm/nasm-bison.y b/modules/parsers/nasm/nasm-bison.y index e7bcd827..996ef574 100644 --- a/modules/parsers/nasm/nasm-bison.y +++ b/modules/parsers/nasm/nasm-bison.y @@ -29,7 +29,7 @@ RCSID("$IdPath$"); #include "bitvect.h" -#include "globals.h" +#include "linemgr.h" #include "errwarn.h" #include "intnum.h" #include "floatnum.h" @@ -60,6 +60,13 @@ extern char *nasm_parser_locallabel_base; extern size_t nasm_parser_locallabel_base_len; extern /*@dependent@*/ arch *nasm_parser_arch; extern /*@dependent@*/ objfmt *nasm_parser_objfmt; +extern /*@dependent@*/ linemgr *nasm_parser_linemgr; + +#define p_line_index (nasm_parser_linemgr->get_current()) + +#define p_expr_new_tree(l,o,r) expr_new_tree(l,o,r,p_line_index) +#define p_expr_new_branch(o,r) expr_new_branch(o,r,p_line_index) +#define p_expr_new_ident(r) expr_new_ident(r,p_line_index) static /*@null@*/ bytecode *nasm_parser_prev_bc = (bytecode *)NULL; static bytecode *nasm_parser_temp_bc; @@ -134,7 +141,7 @@ input: /* empty */ $2); if (nasm_parser_temp_bc) nasm_parser_prev_bc = nasm_parser_temp_bc; - line_index++; + nasm_parser_linemgr->goto_next(); } ; @@ -144,8 +151,8 @@ line: '\n' { $$ = (bytecode *)NULL; } /* %line indicates the line number of the *next* line, so subtract out * the increment when setting the line number. */ - line_set($5, intnum_get_uint($2)-intnum_get_uint($4), - intnum_get_uint($4)); + nasm_parser_linemgr->set($5, intnum_get_uint($2)-intnum_get_uint($4), + intnum_get_uint($4)); intnum_delete($2); intnum_delete($4); xfree($5); @@ -155,7 +162,8 @@ line: '\n' { $$ = (bytecode *)NULL; } $$ = (bytecode *)NULL; } | error '\n' { - Error(_("label or instruction expected at start of line")); + Error(p_line_index, + _("label or instruction expected at start of line")); $$ = (bytecode *)NULL; yyerrok; } @@ -167,43 +175,55 @@ lineexp: exp | label exp { $$ = $2; } | label TIMES expr exp { $$ = $4; bc_set_multiple($$, $3); } | label_id_equ EQU expr { - symrec_define_equ($1, $3); + symrec_define_equ($1, $3, p_line_index); xfree($1); $$ = (bytecode *)NULL; } ; exp: instr - | DECLARE_DATA datavals { $$ = bc_new_data(&$2, $1); } - | RESERVE_SPACE expr { $$ = bc_new_reserve($2, $1); } - | INCBIN STRING { $$ = bc_new_incbin($2, NULL, NULL); } - | INCBIN STRING ',' expr { $$ = bc_new_incbin($2, $4, NULL); } - | INCBIN STRING ',' expr ',' expr { $$ = bc_new_incbin($2, $4, $6); } + | DECLARE_DATA datavals { + $$ = bc_new_data(&$2, $1, p_line_index); + } + | RESERVE_SPACE expr { + $$ = bc_new_reserve($2, $1, p_line_index); + } + | INCBIN STRING { + $$ = bc_new_incbin($2, NULL, NULL, p_line_index); + } + | INCBIN STRING ',' expr { + $$ = bc_new_incbin($2, $4, NULL, p_line_index); + } + | INCBIN STRING ',' expr ',' expr { + $$ = bc_new_incbin($2, $4, $6, p_line_index); + } ; instr: INSN { $$ = nasm_parser_arch->parse.new_insn($1, 0, NULL, nasm_parser_cur_section, - nasm_parser_prev_bc); + nasm_parser_prev_bc, + p_line_index); } | INSN operands { $$ = nasm_parser_arch->parse.new_insn($1, $2.num_operands, &$2.operands, nasm_parser_cur_section, - nasm_parser_prev_bc); + nasm_parser_prev_bc, + p_line_index); ops_delete(&$2.operands, 0); } | INSN error { - Error(_("expression syntax error")); + Error(p_line_index, _("expression syntax error")); $$ = NULL; } | PREFIX instr { $$ = $2; - nasm_parser_arch->parse.handle_prefix($$, $1); + nasm_parser_arch->parse.handle_prefix($$, $1, p_line_index); } | SEGREG instr { $$ = $2; - nasm_parser_arch->parse.handle_seg_prefix($$, $1[0]); + nasm_parser_arch->parse.handle_seg_prefix($$, $1[0], p_line_index); } ; @@ -214,19 +234,19 @@ datavals: dataval { dvs_initialize(&$$); dvs_append(&$$, $1); } dataval: dvexpr { $$ = dv_new_expr($1); } | STRING { $$ = dv_new_string($1); } | error { - Error(_("expression syntax error")); + Error(p_line_index, _("expression syntax error")); $$ = (dataval *)NULL; } ; label: label_id { symrec_define_label($1, nasm_parser_cur_section, nasm_parser_prev_bc, - 1); + 1, p_line_index); xfree($1); } | label_id ':' { symrec_define_label($1, nasm_parser_cur_section, nasm_parser_prev_bc, - 1); + 1, p_line_index); xfree($1); } ; @@ -254,7 +274,7 @@ directive: DIRECTIVE_NAME directive_val { xfree($1); } | DIRECTIVE_NAME error { - Error(_("invalid arguments to [%s]"), $1); + Error(p_line_index, _("invalid arguments to [%s]"), $1); xfree($1); } ; @@ -298,7 +318,7 @@ memaddr: expr { } | SEGREG ':' memaddr { $$ = $3; - nasm_parser_arch->parse.handle_seg_override($$, $1[0]); + nasm_parser_arch->parse.handle_seg_override($$, $1[0], p_line_index); } | BYTE memaddr { $$ = $2; ea_set_len($$, 1); } | WORD memaddr { $$ = $2; ea_set_len($$, 2); } @@ -326,7 +346,7 @@ operand: '[' memaddr ']' { $$ = operand_new_mem($2); } $$ = $2; if ($$->type == INSN_OPERAND_REG && nasm_parser_arch->get_reg_size($$->data.reg) != 1) - Error(_("cannot override register size")); + Error(p_line_index, _("cannot override register size")); else $$->size = 1; } @@ -334,7 +354,7 @@ operand: '[' memaddr ']' { $$ = operand_new_mem($2); } $$ = $2; if ($$->type == INSN_OPERAND_REG && nasm_parser_arch->get_reg_size($$->data.reg) != 2) - Error(_("cannot override register size")); + Error(p_line_index, _("cannot override register size")); else $$->size = 2; } @@ -342,7 +362,7 @@ operand: '[' memaddr ']' { $$ = operand_new_mem($2); } $$ = $2; if ($$->type == INSN_OPERAND_REG && nasm_parser_arch->get_reg_size($$->data.reg) != 4) - Error(_("cannot override register size")); + Error(p_line_index, _("cannot override register size")); else $$->size = 4; } @@ -350,7 +370,7 @@ operand: '[' memaddr ']' { $$ = operand_new_mem($2); } $$ = $2; if ($$->type == INSN_OPERAND_REG && nasm_parser_arch->get_reg_size($$->data.reg) != 8) - Error(_("cannot override register size")); + Error(p_line_index, _("cannot override register size")); else $$->size = 8; } @@ -358,7 +378,7 @@ operand: '[' memaddr ']' { $$ = operand_new_mem($2); } $$ = $2; if ($$->type == INSN_OPERAND_REG && nasm_parser_arch->get_reg_size($$->data.reg) != 10) - Error(_("cannot override register size")); + Error(p_line_index, _("cannot override register size")); else $$->size = 10; } @@ -366,7 +386,7 @@ operand: '[' memaddr ']' { $$ = operand_new_mem($2); } $$ = $2; if ($$->type == INSN_OPERAND_REG && nasm_parser_arch->get_reg_size($$->data.reg) != 16) - Error(_("cannot override register size")); + Error(p_line_index, _("cannot override register size")); else $$->size = 16; } @@ -376,57 +396,58 @@ operand: '[' memaddr ']' { $$ = operand_new_mem($2); } /* expression trees */ /* expr w/o FLTNUM and unary + and -, for use in directives */ -direxpr: INTNUM { $$ = expr_new_ident(ExprInt($1)); } +direxpr: INTNUM { $$ = p_expr_new_ident(ExprInt($1)); } | ID { - $$ = expr_new_ident(ExprSym(symrec_define_label($1, NULL, NULL, 0))); + $$ = p_expr_new_ident(ExprSym(symrec_define_label($1, NULL, NULL, 0, + p_line_index))); xfree($1); } - | direxpr '|' direxpr { $$ = expr_new_tree($1, EXPR_OR, $3); } - | direxpr '^' direxpr { $$ = expr_new_tree($1, EXPR_XOR, $3); } - | direxpr '&' direxpr { $$ = expr_new_tree($1, EXPR_AND, $3); } - | direxpr LEFT_OP direxpr { $$ = expr_new_tree($1, EXPR_SHL, $3); } - | direxpr RIGHT_OP direxpr { $$ = expr_new_tree($1, EXPR_SHR, $3); } - | direxpr '+' direxpr { $$ = expr_new_tree($1, EXPR_ADD, $3); } - | direxpr '-' direxpr { $$ = expr_new_tree($1, EXPR_SUB, $3); } - | direxpr '*' direxpr { $$ = expr_new_tree($1, EXPR_MUL, $3); } - | direxpr '/' direxpr { $$ = expr_new_tree($1, EXPR_DIV, $3); } - | direxpr SIGNDIV direxpr { $$ = expr_new_tree($1, EXPR_SIGNDIV, $3); } - | direxpr '%' direxpr { $$ = expr_new_tree($1, EXPR_MOD, $3); } - | direxpr SIGNMOD direxpr { $$ = expr_new_tree($1, EXPR_SIGNMOD, $3); } - /*| '!' expr { $$ = expr_new_branch(EXPR_LNOT, $2); }*/ - | '~' direxpr %prec UNARYOP { $$ = expr_new_branch(EXPR_NOT, $2); } + | direxpr '|' direxpr { $$ = p_expr_new_tree($1, EXPR_OR, $3); } + | direxpr '^' direxpr { $$ = p_expr_new_tree($1, EXPR_XOR, $3); } + | direxpr '&' direxpr { $$ = p_expr_new_tree($1, EXPR_AND, $3); } + | direxpr LEFT_OP direxpr { $$ = p_expr_new_tree($1, EXPR_SHL, $3); } + | direxpr RIGHT_OP direxpr { $$ = p_expr_new_tree($1, EXPR_SHR, $3); } + | direxpr '+' direxpr { $$ = p_expr_new_tree($1, EXPR_ADD, $3); } + | direxpr '-' direxpr { $$ = p_expr_new_tree($1, EXPR_SUB, $3); } + | direxpr '*' direxpr { $$ = p_expr_new_tree($1, EXPR_MUL, $3); } + | direxpr '/' direxpr { $$ = p_expr_new_tree($1, EXPR_DIV, $3); } + | direxpr SIGNDIV direxpr { $$ = p_expr_new_tree($1, EXPR_SIGNDIV, $3); } + | direxpr '%' direxpr { $$ = p_expr_new_tree($1, EXPR_MOD, $3); } + | direxpr SIGNMOD direxpr { $$ = p_expr_new_tree($1, EXPR_SIGNMOD, $3); } + /*| '!' expr { $$ = p_expr_new_branch(EXPR_LNOT, $2); }*/ + | '~' direxpr %prec UNARYOP { $$ = p_expr_new_branch(EXPR_NOT, $2); } | '(' direxpr ')' { $$ = $2; } ; -dvexpr: INTNUM { $$ = expr_new_ident(ExprInt($1)); } - | FLTNUM { $$ = expr_new_ident(ExprFloat($1)); } - | explabel { $$ = expr_new_ident(ExprSym($1)); } - /*| dvexpr '||' dvexpr { $$ = expr_new_tree($1, EXPR_LOR, $3); }*/ - | dvexpr '|' dvexpr { $$ = expr_new_tree($1, EXPR_OR, $3); } - | dvexpr '^' dvexpr { $$ = expr_new_tree($1, EXPR_XOR, $3); } - /*| dvexpr '&&' dvexpr { $$ = expr_new_tree($1, EXPR_LAND, $3); }*/ - | dvexpr '&' dvexpr { $$ = expr_new_tree($1, EXPR_AND, $3); } - /*| dvexpr '==' dvexpr { $$ = expr_new_tree($1, EXPR_EQUALS, $3); }*/ - /*| dvexpr '>' dvexpr { $$ = expr_new_tree($1, EXPR_GT, $3); }*/ - /*| dvexpr '<' dvexpr { $$ = expr_new_tree($1, EXPR_GT, $3); }*/ - /*| dvexpr '>=' dvexpr { $$ = expr_new_tree($1, EXPR_GE, $3); }*/ - /*| dvexpr '<=' dvexpr { $$ = expr_new_tree($1, EXPR_GE, $3); }*/ - /*| dvexpr '!=' dvexpr { $$ = expr_new_tree($1, EXPR_NE, $3); }*/ - | dvexpr LEFT_OP dvexpr { $$ = expr_new_tree($1, EXPR_SHL, $3); } - | dvexpr RIGHT_OP dvexpr { $$ = expr_new_tree($1, EXPR_SHR, $3); } - | dvexpr '+' dvexpr { $$ = expr_new_tree($1, EXPR_ADD, $3); } - | dvexpr '-' dvexpr { $$ = expr_new_tree($1, EXPR_SUB, $3); } - | dvexpr '*' dvexpr { $$ = expr_new_tree($1, EXPR_MUL, $3); } - | dvexpr '/' dvexpr { $$ = expr_new_tree($1, EXPR_DIV, $3); } - | dvexpr SIGNDIV dvexpr { $$ = expr_new_tree($1, EXPR_SIGNDIV, $3); } - | dvexpr '%' dvexpr { $$ = expr_new_tree($1, EXPR_MOD, $3); } - | dvexpr SIGNMOD dvexpr { $$ = expr_new_tree($1, EXPR_SIGNMOD, $3); } +dvexpr: INTNUM { $$ = p_expr_new_ident(ExprInt($1)); } + | FLTNUM { $$ = p_expr_new_ident(ExprFloat($1)); } + | explabel { $$ = p_expr_new_ident(ExprSym($1)); } + /*| dvexpr '||' dvexpr { $$ = p_expr_new_tree($1, EXPR_LOR, $3); }*/ + | dvexpr '|' dvexpr { $$ = p_expr_new_tree($1, EXPR_OR, $3); } + | dvexpr '^' dvexpr { $$ = p_expr_new_tree($1, EXPR_XOR, $3); } + /*| dvexpr '&&' dvexpr { $$ = p_expr_new_tree($1, EXPR_LAND, $3); }*/ + | dvexpr '&' dvexpr { $$ = p_expr_new_tree($1, EXPR_AND, $3); } + /*| dvexpr '==' dvexpr { $$ = p_expr_new_tree($1, EXPR_EQUALS, $3); }*/ + /*| dvexpr '>' dvexpr { $$ = p_expr_new_tree($1, EXPR_GT, $3); }*/ + /*| dvexpr '<' dvexpr { $$ = p_expr_new_tree($1, EXPR_GT, $3); }*/ + /*| dvexpr '>=' dvexpr { $$ = p_expr_new_tree($1, EXPR_GE, $3); }*/ + /*| dvexpr '<=' dvexpr { $$ = p_expr_new_tree($1, EXPR_GE, $3); }*/ + /*| dvexpr '!=' dvexpr { $$ = p_expr_new_tree($1, EXPR_NE, $3); }*/ + | dvexpr LEFT_OP dvexpr { $$ = p_expr_new_tree($1, EXPR_SHL, $3); } + | dvexpr RIGHT_OP dvexpr { $$ = p_expr_new_tree($1, EXPR_SHR, $3); } + | dvexpr '+' dvexpr { $$ = p_expr_new_tree($1, EXPR_ADD, $3); } + | dvexpr '-' dvexpr { $$ = p_expr_new_tree($1, EXPR_SUB, $3); } + | dvexpr '*' dvexpr { $$ = p_expr_new_tree($1, EXPR_MUL, $3); } + | dvexpr '/' dvexpr { $$ = p_expr_new_tree($1, EXPR_DIV, $3); } + | dvexpr SIGNDIV dvexpr { $$ = p_expr_new_tree($1, EXPR_SIGNDIV, $3); } + | dvexpr '%' dvexpr { $$ = p_expr_new_tree($1, EXPR_MOD, $3); } + | dvexpr SIGNMOD dvexpr { $$ = p_expr_new_tree($1, EXPR_SIGNMOD, $3); } | '+' dvexpr %prec UNARYOP { $$ = $2; } - | '-' dvexpr %prec UNARYOP { $$ = expr_new_branch(EXPR_NEG, $2); } - /*| '!' dvexpr { $$ = expr_new_branch(EXPR_LNOT, $2); }*/ - | '~' dvexpr %prec UNARYOP { $$ = expr_new_branch(EXPR_NOT, $2); } - | SEG dvexpr { $$ = expr_new_branch(EXPR_SEG, $2); } - | WRT dvexpr { $$ = expr_new_branch(EXPR_WRT, $2); } + | '-' dvexpr %prec UNARYOP { $$ = p_expr_new_branch(EXPR_NEG, $2); } + /*| '!' dvexpr { $$ = p_expr_new_branch(EXPR_LNOT, $2); }*/ + | '~' dvexpr %prec UNARYOP { $$ = p_expr_new_branch(EXPR_NOT, $2); } + | SEG dvexpr { $$ = p_expr_new_branch(EXPR_SEG, $2); } + | WRT dvexpr { $$ = p_expr_new_branch(EXPR_WRT, $2); } | '(' dvexpr ')' { $$ = $2; } ; @@ -434,64 +455,66 @@ dvexpr: INTNUM { $$ = expr_new_ident(ExprInt($1)); } * We don't attempt to check memory expressions for validity here. * Essentially the same as expr_no_string above but adds REG and STRING. */ -expr: INTNUM { $$ = expr_new_ident(ExprInt($1)); } - | FLTNUM { $$ = expr_new_ident(ExprFloat($1)); } - | REG { $$ = expr_new_ident(ExprReg($1[0])); } +expr: INTNUM { $$ = p_expr_new_ident(ExprInt($1)); } + | FLTNUM { $$ = p_expr_new_ident(ExprFloat($1)); } + | REG { $$ = p_expr_new_ident(ExprReg($1[0])); } | STRING { - $$ = expr_new_ident(ExprInt(intnum_new_charconst_nasm($1))); + $$ = p_expr_new_ident(ExprInt(intnum_new_charconst_nasm($1, + p_line_index))); xfree($1); } - | explabel { $$ = expr_new_ident(ExprSym($1)); } - /*| expr '||' expr { $$ = expr_new_tree($1, EXPR_LOR, $3); }*/ - | expr '|' expr { $$ = expr_new_tree($1, EXPR_OR, $3); } - | expr '^' expr { $$ = expr_new_tree($1, EXPR_XOR, $3); } - /*| expr '&&' expr { $$ = expr_new_tree($1, EXPR_LAND, $3); }*/ - | expr '&' expr { $$ = expr_new_tree($1, EXPR_AND, $3); } - /*| expr '==' expr { $$ = expr_new_tree($1, EXPR_EQUALS, $3); }*/ - /*| expr '>' expr { $$ = expr_new_tree($1, EXPR_GT, $3); }*/ - /*| expr '<' expr { $$ = expr_new_tree($1, EXPR_GT, $3); }*/ - /*| expr '>=' expr { $$ = expr_new_tree($1, EXPR_GE, $3); }*/ - /*| expr '<=' expr { $$ = expr_new_tree($1, EXPR_GE, $3); }*/ - /*| expr '!=' expr { $$ = expr_new_tree($1, EXPR_NE, $3); }*/ - | expr LEFT_OP expr { $$ = expr_new_tree($1, EXPR_SHL, $3); } - | expr RIGHT_OP expr { $$ = expr_new_tree($1, EXPR_SHR, $3); } - | expr '+' expr { $$ = expr_new_tree($1, EXPR_ADD, $3); } - | expr '-' expr { $$ = expr_new_tree($1, EXPR_SUB, $3); } - | expr '*' expr { $$ = expr_new_tree($1, EXPR_MUL, $3); } - | expr '/' expr { $$ = expr_new_tree($1, EXPR_DIV, $3); } - | expr SIGNDIV expr { $$ = expr_new_tree($1, EXPR_SIGNDIV, $3); } - | expr '%' expr { $$ = expr_new_tree($1, EXPR_MOD, $3); } - | expr SIGNMOD expr { $$ = expr_new_tree($1, EXPR_SIGNMOD, $3); } + | explabel { $$ = p_expr_new_ident(ExprSym($1)); } + /*| expr '||' expr { $$ = p_expr_new_tree($1, EXPR_LOR, $3); }*/ + | expr '|' expr { $$ = p_expr_new_tree($1, EXPR_OR, $3); } + | expr '^' expr { $$ = p_expr_new_tree($1, EXPR_XOR, $3); } + /*| expr '&&' expr { $$ = p_expr_new_tree($1, EXPR_LAND, $3); }*/ + | expr '&' expr { $$ = p_expr_new_tree($1, EXPR_AND, $3); } + /*| expr '==' expr { $$ = p_expr_new_tree($1, EXPR_EQUALS, $3); }*/ + /*| expr '>' expr { $$ = p_expr_new_tree($1, EXPR_GT, $3); }*/ + /*| expr '<' expr { $$ = p_expr_new_tree($1, EXPR_GT, $3); }*/ + /*| expr '>=' expr { $$ = p_expr_new_tree($1, EXPR_GE, $3); }*/ + /*| expr '<=' expr { $$ = p_expr_new_tree($1, EXPR_GE, $3); }*/ + /*| expr '!=' expr { $$ = p_expr_new_tree($1, EXPR_NE, $3); }*/ + | expr LEFT_OP expr { $$ = p_expr_new_tree($1, EXPR_SHL, $3); } + | expr RIGHT_OP expr { $$ = p_expr_new_tree($1, EXPR_SHR, $3); } + | expr '+' expr { $$ = p_expr_new_tree($1, EXPR_ADD, $3); } + | expr '-' expr { $$ = p_expr_new_tree($1, EXPR_SUB, $3); } + | expr '*' expr { $$ = p_expr_new_tree($1, EXPR_MUL, $3); } + | expr '/' expr { $$ = p_expr_new_tree($1, EXPR_DIV, $3); } + | expr SIGNDIV expr { $$ = p_expr_new_tree($1, EXPR_SIGNDIV, $3); } + | expr '%' expr { $$ = p_expr_new_tree($1, EXPR_MOD, $3); } + | expr SIGNMOD expr { $$ = p_expr_new_tree($1, EXPR_SIGNMOD, $3); } | '+' expr %prec UNARYOP { $$ = $2; } - | '-' expr %prec UNARYOP { $$ = expr_new_branch(EXPR_NEG, $2); } - /*| '!' expr { $$ = expr_new_branch(EXPR_LNOT, $2); }*/ - | '~' expr %prec UNARYOP { $$ = expr_new_branch(EXPR_NOT, $2); } - | SEG expr { $$ = expr_new_branch(EXPR_SEG, $2); } - | WRT expr { $$ = expr_new_branch(EXPR_WRT, $2); } - | expr ':' expr { $$ = expr_new_tree($1, EXPR_SEGOFF, $3); } + | '-' expr %prec UNARYOP { $$ = p_expr_new_branch(EXPR_NEG, $2); } + /*| '!' expr { $$ = p_expr_new_branch(EXPR_LNOT, $2); }*/ + | '~' expr %prec UNARYOP { $$ = p_expr_new_branch(EXPR_NOT, $2); } + | SEG expr { $$ = p_expr_new_branch(EXPR_SEG, $2); } + | WRT expr { $$ = p_expr_new_branch(EXPR_WRT, $2); } + | expr ':' expr { $$ = p_expr_new_tree($1, EXPR_SEGOFF, $3); } | '(' expr ')' { $$ = $2; } ; explabel: ID { - $$ = symrec_use($1); + $$ = symrec_use($1, p_line_index); xfree($1); } | SPECIAL_ID { - $$ = symrec_use($1); + $$ = symrec_use($1, p_line_index); xfree($1); } | LOCAL_ID { - $$ = symrec_use($1); + $$ = symrec_use($1, p_line_index); xfree($1); } | '$' { /* "$" references the current assembly position */ $$ = symrec_define_label("$", nasm_parser_cur_section, - nasm_parser_prev_bc, 0); + nasm_parser_prev_bc, 0, p_line_index); } | START_SECTION_ID { /* "$$" references the start of the current section */ - $$ = symrec_define_label("$$", nasm_parser_cur_section, NULL, 0); + $$ = symrec_define_label("$$", nasm_parser_cur_section, NULL, 0, + p_line_index); } ; @@ -504,64 +527,71 @@ nasm_parser_directive(const char *name, valparamhead *valparams, { valparam *vp, *vp2; symrec *sym; + unsigned long lindex = p_line_index; /* Handle (mostly) output-format independent directives here */ if (strcasecmp(name, "extern") == 0) { vp = vps_first(valparams); if (vp->val) { - sym = symrec_declare(vp->val, SYM_EXTERN); + sym = symrec_declare(vp->val, SYM_EXTERN, lindex); if (nasm_parser_objfmt->extern_declare) - nasm_parser_objfmt->extern_declare(sym, objext_valparams); + nasm_parser_objfmt->extern_declare(sym, objext_valparams, + lindex); } else - Error(_("invalid argument to [%s]"), "EXTERN"); + Error(lindex, _("invalid argument to [%s]"), "EXTERN"); } else if (strcasecmp(name, "global") == 0) { vp = vps_first(valparams); if (vp->val) { - sym = symrec_declare(vp->val, SYM_GLOBAL); + sym = symrec_declare(vp->val, SYM_GLOBAL, lindex); if (nasm_parser_objfmt->global_declare) - nasm_parser_objfmt->global_declare(sym, objext_valparams); + nasm_parser_objfmt->global_declare(sym, objext_valparams, + lindex); } else - Error(_("invalid argument to [%s]"), "GLOBAL"); + Error(lindex, _("invalid argument to [%s]"), "GLOBAL"); } else if (strcasecmp(name, "common") == 0) { vp = vps_first(valparams); if (vp->val) { vp2 = vps_next(vp); if (!vp2 || (!vp2->val && !vp2->param)) - Error(_("no size specified in %s declaration"), "COMMON"); + Error(lindex, _("no size specified in %s declaration"), + "COMMON"); else { if (vp2->val) { - sym = symrec_declare(vp->val, SYM_COMMON); + sym = symrec_declare(vp->val, SYM_COMMON, lindex); if (nasm_parser_objfmt->common_declare) nasm_parser_objfmt->common_declare(sym, - expr_new_ident(ExprSym(symrec_use(vp2->val))), - objext_valparams); + p_expr_new_ident(ExprSym(symrec_use(vp2->val, + lindex))), + objext_valparams, lindex); } else if (vp2->param) { - sym = symrec_declare(vp->val, SYM_COMMON); + sym = symrec_declare(vp->val, SYM_COMMON, lindex); if (nasm_parser_objfmt->common_declare) nasm_parser_objfmt->common_declare(sym, vp2->param, - objext_valparams); + objext_valparams, + lindex); vp2->param = NULL; } } } else - Error(_("invalid argument to [%s]"), "COMMON"); + Error(lindex, _("invalid argument to [%s]"), "COMMON"); } else if (strcasecmp(name, "section") == 0 || strcasecmp(name, "segment") == 0) { section *new_section = nasm_parser_objfmt->sections_switch(&nasm_parser_sections, - valparams, objext_valparams); + valparams, objext_valparams, + lindex); if (new_section) { nasm_parser_cur_section = new_section; nasm_parser_prev_bc = bcs_last(section_get_bytecodes(new_section)); } else - Error(_("invalid argument to [%s]"), "SECTION"); + Error(lindex, _("invalid argument to [%s]"), "SECTION"); } else if (strcasecmp(name, "absolute") == 0) { /* it can be just an ID or a complete expression, so handle both. */ vp = vps_first(valparams); if (vp->val) nasm_parser_cur_section = sections_switch_absolute(&nasm_parser_sections, - expr_new_ident(ExprSym(symrec_use(vp->val)))); + p_expr_new_ident(ExprSym(symrec_use(vp->val, lindex)))); else if (vp->param) { nasm_parser_cur_section = sections_switch_absolute(&nasm_parser_sections, vp->param); @@ -571,26 +601,27 @@ nasm_parser_directive(const char *name, valparamhead *valparams, } else if (strcasecmp(name, "cpu") == 0) { vps_foreach(vp, valparams) { if (vp->val) - nasm_parser_arch->parse.switch_cpu(vp->val); + nasm_parser_arch->parse.switch_cpu(vp->val, lindex); else if (vp->param) { const intnum *intcpu; intcpu = expr_get_intnum(&vp->param, NULL); if (!intcpu) - Error(_("invalid argument to [%s]"), "CPU"); + Error(lindex, _("invalid argument to [%s]"), "CPU"); else { char strcpu[16]; sprintf(strcpu, "%lu", intnum_get_uint(intcpu)); - nasm_parser_arch->parse.switch_cpu(strcpu); + nasm_parser_arch->parse.switch_cpu(strcpu, lindex); } } } } else if (!nasm_parser_arch->parse.directive(name, valparams, objext_valparams, - &nasm_parser_sections)) { + &nasm_parser_sections, + lindex)) { ; } else if (nasm_parser_objfmt->directive(name, valparams, objext_valparams, - &nasm_parser_sections)) { - Error(_("unrecognized directive [%s]"), name); + &nasm_parser_sections, lindex)) { + Error(lindex, _("unrecognized directive [%s]"), name); } vps_delete(valparams); @@ -601,6 +632,6 @@ nasm_parser_directive(const char *name, valparamhead *valparams, void nasm_parser_error(const char *s) { - ParserError(s); + ParserError(p_line_index, s); } diff --git a/modules/parsers/nasm/nasm-parser.c b/modules/parsers/nasm/nasm-parser.c index 6d482108..bb78777c 100644 --- a/modules/parsers/nasm/nasm-parser.c +++ b/modules/parsers/nasm/nasm-parser.c @@ -36,7 +36,7 @@ extern int nasm_parser_debug; extern int nasm_parser_parse(void); extern void nasm_parser_cleanup(void); -size_t (*nasm_parser_input) (char *buf, size_t max_size); +size_t (*nasm_parser_input) (char *buf, size_t max_size, linemgr *lm); sectionhead nasm_parser_sections; /*@dependent@*/ section *nasm_parser_cur_section; @@ -45,9 +45,10 @@ extern /*@only@*/ char *nasm_parser_locallabel_base; /*@dependent@*/ arch *nasm_parser_arch; /*@dependent@*/ objfmt *nasm_parser_objfmt; +/*@dependent@*/ linemgr *nasm_parser_linemgr; static /*@dependent@*/ sectionhead * -nasm_parser_do_parse(preproc *pp, arch *a, objfmt *of, FILE *f, +nasm_parser_do_parse(preproc *pp, arch *a, objfmt *of, linemgr *lm, FILE *f, const char *in_filename) /*@globals killed nasm_parser_locallabel_base @*/ { @@ -56,6 +57,7 @@ nasm_parser_do_parse(preproc *pp, arch *a, objfmt *of, FILE *f, nasm_parser_input = pp->input; nasm_parser_arch = a; nasm_parser_objfmt = of; + nasm_parser_linemgr = lm; /* Initialize section list */ nasm_parser_cur_section = sections_initialize(&nasm_parser_sections, of); diff --git a/modules/parsers/nasm/nasm-token.re b/modules/parsers/nasm/nasm-token.re index 52173c16..fc1dd26a 100644 --- a/modules/parsers/nasm/nasm-token.re +++ b/modules/parsers/nasm/nasm-token.re @@ -26,6 +26,7 @@ RCSID("$IdPath$"); #include "bitvect.h" +#include "linemgr.h" #include "errwarn.h" #include "intnum.h" #include "floatnum.h" @@ -62,8 +63,11 @@ void nasm_parser_cleanup(void); void nasm_parser_set_directive_state(void); int nasm_parser_lex(void); -extern size_t (*nasm_parser_input) (char *buf, size_t max_size); +extern size_t (*nasm_parser_input) (char *buf, size_t max_size, linemgr *lm); extern /*@dependent@*/ arch *nasm_parser_arch; +extern /*@dependent@*/ linemgr *nasm_parser_linemgr; + +#define p_line_index (nasm_parser_linemgr->get_current()) typedef struct Scanner { @@ -101,7 +105,8 @@ fill(YYCTYPE *cursor) xfree(s.bot); s.bot = buf; } - if((cnt = nasm_parser_input(s.lim, BSIZE)) != BSIZE){ + if((cnt = nasm_parser_input(s.lim, BSIZE, + nasm_parser_linemgr)) != BSIZE){ s.eof = &s.lim[cnt]; *s.eof++ = '\n'; } s.lim += cnt; @@ -215,7 +220,7 @@ scan: digit+ { savech = s.tok[TOKLEN]; s.tok[TOKLEN] = '\0'; - yylval.intn = intnum_new_dec(s.tok); + yylval.intn = intnum_new_dec(s.tok, p_line_index); s.tok[TOKLEN] = savech; RETURN(INTNUM); } @@ -223,21 +228,21 @@ scan: bindigit+ "b" { s.tok[TOKLEN-1] = '\0'; /* strip off 'b' */ - yylval.intn = intnum_new_bin(s.tok); + yylval.intn = intnum_new_bin(s.tok, p_line_index); RETURN(INTNUM); } /* 777q - octal number */ octdigit+ "q" { s.tok[TOKLEN-1] = '\0'; /* strip off 'q' */ - yylval.intn = intnum_new_oct(s.tok); + yylval.intn = intnum_new_oct(s.tok, p_line_index); RETURN(INTNUM); } /* 0AAh form of hexidecimal number */ digit hexdigit* "h" { s.tok[TOKLEN-1] = '\0'; /* strip off 'h' */ - yylval.intn = intnum_new_hex(s.tok); + yylval.intn = intnum_new_hex(s.tok, p_line_index); RETURN(INTNUM); } @@ -246,9 +251,11 @@ scan: savech = s.tok[TOKLEN]; s.tok[TOKLEN] = '\0'; if (s.tok[1] == 'x') - yylval.intn = intnum_new_hex(s.tok+2); /* skip 0 and x */ + /* skip 0 and x */ + yylval.intn = intnum_new_hex(s.tok+2, p_line_index); else - yylval.intn = intnum_new_hex(s.tok+1); /* don't skip 0 */ + /* don't skip 0 */ + yylval.intn = intnum_new_hex(s.tok+1, p_line_index); s.tok[TOKLEN] = savech; RETURN(INTNUM); } @@ -335,7 +342,8 @@ scan: yylval.str_val = xstrndup(s.tok, TOKLEN); RETURN(ID); } else if (!nasm_parser_locallabel_base) { - Warning(_("no non-local label before `%s'"), s.tok[0]); + Warning(p_line_index, _("no non-local label before `%s'"), + s.tok[0]); yylval.str_val = xstrndup(s.tok, TOKLEN); } else { len = TOKLEN + nasm_parser_locallabel_base_len; @@ -359,7 +367,7 @@ scan: savech = s.tok[TOKLEN]; s.tok[TOKLEN] = '\0'; check_id_ret = nasm_parser_arch->parse.check_identifier( - yylval.arch_data, s.tok); + yylval.arch_data, s.tok, p_line_index); s.tok[TOKLEN] = savech; switch (check_id_ret) { case ARCH_CHECK_ID_NONE: @@ -377,7 +385,8 @@ scan: case ARCH_CHECK_ID_TARGETMOD: RETURN(TARGETMOD); default: - Warning(_("Arch feature not supported, treating as identifier")); + Warning(p_line_index, + _("Arch feature not supported, treating as identifier")); yylval.str_val = xstrndup(s.tok, TOKLEN); RETURN(ID); } @@ -391,7 +400,8 @@ scan: any { if (WARN_ENABLED(WARN_UNRECOGNIZED_CHAR)) - Warning(_("ignoring unrecognized character `%s'"), + Warning(p_line_index, + _("ignoring unrecognized character `%s'"), conv_unprint(s.tok[0])); goto scan; } @@ -406,7 +416,7 @@ linechg: linechg_numcount++; savech = s.tok[TOKLEN]; s.tok[TOKLEN] = '\0'; - yylval.intn = intnum_new_dec(s.tok); + yylval.intn = intnum_new_dec(s.tok, p_line_index); s.tok[TOKLEN] = savech; RETURN(INTNUM); } @@ -430,7 +440,8 @@ linechg: any { if (WARN_ENABLED(WARN_UNRECOGNIZED_CHAR)) - Warning(_("ignoring unrecognized character `%s'"), + Warning(p_line_index, + _("ignoring unrecognized character `%s'"), conv_unprint(s.tok[0])); goto linechg; } @@ -472,7 +483,8 @@ directive: any { if (WARN_ENABLED(WARN_UNRECOGNIZED_CHAR)) - Warning(_("ignoring unrecognized character `%s'"), + Warning(p_line_index, + _("ignoring unrecognized character `%s'"), conv_unprint(s.tok[0])); goto directive; } @@ -490,9 +502,9 @@ stringconst_scan: /*!re2c "\n" { if (cursor == s.eof) - Error(_("unexpected end of file in string")); + Error(p_line_index, _("unexpected end of file in string")); else - Error(_("unterminated string")); + Error(p_line_index, _("unterminated string")); strbuf[count] = '\0'; yylval.str_val = strbuf; RETURN(STRING); diff --git a/modules/preprocs/raw/raw-preproc.c b/modules/preprocs/raw/raw-preproc.c index 6de9b0c2..c6cb89d2 100644 --- a/modules/preprocs/raw/raw-preproc.c +++ b/modules/preprocs/raw/raw-preproc.c @@ -42,7 +42,7 @@ raw_preproc_initialize(FILE *f, const char *in_filename) } static size_t -raw_preproc_input(char *buf, size_t max_size) +raw_preproc_input(char *buf, size_t max_size, unsigned long lindex) { int c = '*'; size_t n; @@ -53,9 +53,9 @@ raw_preproc_input(char *buf, size_t max_size) if (c == '\n') buf[n++] = (char)c; if (c == EOF && ferror(in)) - Error(_("error when reading from file")); + Error(lindex, _("error when reading from file")); } else if (((n = fread(buf, 1, max_size, in)) == 0) && ferror(in)) - Error(_("error when reading from file")); + Error(lindex, _("error when reading from file")); return n; } diff --git a/modules/preprocs/yapp/yapp-preproc.c b/modules/preprocs/yapp/yapp-preproc.c index 230529b8..681f7b78 100644 --- a/modules/preprocs/yapp/yapp-preproc.c +++ b/modules/preprocs/yapp/yapp-preproc.c @@ -22,6 +22,7 @@ #include "util.h" /*@unused@*/ RCSID("$IdPath$"); +#include "linemgr.h" #include "errwarn.h" #include "preproc.h" #include "hamt.h" @@ -36,9 +37,12 @@ static int saved_length; static HAMT *macro_table; -YAPP_Output current_output; +static YAPP_Output current_output; YYSTYPE yapp_preproc_lval; +/*@dependent@*/ linemgr *yapp_preproc_linemgr; +#define p_line_index (yapp_preproc_linemgr->get_current()) + int isatty(int); /* Build source and macro representations */ @@ -116,13 +120,13 @@ yapp_macro_insert (char *name, int argc, int fillargs) void yapp_macro_error_exists (YAPP_Macro *v) { - if (v) Error(_("Redefining macro of the same name %d:%d"), v->type, v->args); + if (v) Error(p_line_index, _("Redefining macro of the same name %d:%d"), v->type, v->args); } void yapp_macro_error_sameargname (YAPP_Macro *v) { - if (v) Error(_("Duplicate argument names in macro")); + if (v) Error(p_line_index, _("Duplicate argument names in macro")); } YAPP_Macro * @@ -137,7 +141,7 @@ yapp_define_insert (char *name, int argc, int fillargs) if ((argc >= 0 && ym->args < 0) || (argc < 0 && ym->args >= 0)) { - Warning(_("Attempted %%define both with and without parameters")); + Warning(p_line_index, _("Attempted %%define both with and without parameters")); return NULL; } } @@ -236,8 +240,8 @@ static void yapp_preproc_initialize(FILE *f, const char *in_filename) { is_interactive = f ? (isatty(fileno(f)) > 0) : 0; - current_file = xstrdup(in_filename); - line_number = 1; + yapp_preproc_current_file = xstrdup(in_filename); + yapp_preproc_line_number = 1; yapp_lex_initialize(f); SLIST_INIT(&output_head); SLIST_INIT(&source_head); @@ -364,8 +368,8 @@ append_token(int token, struct source_head *to_head, source **to_tail) && (token == '\n' || token == LINE)) { free ((*to_tail)->token.str); - (*to_tail)->token.str = xmalloc(23+strlen(current_file)); - sprintf((*to_tail)->token.str, "%%line %d+1 %s\n", line_number, current_file); + (*to_tail)->token.str = xmalloc(23+strlen(yapp_preproc_current_file)); + sprintf((*to_tail)->token.str, "%%line %d+1 %s\n", yapp_preproc_line_number, yapp_preproc_current_file); } else { src = xmalloc(sizeof(source)); @@ -400,8 +404,8 @@ append_token(int token, struct source_head *to_head, source **to_tail) case LINE: /* TODO: consider removing any trailing newline or LINE tokens */ - src->token.str = xmalloc(23+strlen(current_file)); - sprintf(src->token.str, "%%line %d+1 %s\n", line_number, current_file); + src->token.str = xmalloc(23+strlen(yapp_preproc_current_file)); + sprintf(src->token.str, "%%line %d+1 %s\n", yapp_preproc_line_number, yapp_preproc_current_file); break; default: @@ -457,7 +461,7 @@ eat_through_return(struct source_head *to_head, source **to_tail) while ((token = yapp_preproc_lex()) != '\n') { if (token == 0) return 0; - Error(_("Skipping possibly valid %%define stuff")); + Error(p_line_index, _("Skipping possibly valid %%define stuff")); } append_token('\n', to_head, to_tail); return '\n'; @@ -470,7 +474,7 @@ yapp_get_ident(const char *synlvl) if (token == WHITESPACE) token = yapp_preproc_lex(); if (token != IDENT) { - Error(_("Identifier expected after %%%s"), synlvl); + Error(p_line_index, _("Identifier expected after %%%s"), synlvl); } return token; } @@ -579,7 +583,7 @@ expand_macro(char *name, if (token < 256) InternalError(_("Unexpected character token in parameter expansion")); else - Error(_("Cannot handle preprocessor items inside possible macro invocation")); + Error(p_line_index, _("Cannot handle preprocessor items inside possible macro invocation")); } } @@ -739,13 +743,15 @@ expand_token_list(struct source_head *paramexp, struct source_head *to_head, sou } static size_t -yapp_preproc_input(char *buf, size_t max_size) +yapp_preproc_input(char *buf, size_t max_size, linemgr *lm) { static YAPP_State state = YAPP_STATE_INITIAL; int n = 0; int token; int need_line_directive = 0; + yapp_preproc_linemgr = lm; + while (saved_length < max_size && state != YAPP_STATE_EOF) { token = yapp_preproc_lex(); @@ -759,7 +765,7 @@ yapp_preproc_input(char *buf, size_t max_size) append_token(token, &source_head, &source_tail); /*if (append_to_return()==0) state=YAPP_STATE_EOF;*/ ydebug(("YAPP: default: '%c' \"%s\"\n", token, yapp_preproc_lval.str_val)); - /*Error(_("YAPP got an unhandled token."));*/ + /*Error(p_line_index, _("YAPP got an unhandled token."));*/ break; case IDENT: @@ -828,7 +834,7 @@ yapp_preproc_input(char *buf, size_t max_size) break; } else if (last_token == ',' || token != ',') - Error(_("Unexpected token in %%define parameters")); + Error(p_line_index, _("Unexpected token in %%define parameters")); last_token = token; } if (token == ')') { @@ -908,7 +914,7 @@ yapp_preproc_input(char *buf, size_t max_size) } break; default: - Error(_("YAPP got into a bad state")); + Error(p_line_index, _("YAPP got into a bad state")); } if (need_line_directive) { append_token(LINE, &source_head, &source_tail); diff --git a/modules/preprocs/yapp/yapp-token.h b/modules/preprocs/yapp/yapp-token.h index 8d503aab..01c67b7a 100644 --- a/modules/preprocs/yapp/yapp-token.h +++ b/modules/preprocs/yapp/yapp-token.h @@ -58,7 +58,9 @@ typedef union { extern YYSTYPE yapp_preproc_lval; -extern char *current_file; -extern int line_number; +extern char *yapp_preproc_current_file; +extern int yapp_preproc_line_number; +extern /*@dependent@*/ linemgr *yapp_preproc_linemgr; +#define p_line_index (yapp_preproc_linemgr->get_current()) int yapp_preproc_lex(void); diff --git a/modules/preprocs/yapp/yapp-token.l b/modules/preprocs/yapp/yapp-token.l index 4b7a7b28..17072e20 100644 --- a/modules/preprocs/yapp/yapp-token.l +++ b/modules/preprocs/yapp/yapp-token.l @@ -25,6 +25,7 @@ #include +#include "linemgr.h" #include "errwarn.h" #include "src/preprocs/yapp/yapp-preproc.h" @@ -52,8 +53,8 @@ struct include_s { }; typedef struct include_s include; -char *current_file; -int line_number; +char *yapp_preproc_current_file; +int yapp_preproc_line_number; %} %option noyywrap @@ -142,9 +143,9 @@ DIR %[ \t]* } if(inch == '\n') - Error(_("unterminated string")); + Error(p_line_index, _("unterminated string")); else if(inch == EOF) - Error(_("unexpected end of file in string")); + Error(p_line_index, _("unexpected end of file in string")); strbuf[count] = '\0'; @@ -175,18 +176,18 @@ DIR %[ \t]* /* FIXME: handle includes that aren't relative */ incfile = fopen (yytext, "r"); if(!incfile) { - Error(_("include file `%s': %s"), + Error(p_line_index, _("include file `%s': %s"), yytext, strerror(errno)); free(inc); } else { yyin = incfile; - inc->filename = current_file; - inc->line_number = line_number; + inc->filename = yapp_preproc_current_file; + inc->line_number = yapp_preproc_line_number; SLIST_INSERT_HEAD(&includes_head, inc, next); - line_number = 1; - current_file = xstrdup(yytext); + yapp_preproc_line_number = 1; + yapp_preproc_current_file = xstrdup(yytext); BEGIN(INITIAL); yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE)); } @@ -203,9 +204,9 @@ DIR %[ \t]* inc = SLIST_FIRST(&includes_head); yy_delete_buffer (YY_CURRENT_BUFFER); yy_switch_to_buffer (inc->include_state); - free(current_file); - current_file = inc->filename; - line_number = inc->line_number + 1; + free(yapp_preproc_current_file); + yapp_preproc_current_file = inc->filename; + yapp_preproc_line_number = inc->line_number + 1; SLIST_REMOVE_HEAD(&includes_head, next); free(inc); @@ -223,16 +224,16 @@ DIR %[ \t]* {DIR}line[^\n] ; {DIR}line BEGIN(line); -{DIGIT}+ line_number = strtoul(yytext, (char **)NULL, 10); +{DIGIT}+ yapp_preproc_line_number = strtoul(yytext, (char **)NULL, 10); {DIGIT}+{WS}*\n { - line_number = strtoul(yytext, (char **)NULL, 10); + yapp_preproc_line_number = strtoul(yytext, (char **)NULL, 10); BEGIN(INITIAL); return LINE; } {WS}+["] ; /* eat space before file */ [^ \t\n"]* { /* have the filename */ - free(current_file); - current_file = xstrdup(yytext); + free(yapp_preproc_current_file); + yapp_preproc_current_file = xstrdup(yytext); } ["]{WS}*\n { BEGIN(INITIAL); @@ -321,23 +322,23 @@ DIR %[ \t]* {DIR}pop[^\n]* ; {DIR}repl[^\n]* ; -[^%\n]*\n { line_number++; return '\n'; } +[^%\n]*\n { yapp_preproc_line_number++; return '\n'; } -;.*\n { line_number++; return '\n'; } +;.*\n { yapp_preproc_line_number++; return '\n'; } {WS}+ { yylval.str_val = yytext; return WHITESPACE; } -{WS}*\n { line_number++; return '\n'; } +{WS}*\n { yapp_preproc_line_number++; return '\n'; } [][+*/,()-] { return yytext[0]; } . { - Warning(_("Unhandled character in `%s'"), conv_unprint(yytext[0])); + Warning(p_line_index, _("Unhandled character in `%s'"), conv_unprint(yytext[0])); } . { - Warning(_("ignoring unrecognized character `%s'"), + Warning(p_line_index, _("ignoring unrecognized character `%s'"), conv_unprint(yytext[0])); } diff --git a/src/Makefile.inc b/src/Makefile.inc index 5eca5ae8..1926b5ae 100644 --- a/src/Makefile.inc +++ b/src/Makefile.inc @@ -11,8 +11,7 @@ libyasm_la_SOURCES = \ src/expr-int.h \ src/symrec.c \ src/symrec.h \ - src/globals.c \ - src/globals.h \ + src/linemgr.h \ src/util.h \ src/coretype.h \ src/file.c \ @@ -47,7 +46,8 @@ yasm_SOURCES += \ src/objfmt.c \ src/parser.c \ src/module.h \ - src/module.c + src/module.c \ + src/linemgr.c EXTRA_DIST += \ diff --git a/src/arch.c b/src/arch.c index bf46b3c0..d26fc917 100644 --- a/src/arch.c +++ b/src/arch.c @@ -22,7 +22,6 @@ #include "util.h" /*@unused@*/ RCSID("$IdPath$"); -#include "globals.h" #include "expr.h" #include "bytecode.h" diff --git a/src/arch.h b/src/arch.h index 02f93db6..3d23ea8c 100644 --- a/src/arch.h +++ b/src/arch.h @@ -69,7 +69,7 @@ struct arch { * parse functions! The bytecode and output functions should be able * to handle any CPU. */ - void (*switch_cpu) (const char *cpuid); + void (*switch_cpu) (const char *cpuid, unsigned long lindex); /* Checks an generic identifier to see if it matches architecture * specific names for instructions, registers, etc (see the @@ -82,7 +82,8 @@ struct arch { * used for TARGETMOD, REG, and SEGREG return values. */ arch_check_id_retval (*check_identifier) (unsigned long data[4], - const char *id); + const char *id, + unsigned long lindex); /* Architecture-specific directive support. Returns 1 if directive was * not recognized. Returns 0 if directive was recognized, even if it @@ -91,7 +92,7 @@ struct arch { */ int (*directive) (const char *name, valparamhead *valparams, /*@null@*/ valparamhead *objext_valparams, - sectionhead *headp); + sectionhead *headp, unsigned long lindex); /* Creates an instruction. Creates a bytecode by matching the * instruction data and the parameters given with a valid instruction. @@ -102,20 +103,24 @@ struct arch { int num_operands, /*@null@*/ insn_operandhead *operands, section *cur_section, - /*@null@*/ bytecode *prev_bc); + /*@null@*/ bytecode *prev_bc, + unsigned long lindex); /* Handle an instruction prefix by modifying bc as necessary. */ - void (*handle_prefix) (bytecode *bc, const unsigned long data[4]); + void (*handle_prefix) (bytecode *bc, const unsigned long data[4], + unsigned long lindex); /* Handle an segment register instruction prefix by modifying bc as * necessary. */ - void (*handle_seg_prefix) (bytecode *bc, unsigned long segreg); + void (*handle_seg_prefix) (bytecode *bc, unsigned long segreg, + unsigned long lindex); /* Handle memory expression segment overrides by modifying ea as * necessary. */ - void (*handle_seg_override) (effaddr *ea, unsigned long segreg); + void (*handle_seg_override) (effaddr *ea, unsigned long segreg, + unsigned long lindex); /* Convert an expression into an effective address. */ effaddr * (*ea_new_expr) (/*@keep@*/ expr *e); diff --git a/src/arch/x86/x86arch.c b/src/arch/x86/x86arch.c index bf962947..36d3db9f 100644 --- a/src/arch/x86/x86arch.c +++ b/src/arch/x86/x86arch.c @@ -24,7 +24,6 @@ #include "file.h" -#include "globals.h" #include "errwarn.h" #include "intnum.h" #include "floatnum.h" @@ -42,7 +41,7 @@ unsigned char yasm_x86_LTX_mode_bits = 0; int x86_directive(const char *name, valparamhead *valparams, /*@unused@*/ /*@null@*/ valparamhead *objext_valparams, - /*@unused@*/ sectionhead *headp) + /*@unused@*/ sectionhead *headp, unsigned long lindex) { valparam *vp; const intnum *intn; @@ -54,7 +53,7 @@ x86_directive(const char *name, valparamhead *valparams, (lval = intnum_get_int(intn)) && (lval == 16 || lval == 32)) yasm_x86_LTX_mode_bits = (unsigned char)lval; else - Error(_("invalid argument to [%s]"), "BITS"); + Error(lindex, _("invalid argument to [%s]"), "BITS"); return 0; } else return 1; @@ -132,11 +131,12 @@ x86_segreg_print(FILE *f, unsigned long segreg) } void -x86_handle_prefix(bytecode *bc, const unsigned long data[4]) +x86_handle_prefix(bytecode *bc, const unsigned long data[4], + unsigned long lindex) { switch((x86_parse_insn_prefix)data[0]) { case X86_LOCKREP: - x86_bc_insn_set_lockrep_prefix(bc, (unsigned char)data[1]); + x86_bc_insn_set_lockrep_prefix(bc, (unsigned char)data[1], lindex); break; case X86_ADDRSIZE: x86_bc_insn_addrsize_override(bc, (unsigned char)data[1]); @@ -148,15 +148,17 @@ x86_handle_prefix(bytecode *bc, const unsigned long data[4]) } void -x86_handle_seg_prefix(bytecode *bc, unsigned long segreg) +x86_handle_seg_prefix(bytecode *bc, unsigned long segreg, unsigned long lindex) { - x86_ea_set_segment(x86_bc_insn_get_ea(bc), (unsigned char)(segreg>>8)); + x86_ea_set_segment(x86_bc_insn_get_ea(bc), (unsigned char)(segreg>>8), + lindex); } void -x86_handle_seg_override(effaddr *ea, unsigned long segreg) +x86_handle_seg_override(effaddr *ea, unsigned long segreg, + unsigned long lindex) { - x86_ea_set_segment(ea, (unsigned char)(segreg>>8)); + x86_ea_set_segment(ea, (unsigned char)(segreg>>8), lindex); } /* Define arch structure -- see arch.h for details */ diff --git a/src/arch/x86/x86arch.h b/src/arch/x86/x86arch.h index 824912b0..4674070b 100644 --- a/src/arch/x86/x86arch.h +++ b/src/arch/x86/x86arch.h @@ -62,7 +62,8 @@ typedef enum { JR_NEAR_FORCED } x86_jmprel_opcode_sel; -void x86_ea_set_segment(/*@null@*/ effaddr *ea, unsigned char segment); +void x86_ea_set_segment(/*@null@*/ effaddr *ea, unsigned char segment, + unsigned long lindex); void x86_ea_set_disponly(effaddr *ea); effaddr *x86_ea_new_reg(unsigned char reg); effaddr *x86_ea_new_imm(/*@keep@*/expr *imm, unsigned char im_len); @@ -72,16 +73,15 @@ effaddr *x86_ea_new_expr(/*@keep@*/ expr *e); void x86_bc_insn_opersize_override(bytecode *bc, unsigned char opersize); void x86_bc_insn_addrsize_override(bytecode *bc, unsigned char addrsize); -void x86_bc_insn_set_lockrep_prefix(bytecode *bc, unsigned char prefix); - -void x86_set_jmprel_opcode_sel(x86_jmprel_opcode_sel *old_sel, - x86_jmprel_opcode_sel new_sel); +void x86_bc_insn_set_lockrep_prefix(bytecode *bc, unsigned char prefix, + unsigned long lindex); /* Structure with *all* inputs passed to x86_bytecode_new_insn(). * IMPORTANT: ea_ptr and im_ptr cannot be reused or freed after calling the * function (it doesn't make a copy). */ typedef struct x86_new_insn_data { + unsigned long lindex; /*@keep@*/ /*@null@*/ effaddr *ea; /*@keep@*/ /*@null@*/ expr *imm; unsigned char opersize; @@ -100,6 +100,7 @@ bytecode *x86_bc_new_insn(x86_new_insn_data *d); * Pass 0 for the opcode_len if that version of the opcode doesn't exist. */ typedef struct x86_new_jmprel_data { + unsigned long lindex; /*@keep@*/ expr *target; x86_jmprel_opcode_sel op_sel; unsigned char short_op_len; @@ -128,26 +129,31 @@ int x86_expr_checkea(expr **ep, unsigned char *addrsize, unsigned char bits, unsigned char *v_sib, unsigned char *n_sib, calc_bc_dist_func calc_bc_dist); -void x86_switch_cpu(const char *cpuid); +void x86_switch_cpu(const char *cpuid, unsigned long lindex); arch_check_id_retval x86_check_identifier(unsigned long data[2], - const char *id); + const char *id, + unsigned long lindex); int x86_directive(const char *name, valparamhead *valparams, /*@null@*/ valparamhead *objext_valparams, - sectionhead *headp); + sectionhead *headp, unsigned long lindex); /*@null@*/ bytecode *x86_new_insn(const unsigned long data[2], int num_operands, /*@null@*/ insn_operandhead *operands, section *cur_section, - /*@null@*/ bytecode *prev_bc); + /*@null@*/ bytecode *prev_bc, + unsigned long lindex); -void x86_handle_prefix(bytecode *bc, const unsigned long data[4]); +void x86_handle_prefix(bytecode *bc, const unsigned long data[4], + unsigned long lindex); -void x86_handle_seg_prefix(bytecode *bc, unsigned long segreg); +void x86_handle_seg_prefix(bytecode *bc, unsigned long segreg, + unsigned long lindex); -void x86_handle_seg_override(effaddr *ea, unsigned long segreg); +void x86_handle_seg_override(effaddr *ea, unsigned long segreg, + unsigned long lindex); int x86_floatnum_tobytes(const floatnum *flt, unsigned char **bufp, unsigned long valsize, const expr *e); diff --git a/src/arch/x86/x86bc.c b/src/arch/x86/x86bc.c index d6aa652c..26d76055 100644 --- a/src/arch/x86/x86bc.c +++ b/src/arch/x86/x86bc.c @@ -24,7 +24,6 @@ #include "file.h" -#include "globals.h" #include "errwarn.h" #include "intnum.h" #include "expr.h" @@ -125,7 +124,7 @@ x86_bc_new_insn(x86_new_insn_data *d) x86_insn *insn; insn = (x86_insn *)bc_new_common((bytecode_type)X86_BC_INSN, - sizeof(x86_insn)); + sizeof(x86_insn), d->lindex); insn->ea = (x86_effaddr *)d->ea; if (d->ea) { @@ -165,15 +164,15 @@ x86_bc_new_jmprel(x86_new_jmprel_data *d) x86_jmprel *jmprel; jmprel = (x86_jmprel *)bc_new_common((bytecode_type)X86_BC_JMPREL, - sizeof(x86_jmprel)); + sizeof(x86_jmprel), d->lindex); jmprel->target = d->target; jmprel->op_sel = d->op_sel; if ((d->op_sel == JR_SHORT_FORCED) && (d->near_op_len == 0)) - Error(_("no SHORT form of that jump instruction exists")); + Error(d->lindex, _("no SHORT form of that jump instruction exists")); if ((d->op_sel == JR_NEAR_FORCED) && (d->short_op_len == 0)) - Error(_("no NEAR form of that jump instruction exists")); + Error(d->lindex, _("no NEAR form of that jump instruction exists")); jmprel->shortop.opcode[0] = d->short_op[0]; jmprel->shortop.opcode[1] = d->short_op[1]; @@ -196,7 +195,7 @@ x86_bc_new_jmprel(x86_new_jmprel_data *d) /*@=compmempass =mustfree@*/ void -x86_ea_set_segment(effaddr *ea, unsigned char segment) +x86_ea_set_segment(effaddr *ea, unsigned char segment, unsigned long lindex) { x86_effaddr *x86_ea = (x86_effaddr *)ea; @@ -204,7 +203,7 @@ x86_ea_set_segment(effaddr *ea, unsigned char segment) return; if (segment != 0 && x86_ea->segment != 0) - Warning(_("multiple segment overrides, using leftmost")); + Warning(lindex, _("multiple segment overrides, using leftmost")); x86_ea->segment = segment; } @@ -347,7 +346,8 @@ x86_bc_insn_addrsize_override(bytecode *bc, unsigned char addrsize) } void -x86_bc_insn_set_lockrep_prefix(bytecode *bc, unsigned char prefix) +x86_bc_insn_set_lockrep_prefix(bytecode *bc, unsigned char prefix, + unsigned long lindex) { x86_insn *insn; x86_jmprel *jmprel; @@ -370,24 +370,11 @@ x86_bc_insn_set_lockrep_prefix(bytecode *bc, unsigned char prefix) } if (*lockrep_pre != 0) - Warning(_("multiple LOCK or REP prefixes, using leftmost")); + Warning(lindex, _("multiple LOCK or REP prefixes, using leftmost")); *lockrep_pre = prefix; } -void -x86_set_jmprel_opcode_sel(x86_jmprel_opcode_sel *old_sel, - x86_jmprel_opcode_sel new_sel) -{ - if (!old_sel) - return; - - if (new_sel != JR_NONE && ((*old_sel == JR_SHORT_FORCED) || - (*old_sel == JR_NEAR_FORCED))) - Warning(_("multiple SHORT or NEAR specifiers, using leftmost")); - *old_sel = new_sel; -} - void x86_bc_delete(bytecode *bc) { @@ -667,20 +654,20 @@ x86_bc_resolve_jmprel(x86_jmprel *jmprel, unsigned long *len, int save, temp = expr_copy(jmprel->target); num = expr_get_intnum(&temp, calc_bc_dist); if (!num) { - ErrorAt(bc->line, - _("short jump target external or out of segment")); + Error(bc->line, + _("short jump target external or out of segment")); return BC_RESOLVE_ERROR | BC_RESOLVE_UNKNOWN_LEN; } else { rel = intnum_get_int(num); rel -= jmprel->shortop.opcode_len+1; /* does a short form exist? */ if (jmprel->shortop.opcode_len == 0) { - ErrorAt(bc->line, _("short jump does not exist")); + Error(bc->line, _("short jump does not exist")); return BC_RESOLVE_ERROR | BC_RESOLVE_UNKNOWN_LEN; } /* short displacement must fit in -128 <= rel <= +127 */ if (rel < -128 || rel > 127) { - ErrorAt(bc->line, _("short jump out of range")); + Error(bc->line, _("short jump out of range")); return BC_RESOLVE_ERROR | BC_RESOLVE_UNKNOWN_LEN; } } @@ -691,7 +678,7 @@ x86_bc_resolve_jmprel(x86_jmprel *jmprel, unsigned long *len, int save, jrshort = 0; if (save) { if (jmprel->nearop.opcode_len == 0) { - ErrorAt(bc->line, _("near jump does not exist")); + Error(bc->line, _("near jump does not exist")); return BC_RESOLVE_ERROR | BC_RESOLVE_UNKNOWN_LEN; } } @@ -726,7 +713,8 @@ x86_bc_resolve_jmprel(x86_jmprel *jmprel, unsigned long *len, int save, * it to actually be within short range). */ if (save) { - ErrorAt(bc->line, _("short jump out of range (near jump does not exist)")); + Error(bc->line, + _("short jump out of range (near jump does not exist)")); return BC_RESOLVE_ERROR | BC_RESOLVE_UNKNOWN_LEN; } jrshort = 1; @@ -742,8 +730,8 @@ x86_bc_resolve_jmprel(x86_jmprel *jmprel, unsigned long *len, int save, jrshort = 0; } else { if (save) { - ErrorAt(bc->line, - _("short jump out of range (near jump does not exist)")); + Error(bc->line, + _("short jump out of range (near jump does not exist)")); return BC_RESOLVE_ERROR | BC_RESOLVE_UNKNOWN_LEN; } jrshort = 1; @@ -928,7 +916,7 @@ x86_bc_tobytes_jmprel(x86_jmprel *jmprel, unsigned char **bufp, case JR_NEAR: /* 2/4 byte relative displacement (depending on operand size) */ if (jmprel->nearop.opcode_len == 0) { - ErrorAt(bc->line, _("near jump does not exist")); + Error(bc->line, _("near jump does not exist")); return 1; } diff --git a/src/arch/x86/x86expr.c b/src/arch/x86/x86expr.c index 20c1b677..ea022f6d 100644 --- a/src/arch/x86/x86expr.c +++ b/src/arch/x86/x86expr.c @@ -24,7 +24,6 @@ #include "bitvect.h" -#include "globals.h" #include "errwarn.h" #include "intnum.h" #include "floatnum.h" @@ -379,7 +378,7 @@ x86_checkea_calc_displen(expr **ep, unsigned int wordsize, int noreg, */ if (!intnum_check_size(intn, (size_t)wordsize, 0) && !intnum_check_size(intn, 1, 1)) { - ErrorAt(e->line, _("invalid effective address")); + Error(e->line, _("invalid effective address")); return 0; } @@ -443,8 +442,8 @@ x86_checkea_calc_displen(expr **ep, unsigned int wordsize, int noreg, case 2: case 4: if (wordsize != *displen) { - ErrorAt(e->line, - _("invalid effective address (displacement size)")); + Error(e->line, + _("invalid effective address (displacement size)")); return 0; } /* TODO: Add optional warning here about 2/4 not being valid @@ -536,7 +535,7 @@ x86_expr_checkea(expr **ep, unsigned char *addrsize, unsigned char bits, calc_bc_dist)) { case 0: e = *ep; - ErrorAt(e->line, _("invalid effective address")); + Error(e->line, _("invalid effective address")); return 0; case 1: return 1; @@ -558,7 +557,7 @@ x86_expr_checkea(expr **ep, unsigned char *addrsize, unsigned char bits, */ for (i=0; i<8; i++) { if (reg32mult[i] < 0) { - ErrorAt(e->line, _("invalid effective address")); + Error(e->line, _("invalid effective address")); return 0; } if (i != indexreg && reg32mult[i] == 1 && basereg == REG32_NONE) @@ -599,7 +598,7 @@ x86_expr_checkea(expr **ep, unsigned char *addrsize, unsigned char bits, */ for (i=0; i<8; i++) if (i != basereg && i != indexreg && reg32mult[i] != 0) { - ErrorAt(e->line, _("invalid effective address")); + Error(e->line, _("invalid effective address")); return 0; } @@ -607,7 +606,7 @@ x86_expr_checkea(expr **ep, unsigned char *addrsize, unsigned char bits, if (indexreg != REG32_NONE && reg32mult[indexreg] != 1 && reg32mult[indexreg] != 2 && reg32mult[indexreg] != 4 && reg32mult[indexreg] != 8) { - ErrorAt(e->line, _("invalid effective address")); + Error(e->line, _("invalid effective address")); return 0; } @@ -617,7 +616,7 @@ x86_expr_checkea(expr **ep, unsigned char *addrsize, unsigned char bits, * legal. */ if (reg32mult[REG32_ESP] > 1 || basereg == REG32_ESP) { - ErrorAt(e->line, _("invalid effective address")); + Error(e->line, _("invalid effective address")); return 0; } /* If mult==1 and basereg is not ESP, swap indexreg w/basereg. */ @@ -721,7 +720,7 @@ x86_expr_checkea(expr **ep, unsigned char *addrsize, unsigned char bits, calc_bc_dist)) { case 0: e = *ep; - ErrorAt(e->line, _("invalid effective address")); + Error(e->line, _("invalid effective address")); return 0; case 1: return 1; @@ -733,7 +732,7 @@ x86_expr_checkea(expr **ep, unsigned char *addrsize, unsigned char bits, /* reg multipliers not 0 or 1 are illegal. */ if (reg16mult.bx & ~1 || reg16mult.si & ~1 || reg16mult.di & ~1 || reg16mult.bp & ~1) { - ErrorAt(e->line, _("invalid effective address")); + Error(e->line, _("invalid effective address")); return 0; } @@ -749,7 +748,7 @@ x86_expr_checkea(expr **ep, unsigned char *addrsize, unsigned char bits, /* Check the modrm value for invalid combinations. */ if (modrm16[havereg] & 0070) { - ErrorAt(e->line, _("invalid effective address")); + Error(e->line, _("invalid effective address")); return 0; } @@ -777,17 +776,17 @@ x86_floatnum_tobytes(const floatnum *flt, unsigned char **bufp, int fltret; if (!floatnum_check_size(flt, (size_t)valsize)) { - ErrorAt(e->line, _("invalid floating point constant size")); + Error(e->line, _("invalid floating point constant size")); return 1; } fltret = floatnum_get_sized(flt, *bufp, (size_t)valsize); if (fltret < 0) { - ErrorAt(e->line, _("underflow in floating point expression")); + Error(e->line, _("underflow in floating point expression")); return 1; } if (fltret > 0) { - ErrorAt(e->line, _("overflow in floating point expression")); + Error(e->line, _("overflow in floating point expression")); return 1; } *bufp += valsize; diff --git a/src/arch/x86/x86id.re b/src/arch/x86/x86id.re index cd729306..4cee4eed 100644 --- a/src/arch/x86/x86id.re +++ b/src/arch/x86/x86id.re @@ -24,7 +24,6 @@ RCSID("$IdPath$"); #include "bitvect.h" -#include "globals.h" #include "errwarn.h" #include "intnum.h" #include "floatnum.h" @@ -1308,7 +1307,8 @@ static const x86_insn_info xbts_insn[] = { static bytecode * x86_new_jmprel(const unsigned long data[4], int num_operands, insn_operandhead *operands, x86_insn_info *jrinfo, - section *cur_section, /*@null@*/ bytecode *prev_bc) + section *cur_section, /*@null@*/ bytecode *prev_bc, + unsigned long lindex) { x86_new_jmprel_data d; int num_info = (int)(data[1]&0xFF); @@ -1317,13 +1317,15 @@ x86_new_jmprel(const unsigned long data[4], int num_operands, insn_operand *op; static const unsigned char size_lookup[] = {0, 8, 16, 32, 64, 80, 128, 0}; + d.lindex = lindex; + /* We know the target is in operand 0, but sanity check for Imm. */ op = ops_first(operands); if (op->type != INSN_OPERAND_IMM) InternalError(_("invalid operand conversion")); d.target = expr_new(EXPR_SUB, ExprExpr(op->data.val), ExprSym(symrec_define_label("$", cur_section, prev_bc, - 0))); + 0, lindex)), lindex); /* See if the user explicitly specified short/near. */ switch (jrinfo->operands[0] & OPTM_MASK) { @@ -1398,7 +1400,7 @@ x86_new_jmprel(const unsigned long data[4], int num_operands, bytecode * x86_new_insn(const unsigned long data[4], int num_operands, insn_operandhead *operands, section *cur_section, - /*@null@*/ bytecode *prev_bc) + /*@null@*/ bytecode *prev_bc, unsigned long lindex) { x86_new_insn_data d; int num_info = (int)(data[1]&0xFF); @@ -1616,7 +1618,7 @@ x86_new_insn(const unsigned long data[4], int num_operands, if (!found) { /* Didn't find a matching one */ - Error(_("invalid combination of opcode and operands")); + Error(lindex, _("invalid combination of opcode and operands")); return NULL; } @@ -1628,10 +1630,10 @@ x86_new_insn(const unsigned long data[4], int num_operands, case MOD_ExtErr: switch ((info->modifiers & MOD_ExtIndex_MASK)>>MOD_ExtIndex_SHIFT) { case 0: - Error(_("mismatch in operand sizes")); + Error(lindex, _("mismatch in operand sizes")); break; case 1: - Error(_("operand size not specified")); + Error(lindex, _("operand size not specified")); break; default: InternalError(_("unrecognized x86 ext mod index")); @@ -1650,9 +1652,10 @@ x86_new_insn(const unsigned long data[4], int num_operands, /* Shortcut to JmpRel */ if (operands && (info->operands[0] & OPA_MASK) == OPA_JmpRel) return x86_new_jmprel(data, num_operands, operands, info, cur_section, - prev_bc); + prev_bc, lindex); /* Copy what we can from info */ + d.lindex = lindex; d.ea = NULL; d.imm = NULL; d.opersize = info->opersize; @@ -1692,7 +1695,8 @@ x86_new_insn(const unsigned long data[4], int num_operands, mod_data >>= 8; } if (info->modifiers & MOD_Imm8) { - d.imm = expr_new_ident(ExprInt(intnum_new_int(mod_data & 0xFF))); + d.imm = expr_new_ident(ExprInt(intnum_new_int(mod_data & 0xFF)), + lindex); d.im_len = 1; /*mod_data >>= 8;*/ } @@ -1840,7 +1844,7 @@ x86_new_insn(const unsigned long data[4], int num_operands, */ void -x86_switch_cpu(const char *id) +x86_switch_cpu(const char *id, unsigned long lindex) { /*const char *marker;*/ @@ -1949,18 +1953,19 @@ x86_switch_cpu(const char *id) /* catchalls */ [\001-\377]+ { - Warning(_("unrecognized CPU identifier `%s'"), id); + Warning(lindex, _("unrecognized CPU identifier `%s'"), id); return; } [\000] { - Warning(_("unrecognized CPU identifier `%s'"), id); + Warning(lindex, _("unrecognized CPU identifier `%s'"), id); return; } */ } arch_check_id_retval -x86_check_identifier(unsigned long data[4], const char *id) +x86_check_identifier(unsigned long data[4], const char *id, + /*@unused@*/ unsigned long lindex) { const char *oid = id; /*const char *marker;*/ diff --git a/src/bytecode.c b/src/bytecode.c index 0aedd6d0..3a1132d3 100644 --- a/src/bytecode.c +++ b/src/bytecode.c @@ -24,7 +24,6 @@ #include "file.h" -#include "globals.h" #include "errwarn.h" #include "intnum.h" #include "expr.h" @@ -106,9 +105,10 @@ bc_initialize(arch *a) } immval * -imm_new_int(unsigned long int_val) +imm_new_int(unsigned long int_val, unsigned long lindex) { - return imm_new_expr(expr_new_ident(ExprInt(intnum_new_uint(int_val)))); + return imm_new_expr(expr_new_ident(ExprInt(intnum_new_uint(int_val)), + lindex)); } immval * @@ -179,13 +179,13 @@ void bc_set_multiple(bytecode *bc, expr *e) { if (bc->multiple) - bc->multiple = expr_new_tree(bc->multiple, EXPR_MUL, e); + bc->multiple = expr_new_tree(bc->multiple, EXPR_MUL, e, e->line); else bc->multiple = e; } bytecode * -bc_new_common(bytecode_type type, size_t size) +bc_new_common(bytecode_type type, size_t size, unsigned long lindex) { bytecode *bc = xmalloc(size); @@ -194,7 +194,7 @@ bc_new_common(bytecode_type type, size_t size) bc->multiple = (expr *)NULL; bc->len = 0; - bc->line = line_index; + bc->line = lindex; bc->offset = 0; @@ -204,11 +204,12 @@ bc_new_common(bytecode_type type, size_t size) } bytecode * -bc_new_data(datavalhead *datahead, unsigned char size) +bc_new_data(datavalhead *datahead, unsigned char size, unsigned long lindex) { bytecode_data *data; - data = (bytecode_data *)bc_new_common(BC_DATA, sizeof(bytecode_data)); + data = (bytecode_data *)bc_new_common(BC_DATA, sizeof(bytecode_data), + lindex); data->datahead = *datahead; data->size = size; @@ -217,12 +218,13 @@ bc_new_data(datavalhead *datahead, unsigned char size) } bytecode * -bc_new_reserve(expr *numitems, unsigned char itemsize) +bc_new_reserve(expr *numitems, unsigned char itemsize, unsigned long lindex) { bytecode_reserve *reserve; reserve = (bytecode_reserve *)bc_new_common(BC_RESERVE, - sizeof(bytecode_reserve)); + sizeof(bytecode_reserve), + lindex); /*@-mustfree@*/ reserve->numitems = numitems; @@ -233,12 +235,13 @@ bc_new_reserve(expr *numitems, unsigned char itemsize) } bytecode * -bc_new_incbin(char *filename, expr *start, expr *maxlen) +bc_new_incbin(char *filename, expr *start, expr *maxlen, unsigned long lindex) { bytecode_incbin *incbin; incbin = (bytecode_incbin *)bc_new_common(BC_INCBIN, - sizeof(bytecode_incbin)); + sizeof(bytecode_incbin), + lindex); /*@-mustfree@*/ incbin->filename = filename; @@ -250,11 +253,12 @@ bc_new_incbin(char *filename, expr *start, expr *maxlen) } bytecode * -bc_new_align(unsigned long boundary) +bc_new_align(unsigned long boundary, unsigned long lindex) { bytecode_align *align; - align = (bytecode_align *)bc_new_common(BC_ALIGN, sizeof(bytecode_align)); + align = (bytecode_align *)bc_new_common(BC_ALIGN, sizeof(bytecode_align), + lindex); align->boundary = boundary; @@ -263,13 +267,14 @@ bc_new_align(unsigned long boundary) bytecode * bc_new_objfmt_data(unsigned int type, unsigned long len, objfmt *of, - void *data) + void *data, unsigned long lindex) { bytecode_objfmt_data *objfmt_data; objfmt_data = (bytecode_objfmt_data *)bc_new_common(BC_ALIGN, - sizeof(bytecode_objfmt_data)); + sizeof(bytecode_objfmt_data), + lindex); objfmt_data->type = type; objfmt_data->of = of; @@ -346,8 +351,6 @@ bc_print(FILE *f, int indent_level, const bytecode *bc) const bytecode_incbin *incbin; const bytecode_align *align; const bytecode_objfmt_data *objfmt_data; - const char *filename; - unsigned long line; switch (bc->type) { case BC_EMPTY: @@ -414,9 +417,7 @@ bc_print(FILE *f, int indent_level, const bytecode *bc) else expr_print(f, bc->multiple); fprintf(f, "\n%*sLength=%lu\n", indent_level, "", bc->len); - line_lookup(bc->line, &filename, &line); - fprintf(f, "%*sFilename=\"%s\" Line Number=%lu\n", indent_level, "", - filename, line); + fprintf(f, "%*sLine Index=%lu\n", indent_level, "", bc->line); fprintf(f, "%*sOffset=%lx\n", indent_level, "", bc->offset); } @@ -499,11 +500,10 @@ bc_resolve_reserve(bytecode_reserve *reserve, unsigned long *len, int save, * the circular reference error to filter through. */ if (temp && expr_contains(temp, EXPR_FLOAT)) - ErrorAt(line, - _("expression must not contain floating point value")); + Error(line, _("expression must not contain floating point value")); else - ErrorAt(line, - _("attempt to reserve non-constant quantity of space")); + Error(line, + _("attempt to reserve non-constant quantity of space")); retval = BC_RESOLVE_ERROR | BC_RESOLVE_UNKNOWN_LEN; } else *len += intnum_get_uint(num)*reserve->itemsize; @@ -565,13 +565,12 @@ bc_resolve_incbin(bytecode_incbin *incbin, unsigned long *len, int save, /* Open file and determine its length */ f = fopen(incbin->filename, "rb"); if (!f) { - ErrorAt(line, _("`incbin': unable to open file `%s'"), - incbin->filename); + Error(line, _("`incbin': unable to open file `%s'"), incbin->filename); return BC_RESOLVE_ERROR | BC_RESOLVE_UNKNOWN_LEN; } if (fseek(f, 0L, SEEK_END) < 0) { - ErrorAt(line, _("`incbin': unable to seek on file `%s'"), - incbin->filename); + Error(line, _("`incbin': unable to seek on file `%s'"), + incbin->filename); return BC_RESOLVE_ERROR | BC_RESOLVE_UNKNOWN_LEN; } flen = (unsigned long)ftell(f); @@ -579,8 +578,8 @@ bc_resolve_incbin(bytecode_incbin *incbin, unsigned long *len, int save, /* Compute length of incbin from start, maxlen, and len */ if (start > flen) { - WarningAt(line, _("`incbin': start past end of file `%s'"), - incbin->filename); + Warning(line, _("`incbin': start past end of file `%s'"), + incbin->filename); start = flen; } flen -= start; @@ -645,8 +644,8 @@ bc_resolve(bytecode *bc, int save, const section *sect, if (!num) { retval = BC_RESOLVE_UNKNOWN_LEN; if (temp && expr_contains(temp, EXPR_FLOAT)) { - ErrorAt(bc->line, - _("expression must not contain floating point value")); + Error(bc->line, + _("expression must not contain floating point value")); retval |= BC_RESOLVE_ERROR; } } else @@ -719,23 +718,22 @@ bc_tobytes_incbin(bytecode_incbin *incbin, unsigned char **bufp, /* Open file */ f = fopen(incbin->filename, "rb"); if (!f) { - ErrorAt(line, _("`incbin': unable to open file `%s'"), - incbin->filename); + Error(line, _("`incbin': unable to open file `%s'"), incbin->filename); return 1; } /* Seek to start of data */ if (fseek(f, (long)start, SEEK_SET) < 0) { - ErrorAt(line, _("`incbin': unable to seek on file `%s'"), - incbin->filename); + Error(line, _("`incbin': unable to seek on file `%s'"), + incbin->filename); fclose(f); return 1; } /* Read len bytes */ if (fread(*bufp, (size_t)len, 1, f) < (size_t)len) { - ErrorAt(line, _("`incbin': unable to read %lu bytes from file `%s'"), - len, incbin->filename); + Error(line, _("`incbin': unable to read %lu bytes from file `%s'"), + len, incbin->filename); fclose(f); return 1; } diff --git a/src/bytecode.h b/src/bytecode.h index 6ad5bb38..f2938ef4 100644 --- a/src/bytecode.h +++ b/src/bytecode.h @@ -42,7 +42,7 @@ typedef enum { void bc_initialize(arch *a); -/*@only@*/ immval *imm_new_int(unsigned long int_val); +/*@only@*/ immval *imm_new_int(unsigned long int_val, unsigned long lindex); /*@only@*/ immval *imm_new_expr(/*@keep@*/ expr *e); /*@observer@*/ const expr *ea_get_disp(const effaddr *ea); @@ -53,16 +53,22 @@ void ea_print(FILE *f, int indent_level, const effaddr *ea); void bc_set_multiple(bytecode *bc, /*@keep@*/ expr *e); -/*@only@*/ bytecode *bc_new_common(bytecode_type type, size_t datasize); -/*@only@*/ bytecode *bc_new_data(datavalhead *datahead, unsigned char size); +/*@only@*/ bytecode *bc_new_common(bytecode_type type, size_t datasize, + unsigned long lindex); +/*@only@*/ bytecode *bc_new_data(datavalhead *datahead, unsigned char size, + unsigned long lindex); /*@only@*/ bytecode *bc_new_reserve(/*@only@*/ expr *numitems, - unsigned char itemsize); + unsigned char itemsize, + unsigned long lindex); /*@only@*/ bytecode *bc_new_incbin(/*@only@*/ char *filename, /*@only@*/ /*@null@*/ expr *start, - /*@only@*/ /*@null@*/ expr *maxlen); -/*@only@*/ bytecode *bc_new_align(unsigned long boundary); + /*@only@*/ /*@null@*/ expr *maxlen, + unsigned long lindex); +/*@only@*/ bytecode *bc_new_align(unsigned long boundary, + unsigned long lindex); /*@only@*/ bytecode *bc_new_objfmt_data(unsigned int type, unsigned long len, - objfmt *of, /*@only@*/ void *data); + objfmt *of, /*@only@*/ void *data, + unsigned long lindex); void bc_delete(/*@only@*/ /*@null@*/ bytecode *bc); diff --git a/src/coretype.h b/src/coretype.h index 29f2709d..d1585fa9 100644 --- a/src/coretype.h +++ b/src/coretype.h @@ -42,6 +42,8 @@ typedef struct expr expr; typedef struct intnum intnum; typedef struct floatnum floatnum; +typedef struct linemgr linemgr; + typedef enum { EXPR_ADD, EXPR_SUB, diff --git a/src/errwarn.c b/src/errwarn.c index 47d72b66..c74d5337 100644 --- a/src/errwarn.c +++ b/src/errwarn.c @@ -28,7 +28,7 @@ # include #endif -#include "globals.h" +#include "linemgr.h" #include "errwarn.h" @@ -235,29 +235,17 @@ warning_common(unsigned long lindex, const char *fmt, va_list va) * system. */ void -ParserError(const char *s) +ParserError(unsigned long lindex, const char *s) { - Error("%s %s", _("parser error:"), s); + Error(lindex, "%s %s", _("parser error:"), s); previous_we->type = WE_PARSERERROR; } -/* Register an error during the parser stage (uses global line_index). Does - * not print the error, only stores it for OutputAllErrorWarning() to print. - */ -void -Error(const char *fmt, ...) -{ - va_list va; - va_start(va, fmt); - error_common(line_index, fmt, va); - va_end(va); -} - /* Register an error at line lindex. Does not print the error, only stores it * for OutputAllErrorWarning() to print. */ void -ErrorAt(unsigned long lindex, const char *fmt, ...) +Error(unsigned long lindex, const char *fmt, ...) { va_list va; va_start(va, fmt); @@ -265,23 +253,11 @@ ErrorAt(unsigned long lindex, const char *fmt, ...) va_end(va); } -/* Register a warning during the parser stage (uses global line_index). Does - * not print the warning, only stores it for OutputAllErrorWarning() to print. - */ -void -Warning(const char *fmt, ...) -{ - va_list va; - va_start(va, fmt); - warning_common(line_index, fmt, va); - va_end(va); -} - /* Register an warning at line lindex. Does not print the warning, only stores * it for OutputAllErrorWarning() to print. */ void -WarningAt(unsigned long lindex, const char *fmt, ...) +Warning(unsigned long lindex, const char *fmt, ...) { va_list va; va_start(va, fmt); @@ -329,7 +305,7 @@ GetNumErrors(void) /* Output all previously stored errors and warnings to stderr. */ void -OutputAllErrorWarning(void) +OutputAllErrorWarning(linemgr *lm) { errwarn *we; const char *filename; @@ -345,7 +321,7 @@ OutputAllErrorWarning(void) while (!SLIST_EMPTY(&errwarns)) { we = SLIST_FIRST(&errwarns); /* Output error/warning */ - line_lookup(we->line, &filename, &line); + lm->lookup(we->line, &filename, &line); if (we->type == WE_ERROR) fprintf(stderr, "%s:%lu: %s\n", filename, line, we->msg); else diff --git a/src/errwarn.h b/src/errwarn.h index bc95e328..75ee91a3 100644 --- a/src/errwarn.h +++ b/src/errwarn.h @@ -60,21 +60,16 @@ typedef enum { /*@shared@*/ char *conv_unprint(char ch); -void ParserError(const char *); +void ParserError(unsigned long lindex, const char *); /*@exits@*/ void InternalError_(const char *file, unsigned int line, const char *message); #define InternalError(msg) InternalError_(__FILE__, __LINE__, msg) /*@exits@*/ void Fatal(fatal_num); -void Error(const char *, ...) /*@printflike@*/; -void Warning(const char *, ...) /*@printflike@*/; -/* Use Error() and Warning() instead of ErrorAt() and WarningAt() when being - * called in line order from a parser. - */ -void ErrorAt(unsigned long lindex, const char *, ...) /*@printflike@*/; -void WarningAt(unsigned long lindex, const char *, ...) /*@printflike@*/; +void Error(unsigned long lindex, const char *, ...) /*@printflike@*/; +void Warning(unsigned long lindex, const char *, ...) /*@printflike@*/; /* These two functions immediately output the error or warning, with no file * or line information. They should be used for errors and warnings outside @@ -87,6 +82,6 @@ void WarningNow(const char *, ...) /*@printflike@*/; unsigned int GetNumErrors(void); /* Outputs all errors/warnings to standard error. */ -void OutputAllErrorWarning(void); +void OutputAllErrorWarning(linemgr *lm); #endif diff --git a/src/expr.c b/src/expr.c index c3458698..a56762aa 100644 --- a/src/expr.c +++ b/src/expr.c @@ -24,7 +24,6 @@ #include "bitvect.h" -#include "globals.h" #include "errwarn.h" #include "intnum.h" #include "floatnum.h" @@ -56,7 +55,7 @@ expr_initialize(arch *a) * If it's a unary operator, put the element in left and set right=NULL. */ /*@-compmempass@*/ expr * -expr_new(ExprOp op, ExprItem *left, ExprItem *right) +expr_new(ExprOp op, ExprItem *left, ExprItem *right, unsigned long lindex) { expr *ptr, *sube; ptr = xmalloc(sizeof(expr)); @@ -103,7 +102,7 @@ expr_new(ExprOp op, ExprItem *left, ExprItem *right) } } - ptr->line = line_index; + ptr->line = lindex; return ptr; } @@ -177,11 +176,11 @@ expr_xform_bc_dist(/*@returned@*/ /*@only@*/ expr *e, symrec_get_label(e->terms[i].data.sym, §, &precbc) && section_is_absolute(sect) && (dist = calc_bc_dist(sect, NULL, precbc))) { + const expr *start = section_get_start(sect); e->terms[i].type = EXPR_EXPR; e->terms[i].data.expn = - expr_new(EXPR_ADD, - ExprExpr(expr_copy(section_get_start(sect))), - ExprInt(dist)); + expr_new(EXPR_ADD, ExprExpr(expr_copy(start)), ExprInt(dist), + start->line); } } @@ -316,7 +315,7 @@ expr_xform_neg_helper(/*@returned@*/ /*@only@*/ expr *e) * floatnums present below; if there ARE floatnums, recurse. */ if (e->terms[0].type == EXPR_FLOAT) - floatnum_calc(e->terms[0].data.flt, EXPR_NEG, NULL); + floatnum_calc(e->terms[0].data.flt, EXPR_NEG, NULL, e->line); else if (e->terms[0].type == EXPR_EXPR && expr_contains(e->terms[0].data.expn, EXPR_FLOAT)) expr_xform_neg_helper(e->terms[0].data.expn); @@ -701,7 +700,7 @@ expr_level_tree(expr *e, int fold_const, int simplify_ident, /* Check for circular reference */ SLIST_FOREACH(np, eh, next) { if (np->e == equ_expr) { - ErrorAt(e->line, _("circular reference detected.")); + Error(e->line, _("circular reference detected.")); return e; } } diff --git a/src/expr.h b/src/expr.h index 06680bda..36e53972 100644 --- a/src/expr.h +++ b/src/expr.h @@ -27,7 +27,8 @@ typedef struct ExprItem ExprItem; void expr_initialize(arch *a); /*@only@*/ expr *expr_new(ExprOp, /*@only@*/ ExprItem *, - /*@only@*/ /*@null@*/ ExprItem *); + /*@only@*/ /*@null@*/ ExprItem *, + unsigned long lindex); /*@only@*/ ExprItem *ExprSym(/*@keep@*/ symrec *); /*@only@*/ ExprItem *ExprExpr(/*@keep@*/ expr *); @@ -35,12 +36,12 @@ void expr_initialize(arch *a); /*@only@*/ ExprItem *ExprFloat(/*@keep@*/ floatnum *); /*@only@*/ ExprItem *ExprReg(unsigned long reg); -#define expr_new_tree(l,o,r) \ - expr_new ((o), ExprExpr(l), ExprExpr(r)) -#define expr_new_branch(o,r) \ - expr_new ((o), ExprExpr(r), (ExprItem *)NULL) -#define expr_new_ident(r) \ - expr_new (EXPR_IDENT, (r), (ExprItem *)NULL) +#define expr_new_tree(l,o,r,i) \ + expr_new ((o), ExprExpr(l), ExprExpr(r), i) +#define expr_new_branch(o,r,i) \ + expr_new ((o), ExprExpr(r), (ExprItem *)NULL, i) +#define expr_new_ident(r,i) \ + expr_new (EXPR_IDENT, (r), (ExprItem *)NULL, i) /* allocates and makes an exact duplicate of e */ expr *expr_copy(const expr *e); diff --git a/src/floatnum.c b/src/floatnum.c index 6f98dc26..f7b4fe60 100644 --- a/src/floatnum.c +++ b/src/floatnum.c @@ -511,10 +511,11 @@ floatnum_delete(floatnum *flt) } void -floatnum_calc(floatnum *acc, ExprOp op, /*@unused@*/ floatnum *operand) +floatnum_calc(floatnum *acc, ExprOp op, /*@unused@*/ floatnum *operand, + unsigned long lindex) { if (op != EXPR_NEG) - Error(_("Unsupported floating-point arithmetic operation")); + Error(lindex, _("Unsupported floating-point arithmetic operation")); else acc->sign ^= 1; } diff --git a/src/floatnum.h b/src/floatnum.h index c699e317..a5d05bba 100644 --- a/src/floatnum.h +++ b/src/floatnum.h @@ -32,7 +32,8 @@ void floatnum_shutdown(void); void floatnum_delete(/*@only@*/ floatnum *flt); /* calculation function: acc = acc op operand */ -void floatnum_calc(floatnum *acc, ExprOp op, floatnum *operand); +void floatnum_calc(floatnum *acc, ExprOp op, floatnum *operand, + unsigned long lindex); /* The get functions return nonzero if flt can't fit into that size format: * -1 if underflow occurred, 1 if overflow occurred. diff --git a/src/globals.c b/src/globals.c deleted file mode 100644 index 61763af5..00000000 --- a/src/globals.c +++ /dev/null @@ -1,146 +0,0 @@ -/* - * Global variables - * - * Copyright (C) 2001 Peter Johnson - * - * This file is part of YASM. - * - * YASM is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * YASM is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ -#include "util.h" -/*@unused@*/ RCSID("$IdPath$"); - -#include "hamt.h" - -#include "globals.h" - - -/* Source lines tracking */ -typedef struct { - struct line_index_mapping *vector; - unsigned long size; - unsigned long allocated; -} line_index_mapping_head; - -typedef struct line_index_mapping { - /* monotonically increasing line index */ - unsigned long index; - - /* related info */ - /* "original" source filename */ - /*@null@*/ /*@dependent@*/ const char *filename; - /* "original" source base line number */ - unsigned long line; - /* "original" source line number increment (for following lines) */ - unsigned long line_inc; -} line_index_mapping; - -/* Shared storage for filenames */ -static /*@only@*/ /*@null@*/ HAMT *filename_table = NULL; - -/* Virtual line number. Uniquely specifies every line read by the parser. */ -unsigned long line_index = 1; -static /*@only@*/ /*@null@*/ line_index_mapping_head *line_index_map = NULL; - -/* Global assembler options. */ -unsigned int asm_options = 0; - -static void -filename_delete_one(/*@only@*/ void *d) -{ - xfree(d); -} - -void -line_set(const char *filename, unsigned long line, unsigned long line_inc) -{ - char *copy; - int replace = 0; - line_index_mapping *mapping; - - /* Create a new mapping in the map */ - if (!line_index_map) { - /* initialize vector */ - line_index_map = xmalloc(sizeof(line_index_mapping_head)); - line_index_map->vector = xmalloc(8*sizeof(line_index_mapping)); - line_index_map->size = 0; - line_index_map->allocated = 8; - } - if (line_index_map->size >= line_index_map->allocated) { - /* allocate another size bins when full for 2x space */ - line_index_map->vector = xrealloc(line_index_map->vector, - 2*line_index_map->allocated*sizeof(line_index_mapping)); - line_index_map->allocated *= 2; - } - mapping = &line_index_map->vector[line_index_map->size]; - line_index_map->size++; - - /* Fill it */ - - /* Copy the filename (via shared storage) */ - copy = xstrdup(filename); - if (!filename_table) - filename_table = HAMT_new(); - /*@-aliasunique@*/ - mapping->filename = HAMT_insert(filename_table, copy, copy, &replace, - filename_delete_one); - /*@=aliasunique@*/ - - mapping->index = line_index; - mapping->line = line; - mapping->line_inc = line_inc; -} - -void -line_shutdown(void) -{ - if (line_index_map) { - xfree(line_index_map->vector); - xfree(line_index_map); - line_index_map = NULL; - } - - if (filename_table) { - HAMT_delete(filename_table, filename_delete_one); - filename_table = NULL; - } -} - -void -line_lookup(unsigned long lindex, const char **filename, unsigned long *line) -{ - line_index_mapping *mapping; - unsigned long vindex, step; - - assert(lindex <= line_index); - - /* Binary search through map to find highest line_index <= index */ - assert(line_index_map != NULL); - vindex = 0; - /* start step as the greatest power of 2 <= size */ - step = 1; - while (step*2<=line_index_map->size) - step*=2; - while (step>0) { - if (vindex+step < line_index_map->size - && line_index_map->vector[vindex+step].index <= lindex) - vindex += step; - step /= 2; - } - mapping = &line_index_map->vector[vindex]; - - *filename = mapping->filename; - *line = mapping->line+mapping->line_inc*(lindex-mapping->index); -} diff --git a/src/globals.h b/src/globals.h deleted file mode 100644 index a38ed352..00000000 --- a/src/globals.h +++ /dev/null @@ -1,37 +0,0 @@ -/* $IdPath$ - * Globals header file - * - * Copyright (C) 2001 Peter Johnson - * - * This file is part of YASM. - * - * YASM is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * YASM is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ -#ifndef YASM_GLOBALS_H -#define YASM_GLOBALS_H - -/* Virtual line number. Uniquely specifies every line read by the parser. */ -extern unsigned long line_index; - -/* Global assembler options. */ -extern unsigned int asm_options; - -void line_set(const char *filename, unsigned long line, - unsigned long line_inc); -void line_shutdown(void); -void line_lookup(unsigned long lindex, /*@out@*/ const char **filename, - /*@out@*/ unsigned long *line); - -#endif diff --git a/src/intnum.c b/src/intnum.c index b066c712..3ca4775d 100644 --- a/src/intnum.c +++ b/src/intnum.c @@ -56,7 +56,7 @@ intnum_shutdown(void) } intnum * -intnum_new_dec(char *str) +intnum_new_dec(char *str, unsigned long lindex) { intnum *intn = xmalloc(sizeof(intnum)); @@ -68,7 +68,7 @@ intnum_new_dec(char *str) } if (BitVector_from_Dec_static(conv_bv, (unsigned char *)str) == ErrCode_Ovfl) - Warning(_("Numeric constant too large for internal format")); + Warning(lindex, _("Numeric constant too large for internal format")); if (Set_Max(conv_bv) < 32) { intn->type = INTNUM_UL; intn->val.ul = BitVector_Chunk_Read(conv_bv, 32, 0); @@ -81,14 +81,14 @@ intnum_new_dec(char *str) } intnum * -intnum_new_bin(char *str) +intnum_new_bin(char *str, unsigned long lindex) { intnum *intn = xmalloc(sizeof(intnum)); intn->origsize = (unsigned char)strlen(str); if(intn->origsize > BITVECT_ALLOC_SIZE) - Warning(_("Numeric constant too large for internal format")); + Warning(lindex, _("Numeric constant too large for internal format")); if (!conv_bv) { conv_bv = BitVector_Create(BITVECT_ALLOC_SIZE, FALSE); @@ -107,14 +107,14 @@ intnum_new_bin(char *str) } intnum * -intnum_new_oct(char *str) +intnum_new_oct(char *str, unsigned long lindex) { intnum *intn = xmalloc(sizeof(intnum)); intn->origsize = strlen(str)*3; if(intn->origsize > BITVECT_ALLOC_SIZE) - Warning(_("Numeric constant too large for internal format")); + Warning(lindex, _("Numeric constant too large for internal format")); if (!conv_bv) { conv_bv = BitVector_Create(BITVECT_ALLOC_SIZE, FALSE); @@ -133,14 +133,14 @@ intnum_new_oct(char *str) } intnum * -intnum_new_hex(char *str) +intnum_new_hex(char *str, unsigned long lindex) { intnum *intn = xmalloc(sizeof(intnum)); intn->origsize = strlen(str)*4; if(intn->origsize > BITVECT_ALLOC_SIZE) - Warning(_("Numeric constant too large for internal format")); + Warning(lindex, _("Numeric constant too large for internal format")); if (!conv_bv) { conv_bv = BitVector_Create(BITVECT_ALLOC_SIZE, FALSE); @@ -160,13 +160,14 @@ intnum_new_hex(char *str) /*@-usedef -compdef -uniondef@*/ intnum * -intnum_new_charconst_nasm(const char *str) +intnum_new_charconst_nasm(const char *str, unsigned long lindex) { intnum *intn = xmalloc(sizeof(intnum)); size_t len = strlen(str); if (len > 4) - Warning(_("character constant too large, ignoring trailing characters")); + Warning(lindex, + _("character constant too large, ignoring trailing characters")); intn->val.ul = 0; intn->type = INTNUM_UL; diff --git a/src/intnum.h b/src/intnum.h index 0352b441..99f1a105 100644 --- a/src/intnum.h +++ b/src/intnum.h @@ -25,12 +25,13 @@ /* Clean up internal allocations */ void intnum_shutdown(void); -/*@only@*/ intnum *intnum_new_dec(char *str); -/*@only@*/ intnum *intnum_new_bin(char *str); -/*@only@*/ intnum *intnum_new_oct(char *str); -/*@only@*/ intnum *intnum_new_hex(char *str); +/*@only@*/ intnum *intnum_new_dec(char *str, unsigned long lindex); +/*@only@*/ intnum *intnum_new_bin(char *str, unsigned long lindex); +/*@only@*/ intnum *intnum_new_oct(char *str, unsigned long lindex); +/*@only@*/ intnum *intnum_new_hex(char *str, unsigned long lindex); /* convert character constant to integer value, using NASM rules */ -/*@only@*/ intnum *intnum_new_charconst_nasm(const char *str); +/*@only@*/ intnum *intnum_new_charconst_nasm(const char *str, + unsigned long lindex); /*@only@*/ intnum *intnum_new_uint(unsigned long i); /*@only@*/ intnum *intnum_new_int(long i); /*@only@*/ intnum *intnum_copy(const intnum *intn); diff --git a/src/linemgr.c b/src/linemgr.c index 61763af5..718b01b9 100644 --- a/src/linemgr.c +++ b/src/linemgr.c @@ -1,7 +1,7 @@ /* - * Global variables + * YASM assembler line manager (for parse stage) * - * Copyright (C) 2001 Peter Johnson + * Copyright (C) 2002 Peter Johnson * * This file is part of YASM. * @@ -24,7 +24,7 @@ #include "hamt.h" -#include "globals.h" +#include "linemgr.h" /* Source lines tracking */ @@ -48,14 +48,11 @@ typedef struct line_index_mapping { } line_index_mapping; /* Shared storage for filenames */ -static /*@only@*/ /*@null@*/ HAMT *filename_table = NULL; +static /*@only@*/ /*@null@*/ HAMT *filename_table; /* Virtual line number. Uniquely specifies every line read by the parser. */ -unsigned long line_index = 1; -static /*@only@*/ /*@null@*/ line_index_mapping_head *line_index_map = NULL; - -/* Global assembler options. */ -unsigned int asm_options = 0; +static unsigned long line_index; +static /*@only@*/ /*@null@*/ line_index_mapping_head *line_index_map; static void filename_delete_one(/*@only@*/ void *d) @@ -63,21 +60,15 @@ filename_delete_one(/*@only@*/ void *d) xfree(d); } -void -line_set(const char *filename, unsigned long line, unsigned long line_inc) +static void +yasm_linemgr_set(const char *filename, unsigned long line, + unsigned long line_inc) { char *copy; int replace = 0; line_index_mapping *mapping; /* Create a new mapping in the map */ - if (!line_index_map) { - /* initialize vector */ - line_index_map = xmalloc(sizeof(line_index_mapping_head)); - line_index_map->vector = xmalloc(8*sizeof(line_index_mapping)); - line_index_map->size = 0; - line_index_map->allocated = 8; - } if (line_index_map->size >= line_index_map->allocated) { /* allocate another size bins when full for 2x space */ line_index_map->vector = xrealloc(line_index_map->vector, @@ -91,8 +82,6 @@ line_set(const char *filename, unsigned long line, unsigned long line_inc) /* Copy the filename (via shared storage) */ copy = xstrdup(filename); - if (!filename_table) - filename_table = HAMT_new(); /*@-aliasunique@*/ mapping->filename = HAMT_insert(filename_table, copy, copy, &replace, filename_delete_one); @@ -103,8 +92,22 @@ line_set(const char *filename, unsigned long line, unsigned long line_inc) mapping->line_inc = line_inc; } -void -line_shutdown(void) +static void +yasm_linemgr_initialize(void) +{ + filename_table = HAMT_new(); + + line_index = 1; + + /* initialize mapping vector */ + line_index_map = xmalloc(sizeof(line_index_mapping_head)); + line_index_map->vector = xmalloc(8*sizeof(line_index_mapping)); + line_index_map->size = 0; + line_index_map->allocated = 8; +} + +static void +yasm_linemgr_cleanup(void) { if (line_index_map) { xfree(line_index_map->vector); @@ -118,8 +121,21 @@ line_shutdown(void) } } -void -line_lookup(unsigned long lindex, const char **filename, unsigned long *line) +static unsigned long +yasm_linemgr_get_current(void) +{ + return line_index; +} + +static unsigned long +yasm_linemgr_goto_next(void) +{ + return ++line_index; +} + +static void +yasm_linemgr_lookup(unsigned long lindex, const char **filename, + unsigned long *line) { line_index_mapping *mapping; unsigned long vindex, step; @@ -144,3 +160,12 @@ line_lookup(unsigned long lindex, const char **filename, unsigned long *line) *filename = mapping->filename; *line = mapping->line+mapping->line_inc*(lindex-mapping->index); } + +linemgr yasm_linemgr = { + yasm_linemgr_initialize, + yasm_linemgr_cleanup, + yasm_linemgr_get_current, + yasm_linemgr_goto_next, + yasm_linemgr_set, + yasm_linemgr_lookup +}; diff --git a/src/linemgr.h b/src/linemgr.h index a38ed352..85d474e0 100644 --- a/src/linemgr.h +++ b/src/linemgr.h @@ -1,7 +1,7 @@ /* $IdPath$ - * Globals header file + * Line manager (for parse stage) header file * - * Copyright (C) 2001 Peter Johnson + * Copyright (C) 2002 Peter Johnson * * This file is part of YASM. * @@ -19,19 +19,34 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#ifndef YASM_GLOBALS_H -#define YASM_GLOBALS_H +#ifndef YASM_LINEMGR_H +#define YASM_LINEMGR_H -/* Virtual line number. Uniquely specifies every line read by the parser. */ -extern unsigned long line_index; +struct linemgr { + /* Initialize cur_lindex and any manager internal data structures. */ + void (*initialize) (void); -/* Global assembler options. */ -extern unsigned int asm_options; + /* Cleans up any memory allocated by initialize. */ + void (*cleanup) (void); -void line_set(const char *filename, unsigned long line, - unsigned long line_inc); -void line_shutdown(void); -void line_lookup(unsigned long lindex, /*@out@*/ const char **filename, - /*@out@*/ unsigned long *line); + /* Returns the current line index. */ + unsigned long (*get_current) (void); + + /* Goes to the next line (increments the current line index), returns + * the current (new) line index. + */ + unsigned long (*goto_next) (void); + + /* Sets a new file/line association starting point at the current line + * index. line_inc indicates how much the "real" line is incremented by + * for each line index increment (0 is perfectly legal). + */ + void (*set) (const char *filename, unsigned long line, + unsigned long line_inc); + + /* Look up the associated actual file and line for a line index. */ + void (*lookup) (unsigned long lindex, /*@out@*/ const char **filename, + /*@out@*/ unsigned long *line); +}; #endif diff --git a/src/main.c b/src/main.c index dc240b68..1c7d7e17 100644 --- a/src/main.c +++ b/src/main.c @@ -28,8 +28,8 @@ #include "bitvect.h" #include "file.h" -#include "globals.h" #include "options.h" +#include "linemgr.h" #include "errwarn.h" #include "intnum.h" #include "floatnum.h" @@ -47,6 +47,9 @@ #include "arch.h" +/* YASM's line manager (for parse stage). */ +extern linemgr yasm_linemgr; + /* Extra path to search for our modules. */ #ifndef YASM_MODULE_PATH_ENV # define YASM_MODULE_PATH_ENV "YASM_MODULE_PATH" @@ -204,8 +207,9 @@ main(int argc, char *argv[]) in_filename = xstrdup("-"); } - /* Initialize line info */ - line_set(in_filename, 1, 1); + /* Initialize line manager */ + yasm_linemgr.initialize(); + yasm_linemgr.set(in_filename, 1, 1); /* handle preproc-only case here */ if (preproc_only) { @@ -236,7 +240,8 @@ main(int argc, char *argv[]) /* Pre-process until done */ cur_preproc->initialize(in, in_filename); - while ((got = cur_preproc->input(preproc_buf, PREPROC_BUF_SIZE)) != 0) + while ((got = cur_preproc->input(preproc_buf, PREPROC_BUF_SIZE, + &yasm_linemgr)) != 0) fwrite(preproc_buf, got, 1, obj); if (in != stdin) @@ -246,7 +251,7 @@ main(int argc, char *argv[]) fclose(obj); if (GetNumErrors() > 0) { - OutputAllErrorWarning(); + OutputAllErrorWarning(&yasm_linemgr); if (obj != stdout) remove(obj_filename); xfree(preproc_buf); @@ -370,15 +375,15 @@ main(int argc, char *argv[]) } /* Parse! */ - sections = cur_parser->do_parse(cur_preproc, cur_arch, cur_objfmt, in, - in_filename); + sections = cur_parser->do_parse(cur_preproc, cur_arch, cur_objfmt, + &yasm_linemgr, in, in_filename); /* Close input file */ if (in != stdin) fclose(in); if (GetNumErrors() > 0) { - OutputAllErrorWarning(); + OutputAllErrorWarning(&yasm_linemgr); cleanup(sections); return EXIT_FAILURE; } @@ -387,7 +392,7 @@ main(int argc, char *argv[]) cur_optimizer->optimize(sections); if (GetNumErrors() > 0) { - OutputAllErrorWarning(); + OutputAllErrorWarning(&yasm_linemgr); cleanup(sections); return EXIT_FAILURE; } @@ -412,13 +417,13 @@ main(int argc, char *argv[]) * object file (to make sure it's not left newer than the source). */ if (GetNumErrors() > 0) { - OutputAllErrorWarning(); + OutputAllErrorWarning(&yasm_linemgr); remove(obj_filename); cleanup(sections); return EXIT_FAILURE; } - OutputAllErrorWarning(); + OutputAllErrorWarning(&yasm_linemgr); cleanup(sections); return EXIT_SUCCESS; @@ -448,7 +453,7 @@ cleanup(sectionhead *sections) if (sections) sections_delete(sections); symrec_delete_all(); - line_shutdown(); + yasm_linemgr.cleanup(); floatnum_shutdown(); intnum_shutdown(); diff --git a/src/module.c b/src/module.c index ba31ebd3..8b1e7410 100644 --- a/src/module.c +++ b/src/module.c @@ -25,7 +25,6 @@ #include "ltdl.h" #include "module.h" -#include "globals.h" typedef struct module { diff --git a/src/objfmt.h b/src/objfmt.h index 24f9b935..55f7b9cc 100644 --- a/src/objfmt.h +++ b/src/objfmt.h @@ -76,7 +76,8 @@ struct objfmt { */ /*@observer@*/ /*@null@*/ section * (*sections_switch)(sectionhead *headp, valparamhead *valparams, - /*@null@*/ valparamhead *objext_valparams); + /*@null@*/ valparamhead *objext_valparams, + unsigned long lindex); /* Object format-specific data handling functions for sections. * May be NULL if no data is ever allocated in sections_switch(). @@ -88,11 +89,14 @@ struct objfmt { * May be NULL if objfmt doesn't care about such declarations. */ void (*extern_declare)(symrec *sym, - /*@null@*/ valparamhead *objext_valparams); + /*@null@*/ valparamhead *objext_valparams, + unsigned long lindex); void (*global_declare)(symrec *sym, - /*@null@*/ valparamhead *objext_valparams); + /*@null@*/ valparamhead *objext_valparams, + unsigned long lindex); void (*common_declare)(symrec *sym, /*@only@*/ expr *size, - /*@null@*/ valparamhead *objext_valparams); + /*@null@*/ valparamhead *objext_valparams, + unsigned long lindex); /* May be NULL if symrec_set_of_data() is never called. */ void (*symrec_data_delete)(/*@only@*/ void *data); @@ -104,7 +108,7 @@ struct objfmt { */ int (*directive)(const char *name, valparamhead *valparams, /*@null@*/ valparamhead *objext_valparams, - sectionhead *headp); + sectionhead *headp, unsigned long lindex); /* Bytecode objfmt data (BC_OBJFMT_DATA) handling functions. * May be NULL if no BC_OBJFMT_DATA is ever allocated by the object format. diff --git a/src/objfmts/bin/bin-objfmt.c b/src/objfmts/bin/bin-objfmt.c index 2e5f0ed9..1ff0534c 100644 --- a/src/objfmts/bin/bin-objfmt.c +++ b/src/objfmts/bin/bin-objfmt.c @@ -24,7 +24,6 @@ #include "file.h" -#include "globals.h" #include "errwarn.h" #include "intnum.h" #include "floatnum.h" @@ -122,11 +121,11 @@ bin_objfmt_expr_xform(/*@returned@*/ /*@only@*/ expr *e, if (e->terms[i].type == EXPR_SYM && symrec_get_label(e->terms[i].data.sym, §, &precbc) && (dist = common_calc_bc_dist(sect, NULL, precbc))) { + const expr *start = section_get_start(sect); e->terms[i].type = EXPR_EXPR; e->terms[i].data.expn = - expr_new(EXPR_ADD, - ExprExpr(expr_copy(section_get_start(sect))), - ExprInt(dist)); + expr_new(EXPR_ADD, ExprExpr(expr_copy(start)), ExprInt(dist), + start->line); } } @@ -164,13 +163,13 @@ bin_objfmt_output_expr(expr **ep, unsigned char **bufp, unsigned long valsize, /* Check for complex float expressions */ if (expr_contains(*ep, EXPR_FLOAT)) { - ErrorAt((*ep)->line, _("floating point expression too complex")); + Error((*ep)->line, _("floating point expression too complex")); return 1; } /* Couldn't output, assume it contains an external reference. */ - ErrorAt((*ep)->line, - _("binary object format does not support external references")); + Error((*ep)->line, + _("binary object format does not support external references")); return 1; } @@ -199,8 +198,8 @@ bin_objfmt_output_bytecode(bytecode *bc, /*@null@*/ void *d) /* Warn that gaps are converted to 0 and write out the 0's. */ if (gap) { unsigned long left; - WarningAt(bc->line, - _("uninitialized space declared in code/data section: zeroing")); + Warning(bc->line, + _("uninitialized space declared in code/data section: zeroing")); /* Write out in chunks */ memset(info->buf, 0, REGULAR_OUTBUF_SIZE); left = multiple*size; @@ -268,7 +267,7 @@ bin_objfmt_output(FILE *f, sectionhead *sections) if (data) { start = bin_objfmt_align_section(data, prevsect, start, 4, prevsectlenptr, prevsectpadptr); - section_set_start(data, start); + section_set_start(data, start, 0); datastart = start; prevsect = data; prevsectlenptr = &datalen; @@ -277,7 +276,7 @@ bin_objfmt_output(FILE *f, sectionhead *sections) if (bss) { start = bin_objfmt_align_section(bss, prevsect, start, 4, prevsectlenptr, prevsectpadptr); - section_set_start(bss, start); + section_set_start(bss, start, 0); } /* Output .text first. */ @@ -315,7 +314,8 @@ bin_objfmt_cleanup(void) static /*@observer@*/ /*@null@*/ section * bin_objfmt_sections_switch(sectionhead *headp, valparamhead *valparams, /*@unused@*/ /*@null@*/ - valparamhead *objext_valparams) + valparamhead *objext_valparams, + unsigned long lindex) { valparam *vp; section *retval; @@ -340,7 +340,7 @@ bin_objfmt_sections_switch(sectionhead *headp, valparamhead *valparams, resonly = 1; } else { /* other section names not recognized. */ - Error(_("segment name `%s' not recognized"), sectname); + Error(lindex, _("segment name `%s' not recognized"), sectname); return NULL; } @@ -351,14 +351,15 @@ bin_objfmt_sections_switch(sectionhead *headp, valparamhead *valparams, unsigned long bitcnt; if (strcmp(sectname, ".text") == 0) { - Error(_("cannot specify an alignment to the `%s' section"), + Error(lindex, + _("cannot specify an alignment to the `%s' section"), sectname); return NULL; } align = expr_get_intnum(&vp->param, NULL); if (!align) { - Error(_("argument to `%s' is not a power of two"), + Error(lindex, _("argument to `%s' is not a power of two"), vp->val); return NULL; } @@ -369,7 +370,7 @@ bin_objfmt_sections_switch(sectionhead *headp, valparamhead *valparams, */ BitCount(bitcnt, alignval); if (bitcnt > 1) { - Error(_("argument to `%s' is not a power of two"), + Error(lindex, _("argument to `%s' is not a power of two"), vp->val); return NULL; } @@ -379,7 +380,7 @@ bin_objfmt_sections_switch(sectionhead *headp, valparamhead *valparams, } retval = sections_switch_general(headp, sectname, start, resonly, - &isnew); + &isnew, lindex); if (isnew) { if (have_alignval) { @@ -388,9 +389,10 @@ bin_objfmt_sections_switch(sectionhead *headp, valparamhead *valparams, section_set_of_data(retval, &yasm_bin_LTX_objfmt, data); } - symrec_define_label(sectname, retval, (bytecode *)NULL, 1); + symrec_define_label(sectname, retval, (bytecode *)NULL, 1, lindex); } else if (have_alignval) - Warning(_("alignment value ignored on section redeclaration")); + Warning(lindex, + _("alignment value ignored on section redeclaration")); return retval; } else @@ -406,16 +408,16 @@ bin_objfmt_section_data_delete(/*@only@*/ void *d) static void bin_objfmt_common_declare(/*@unused@*/ symrec *sym, /*@only@*/ expr *size, /*@unused@*/ /*@null@*/ - valparamhead *objext_valparams) + valparamhead *objext_valparams, unsigned long lindex) { expr_delete(size); - Error(_("binary object format does not support common variables")); + Error(lindex, _("binary object format does not support common variables")); } static int bin_objfmt_directive(const char *name, valparamhead *valparams, /*@unused@*/ /*@null@*/ valparamhead *objext_valparams, - sectionhead *headp) + sectionhead *headp, unsigned long lindex) { section *sect; valparam *vp; @@ -426,13 +428,13 @@ bin_objfmt_directive(const char *name, valparamhead *valparams, /* ORG takes just a simple integer as param */ vp = vps_first(valparams); if (vp->val) { - Error(_("argument to ORG should be numeric")); + Error(lindex, _("argument to ORG should be numeric")); return 0; } else if (vp->param) start = expr_get_intnum(&vp->param, NULL); if (!start) { - Error(_("argument to ORG should be numeric")); + Error(lindex, _("argument to ORG should be numeric")); return 0; } @@ -440,7 +442,7 @@ bin_objfmt_directive(const char *name, valparamhead *valparams, sect = sections_find_general(headp, ".text"); if (!sect) InternalError(_("bin objfmt: .text section does not exist before ORG is called?")); - section_set_start(sect, intnum_get_uint(start)); + section_set_start(sect, intnum_get_uint(start), lindex); return 0; /* directive recognized */ } else diff --git a/src/objfmts/coff/coff-objfmt.c b/src/objfmts/coff/coff-objfmt.c index 135d46fb..db6d3075 100644 --- a/src/objfmts/coff/coff-objfmt.c +++ b/src/objfmts/coff/coff-objfmt.c @@ -24,7 +24,6 @@ #include "file.h" -#include "globals.h" #include "errwarn.h" #include "intnum.h" #include "floatnum.h" @@ -210,7 +209,7 @@ coff_objfmt_initialize(const char *in_filename, data->index = 0; data->sclass = COFF_SCL_FILE; data->size = NULL; - filesym = symrec_define_label(".file", NULL, NULL, 0); + filesym = symrec_define_label(".file", NULL, NULL, 0, 0); symrec_set_of_data(filesym, &yasm_coff_LTX_objfmt, data); entry = xmalloc(sizeof(coff_symtab_entry)); @@ -274,7 +273,7 @@ coff_objfmt_output_expr(expr **ep, unsigned char **bufp, unsigned long valsize, SymVisibility vis; if (valsize != 4) { - ErrorAt((*ep)->line, _("coff: invalid relocation size")); + Error((*ep)->line, _("coff: invalid relocation size")); return 1; } @@ -291,7 +290,8 @@ coff_objfmt_output_expr(expr **ep, unsigned char **bufp, unsigned long valsize, csymd = symrec_get_of_data(sym); assert(csymd != NULL); *ep = expr_new(EXPR_ADD, ExprExpr(*ep), - ExprExpr(expr_copy(csymd->size))); + ExprExpr(expr_copy(csymd->size)), + csymd->size->line); *ep = expr_simplify(*ep, common_calc_bc_dist); } else if (!(vis & SYM_EXTERN)) { /* Local symbols need relocation to their section's start */ @@ -302,7 +302,8 @@ coff_objfmt_output_expr(expr **ep, unsigned char **bufp, unsigned long valsize, reloc->sym = label_csd->sym; if (COFF_SET_VMA) *ep = expr_new(EXPR_ADD, ExprExpr(*ep), - ExprInt(intnum_new_uint(label_csd->addr))); + ExprInt(intnum_new_uint(label_csd->addr)), + (*ep)->line); } } @@ -311,7 +312,8 @@ coff_objfmt_output_expr(expr **ep, unsigned char **bufp, unsigned long valsize, /* Need to reference to start of section, so add $$ in. */ *ep = expr_new(EXPR_ADD, ExprExpr(*ep), ExprSym(symrec_define_label("$$", info->sect, NULL, - 0))); + 0, (*ep)->line)), + (*ep)->line); *ep = expr_simplify(*ep, common_calc_bc_dist); } else reloc->type = COFF_RELOC_ADDR32; @@ -324,11 +326,11 @@ coff_objfmt_output_expr(expr **ep, unsigned char **bufp, unsigned long valsize, /* Check for complex float expressions */ if (expr_contains(*ep, EXPR_FLOAT)) { - ErrorAt((*ep)->line, _("floating point expression too complex")); + Error((*ep)->line, _("floating point expression too complex")); return 1; } - ErrorAt((*ep)->line, _("coff: relocation too complex")); + Error((*ep)->line, _("coff: relocation too complex")); return 1; } @@ -359,8 +361,8 @@ coff_objfmt_output_bytecode(bytecode *bc, /*@null@*/ void *d) /* Warn that gaps are converted to 0 and write out the 0's. */ if (gap) { unsigned long left; - WarningAt(bc->line, - _("uninitialized space declared in code/data section: zeroing")); + Warning(bc->line, + _("uninitialized space declared in code/data section: zeroing")); /* Write out in chunks */ memset(info->buf, 0, REGULAR_OUTBUF_SIZE); left = multiple*size; @@ -578,8 +580,8 @@ coff_objfmt_output(FILE *f, sectionhead *sections) const intnum *intn; intn = expr_get_intnum(&csymd->size, common_calc_bc_dist); if (!intn) - ErrorAt(csymd->size->line, - _("COMMON data size not an integer expression")); + Error(csymd->size->line, + _("COMMON data size not an integer expression")); else value = intnum_get_uint(intn); scnum = 0; @@ -695,7 +697,8 @@ coff_objfmt_cleanup(void) static /*@observer@*/ /*@null@*/ section * coff_objfmt_sections_switch(sectionhead *headp, valparamhead *valparams, /*@unused@*/ /*@null@*/ - valparamhead *objext_valparams) + valparamhead *objext_valparams, + unsigned long lindex) { valparam *vp = vps_first(valparams); section *retval; @@ -710,7 +713,8 @@ coff_objfmt_sections_switch(sectionhead *headp, valparamhead *valparams, sectname = vp->val; if (strlen(sectname) > 8) { - Warning(_("COFF section names limited to 8 characters: truncating")); + Warning(lindex, + _("COFF section names limited to 8 characters: truncating")); sectname[8] = '\0'; } @@ -737,7 +741,8 @@ coff_objfmt_sections_switch(sectionhead *headp, valparamhead *valparams, } } - retval = sections_switch_general(headp, sectname, 0, resonly, &isnew); + retval = sections_switch_general(headp, sectname, 0, resonly, &isnew, + lindex); if (isnew) { coff_section_data *data; @@ -754,12 +759,13 @@ coff_objfmt_sections_switch(sectionhead *headp, valparamhead *valparams, STAILQ_INIT(&data->relocs); section_set_of_data(retval, &yasm_coff_LTX_objfmt, data); - sym = symrec_define_label(sectname, retval, (bytecode *)NULL, 1); + sym = symrec_define_label(sectname, retval, (bytecode *)NULL, 1, + lindex); coff_objfmt_symtab_append(sym, COFF_SCL_STAT, NULL, 1, COFF_SYMTAB_AUX_SECT); data->sym = sym; } else if (flags_override) - Warning(_("section flags ignored on section redeclaration")); + Warning(lindex, _("section flags ignored on section redeclaration")); return retval; } @@ -826,7 +832,8 @@ coff_objfmt_section_data_print(FILE *f, int indent_level, void *data) static void coff_objfmt_extglob_declare(symrec *sym, /*@unused@*/ - /*@null@*/ valparamhead *objext_valparams) + /*@null@*/ valparamhead *objext_valparams, + /*@unused@*/ unsigned long lindex) { coff_objfmt_symtab_append(sym, COFF_SCL_EXT, NULL, 0, COFF_SYMTAB_AUX_NONE); @@ -834,7 +841,8 @@ coff_objfmt_extglob_declare(symrec *sym, /*@unused@*/ static void coff_objfmt_common_declare(symrec *sym, /*@only@*/ expr *size, /*@unused@*/ - /*@null@*/ valparamhead *objext_valparams) + /*@null@*/ valparamhead *objext_valparams, + /*@unused@*/ unsigned long lindex) { coff_objfmt_symtab_append(sym, COFF_SCL_EXT, size, 0, COFF_SYMTAB_AUX_NONE); @@ -868,7 +876,8 @@ static int coff_objfmt_directive(/*@unused@*/ const char *name, /*@unused@*/ valparamhead *valparams, /*@unused@*/ /*@null@*/ valparamhead *objext_valparams, - /*@unused@*/ sectionhead *headp) + /*@unused@*/ sectionhead *headp, + /*@unused@*/ unsigned long lindex) { return 1; /* no objfmt directives */ } diff --git a/src/objfmts/dbg/dbg-objfmt.c b/src/objfmts/dbg/dbg-objfmt.c index a50d5f49..3b10a442 100644 --- a/src/objfmts/dbg/dbg-objfmt.c +++ b/src/objfmts/dbg/dbg-objfmt.c @@ -22,7 +22,6 @@ #include "util.h" /*@unused@*/ RCSID("$IdPath$"); -#include "globals.h" #include "errwarn.h" #include "expr.h" #include "symrec.h" @@ -81,7 +80,8 @@ dbg_objfmt_cleanup(void) static /*@observer@*/ /*@null@*/ section * dbg_objfmt_sections_switch(sectionhead *headp, valparamhead *valparams, /*@unused@*/ /*@null@*/ - valparamhead *objext_valparams) + valparamhead *objext_valparams, + unsigned long lindex) { valparam *vp; section *retval; @@ -91,13 +91,14 @@ dbg_objfmt_sections_switch(sectionhead *headp, valparamhead *valparams, vps_print(dbg_objfmt_file, valparams); fprintf(dbg_objfmt_file, ", "); vps_print(dbg_objfmt_file, objext_valparams); - fprintf(dbg_objfmt_file, "), returning "); + fprintf(dbg_objfmt_file, ", %lu), returning ", lindex); if ((vp = vps_first(valparams)) && !vp->param && vp->val != NULL) { - retval = sections_switch_general(headp, vp->val, 200, 0, &isnew); + retval = sections_switch_general(headp, vp->val, 200, 0, &isnew, + lindex); if (isnew) { fprintf(dbg_objfmt_file, "(new) "); - symrec_define_label(vp->val, retval, (bytecode *)NULL, 1); + symrec_define_label(vp->val, retval, (bytecode *)NULL, 1, lindex); } fprintf(dbg_objfmt_file, "\"%s\" section\n", vp->val); return retval; @@ -125,34 +126,35 @@ dbg_objfmt_section_data_print(FILE *f, int indent_level, /*@null@*/ void *data) static void dbg_objfmt_extern_declare(symrec *sym, /*@unused@*/ /*@null@*/ - valparamhead *objext_valparams) + valparamhead *objext_valparams, unsigned long lindex) { fprintf(dbg_objfmt_file, "extern_declare(\"%s\", ", symrec_get_name(sym)); vps_print(dbg_objfmt_file, objext_valparams); - fprintf(dbg_objfmt_file, "), setting of_data=NULL\n"); + fprintf(dbg_objfmt_file, ", %lu), setting of_data=NULL\n", lindex); symrec_set_of_data(sym, &yasm_dbg_LTX_objfmt, NULL); } static void dbg_objfmt_global_declare(symrec *sym, /*@unused@*/ /*@null@*/ - valparamhead *objext_valparams) + valparamhead *objext_valparams, unsigned long lindex) { fprintf(dbg_objfmt_file, "global_declare(\"%s\", ", symrec_get_name(sym)); vps_print(dbg_objfmt_file, objext_valparams); - fprintf(dbg_objfmt_file, "), setting of_data=NULL\n"); + fprintf(dbg_objfmt_file, ", %lu), setting of_data=NULL\n", lindex); symrec_set_of_data(sym, &yasm_dbg_LTX_objfmt, NULL); } static void dbg_objfmt_common_declare(symrec *sym, /*@only@*/ expr *size, /*@unused@*/ - /*@null@*/ valparamhead *objext_valparams) + /*@null@*/ valparamhead *objext_valparams, + unsigned long lindex) { assert(dbg_objfmt_file != NULL); fprintf(dbg_objfmt_file, "common_declare(\"%s\", ", symrec_get_name(sym)); expr_print(dbg_objfmt_file, size); fprintf(dbg_objfmt_file, ", "); vps_print(dbg_objfmt_file, objext_valparams); - fprintf(dbg_objfmt_file, "), setting of_data="); + fprintf(dbg_objfmt_file, ", %lu), setting of_data=", lindex); expr_print(dbg_objfmt_file, size); symrec_set_of_data(sym, &yasm_dbg_LTX_objfmt, size); fprintf(dbg_objfmt_file, "\n"); @@ -184,13 +186,13 @@ dbg_objfmt_symrec_data_print(FILE *f, int indent_level, /*@null@*/ void *data) static int dbg_objfmt_directive(const char *name, valparamhead *valparams, /*@null@*/ valparamhead *objext_valparams, - /*@unused@*/ sectionhead *headp) + /*@unused@*/ sectionhead *headp, unsigned long lindex) { fprintf(dbg_objfmt_file, "directive(\"%s\", ", name); vps_print(dbg_objfmt_file, valparams); fprintf(dbg_objfmt_file, ", "); vps_print(dbg_objfmt_file, objext_valparams); - fprintf(dbg_objfmt_file, "), returning 0 (recognized)\n"); + fprintf(dbg_objfmt_file, ", %lu), returning 0 (recognized)\n", lindex); return 0; /* dbg format "recognizes" all directives */ } diff --git a/src/optimizers/basic/basic-optimizer.c b/src/optimizers/basic/basic-optimizer.c index cc6a903c..5c654b7d 100644 --- a/src/optimizers/basic/basic-optimizer.c +++ b/src/optimizers/basic/basic-optimizer.c @@ -129,7 +129,7 @@ basic_optimize_bytecode_1(/*@observer@*/ bytecode *bc, void *d) bcr_retval = bc_resolve(bc, 0, data->sect, basic_optimize_calc_bc_dist_1); if (bcr_retval & BC_RESOLVE_UNKNOWN_LEN) { if (!(bcr_retval & BC_RESOLVE_ERROR)) - ErrorAt(bc->line, _("circular reference detected.")); + Error(bc->line, _("circular reference detected.")); data->saw_unknown = -1; return 0; } diff --git a/src/parser.h b/src/parser.h index 4c934bec..01b0db32 100644 --- a/src/parser.h +++ b/src/parser.h @@ -50,8 +50,8 @@ struct parser { * This function returns the starting section of a linked list of sections * (whatever was in the file). */ - sectionhead *(*do_parse) (preproc *pp, arch *a, objfmt *of, FILE *f, - const char *in_filename); + sectionhead *(*do_parse) (preproc *pp, arch *a, objfmt *of, linemgr *lm, + FILE *f, const char *in_filename); }; /* Generic functions for all parsers - implemented in src/parser.c */ diff --git a/src/parsers/nasm/nasm-bison.y b/src/parsers/nasm/nasm-bison.y index e7bcd827..996ef574 100644 --- a/src/parsers/nasm/nasm-bison.y +++ b/src/parsers/nasm/nasm-bison.y @@ -29,7 +29,7 @@ RCSID("$IdPath$"); #include "bitvect.h" -#include "globals.h" +#include "linemgr.h" #include "errwarn.h" #include "intnum.h" #include "floatnum.h" @@ -60,6 +60,13 @@ extern char *nasm_parser_locallabel_base; extern size_t nasm_parser_locallabel_base_len; extern /*@dependent@*/ arch *nasm_parser_arch; extern /*@dependent@*/ objfmt *nasm_parser_objfmt; +extern /*@dependent@*/ linemgr *nasm_parser_linemgr; + +#define p_line_index (nasm_parser_linemgr->get_current()) + +#define p_expr_new_tree(l,o,r) expr_new_tree(l,o,r,p_line_index) +#define p_expr_new_branch(o,r) expr_new_branch(o,r,p_line_index) +#define p_expr_new_ident(r) expr_new_ident(r,p_line_index) static /*@null@*/ bytecode *nasm_parser_prev_bc = (bytecode *)NULL; static bytecode *nasm_parser_temp_bc; @@ -134,7 +141,7 @@ input: /* empty */ $2); if (nasm_parser_temp_bc) nasm_parser_prev_bc = nasm_parser_temp_bc; - line_index++; + nasm_parser_linemgr->goto_next(); } ; @@ -144,8 +151,8 @@ line: '\n' { $$ = (bytecode *)NULL; } /* %line indicates the line number of the *next* line, so subtract out * the increment when setting the line number. */ - line_set($5, intnum_get_uint($2)-intnum_get_uint($4), - intnum_get_uint($4)); + nasm_parser_linemgr->set($5, intnum_get_uint($2)-intnum_get_uint($4), + intnum_get_uint($4)); intnum_delete($2); intnum_delete($4); xfree($5); @@ -155,7 +162,8 @@ line: '\n' { $$ = (bytecode *)NULL; } $$ = (bytecode *)NULL; } | error '\n' { - Error(_("label or instruction expected at start of line")); + Error(p_line_index, + _("label or instruction expected at start of line")); $$ = (bytecode *)NULL; yyerrok; } @@ -167,43 +175,55 @@ lineexp: exp | label exp { $$ = $2; } | label TIMES expr exp { $$ = $4; bc_set_multiple($$, $3); } | label_id_equ EQU expr { - symrec_define_equ($1, $3); + symrec_define_equ($1, $3, p_line_index); xfree($1); $$ = (bytecode *)NULL; } ; exp: instr - | DECLARE_DATA datavals { $$ = bc_new_data(&$2, $1); } - | RESERVE_SPACE expr { $$ = bc_new_reserve($2, $1); } - | INCBIN STRING { $$ = bc_new_incbin($2, NULL, NULL); } - | INCBIN STRING ',' expr { $$ = bc_new_incbin($2, $4, NULL); } - | INCBIN STRING ',' expr ',' expr { $$ = bc_new_incbin($2, $4, $6); } + | DECLARE_DATA datavals { + $$ = bc_new_data(&$2, $1, p_line_index); + } + | RESERVE_SPACE expr { + $$ = bc_new_reserve($2, $1, p_line_index); + } + | INCBIN STRING { + $$ = bc_new_incbin($2, NULL, NULL, p_line_index); + } + | INCBIN STRING ',' expr { + $$ = bc_new_incbin($2, $4, NULL, p_line_index); + } + | INCBIN STRING ',' expr ',' expr { + $$ = bc_new_incbin($2, $4, $6, p_line_index); + } ; instr: INSN { $$ = nasm_parser_arch->parse.new_insn($1, 0, NULL, nasm_parser_cur_section, - nasm_parser_prev_bc); + nasm_parser_prev_bc, + p_line_index); } | INSN operands { $$ = nasm_parser_arch->parse.new_insn($1, $2.num_operands, &$2.operands, nasm_parser_cur_section, - nasm_parser_prev_bc); + nasm_parser_prev_bc, + p_line_index); ops_delete(&$2.operands, 0); } | INSN error { - Error(_("expression syntax error")); + Error(p_line_index, _("expression syntax error")); $$ = NULL; } | PREFIX instr { $$ = $2; - nasm_parser_arch->parse.handle_prefix($$, $1); + nasm_parser_arch->parse.handle_prefix($$, $1, p_line_index); } | SEGREG instr { $$ = $2; - nasm_parser_arch->parse.handle_seg_prefix($$, $1[0]); + nasm_parser_arch->parse.handle_seg_prefix($$, $1[0], p_line_index); } ; @@ -214,19 +234,19 @@ datavals: dataval { dvs_initialize(&$$); dvs_append(&$$, $1); } dataval: dvexpr { $$ = dv_new_expr($1); } | STRING { $$ = dv_new_string($1); } | error { - Error(_("expression syntax error")); + Error(p_line_index, _("expression syntax error")); $$ = (dataval *)NULL; } ; label: label_id { symrec_define_label($1, nasm_parser_cur_section, nasm_parser_prev_bc, - 1); + 1, p_line_index); xfree($1); } | label_id ':' { symrec_define_label($1, nasm_parser_cur_section, nasm_parser_prev_bc, - 1); + 1, p_line_index); xfree($1); } ; @@ -254,7 +274,7 @@ directive: DIRECTIVE_NAME directive_val { xfree($1); } | DIRECTIVE_NAME error { - Error(_("invalid arguments to [%s]"), $1); + Error(p_line_index, _("invalid arguments to [%s]"), $1); xfree($1); } ; @@ -298,7 +318,7 @@ memaddr: expr { } | SEGREG ':' memaddr { $$ = $3; - nasm_parser_arch->parse.handle_seg_override($$, $1[0]); + nasm_parser_arch->parse.handle_seg_override($$, $1[0], p_line_index); } | BYTE memaddr { $$ = $2; ea_set_len($$, 1); } | WORD memaddr { $$ = $2; ea_set_len($$, 2); } @@ -326,7 +346,7 @@ operand: '[' memaddr ']' { $$ = operand_new_mem($2); } $$ = $2; if ($$->type == INSN_OPERAND_REG && nasm_parser_arch->get_reg_size($$->data.reg) != 1) - Error(_("cannot override register size")); + Error(p_line_index, _("cannot override register size")); else $$->size = 1; } @@ -334,7 +354,7 @@ operand: '[' memaddr ']' { $$ = operand_new_mem($2); } $$ = $2; if ($$->type == INSN_OPERAND_REG && nasm_parser_arch->get_reg_size($$->data.reg) != 2) - Error(_("cannot override register size")); + Error(p_line_index, _("cannot override register size")); else $$->size = 2; } @@ -342,7 +362,7 @@ operand: '[' memaddr ']' { $$ = operand_new_mem($2); } $$ = $2; if ($$->type == INSN_OPERAND_REG && nasm_parser_arch->get_reg_size($$->data.reg) != 4) - Error(_("cannot override register size")); + Error(p_line_index, _("cannot override register size")); else $$->size = 4; } @@ -350,7 +370,7 @@ operand: '[' memaddr ']' { $$ = operand_new_mem($2); } $$ = $2; if ($$->type == INSN_OPERAND_REG && nasm_parser_arch->get_reg_size($$->data.reg) != 8) - Error(_("cannot override register size")); + Error(p_line_index, _("cannot override register size")); else $$->size = 8; } @@ -358,7 +378,7 @@ operand: '[' memaddr ']' { $$ = operand_new_mem($2); } $$ = $2; if ($$->type == INSN_OPERAND_REG && nasm_parser_arch->get_reg_size($$->data.reg) != 10) - Error(_("cannot override register size")); + Error(p_line_index, _("cannot override register size")); else $$->size = 10; } @@ -366,7 +386,7 @@ operand: '[' memaddr ']' { $$ = operand_new_mem($2); } $$ = $2; if ($$->type == INSN_OPERAND_REG && nasm_parser_arch->get_reg_size($$->data.reg) != 16) - Error(_("cannot override register size")); + Error(p_line_index, _("cannot override register size")); else $$->size = 16; } @@ -376,57 +396,58 @@ operand: '[' memaddr ']' { $$ = operand_new_mem($2); } /* expression trees */ /* expr w/o FLTNUM and unary + and -, for use in directives */ -direxpr: INTNUM { $$ = expr_new_ident(ExprInt($1)); } +direxpr: INTNUM { $$ = p_expr_new_ident(ExprInt($1)); } | ID { - $$ = expr_new_ident(ExprSym(symrec_define_label($1, NULL, NULL, 0))); + $$ = p_expr_new_ident(ExprSym(symrec_define_label($1, NULL, NULL, 0, + p_line_index))); xfree($1); } - | direxpr '|' direxpr { $$ = expr_new_tree($1, EXPR_OR, $3); } - | direxpr '^' direxpr { $$ = expr_new_tree($1, EXPR_XOR, $3); } - | direxpr '&' direxpr { $$ = expr_new_tree($1, EXPR_AND, $3); } - | direxpr LEFT_OP direxpr { $$ = expr_new_tree($1, EXPR_SHL, $3); } - | direxpr RIGHT_OP direxpr { $$ = expr_new_tree($1, EXPR_SHR, $3); } - | direxpr '+' direxpr { $$ = expr_new_tree($1, EXPR_ADD, $3); } - | direxpr '-' direxpr { $$ = expr_new_tree($1, EXPR_SUB, $3); } - | direxpr '*' direxpr { $$ = expr_new_tree($1, EXPR_MUL, $3); } - | direxpr '/' direxpr { $$ = expr_new_tree($1, EXPR_DIV, $3); } - | direxpr SIGNDIV direxpr { $$ = expr_new_tree($1, EXPR_SIGNDIV, $3); } - | direxpr '%' direxpr { $$ = expr_new_tree($1, EXPR_MOD, $3); } - | direxpr SIGNMOD direxpr { $$ = expr_new_tree($1, EXPR_SIGNMOD, $3); } - /*| '!' expr { $$ = expr_new_branch(EXPR_LNOT, $2); }*/ - | '~' direxpr %prec UNARYOP { $$ = expr_new_branch(EXPR_NOT, $2); } + | direxpr '|' direxpr { $$ = p_expr_new_tree($1, EXPR_OR, $3); } + | direxpr '^' direxpr { $$ = p_expr_new_tree($1, EXPR_XOR, $3); } + | direxpr '&' direxpr { $$ = p_expr_new_tree($1, EXPR_AND, $3); } + | direxpr LEFT_OP direxpr { $$ = p_expr_new_tree($1, EXPR_SHL, $3); } + | direxpr RIGHT_OP direxpr { $$ = p_expr_new_tree($1, EXPR_SHR, $3); } + | direxpr '+' direxpr { $$ = p_expr_new_tree($1, EXPR_ADD, $3); } + | direxpr '-' direxpr { $$ = p_expr_new_tree($1, EXPR_SUB, $3); } + | direxpr '*' direxpr { $$ = p_expr_new_tree($1, EXPR_MUL, $3); } + | direxpr '/' direxpr { $$ = p_expr_new_tree($1, EXPR_DIV, $3); } + | direxpr SIGNDIV direxpr { $$ = p_expr_new_tree($1, EXPR_SIGNDIV, $3); } + | direxpr '%' direxpr { $$ = p_expr_new_tree($1, EXPR_MOD, $3); } + | direxpr SIGNMOD direxpr { $$ = p_expr_new_tree($1, EXPR_SIGNMOD, $3); } + /*| '!' expr { $$ = p_expr_new_branch(EXPR_LNOT, $2); }*/ + | '~' direxpr %prec UNARYOP { $$ = p_expr_new_branch(EXPR_NOT, $2); } | '(' direxpr ')' { $$ = $2; } ; -dvexpr: INTNUM { $$ = expr_new_ident(ExprInt($1)); } - | FLTNUM { $$ = expr_new_ident(ExprFloat($1)); } - | explabel { $$ = expr_new_ident(ExprSym($1)); } - /*| dvexpr '||' dvexpr { $$ = expr_new_tree($1, EXPR_LOR, $3); }*/ - | dvexpr '|' dvexpr { $$ = expr_new_tree($1, EXPR_OR, $3); } - | dvexpr '^' dvexpr { $$ = expr_new_tree($1, EXPR_XOR, $3); } - /*| dvexpr '&&' dvexpr { $$ = expr_new_tree($1, EXPR_LAND, $3); }*/ - | dvexpr '&' dvexpr { $$ = expr_new_tree($1, EXPR_AND, $3); } - /*| dvexpr '==' dvexpr { $$ = expr_new_tree($1, EXPR_EQUALS, $3); }*/ - /*| dvexpr '>' dvexpr { $$ = expr_new_tree($1, EXPR_GT, $3); }*/ - /*| dvexpr '<' dvexpr { $$ = expr_new_tree($1, EXPR_GT, $3); }*/ - /*| dvexpr '>=' dvexpr { $$ = expr_new_tree($1, EXPR_GE, $3); }*/ - /*| dvexpr '<=' dvexpr { $$ = expr_new_tree($1, EXPR_GE, $3); }*/ - /*| dvexpr '!=' dvexpr { $$ = expr_new_tree($1, EXPR_NE, $3); }*/ - | dvexpr LEFT_OP dvexpr { $$ = expr_new_tree($1, EXPR_SHL, $3); } - | dvexpr RIGHT_OP dvexpr { $$ = expr_new_tree($1, EXPR_SHR, $3); } - | dvexpr '+' dvexpr { $$ = expr_new_tree($1, EXPR_ADD, $3); } - | dvexpr '-' dvexpr { $$ = expr_new_tree($1, EXPR_SUB, $3); } - | dvexpr '*' dvexpr { $$ = expr_new_tree($1, EXPR_MUL, $3); } - | dvexpr '/' dvexpr { $$ = expr_new_tree($1, EXPR_DIV, $3); } - | dvexpr SIGNDIV dvexpr { $$ = expr_new_tree($1, EXPR_SIGNDIV, $3); } - | dvexpr '%' dvexpr { $$ = expr_new_tree($1, EXPR_MOD, $3); } - | dvexpr SIGNMOD dvexpr { $$ = expr_new_tree($1, EXPR_SIGNMOD, $3); } +dvexpr: INTNUM { $$ = p_expr_new_ident(ExprInt($1)); } + | FLTNUM { $$ = p_expr_new_ident(ExprFloat($1)); } + | explabel { $$ = p_expr_new_ident(ExprSym($1)); } + /*| dvexpr '||' dvexpr { $$ = p_expr_new_tree($1, EXPR_LOR, $3); }*/ + | dvexpr '|' dvexpr { $$ = p_expr_new_tree($1, EXPR_OR, $3); } + | dvexpr '^' dvexpr { $$ = p_expr_new_tree($1, EXPR_XOR, $3); } + /*| dvexpr '&&' dvexpr { $$ = p_expr_new_tree($1, EXPR_LAND, $3); }*/ + | dvexpr '&' dvexpr { $$ = p_expr_new_tree($1, EXPR_AND, $3); } + /*| dvexpr '==' dvexpr { $$ = p_expr_new_tree($1, EXPR_EQUALS, $3); }*/ + /*| dvexpr '>' dvexpr { $$ = p_expr_new_tree($1, EXPR_GT, $3); }*/ + /*| dvexpr '<' dvexpr { $$ = p_expr_new_tree($1, EXPR_GT, $3); }*/ + /*| dvexpr '>=' dvexpr { $$ = p_expr_new_tree($1, EXPR_GE, $3); }*/ + /*| dvexpr '<=' dvexpr { $$ = p_expr_new_tree($1, EXPR_GE, $3); }*/ + /*| dvexpr '!=' dvexpr { $$ = p_expr_new_tree($1, EXPR_NE, $3); }*/ + | dvexpr LEFT_OP dvexpr { $$ = p_expr_new_tree($1, EXPR_SHL, $3); } + | dvexpr RIGHT_OP dvexpr { $$ = p_expr_new_tree($1, EXPR_SHR, $3); } + | dvexpr '+' dvexpr { $$ = p_expr_new_tree($1, EXPR_ADD, $3); } + | dvexpr '-' dvexpr { $$ = p_expr_new_tree($1, EXPR_SUB, $3); } + | dvexpr '*' dvexpr { $$ = p_expr_new_tree($1, EXPR_MUL, $3); } + | dvexpr '/' dvexpr { $$ = p_expr_new_tree($1, EXPR_DIV, $3); } + | dvexpr SIGNDIV dvexpr { $$ = p_expr_new_tree($1, EXPR_SIGNDIV, $3); } + | dvexpr '%' dvexpr { $$ = p_expr_new_tree($1, EXPR_MOD, $3); } + | dvexpr SIGNMOD dvexpr { $$ = p_expr_new_tree($1, EXPR_SIGNMOD, $3); } | '+' dvexpr %prec UNARYOP { $$ = $2; } - | '-' dvexpr %prec UNARYOP { $$ = expr_new_branch(EXPR_NEG, $2); } - /*| '!' dvexpr { $$ = expr_new_branch(EXPR_LNOT, $2); }*/ - | '~' dvexpr %prec UNARYOP { $$ = expr_new_branch(EXPR_NOT, $2); } - | SEG dvexpr { $$ = expr_new_branch(EXPR_SEG, $2); } - | WRT dvexpr { $$ = expr_new_branch(EXPR_WRT, $2); } + | '-' dvexpr %prec UNARYOP { $$ = p_expr_new_branch(EXPR_NEG, $2); } + /*| '!' dvexpr { $$ = p_expr_new_branch(EXPR_LNOT, $2); }*/ + | '~' dvexpr %prec UNARYOP { $$ = p_expr_new_branch(EXPR_NOT, $2); } + | SEG dvexpr { $$ = p_expr_new_branch(EXPR_SEG, $2); } + | WRT dvexpr { $$ = p_expr_new_branch(EXPR_WRT, $2); } | '(' dvexpr ')' { $$ = $2; } ; @@ -434,64 +455,66 @@ dvexpr: INTNUM { $$ = expr_new_ident(ExprInt($1)); } * We don't attempt to check memory expressions for validity here. * Essentially the same as expr_no_string above but adds REG and STRING. */ -expr: INTNUM { $$ = expr_new_ident(ExprInt($1)); } - | FLTNUM { $$ = expr_new_ident(ExprFloat($1)); } - | REG { $$ = expr_new_ident(ExprReg($1[0])); } +expr: INTNUM { $$ = p_expr_new_ident(ExprInt($1)); } + | FLTNUM { $$ = p_expr_new_ident(ExprFloat($1)); } + | REG { $$ = p_expr_new_ident(ExprReg($1[0])); } | STRING { - $$ = expr_new_ident(ExprInt(intnum_new_charconst_nasm($1))); + $$ = p_expr_new_ident(ExprInt(intnum_new_charconst_nasm($1, + p_line_index))); xfree($1); } - | explabel { $$ = expr_new_ident(ExprSym($1)); } - /*| expr '||' expr { $$ = expr_new_tree($1, EXPR_LOR, $3); }*/ - | expr '|' expr { $$ = expr_new_tree($1, EXPR_OR, $3); } - | expr '^' expr { $$ = expr_new_tree($1, EXPR_XOR, $3); } - /*| expr '&&' expr { $$ = expr_new_tree($1, EXPR_LAND, $3); }*/ - | expr '&' expr { $$ = expr_new_tree($1, EXPR_AND, $3); } - /*| expr '==' expr { $$ = expr_new_tree($1, EXPR_EQUALS, $3); }*/ - /*| expr '>' expr { $$ = expr_new_tree($1, EXPR_GT, $3); }*/ - /*| expr '<' expr { $$ = expr_new_tree($1, EXPR_GT, $3); }*/ - /*| expr '>=' expr { $$ = expr_new_tree($1, EXPR_GE, $3); }*/ - /*| expr '<=' expr { $$ = expr_new_tree($1, EXPR_GE, $3); }*/ - /*| expr '!=' expr { $$ = expr_new_tree($1, EXPR_NE, $3); }*/ - | expr LEFT_OP expr { $$ = expr_new_tree($1, EXPR_SHL, $3); } - | expr RIGHT_OP expr { $$ = expr_new_tree($1, EXPR_SHR, $3); } - | expr '+' expr { $$ = expr_new_tree($1, EXPR_ADD, $3); } - | expr '-' expr { $$ = expr_new_tree($1, EXPR_SUB, $3); } - | expr '*' expr { $$ = expr_new_tree($1, EXPR_MUL, $3); } - | expr '/' expr { $$ = expr_new_tree($1, EXPR_DIV, $3); } - | expr SIGNDIV expr { $$ = expr_new_tree($1, EXPR_SIGNDIV, $3); } - | expr '%' expr { $$ = expr_new_tree($1, EXPR_MOD, $3); } - | expr SIGNMOD expr { $$ = expr_new_tree($1, EXPR_SIGNMOD, $3); } + | explabel { $$ = p_expr_new_ident(ExprSym($1)); } + /*| expr '||' expr { $$ = p_expr_new_tree($1, EXPR_LOR, $3); }*/ + | expr '|' expr { $$ = p_expr_new_tree($1, EXPR_OR, $3); } + | expr '^' expr { $$ = p_expr_new_tree($1, EXPR_XOR, $3); } + /*| expr '&&' expr { $$ = p_expr_new_tree($1, EXPR_LAND, $3); }*/ + | expr '&' expr { $$ = p_expr_new_tree($1, EXPR_AND, $3); } + /*| expr '==' expr { $$ = p_expr_new_tree($1, EXPR_EQUALS, $3); }*/ + /*| expr '>' expr { $$ = p_expr_new_tree($1, EXPR_GT, $3); }*/ + /*| expr '<' expr { $$ = p_expr_new_tree($1, EXPR_GT, $3); }*/ + /*| expr '>=' expr { $$ = p_expr_new_tree($1, EXPR_GE, $3); }*/ + /*| expr '<=' expr { $$ = p_expr_new_tree($1, EXPR_GE, $3); }*/ + /*| expr '!=' expr { $$ = p_expr_new_tree($1, EXPR_NE, $3); }*/ + | expr LEFT_OP expr { $$ = p_expr_new_tree($1, EXPR_SHL, $3); } + | expr RIGHT_OP expr { $$ = p_expr_new_tree($1, EXPR_SHR, $3); } + | expr '+' expr { $$ = p_expr_new_tree($1, EXPR_ADD, $3); } + | expr '-' expr { $$ = p_expr_new_tree($1, EXPR_SUB, $3); } + | expr '*' expr { $$ = p_expr_new_tree($1, EXPR_MUL, $3); } + | expr '/' expr { $$ = p_expr_new_tree($1, EXPR_DIV, $3); } + | expr SIGNDIV expr { $$ = p_expr_new_tree($1, EXPR_SIGNDIV, $3); } + | expr '%' expr { $$ = p_expr_new_tree($1, EXPR_MOD, $3); } + | expr SIGNMOD expr { $$ = p_expr_new_tree($1, EXPR_SIGNMOD, $3); } | '+' expr %prec UNARYOP { $$ = $2; } - | '-' expr %prec UNARYOP { $$ = expr_new_branch(EXPR_NEG, $2); } - /*| '!' expr { $$ = expr_new_branch(EXPR_LNOT, $2); }*/ - | '~' expr %prec UNARYOP { $$ = expr_new_branch(EXPR_NOT, $2); } - | SEG expr { $$ = expr_new_branch(EXPR_SEG, $2); } - | WRT expr { $$ = expr_new_branch(EXPR_WRT, $2); } - | expr ':' expr { $$ = expr_new_tree($1, EXPR_SEGOFF, $3); } + | '-' expr %prec UNARYOP { $$ = p_expr_new_branch(EXPR_NEG, $2); } + /*| '!' expr { $$ = p_expr_new_branch(EXPR_LNOT, $2); }*/ + | '~' expr %prec UNARYOP { $$ = p_expr_new_branch(EXPR_NOT, $2); } + | SEG expr { $$ = p_expr_new_branch(EXPR_SEG, $2); } + | WRT expr { $$ = p_expr_new_branch(EXPR_WRT, $2); } + | expr ':' expr { $$ = p_expr_new_tree($1, EXPR_SEGOFF, $3); } | '(' expr ')' { $$ = $2; } ; explabel: ID { - $$ = symrec_use($1); + $$ = symrec_use($1, p_line_index); xfree($1); } | SPECIAL_ID { - $$ = symrec_use($1); + $$ = symrec_use($1, p_line_index); xfree($1); } | LOCAL_ID { - $$ = symrec_use($1); + $$ = symrec_use($1, p_line_index); xfree($1); } | '$' { /* "$" references the current assembly position */ $$ = symrec_define_label("$", nasm_parser_cur_section, - nasm_parser_prev_bc, 0); + nasm_parser_prev_bc, 0, p_line_index); } | START_SECTION_ID { /* "$$" references the start of the current section */ - $$ = symrec_define_label("$$", nasm_parser_cur_section, NULL, 0); + $$ = symrec_define_label("$$", nasm_parser_cur_section, NULL, 0, + p_line_index); } ; @@ -504,64 +527,71 @@ nasm_parser_directive(const char *name, valparamhead *valparams, { valparam *vp, *vp2; symrec *sym; + unsigned long lindex = p_line_index; /* Handle (mostly) output-format independent directives here */ if (strcasecmp(name, "extern") == 0) { vp = vps_first(valparams); if (vp->val) { - sym = symrec_declare(vp->val, SYM_EXTERN); + sym = symrec_declare(vp->val, SYM_EXTERN, lindex); if (nasm_parser_objfmt->extern_declare) - nasm_parser_objfmt->extern_declare(sym, objext_valparams); + nasm_parser_objfmt->extern_declare(sym, objext_valparams, + lindex); } else - Error(_("invalid argument to [%s]"), "EXTERN"); + Error(lindex, _("invalid argument to [%s]"), "EXTERN"); } else if (strcasecmp(name, "global") == 0) { vp = vps_first(valparams); if (vp->val) { - sym = symrec_declare(vp->val, SYM_GLOBAL); + sym = symrec_declare(vp->val, SYM_GLOBAL, lindex); if (nasm_parser_objfmt->global_declare) - nasm_parser_objfmt->global_declare(sym, objext_valparams); + nasm_parser_objfmt->global_declare(sym, objext_valparams, + lindex); } else - Error(_("invalid argument to [%s]"), "GLOBAL"); + Error(lindex, _("invalid argument to [%s]"), "GLOBAL"); } else if (strcasecmp(name, "common") == 0) { vp = vps_first(valparams); if (vp->val) { vp2 = vps_next(vp); if (!vp2 || (!vp2->val && !vp2->param)) - Error(_("no size specified in %s declaration"), "COMMON"); + Error(lindex, _("no size specified in %s declaration"), + "COMMON"); else { if (vp2->val) { - sym = symrec_declare(vp->val, SYM_COMMON); + sym = symrec_declare(vp->val, SYM_COMMON, lindex); if (nasm_parser_objfmt->common_declare) nasm_parser_objfmt->common_declare(sym, - expr_new_ident(ExprSym(symrec_use(vp2->val))), - objext_valparams); + p_expr_new_ident(ExprSym(symrec_use(vp2->val, + lindex))), + objext_valparams, lindex); } else if (vp2->param) { - sym = symrec_declare(vp->val, SYM_COMMON); + sym = symrec_declare(vp->val, SYM_COMMON, lindex); if (nasm_parser_objfmt->common_declare) nasm_parser_objfmt->common_declare(sym, vp2->param, - objext_valparams); + objext_valparams, + lindex); vp2->param = NULL; } } } else - Error(_("invalid argument to [%s]"), "COMMON"); + Error(lindex, _("invalid argument to [%s]"), "COMMON"); } else if (strcasecmp(name, "section") == 0 || strcasecmp(name, "segment") == 0) { section *new_section = nasm_parser_objfmt->sections_switch(&nasm_parser_sections, - valparams, objext_valparams); + valparams, objext_valparams, + lindex); if (new_section) { nasm_parser_cur_section = new_section; nasm_parser_prev_bc = bcs_last(section_get_bytecodes(new_section)); } else - Error(_("invalid argument to [%s]"), "SECTION"); + Error(lindex, _("invalid argument to [%s]"), "SECTION"); } else if (strcasecmp(name, "absolute") == 0) { /* it can be just an ID or a complete expression, so handle both. */ vp = vps_first(valparams); if (vp->val) nasm_parser_cur_section = sections_switch_absolute(&nasm_parser_sections, - expr_new_ident(ExprSym(symrec_use(vp->val)))); + p_expr_new_ident(ExprSym(symrec_use(vp->val, lindex)))); else if (vp->param) { nasm_parser_cur_section = sections_switch_absolute(&nasm_parser_sections, vp->param); @@ -571,26 +601,27 @@ nasm_parser_directive(const char *name, valparamhead *valparams, } else if (strcasecmp(name, "cpu") == 0) { vps_foreach(vp, valparams) { if (vp->val) - nasm_parser_arch->parse.switch_cpu(vp->val); + nasm_parser_arch->parse.switch_cpu(vp->val, lindex); else if (vp->param) { const intnum *intcpu; intcpu = expr_get_intnum(&vp->param, NULL); if (!intcpu) - Error(_("invalid argument to [%s]"), "CPU"); + Error(lindex, _("invalid argument to [%s]"), "CPU"); else { char strcpu[16]; sprintf(strcpu, "%lu", intnum_get_uint(intcpu)); - nasm_parser_arch->parse.switch_cpu(strcpu); + nasm_parser_arch->parse.switch_cpu(strcpu, lindex); } } } } else if (!nasm_parser_arch->parse.directive(name, valparams, objext_valparams, - &nasm_parser_sections)) { + &nasm_parser_sections, + lindex)) { ; } else if (nasm_parser_objfmt->directive(name, valparams, objext_valparams, - &nasm_parser_sections)) { - Error(_("unrecognized directive [%s]"), name); + &nasm_parser_sections, lindex)) { + Error(lindex, _("unrecognized directive [%s]"), name); } vps_delete(valparams); @@ -601,6 +632,6 @@ nasm_parser_directive(const char *name, valparamhead *valparams, void nasm_parser_error(const char *s) { - ParserError(s); + ParserError(p_line_index, s); } diff --git a/src/parsers/nasm/nasm-parser.c b/src/parsers/nasm/nasm-parser.c index 6d482108..bb78777c 100644 --- a/src/parsers/nasm/nasm-parser.c +++ b/src/parsers/nasm/nasm-parser.c @@ -36,7 +36,7 @@ extern int nasm_parser_debug; extern int nasm_parser_parse(void); extern void nasm_parser_cleanup(void); -size_t (*nasm_parser_input) (char *buf, size_t max_size); +size_t (*nasm_parser_input) (char *buf, size_t max_size, linemgr *lm); sectionhead nasm_parser_sections; /*@dependent@*/ section *nasm_parser_cur_section; @@ -45,9 +45,10 @@ extern /*@only@*/ char *nasm_parser_locallabel_base; /*@dependent@*/ arch *nasm_parser_arch; /*@dependent@*/ objfmt *nasm_parser_objfmt; +/*@dependent@*/ linemgr *nasm_parser_linemgr; static /*@dependent@*/ sectionhead * -nasm_parser_do_parse(preproc *pp, arch *a, objfmt *of, FILE *f, +nasm_parser_do_parse(preproc *pp, arch *a, objfmt *of, linemgr *lm, FILE *f, const char *in_filename) /*@globals killed nasm_parser_locallabel_base @*/ { @@ -56,6 +57,7 @@ nasm_parser_do_parse(preproc *pp, arch *a, objfmt *of, FILE *f, nasm_parser_input = pp->input; nasm_parser_arch = a; nasm_parser_objfmt = of; + nasm_parser_linemgr = lm; /* Initialize section list */ nasm_parser_cur_section = sections_initialize(&nasm_parser_sections, of); diff --git a/src/parsers/nasm/nasm-token.re b/src/parsers/nasm/nasm-token.re index 52173c16..fc1dd26a 100644 --- a/src/parsers/nasm/nasm-token.re +++ b/src/parsers/nasm/nasm-token.re @@ -26,6 +26,7 @@ RCSID("$IdPath$"); #include "bitvect.h" +#include "linemgr.h" #include "errwarn.h" #include "intnum.h" #include "floatnum.h" @@ -62,8 +63,11 @@ void nasm_parser_cleanup(void); void nasm_parser_set_directive_state(void); int nasm_parser_lex(void); -extern size_t (*nasm_parser_input) (char *buf, size_t max_size); +extern size_t (*nasm_parser_input) (char *buf, size_t max_size, linemgr *lm); extern /*@dependent@*/ arch *nasm_parser_arch; +extern /*@dependent@*/ linemgr *nasm_parser_linemgr; + +#define p_line_index (nasm_parser_linemgr->get_current()) typedef struct Scanner { @@ -101,7 +105,8 @@ fill(YYCTYPE *cursor) xfree(s.bot); s.bot = buf; } - if((cnt = nasm_parser_input(s.lim, BSIZE)) != BSIZE){ + if((cnt = nasm_parser_input(s.lim, BSIZE, + nasm_parser_linemgr)) != BSIZE){ s.eof = &s.lim[cnt]; *s.eof++ = '\n'; } s.lim += cnt; @@ -215,7 +220,7 @@ scan: digit+ { savech = s.tok[TOKLEN]; s.tok[TOKLEN] = '\0'; - yylval.intn = intnum_new_dec(s.tok); + yylval.intn = intnum_new_dec(s.tok, p_line_index); s.tok[TOKLEN] = savech; RETURN(INTNUM); } @@ -223,21 +228,21 @@ scan: bindigit+ "b" { s.tok[TOKLEN-1] = '\0'; /* strip off 'b' */ - yylval.intn = intnum_new_bin(s.tok); + yylval.intn = intnum_new_bin(s.tok, p_line_index); RETURN(INTNUM); } /* 777q - octal number */ octdigit+ "q" { s.tok[TOKLEN-1] = '\0'; /* strip off 'q' */ - yylval.intn = intnum_new_oct(s.tok); + yylval.intn = intnum_new_oct(s.tok, p_line_index); RETURN(INTNUM); } /* 0AAh form of hexidecimal number */ digit hexdigit* "h" { s.tok[TOKLEN-1] = '\0'; /* strip off 'h' */ - yylval.intn = intnum_new_hex(s.tok); + yylval.intn = intnum_new_hex(s.tok, p_line_index); RETURN(INTNUM); } @@ -246,9 +251,11 @@ scan: savech = s.tok[TOKLEN]; s.tok[TOKLEN] = '\0'; if (s.tok[1] == 'x') - yylval.intn = intnum_new_hex(s.tok+2); /* skip 0 and x */ + /* skip 0 and x */ + yylval.intn = intnum_new_hex(s.tok+2, p_line_index); else - yylval.intn = intnum_new_hex(s.tok+1); /* don't skip 0 */ + /* don't skip 0 */ + yylval.intn = intnum_new_hex(s.tok+1, p_line_index); s.tok[TOKLEN] = savech; RETURN(INTNUM); } @@ -335,7 +342,8 @@ scan: yylval.str_val = xstrndup(s.tok, TOKLEN); RETURN(ID); } else if (!nasm_parser_locallabel_base) { - Warning(_("no non-local label before `%s'"), s.tok[0]); + Warning(p_line_index, _("no non-local label before `%s'"), + s.tok[0]); yylval.str_val = xstrndup(s.tok, TOKLEN); } else { len = TOKLEN + nasm_parser_locallabel_base_len; @@ -359,7 +367,7 @@ scan: savech = s.tok[TOKLEN]; s.tok[TOKLEN] = '\0'; check_id_ret = nasm_parser_arch->parse.check_identifier( - yylval.arch_data, s.tok); + yylval.arch_data, s.tok, p_line_index); s.tok[TOKLEN] = savech; switch (check_id_ret) { case ARCH_CHECK_ID_NONE: @@ -377,7 +385,8 @@ scan: case ARCH_CHECK_ID_TARGETMOD: RETURN(TARGETMOD); default: - Warning(_("Arch feature not supported, treating as identifier")); + Warning(p_line_index, + _("Arch feature not supported, treating as identifier")); yylval.str_val = xstrndup(s.tok, TOKLEN); RETURN(ID); } @@ -391,7 +400,8 @@ scan: any { if (WARN_ENABLED(WARN_UNRECOGNIZED_CHAR)) - Warning(_("ignoring unrecognized character `%s'"), + Warning(p_line_index, + _("ignoring unrecognized character `%s'"), conv_unprint(s.tok[0])); goto scan; } @@ -406,7 +416,7 @@ linechg: linechg_numcount++; savech = s.tok[TOKLEN]; s.tok[TOKLEN] = '\0'; - yylval.intn = intnum_new_dec(s.tok); + yylval.intn = intnum_new_dec(s.tok, p_line_index); s.tok[TOKLEN] = savech; RETURN(INTNUM); } @@ -430,7 +440,8 @@ linechg: any { if (WARN_ENABLED(WARN_UNRECOGNIZED_CHAR)) - Warning(_("ignoring unrecognized character `%s'"), + Warning(p_line_index, + _("ignoring unrecognized character `%s'"), conv_unprint(s.tok[0])); goto linechg; } @@ -472,7 +483,8 @@ directive: any { if (WARN_ENABLED(WARN_UNRECOGNIZED_CHAR)) - Warning(_("ignoring unrecognized character `%s'"), + Warning(p_line_index, + _("ignoring unrecognized character `%s'"), conv_unprint(s.tok[0])); goto directive; } @@ -490,9 +502,9 @@ stringconst_scan: /*!re2c "\n" { if (cursor == s.eof) - Error(_("unexpected end of file in string")); + Error(p_line_index, _("unexpected end of file in string")); else - Error(_("unterminated string")); + Error(p_line_index, _("unterminated string")); strbuf[count] = '\0'; yylval.str_val = strbuf; RETURN(STRING); diff --git a/src/preproc.h b/src/preproc.h index 7b0f1d0a..a1e2b9fe 100644 --- a/src/preproc.h +++ b/src/preproc.h @@ -43,7 +43,7 @@ struct preproc { /* Gets more preprocessed source code (up to max_size bytes) into buf. * Note that more than a single line may be returned in buf. */ - size_t (*input) (/*@out@*/ char *buf, size_t max_size); + size_t (*input) (/*@out@*/ char *buf, size_t max_size, linemgr *lm); }; #endif diff --git a/src/preprocs/raw/raw-preproc.c b/src/preprocs/raw/raw-preproc.c index 6de9b0c2..c6cb89d2 100644 --- a/src/preprocs/raw/raw-preproc.c +++ b/src/preprocs/raw/raw-preproc.c @@ -42,7 +42,7 @@ raw_preproc_initialize(FILE *f, const char *in_filename) } static size_t -raw_preproc_input(char *buf, size_t max_size) +raw_preproc_input(char *buf, size_t max_size, unsigned long lindex) { int c = '*'; size_t n; @@ -53,9 +53,9 @@ raw_preproc_input(char *buf, size_t max_size) if (c == '\n') buf[n++] = (char)c; if (c == EOF && ferror(in)) - Error(_("error when reading from file")); + Error(lindex, _("error when reading from file")); } else if (((n = fread(buf, 1, max_size, in)) == 0) && ferror(in)) - Error(_("error when reading from file")); + Error(lindex, _("error when reading from file")); return n; } diff --git a/src/preprocs/yapp/yapp-preproc.c b/src/preprocs/yapp/yapp-preproc.c index 230529b8..681f7b78 100644 --- a/src/preprocs/yapp/yapp-preproc.c +++ b/src/preprocs/yapp/yapp-preproc.c @@ -22,6 +22,7 @@ #include "util.h" /*@unused@*/ RCSID("$IdPath$"); +#include "linemgr.h" #include "errwarn.h" #include "preproc.h" #include "hamt.h" @@ -36,9 +37,12 @@ static int saved_length; static HAMT *macro_table; -YAPP_Output current_output; +static YAPP_Output current_output; YYSTYPE yapp_preproc_lval; +/*@dependent@*/ linemgr *yapp_preproc_linemgr; +#define p_line_index (yapp_preproc_linemgr->get_current()) + int isatty(int); /* Build source and macro representations */ @@ -116,13 +120,13 @@ yapp_macro_insert (char *name, int argc, int fillargs) void yapp_macro_error_exists (YAPP_Macro *v) { - if (v) Error(_("Redefining macro of the same name %d:%d"), v->type, v->args); + if (v) Error(p_line_index, _("Redefining macro of the same name %d:%d"), v->type, v->args); } void yapp_macro_error_sameargname (YAPP_Macro *v) { - if (v) Error(_("Duplicate argument names in macro")); + if (v) Error(p_line_index, _("Duplicate argument names in macro")); } YAPP_Macro * @@ -137,7 +141,7 @@ yapp_define_insert (char *name, int argc, int fillargs) if ((argc >= 0 && ym->args < 0) || (argc < 0 && ym->args >= 0)) { - Warning(_("Attempted %%define both with and without parameters")); + Warning(p_line_index, _("Attempted %%define both with and without parameters")); return NULL; } } @@ -236,8 +240,8 @@ static void yapp_preproc_initialize(FILE *f, const char *in_filename) { is_interactive = f ? (isatty(fileno(f)) > 0) : 0; - current_file = xstrdup(in_filename); - line_number = 1; + yapp_preproc_current_file = xstrdup(in_filename); + yapp_preproc_line_number = 1; yapp_lex_initialize(f); SLIST_INIT(&output_head); SLIST_INIT(&source_head); @@ -364,8 +368,8 @@ append_token(int token, struct source_head *to_head, source **to_tail) && (token == '\n' || token == LINE)) { free ((*to_tail)->token.str); - (*to_tail)->token.str = xmalloc(23+strlen(current_file)); - sprintf((*to_tail)->token.str, "%%line %d+1 %s\n", line_number, current_file); + (*to_tail)->token.str = xmalloc(23+strlen(yapp_preproc_current_file)); + sprintf((*to_tail)->token.str, "%%line %d+1 %s\n", yapp_preproc_line_number, yapp_preproc_current_file); } else { src = xmalloc(sizeof(source)); @@ -400,8 +404,8 @@ append_token(int token, struct source_head *to_head, source **to_tail) case LINE: /* TODO: consider removing any trailing newline or LINE tokens */ - src->token.str = xmalloc(23+strlen(current_file)); - sprintf(src->token.str, "%%line %d+1 %s\n", line_number, current_file); + src->token.str = xmalloc(23+strlen(yapp_preproc_current_file)); + sprintf(src->token.str, "%%line %d+1 %s\n", yapp_preproc_line_number, yapp_preproc_current_file); break; default: @@ -457,7 +461,7 @@ eat_through_return(struct source_head *to_head, source **to_tail) while ((token = yapp_preproc_lex()) != '\n') { if (token == 0) return 0; - Error(_("Skipping possibly valid %%define stuff")); + Error(p_line_index, _("Skipping possibly valid %%define stuff")); } append_token('\n', to_head, to_tail); return '\n'; @@ -470,7 +474,7 @@ yapp_get_ident(const char *synlvl) if (token == WHITESPACE) token = yapp_preproc_lex(); if (token != IDENT) { - Error(_("Identifier expected after %%%s"), synlvl); + Error(p_line_index, _("Identifier expected after %%%s"), synlvl); } return token; } @@ -579,7 +583,7 @@ expand_macro(char *name, if (token < 256) InternalError(_("Unexpected character token in parameter expansion")); else - Error(_("Cannot handle preprocessor items inside possible macro invocation")); + Error(p_line_index, _("Cannot handle preprocessor items inside possible macro invocation")); } } @@ -739,13 +743,15 @@ expand_token_list(struct source_head *paramexp, struct source_head *to_head, sou } static size_t -yapp_preproc_input(char *buf, size_t max_size) +yapp_preproc_input(char *buf, size_t max_size, linemgr *lm) { static YAPP_State state = YAPP_STATE_INITIAL; int n = 0; int token; int need_line_directive = 0; + yapp_preproc_linemgr = lm; + while (saved_length < max_size && state != YAPP_STATE_EOF) { token = yapp_preproc_lex(); @@ -759,7 +765,7 @@ yapp_preproc_input(char *buf, size_t max_size) append_token(token, &source_head, &source_tail); /*if (append_to_return()==0) state=YAPP_STATE_EOF;*/ ydebug(("YAPP: default: '%c' \"%s\"\n", token, yapp_preproc_lval.str_val)); - /*Error(_("YAPP got an unhandled token."));*/ + /*Error(p_line_index, _("YAPP got an unhandled token."));*/ break; case IDENT: @@ -828,7 +834,7 @@ yapp_preproc_input(char *buf, size_t max_size) break; } else if (last_token == ',' || token != ',') - Error(_("Unexpected token in %%define parameters")); + Error(p_line_index, _("Unexpected token in %%define parameters")); last_token = token; } if (token == ')') { @@ -908,7 +914,7 @@ yapp_preproc_input(char *buf, size_t max_size) } break; default: - Error(_("YAPP got into a bad state")); + Error(p_line_index, _("YAPP got into a bad state")); } if (need_line_directive) { append_token(LINE, &source_head, &source_tail); diff --git a/src/preprocs/yapp/yapp-token.h b/src/preprocs/yapp/yapp-token.h index 8d503aab..01c67b7a 100644 --- a/src/preprocs/yapp/yapp-token.h +++ b/src/preprocs/yapp/yapp-token.h @@ -58,7 +58,9 @@ typedef union { extern YYSTYPE yapp_preproc_lval; -extern char *current_file; -extern int line_number; +extern char *yapp_preproc_current_file; +extern int yapp_preproc_line_number; +extern /*@dependent@*/ linemgr *yapp_preproc_linemgr; +#define p_line_index (yapp_preproc_linemgr->get_current()) int yapp_preproc_lex(void); diff --git a/src/preprocs/yapp/yapp-token.l b/src/preprocs/yapp/yapp-token.l index 4b7a7b28..17072e20 100644 --- a/src/preprocs/yapp/yapp-token.l +++ b/src/preprocs/yapp/yapp-token.l @@ -25,6 +25,7 @@ #include +#include "linemgr.h" #include "errwarn.h" #include "src/preprocs/yapp/yapp-preproc.h" @@ -52,8 +53,8 @@ struct include_s { }; typedef struct include_s include; -char *current_file; -int line_number; +char *yapp_preproc_current_file; +int yapp_preproc_line_number; %} %option noyywrap @@ -142,9 +143,9 @@ DIR %[ \t]* } if(inch == '\n') - Error(_("unterminated string")); + Error(p_line_index, _("unterminated string")); else if(inch == EOF) - Error(_("unexpected end of file in string")); + Error(p_line_index, _("unexpected end of file in string")); strbuf[count] = '\0'; @@ -175,18 +176,18 @@ DIR %[ \t]* /* FIXME: handle includes that aren't relative */ incfile = fopen (yytext, "r"); if(!incfile) { - Error(_("include file `%s': %s"), + Error(p_line_index, _("include file `%s': %s"), yytext, strerror(errno)); free(inc); } else { yyin = incfile; - inc->filename = current_file; - inc->line_number = line_number; + inc->filename = yapp_preproc_current_file; + inc->line_number = yapp_preproc_line_number; SLIST_INSERT_HEAD(&includes_head, inc, next); - line_number = 1; - current_file = xstrdup(yytext); + yapp_preproc_line_number = 1; + yapp_preproc_current_file = xstrdup(yytext); BEGIN(INITIAL); yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE)); } @@ -203,9 +204,9 @@ DIR %[ \t]* inc = SLIST_FIRST(&includes_head); yy_delete_buffer (YY_CURRENT_BUFFER); yy_switch_to_buffer (inc->include_state); - free(current_file); - current_file = inc->filename; - line_number = inc->line_number + 1; + free(yapp_preproc_current_file); + yapp_preproc_current_file = inc->filename; + yapp_preproc_line_number = inc->line_number + 1; SLIST_REMOVE_HEAD(&includes_head, next); free(inc); @@ -223,16 +224,16 @@ DIR %[ \t]* {DIR}line[^\n] ; {DIR}line BEGIN(line); -{DIGIT}+ line_number = strtoul(yytext, (char **)NULL, 10); +{DIGIT}+ yapp_preproc_line_number = strtoul(yytext, (char **)NULL, 10); {DIGIT}+{WS}*\n { - line_number = strtoul(yytext, (char **)NULL, 10); + yapp_preproc_line_number = strtoul(yytext, (char **)NULL, 10); BEGIN(INITIAL); return LINE; } {WS}+["] ; /* eat space before file */ [^ \t\n"]* { /* have the filename */ - free(current_file); - current_file = xstrdup(yytext); + free(yapp_preproc_current_file); + yapp_preproc_current_file = xstrdup(yytext); } ["]{WS}*\n { BEGIN(INITIAL); @@ -321,23 +322,23 @@ DIR %[ \t]* {DIR}pop[^\n]* ; {DIR}repl[^\n]* ; -[^%\n]*\n { line_number++; return '\n'; } +[^%\n]*\n { yapp_preproc_line_number++; return '\n'; } -;.*\n { line_number++; return '\n'; } +;.*\n { yapp_preproc_line_number++; return '\n'; } {WS}+ { yylval.str_val = yytext; return WHITESPACE; } -{WS}*\n { line_number++; return '\n'; } +{WS}*\n { yapp_preproc_line_number++; return '\n'; } [][+*/,()-] { return yytext[0]; } . { - Warning(_("Unhandled character in `%s'"), conv_unprint(yytext[0])); + Warning(p_line_index, _("Unhandled character in `%s'"), conv_unprint(yytext[0])); } . { - Warning(_("ignoring unrecognized character `%s'"), + Warning(p_line_index, _("ignoring unrecognized character `%s'"), conv_unprint(yytext[0])); } diff --git a/src/section.c b/src/section.c index 88d10fff..430c73bf 100644 --- a/src/section.c +++ b/src/section.c @@ -22,7 +22,6 @@ #include "util.h" /*@unused@*/ RCSID("$IdPath$"); -#include "globals.h" #include "errwarn.h" #include "intnum.h" #include "expr.h" @@ -72,7 +71,7 @@ sections_initialize(sectionhead *headp, objfmt *of) vp_new(vp, xstrdup(of->default_section_name), NULL); vps_initialize(&vps); vps_append(&vps, vp); - s = of->sections_switch(headp, &vps, NULL); + s = of->sections_switch(headp, &vps, NULL, 0); vps_delete(&vps); return s; @@ -82,7 +81,8 @@ sections_initialize(sectionhead *headp, objfmt *of) /*@-onlytrans@*/ section * sections_switch_general(sectionhead *headp, const char *name, - unsigned long start, int res_only, int *isnew) + unsigned long start, int res_only, int *isnew, + unsigned long lindex) { section *s; @@ -107,7 +107,7 @@ sections_switch_general(sectionhead *headp, const char *name, s->data.general.name = xstrdup(name); s->data.general.of = NULL; s->data.general.of_data = NULL; - s->start = expr_new_ident(ExprInt(intnum_new_uint(start))); + s->start = expr_new_ident(ExprInt(intnum_new_uint(start)), lindex); bcs_initialize(&s->bc); s->opt_flags = 0; @@ -260,10 +260,10 @@ section_get_name(const section *sect) } void -section_set_start(section *sect, unsigned long start) +section_set_start(section *sect, unsigned long start, unsigned long lindex) { expr_delete(sect->start); - sect->start = expr_new_ident(ExprInt(intnum_new_uint(start))); + sect->start = expr_new_ident(ExprInt(intnum_new_uint(start)), lindex); } const expr * diff --git a/src/section.h b/src/section.h index 7645e40c..c7f5e954 100644 --- a/src/section.h +++ b/src/section.h @@ -28,7 +28,8 @@ const char *name, unsigned long start, int res_only, - /*@out@*/ int *isnew); + /*@out@*/ int *isnew, + unsigned long lindex); /*@dependent@*/ section *sections_switch_absolute(sectionhead *headp, /*@keep@*/ expr *start); @@ -63,7 +64,8 @@ int sections_traverse(sectionhead *headp, /*@null@*/ void *d, /*@observer@*/ /*@null@*/ const char *section_get_name(const section *sect); -void section_set_start(section *sect, unsigned long start); +void section_set_start(section *sect, unsigned long start, + unsigned long lindex); /*@observer@*/ const expr *section_get_start(const section *sect); void section_delete(/*@only@*/ section *sect); diff --git a/src/symrec.c b/src/symrec.c index 876c1c32..fe32457b 100644 --- a/src/symrec.c +++ b/src/symrec.c @@ -28,7 +28,6 @@ #include "hamt.h" -#include "globals.h" #include "errwarn.h" #include "floatnum.h" #include "expr.h" @@ -177,27 +176,29 @@ symrec_traverse(void *d, int (*func) (symrec *sym, void *d)) } symrec * -symrec_use(const char *name) +symrec_use(const char *name, unsigned long lindex) { symrec *rec = symrec_get_or_new(name, 1); if (rec->line == 0) - rec->line = line_index; /* set line number of first use */ + rec->line = lindex; /* set line number of first use */ rec->status |= SYM_USED; return rec; } static /*@dependent@*/ symrec * -symrec_define(const char *name, SymType type, int in_table) +symrec_define(const char *name, SymType type, int in_table, + unsigned long lindex) { symrec *rec = symrec_get_or_new(name, in_table); /* Has it been defined before (either by DEFINED or COMMON/EXTERN)? */ if ((rec->status & SYM_DEFINED) || (rec->visibility & (SYM_COMMON | SYM_EXTERN))) { - Error(_("duplicate definition of `%s'; first defined on line %lu"), + Error(lindex, + _("duplicate definition of `%s'; first defined on line %lu"), name, rec->line); } else { - rec->line = line_index; /* set line number of definition */ + rec->line = lindex; /* set line number of definition */ rec->type = type; rec->status |= SYM_DEFINED; } @@ -205,9 +206,9 @@ symrec_define(const char *name, SymType type, int in_table) } symrec * -symrec_define_equ(const char *name, expr *e) +symrec_define_equ(const char *name, expr *e, unsigned long lindex) { - symrec *rec = symrec_define(name, SYM_EQU, 1); + symrec *rec = symrec_define(name, SYM_EQU, 1, lindex); rec->value.expn = e; rec->status |= SYM_VALUED; return rec; @@ -215,16 +216,16 @@ symrec_define_equ(const char *name, expr *e) symrec * symrec_define_label(const char *name, section *sect, bytecode *precbc, - int in_table) + int in_table, unsigned long lindex) { - symrec *rec = symrec_define(name, SYM_LABEL, in_table); + symrec *rec = symrec_define(name, SYM_LABEL, in_table, lindex); rec->value.label.sect = sect; rec->value.label.bc = precbc; return rec; } symrec * -symrec_declare(const char *name, SymVisibility vis) +symrec_declare(const char *name, SymVisibility vis, unsigned long lindex) { symrec *rec = symrec_get_or_new(name, 1); @@ -246,7 +247,8 @@ symrec_declare(const char *name, SymVisibility vis) ((rec->visibility & SYM_EXTERN) && (vis == SYM_EXTERN))))) rec->visibility |= vis; else - Error(_("duplicate definition of `%s'; first defined on line %lu"), + Error(lindex, + _("duplicate definition of `%s'; first defined on line %lu"), name, rec->line); return rec; } @@ -323,7 +325,7 @@ symrec_parser_finalize_checksym(symrec *sym, /*@unused@*/ /*@null@*/ void *d) /* error if a symbol is used but never defined or extern/common declared */ if ((sym->status & SYM_USED) && !(sym->status & SYM_DEFINED) && !(sym->visibility & (SYM_EXTERN | SYM_COMMON))) { - ErrorAt(sym->line, _("undefined symbol `%s' (first use)"), sym->name); + Error(sym->line, _("undefined symbol `%s' (first use)"), sym->name); if (sym->line < firstundef_line) firstundef_line = sym->line; } @@ -337,8 +339,8 @@ symrec_parser_finalize(void) firstundef_line = ULONG_MAX; symrec_traverse(NULL, symrec_parser_finalize_checksym); if (firstundef_line < ULONG_MAX) - ErrorAt(firstundef_line, - _(" (Each undefined symbol is reported only once.)")); + Error(firstundef_line, + _(" (Each undefined symbol is reported only once.)")); } void @@ -389,9 +391,6 @@ symrec_print_all(FILE *f, int indent_level) void symrec_print(FILE *f, int indent_level, const symrec *sym) { - const char *filename; - unsigned long line; - switch (sym->type) { case SYM_UNKNOWN: fprintf(f, "%*s-Unknown (Common/Extern)-\n", indent_level, ""); @@ -451,7 +450,5 @@ symrec_print(FILE *f, int indent_level, const symrec *sym) fprintf(f, "%*sUNKNOWN\n", indent_level+1, ""); } - line_lookup(sym->line, &filename, &line); - fprintf(f, "%*sFilename=\"%s\" Line Number=%lu\n", indent_level, "", - filename, line); + fprintf(f, "%*sLine Index=%lu\n", indent_level, "", sym->line); } diff --git a/src/symrec.h b/src/symrec.h index 49ac2b91..47d148e8 100644 --- a/src/symrec.h +++ b/src/symrec.h @@ -22,9 +22,9 @@ #ifndef YASM_SYMREC_H #define YASM_SYMREC_H -/*@dependent@*/ symrec *symrec_use(const char *name); -/*@dependent@*/ symrec *symrec_define_equ(const char *name, - /*@keep@*/ expr *e); +/*@dependent@*/ symrec *symrec_use(const char *name, unsigned long lindex); +/*@dependent@*/ symrec *symrec_define_equ(const char *name, /*@keep@*/ expr *e, + unsigned long lindex); /* in_table specifies if the label should be inserted into the symbol table. * All labels are memory managed internally. */ @@ -32,8 +32,10 @@ /*@dependent@*/ /*@null@*/ section *sect, /*@dependent@*/ /*@null@*/ - bytecode *precbc, int in_table); -/*@dependent@*/ symrec *symrec_declare(const char *name, SymVisibility vis); + bytecode *precbc, int in_table, + unsigned long lindex); +/*@dependent@*/ symrec *symrec_declare(const char *name, SymVisibility vis, + unsigned long lindex); /*@observer@*/ const char *symrec_get_name(const symrec *sym); SymVisibility symrec_get_visibility(const symrec *sym); diff --git a/src/valparam.c b/src/valparam.c index 2339af9b..ddae81d2 100644 --- a/src/valparam.c +++ b/src/valparam.c @@ -22,7 +22,6 @@ #include "util.h" /*@unused@*/ RCSID("$IdPath$"); -#include "globals.h" #include "expr.h"