From: Peter Johnson Date: Wed, 9 Jan 2002 07:23:00 +0000 (-0000) Subject: Make resolve_label() take a symrec * instead of a section * and bytecode *. X-Git-Tag: v0.1.0~99 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e23d88b9573ff48fd725ed060f7bd063ca0b72fb;p=yasm Make resolve_label() take a symrec * instead of a section * and bytecode *. Extend expr_expand_equ() into expr_expand_labelequ() and call resolve_label() here. svn path=/trunk/yasm/; revision=440 --- diff --git a/libyasm/arch.h b/libyasm/arch.h index 48f04aad..408f44ea 100644 --- a/libyasm/arch.h +++ b/libyasm/arch.h @@ -41,8 +41,7 @@ struct arch { /* See bytecode.h comments on bc_calc_len() */ unsigned long (*bc_calc_len) (bytecode *bc, /*@only@*/ /*@null@*/ - intnum *(*resolve_label) (section *sect, - /*@null@*/ bytecode *bc)); + intnum *(*resolve_label) (symrec *sym)); } bc; }; diff --git a/libyasm/bytecode.c b/libyasm/bytecode.c index 225966ca..bed8f7d9 100644 --- a/libyasm/bytecode.c +++ b/libyasm/bytecode.c @@ -308,8 +308,7 @@ bc_print(FILE *f, const bytecode *bc) } unsigned long -bc_calc_len(bytecode *bc, - intnum *(*resolve_label) (section *sect, /*@null@*/ bytecode *bc)) +bc_calc_len(bytecode *bc, intnum *(*resolve_label) (symrec *sym)) { switch (bc->type) { case BC_EMPTY: diff --git a/libyasm/bytecode.h b/libyasm/bytecode.h index 3cb06718..12421142 100644 --- a/libyasm/bytecode.h +++ b/libyasm/bytecode.h @@ -64,8 +64,7 @@ void bc_print(FILE *f, const bytecode *bc); * in-file label (eg, not an EXTERN variable, which is indeterminate). */ unsigned long bc_calc_len(bytecode *bc, /*@only@*/ /*@null@*/ - intnum *(*resolve_label) (section *sect, - /*@null@*/ bytecode *bc)); + intnum *(*resolve_label) (symrec *sym)); /* void bcs_initialize(bytecodehead *headp); */ #define bcs_initialize(headp) STAILQ_INIT(headp) diff --git a/libyasm/expr.c b/libyasm/expr.c index be0ab2e4..ea206ad1 100644 --- a/libyasm/expr.c +++ b/libyasm/expr.c @@ -716,24 +716,37 @@ expr_contains(expr *e, ExprType t) return expr_traverse_leaves_in(e, &t, expr_contains_callback); } +/* NOTE: This can't be passed through *d because of data/function pointer + * portability issues. + */ +static intnum *(*labelequ_resolve_label) (symrec *sym); + static int -expr_expand_equ_callback(ExprItem *ei, /*@unused@*/ void *d) +expr_expand_labelequ_callback(ExprItem *ei, /*@unused@*/ void *d) { const expr *equ_expr; + intnum *intn; if (ei->type == EXPR_SYM) { equ_expr = symrec_get_equ(ei->data.sym); if (equ_expr) { ei->type = EXPR_EXPR; ei->data.expn = expr_copy(equ_expr); + } else { + intn = labelequ_resolve_label(ei->data.sym); + if (intn) { + ei->type = EXPR_INT; + ei->data.intn = intn; + } } } return 0; } void -expr_expand_equ(expr *e) +expr_expand_labelequ(expr *e, intnum *(*resolve_label) (symrec *sym)) { - expr_traverse_leaves_in(e, NULL, expr_expand_equ_callback); + labelequ_resolve_label = resolve_label; + expr_traverse_leaves_in(e, NULL, expr_expand_labelequ_callback); } /* Traverse over expression tree, calling func for each operation AFTER the diff --git a/libyasm/expr.h b/libyasm/expr.h index 4f7ee9bf..b8a696ae 100644 --- a/libyasm/expr.h +++ b/libyasm/expr.h @@ -46,9 +46,9 @@ typedef struct ExprItem ExprItem; void expr_delete(/*@only@*/ /*@null@*/ expr *e); /* Expands all (symrec) equ's in the expression into full expression - * instances. + * instances. Also resolves labels, if possible. */ -void expr_expand_equ(expr *e); +void expr_expand_labelequ(expr *e, intnum *(*resolve_label) (symrec *sym)); /* Simplifies the expression e as much as possible, eliminating extraneous * branches and simplifying integer-only subexpressions. diff --git a/modules/arch/x86/x86-int.h b/modules/arch/x86/x86-int.h index f8d79ed5..262e7673 100644 --- a/modules/arch/x86/x86-int.h +++ b/modules/arch/x86/x86-int.h @@ -97,8 +97,7 @@ typedef struct x86_jmprel { void x86_bc_delete(bytecode *bc); void x86_bc_print(FILE *f, const bytecode *bc); unsigned long x86_bc_calc_len(bytecode *bc, /*@only@*/ /*@null@*/ - intnum *(*resolve_label) (section *sect, - /*@null@*/ bytecode *bc)); + intnum *(*resolve_label) (symrec *sym)); int x86_expr_checkea(expr **ep, unsigned char *addrsize, unsigned char bits, unsigned char nosplit, unsigned char *displen, diff --git a/modules/arch/x86/x86bc.c b/modules/arch/x86/x86bc.c index 2f1602da..76b66119 100644 --- a/modules/arch/x86/x86bc.c +++ b/modules/arch/x86/x86bc.c @@ -462,8 +462,7 @@ x86_bc_print(FILE *f, const bytecode *bc) static unsigned long x86_bc_calc_len_insn(x86_insn *insn, /*@only@*/ /*@null@*/ - intnum *(*resolve_label) (section *sect, - /*@null@*/ bytecode *bc)) + intnum *(*resolve_label) (symrec *sym)) { effaddr *ea = insn->ea; x86_effaddr_data *ead = ea_get_data(ea); @@ -473,7 +472,7 @@ x86_bc_calc_len_insn(x86_insn *insn, /*@only@*/ /*@null@*/ if ((ea->disp) && ((!ead->valid_sib && ead->need_sib) || (!ead->valid_modrm && ead->need_modrm))) { /* First expand equ's */ - expr_expand_equ(ea->disp); + expr_expand_labelequ(ea->disp, resolve_label); /* Check validity of effective address and calc R/M bits of * Mod/RM byte and SIB byte. We won't know the Mod field @@ -492,7 +491,7 @@ x86_bc_calc_len_insn(x86_insn *insn, /*@only@*/ /*@null@*/ const intnum *num; if (imm->val) { - expr_expand_equ(imm->val); + expr_expand_labelequ(imm->val, resolve_label); imm->val = expr_simplify(imm->val); } /* TODO: check imm f_len vs. len? */ @@ -520,8 +519,7 @@ x86_bc_calc_len_insn(x86_insn *insn, /*@only@*/ /*@null@*/ unsigned long x86_bc_calc_len(bytecode *bc, - intnum *(*resolve_label) (section *sect, - /*@null@*/ bytecode *bc)) + intnum *(*resolve_label) (symrec *sym)) { x86_insn *insn; diff --git a/modules/optimizers/basic/basic-optimizer.c b/modules/optimizers/basic/basic-optimizer.c index 8a14b1f4..65ec6912 100644 --- a/modules/optimizers/basic/basic-optimizer.c +++ b/modules/optimizers/basic/basic-optimizer.c @@ -22,6 +22,8 @@ #include "util.h" RCSID("$IdPath$"); +#include "symrec.h" + #include "bytecode.h" #include "section.h" @@ -39,11 +41,11 @@ RCSID("$IdPath$"); #define BCFLAG_DONE (1<<1) static /*@only@*/ /*@null@*/ intnum * -basic_optimize_resolve_label(section *sect, bytecode *bc) +basic_optimize_resolve_label(symrec *sym) { unsigned long flags; - flags = section_get_opt_flags(sect); + flags = symrec_get_opt_flags(sym); return NULL; } diff --git a/src/arch.h b/src/arch.h index 48f04aad..408f44ea 100644 --- a/src/arch.h +++ b/src/arch.h @@ -41,8 +41,7 @@ struct arch { /* See bytecode.h comments on bc_calc_len() */ unsigned long (*bc_calc_len) (bytecode *bc, /*@only@*/ /*@null@*/ - intnum *(*resolve_label) (section *sect, - /*@null@*/ bytecode *bc)); + intnum *(*resolve_label) (symrec *sym)); } bc; }; diff --git a/src/arch/x86/x86-int.h b/src/arch/x86/x86-int.h index f8d79ed5..262e7673 100644 --- a/src/arch/x86/x86-int.h +++ b/src/arch/x86/x86-int.h @@ -97,8 +97,7 @@ typedef struct x86_jmprel { void x86_bc_delete(bytecode *bc); void x86_bc_print(FILE *f, const bytecode *bc); unsigned long x86_bc_calc_len(bytecode *bc, /*@only@*/ /*@null@*/ - intnum *(*resolve_label) (section *sect, - /*@null@*/ bytecode *bc)); + intnum *(*resolve_label) (symrec *sym)); int x86_expr_checkea(expr **ep, unsigned char *addrsize, unsigned char bits, unsigned char nosplit, unsigned char *displen, diff --git a/src/arch/x86/x86bc.c b/src/arch/x86/x86bc.c index 2f1602da..76b66119 100644 --- a/src/arch/x86/x86bc.c +++ b/src/arch/x86/x86bc.c @@ -462,8 +462,7 @@ x86_bc_print(FILE *f, const bytecode *bc) static unsigned long x86_bc_calc_len_insn(x86_insn *insn, /*@only@*/ /*@null@*/ - intnum *(*resolve_label) (section *sect, - /*@null@*/ bytecode *bc)) + intnum *(*resolve_label) (symrec *sym)) { effaddr *ea = insn->ea; x86_effaddr_data *ead = ea_get_data(ea); @@ -473,7 +472,7 @@ x86_bc_calc_len_insn(x86_insn *insn, /*@only@*/ /*@null@*/ if ((ea->disp) && ((!ead->valid_sib && ead->need_sib) || (!ead->valid_modrm && ead->need_modrm))) { /* First expand equ's */ - expr_expand_equ(ea->disp); + expr_expand_labelequ(ea->disp, resolve_label); /* Check validity of effective address and calc R/M bits of * Mod/RM byte and SIB byte. We won't know the Mod field @@ -492,7 +491,7 @@ x86_bc_calc_len_insn(x86_insn *insn, /*@only@*/ /*@null@*/ const intnum *num; if (imm->val) { - expr_expand_equ(imm->val); + expr_expand_labelequ(imm->val, resolve_label); imm->val = expr_simplify(imm->val); } /* TODO: check imm f_len vs. len? */ @@ -520,8 +519,7 @@ x86_bc_calc_len_insn(x86_insn *insn, /*@only@*/ /*@null@*/ unsigned long x86_bc_calc_len(bytecode *bc, - intnum *(*resolve_label) (section *sect, - /*@null@*/ bytecode *bc)) + intnum *(*resolve_label) (symrec *sym)) { x86_insn *insn; diff --git a/src/bytecode.c b/src/bytecode.c index 225966ca..bed8f7d9 100644 --- a/src/bytecode.c +++ b/src/bytecode.c @@ -308,8 +308,7 @@ bc_print(FILE *f, const bytecode *bc) } unsigned long -bc_calc_len(bytecode *bc, - intnum *(*resolve_label) (section *sect, /*@null@*/ bytecode *bc)) +bc_calc_len(bytecode *bc, intnum *(*resolve_label) (symrec *sym)) { switch (bc->type) { case BC_EMPTY: diff --git a/src/bytecode.h b/src/bytecode.h index 3cb06718..12421142 100644 --- a/src/bytecode.h +++ b/src/bytecode.h @@ -64,8 +64,7 @@ void bc_print(FILE *f, const bytecode *bc); * in-file label (eg, not an EXTERN variable, which is indeterminate). */ unsigned long bc_calc_len(bytecode *bc, /*@only@*/ /*@null@*/ - intnum *(*resolve_label) (section *sect, - /*@null@*/ bytecode *bc)); + intnum *(*resolve_label) (symrec *sym)); /* void bcs_initialize(bytecodehead *headp); */ #define bcs_initialize(headp) STAILQ_INIT(headp) diff --git a/src/expr.c b/src/expr.c index be0ab2e4..ea206ad1 100644 --- a/src/expr.c +++ b/src/expr.c @@ -716,24 +716,37 @@ expr_contains(expr *e, ExprType t) return expr_traverse_leaves_in(e, &t, expr_contains_callback); } +/* NOTE: This can't be passed through *d because of data/function pointer + * portability issues. + */ +static intnum *(*labelequ_resolve_label) (symrec *sym); + static int -expr_expand_equ_callback(ExprItem *ei, /*@unused@*/ void *d) +expr_expand_labelequ_callback(ExprItem *ei, /*@unused@*/ void *d) { const expr *equ_expr; + intnum *intn; if (ei->type == EXPR_SYM) { equ_expr = symrec_get_equ(ei->data.sym); if (equ_expr) { ei->type = EXPR_EXPR; ei->data.expn = expr_copy(equ_expr); + } else { + intn = labelequ_resolve_label(ei->data.sym); + if (intn) { + ei->type = EXPR_INT; + ei->data.intn = intn; + } } } return 0; } void -expr_expand_equ(expr *e) +expr_expand_labelequ(expr *e, intnum *(*resolve_label) (symrec *sym)) { - expr_traverse_leaves_in(e, NULL, expr_expand_equ_callback); + labelequ_resolve_label = resolve_label; + expr_traverse_leaves_in(e, NULL, expr_expand_labelequ_callback); } /* Traverse over expression tree, calling func for each operation AFTER the diff --git a/src/expr.h b/src/expr.h index 4f7ee9bf..b8a696ae 100644 --- a/src/expr.h +++ b/src/expr.h @@ -46,9 +46,9 @@ typedef struct ExprItem ExprItem; void expr_delete(/*@only@*/ /*@null@*/ expr *e); /* Expands all (symrec) equ's in the expression into full expression - * instances. + * instances. Also resolves labels, if possible. */ -void expr_expand_equ(expr *e); +void expr_expand_labelequ(expr *e, intnum *(*resolve_label) (symrec *sym)); /* Simplifies the expression e as much as possible, eliminating extraneous * branches and simplifying integer-only subexpressions. diff --git a/src/optimizers/basic/basic-optimizer.c b/src/optimizers/basic/basic-optimizer.c index 8a14b1f4..65ec6912 100644 --- a/src/optimizers/basic/basic-optimizer.c +++ b/src/optimizers/basic/basic-optimizer.c @@ -22,6 +22,8 @@ #include "util.h" RCSID("$IdPath$"); +#include "symrec.h" + #include "bytecode.h" #include "section.h" @@ -39,11 +41,11 @@ RCSID("$IdPath$"); #define BCFLAG_DONE (1<<1) static /*@only@*/ /*@null@*/ intnum * -basic_optimize_resolve_label(section *sect, bytecode *bc) +basic_optimize_resolve_label(symrec *sym) { unsigned long flags; - flags = section_get_opt_flags(sect); + flags = symrec_get_opt_flags(sym); return NULL; }