4 * Copyright (C) 2001-2007 Michael Urman, Peter Johnson
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
29 #include "libyasm-stdint.h"
45 static /*@only@*/ yasm_expr *expr_level_op
46 (/*@returned@*/ /*@only@*/ yasm_expr *e, int fold_const,
47 int simplify_ident, int simplify_reg_mul);
48 static int expr_traverse_nodes_post(/*@null@*/ yasm_expr *e,
50 int (*func) (/*@null@*/ yasm_expr *e,
52 static void expr_delete_term(yasm_expr__item *term, int recurse);
54 /* Bitmap of used items. We should really never need more than 2 at a time,
55 * so 31 is pretty much overkill.
57 static unsigned long itempool_used = 0;
58 static yasm_expr__item itempool[31];
60 /* allocate a new expression node, with children as defined.
61 * If it's a unary operator, put the element in left and set right=NULL. */
64 yasm_expr_create(yasm_expr_op op, yasm_expr__item *left,
65 yasm_expr__item *right, unsigned long line)
67 yasm_expr *ptr, *sube;
69 ptr = yasm_xmalloc(sizeof(yasm_expr));
73 ptr->terms[0].type = YASM_EXPR_NONE;
74 ptr->terms[1].type = YASM_EXPR_NONE;
76 ptr->terms[0] = *left; /* structure copy */
77 z = (unsigned long)(left-itempool);
79 yasm_internal_error(N_("could not find expritem in pool"));
80 itempool_used &= ~(1<<z);
83 /* Search downward until we find something *other* than an
84 * IDENT, then bring it up to the current level.
86 while (ptr->terms[0].type == YASM_EXPR_EXPR &&
87 ptr->terms[0].data.expn->op == YASM_EXPR_IDENT) {
88 sube = ptr->terms[0].data.expn;
89 ptr->terms[0] = sube->terms[0]; /* structure copy */
95 yasm_internal_error(N_("Right side of expression must exist"));
99 ptr->terms[1] = *right; /* structure copy */
100 z = (unsigned long)(right-itempool);
102 yasm_internal_error(N_("could not find expritem in pool"));
103 itempool_used &= ~(1<<z);
106 /* Search downward until we find something *other* than an
107 * IDENT, then bring it up to the current level.
109 while (ptr->terms[1].type == YASM_EXPR_EXPR &&
110 ptr->terms[1].data.expn->op == YASM_EXPR_IDENT) {
111 sube = ptr->terms[1].data.expn;
112 ptr->terms[1] = sube->terms[0]; /* structure copy */
121 return expr_level_op(ptr, 1, 1, 0);
126 static yasm_expr__item *
130 unsigned long v = itempool_used & 0x7fffffff;
137 yasm_internal_error(N_("too many expritems"));
138 itempool_used |= 1<<z;
143 yasm_expr_precbc(yasm_bytecode *precbc)
145 yasm_expr__item *e = expr_get_item();
146 e->type = YASM_EXPR_PRECBC;
147 e->data.precbc = precbc;
152 yasm_expr_sym(yasm_symrec *s)
154 yasm_expr__item *e = expr_get_item();
155 e->type = YASM_EXPR_SYM;
161 yasm_expr_expr(yasm_expr *x)
163 yasm_expr__item *e = expr_get_item();
164 e->type = YASM_EXPR_EXPR;
170 yasm_expr_int(yasm_intnum *i)
172 yasm_expr__item *e = expr_get_item();
173 e->type = YASM_EXPR_INT;
179 yasm_expr_float(yasm_floatnum *f)
181 yasm_expr__item *e = expr_get_item();
182 e->type = YASM_EXPR_FLOAT;
188 yasm_expr_reg(uintptr_t reg)
190 yasm_expr__item *e = expr_get_item();
191 e->type = YASM_EXPR_REG;
196 /* Transforms instances of symrec-symrec [symrec+(-1*symrec)] into single
197 * expritems if possible. Uses a simple n^2 algorithm because n is usually
198 * quite small. Also works for precbc-precbc (or symrec-precbc,
201 static /*@only@*/ yasm_expr *
202 expr_xform_bc_dist_base(/*@returned@*/ /*@only@*/ yasm_expr *e,
203 /*@null@*/ void *cbd,
204 int (*callback) (yasm_expr__item *ei,
205 yasm_bytecode *precbc,
206 yasm_bytecode *precbc2,
210 /*@dependent@*/ yasm_section *sect;
211 /*@dependent@*/ /*@null@*/ yasm_bytecode *precbc;
214 /* Handle symrec-symrec in ADD exprs by looking for (-1*symrec) and
215 * symrec term pairs (where both symrecs are in the same segment).
217 if (e->op != YASM_EXPR_ADD)
220 for (i=0; i<e->numterms; i++) {
224 yasm_symrec *sym = NULL;
225 /*@dependent@*/ yasm_section *sect2;
226 /*@dependent@*/ /*@null@*/ yasm_bytecode *precbc2;
228 /* First look for an (-1*symrec) term */
229 if (e->terms[i].type != YASM_EXPR_EXPR)
231 sube = e->terms[i].data.expn;
232 if (sube->op != YASM_EXPR_MUL || sube->numterms != 2)
235 if (sube->terms[0].type == YASM_EXPR_INT &&
236 (sube->terms[1].type == YASM_EXPR_SYM ||
237 sube->terms[1].type == YASM_EXPR_PRECBC)) {
238 intn = sube->terms[0].data.intn;
239 if (sube->terms[1].type == YASM_EXPR_PRECBC)
240 precbc = sube->terms[1].data.precbc;
242 sym = sube->terms[1].data.sym;
243 } else if ((sube->terms[0].type == YASM_EXPR_SYM ||
244 sube->terms[0].type == YASM_EXPR_PRECBC) &&
245 sube->terms[1].type == YASM_EXPR_INT) {
246 if (sube->terms[0].type == YASM_EXPR_PRECBC)
247 precbc = sube->terms[0].data.precbc;
249 sym = sube->terms[0].data.sym;
250 intn = sube->terms[1].data.intn;
254 if (!yasm_intnum_is_neg1(intn))
257 if (sym && !yasm_symrec_get_label(sym, &precbc))
259 sect2 = yasm_bc_get_section(precbc);
261 /* Now look for a symrec term in the same segment */
262 for (j=0; j<e->numterms; j++) {
263 if (((e->terms[j].type == YASM_EXPR_SYM &&
264 yasm_symrec_get_label(e->terms[j].data.sym, &precbc2)) ||
265 (e->terms[j].type == YASM_EXPR_PRECBC &&
266 (precbc2 = e->terms[j].data.precbc))) &&
267 (sect = yasm_bc_get_section(precbc2)) &&
269 callback(&e->terms[j], precbc, precbc2, cbd)) {
270 /* Delete the matching (-1*symrec) term */
271 yasm_expr_destroy(sube);
272 e->terms[i].type = YASM_EXPR_NONE;
273 break; /* stop looking for matching symrec term */
278 /* Clean up any deleted (EXPR_NONE) terms */
280 for (i=0; i<e->numterms; i++) {
281 if (e->terms[i].type != YASM_EXPR_NONE)
282 e->terms[numterms++] = e->terms[i]; /* structure copy */
284 if (e->numterms != numterms) {
285 e->numterms = numterms;
286 e = yasm_xrealloc(e, sizeof(yasm_expr)+((numterms<2) ? 0 :
287 sizeof(yasm_expr__item)*(numterms-2)));
289 e->op = YASM_EXPR_IDENT;
296 expr_xform_bc_dist_cb(yasm_expr__item *ei, yasm_bytecode *precbc,
297 yasm_bytecode *precbc2, /*@null@*/ void *d)
299 yasm_intnum *dist = yasm_calc_bc_dist(precbc, precbc2);
302 /* Change the term to an integer */
303 ei->type = YASM_EXPR_INT;
304 ei->data.intn = dist;
308 /* Transforms instances of symrec-symrec [symrec+(-1*symrec)] into integers if
311 static /*@only@*/ yasm_expr *
312 expr_xform_bc_dist(/*@returned@*/ /*@only@*/ yasm_expr *e)
314 return expr_xform_bc_dist_base(e, NULL, expr_xform_bc_dist_cb);
317 typedef struct bc_dist_subst_cbd {
318 void (*callback) (unsigned int subst, yasm_bytecode *precbc,
319 yasm_bytecode *precbc2, void *cbd);
325 expr_bc_dist_subst_cb(yasm_expr__item *ei, yasm_bytecode *precbc,
326 yasm_bytecode *precbc2, /*@null@*/ void *d)
328 bc_dist_subst_cbd *my_cbd = d;
329 assert(my_cbd != NULL);
330 /* Call higher-level callback */
331 my_cbd->callback(my_cbd->subst, precbc, precbc2, my_cbd->cbd);
332 /* Change the term to an subst */
333 ei->type = YASM_EXPR_SUBST;
334 ei->data.subst = my_cbd->subst;
340 expr_xform_bc_dist_subst(yasm_expr *e, void *d)
342 return expr_xform_bc_dist_base(e, d, expr_bc_dist_subst_cb);
346 yasm_expr__bc_dist_subst(yasm_expr **ep, void *cbd,
347 void (*callback) (unsigned int subst,
348 yasm_bytecode *precbc,
349 yasm_bytecode *precbc2,
352 bc_dist_subst_cbd my_cbd; /* callback info for low-level callback */
353 my_cbd.callback = callback;
356 *ep = yasm_expr__level_tree(*ep, 1, 1, 1, 0, &expr_xform_bc_dist_subst,
361 /* Negate just a single ExprItem by building a -1*ei subexpression */
363 expr_xform_neg_item(yasm_expr *e, yasm_expr__item *ei)
365 yasm_expr *sube = yasm_xmalloc(sizeof(yasm_expr));
367 /* Build -1*ei subexpression */
368 sube->op = YASM_EXPR_MUL;
369 sube->line = e->line;
371 sube->terms[0].type = YASM_EXPR_INT;
372 sube->terms[0].data.intn = yasm_intnum_create_int(-1);
373 sube->terms[1] = *ei; /* structure copy */
375 /* Replace original ExprItem with subexp */
376 ei->type = YASM_EXPR_EXPR;
377 ei->data.expn = sube;
380 /* Negates e by multiplying by -1, with distribution over lower-precedence
381 * operators (eg ADD) and special handling to simplify result w/ADD, NEG, and
384 * Returns a possibly reallocated e.
386 static /*@only@*/ yasm_expr *
387 expr_xform_neg_helper(/*@returned@*/ /*@only@*/ yasm_expr *e)
394 /* distribute (recursively if expr) over terms */
395 for (i=0; i<e->numterms; i++) {
396 if (e->terms[i].type == YASM_EXPR_EXPR)
397 e->terms[i].data.expn =
398 expr_xform_neg_helper(e->terms[i].data.expn);
400 expr_xform_neg_item(e, &e->terms[i]);
404 /* change op to ADD, and recursively negate left side (if expr) */
405 e->op = YASM_EXPR_ADD;
406 if (e->terms[0].type == YASM_EXPR_EXPR)
407 e->terms[0].data.expn =
408 expr_xform_neg_helper(e->terms[0].data.expn);
410 expr_xform_neg_item(e, &e->terms[0]);
413 /* Negating a negated value? Make it an IDENT. */
414 e->op = YASM_EXPR_IDENT;
416 case YASM_EXPR_IDENT:
417 /* Negating an ident? Change it into a MUL w/ -1 if there's no
418 * floatnums present below; if there ARE floatnums, recurse.
420 if (e->terms[0].type == YASM_EXPR_FLOAT)
421 yasm_floatnum_calc(e->terms[0].data.flt, YASM_EXPR_NEG, NULL);
422 else if (e->terms[0].type == YASM_EXPR_INT)
423 yasm_intnum_calc(e->terms[0].data.intn, YASM_EXPR_NEG, NULL);
424 else if (e->terms[0].type == YASM_EXPR_EXPR &&
425 yasm_expr__contains(e->terms[0].data.expn, YASM_EXPR_FLOAT))
426 expr_xform_neg_helper(e->terms[0].data.expn);
428 e->op = YASM_EXPR_MUL;
430 e->terms[1].type = YASM_EXPR_INT;
431 e->terms[1].data.intn = yasm_intnum_create_int(-1);
435 /* Everything else. MUL will be combined when it's leveled.
436 * Make a new expr (to replace e) with -1*e.
438 ne = yasm_xmalloc(sizeof(yasm_expr));
439 ne->op = YASM_EXPR_MUL;
442 ne->terms[0].type = YASM_EXPR_INT;
443 ne->terms[0].data.intn = yasm_intnum_create_int(-1);
444 ne->terms[1].type = YASM_EXPR_EXPR;
445 ne->terms[1].data.expn = e;
451 /* Transforms negatives into expressions that are easier to combine:
455 * Call post-order on an expression tree to transform the entire tree.
457 * Returns a possibly reallocated e.
459 static /*@only@*/ yasm_expr *
460 expr_xform_neg(/*@returned@*/ /*@only@*/ yasm_expr *e)
464 /* Turn -x into -1*x */
465 e->op = YASM_EXPR_IDENT;
466 return expr_xform_neg_helper(e);
468 /* Turn a-b into a+(-1*b) */
470 /* change op to ADD, and recursively negate right side (if expr) */
471 e->op = YASM_EXPR_ADD;
472 if (e->terms[1].type == YASM_EXPR_EXPR)
473 e->terms[1].data.expn =
474 expr_xform_neg_helper(e->terms[1].data.expn);
476 expr_xform_neg_item(e, &e->terms[1]);
485 /* Look for simple identities that make the entire result constant:
489 expr_is_constant(yasm_expr_op op, yasm_intnum *intn)
491 int iszero = yasm_intnum_is_zero(intn);
492 return ((iszero && op == YASM_EXPR_MUL) ||
493 (iszero && op == YASM_EXPR_AND) ||
494 (iszero && op == YASM_EXPR_LAND) ||
495 (yasm_intnum_is_neg1(intn) && op == YASM_EXPR_OR));
498 /* Look for simple "left" identities like 0+x, 1*x, etc. */
500 expr_can_destroy_int_left(yasm_expr_op op, yasm_intnum *intn)
502 int iszero = yasm_intnum_is_zero(intn);
503 return ((yasm_intnum_is_pos1(intn) && op == YASM_EXPR_MUL) ||
504 (iszero && op == YASM_EXPR_ADD) ||
505 (yasm_intnum_is_neg1(intn) && op == YASM_EXPR_AND) ||
506 (!iszero && op == YASM_EXPR_LAND) ||
507 (iszero && op == YASM_EXPR_OR) ||
508 (iszero && op == YASM_EXPR_LOR));
511 /* Look for simple "right" identities like x+|-0, x*&/1 */
513 expr_can_destroy_int_right(yasm_expr_op op, yasm_intnum *intn)
515 int iszero = yasm_intnum_is_zero(intn);
516 int ispos1 = yasm_intnum_is_pos1(intn);
517 return ((ispos1 && op == YASM_EXPR_MUL) ||
518 (ispos1 && op == YASM_EXPR_DIV) ||
519 (iszero && op == YASM_EXPR_ADD) ||
520 (iszero && op == YASM_EXPR_SUB) ||
521 (yasm_intnum_is_neg1(intn) && op == YASM_EXPR_AND) ||
522 (!iszero && op == YASM_EXPR_LAND) ||
523 (iszero && op == YASM_EXPR_OR) ||
524 (iszero && op == YASM_EXPR_LOR) ||
525 (iszero && op == YASM_EXPR_SHL) ||
526 (iszero && op == YASM_EXPR_SHR));
529 /* Check for and simplify identities. Returns new number of expr terms.
530 * Sets e->op = EXPR_IDENT if numterms ends up being 1.
531 * Uses numterms parameter instead of e->numterms for basis of "new" number
533 * Assumes int_term is *only* integer term in e.
534 * NOTE: Really designed to only be used by expr_level_op().
537 expr_simplify_identity(yasm_expr *e, int numterms, int *int_term,
538 int simplify_reg_mul)
543 /* Don't do this step if it's 1*REG. Save and restore numterms so
544 * yasm_expr__contains() works correctly.
546 save_numterms = e->numterms;
547 e->numterms = numterms;
548 if (simplify_reg_mul || e->op != YASM_EXPR_MUL
549 || !yasm_intnum_is_pos1(e->terms[*int_term].data.intn)
550 || !yasm_expr__contains(e, YASM_EXPR_REG)) {
551 /* Check for simple identities that delete the intnum.
552 * Don't delete if the intnum is the only thing in the expn.
554 if ((*int_term == 0 && numterms > 1 &&
555 expr_can_destroy_int_left(e->op, e->terms[0].data.intn)) ||
557 expr_can_destroy_int_right(e->op,
558 e->terms[*int_term].data.intn))) {
559 /* Delete the intnum */
560 yasm_intnum_destroy(e->terms[*int_term].data.intn);
562 /* Slide everything to its right over by 1 */
563 if (*int_term != numterms-1) /* if it wasn't last.. */
564 memmove(&e->terms[*int_term], &e->terms[*int_term+1],
565 (numterms-1-*int_term)*sizeof(yasm_expr__item));
567 /* Update numterms */
569 *int_term = -1; /* no longer an int term */
572 e->numterms = save_numterms;
574 /* Check for simple identites that delete everything BUT the intnum.
575 * Don't bother if the intnum is the only thing in the expn.
577 if (numterms > 1 && *int_term != -1 &&
578 expr_is_constant(e->op, e->terms[*int_term].data.intn)) {
579 /* Loop through, deleting everything but the integer term */
580 for (i=0; i<e->numterms; i++)
582 expr_delete_term(&e->terms[i], 1);
584 /* Move integer term to the first term (if not already there) */
586 e->terms[0] = e->terms[*int_term]; /* structure copy */
588 /* Set numterms to 1 */
592 /* Compute NOT, NEG, and LNOT on single intnum. */
593 if (numterms == 1 && *int_term == 0 &&
594 (e->op == YASM_EXPR_NOT || e->op == YASM_EXPR_NEG ||
595 e->op == YASM_EXPR_LNOT))
596 yasm_intnum_calc(e->terms[0].data.intn, e->op, NULL);
598 /* Change expression to IDENT if possible. */
600 e->op = YASM_EXPR_IDENT;
602 /* Return the updated numterms */
606 /* Levels the expression tree starting at e. Eg:
608 * (a+b)+(c+d) -> a+b+c+d
609 * Naturally, only levels operators that allow more than two operand terms.
610 * NOTE: only does *one* level of leveling (no recursion). Should be called
611 * post-order on a tree to combine deeper levels.
612 * Also brings up any IDENT values into the current level (for ALL operators).
613 * Folds (combines by evaluation) *integer* constant values if fold_const != 0.
615 * Returns a possibly reallocated e.
618 static /*@only@*/ yasm_expr *
619 expr_level_op(/*@returned@*/ /*@only@*/ yasm_expr *e, int fold_const,
620 int simplify_ident, int simplify_reg_mul)
622 int i, j, o, fold_numterms, level_numterms, level_fold_numterms;
623 int first_int_term = -1;
625 /* Determine how many operands will need to be brought up (for leveling).
626 * Go ahead and bring up any IDENT'ed values.
628 while (e->op == YASM_EXPR_IDENT && e->terms[0].type == YASM_EXPR_EXPR) {
629 yasm_expr *sube = e->terms[0].data.expn;
634 /* If non-numeric expression, don't fold constants. */
635 if (e->op > YASM_EXPR_NONNUM)
638 level_numterms = e->numterms;
639 level_fold_numterms = 0;
640 for (i=0; i<e->numterms; i++) {
641 /* Search downward until we find something *other* than an
642 * IDENT, then bring it up to the current level.
644 while (e->terms[i].type == YASM_EXPR_EXPR &&
645 e->terms[i].data.expn->op == YASM_EXPR_IDENT) {
646 yasm_expr *sube = e->terms[i].data.expn;
647 e->terms[i] = sube->terms[0];
651 if (e->terms[i].type == YASM_EXPR_EXPR &&
652 e->terms[i].data.expn->op == e->op) {
653 /* It's an expression w/the same operator, add in its numterms.
654 * But don't forget to subtract one for the expr itself!
656 level_numterms += e->terms[i].data.expn->numterms - 1;
658 /* If we're folding constants, count up the number of constants
659 * that will be merged in.
662 for (j=0; j<e->terms[i].data.expn->numterms; j++)
663 if (e->terms[i].data.expn->terms[j].type ==
665 level_fold_numterms++;
668 /* Find the first integer term (if one is present) if we're folding
671 if (fold_const && first_int_term == -1 &&
672 e->terms[i].type == YASM_EXPR_INT)
676 /* Look for other integer terms if there's one and combine.
677 * Also eliminate empty spaces when combining and adjust numterms
680 fold_numterms = e->numterms;
681 if (first_int_term != -1) {
682 for (i=first_int_term+1, o=first_int_term+1; i<e->numterms; i++) {
683 if (e->terms[i].type == YASM_EXPR_INT) {
684 yasm_intnum_calc(e->terms[first_int_term].data.intn, e->op,
685 e->terms[i].data.intn);
688 /* make sure to delete folded intnum */
689 yasm_intnum_destroy(e->terms[i].data.intn);
691 /* copy term if it changed places */
692 e->terms[o++] = e->terms[i];
697 if (simplify_ident) {
698 int new_fold_numterms;
699 /* Simplify identities and make IDENT if possible. */
701 expr_simplify_identity(e, fold_numterms, &first_int_term,
703 level_numterms -= fold_numterms-new_fold_numterms;
704 fold_numterms = new_fold_numterms;
706 if (fold_numterms == 1)
707 e->op = YASM_EXPR_IDENT;
710 /* Only level operators that allow more than two operand terms.
711 * Also don't bother leveling if it's not necessary to bring up any terms.
713 if ((e->op != YASM_EXPR_ADD && e->op != YASM_EXPR_MUL &&
714 e->op != YASM_EXPR_OR && e->op != YASM_EXPR_AND &&
715 e->op != YASM_EXPR_LOR && e->op != YASM_EXPR_LAND &&
716 e->op != YASM_EXPR_LXOR && e->op != YASM_EXPR_XOR) ||
717 level_numterms <= fold_numterms) {
718 /* Downsize e if necessary */
719 if (fold_numterms < e->numterms && e->numterms > 2)
720 e = yasm_xrealloc(e, sizeof(yasm_expr)+((fold_numterms<2) ? 0 :
721 sizeof(yasm_expr__item)*(fold_numterms-2)));
722 /* Update numterms */
723 e->numterms = fold_numterms;
727 /* Adjust numterms for constant folding from terms being "pulled up".
728 * Careful: if there's no integer term in e, then save space for it.
731 level_numterms -= level_fold_numterms;
732 if (first_int_term == -1 && level_fold_numterms != 0)
736 /* Alloc more (or conceivably less, but not usually) space for e */
737 e = yasm_xrealloc(e, sizeof(yasm_expr)+((level_numterms<2) ? 0 :
738 sizeof(yasm_expr__item)*(level_numterms-2)));
740 /* Copy up ExprItem's. Iterate from right to left to keep the same
741 * ordering as was present originally.
742 * Combine integer terms as necessary.
744 for (i=fold_numterms-1, o=level_numterms-1; i>=0; i--) {
745 if (e->terms[i].type == YASM_EXPR_EXPR &&
746 e->terms[i].data.expn->op == e->op) {
747 /* bring up subexpression */
748 yasm_expr *sube = e->terms[i].data.expn;
750 /* copy terms right to left */
751 for (j=sube->numterms-1; j>=0; j--) {
752 if (fold_const && sube->terms[j].type == YASM_EXPR_INT) {
753 /* Need to fold it in.. but if there's no int term already,
754 * just copy into a new one.
756 if (first_int_term == -1) {
757 first_int_term = o--;
758 e->terms[first_int_term] = sube->terms[j]; /* struc */
760 yasm_intnum_calc(e->terms[first_int_term].data.intn,
761 e->op, sube->terms[j].data.intn);
762 /* make sure to delete folded intnum */
763 yasm_intnum_destroy(sube->terms[j].data.intn);
766 if (o == first_int_term)
768 e->terms[o--] = sube->terms[j]; /* structure copy */
772 /* delete subexpression, but *don't delete nodes* (as we've just
777 /* copy operand if it changed places */
778 if (o == first_int_term)
780 e->terms[o] = e->terms[i];
781 /* If we moved the first_int_term, change first_int_num too */
782 if (i == first_int_term)
789 /* Simplify identities, make IDENT if possible, and save to e->numterms. */
790 if (simplify_ident && first_int_term != -1) {
791 e->numterms = expr_simplify_identity(e, level_numterms,
792 &first_int_term, simplify_reg_mul);
794 e->numterms = level_numterms;
795 if (level_numterms == 1)
796 e->op = YASM_EXPR_IDENT;
803 typedef SLIST_HEAD(yasm__exprhead, yasm__exprentry) yasm__exprhead;
804 typedef struct yasm__exprentry {
805 /*@reldef@*/ SLIST_ENTRY(yasm__exprentry) next;
806 /*@null@*/ const yasm_expr *e;
810 expr_expand_equ(yasm_expr *e, yasm__exprhead *eh)
816 for (i=0; i<e->numterms; i++) {
817 const yasm_expr *equ_expr;
820 if (e->terms[i].type == YASM_EXPR_SYM &&
821 (equ_expr = yasm_symrec_get_equ(e->terms[i].data.sym))) {
824 /* Check for circular reference */
825 SLIST_FOREACH(np, eh, next) {
826 if (np->e == equ_expr) {
827 yasm_error_set(YASM_ERROR_TOO_COMPLEX,
828 N_("circular reference detected"));
833 e->terms[i].type = YASM_EXPR_EXPR;
834 e->terms[i].data.expn = yasm_expr_copy(equ_expr);
836 /* Remember we saw this equ and recurse */
838 SLIST_INSERT_HEAD(eh, &ee, next);
839 e->terms[i].data.expn = expr_expand_equ(e->terms[i].data.expn, eh);
840 SLIST_REMOVE_HEAD(eh, next);
841 } else if (e->terms[i].type == YASM_EXPR_EXPR)
843 e->terms[i].data.expn = expr_expand_equ(e->terms[i].data.expn, eh);
850 expr_level_tree(yasm_expr *e, int fold_const, int simplify_ident,
851 int simplify_reg_mul, int calc_bc_dist,
852 yasm_expr_xform_func expr_xform_extra,
853 void *expr_xform_extra_data)
857 e = expr_xform_neg(e);
860 for (i=0; i<e->numterms; i++) {
862 if (e->terms[i].type == YASM_EXPR_EXPR)
863 e->terms[i].data.expn =
864 expr_level_tree(e->terms[i].data.expn, fold_const,
865 simplify_ident, simplify_reg_mul, calc_bc_dist,
866 expr_xform_extra, expr_xform_extra_data);
869 /* Check for SEG of SEG:OFF, if we match, simplify to just the segment */
870 if (e->op == YASM_EXPR_SEG && e->terms[0].type == YASM_EXPR_EXPR &&
871 e->terms[0].data.expn->op == YASM_EXPR_SEGOFF) {
872 e->op = YASM_EXPR_IDENT;
873 e->terms[0].data.expn->op = YASM_EXPR_IDENT;
874 /* Destroy the second (offset) term */
875 e->terms[0].data.expn->numterms = 1;
876 expr_delete_term(&e->terms[0].data.expn->terms[1], 1);
880 e = expr_level_op(e, fold_const, simplify_ident, simplify_reg_mul);
881 if (calc_bc_dist || expr_xform_extra) {
883 e = expr_xform_bc_dist(e);
884 if (expr_xform_extra)
885 e = expr_xform_extra(e, expr_xform_extra_data);
886 e = expr_level_tree(e, fold_const, simplify_ident, simplify_reg_mul,
892 /* Level an entire expn tree, expanding equ's as we go */
894 yasm_expr__level_tree(yasm_expr *e, int fold_const, int simplify_ident,
895 int simplify_reg_mul, int calc_bc_dist,
896 yasm_expr_xform_func expr_xform_extra,
897 void *expr_xform_extra_data)
905 e = expr_expand_equ(e, &eh);
906 e = expr_level_tree(e, fold_const, simplify_ident, simplify_reg_mul,
907 calc_bc_dist, expr_xform_extra, expr_xform_extra_data);
912 /* Comparison function for expr_order_terms().
913 * Assumes ExprType enum is in canonical order.
916 expr_order_terms_compare(const void *va, const void *vb)
918 const yasm_expr__item *a = va, *b = vb;
919 return (a->type - b->type);
922 /* Reorder terms of e into canonical order. Only reorders if reordering
923 * doesn't change meaning of expression. (eg, doesn't reorder SUB).
924 * Canonical order: REG, INT, FLOAT, SYM, EXPR.
925 * Multiple terms of a single type are kept in the same order as in
926 * the original expression.
927 * NOTE: Only performs reordering on *one* level (no recursion).
930 yasm_expr__order_terms(yasm_expr *e)
932 /* don't bother reordering if only one element */
933 if (e->numterms == 1)
936 /* only reorder some types of operations */
946 /* Use mergesort to sort. It's fast on already sorted values and a
947 * stable sort (multiple terms of same type are kept in the same
950 yasm__mergesort(e->terms, (size_t)e->numterms,
951 sizeof(yasm_expr__item), expr_order_terms_compare);
959 expr_item_copy(yasm_expr__item *dest, const yasm_expr__item *src)
961 dest->type = src->type;
964 /* Symbols don't need to be copied */
965 dest->data.sym = src->data.sym;
967 case YASM_EXPR_PRECBC:
968 /* Nor do direct bytecode references */
969 dest->data.precbc = src->data.precbc;
972 dest->data.expn = yasm_expr__copy_except(src->data.expn, -1);
975 dest->data.intn = yasm_intnum_copy(src->data.intn);
977 case YASM_EXPR_FLOAT:
978 dest->data.flt = yasm_floatnum_copy(src->data.flt);
981 dest->data.reg = src->data.reg;
983 case YASM_EXPR_SUBST:
984 dest->data.subst = src->data.subst;
991 /* Copy entire expression EXCEPT for index "except" at *top level only*. */
993 yasm_expr__copy_except(const yasm_expr *e, int except)
998 n = yasm_xmalloc(sizeof(yasm_expr) +
999 sizeof(yasm_expr__item)*(e->numterms<2?0:e->numterms-2));
1003 n->numterms = e->numterms;
1004 for (i=0; i<e->numterms; i++) {
1006 expr_item_copy(&n->terms[i], &e->terms[i]);
1013 expr_delete_term(yasm_expr__item *term, int recurse)
1015 switch (term->type) {
1017 yasm_intnum_destroy(term->data.intn);
1019 case YASM_EXPR_FLOAT:
1020 yasm_floatnum_destroy(term->data.flt);
1022 case YASM_EXPR_EXPR:
1024 yasm_expr_destroy(term->data.expn);
1032 expr_destroy_each(/*@only@*/ yasm_expr *e, /*@unused@*/ void *d)
1035 for (i=0; i<e->numterms; i++)
1036 expr_delete_term(&e->terms[i], 0);
1037 yasm_xfree(e); /* free ourselves */
1038 return 0; /* don't stop recursion */
1043 yasm_expr_destroy(yasm_expr *e)
1045 expr_traverse_nodes_post(e, NULL, expr_destroy_each);
1050 yasm_expr_is_op(const yasm_expr *e, yasm_expr_op op)
1052 return (e->op == op);
1056 expr_contains_callback(const yasm_expr__item *ei, void *d)
1058 yasm_expr__type *t = d;
1059 return (ei->type & *t);
1063 yasm_expr__contains(const yasm_expr *e, yasm_expr__type t)
1065 return yasm_expr__traverse_leaves_in_const(e, &t, expr_contains_callback);
1068 typedef struct subst_cbd {
1069 unsigned int num_items;
1070 const yasm_expr__item *items;
1074 expr_subst_callback(yasm_expr__item *ei, void *d)
1077 if (ei->type != YASM_EXPR_SUBST)
1079 if (ei->data.subst >= cbd->num_items)
1080 return 1; /* error */
1081 expr_item_copy(ei, &cbd->items[ei->data.subst]);
1086 yasm_expr__subst(yasm_expr *e, unsigned int num_items,
1087 const yasm_expr__item *items)
1090 cbd.num_items = num_items;
1092 return yasm_expr__traverse_leaves_in(e, &cbd, expr_subst_callback);
1095 /* Traverse over expression tree, calling func for each operation AFTER the
1096 * branches (if expressions) have been traversed (eg, postorder
1097 * traversal). The data pointer d is passed to each func call.
1099 * Stops early (and returns 1) if func returns 1. Otherwise returns 0.
1102 expr_traverse_nodes_post(yasm_expr *e, void *d,
1103 int (*func) (/*@null@*/ yasm_expr *e,
1104 /*@null@*/ void *d))
1111 /* traverse terms */
1112 for (i=0; i<e->numterms; i++) {
1113 if (e->terms[i].type == YASM_EXPR_EXPR &&
1114 expr_traverse_nodes_post(e->terms[i].data.expn, d, func))
1122 /* Traverse over expression tree in order, calling func for each leaf
1123 * (non-operation). The data pointer d is passed to each func call.
1125 * Stops early (and returns 1) if func returns 1. Otherwise returns 0.
1128 yasm_expr__traverse_leaves_in_const(const yasm_expr *e, void *d,
1129 int (*func) (/*@null@*/ const yasm_expr__item *ei, /*@null@*/ void *d))
1136 for (i=0; i<e->numterms; i++) {
1137 if (e->terms[i].type == YASM_EXPR_EXPR) {
1138 if (yasm_expr__traverse_leaves_in_const(e->terms[i].data.expn, d,
1142 if (func(&e->terms[i], d))
1149 /* Traverse over expression tree in order, calling func for each leaf
1150 * (non-operation). The data pointer d is passed to each func call.
1152 * Stops early (and returns 1) if func returns 1. Otherwise returns 0.
1155 yasm_expr__traverse_leaves_in(yasm_expr *e, void *d,
1156 int (*func) (/*@null@*/ yasm_expr__item *ei, /*@null@*/ void *d))
1163 for (i=0; i<e->numterms; i++) {
1164 if (e->terms[i].type == YASM_EXPR_EXPR) {
1165 if (yasm_expr__traverse_leaves_in(e->terms[i].data.expn, d, func))
1168 if (func(&e->terms[i], d))
1176 yasm_expr_extract_deep_segoff(yasm_expr **ep)
1182 /* Try to extract at this level */
1183 retval = yasm_expr_extract_segoff(ep);
1187 /* Not at this level? Search any expr children. */
1188 for (i=0; i<e->numterms; i++) {
1189 if (e->terms[i].type == YASM_EXPR_EXPR) {
1190 retval = yasm_expr_extract_deep_segoff(&e->terms[i].data.expn);
1196 /* Didn't find one */
1201 yasm_expr_extract_segoff(yasm_expr **ep)
1206 /* If not SEG:OFF, we can't do this transformation */
1207 if (e->op != YASM_EXPR_SEGOFF)
1210 /* Extract the SEG portion out to its own expression */
1211 if (e->terms[0].type == YASM_EXPR_EXPR)
1212 retval = e->terms[0].data.expn;
1214 /* Need to build IDENT expression to hold non-expression contents */
1215 retval = yasm_xmalloc(sizeof(yasm_expr));
1216 retval->op = YASM_EXPR_IDENT;
1217 retval->numterms = 1;
1218 retval->terms[0] = e->terms[0]; /* structure copy */
1221 /* Delete the SEG: portion by changing the expression into an IDENT */
1222 e->op = YASM_EXPR_IDENT;
1224 e->terms[0] = e->terms[1]; /* structure copy */
1230 yasm_expr_extract_wrt(yasm_expr **ep)
1235 /* If not WRT, we can't do this transformation */
1236 if (e->op != YASM_EXPR_WRT)
1239 /* Extract the right side portion out to its own expression */
1240 if (e->terms[1].type == YASM_EXPR_EXPR)
1241 retval = e->terms[1].data.expn;
1243 /* Need to build IDENT expression to hold non-expression contents */
1244 retval = yasm_xmalloc(sizeof(yasm_expr));
1245 retval->op = YASM_EXPR_IDENT;
1246 retval->numterms = 1;
1247 retval->terms[0] = e->terms[1]; /* structure copy */
1250 /* Delete the right side portion by changing the expr into an IDENT */
1251 e->op = YASM_EXPR_IDENT;
1257 /*@-unqualifiedtrans -nullderef -nullstate -onlytrans@*/
1259 yasm_expr_get_intnum(yasm_expr **ep, int calc_bc_dist)
1261 *ep = yasm_expr_simplify(*ep, calc_bc_dist);
1263 if ((*ep)->op == YASM_EXPR_IDENT && (*ep)->terms[0].type == YASM_EXPR_INT)
1264 return (*ep)->terms[0].data.intn;
1266 return (yasm_intnum *)NULL;
1268 /*@=unqualifiedtrans =nullderef -nullstate -onlytrans@*/
1270 /*@-unqualifiedtrans -nullderef -nullstate -onlytrans@*/
1272 yasm_expr_get_symrec(yasm_expr **ep, int simplify)
1275 *ep = yasm_expr_simplify(*ep, 0);
1277 if ((*ep)->op == YASM_EXPR_IDENT && (*ep)->terms[0].type == YASM_EXPR_SYM)
1278 return (*ep)->terms[0].data.sym;
1280 return (yasm_symrec *)NULL;
1282 /*@=unqualifiedtrans =nullderef -nullstate -onlytrans@*/
1284 /*@-unqualifiedtrans -nullderef -nullstate -onlytrans@*/
1286 yasm_expr_get_reg(yasm_expr **ep, int simplify)
1289 *ep = yasm_expr_simplify(*ep, 0);
1291 if ((*ep)->op == YASM_EXPR_IDENT && (*ep)->terms[0].type == YASM_EXPR_REG)
1292 return &((*ep)->terms[0].data.reg);
1296 /*@=unqualifiedtrans =nullderef -nullstate -onlytrans@*/
1299 yasm_expr_print(const yasm_expr *e, FILE *f)
1305 fprintf(f, "(nil)");
1322 case YASM_EXPR_SIGNDIV:
1323 strcpy(opstr, "//");
1328 case YASM_EXPR_SIGNMOD:
1329 strcpy(opstr, "%%");
1348 case YASM_EXPR_XNOR:
1349 strcpy(opstr, "XNOR");
1352 strcpy(opstr, "NOR");
1355 strcpy(opstr, "<<");
1358 strcpy(opstr, ">>");
1361 strcpy(opstr, "||");
1363 case YASM_EXPR_LAND:
1364 strcpy(opstr, "&&");
1366 case YASM_EXPR_LNOT:
1369 case YASM_EXPR_LXOR:
1370 strcpy(opstr, "^^");
1372 case YASM_EXPR_LXNOR:
1373 strcpy(opstr, "LXNOR");
1375 case YASM_EXPR_LNOR:
1376 strcpy(opstr, "LNOR");
1385 strcpy(opstr, "<=");
1388 strcpy(opstr, ">=");
1391 strcpy(opstr, "!=");
1394 strcpy(opstr, "==");
1401 strcpy(opstr, " WRT ");
1403 case YASM_EXPR_SEGOFF:
1406 case YASM_EXPR_IDENT:
1410 strcpy(opstr, " !UNK! ");
1413 for (i=0; i<e->numterms; i++) {
1414 switch (e->terms[i].type) {
1415 case YASM_EXPR_PRECBC:
1417 yasm_bc_next_offset(e->terms[i].data.precbc));
1420 fprintf(f, "%s", yasm_symrec_get_name(e->terms[i].data.sym));
1422 case YASM_EXPR_EXPR:
1424 yasm_expr_print(e->terms[i].data.expn, f);
1428 yasm_intnum_print(e->terms[i].data.intn, f);
1430 case YASM_EXPR_FLOAT:
1431 yasm_floatnum_print(e->terms[i].data.flt, f);
1435 /*yasm_arch_reg_print(arch, e->terms[i].data.reg, f);*/
1437 case YASM_EXPR_SUBST:
1438 fprintf(f, "[%u]", e->terms[i].data.subst);
1440 case YASM_EXPR_NONE:
1443 if (i < e->numterms-1)
1444 fprintf(f, "%s", opstr);
1449 yasm_expr_size(const yasm_expr *e)
1453 unsigned int size = 0, newsize;
1455 if (e->op == YASM_EXPR_IDENT) {
1456 if (e->terms[0].type == YASM_EXPR_SYM)
1457 return yasm_symrec_get_size(e->terms[0].data.sym);
1460 if (e->op != YASM_EXPR_ADD && e->op != YASM_EXPR_SUB)
1463 for (i=0; i<e->numterms; i++) {
1465 switch (e->terms[i].type) {
1466 case YASM_EXPR_EXPR:
1467 newsize = yasm_expr_size(e->terms[i].data.expn);
1470 newsize = yasm_symrec_get_size(e->terms[i].data.sym);
1478 /* either sum of idents (?!) or substract of idents */
1483 /* exactly one offset */
1488 yasm_expr_segment(const yasm_expr *e)
1492 const char *segment = NULL;
1494 if (e->op == YASM_EXPR_IDENT) {
1495 if (e->terms[0].type == YASM_EXPR_SYM)
1496 return yasm_symrec_get_segment(e->terms[0].data.sym);
1499 if (e->op != YASM_EXPR_ADD && e->op != YASM_EXPR_SUB)
1502 for (i=0; i<e->numterms; i++) {
1503 if ((e->op == YASM_EXPR_ADD || !i) &&
1504 e->terms[i].type == YASM_EXPR_EXPR) {
1505 if ((segment = yasm_expr_segment(e->terms[i].data.expn))) {
1507 /* either sum of idents (?!) or substract of idents */
1514 /* exactly one offset */