libyasm
Data Structures | Macros | Typedefs | Enumerations | Functions
expr.h File Reference

YASM expression interface. More...

Go to the source code of this file.

Data Structures

struct  yasm_expr__item
 Expression item. More...
 
struct  yasm_expr
 Expression. More...
 

Macros

#define YASM_LIB_DECL
 
#define yasm_expr_create_tree(l, o, r, i)   yasm_expr_create ((o), yasm_expr_expr(l), yasm_expr_expr(r), i)
 Create a new expression tree e=l op r. More...
 
#define yasm_expr_create_branch(o, r, i)   yasm_expr_create ((o), yasm_expr_expr(r), (yasm_expr__item *)NULL, i)
 Create a new expression branch e=op r. More...
 
#define yasm_expr_create_ident(r, i)   yasm_expr_create (YASM_EXPR_IDENT, (r), (yasm_expr__item *)NULL, i)
 Create a new expression identity e=r. More...
 
#define yasm_expr_simplify(e, cbd)   yasm_expr__level_tree(e, 1, 1, 1, cbd, NULL, NULL)
 Simplify an expression as much as possible. More...
 

Typedefs

typedef enum yasm_expr__type yasm_expr__type
 Type of an expression item. More...
 
typedef struct yasm_expr__item yasm_expr__item
 Expression item. More...
 
typedef yasm_expr *(* yasm_expr_xform_func )(yasm_expr *e, void *d)
 Extra transformation function for yasm_expr__level_tree(). More...
 

Enumerations

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, YASM_EXPR_EXPR = 1<<6
}
 Type of an expression item. More...
 

Functions

YASM_LIB_DECL yasm_expryasm_expr_create (yasm_expr_op op, yasm_expr__item *a, yasm_expr__item *b, unsigned long line)
 Create a new expression e=a op b. More...
 
YASM_LIB_DECL yasm_expr__itemyasm_expr_precbc (yasm_bytecode *precbc)
 Create a new preceding-bytecode expression item. More...
 
YASM_LIB_DECL yasm_expr__itemyasm_expr_sym (yasm_symrec *sym)
 Create a new symbol expression item. More...
 
YASM_LIB_DECL yasm_expr__itemyasm_expr_expr (yasm_expr *e)
 Create a new expression expression item. More...
 
YASM_LIB_DECL yasm_expr__itemyasm_expr_int (yasm_intnum *intn)
 Create a new intnum expression item. More...
 
YASM_LIB_DECL yasm_expr__itemyasm_expr_float (yasm_floatnum *flt)
 Create a new floatnum expression item. More...
 
YASM_LIB_DECL yasm_expr__itemyasm_expr_reg (uintptr_t reg)
 Create a new register expression item. More...
 
yasm_expryasm_expr_copy (const yasm_expr *e)
 Duplicate an expression. More...
 
YASM_LIB_DECL void yasm_expr_destroy (yasm_expr *e)
 Destroy (free allocated memory for) an expression. More...
 
YASM_LIB_DECL int yasm_expr_is_op (const yasm_expr *e, yasm_expr_op op)
 Determine if an expression is a specified operation (at the top level). More...
 
YASM_LIB_DECL yasm_expryasm_expr__level_tree (yasm_expr *e, int fold_const, int simplify_ident, int simplify_reg_mul, int calc_bc_dist, yasm_expr_xform_func expr_xform_extra, void *expr_xform_extra_data)
 Level an entire expression tree. More...
 
YASM_LIB_DECL yasm_expryasm_expr_extract_deep_segoff (yasm_expr **ep)
 Extract the segment portion of an expression containing SEG:OFF, leaving the offset. More...
 
YASM_LIB_DECL yasm_expryasm_expr_extract_segoff (yasm_expr **ep)
 Extract the segment portion of a SEG:OFF expression, leaving the offset. More...
 
YASM_LIB_DECL yasm_expryasm_expr_extract_wrt (yasm_expr **ep)
 Extract the right portion (y) of a x WRT y expression, leaving the left portion (x). More...
 
YASM_LIB_DECL yasm_intnumyasm_expr_get_intnum (yasm_expr **ep, int calc_bc_dist)
 Get the integer value of an expression if it's just an integer. More...
 
YASM_LIB_DECL const yasm_symrecyasm_expr_get_symrec (yasm_expr **ep, int simplify)
 Get the symbol value of an expression if it's just a symbol. More...
 
YASM_LIB_DECL const uintptr_t * yasm_expr_get_reg (yasm_expr **ep, int simplify)
 Get the register value of an expression if it's just a register. More...
 
YASM_LIB_DECL void yasm_expr_print (const yasm_expr *e, FILE *f)
 Print an expression. More...
 
YASM_LIB_DECL unsigned int yasm_expr_size (const yasm_expr *e)
 Return the size of an expression, if the user provided it. More...
 
YASM_LIB_DECL const char * yasm_expr_segment (const yasm_expr *e)
 Return the segment of an expression, if the user provided it. More...
 
YASM_LIB_DECL int yasm_expr__traverse_leaves_in_const (const yasm_expr *e, void *d, int(*func)(const yasm_expr__item *ei, void *d))
 Traverse over expression tree in order (const version). More...
 
YASM_LIB_DECL int yasm_expr__traverse_leaves_in (yasm_expr *e, void *d, int(*func)(yasm_expr__item *ei, void *d))
 Traverse over expression tree in order. More...
 
YASM_LIB_DECL void yasm_expr__order_terms (yasm_expr *e)
 Reorder terms of e into canonical order. More...
 
YASM_LIB_DECL yasm_expryasm_expr__copy_except (const yasm_expr *e, int except)
 Copy entire expression EXCEPT for index "except" at top level only. More...
 
YASM_LIB_DECL int yasm_expr__contains (const yasm_expr *e, yasm_expr__type t)
 Test if expression contains an item. More...
 
YASM_LIB_DECL int yasm_expr__bc_dist_subst (yasm_expr **ep, void *cbd, void(*callback)(unsigned int subst, yasm_bytecode *precbc, yasm_bytecode *precbc2, void *cbd))
 Transform symrec-symrec terms in expression into YASM_EXPR_SUBST items. More...
 
YASM_LIB_DECL int yasm_expr__subst (yasm_expr *e, unsigned int num_items, const yasm_expr__item *items)
 Substitute items into expr YASM_EXPR_SUBST items (by index). More...
 

Detailed Description

YASM expression interface.

Definition in file expr.h.

Macro Definition Documentation

#define yasm_expr_create_branch (   o,
  r,
 
)    yasm_expr_create ((o), yasm_expr_expr(r), (yasm_expr__item *)NULL, i)

Create a new expression branch e=op r.

Parameters
ooperation
rexpression for right side of new expression
iline index
Returns
Newly allocated expression.

Definition at line 152 of file expr.h.

#define yasm_expr_create_ident (   r,
 
)    yasm_expr_create (YASM_EXPR_IDENT, (r), (yasm_expr__item *)NULL, i)

Create a new expression identity e=r.

Parameters
rexpression for identity within new expression
iline index
Returns
Newly allocated expression.

Definition at line 160 of file expr.h.

#define yasm_expr_create_tree (   l,
  o,
  r,
 
)    yasm_expr_create ((o), yasm_expr_expr(l), yasm_expr_expr(r), i)

Create a new expression tree e=l op r.

Parameters
lexpression for left side of new expression
ooperation
rexpression for right side of new expression
iline index
Returns
Newly allocated expression.

Definition at line 143 of file expr.h.

#define yasm_expr_simplify (   e,
  cbd 
)    yasm_expr__level_tree(e, 1, 1, 1, cbd, NULL, NULL)

Simplify an expression as much as possible.

Eliminates extraneous branches and simplifies integer-only subexpressions. Simplified version of yasm_expr__level_tree().

Parameters
eexpression
cbdif distance between bytecodes should be calculated
Returns
Simplified expression.

Definition at line 222 of file expr.h.

Typedef Documentation

Expression 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 yasm_expr*(* yasm_expr_xform_func)(yasm_expr *e,void *d)

Extra transformation function for yasm_expr__level_tree().

Parameters
eexpression being simplified
ddata provided as expr_xform_extra_data to yasm_expr__level_tree()
Returns
Transformed e.

Definition at line 194 of file expr.h.

Enumeration Type Documentation

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.

Enumerator
YASM_EXPR_NONE 

Nothing.

YASM_EXPR_REG 

Register.

YASM_EXPR_INT 

Integer value.

YASM_EXPR_SUBST 

Substitution placeholder.

YASM_EXPR_FLOAT 

Floating point value.

YASM_EXPR_SYM 

Symbol.

YASM_EXPR_PRECBC 

Direct bytecode ref (rather than via sym)

YASM_EXPR_EXPR 

Subexpression.

Definition at line 42 of file expr.h.

Function Documentation

YASM_LIB_DECL int yasm_expr__bc_dist_subst ( yasm_expr **  ep,
void *  cbd,
void(*)(unsigned int subst, yasm_bytecode *precbc, yasm_bytecode *precbc2, void *cbd)  callback 
)

Transform symrec-symrec terms in expression into YASM_EXPR_SUBST items.

Calls the callback function for each symrec-symrec term.

Parameters
epexpression (pointer to)
cbdcallback data passed to callback function
callbackcallback function: given subst index for bytecode pair, bytecode pair (bc2-bc1), and cbd (callback data)
Returns
Number of transformations made.
YASM_LIB_DECL int yasm_expr__contains ( const yasm_expr e,
yasm_expr__type  t 
)

Test if expression contains an item.

Searches recursively into subexpressions.

Parameters
eexpression
ttype of item to look for
Returns
Nonzero if expression contains an item of type t, zero if not.
YASM_LIB_DECL yasm_expr* yasm_expr__copy_except ( const yasm_expr e,
int  except 
)

Copy entire expression EXCEPT for index "except" at top level only.

Parameters
eexpression
exceptterm index not to copy; -1 to copy all terms
Returns
Newly allocated copy of expression.
YASM_LIB_DECL yasm_expr* yasm_expr__level_tree ( yasm_expr e,
int  fold_const,
int  simplify_ident,
int  simplify_reg_mul,
int  calc_bc_dist,
yasm_expr_xform_func  expr_xform_extra,
void *  expr_xform_extra_data 
)

Level an entire expression tree.

Parameters
eexpression
fold_constenable constant folding if nonzero
simplify_identsimplify identities
simplify_reg_mulsimplify REG*1 identities
calc_bc_distnonzero if distances between bytecodes should be calculated, 0 if they should be left intact
expr_xform_extraextra transformation function
expr_xform_extra_datadata to pass to expr_xform_extra
Returns
Leveled expression.
YASM_LIB_DECL void yasm_expr__order_terms ( yasm_expr e)

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.

Parameters
eexpression
Note
Only performs reordering on one level (no recursion).
YASM_LIB_DECL int yasm_expr__subst ( yasm_expr e,
unsigned int  num_items,
const yasm_expr__item items 
)

Substitute items into expr YASM_EXPR_SUBST items (by index).

Items are copied, so caller is responsible for freeing array of items.

Parameters
eexpression
num_itemsnumber of items in items array
itemsitems array
Returns
1 on error (index out of range).
YASM_LIB_DECL int yasm_expr__traverse_leaves_in ( yasm_expr e,
void *  d,
int(*)(yasm_expr__item *ei, void *d)  func 
)

Traverse over expression tree in order.

Calls func for each leaf (non-operation).

Parameters
eexpression
ddata passed to each call to func
funccallback function
Returns
Stops early (and returns 1) if func returns 1. Otherwise returns 0.
YASM_LIB_DECL int yasm_expr__traverse_leaves_in_const ( const yasm_expr e,
void *  d,
int(*)(const yasm_expr__item *ei, void *d)  func 
)

Traverse over expression tree in order (const version).

Calls func for each leaf (non-operation).

Parameters
eexpression
ddata passed to each call to func
funccallback function
Returns
Stops early (and returns 1) if func returns 1. Otherwise returns 0.
yasm_expr* yasm_expr_copy ( const yasm_expr e)

Duplicate an expression.

Parameters
eexpression
Returns
Newly allocated expression identical to e.
YASM_LIB_DECL yasm_expr* yasm_expr_create ( yasm_expr_op  op,
yasm_expr__item a,
yasm_expr__item b,
unsigned long  line 
)

Create a new expression e=a op b.

Parameters
opoperation
aexpression item a
bexpression item b (optional depending on op)
linevirtual line (where expression defined)
Returns
Newly allocated expression.
YASM_LIB_DECL void yasm_expr_destroy ( yasm_expr e)

Destroy (free allocated memory for) an expression.

Parameters
eexpression
YASM_LIB_DECL yasm_expr__item* yasm_expr_expr ( yasm_expr e)

Create a new expression expression item.

Parameters
eexpression
Returns
Newly allocated expression item.
YASM_LIB_DECL yasm_expr* yasm_expr_extract_deep_segoff ( yasm_expr **  ep)

Extract the segment portion of an expression containing SEG:OFF, leaving the offset.

Parameters
epexpression (pointer to)
Returns
NULL if unable to extract a segment (expr does not contain a YASM_EXPR_SEGOFF operator), otherwise the segment expression. The input expression is modified such that on return, it's the offset expression.
YASM_LIB_DECL yasm_expr* yasm_expr_extract_segoff ( yasm_expr **  ep)

Extract the segment portion of a SEG:OFF expression, leaving the offset.

Parameters
epexpression (pointer to)
Returns
NULL if unable to extract a segment (YASM_EXPR_SEGOFF not the top-level operator), otherwise the segment expression. The input expression is modified such that on return, it's the offset expression.
YASM_LIB_DECL yasm_expr* yasm_expr_extract_wrt ( yasm_expr **  ep)

Extract the right portion (y) of a x WRT y expression, leaving the left portion (x).

Parameters
epexpression (pointer to)
Returns
NULL if unable to extract (YASM_EXPR_WRT not the top-level operator), otherwise the right side of the WRT expression. The input expression is modified such that on return, it's the left side of the WRT expression.
YASM_LIB_DECL yasm_expr__item* yasm_expr_float ( yasm_floatnum flt)

Create a new floatnum expression item.

Parameters
fltfloatnum
Returns
Newly allocated expression item.
YASM_LIB_DECL yasm_intnum* yasm_expr_get_intnum ( yasm_expr **  ep,
int  calc_bc_dist 
)

Get the integer value of an expression if it's just an integer.

Parameters
epexpression (pointer to)
calc_bc_distnonzero if distances between bytecodes should be calculated, 0 if NULL should be returned in this case
Returns
NULL if the expression is too complex (contains anything other than integers, ie floats, non-valued labels, registers); otherwise the intnum value of the expression.
YASM_LIB_DECL const uintptr_t* yasm_expr_get_reg ( yasm_expr **  ep,
int  simplify 
)

Get the register value of an expression if it's just a register.

Parameters
epexpression (pointer to)
simplifyif nonzero, simplify the expression first
Returns
NULL if the expression is too complex; otherwise the register value of the expression.
YASM_LIB_DECL const yasm_symrec* yasm_expr_get_symrec ( yasm_expr **  ep,
int  simplify 
)

Get the symbol value of an expression if it's just a symbol.

Parameters
epexpression (pointer to)
simplifyif nonzero, simplify the expression first
Returns
NULL if the expression is too complex; otherwise the symbol value of the expression.
YASM_LIB_DECL yasm_expr__item* yasm_expr_int ( yasm_intnum intn)

Create a new intnum expression item.

Parameters
intnintnum
Returns
Newly allocated expression item.
YASM_LIB_DECL int yasm_expr_is_op ( const yasm_expr e,
yasm_expr_op  op 
)

Determine if an expression is a specified operation (at the top level).

Parameters
eexpression
opoperator
Returns
Nonzero if the expression was the specified operation at the top level, zero otherwise.
YASM_LIB_DECL yasm_expr__item* yasm_expr_precbc ( yasm_bytecode precbc)

Create a new preceding-bytecode expression item.

Parameters
precbcpreceding bytecode
Returns
Newly allocated expression item.
YASM_LIB_DECL void yasm_expr_print ( const yasm_expr e,
FILE *  f 
)

Print an expression.

For debugging purposes.

Parameters
eexpression
ffile
YASM_LIB_DECL yasm_expr__item* yasm_expr_reg ( uintptr_t  reg)

Create a new register expression item.

Parameters
regregister
Returns
Newly allocated expression item.
YASM_LIB_DECL const char* yasm_expr_segment ( const yasm_expr e)

Return the segment of an expression, if the user provided it.

Parameters
eexpression
YASM_LIB_DECL unsigned int yasm_expr_size ( const yasm_expr e)

Return the size of an expression, if the user provided it.

Parameters
eexpression
YASM_LIB_DECL yasm_expr__item* yasm_expr_sym ( yasm_symrec sym)

Create a new symbol expression item.

Parameters
symsymbol
Returns
Newly allocated expression item.