blob: f7cfbceeddb89be6a1ffa1cad523f6deaf43f32c [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
H. Peter Anvin164d2462017-02-20 02:39:56 -08002 *
3 * Copyright 1996-2017 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
6 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
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.
H. Peter Anvin164d2462017-02-20 02:39:56 -080017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * 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.
31 *
32 * ----------------------------------------------------------------------- */
33
H. Peter Anvin87f252a2007-09-19 21:40:37 -070034/*
35 * exprlib.c
36 *
37 * Library routines to manipulate expression data types.
38 */
39
40#include "nasm.h"
41
42/*
H. Peter Anvin6867acc2007-10-10 14:58:45 -070043 * Return true if the argument is a simple scalar. (Or a far-
H. Peter Anvin87f252a2007-09-19 21:40:37 -070044 * absolute, which counts.)
45 */
H. Peter Anvin164d2462017-02-20 02:39:56 -080046bool is_simple(const expr *vect)
H. Peter Anvin87f252a2007-09-19 21:40:37 -070047{
48 while (vect->type && !vect->value)
49 vect++;
50 if (!vect->type)
H. Peter Anvin164d2462017-02-20 02:39:56 -080051 return true;
H. Peter Anvin87f252a2007-09-19 21:40:37 -070052 if (vect->type != EXPR_SIMPLE)
H. Peter Anvin164d2462017-02-20 02:39:56 -080053 return false;
H. Peter Anvin87f252a2007-09-19 21:40:37 -070054 do {
55 vect++;
56 } while (vect->type && !vect->value);
57 if (vect->type && vect->type < EXPR_SEGBASE + SEG_ABS)
H. Peter Anvin164d2462017-02-20 02:39:56 -080058 return false;
59 return true;
H. Peter Anvin87f252a2007-09-19 21:40:37 -070060}
61
62/*
H. Peter Anvin6867acc2007-10-10 14:58:45 -070063 * Return true if the argument is a simple scalar, _NOT_ a far-
H. Peter Anvin87f252a2007-09-19 21:40:37 -070064 * absolute.
65 */
H. Peter Anvin164d2462017-02-20 02:39:56 -080066bool is_really_simple(const expr *vect)
H. Peter Anvin87f252a2007-09-19 21:40:37 -070067{
68 while (vect->type && !vect->value)
69 vect++;
70 if (!vect->type)
H. Peter Anvin164d2462017-02-20 02:39:56 -080071 return true;
H. Peter Anvin87f252a2007-09-19 21:40:37 -070072 if (vect->type != EXPR_SIMPLE)
H. Peter Anvin164d2462017-02-20 02:39:56 -080073 return false;
H. Peter Anvin87f252a2007-09-19 21:40:37 -070074 do {
75 vect++;
76 } while (vect->type && !vect->value);
77 if (vect->type)
H. Peter Anvin164d2462017-02-20 02:39:56 -080078 return false;
79 return true;
H. Peter Anvin87f252a2007-09-19 21:40:37 -070080}
81
82/*
H. Peter Anvin6867acc2007-10-10 14:58:45 -070083 * Return true if the argument is relocatable (i.e. a simple
H. Peter Anvin164d2462017-02-20 02:39:56 -080084 * scalar, plus at most one segment-base, possibly a subtraction
85 * of the current segment base, plus possibly a WRT).
H. Peter Anvin87f252a2007-09-19 21:40:37 -070086 */
H. Peter Anvin164d2462017-02-20 02:39:56 -080087bool is_reloc(const expr *vect)
H. Peter Anvin87f252a2007-09-19 21:40:37 -070088{
H. Peter Anvin164d2462017-02-20 02:39:56 -080089 bool has_rel = false; /* Has a self-segment-subtract */
90 bool has_seg = false; /* Has a segment base */
91
92 for (; vect->type; vect++) {
93 if (!vect->value) {
94 /* skip value-0 terms */
95 continue;
96 } else if (vect->type < EXPR_SIMPLE) {
97 /* false if a register is present */
98 return false;
99 } else if (vect->type == EXPR_SIMPLE) {
100 /* skip over a pure number term... */
101 continue;
102 } else if (vect->type == EXPR_WRT) {
103 /* skip over a WRT term... */
104 continue;
105 } else if (vect->type < EXPR_SEGBASE) {
106 /* other special type -> problem */
107 return false;
108 } else if (vect->value == 1) {
109 if (has_seg)
110 return false; /* only one segbase allowed */
111 has_seg = true;
112 } else if (vect->value == -1) {
113 if (vect->type != location.segment + EXPR_SEGBASE)
114 return false; /* can only subtract current segment */
115 if (has_rel)
116 return false; /* already is relative */
117 has_rel = true;
118 }
H. Peter Anvin87f252a2007-09-19 21:40:37 -0700119 }
H. Peter Anvin164d2462017-02-20 02:39:56 -0800120
121 return true;
H. Peter Anvin87f252a2007-09-19 21:40:37 -0700122}
123
124/*
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700125 * Return true if the argument contains an `unknown' part.
H. Peter Anvin87f252a2007-09-19 21:40:37 -0700126 */
H. Peter Anvin164d2462017-02-20 02:39:56 -0800127bool is_unknown(const expr *vect)
H. Peter Anvin87f252a2007-09-19 21:40:37 -0700128{
129 while (vect->type && vect->type < EXPR_UNKNOWN)
130 vect++;
131 return (vect->type == EXPR_UNKNOWN);
132}
133
134/*
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700135 * Return true if the argument contains nothing but an `unknown'
H. Peter Anvin87f252a2007-09-19 21:40:37 -0700136 * part.
137 */
H. Peter Anvin164d2462017-02-20 02:39:56 -0800138bool is_just_unknown(const expr *vect)
H. Peter Anvin87f252a2007-09-19 21:40:37 -0700139{
140 while (vect->type && !vect->value)
141 vect++;
142 return (vect->type == EXPR_UNKNOWN);
143}
144
145/*
146 * Return the scalar part of a relocatable vector. (Including
147 * simple scalar vectors - those qualify as relocatable.)
148 */
H. Peter Anvin164d2462017-02-20 02:39:56 -0800149int64_t reloc_value(const expr *vect)
H. Peter Anvin87f252a2007-09-19 21:40:37 -0700150{
151 while (vect->type && !vect->value)
152 vect++;
153 if (!vect->type)
154 return 0;
155 if (vect->type == EXPR_SIMPLE)
156 return vect->value;
157 else
158 return 0;
159}
160
161/*
162 * Return the segment number of a relocatable vector, or NO_SEG for
163 * simple scalars.
164 */
H. Peter Anvin164d2462017-02-20 02:39:56 -0800165int32_t reloc_seg(const expr *vect)
H. Peter Anvin87f252a2007-09-19 21:40:37 -0700166{
H. Peter Anvin164d2462017-02-20 02:39:56 -0800167 for (; vect->type; vect++) {
168 if (vect->type >= EXPR_SEGBASE && vect->value == 1)
169 return vect->type - EXPR_SEGBASE;
H. Peter Anvin87f252a2007-09-19 21:40:37 -0700170 }
H. Peter Anvin164d2462017-02-20 02:39:56 -0800171
172 return NO_SEG;
H. Peter Anvin87f252a2007-09-19 21:40:37 -0700173}
174
175/*
176 * Return the WRT segment number of a relocatable vector, or NO_SEG
177 * if no WRT part is present.
178 */
H. Peter Anvin164d2462017-02-20 02:39:56 -0800179int32_t reloc_wrt(const expr *vect)
H. Peter Anvin87f252a2007-09-19 21:40:37 -0700180{
181 while (vect->type && vect->type < EXPR_WRT)
182 vect++;
183 if (vect->type == EXPR_WRT) {
184 return vect->value;
185 } else
186 return NO_SEG;
187}
H. Peter Anvin164d2462017-02-20 02:39:56 -0800188
189/*
190 * Return true if this expression contains a subtraction of the location
191 */
192bool is_self_relative(const expr *vect)
193{
194 for (; vect->type; vect++) {
195 if (vect->type == location.segment + EXPR_SEGBASE && vect->value == -1)
196 return true;
197 }
198
199 return false;
200}