From: Peter Johnson Date: Sat, 14 Jul 2007 05:31:08 +0000 (-0000) Subject: Merge expr-int.h into expr.h, removing YASM_EXPR_INTERNAL in the process. X-Git-Tag: v0.6.2~8^2~17 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2bb74148c18a8821c95571ea30294a39ac5b903a;p=yasm Merge expr-int.h into expr.h, removing YASM_EXPR_INTERNAL in the process. svn path=/trunk/yasm/; revision=1895 --- diff --git a/libyasm.h b/libyasm.h index bd38c646..6d7778e9 100644 --- a/libyasm.h +++ b/libyasm.h @@ -30,10 +30,6 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * \endlicense - * - * Additional parts may be included via: - * - YASM_EXPR_INTERNAL: reveal expr internal structures via expr-int.h - * inclusion */ #ifndef YASM_LIB_H #define YASM_LIB_H @@ -77,9 +73,6 @@ typedef unsigned long uintptr_t; #include #include -#ifdef YASM_EXPR_INTERNAL -#include -#endif #include #include diff --git a/libyasm/Makefile.inc b/libyasm/Makefile.inc index ee84ac48..9e82a0e3 100644 --- a/libyasm/Makefile.inc +++ b/libyasm/Makefile.inc @@ -59,7 +59,6 @@ modinclude_HEADERS += libyasm/coretype.h modinclude_HEADERS += libyasm/dbgfmt.h modinclude_HEADERS += libyasm/errwarn.h modinclude_HEADERS += libyasm/expr.h -modinclude_HEADERS += libyasm/expr-int.h modinclude_HEADERS += libyasm/file.h modinclude_HEADERS += libyasm/floatnum.h modinclude_HEADERS += libyasm/hamt.h diff --git a/libyasm/bc-reserve.c b/libyasm/bc-reserve.c index 1bc0c888..32f524f9 100644 --- a/libyasm/bc-reserve.c +++ b/libyasm/bc-reserve.c @@ -37,8 +37,6 @@ #include "bytecode.h" -#include "expr-int.h" - typedef struct bytecode_reserve { /*@only@*/ /*@null@*/ yasm_expr *numitems; /* number of items to reserve */ diff --git a/libyasm/bytecode.c b/libyasm/bytecode.c index 3828795c..b8386f79 100644 --- a/libyasm/bytecode.c +++ b/libyasm/bytecode.c @@ -38,8 +38,6 @@ #include "bytecode.h" -#include "expr-int.h" - void yasm_bc_set_multiple(yasm_bytecode *bc, yasm_expr *e) diff --git a/libyasm/coretype.h b/libyasm/coretype.h index 395a9c43..18dab291 100644 --- a/libyasm/coretype.h +++ b/libyasm/coretype.h @@ -74,7 +74,7 @@ typedef struct yasm_assoc_data_callback { */ typedef struct yasm_errwarns yasm_errwarns; -/** Bytecode. \see bytecode.h for defails and related functions. */ +/** Bytecode. \see bytecode.h for details and related functions. */ typedef struct yasm_bytecode yasm_bytecode; /** Object. \see section.h for details and related functions. */ @@ -89,10 +89,7 @@ typedef struct yasm_symtab yasm_symtab; /** Symbol record (opaque type). \see symrec.h for related functions. */ typedef struct yasm_symrec yasm_symrec; -/** Expression (opaque type). - * \see expr.h for related functions. - * Define YASM_EXPR_INTERNAL to get visible internals. - */ +/** Expression. \see expr.h for details and related functions. */ typedef struct yasm_expr yasm_expr; /** Integer value (opaque type). \see intnum.h for related functions. */ typedef struct yasm_intnum yasm_intnum; diff --git a/libyasm/expr-int.h b/libyasm/expr-int.h deleted file mode 100644 index 6946adc5..00000000 --- a/libyasm/expr-int.h +++ /dev/null @@ -1,118 +0,0 @@ -/* $Id$ - * Expressions internal structures header file - * - * Copyright (C) 2001-2007 Peter Johnson - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS'' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -#ifndef YASM_EXPR_INT_H -#define YASM_EXPR_INT_H - -/* Types listed in canonical sorting order. See expr_order_terms(). - * Note precbc must be used carefully (in a-b pairs), as only symrecs can - * become the relative term in a #yasm_value. - */ -typedef enum yasm_expr__type { - YASM_EXPR_NONE = 0, - YASM_EXPR_REG = 1<<0, - YASM_EXPR_INT = 1<<1, - YASM_EXPR_SUBST = 1<<2, - YASM_EXPR_FLOAT = 1<<3, - YASM_EXPR_SYM = 1<<4, - YASM_EXPR_PRECBC = 1<<5, /* direct bytecode ref (rather than via symrec) */ - YASM_EXPR_EXPR = 1<<6 -} yasm_expr__type; - -struct yasm_expr__item { - yasm_expr__type type; - union { - yasm_bytecode *precbc; - yasm_symrec *sym; - yasm_expr *expn; - yasm_intnum *intn; - yasm_floatnum *flt; - uintptr_t reg; - unsigned int subst; - } data; -}; - -/* Some operations may allow more than two operand terms: - * ADD, MUL, OR, AND, XOR - */ -struct yasm_expr { - yasm_expr_op op; - unsigned long line; - int numterms; - yasm_expr__item terms[2]; /* structure may be extended to include more */ -}; - -/* Traverse over expression tree in order, calling func for each leaf - * (non-operation). The data pointer d is passed to each func call. - * - * Stops early (and returns 1) if func returns 1. Otherwise returns 0. - */ -int yasm_expr__traverse_leaves_in_const - (const yasm_expr *e, /*@null@*/ void *d, - int (*func) (/*@null@*/ const yasm_expr__item *ei, /*@null@*/ void *d)); -int yasm_expr__traverse_leaves_in - (yasm_expr *e, /*@null@*/ void *d, - int (*func) (/*@null@*/ yasm_expr__item *ei, /*@null@*/ void *d)); - -/* Reorder terms of e into canonical order. Only reorders if reordering - * doesn't change meaning of expression. (eg, doesn't reorder SUB). - * Canonical order: REG, INT, FLOAT, SYM, EXPR. - * Multiple terms of a single type are kept in the same order as in - * the original expression. - * NOTE: Only performs reordering on *one* level (no recursion). - */ -void yasm_expr__order_terms(yasm_expr *e); - -/* Copy entire expression EXCEPT for index "except" at *top level only*. */ -yasm_expr *yasm_expr__copy_except(const yasm_expr *e, int except); - -int yasm_expr__contains(const yasm_expr *e, yasm_expr__type t); - -/** Transform symrec-symrec terms in expression into YASM_EXPR_SUBST items. - * Calls the callback function for each symrec-symrec term. - * \param ep expression (pointer to) - * \param cbd callback data passed to callback function - * \param callback callback function: given subst index for bytecode - * pair, bytecode pair (bc2-bc1), and cbd (callback data) - * \return Number of transformations made. - */ -int yasm_expr__bc_dist_subst(yasm_expr **ep, void *cbd, - void (*callback) (unsigned int subst, - yasm_bytecode *precbc, - yasm_bytecode *precbc2, - void *cbd)); - -/** Substitute items into expr YASM_EXPR_SUBST items (by index). Items are - * copied, so caller is responsible for freeing array of items. - * \param e expression - * \param num_items number of items in items array - * \param items items array - * \return 1 on error (index out of range). - */ -int yasm_expr__subst(yasm_expr *e, unsigned int num_items, - const yasm_expr__item *items); - -#endif diff --git a/libyasm/expr.c b/libyasm/expr.c index 34800935..fe7cb9a9 100644 --- a/libyasm/expr.c +++ b/libyasm/expr.c @@ -42,8 +42,6 @@ #include "arch.h" -#include "expr-int.h" - static int expr_traverse_nodes_post(/*@null@*/ yasm_expr *e, /*@null@*/ void *d, @@ -1007,12 +1005,6 @@ yasm_expr__copy_except(const yasm_expr *e, int except) return n; } -yasm_expr * -yasm_expr_copy(const yasm_expr *e) -{ - return yasm_expr__copy_except(e, -1); -} - static void expr_delete_term(yasm_expr__item *term, int recurse) { diff --git a/libyasm/expr.h b/libyasm/expr.h index ca189d0a..694ba72e 100644 --- a/libyasm/expr.h +++ b/libyasm/expr.h @@ -34,8 +34,50 @@ #ifndef YASM_EXPR_H #define YASM_EXPR_H -/** Expression item (opaque type). \internal */ -typedef struct yasm_expr__item yasm_expr__item; +/** Type of an expression item. Types are listed in canonical sorting order. + * See expr_order_terms(). + * Note #YASM_EXPR_PRECBC must be used carefully (in a-b pairs), as only + * symrecs can become the relative term in a #yasm_value. + */ +typedef enum yasm_expr__type { + YASM_EXPR_NONE = 0, /**< Nothing */ + YASM_EXPR_REG = 1<<0, /**< Register */ + YASM_EXPR_INT = 1<<1, /**< Integer value */ + YASM_EXPR_SUBST = 1<<2, /**< Substitution placeholder */ + YASM_EXPR_FLOAT = 1<<3, /**< Floating point value */ + YASM_EXPR_SYM = 1<<4, /**< Symbol */ + YASM_EXPR_PRECBC = 1<<5,/**< Direct bytecode ref (rather than via sym) */ + YASM_EXPR_EXPR = 1<<6 /**< Subexpression */ +} yasm_expr__type; + +/** Expression item. */ +typedef struct yasm_expr__item { + yasm_expr__type type; /**< Type */ + + /** Expression item data. Correct value depends on type. */ + union { + yasm_bytecode *precbc; /**< Direct bytecode ref (#YASM_EXPR_PRECBC) */ + yasm_symrec *sym; /**< Symbol (#YASM_EXPR_SYM) */ + yasm_expr *expn; /**< Subexpression (#YASM_EXPR_EXPR) */ + yasm_intnum *intn; /**< Integer value (#YASM_EXPR_INT) */ + yasm_floatnum *flt; /**< Floating point value (#YASM_EXPR_FLOAT) */ + uintptr_t reg; /**< Register (#YASM_EXPR_REG) */ + unsigned int subst; /**< Subst placeholder (#YASM_EXPR_SUBST) */ + } data; +} yasm_expr__item; + +/** Expression. */ +struct yasm_expr { + yasm_expr_op op; /**< Operation. */ + unsigned long line; /**< Line number where expression was defined. */ + int numterms; /**< Number of terms in the expression. */ + + /** Terms of the expression. Structure may be extended to include more + * terms, as some operations may allow more than two operand terms + * (ADD, MUL, OR, AND, XOR). + */ + yasm_expr__item terms[2]; +}; /** Create a new expression e=a op b. * \param op operation @@ -116,6 +158,7 @@ typedef struct yasm_expr__item yasm_expr__item; * \return Newly allocated expression identical to e. */ yasm_expr *yasm_expr_copy(const yasm_expr *e); +#define yasm_expr_copy(e) yasm_expr__copy_except(e, -1) /** Destroy (free allocated memory for) an expression. * \param e expression @@ -231,4 +274,77 @@ typedef /*@only@*/ yasm_expr * (*yasm_expr_xform_func) */ void yasm_expr_print(/*@null@*/ const yasm_expr *e, FILE *f); +/** Traverse over expression tree in order (const version). + * Calls func for each leaf (non-operation). + * \param e expression + * \param d data passed to each call to func + * \param func callback function + * \return Stops early (and returns 1) if func returns 1. + * Otherwise returns 0. + */ +int yasm_expr__traverse_leaves_in_const + (const yasm_expr *e, /*@null@*/ void *d, + int (*func) (/*@null@*/ const yasm_expr__item *ei, /*@null@*/ void *d)); + +/** Traverse over expression tree in order. + * Calls func for each leaf (non-operation). + * \param e expression + * \param d data passed to each call to func + * \param func callback function + * \return Stops early (and returns 1) if func returns 1. + * Otherwise returns 0. + */ +int yasm_expr__traverse_leaves_in + (yasm_expr *e, /*@null@*/ void *d, + int (*func) (/*@null@*/ yasm_expr__item *ei, /*@null@*/ void *d)); + +/** Reorder terms of e into canonical order. Only reorders if reordering + * doesn't change meaning of expression. (eg, doesn't reorder SUB). + * Canonical order: REG, INT, FLOAT, SYM, EXPR. + * Multiple terms of a single type are kept in the same order as in + * the original expression. + * \param e expression + * \note Only performs reordering on *one* level (no recursion). + */ +void yasm_expr__order_terms(yasm_expr *e); + +/** Copy entire expression EXCEPT for index "except" at *top level only*. + * \param e expression + * \param except term index not to copy; -1 to copy all terms + * \return Newly allocated copy of expression. + */ +yasm_expr *yasm_expr__copy_except(const yasm_expr *e, int except); + +/** Test if expression contains an item. Searches recursively into + * subexpressions. + * \param e expression + * \param t type of item to look for + * \return Nonzero if expression contains an item of type t, zero if not. + */ +int yasm_expr__contains(const yasm_expr *e, yasm_expr__type t); + +/** Transform symrec-symrec terms in expression into #YASM_EXPR_SUBST items. + * Calls the callback function for each symrec-symrec term. + * \param ep expression (pointer to) + * \param cbd callback data passed to callback function + * \param callback callback function: given subst index for bytecode + * pair, bytecode pair (bc2-bc1), and cbd (callback data) + * \return Number of transformations made. + */ +int yasm_expr__bc_dist_subst(yasm_expr **ep, void *cbd, + void (*callback) (unsigned int subst, + yasm_bytecode *precbc, + yasm_bytecode *precbc2, + void *cbd)); + +/** Substitute items into expr YASM_EXPR_SUBST items (by index). Items are + * copied, so caller is responsible for freeing array of items. + * \param e expression + * \param num_items number of items in items array + * \param items items array + * \return 1 on error (index out of range). + */ +int yasm_expr__subst(yasm_expr *e, unsigned int num_items, + const yasm_expr__item *items); + #endif diff --git a/libyasm/section.c b/libyasm/section.c index 21ea56a9..c5b83cb8 100644 --- a/libyasm/section.c +++ b/libyasm/section.c @@ -49,8 +49,6 @@ #include "dbgfmt.h" #include "objfmt.h" -#include "expr-int.h" - #include "inttree.h" diff --git a/libyasm/value.c b/libyasm/value.c index 2fcabf49..e3eda5fd 100644 --- a/libyasm/value.c +++ b/libyasm/value.c @@ -43,7 +43,6 @@ #include "arch.h" -#include "expr-int.h" void yasm_value_initialize(/*@out@*/ yasm_value *value, diff --git a/modules/arch/lc3b/lc3bid.re b/modules/arch/lc3b/lc3bid.re index bfdb4c0b..2529664a 100644 --- a/modules/arch/lc3b/lc3bid.re +++ b/modules/arch/lc3b/lc3bid.re @@ -27,7 +27,6 @@ #include RCSID("$Id$"); -#define YASM_EXPR_INTERNAL #include #include "modules/arch/lc3b/lc3barch.h" diff --git a/modules/arch/x86/x86bc.c b/modules/arch/x86/x86bc.c index 5a73e5de..6dd31110 100644 --- a/modules/arch/x86/x86bc.c +++ b/modules/arch/x86/x86bc.c @@ -27,7 +27,6 @@ #include /*@unused@*/ RCSID("$Id$"); -#define YASM_EXPR_INTERNAL #include #include "x86arch.h" diff --git a/modules/arch/x86/x86expr.c b/modules/arch/x86/x86expr.c index ae99f7d2..8d11b1ab 100644 --- a/modules/arch/x86/x86expr.c +++ b/modules/arch/x86/x86expr.c @@ -27,7 +27,6 @@ #include /*@unused@*/ RCSID("$Id$"); -#define YASM_EXPR_INTERNAL #include #include "x86arch.h" diff --git a/modules/arch/x86/x86id.c b/modules/arch/x86/x86id.c index 69e609a5..278cb2df 100644 --- a/modules/arch/x86/x86id.c +++ b/modules/arch/x86/x86id.c @@ -28,7 +28,6 @@ #include RCSID("$Id$"); -#define YASM_EXPR_INTERNAL #include #include diff --git a/modules/objfmts/bin/bin-objfmt.c b/modules/objfmts/bin/bin-objfmt.c index 56515726..7056f97a 100644 --- a/modules/objfmts/bin/bin-objfmt.c +++ b/modules/objfmts/bin/bin-objfmt.c @@ -27,7 +27,6 @@ #include /*@unused@*/ RCSID("$Id$"); -#define YASM_EXPR_INTERNAL #include diff --git a/modules/objfmts/coff/coff-objfmt.c b/modules/objfmts/coff/coff-objfmt.c index de31df71..52f1e4aa 100644 --- a/modules/objfmts/coff/coff-objfmt.c +++ b/modules/objfmts/coff/coff-objfmt.c @@ -28,7 +28,6 @@ #include /*@unused@*/ RCSID("$Id$"); -#define YASM_EXPR_INTERNAL #include #include "coff-objfmt.h" diff --git a/modules/objfmts/coff/win64-except.c b/modules/objfmts/coff/win64-except.c index cbda39d3..ea703812 100644 --- a/modules/objfmts/coff/win64-except.c +++ b/modules/objfmts/coff/win64-except.c @@ -27,7 +27,6 @@ #include /*@unused@*/ RCSID("$Id$"); -#define YASM_EXPR_INTERNAL #include #include "coff-objfmt.h" diff --git a/modules/objfmts/elf/elf-objfmt.c b/modules/objfmts/elf/elf-objfmt.c index 5f726eac..664d1351 100644 --- a/modules/objfmts/elf/elf-objfmt.c +++ b/modules/objfmts/elf/elf-objfmt.c @@ -44,7 +44,6 @@ * Each Section is spatially disjoint, and has exactly one SHT entry. */ -#define YASM_EXPR_INTERNAL #include #include "elf.h" diff --git a/modules/objfmts/elf/elf-x86-amd64.c b/modules/objfmts/elf/elf-x86-amd64.c index 559fb50c..ce0cdda1 100644 --- a/modules/objfmts/elf/elf-x86-amd64.c +++ b/modules/objfmts/elf/elf-x86-amd64.c @@ -28,7 +28,6 @@ #include /*@unused@*/ RCSID("$Id$"); -#define YASM_EXPR_INTERNAL #include #define YASM_OBJFMT_ELF_INTERNAL #include "elf.h" diff --git a/modules/objfmts/elf/elf-x86-x86.c b/modules/objfmts/elf/elf-x86-x86.c index d639b599..827c861e 100644 --- a/modules/objfmts/elf/elf-x86-x86.c +++ b/modules/objfmts/elf/elf-x86-x86.c @@ -28,7 +28,6 @@ #include /*@unused@*/ RCSID("$Id$"); -#define YASM_EXPR_INTERNAL #include #define YASM_OBJFMT_ELF_INTERNAL #include "elf.h" diff --git a/modules/objfmts/elf/elf.c b/modules/objfmts/elf/elf.c index e3b23276..f187b9e7 100644 --- a/modules/objfmts/elf/elf.c +++ b/modules/objfmts/elf/elf.c @@ -28,7 +28,6 @@ #include /*@unused@*/ RCSID("$Id$"); -#define YASM_EXPR_INTERNAL #include #define YASM_OBJFMT_ELF_INTERNAL #include "elf.h" diff --git a/modules/objfmts/macho/macho-objfmt.c b/modules/objfmts/macho/macho-objfmt.c index d1462829..3f13948f 100644 --- a/modules/objfmts/macho/macho-objfmt.c +++ b/modules/objfmts/macho/macho-objfmt.c @@ -108,7 +108,6 @@ /* optional: automatically prefix underscores to global exported symbols */ /*#define AUTO_UNDERSCORE*/ -#define YASM_EXPR_INTERNAL #include /* MACH-O DEFINES */ diff --git a/modules/objfmts/rdf/rdf-objfmt.c b/modules/objfmts/rdf/rdf-objfmt.c index 6a0f6923..2b96bed3 100644 --- a/modules/objfmts/rdf/rdf-objfmt.c +++ b/modules/objfmts/rdf/rdf-objfmt.c @@ -27,7 +27,6 @@ #include /*@unused@*/ RCSID("$Id$"); -#define YASM_EXPR_INTERNAL #include diff --git a/modules/objfmts/xdf/xdf-objfmt.c b/modules/objfmts/xdf/xdf-objfmt.c index f68237e6..4ce2f3ef 100644 --- a/modules/objfmts/xdf/xdf-objfmt.c +++ b/modules/objfmts/xdf/xdf-objfmt.c @@ -27,7 +27,6 @@ #include /*@unused@*/ RCSID("$Id$"); -#define YASM_EXPR_INTERNAL #include diff --git a/modules/parsers/gas/gas-parse.c b/modules/parsers/gas/gas-parse.c index 6e79ec1c..6f92ebb2 100644 --- a/modules/parsers/gas/gas-parse.c +++ b/modules/parsers/gas/gas-parse.c @@ -30,7 +30,6 @@ #include RCSID("$Id$"); -#define YASM_EXPR_INTERNAL #include #include diff --git a/modules/parsers/nasm/nasm-parse.c b/modules/parsers/nasm/nasm-parse.c index 5ef7c11f..b449dd1c 100644 --- a/modules/parsers/nasm/nasm-parse.c +++ b/modules/parsers/nasm/nasm-parse.c @@ -27,7 +27,6 @@ #include RCSID("$Id$"); -#define YASM_EXPR_INTERNAL #include #include