aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/tools/yasm/libyasm/expr.h
blob: 0de62dfed7bd97427f8fd9724a07581cdc2cd37d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/**
 * \file libyasm/expr.h
 * \brief YASM expression interface.
 *
 * \license
 *  Copyright (C) 2001-2007  Michael Urman, Peter Johnson
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *  - Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  - 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.
 * \endlicense
 */
#ifndef YASM_EXPR_H
#define YASM_EXPR_H

#ifndef YASM_LIB_DECL
#define YASM_LIB_DECL
#endif

/** 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
 * \param a         expression item a
 * \param b         expression item b (optional depending on op)
 * \param line      virtual line (where expression defined)
 * \return Newly allocated expression.
 */
YASM_LIB_DECL
/*@only@*/ yasm_expr *yasm_expr_create
    (yasm_expr_op op, /*@only@*/ yasm_expr__item *a,
     /*@only@*/ /*@null@*/ yasm_expr__item *b, unsigned long line);

/** Create a new preceding-bytecode expression item.
 * \param precbc    preceding bytecode
 * \return Newly allocated expression item.
 */
YASM_LIB_DECL
/*@only@*/ yasm_expr__item *yasm_expr_precbc(/*@keep@*/ yasm_bytecode *precbc);

/** Create a new symbol expression item.
 * \param sym       symbol
 * \return Newly allocated expression item.
 */
YASM_LIB_DECL
/*@only@*/ yasm_expr__item *yasm_expr_sym(/*@keep@*/ yasm_symrec *sym);

/** Create a new expression expression item.
 * \param e         expression
 * \return Newly allocated expression item.
 */
YASM_LIB_DECL
/*@only@*/ yasm_expr__item *yasm_expr_expr(/*@keep@*/ yasm_expr *e);

/** Create a new intnum expression item.
 * \param intn      intnum
 * \return Newly allocated expression item.
 */
YASM_LIB_DECL
/*@only@*/ yasm_expr__item *yasm_expr_int(/*@keep@*/ yasm_intnum *intn);

/** Create a new floatnum expression item.
 * \param flt       floatnum
 * \return Newly allocated expression item.
 */
YASM_LIB_DECL
/*@only@*/ yasm_expr__item *yasm_expr_float(/*@keep@*/ yasm_floatnum *flt);

/** Create a new register expression item.
 * \param reg       register
 * \return Newly allocated expression item.
 */
YASM_LIB_DECL
/*@only@*/ yasm_expr__item *yasm_expr_reg(uintptr_t reg);

/** Create a new expression tree e=l op r.
 * \param l     expression for left side of new expression
 * \param o     operation
 * \param r     expression for right side of new expression
 * \param i     line index
 * \return Newly allocated expression.
 */
#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 branch e=op r.
 * \param o     operation
 * \param r     expression for right side of new expression
 * \param i     line index
 * \return Newly allocated expression.
 */
#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 identity e=r.
 * \param r     expression for identity within new expression
 * \param i     line index
 * \return Newly allocated expression.
 */
#define yasm_expr_create_ident(r,i) \
    yasm_expr_create (YASM_EXPR_IDENT, (r), (yasm_expr__item *)NULL, i)

/** Duplicate an expression.
 * \param e     expression
 * \return Newly allocated expression identical to e.
 */
yasm_expr *yasm_expr_copy(const yasm_expr *e);
#ifndef YASM_DOXYGEN
#define yasm_expr_copy(e)   yasm_expr__copy_except(e, -1)
#endif

/** Destroy (free allocated memory for) an expression.
 * \param e     expression
 */
YASM_LIB_DECL
void yasm_expr_destroy(/*@only@*/ /*@null@*/ yasm_expr *e);

/** Determine if an expression is a specified operation (at the top level).
 * \param e             expression
 * \param op            operator
 * \return Nonzero if the expression was the specified operation at the top
 *         level, zero otherwise.
 */
YASM_LIB_DECL
int yasm_expr_is_op(const yasm_expr *e, yasm_expr_op op);

/** Extra transformation function for yasm_expr__level_tree().
 * \param e     expression being simplified
 * \param d     data provided as expr_xform_extra_data to
 *              yasm_expr__level_tree()
 * \return Transformed e.
 */
typedef /*@only@*/ yasm_expr * (*yasm_expr_xform_func)
    (/*@returned@*/ /*@only@*/ yasm_expr *e, /*@null@*/ void *d);

/** Level an entire expression tree.
 * \internal
 * \param e                 expression
 * \param fold_const        enable constant folding if nonzero
 * \param simplify_ident    simplify identities
 * \param simplify_reg_mul  simplify REG*1 identities
 * \param calc_bc_dist      nonzero if distances between bytecodes should be
 *                          calculated, 0 if they should be left intact
 * \param expr_xform_extra  extra transformation function
 * \param expr_xform_extra_data data to pass to expr_xform_extra
 * \return Leveled expression.
 */
YASM_LIB_DECL
/*@only@*/ /*@null@*/ yasm_expr *yasm_expr__level_tree
    (/*@returned@*/ /*@only@*/ /*@null@*/ yasm_expr *e, int fold_const,
     int simplify_ident, int simplify_reg_mul, int calc_bc_dist,
     /*@null@*/ yasm_expr_xform_func expr_xform_extra,
     /*@null@*/ void *expr_xform_extra_data);

/** Simplify an expression as much as possible.  Eliminates extraneous
 * branches and simplifies integer-only subexpressions.  Simplified version
 * of yasm_expr__level_tree().
 * \param e     expression
 * \param cbd   if distance between bytecodes should be calculated
 * \return Simplified expression.
 */
#define yasm_expr_simplify(e, cbd) \
    yasm_expr__level_tree(e, 1, 1, 1, cbd, NULL, NULL)

/** Extract the segment portion of an expression containing SEG:OFF, leaving
 * the offset.
 * \param ep            expression (pointer to)
 * \return 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
/*@only@*/ /*@null@*/ yasm_expr *yasm_expr_extract_deep_segoff(yasm_expr **ep);

/** Extract the segment portion of a SEG:OFF expression, leaving the offset.
 * \param ep            expression (pointer to)
 * \return 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
/*@only@*/ /*@null@*/ yasm_expr *yasm_expr_extract_segoff(yasm_expr **ep);

/** Extract the right portion (y) of a x WRT y expression, leaving the left
 * portion (x).
 * \param ep            expression (pointer to)
 * \return 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
/*@only@*/ /*@null@*/ yasm_expr *yasm_expr_extract_wrt(yasm_expr **ep);

/** Get the integer value of an expression if it's just an integer.
 * \param ep            expression (pointer to)
 * \param calc_bc_dist  nonzero if distances between bytecodes should be
 *                      calculated, 0 if NULL should be returned in this case
 * \return 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
/*@dependent@*/ /*@null@*/ yasm_intnum *yasm_expr_get_intnum
    (yasm_expr **ep, int calc_bc_dist);

/** Get the symbol value of an expression if it's just a symbol.
 * \param ep            expression (pointer to)
 * \param simplify      if nonzero, simplify the expression first
 * \return NULL if the expression is too complex; otherwise the symbol value of
 *         the expression.
 */
YASM_LIB_DECL
/*@dependent@*/ /*@null@*/ const yasm_symrec *yasm_expr_get_symrec
    (yasm_expr **ep, int simplify);

/** Get the register value of an expression if it's just a register.
 * \param ep            expression (pointer to)
 * \param simplify      if nonzero, simplify the expression first
 * \return NULL if the expression is too complex; otherwise the register value
 *         of the expression.
 */
YASM_LIB_DECL
/*@dependent@*/ /*@null@*/ const uintptr_t *yasm_expr_get_reg
    (yasm_expr **ep, int simplify);

/** Print an expression.  For debugging purposes.
 * \param e     expression
 * \param f     file
 */
YASM_LIB_DECL
void yasm_expr_print(/*@null@*/ const yasm_expr *e, FILE *f);

/** Return the size of an expression, if the user provided it
 * \param e     expression
 */
YASM_LIB_DECL
unsigned int yasm_expr_size(const yasm_expr *e);

/** Return the segment of an expression, if the user provided it
 * \param e     expression
 */
YASM_LIB_DECL
const char *yasm_expr_segment(const yasm_expr *e);

/** 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.
 */
YASM_LIB_DECL
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.
 */
YASM_LIB_DECL
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).
 */
YASM_LIB_DECL
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_LIB_DECL
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.
 */
YASM_LIB_DECL
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.
 */
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));

/** 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).
 */
YASM_LIB_DECL
int yasm_expr__subst(yasm_expr *e, unsigned int num_items,
                     const yasm_expr__item *items);

#endif