summaryrefslogtreecommitdiff
path: root/libyasm/expr.c
blob: 6256f59106a76519a3e4666c04200c85908093ea (plain)
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
/* $IdPath$
 * Expression handling
 *
 *  Copyright (C) 2001  Michael Urman, Peter Johnson
 *
 *  This file is part of YASM.
 *
 *  YASM is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  YASM is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "util.h"

#include <stdio.h>

#ifdef STDC_HEADERS
# include <stdlib.h>
# include <string.h>
#endif

#include <libintl.h>
#define _(String)	gettext(String)
#ifdef gettext_noop
#define N_(String)	gettext_noop(String)
#else
#define N_(String)	(String)
#endif

#include "bitvect.h"

#include "globals.h"
#include "errwarn.h"
#include "intnum.h"
#include "floatnum.h"
#include "expr.h"
#include "symrec.h"

RCSID("$IdPath$");

/* Types listed in canonical sorting order.  See expr_order_terms(). */
typedef enum {
    EXPR_NONE = 0,
    EXPR_REG = 1<<0,
    EXPR_INT = 1<<1,
    EXPR_FLOAT = 1<<2,
    EXPR_SYM = 1<<3,
    EXPR_EXPR = 1<<4
} ExprType;

struct ExprItem {
    ExprType type;
    union {
	symrec *sym;
	expr *expn;
	intnum *intn;
	floatnum *flt;
	struct reg {
	    unsigned char num;
	    unsigned char size;	/* in bits, eg AX=16, EAX=32 */
	} reg;
    } data;
};

/* Some operations may allow more than two operand terms:
 * ADD, MUL, OR, AND, XOR
 */
struct expr {
    ExprOp op;
    char *filename;
    unsigned long line;
    int numterms;
    ExprItem terms[2];		/* structure may be extended to include more */
};

static int expr_traverse_nodes_post(expr *e, void *d,
				    int (*func) (expr *e, void *d));
static int expr_traverse_leaves_in(expr *e, void *d,
				   int (*func) (ExprItem *ei, void *d));

/* allocate a new expression node, with children as defined.
 * If it's a unary operator, put the element on the right */
expr *
expr_new(ExprOp op, ExprItem *left, ExprItem *right)
{
    expr *ptr;
    ptr = xmalloc(sizeof(expr));

    ptr->op = op;
    ptr->numterms = 0;
    ptr->terms[0].type = EXPR_NONE;
    ptr->terms[1].type = EXPR_NONE;
    if (left) {
	memcpy(&ptr->terms[0], left, sizeof(ExprItem));
	free(left);
	ptr->numterms++;
    } else {
	InternalError(__LINE__, __FILE__,
		      _("Right side of expression must exist"));
    }

    if (right) {
	memcpy(&ptr->terms[1], right, sizeof(ExprItem));
	free(right);
	ptr->numterms++;
    }

    ptr->filename = xstrdup(in_filename);
    ptr->line = line_number;

    return ptr;
}

/* helpers */
ExprItem *
ExprSym(symrec *s)
{
    ExprItem *e = xmalloc(sizeof(ExprItem));
    e->type = EXPR_SYM;
    e->data.sym = s;
    return e;
}

ExprItem *
ExprExpr(expr *x)
{
    ExprItem *e = xmalloc(sizeof(ExprItem));
    e->type = EXPR_EXPR;
    e->data.expn = x;
    return e;
}

ExprItem *
ExprInt(intnum *i)
{
    ExprItem *e = xmalloc(sizeof(ExprItem));
    e->type = EXPR_INT;
    e->data.intn = i;
    return e;
}

ExprItem *
ExprFloat(floatnum *f)
{
    ExprItem *e = xmalloc(sizeof(ExprItem));
    e->type = EXPR_FLOAT;
    e->data.flt = f;
    return e;
}

ExprItem *
ExprReg(unsigned char reg, unsigned char size)
{
    ExprItem *e = xmalloc(sizeof(ExprItem));
    e->type = EXPR_REG;
    e->data.reg.num = reg;
    e->data.reg.size = size;
    return e;
}

/* Negate just a single ExprItem by building a -1*ei subexpression */
static void
expr_xform_neg_item(expr *e, ExprItem *ei)
{
    expr *sube = xmalloc(sizeof(expr));

    /* Build -1*ei subexpression */
    sube->op = EXPR_MUL;
    sube->filename = xstrdup(e->filename);
    sube->line = e->line;
    sube->numterms = 2;
    sube->terms[0].type = EXPR_INT;
    sube->terms[0].data.intn = intnum_new_int(-1);
    sube->terms[1] = *ei;	/* structure copy */

    /* Replace original ExprItem with subexp */
    ei->type = EXPR_EXPR;
    ei->data.expn = sube;
}

/* Negates e by multiplying by -1, with distribution over lower-precedence
 * operators (eg ADD) and special handling to simplify result w/ADD, NEG, and
 * others.
 *
 * Returns a possibly reallocated e.
 */
static expr *
expr_xform_neg_helper(expr *e)
{
    expr *ne;
    int i;

    switch (e->op) {
	case EXPR_ADD:
	    /* distribute (recursively if expr) over terms */
	    for (i=0; i<e->numterms; i++) {
		if (e->terms[i].type == EXPR_EXPR)
		    e->terms[i].data.expn =
			expr_xform_neg_helper(e->terms[i].data.expn);
		else
		    expr_xform_neg_item(e, &e->terms[i]);
	    }
	    break;
	case EXPR_SUB:
	    /* change op to ADD, and recursively negate left side (if expr) */
	    e->op = EXPR_ADD;
	    if (e->terms[0].type == EXPR_EXPR)
		e->terms[0].data.expn =
		    expr_xform_neg_helper(e->terms[0].data.expn);
	    else
		expr_xform_neg_item(e, &e->terms[0]);
	    break;
	case EXPR_NEG:
	    /* Negating a negated value?  Make it an IDENT. */
	    e->op = EXPR_IDENT;
	    break;
	case EXPR_IDENT:
	    /* Negating an ident?  Change it into a MUL w/ -1. */
	    e->op = EXPR_MUL;
	    e->numterms = 2;
	    e->terms[1].type = EXPR_INT;
	    e->terms[1].data.intn = intnum_new_int(-1);
	    break;
	default:
	    /* Everything else.  MUL will be combined when it's leveled.
	     * Make a new expr (to replace e) with -1*e.
	     */
	    ne = xmalloc(sizeof(expr));
	    ne->op = EXPR_MUL;
	    ne->filename = xstrdup(e->filename);
	    ne->line = e->line;
	    ne->numterms = 2;
	    ne->terms[0].type = EXPR_INT;
	    ne->terms[0].data.intn = intnum_new_int(-1);
	    ne->terms[1].type = EXPR_EXPR;
	    ne->terms[1].data.expn = e;
	    return ne;
    }
    return e;
}

/* Transforms negatives into expressions that are easier to combine:
 * -x -> -1*x
 * a-b -> a+(-1*b)
 *
 * Call post-order on an expression tree to transform the entire tree.
 *
 * Returns a possibly reallocated e.
 */
static expr *
expr_xform_neg(expr *e)
{
    switch (e->op) {
	case EXPR_NEG:
	    /* Turn -x into -1*x */
	    e->op = EXPR_IDENT;
	    return expr_xform_neg_helper(e);
	case EXPR_SUB:
	    /* Turn a-b into a+(-1*b) */

	    /* change op to ADD, and recursively negate right side (if expr) */
	    e->op = EXPR_ADD;
	    if (e->terms[1].type == EXPR_EXPR)
		e->terms[1].data.expn =
		    expr_xform_neg_helper(e->terms[1].data.expn);
	    else
		expr_xform_neg_item(e, &e->terms[1]);
	    break;
	default:
	    break;
    }

    return e;
}

/* Level an entire expn tree */
static expr *
expr_xform_neg_tree(expr *e)
{
    int i;

    if (!e)
	return 0;

    /* traverse terms */
    for (i=0; i<e->numterms; i++) {
	if (e->terms[i].type == EXPR_EXPR)
	    e->terms[i].data.expn = expr_xform_neg_tree(e->terms[i].data.expn);
    }

    /* do callback */
    return expr_xform_neg(e);
}

/* Look for simple identities that make the entire result constant:
 * 0*&x, -1|x, etc.
 */
static int
expr_is_constant(ExprOp op, intnum *intn)
{
    return ((intnum_is_zero(intn) && op == EXPR_MUL) ||
	    (intnum_is_zero(intn) && op == EXPR_AND) ||
	    (intnum_is_neg1(intn) && op == EXPR_OR));
}

/* Look for simple "left" identities like 0+x, 1*x, etc. */
static int
expr_can_delete_int_left(ExprOp op, intnum *intn)
{
    return ((intnum_is_pos1(intn) && op == EXPR_MUL) ||
	    (intnum_is_zero(intn) && op == EXPR_ADD) ||
	    (intnum_is_neg1(intn) && op == EXPR_AND) ||
	    (intnum_is_zero(intn) && op == EXPR_OR));
}

/* Look for simple "right" identities like x+|-0, x*&/1 */
static int
expr_can_delete_int_right(ExprOp op, intnum *intn)
{
    return ((intnum_is_pos1(intn) && op == EXPR_MUL) ||
	    (intnum_is_pos1(intn) && op == EXPR_DIV) ||
	    (intnum_is_zero(intn) && op == EXPR_ADD) ||
	    (intnum_is_zero(intn) && op == EXPR_SUB) ||
	    (intnum_is_neg1(intn) && op == EXPR_AND) ||
	    (intnum_is_zero(intn) && op == EXPR_OR) ||
	    (intnum_is_zero(intn) && op == EXPR_SHL) ||
	    (intnum_is_zero(intn) && op == EXPR_SHR));
}

/* Check for and simplify identities.  Returns new number of expr terms.
 * Sets e->op = EXPR_IDENT if numterms ends up being 1.
 * Uses numterms parameter instead of e->numterms for basis of "new" number
 * of terms.
 * Assumes int_term is *only* integer term in e.
 * NOTE: Really designed to only be used by expr_level_op().
 */
static int
expr_simplify_identity(expr *e, int numterms, int int_term)
{
    int i;

    /* Check for simple identities that delete the intnum.
     * Don't delete if the intnum is the only thing in the expn.
     */
    if ((int_term == 0 && numterms > 1 &&
	 expr_can_delete_int_left(e->op, e->terms[0].data.intn)) ||
	(int_term > 0 &&
	 expr_can_delete_int_right(e->op, e->terms[int_term].data.intn))) {
	/* Delete the intnum */
	intnum_delete(e->terms[int_term].data.intn);

	/* Slide everything to its right over by 1 */
	if (int_term != numterms-1) /* if it wasn't last.. */
	    memmove(&e->terms[int_term], &e->terms[int_term+1],
		    (numterms-1-int_term)*sizeof(ExprItem));

	/* Update numterms */
	numterms--;
    }

    /* Check for simple identites that delete everything BUT the intnum.
     * Don't bother if the intnum is the only thing in the expn.
     */
    if (numterms > 1 &&
	expr_is_constant(e->op, e->terms[int_term].data.intn)) {
	/* Loop through, deleting everything but the integer term */
	for (i=0; i<e->numterms; i++)
	    if (i != int_term)
		switch (e->terms[i].type) {
		    case EXPR_INT:
			intnum_delete(e->terms[i].data.intn);
			break;
		    case EXPR_FLOAT:
			floatnum_delete(e->terms[i].data.flt);
			break;
		    case EXPR_EXPR:
			expr_delete(e->terms[i].data.expn);
			break;
		    default:
			break;
		}

	/* Move integer term to the first term (if not already there) */
	if (int_term != 0)
	    e->terms[0] = e->terms[int_term];	/* structure copy */

	/* Set numterms to 1 */
	numterms = 1;
    }

    /* Change expression to IDENT if possible. */
    if (numterms == 1)
	e->op = EXPR_IDENT;

    /* Return the updated numterms */
    return numterms;
}

/* Levels the expression tree starting at e.  Eg:
 * a+(b+c) -> a+b+c
 * (a+b)+(c+d) -> a+b+c+d
 * Naturally, only levels operators that allow more than two operand terms.
 * NOTE: only does *one* level of leveling (no recursion).  Should be called
 *  post-order on a tree to combine deeper levels.
 * Also brings up any IDENT values into the current level (for ALL operators).
 * Folds (combines by evaluation) *integer* constant values if fold_const != 0.
 *
 * Returns a possibly reallocated e.
 */
static expr *
expr_level_op(expr *e, int fold_const)
{
    int i, j, o, fold_numterms, level_numterms, level_fold_numterms;
    int first_int_term = -1;

    /* Determine how many operands will need to be brought up (for leveling).
     * Go ahead and bring up any IDENT'ed values.
     */
    level_numterms = e->numterms;
    level_fold_numterms = 0;
    for (i=0; i<e->numterms; i++) {
	/* Search downward until we find something *other* than an
	 * IDENT, then bring it up to the current level.
	 */
	while (e->terms[i].type == EXPR_EXPR &&
	       e->terms[i].data.expn->op == EXPR_IDENT) {
	    expr *sube = e->terms[i].data.expn;
	    e->terms[i] = sube->terms[0];
	    free(sube->filename);
	    free(sube);
	}

	if (e->terms[i].type == EXPR_EXPR &&
	    e->terms[i].data.expn->op == e->op) {
		/* It's an expression w/the same operator, add in its numterms.
		 * But don't forget to subtract one for the expr itself!
		 */
		level_numterms += e->terms[i].data.expn->numterms - 1;

		/* If we're folding constants, count up the number of constants
		 * that will be merged in.
		 */
		if (fold_const)
		    for (j=0; j<e->terms[i].data.expn->numterms; j++)
			if (e->terms[i].data.expn->terms[j].type == EXPR_INT)
			    level_fold_numterms++;
	}

	/* Find the first integer term (if one is present) if we're folding
	 * constants.
	 */
	if (fold_const && first_int_term == -1 && e->terms[i].type == EXPR_INT)
	    first_int_term = i;
    }

    /* Look for other integer terms if there's one and combine.
     * Also eliminate empty spaces when combining and adjust numterms
     * variables.
     */
    fold_numterms = e->numterms;
    if (first_int_term != -1) {
	for (i=first_int_term+1, o=first_int_term+1; i<e->numterms; i++) {
	    if (e->terms[i].type == EXPR_INT) {
		intnum_calc(e->terms[first_int_term].data.intn, e->op,
			    e->terms[i].data.intn);
		fold_numterms--;
		level_numterms--;
		/* make sure to delete folded intnum */
		intnum_delete(e->terms[i].data.intn);
	    } else if (o != i) {
		/* copy term if it changed places */
		e->terms[o++] = e->terms[i];
	    }
	}

	/* Simplify identities and make IDENT if possible. */
	fold_numterms = expr_simplify_identity(e, fold_numterms,
					       first_int_term);
    }

    /* Only level operators that allow more than two operand terms.
     * Also don't bother leveling if it's not necessary to bring up any terms.
     */
    if ((e->op != EXPR_ADD && e->op != EXPR_MUL && e->op != EXPR_OR &&
	 e->op != EXPR_AND && e->op != EXPR_XOR) ||
	level_numterms <= fold_numterms) {
	/* Downsize e if necessary */
	if (fold_numterms < e->numterms && e->numterms > 2)
	    e = xrealloc(e, sizeof(expr)+((fold_numterms<2) ? 0 :
			 sizeof(ExprItem)*(fold_numterms-2)));
	/* Update numterms */
	e->numterms = fold_numterms;
	return e;
    }

    /* Adjust numterms for constant folding from terms being "pulled up".
     * Careful: if there's no integer term in e, then save space for it.
     */
    if (fold_const) {
	level_numterms -= level_fold_numterms;
	if (first_int_term == -1 && level_fold_numterms != 0)
	    level_numterms++;
    }

    /* Alloc more (or conceivably less, but not usually) space for e */
    e = xrealloc(e, sizeof(expr)+((level_numterms<2) ? 0 :
		 sizeof(ExprItem)*(level_numterms-2)));

    /* Copy up ExprItem's.  Iterate from right to left to keep the same
     * ordering as was present originally.
     * Combine integer terms as necessary.
     */
    for (i=e->numterms-1, o=level_numterms-1; i>=0; i--) {
	if (e->terms[i].type == EXPR_EXPR &&
	    e->terms[i].data.expn->op == e->op) {
	    /* bring up subexpression */
	    expr *sube = e->terms[i].data.expn;

	    /* copy terms right to left */
	    for (j=sube->numterms-1; j>=0; j--) {
		if (fold_const && sube->terms[j].type == EXPR_INT) {
		    /* Need to fold it in.. but if there's no int term already,
		     * just copy into a new one.
		     */
		    if (first_int_term == -1) {
			first_int_term = o--;
			e->terms[first_int_term] = sube->terms[j];  /* struc */
		    } else {
			intnum_calc(e->terms[first_int_term].data.intn, e->op,
				    sube->terms[j].data.intn);
			/* make sure to delete folded intnum */
			intnum_delete(sube->terms[j].data.intn);
		    }
		} else {
		    e->terms[o--] = sube->terms[j];	/* structure copy */
		}
	    }

	    /* delete subexpression, but *don't delete nodes* (as we've just
	     * copied them!)
	     */
	    free(sube->filename);
	    free(sube);
	} else if (o != i) {
	    /* copy operand if it changed places */
	    e->terms[o--] = e->terms[i];
	}
    }

    /* Simplify identities, make IDENT if possible, and save to e->numterms. */
    if (first_int_term != -1) {
	e->numterms = expr_simplify_identity(e, level_numterms,
					     first_int_term);
    } else {
	e->numterms = level_numterms;
	if (level_numterms == 1)
	    e->op = EXPR_IDENT;
    }

    return e;
}

/* Level an entire expn tree */
static expr *
expr_level_tree(expr *e, int fold_const)
{
    int i;

    if (!e)
	return 0;

    /* traverse terms */
    for (i=0; i<e->numterms; i++) {
	if (e->terms[i].type == EXPR_EXPR)
	    e->terms[i].data.expn = expr_level_tree(e->terms[i].data.expn,
						    fold_const);
    }

    /* do callback */
    return expr_level_op(e, fold_const);
}

/* Comparison function for expr_order_terms().
 * Assumes ExprType enum is in canonical order.
 */
static int
expr_order_terms_compare(const void *va, const void *vb)
{
    const ExprItem *a = va, *b = vb;
    return (a->type - b->type);
}

/* 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).
 */
static void
expr_order_terms(expr *e)
{
    /* don't bother reordering if only one element */
    if (e->numterms == 1)
	return;

    /* only reorder some types of operations */
    switch (e->op) {
	case EXPR_ADD:
	case EXPR_MUL:
	case EXPR_OR:
	case EXPR_AND:
	case EXPR_XOR:
	    /* Use mergesort to sort.  It's fast on already sorted values and a
	     * stable sort (multiple terms of same type are kept in the same
	     * order).
	     */
	    mergesort(e->terms, e->numterms, sizeof(ExprItem),
		      expr_order_terms_compare);
	    break;
	default:
	    break;
    }
}

/* Copy entire expression EXCEPT for index "except" at *top level only*. */
static expr *
expr_copy_except(const expr *e, int except)
{
    expr *n;
    int i;
    
    if (!e)
	return 0;

    n = xmalloc(sizeof(expr)+sizeof(ExprItem)*(e->numterms-2));

    n->op = e->op;
    n->filename = xstrdup(e->filename);
    n->line = e->line;
    n->numterms = e->numterms;
    for (i=0; i<e->numterms; i++) {
	ExprItem *dest = &n->terms[i];
	const ExprItem *src = &e->terms[i];

	if (i != except) {
	    dest->type = src->type;
	    switch (src->type) {
		case EXPR_SYM:
		    dest->data.sym = src->data.sym;
		    break;
		case EXPR_EXPR:
		    dest->data.expn = expr_copy_except(src->data.expn, -1);
		    break;
		case EXPR_INT:
		    dest->data.intn = intnum_copy(src->data.intn);
		    break;
		case EXPR_FLOAT:
		    dest->data.flt = floatnum_copy(src->data.flt);
		    break;
		case EXPR_REG:
		    dest->data.reg.num = src->data.reg.num;
		    dest->data.reg.size = src->data.reg.size;
		    break;
		default:
		    break;
	    }
	}
    }

    return n;
}

expr *
expr_copy(const expr *e)
{
    return expr_copy_except(e, -1);
}

static int
expr_delete_each(expr *e, void *d)
{
    int i;
    for (i=0; i<e->numterms; i++) {
	switch (e->terms[i].type) {
	    case EXPR_INT:
		intnum_delete(e->terms[i].data.intn);
		break;
	    case EXPR_FLOAT:
		floatnum_delete(e->terms[i].data.flt);
		break;
	    default:
		break;	/* none of the other types needs to be deleted */
	}
    }
    free(e->filename);
    free(e);	/* free ourselves */
    return 0;	/* don't stop recursion */
}

void
expr_delete(expr *e)
{
    expr_traverse_nodes_post(e, NULL, expr_delete_each);
}

static int
expr_contains_callback(ExprItem *ei, void *d)
{
    ExprType *t = d;
    return (ei->type & *t);
}

static int
expr_contains(expr *e, ExprType t)
{
    return expr_traverse_leaves_in(e, &t, expr_contains_callback);
}

typedef struct checkea_invalid16_data {
    int bx, si, di, bp;		/* total multiplier for each reg */
} checkea_invalid16_data;

/* Only works if ei->type == EXPR_REG (doesn't check).
 * Overwrites ei with intnum of 0 (to eliminate regs from the final expr).
 */
static int *
expr_checkea_get_reg16(ExprItem *ei, checkea_invalid16_data *data)
{
    /* in order: ax,cx,dx,bx,sp,bp,si,di */
    static int *reg16[8] = {0,0,0,0,0,0,0,0};
    int *ret;

    reg16[3] = &data->bx;
    reg16[5] = &data->bp;
    reg16[6] = &data->si;
    reg16[7] = &data->di;

    /* don't allow 32-bit registers */
    if (ei->data.reg.size != 16)
	return 0;

    ret = reg16[ei->data.reg.num & 7];	/* & 7 for sanity check */

    /* only allow BX, SI, DI, BP */
    if (!ret)
	return 0;

    /* overwrite with 0 to eliminate register from displacement expr */
    ei->type = EXPR_INT;
    ei->data.intn = intnum_new_int(0);

    /* we're okay */
    return ret;
}

/* Distribute over registers to help bring them to the topmost level of e.
 * Also check for illegal operations against registers.
 * Returns 0 if something was illegal, 1 if legal and nothing in e changed,
 * and 2 if legal and e needs to be simplified.
 *
 * Only half joking: Someday make this/checkea able to accept crazy things
 *  like: (bx+di)*(bx+di)-bx*bx-2*bx*di-di*di+di?  Probably not: NASM never
 *  accepted such things, and it's doubtful such an expn is valid anyway
 *  (even though the above one is).  But even macros would be hard-pressed
 *  to generate something like this.
 *
 * e must already have been simplified for this function to work properly
 * (as it doesn't think things like SUB are valid).
 *
 * IMPLEMENTATION NOTE: About the only thing this function really needs to
 * "distribute" is: (non-float-expn or intnum) * (sum expn of registers).
 *
 * TODO: Clean up this code, make it easier to understand.
 */
static int
expr_checkea_distcheck_reg(expr **ep)
{
    expr *e = *ep;
    int i;
    int havereg = -1, havereg_expr = -1;
    int retval = 1;	/* default to legal, no changes */

    for (i=0; i<e->numterms; i++) {
	switch (e->terms[i].type) {
	    case EXPR_REG:
		/* Check op to make sure it's valid to use w/register. */
		if (e->op != EXPR_ADD && e->op != EXPR_MUL &&
		    e->op != EXPR_IDENT)
		    return 0;
		/* Check for reg*reg */
		if (e->op == EXPR_MUL && havereg != -1)
		    return 0;
		havereg = i;
		break;
	    case EXPR_FLOAT:
		/* Floats not allowed. */
		return 0;
	    case EXPR_EXPR:
		if (expr_contains(e->terms[i].data.expn, EXPR_REG)) {
		    int ret2;

		    /* Check op to make sure it's valid to use w/register. */
		    if (e->op != EXPR_ADD && e->op != EXPR_MUL)
			return 0;
		    /* Check for reg*reg */
		    if (e->op == EXPR_MUL && havereg != -1)
			return 0;
		    havereg = i;
		    havereg_expr = i;
		    /* Recurse to check lower levels */
		    ret2 = expr_checkea_distcheck_reg(&e->terms[i].data.expn);
		    if (ret2 == 0)
			return 0;
		    if (ret2 == 2)
			retval = 2;
		} else if (expr_contains(e->terms[i].data.expn, EXPR_FLOAT))
		    return 0;	/* Disallow floats */
		break;
	    default:
		break;
	}
    }

    /* just exit if no registers were used */
    if (havereg == -1)
	return retval;

    /* Distribute */
    if (e->op == EXPR_MUL && havereg_expr != -1) {
	expr *ne;

	retval = 2;	/* we're going to change it */

	/* The reg expn *must* be EXPR_ADD at this point.  Sanity check. */
	if (e->terms[havereg_expr].type != EXPR_EXPR ||
	    e->terms[havereg_expr].data.expn->op != EXPR_ADD)
	    InternalError(__LINE__, __FILE__,
			  _("Register expression not ADD or EXPN"));

	/* Iterate over each term in reg expn */
	for (i=0; i<e->terms[havereg_expr].data.expn->numterms; i++) {
	    /* Copy everything EXCEPT havereg_expr term into new expression */
	    ne = expr_copy_except(e, havereg_expr);
	    /* Copy reg expr term into uncopied (empty) term in new expn */
	    ne->terms[havereg_expr] =
		e->terms[havereg_expr].data.expn->terms[i]; /* struct copy */
	    /* Overwrite old reg expr term with new expn */
	    e->terms[havereg_expr].data.expn->terms[i].type = EXPR_EXPR;
	    e->terms[havereg_expr].data.expn->terms[i].data.expn = ne;
	}

	/* Replace e with expanded reg expn */
	ne = e->terms[havereg_expr].data.expn;
	e->terms[havereg_expr].type = EXPR_NONE;    /* don't delete it! */
	expr_delete(e);				    /* but everything else */
	e = ne;
	*ep = ne;
    }

    return retval;
}

static int
expr_checkea_getregsize_callback(ExprItem *ei, void *d)
{
    unsigned char *addrsize = (unsigned char *)d;

    if (ei->type == EXPR_REG) {
	*addrsize = ei->data.reg.size;
	return 1;
    } else
	return 0;
}

int
expr_checkea(expr **ep, unsigned char *addrsize, unsigned char bits,
	     unsigned char *displen, unsigned char *modrm,
	     unsigned char *v_modrm, unsigned char *n_modrm,
	     unsigned char *sib, unsigned char *v_sib, unsigned char *n_sib)
{
    expr *e = *ep;
    const intnum *intn;
    long dispval;
    int i;
    int *reg;

    if (*addrsize == 0) {
	/* we need to figure out the address size from what we know about:
	 * - the displacement length
	 * - what registers are used in the expression
	 * - the bits setting
	 */
	switch (*displen) {
	    case 4:
		/* must be 32-bit */
		*addrsize = 32;
		break;
	    case 2:
		/* must be 16-bit */
		*addrsize = 16;
		break;
	    default:
		/* check for use of 16 or 32-bit registers; if none are used
		 * default to bits setting.
		 */
		if (!expr_traverse_leaves_in(e, addrsize,
					     expr_checkea_getregsize_callback))
		    *addrsize = bits;
		/* TODO: Add optional warning here if switched address size
		 * from bits setting just by register use.. eg [ax] in
		 * 32-bit mode would generate a warning.
		 */
	}
    }

    if (*addrsize == 32 && (*n_modrm || *n_sib)) {
    } else if (*addrsize == 16 && *n_modrm) {
	static const unsigned char modrm16[16] = {
	    0006 /* disp16  */, 0007 /* [BX]    */, 0004 /* [SI]    */,
	    0000 /* [BX+SI] */, 0005 /* [DI]    */, 0001 /* [BX+DI] */,
	    0377 /* invalid */, 0377 /* invalid */, 0006 /* [BP]+d  */,
	    0377 /* invalid */, 0002 /* [BP+SI] */, 0377 /* invalid */,
	    0003 /* [BP+DI] */, 0377 /* invalid */, 0377 /* invalid */,
	    0377 /* invalid */
	};
	checkea_invalid16_data data;
	enum {
	    HAVE_NONE = 0,
	    HAVE_BX = 1<<0,
	    HAVE_SI = 1<<1,
	    HAVE_DI = 1<<2,
	    HAVE_BP = 1<<3
	} havereg = HAVE_NONE;

	data.bx = 0;
	data.si = 0;
	data.di = 0;
	data.bp = 0;

	/* 16-bit cannot have SIB */
	*sib = 0;
	*v_sib = 0;
	*n_sib = 0;

	/* Determine if expression is superficially valid:
	 * Valid expr should be [(int-equiv expn)]+[reg*(int-equiv expn)+...]
	 * where the [...] parts are optional.
	 * To check this, first look at top expn operator.. if it's not ADD or
	 * MUL, then no registers are valid for use.
	 */
	*ep = expr_simplify(*ep);
	e = *ep;
	switch (expr_checkea_distcheck_reg(ep)) {
	    case 0:
		ErrorAt(e->filename, e->line, _("invalid effective address"));
		return 0;
	    case 2:
		/* Need to simplify again */
		*ep = expr_simplify(*ep);
		e = *ep;
		break;
	    default:
		break;
	}

	switch (e->op) {
	    case EXPR_ADD:
		/* Prescan for non-int multipliers.
		 * This is because if any of the terms is a more complex
		 * expr (eg, undetermined value), we don't want to try to
		 * figure out *any* of the expression, because each register
		 * lookup overwrites the register with a 0 value!  And storing
		 * the state of this routine from one excution to the next
		 * would be a major chore.
		 */
		for (i=0; i<e->numterms; i++)
		    if (e->terms[i].type == EXPR_EXPR) {
			if (e->terms[i].data.expn->numterms > 2)
			    return 1;
			expr_order_terms(e->terms[i].data.expn);
			if (e->terms[i].data.expn->terms[1].type != EXPR_INT)
			    return 1;
		    }

		/* FALLTHROUGH */
	    case EXPR_IDENT:
		/* Check each term for register (and possible multiplier). */
		for (i=0; i<e->numterms; i++) {
		    if (e->terms[i].type == EXPR_REG) {
			reg = expr_checkea_get_reg16(&e->terms[i], &data);
			if (!reg) {
			    ErrorAt(e->filename, e->line,
				    _("invalid effective address"));
			    return 0;
			}
			(*reg)++;
		    } else if (e->terms[i].type == EXPR_EXPR) {
			/* Already ordered from ADD above, just grab the value.
			 * Sanity check for EXPR_INT.
			 */
			if (e->terms[i].data.expn->terms[0].type != EXPR_REG)
			    InternalError(__LINE__, __FILE__,
					  _("Register not found in reg expn"));
			if (e->terms[i].data.expn->terms[1].type != EXPR_INT)
			    InternalError(__LINE__, __FILE__,
					  _("Non-integer value in reg expn"));
			reg =
			    expr_checkea_get_reg16(&e->terms[i].data.expn->terms[0],
						   &data);
			if (!reg) {
			    ErrorAt(e->filename, e->line,
				    _("invalid effective address"));
			    return 0;
			}
			(*reg) +=
			    intnum_get_int(e->terms[i].data.expn->terms[1].data.intn);
		    }
		}
		break;
	    case EXPR_MUL:
		/* Here, too, check for non-int multipliers. */
		if (e->numterms > 2)
		    return 1;
		expr_order_terms(e);
		if (e->terms[1].type != EXPR_INT)
		    return 1;
		reg = expr_checkea_get_reg16(&e->terms[0], &data);
		if (!reg) {
		    ErrorAt(e->filename, e->line,
			    _("invalid effective address"));
		    return 0;
		}
		(*reg) += intnum_get_int(e->terms[1].data.intn);
		break;
	    default:
		/* Should never get here! */
		break;
	}

	/* negative reg multipliers are illegal. */
	if (data.bx < 0 || data.si < 0 || data.di < 0 || data.bp < 0) {
	    ErrorAt(e->filename, e->line, _("invalid effective address"));
	    return 0;
	}

	/* Set havereg appropriately */
	if (data.bx > 0)
	    havereg |= HAVE_BX;
	if (data.si > 0)
	    havereg |= HAVE_SI;
	if (data.di > 0)
	    havereg |= HAVE_DI;
	if (data.bp > 0)
	    havereg |= HAVE_BP;

	/* Simplify expr, which is now really just the displacement. This
	 * should get rid of the 0's we put in for registers in the callback.
	 */
	*ep = expr_simplify(*ep);
	e = *ep;

	/* Check the modrm value for invalid combinations. */
	if (modrm16[havereg] & 0070) {
	    ErrorAt(e->filename, e->line, _("invalid effective address"));
	    return 0;
	}

	*modrm |= modrm16[havereg];

	*v_modrm = 0;	/* default to not yet valid */

	switch (*displen) {
	    case 0:
		/* the displacement length hasn't been forced, try to
		 * determine what it is.
		 */
		switch (havereg) {
		    case HAVE_NONE:
			/* no register in expression, so it must be disp16, and
			 * as the Mod bits are set to 0 above, we're done with
			 * the ModRM byte.
			 */
			*displen = 2;
			*v_modrm = 1;
			break;
		    case HAVE_BP:
			/* for BP, there *must* be a displacement value, but we
			 * may not know the size (8 or 16) for sure right now.
			 * We can't leave displen at 0, because that just means
			 * unknown displacement, including none.
			 */
			*displen = 0xff;
			break;
		    default:
			break;
		}

		intn = expr_get_intnum(ep);
		if (!intn)
		    break;	    /* expr still has unknown values */

		/* make sure the displacement will fit in 16 bits if unsigned,
		 * and 8 bits if signed.
		 */
		if (!intnum_check_size(intn, 2, 0) &&
		    !intnum_check_size(intn, 1, 1)) {
		    ErrorAt(e->filename, e->line,
			    _("invalid effective address"));
		    return 0;
		}

		/* don't try to find out what size displacement we have if
		 * displen is known.
		 */
		if (*displen != 0 && *displen != 0xff)
		    break;

		/* Don't worry about overflows here (it's already guaranteed
		 * to be 16 or 8 bits).
		 */
		dispval = intnum_get_int(intn);

		/* Figure out what size displacement we will have. */
		if (*displen != 0xff && dispval == 0) {
		    /* if we know that the displacement is 0 right now,
		     * go ahead and delete the expr (making it so no
		     * displacement value is included in the output).
		     * The Mod bits of ModRM are set to 0 above, and
		     * we're done with the ModRM byte!
		     *
		     * Don't do this if we came from HAVE_BP above, so
		     * check *displen.
		     */
		    expr_delete(e);
		    *ep = (expr *)NULL;
		} else if (dispval >= -128 && dispval <= 127) {
		    /* It fits into a signed byte */
		    *displen = 1;
		    *modrm |= 0100;
		} else {
		    /* It's a 16-bit displacement */
		    *displen = 2;
		    *modrm |= 0200;
		}
		*v_modrm = 1;	/* We're done with ModRM */

		break;

	    /* If not 0, the displacement length was forced; set the Mod bits
	     * appropriately and we're done with the ModRM byte.  We assume
	     * that the user knows what they're doing if they do an explicit
	     * override, so we don't check for overflow (we'll just truncate
	     * when we output).
	     */
	    case 1:
		*modrm |= 0100;
		*v_modrm = 1;
		break;
	    case 2:
		*modrm |= 0200;
		*v_modrm = 1;
		break;
	    default:
		/* any other size is an error */
		ErrorAt(e->filename, e->line,
			_("invalid effective address (displacement size)"));
		return 0;
	}
    }
    return 1;
}

static int
expr_expand_equ_callback(ExprItem *ei, void *d)
{
    const expr *equ_expr;
    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);
	}
    }
    return 0;
}

void
expr_expand_equ(expr *e)
{
    expr_traverse_leaves_in(e, NULL, expr_expand_equ_callback);
}

/* Traverse over expression tree, calling func for each operation AFTER the
 * branches (if expressions) have been traversed (eg, postorder
 * traversal).  The data pointer d is passed to each func call.
 *
 * Stops early (and returns 1) if func returns 1.  Otherwise returns 0.
 */
static int
expr_traverse_nodes_post(expr *e, void *d, int (*func) (expr *e, void *d))
{
    int i;

    if (!e)
	return 0;

    /* traverse terms */
    for (i=0; i<e->numterms; i++) {
	if (e->terms[i].type == EXPR_EXPR &&
	    expr_traverse_nodes_post(e->terms[i].data.expn, d, func))
	    return 1;
    }

    /* do callback */
    return func(e, d);
}

/* 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.
 */
static int
expr_traverse_leaves_in(expr *e, void *d,
			int (*func) (ExprItem *ei, void *d))
{
    int i;

    if (!e)
	return 0;

    for (i=0; i<e->numterms; i++) {
	if (e->terms[i].type == EXPR_EXPR) {
	    if (expr_traverse_leaves_in(e->terms[i].data.expn, d, func))
		return 1;
	} else {
	    if (func(&e->terms[i], d))
		return 1;
	}
    }
    return 0;
}

/* Simplify expression by getting rid of unnecessary branches. */
expr *
expr_simplify(expr *e)
{
    e = expr_xform_neg_tree(e);
    e = expr_level_tree(e, 1);
    return e;
}

const intnum *
expr_get_intnum(expr **ep)
{
    *ep = expr_simplify(*ep);

    if ((*ep)->op == EXPR_IDENT && (*ep)->terms[0].type == EXPR_INT)
	return (*ep)->terms[0].data.intn;
    else
	return (intnum *)NULL;
}

void
expr_print(expr *e)
{
    static const char *regs[] = {"ax","cx","dx","bx","sp","bp","si","di"};
    char opstr[3];
    int i;

    switch (e->op) {
	case EXPR_ADD:
	    strcpy(opstr, "+");
	    break;
	case EXPR_SUB:
	    strcpy(opstr, "-");
	    break;
	case EXPR_MUL:
	    strcpy(opstr, "*");
	    break;
	case EXPR_DIV:
	    strcpy(opstr, "/");
	    break;
	case EXPR_SIGNDIV:
	    strcpy(opstr, "//");
	    break;
	case EXPR_MOD:
	    strcpy(opstr, "%");
	    break;
	case EXPR_SIGNMOD:
	    strcpy(opstr, "%%");
	    break;
	case EXPR_NEG:
	    strcpy(opstr, "-");
	    break;
	case EXPR_NOT:
	    strcpy(opstr, "~");
	    break;
	case EXPR_OR:
	    strcpy(opstr, "|");
	    break;
	case EXPR_AND:
	    strcpy(opstr, "&");
	    break;
	case EXPR_XOR:
	    strcpy(opstr, "^");
	    break;
	case EXPR_SHL:
	    strcpy(opstr, "<<");
	    break;
	case EXPR_SHR:
	    strcpy(opstr, ">>");
	    break;
	case EXPR_LOR:
	    strcpy(opstr, "||");
	    break;
	case EXPR_LAND:
	    strcpy(opstr, "&&");
	    break;
	case EXPR_LNOT:
	    strcpy(opstr, "!");
	    break;
	case EXPR_LT:
	    strcpy(opstr, "<");
	    break;
	case EXPR_GT:
	    strcpy(opstr, ">");
	    break;
	case EXPR_LE:
	    strcpy(opstr, "<=");
	    break;
	case EXPR_GE:
	    strcpy(opstr, ">=");
	    break;
	case EXPR_NE:
	    strcpy(opstr, "!=");
	    break;
	case EXPR_EQ:
	    strcpy(opstr, "==");
	    break;
	case EXPR_IDENT:
	    opstr[0] = 0;
	    break;
    }
    for (i=0; i<e->numterms; i++) {
	switch (e->terms[i].type) {
	    case EXPR_SYM:
		printf("%s", symrec_get_name(e->terms[i].data.sym));
		break;
	    case EXPR_EXPR:
		printf("(");
		expr_print(e->terms[i].data.expn);
		printf(")");
		break;
	    case EXPR_INT:
		intnum_print(e->terms[i].data.intn);
		break;
	    case EXPR_FLOAT:
		floatnum_print(e->terms[i].data.flt);
		break;
	    case EXPR_REG:
		if (e->terms[i].data.reg.size == 32)
		    printf("e");
		printf("%s", regs[e->terms[i].data.reg.num&7]);
		break;
	    case EXPR_NONE:
		break;
	}
	if (i < e->numterms-1)
	    printf("%s", opstr);
    }
}