1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2017 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
37 * Library routines to manipulate expression data types.
43 * Return true if the argument is a simple scalar. (Or a far-
44 * absolute, which counts.)
46 bool is_simple(const expr
*vect
)
48 while (vect
->type
&& !vect
->value
)
52 if (vect
->type
!= EXPR_SIMPLE
)
56 } while (vect
->type
&& !vect
->value
);
57 if (vect
->type
&& vect
->type
< EXPR_SEGBASE
+ SEG_ABS
)
63 * Return true if the argument is a simple scalar, _NOT_ a far-
66 bool is_really_simple(const expr
*vect
)
68 while (vect
->type
&& !vect
->value
)
72 if (vect
->type
!= EXPR_SIMPLE
)
76 } while (vect
->type
&& !vect
->value
);
83 * Return true if the argument is relocatable (i.e. a simple
84 * scalar, plus at most one segment-base, possibly a subtraction
85 * of the current segment base, plus possibly a WRT).
87 bool is_reloc(const expr
*vect
)
89 bool has_rel
= false; /* Has a self-segment-subtract */
90 bool has_seg
= false; /* Has a segment base */
92 for (; vect
->type
; vect
++) {
94 /* skip value-0 terms */
96 } else if (vect
->type
< EXPR_SIMPLE
) {
97 /* false if a register is present */
99 } else if (vect
->type
== EXPR_SIMPLE
) {
100 /* skip over a pure number term... */
102 } else if (vect
->type
== EXPR_WRT
) {
103 /* skip over a WRT term... */
105 } else if (vect
->type
< EXPR_SEGBASE
) {
106 /* other special type -> problem */
108 } else if (vect
->value
== 1) {
110 return false; /* only one segbase allowed */
112 } else if (vect
->value
== -1) {
113 if (vect
->type
!= location
.segment
+ EXPR_SEGBASE
)
114 return false; /* can only subtract current segment */
116 return false; /* already is relative */
125 * Return true if the argument contains an `unknown' part.
127 bool is_unknown(const expr
*vect
)
129 while (vect
->type
&& vect
->type
< EXPR_UNKNOWN
)
131 return (vect
->type
== EXPR_UNKNOWN
);
135 * Return true if the argument contains nothing but an `unknown'
138 bool is_just_unknown(const expr
*vect
)
140 while (vect
->type
&& !vect
->value
)
142 return (vect
->type
== EXPR_UNKNOWN
);
146 * Return the scalar part of a relocatable vector. (Including
147 * simple scalar vectors - those qualify as relocatable.)
149 int64_t reloc_value(const expr
*vect
)
151 while (vect
->type
&& !vect
->value
)
155 if (vect
->type
== EXPR_SIMPLE
)
162 * Return the segment number of a relocatable vector, or NO_SEG for
165 int32_t reloc_seg(const expr
*vect
)
167 for (; vect
->type
; vect
++) {
168 if (vect
->type
>= EXPR_SEGBASE
&& vect
->value
== 1)
169 return vect
->type
- EXPR_SEGBASE
;
176 * Return the WRT segment number of a relocatable vector, or NO_SEG
177 * if no WRT part is present.
179 int32_t reloc_wrt(const expr
*vect
)
181 while (vect
->type
&& vect
->type
< EXPR_WRT
)
183 if (vect
->type
== EXPR_WRT
) {
190 * Return true if this expression contains a subtraction of the location
192 bool is_self_relative(const expr
*vect
)
194 for (; vect
->type
; vect
++) {
195 if (vect
->type
== location
.segment
+ EXPR_SEGBASE
&& vect
->value
== -1)