blob: cf625c29c92f112c040f862a168aab856ceaead1 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006 *
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:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000010 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070011 * * 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.
17 *
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.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * preproc.c macro preprocessor for the Netwide Assembler
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000036 */
37
H. Peter Anvin4836e332002-04-30 20:56:43 +000038/* Typical flow of text through preproc
39 *
Keith Kaniosb7a89542007-04-12 02:40:54 +000040 * pp_getline gets tokenized lines, either
H. Peter Anvin4836e332002-04-30 20:56:43 +000041 *
42 * from a macro expansion
43 *
44 * or
45 * {
46 * read_line gets raw text from stdmacpos, or predef, or current input file
Keith Kaniosb7a89542007-04-12 02:40:54 +000047 * tokenize converts to tokens
H. Peter Anvin4836e332002-04-30 20:56:43 +000048 * }
49 *
50 * expand_mmac_params is used to expand %1 etc., unless a macro is being
51 * defined or a false conditional is being processed
52 * (%0, %1, %+1, %-1, %%foo
53 *
54 * do_directive checks for directives
55 *
56 * expand_smacro is used to expand single line macros
57 *
58 * expand_mmacro is used to expand multi-line macros
59 *
60 * detoken is used to convert the line back to text
61 */
H. Peter Anvineba20a72002-04-30 20:53:55 +000062
H. Peter Anvinfe501952007-10-02 21:53:51 -070063#include "compiler.h"
64
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000065#include <stdio.h>
H. Peter Anvinaf535c12002-04-30 20:59:21 +000066#include <stdarg.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000067#include <stdlib.h>
68#include <stddef.h>
69#include <string.h>
70#include <ctype.h>
H. Peter Anvin76690a12002-04-30 20:52:49 +000071#include <limits.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000072#include <inttypes.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000073
74#include "nasm.h"
75#include "nasmlib.h"
H. Peter Anvin4169a472007-09-12 01:29:43 +000076#include "preproc.h"
H. Peter Anvin97a23472007-09-16 17:57:25 -070077#include "hashtbl.h"
H. Peter Anvin8cad14b2008-06-01 17:23:51 -070078#include "quote.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070079#include "stdscan.h"
80#include "tokens.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070081#include "tables.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000082
83typedef struct SMacro SMacro;
84typedef struct MMacro MMacro;
Keith Kanios891775e2009-07-11 06:08:54 -050085typedef struct MMacroInvocation MMacroInvocation;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000086typedef struct Context Context;
87typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000088typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000089typedef struct Line Line;
90typedef struct Include Include;
91typedef struct Cond Cond;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000092typedef struct IncPath IncPath;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000093
94/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070095 * Note on the storage of both SMacro and MMacros: the hash table
96 * indexes them case-insensitively, and we then have to go through a
97 * linked list of potential case aliases (and, for MMacros, parameter
98 * ranges); this is to preserve the matching semantics of the earlier
99 * code. If the number of case aliases for a specific macro is a
100 * performance issue, you may want to reconsider your coding style.
101 */
102
103/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000104 * Store the definition of a single-line macro.
105 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000106struct SMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000107 SMacro *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000108 char *name;
H. Peter Anvin70055962007-10-11 00:05:31 -0700109 bool casesense;
H. Peter Anvin16ed4382007-10-11 10:06:19 -0700110 bool in_progress;
H. Peter Anvin70055962007-10-11 00:05:31 -0700111 unsigned int nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000112 Token *expansion;
113};
114
115/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000116 * Store the definition of a multi-line macro. This is also used to
117 * store the interiors of `%rep...%endrep' blocks, which are
118 * effectively self-re-invoking multi-line macros which simply
119 * don't have a name or bother to appear in the hash tables. %rep
120 * blocks are signified by having a NULL `name' field.
121 *
122 * In a MMacro describing a `%rep' block, the `in_progress' field
123 * isn't merely boolean, but gives the number of repeats left to
124 * run.
125 *
126 * The `next' field is used for storing MMacros in hash tables; the
127 * `next_active' field is for stacking them on istk entries.
128 *
129 * When a MMacro is being expanded, `params', `iline', `nparam',
130 * `paramlen', `rotate' and `unique' are local to the invocation.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000131 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000132struct MMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000133 MMacro *next;
Keith Kanios891775e2009-07-11 06:08:54 -0500134 MMacroInvocation *prev; /* previous invocation */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000135 char *name;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -0700136 int nparam_min, nparam_max;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700137 bool casesense;
138 bool plus; /* is the last parameter greedy? */
139 bool nolist; /* is this macro listing-inhibited? */
Keith Kanios891775e2009-07-11 06:08:54 -0500140 int64_t in_progress; /* is this macro currently being expanded? */
141 int64_t max_depth; /* maximum number of recursive expansions allowed */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000142 Token *dlist; /* All defaults as one list */
143 Token **defaults; /* Parameter default pointers */
144 int ndefs; /* number of default parameters */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000145 Line *expansion;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000146
147 MMacro *next_active;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000148 MMacro *rep_nest; /* used for nesting %rep */
149 Token **params; /* actual parameters */
150 Token *iline; /* invocation line */
H. Peter Anvin25a99342007-09-22 17:45:45 -0700151 unsigned int nparam, rotate;
152 int *paramlen;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -0700153 uint64_t unique;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000154 int lineno; /* Current line number on expansion */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000155};
156
Keith Kanios891775e2009-07-11 06:08:54 -0500157
158/* Store the definition of a multi-line macro, as defined in a
159 * previous recursive macro expansion.
160 */
161struct MMacroInvocation {
162 MMacroInvocation *prev; /* previous invocation */
163 Token **params; /* actual parameters */
164 Token *iline; /* invocation line */
165 unsigned int nparam, rotate;
166 int *paramlen;
167 uint64_t unique;
168};
169
170
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000171/*
172 * The context stack is composed of a linked list of these.
173 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000174struct Context {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000175 Context *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000176 char *name;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700177 struct hash_table localmac;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000178 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000179};
180
181/*
182 * This is the internal form which we break input lines up into.
183 * Typically stored in linked lists.
184 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000185 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
186 * necessarily used as-is, but is intended to denote the number of
187 * the substituted parameter. So in the definition
188 *
189 * %define a(x,y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700190 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000191 * the token representing `x' will have its type changed to
192 * TOK_SMAC_PARAM, but the one representing `y' will be
193 * TOK_SMAC_PARAM+1.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000194 *
195 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
196 * which doesn't need quotes around it. Used in the pre-include
197 * mechanism as an alternative to trying to find a sensible type of
198 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000199 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000200enum pp_token_type {
201 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
202 TOK_PREPROC_ID, TOK_STRING,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700203 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
204 TOK_INTERNAL_STRING,
205 TOK_PREPROC_Q, TOK_PREPROC_QQ,
H. Peter Anvind784a082009-04-20 14:01:18 -0700206 TOK_PASTE, /* %+ */
H. Peter Anvin9bb46df2009-04-07 21:59:24 -0700207 TOK_INDIRECT, /* %[...] */
H. Peter Anvin5b76fa22008-05-26 11:14:38 -0700208 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
209 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000210};
211
H. Peter Anvine2c80182005-01-15 22:15:51 +0000212struct Token {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000213 Token *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000214 char *text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -0700215 union {
216 SMacro *mac; /* associated macro for TOK_SMAC_END */
217 size_t len; /* scratch length field */
218 } a; /* Auxiliary data */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000219 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000220};
221
222/*
223 * Multi-line macro definitions are stored as a linked list of
224 * these, which is essentially a container to allow several linked
225 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700226 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000227 * Note that in this module, linked lists are treated as stacks
228 * wherever possible. For this reason, Lines are _pushed_ on to the
229 * `expansion' field in MMacro structures, so that the linked list,
230 * if walked, would give the macro lines in reverse order; this
231 * means that we can walk the list when expanding a macro, and thus
232 * push the lines on to the `expansion' field in _istk_ in reverse
233 * order (so that when popped back off they are in the right
234 * order). It may seem cockeyed, and it relies on my design having
235 * an even number of steps in, but it works...
236 *
237 * Some of these structures, rather than being actual lines, are
238 * markers delimiting the end of the expansion of a given macro.
H. Peter Anvin76690a12002-04-30 20:52:49 +0000239 * This is for use in the cycle-tracking and %rep-handling code.
240 * Such structures have `finishes' non-NULL, and `first' NULL. All
241 * others have `finishes' NULL, but `first' may still be NULL if
242 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000243 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000244struct Line {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000245 Line *next;
246 MMacro *finishes;
247 Token *first;
248};
249
250/*
251 * To handle an arbitrary level of file inclusion, we maintain a
252 * stack (ie linked list) of these things.
253 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000254struct Include {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000255 Include *next;
256 FILE *fp;
257 Cond *conds;
258 Line *expansion;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000259 char *fname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000260 int lineno, lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000261 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000262};
263
264/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000265 * Include search path. This is simply a list of strings which get
266 * prepended, in turn, to the name of an include file, in an
267 * attempt to find the file if it's not in the current directory.
268 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000269struct IncPath {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000270 IncPath *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000271 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000272};
273
274/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000275 * Conditional assembly: we maintain a separate stack of these for
276 * each level of file inclusion. (The only reason we keep the
277 * stacks separate is to ensure that a stray `%endif' in a file
278 * included from within the true branch of a `%if' won't terminate
279 * it and cause confusion: instead, rightly, it'll cause an error.)
280 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000281struct Cond {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000282 Cond *next;
283 int state;
284};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000285enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000286 /*
287 * These states are for use just after %if or %elif: IF_TRUE
288 * means the condition has evaluated to truth so we are
289 * currently emitting, whereas IF_FALSE means we are not
290 * currently emitting but will start doing so if a %else comes
291 * up. In these states, all directives are admissible: %elif,
292 * %else and %endif. (And of course %if.)
293 */
294 COND_IF_TRUE, COND_IF_FALSE,
295 /*
296 * These states come up after a %else: ELSE_TRUE means we're
297 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
298 * any %elif or %else will cause an error.
299 */
300 COND_ELSE_TRUE, COND_ELSE_FALSE,
301 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200302 * These states mean that we're not emitting now, and also that
303 * nothing until %endif will be emitted at all. COND_DONE is
304 * used when we've had our moment of emission
305 * and have now started seeing %elifs. COND_NEVER is used when
306 * the condition construct in question is contained within a
307 * non-emitting branch of a larger condition construct,
308 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000309 */
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200310 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000311};
312#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
313
H. Peter Anvin70653092007-10-19 14:42:29 -0700314/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000315 * These defines are used as the possible return values for do_directive
316 */
317#define NO_DIRECTIVE_FOUND 0
318#define DIRECTIVE_FOUND 1
319
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000320/*
321 * Condition codes. Note that we use c_ prefix not C_ because C_ is
322 * used in nasm.h for the "real" condition codes. At _this_ level,
323 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
324 * ones, so we need a different enum...
325 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700326static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000327 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
328 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000329 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000330};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700331enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000332 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
333 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
H. Peter Anvin476d2862007-10-02 22:04:15 -0700334 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
335 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000336};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700337static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000338 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
339 c_A, c_AE, c_B, c_BE, c_C, c_E, c_G, c_GE, c_L, c_LE, c_O, c_P, c_S,
H. Peter Anvince9be342007-09-12 00:22:29 +0000340 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000341};
342
H. Peter Anvin76690a12002-04-30 20:52:49 +0000343/*
344 * Directive names.
345 */
H. Peter Anvin65747262002-05-07 00:10:05 +0000346/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
H. Peter Anvin9bf0aa72007-09-12 02:12:07 +0000347static int is_condition(enum preproc_token arg)
H. Peter Anvin65747262002-05-07 00:10:05 +0000348{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000349 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
H. Peter Anvin65747262002-05-07 00:10:05 +0000350}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000351
352/* For TASM compatibility we need to be able to recognise TASM compatible
353 * conditional compilation directives. Using the NASM pre-processor does
354 * not work, so we look for them specifically from the following list and
355 * then jam in the equivalent NASM directive into the input stream.
356 */
357
H. Peter Anvine2c80182005-01-15 22:15:51 +0000358enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000359 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
360 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
361};
362
H. Peter Anvin476d2862007-10-02 22:04:15 -0700363static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000364 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
365 "ifndef", "include", "local"
366};
367
368static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000369static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000370static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800371static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000372
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000373static Context *cstk;
374static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000375static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000376
H. Peter Anvine2c80182005-01-15 22:15:51 +0000377static efunc _error; /* Pointer to client-provided error reporting function */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000378static evalfunc evaluate;
379
H. Peter Anvine2c80182005-01-15 22:15:51 +0000380static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700381static StrList **dephead, **deptail; /* Dependency list */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000382
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -0700383static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000384
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000385static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700386static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000387
388static ListGen *list;
389
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000390/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000391 * The current set of multi-line macros we have defined.
392 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700393static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000394
395/*
396 * The current set of single-line macros we have defined.
397 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700398static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000399
400/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000401 * The multi-line macro we are currently defining, or the %rep
402 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000403 */
404static MMacro *defining;
405
Charles Crayned4200be2008-07-12 16:42:33 -0700406static uint64_t nested_mac_count;
407static uint64_t nested_rep_count;
408
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000409/*
410 * The number of macro parameters to allocate space for at a time.
411 */
412#define PARAM_DELTA 16
413
414/*
H. Peter Anvina4835d42008-05-20 14:21:29 -0700415 * The standard macro set: defined in macros.c in the array nasm_stdmac.
416 * This gives our position in the macro set, when we're processing it.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000417 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700418static macros_t *stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000419
420/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000421 * The extra standard macros that come from the object format, if
422 * any.
423 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700424static macros_t *extrastdmac = NULL;
H. Peter Anvincfb71762008-06-20 15:20:16 -0700425static bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000426
427/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000428 * Tokens are allocated in blocks to improve speed
429 */
430#define TOKEN_BLOCKSIZE 4096
431static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000432struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000433 Blocks *next;
434 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000435};
436
437static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000438
439/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000440 * Forward declarations.
441 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000442static Token *expand_mmac_params(Token * tline);
443static Token *expand_smacro(Token * tline);
444static Token *expand_id(Token * tline);
H. Peter Anvinf8ad5322009-02-21 17:55:08 -0800445static Context *get_ctx(const char *name, const char **namep,
446 bool all_contexts);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700447static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000448static void error(int severity, const char *fmt, ...);
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200449static void error_precond(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000450static void *new_Block(size_t size);
451static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700452static Token *new_Token(Token * next, enum pp_token_type type,
453 const char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000454static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000455
456/*
457 * Macros for safe checking of token pointers, avoid *(NULL)
458 */
459#define tok_type_(x,t) ((x) && (x)->type == (t))
460#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
461#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
462#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000463
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000464/* Handle TASM specific directives, which do not contain a % in
465 * front of them. We do it here because I could not find any other
466 * place to do it for the moment, and it is a hack (ideally it would
467 * be nice to be able to use the NASM pre-processor to do it).
468 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000469static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000470{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000471 int32_t i, j, k, m, len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000472 char *p = line, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000473
474 /* Skip whitespace */
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700475 while (nasm_isspace(*p) && *p != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000476 p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000477
478 /* Binary search for the directive name */
479 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +0000480 j = elements(tasm_directives);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000481 len = 0;
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700482 while (!nasm_isspace(p[len]) && p[len] != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000483 len++;
484 if (len) {
485 oldchar = p[len];
486 p[len] = 0;
487 while (j - i > 1) {
488 k = (j + i) / 2;
489 m = nasm_stricmp(p, tasm_directives[k]);
490 if (m == 0) {
491 /* We have found a directive, so jam a % in front of it
492 * so that NASM will then recognise it as one if it's own.
493 */
494 p[len] = oldchar;
495 len = strlen(p);
496 oldline = line;
497 line = nasm_malloc(len + 2);
498 line[0] = '%';
499 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700500 /*
501 * NASM does not recognise IFDIFI, so we convert
502 * it to %if 0. This is not used in NASM
503 * compatible code, but does need to parse for the
504 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000505 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700506 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000507 } else {
508 memcpy(line + 1, p, len + 1);
509 }
510 nasm_free(oldline);
511 return line;
512 } else if (m < 0) {
513 j = k;
514 } else
515 i = k;
516 }
517 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000518 }
519 return line;
520}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000521
H. Peter Anvin76690a12002-04-30 20:52:49 +0000522/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000523 * The pre-preprocessing stage... This function translates line
524 * number indications as they emerge from GNU cpp (`# lineno "file"
525 * flags') into NASM preprocessor line number indications (`%line
526 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000527 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000528static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000529{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000530 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000531 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000532
H. Peter Anvine2c80182005-01-15 22:15:51 +0000533 if (line[0] == '#' && line[1] == ' ') {
534 oldline = line;
535 fname = oldline + 2;
536 lineno = atoi(fname);
537 fname += strspn(fname, "0123456789 ");
538 if (*fname == '"')
539 fname++;
540 fnlen = strcspn(fname, "\"");
541 line = nasm_malloc(20 + fnlen);
542 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
543 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000544 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000545 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000546 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000547 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000548}
549
550/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000551 * Free a linked list of tokens.
552 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000553static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000554{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000555 while (list) {
556 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000557 }
558}
559
560/*
561 * Free a linked list of lines.
562 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000563static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000564{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000565 Line *l;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000566 while (list) {
567 l = list;
568 list = list->next;
569 free_tlist(l->first);
570 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000571 }
572}
573
574/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000575 * Free an MMacro
576 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000577static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000578{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000579 nasm_free(m->name);
580 free_tlist(m->dlist);
581 nasm_free(m->defaults);
582 free_llist(m->expansion);
583 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000584}
585
586/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700587 * Free all currently defined macros, and free the hash tables
588 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700589static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700590{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700591 SMacro *s;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700592 const char *key;
593 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700594
H. Peter Anvin072771e2008-05-22 13:17:51 -0700595 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
H. Peter Anvin97a23472007-09-16 17:57:25 -0700596 nasm_free((void *)key);
597 while (s) {
598 SMacro *ns = s->next;
599 nasm_free(s->name);
600 free_tlist(s->expansion);
601 nasm_free(s);
602 s = ns;
603 }
604 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700605 hash_free(smt);
606}
607
608static void free_mmacro_table(struct hash_table *mmt)
609{
610 MMacro *m;
611 const char *key;
612 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700613
614 it = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700615 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
H. Peter Anvin97a23472007-09-16 17:57:25 -0700616 nasm_free((void *)key);
617 while (m) {
618 MMacro *nm = m->next;
619 free_mmacro(m);
620 m = nm;
621 }
622 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700623 hash_free(mmt);
624}
625
626static void free_macros(void)
627{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700628 free_smacro_table(&smacros);
629 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700630}
631
632/*
633 * Initialize the hash tables
634 */
635static void init_macros(void)
636{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700637 hash_init(&smacros, HASH_LARGE);
638 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700639}
640
641/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000642 * Pop the context stack.
643 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000644static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000645{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000646 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000647
648 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700649 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000650 nasm_free(c->name);
651 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000652}
653
H. Peter Anvin072771e2008-05-22 13:17:51 -0700654/*
655 * Search for a key in the hash index; adding it if necessary
656 * (in which case we initialize the data pointer to NULL.)
657 */
658static void **
659hash_findi_add(struct hash_table *hash, const char *str)
660{
661 struct hash_insert hi;
662 void **r;
663 char *strx;
664
665 r = hash_findi(hash, str, &hi);
666 if (r)
667 return r;
668
669 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
670 return hash_add(&hi, strx, NULL);
671}
672
673/*
674 * Like hash_findi, but returns the data element rather than a pointer
675 * to it. Used only when not adding a new element, hence no third
676 * argument.
677 */
678static void *
679hash_findix(struct hash_table *hash, const char *str)
680{
681 void **p;
682
683 p = hash_findi(hash, str, NULL);
684 return p ? *p : NULL;
685}
686
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000687#define BUF_DELTA 512
688/*
689 * Read a line from the top file in istk, handling multiple CR/LFs
690 * at the end of the line read, and handling spurious ^Zs. Will
691 * return lines from the standard macro set if this has not already
692 * been done.
693 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000694static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000695{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000696 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000697 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000698
H. Peter Anvine2c80182005-01-15 22:15:51 +0000699 if (stdmacpos) {
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700700 unsigned char c;
H. Peter Anvin7e50d232008-06-25 14:54:14 -0700701 const unsigned char *p = stdmacpos;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700702 char *ret, *q;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700703 size_t len = 0;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700704 while ((c = *p++)) {
705 if (c >= 0x80)
706 len += pp_directives_len[c-0x80]+1;
707 else
708 len++;
709 }
710 ret = nasm_malloc(len+1);
H. Peter Anvincda81632008-06-21 15:15:40 -0700711 q = ret;
712 while ((c = *stdmacpos++)) {
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700713 if (c >= 0x80) {
714 memcpy(q, pp_directives[c-0x80], pp_directives_len[c-0x80]);
715 q += pp_directives_len[c-0x80];
716 *q++ = ' ';
717 } else {
718 *q++ = c;
719 }
720 }
H. Peter Anvincda81632008-06-21 15:15:40 -0700721 stdmacpos = p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700722 *q = '\0';
723
H. Peter Anvind2456592008-06-19 15:04:18 -0700724 if (!*stdmacpos) {
725 /* This was the last of the standard macro chain... */
726 stdmacpos = NULL;
727 if (any_extrastdmac) {
728 stdmacpos = extrastdmac;
729 any_extrastdmac = false;
730 } else if (do_predef) {
731 Line *pd, *l;
732 Token *head, **tail, *t;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000733
H. Peter Anvind2456592008-06-19 15:04:18 -0700734 /*
735 * Nasty hack: here we push the contents of
736 * `predef' on to the top-level expansion stack,
737 * since this is the most convenient way to
738 * implement the pre-include and pre-define
739 * features.
740 */
741 for (pd = predef; pd; pd = pd->next) {
742 head = NULL;
743 tail = &head;
744 for (t = pd->first; t; t = t->next) {
745 *tail = new_Token(NULL, t->type, t->text, 0);
746 tail = &(*tail)->next;
747 }
748 l = nasm_malloc(sizeof(Line));
749 l->next = istk->expansion;
750 l->first = head;
H. Peter Anvin538002d2008-06-28 18:30:27 -0700751 l->finishes = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700752 istk->expansion = l;
753 }
754 do_predef = false;
755 }
756 }
757 return ret;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000758 }
759
760 bufsize = BUF_DELTA;
761 buffer = nasm_malloc(BUF_DELTA);
762 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000763 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000764 while (1) {
765 q = fgets(p, bufsize - (p - buffer), istk->fp);
766 if (!q)
767 break;
768 p += strlen(p);
769 if (p > buffer && p[-1] == '\n') {
770 /* Convert backslash-CRLF line continuation sequences into
771 nothing at all (for DOS and Windows) */
772 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
773 p -= 3;
774 *p = 0;
775 continued_count++;
776 }
777 /* Also convert backslash-LF line continuation sequences into
778 nothing at all (for Unix) */
779 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
780 p -= 2;
781 *p = 0;
782 continued_count++;
783 } else {
784 break;
785 }
786 }
787 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000788 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000789 bufsize += BUF_DELTA;
790 buffer = nasm_realloc(buffer, bufsize);
791 p = buffer + offset; /* prevent stale-pointer problems */
792 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000793 }
794
H. Peter Anvine2c80182005-01-15 22:15:51 +0000795 if (!q && p == buffer) {
796 nasm_free(buffer);
797 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000798 }
799
H. Peter Anvine2c80182005-01-15 22:15:51 +0000800 src_set_linnum(src_get_linnum() + istk->lineinc +
801 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000802
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000803 /*
804 * Play safe: remove CRs as well as LFs, if any of either are
805 * present at the end of the line.
806 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000807 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000808 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000809
810 /*
811 * Handle spurious ^Z, which may be inserted into source files
812 * by some file transfer utilities.
813 */
814 buffer[strcspn(buffer, "\032")] = '\0';
815
H. Peter Anvin734b1882002-04-30 21:01:08 +0000816 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000817
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000818 return buffer;
819}
820
821/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000822 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000823 * don't need to parse the value out of e.g. numeric tokens: we
824 * simply split one string into many.
825 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000826static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000827{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700828 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000829 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000830 Token *list = NULL;
831 Token *t, **tail = &list;
832
H. Peter Anvine2c80182005-01-15 22:15:51 +0000833 while (*line) {
834 p = line;
835 if (*p == '%') {
836 p++;
H. Peter Anvind784a082009-04-20 14:01:18 -0700837 if (*p == '+' && !nasm_isdigit(p[1])) {
838 p++;
839 type = TOK_PASTE;
840 } else if (nasm_isdigit(*p) ||
841 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000842 do {
843 p++;
844 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700845 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000846 type = TOK_PREPROC_ID;
847 } else if (*p == '{') {
848 p++;
849 while (*p && *p != '}') {
850 p[-1] = *p;
851 p++;
852 }
853 p[-1] = '\0';
854 if (*p)
855 p++;
856 type = TOK_PREPROC_ID;
H. Peter Anvin992fe752008-10-19 15:45:05 -0700857 } else if (*p == '[') {
858 int lvl = 1;
859 line += 2; /* Skip the leading %[ */
860 p++;
H. Peter Anvinec033012008-10-19 22:12:06 -0700861 while (lvl && (c = *p++)) {
H. Peter Anvinca544db2008-10-19 19:30:11 -0700862 switch (c) {
863 case ']':
864 lvl--;
865 break;
866 case '%':
H. Peter Anvinca544db2008-10-19 19:30:11 -0700867 if (*p == '[')
868 lvl++;
869 break;
870 case '\'':
871 case '\"':
872 case '`':
H. Peter Anvinec033012008-10-19 22:12:06 -0700873 p = nasm_skip_string(p)+1;
H. Peter Anvinca544db2008-10-19 19:30:11 -0700874 break;
875 default:
876 break;
H. Peter Anvin992fe752008-10-19 15:45:05 -0700877 }
H. Peter Anvin992fe752008-10-19 15:45:05 -0700878 }
H. Peter Anvinec033012008-10-19 22:12:06 -0700879 p--;
H. Peter Anvin992fe752008-10-19 15:45:05 -0700880 if (*p)
881 *p++ = '\0';
H. Peter Anvine1265812008-10-19 22:14:30 -0700882 if (lvl)
883 error(ERR_NONFATAL, "unterminated %[ construct");
H. Peter Anvin992fe752008-10-19 15:45:05 -0700884 type = TOK_INDIRECT;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700885 } else if (*p == '?') {
886 type = TOK_PREPROC_Q; /* %? */
887 p++;
888 if (*p == '?') {
889 type = TOK_PREPROC_QQ; /* %?? */
890 p++;
891 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000892 } else if (isidchar(*p) ||
893 ((*p == '!' || *p == '%' || *p == '$') &&
894 isidchar(p[1]))) {
895 do {
896 p++;
897 }
898 while (isidchar(*p));
899 type = TOK_PREPROC_ID;
900 } else {
901 type = TOK_OTHER;
902 if (*p == '%')
903 p++;
904 }
905 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
906 type = TOK_ID;
907 p++;
908 while (*p && isidchar(*p))
909 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700910 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000911 /*
912 * A string token.
913 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000914 type = TOK_STRING;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700915 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000916
H. Peter Anvine2c80182005-01-15 22:15:51 +0000917 if (*p) {
918 p++;
919 } else {
H. Peter Anvin917a3492008-09-24 09:14:49 -0700920 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000921 /* Handling unterminated strings by UNV */
922 /* type = -1; */
923 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +0200924 } else if (p[0] == '$' && p[1] == '$') {
H. Peter Anvin8e1f8112009-04-17 14:20:44 -0700925 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +0200926 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000927 } else if (isnumstart(*p)) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700928 bool is_hex = false;
929 bool is_float = false;
930 bool has_e = false;
931 char c, *r;
932
H. Peter Anvine2c80182005-01-15 22:15:51 +0000933 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700934 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000935 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700936
937 if (*p == '$') {
938 p++;
939 is_hex = true;
940 }
941
942 for (;;) {
943 c = *p++;
944
945 if (!is_hex && (c == 'e' || c == 'E')) {
946 has_e = true;
947 if (*p == '+' || *p == '-') {
948 /* e can only be followed by +/- if it is either a
949 prefixed hex number or a floating-point number */
950 p++;
951 is_float = true;
952 }
953 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
954 is_hex = true;
955 } else if (c == 'P' || c == 'p') {
956 is_float = true;
957 if (*p == '+' || *p == '-')
958 p++;
959 } else if (isnumchar(c) || c == '_')
960 ; /* just advance */
961 else if (c == '.') {
962 /* we need to deal with consequences of the legacy
963 parser, like "1.nolist" being two tokens
964 (TOK_NUMBER, TOK_ID) here; at least give it
965 a shot for now. In the future, we probably need
966 a flex-based scanner with proper pattern matching
967 to do it as well as it can be done. Nothing in
968 the world is going to help the person who wants
969 0x123.p16 interpreted as two tokens, though. */
970 r = p;
971 while (*r == '_')
972 r++;
973
H. Peter Anvinf221b9e2008-06-21 11:03:51 -0700974 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700975 (!is_hex && (*r == 'e' || *r == 'E')) ||
976 (*r == 'p' || *r == 'P')) {
977 p = r;
978 is_float = true;
979 } else
980 break; /* Terminate the token */
981 } else
982 break;
983 }
984 p--; /* Point to first character beyond number */
985
H. Peter Anvin8e1f8112009-04-17 14:20:44 -0700986 if (p == line+1 && *line == '$') {
987 type = TOK_OTHER; /* TOKEN_HERE */
988 } else {
989 if (has_e && !is_hex) {
990 /* 1e13 is floating-point, but 1e13h is not */
991 is_float = true;
992 }
H. Peter Anvind784a082009-04-20 14:01:18 -0700993
H. Peter Anvin8e1f8112009-04-17 14:20:44 -0700994 type = is_float ? TOK_FLOAT : TOK_NUMBER;
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700995 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700996 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000997 type = TOK_WHITESPACE;
998 p++;
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700999 while (*p && nasm_isspace(*p))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001000 p++;
1001 /*
1002 * Whitespace just before end-of-line is discarded by
1003 * pretending it's a comment; whitespace just before a
1004 * comment gets lumped into the comment.
1005 */
1006 if (!*p || *p == ';') {
1007 type = TOK_COMMENT;
1008 while (*p)
1009 p++;
1010 }
1011 } else if (*p == ';') {
1012 type = TOK_COMMENT;
1013 while (*p)
1014 p++;
1015 } else {
1016 /*
1017 * Anything else is an operator of some kind. We check
1018 * for all the double-character operators (>>, <<, //,
1019 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001020 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001021 */
1022 type = TOK_OTHER;
1023 if ((p[0] == '>' && p[1] == '>') ||
1024 (p[0] == '<' && p[1] == '<') ||
1025 (p[0] == '/' && p[1] == '/') ||
1026 (p[0] == '<' && p[1] == '=') ||
1027 (p[0] == '>' && p[1] == '=') ||
1028 (p[0] == '=' && p[1] == '=') ||
1029 (p[0] == '!' && p[1] == '=') ||
1030 (p[0] == '<' && p[1] == '>') ||
1031 (p[0] == '&' && p[1] == '&') ||
1032 (p[0] == '|' && p[1] == '|') ||
1033 (p[0] == '^' && p[1] == '^')) {
1034 p++;
1035 }
1036 p++;
1037 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001038
H. Peter Anvine2c80182005-01-15 22:15:51 +00001039 /* Handling unterminated string by UNV */
1040 /*if (type == -1)
1041 {
1042 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1043 t->text[p-line] = *line;
1044 tail = &t->next;
1045 }
1046 else */
1047 if (type != TOK_COMMENT) {
1048 *tail = t = new_Token(NULL, type, line, p - line);
1049 tail = &t->next;
1050 }
1051 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001052 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001053 return list;
1054}
1055
H. Peter Anvince616072002-04-30 21:02:23 +00001056/*
1057 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001058 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001059 * deleted only all at once by the delete_Blocks function.
1060 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001061static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001062{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001063 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001064
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001065 /* first, get to the end of the linked list */
1066 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001067 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001068 /* now allocate the requested chunk */
1069 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001070
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001071 /* now allocate a new block for the next request */
1072 b->next = nasm_malloc(sizeof(Blocks));
1073 /* and initialize the contents of the new block */
1074 b->next->next = NULL;
1075 b->next->chunk = NULL;
1076 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001077}
1078
1079/*
1080 * this function deletes all managed blocks of memory
1081 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001082static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001083{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001084 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001085
H. Peter Anvin70653092007-10-19 14:42:29 -07001086 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001087 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001088 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001089 * free it.
1090 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001091 while (b) {
1092 if (b->chunk)
1093 nasm_free(b->chunk);
1094 a = b;
1095 b = b->next;
1096 if (a != &blocks)
1097 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001098 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001099}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001100
1101/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001102 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001103 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001104 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001105 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001106static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvinc751e862008-06-09 10:18:45 -07001107 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001108{
1109 Token *t;
1110 int i;
1111
H. Peter Anvine2c80182005-01-15 22:15:51 +00001112 if (freeTokens == NULL) {
1113 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1114 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1115 freeTokens[i].next = &freeTokens[i + 1];
1116 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001117 }
1118 t = freeTokens;
1119 freeTokens = t->next;
1120 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001121 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001122 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001123 if (type == TOK_WHITESPACE || text == NULL) {
1124 t->text = NULL;
1125 } else {
1126 if (txtlen == 0)
1127 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001128 t->text = nasm_malloc(txtlen+1);
1129 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001130 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001131 }
1132 return t;
1133}
1134
H. Peter Anvine2c80182005-01-15 22:15:51 +00001135static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001136{
1137 Token *next = t->next;
1138 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001139 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001140 freeTokens = t;
1141 return next;
1142}
1143
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001144/*
1145 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001146 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1147 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001148 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001149static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001150{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001151 Token *t;
1152 int len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001153 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001154 const char *q;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001155
1156 len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001157 for (t = tlist; t; t = t->next) {
1158 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001159 char *p = getenv(t->text + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001160 nasm_free(t->text);
1161 if (p)
1162 t->text = nasm_strdup(p);
1163 else
1164 t->text = NULL;
1165 }
1166 /* Expand local macros here and not during preprocessing */
1167 if (expand_locals &&
1168 t->type == TOK_PREPROC_ID && t->text &&
1169 t->text[0] == '%' && t->text[1] == '$') {
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001170 const char *q;
1171 char *p;
1172 Context *ctx = get_ctx(t->text, &q, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001173 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001174 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001175 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001176 p = nasm_strcat(buffer, q);
1177 nasm_free(t->text);
1178 t->text = p;
1179 }
1180 }
1181 if (t->type == TOK_WHITESPACE) {
1182 len++;
1183 } else if (t->text) {
1184 len += strlen(t->text);
1185 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001186 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001187 p = line = nasm_malloc(len + 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001188 for (t = tlist; t; t = t->next) {
1189 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001190 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001191 } else if (t->text) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001192 q = t->text;
1193 while (*q)
1194 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001195 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001196 }
1197 *p = '\0';
1198 return line;
1199}
1200
1201/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001202 * A scanner, suitable for use by the expression evaluator, which
1203 * operates on a line of Tokens. Expects a pointer to a pointer to
1204 * the first token in the line to be passed in as its private_data
1205 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001206 *
1207 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001208 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001209static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001210{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001211 Token **tlineptr = private_data;
1212 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001213 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001214
H. Peter Anvine2c80182005-01-15 22:15:51 +00001215 do {
1216 tline = *tlineptr;
1217 *tlineptr = tline ? tline->next : NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001218 }
1219 while (tline && (tline->type == TOK_WHITESPACE ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001220 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001221
1222 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001223 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001224
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001225 tokval->t_charptr = tline->text;
1226
H. Peter Anvin76690a12002-04-30 20:52:49 +00001227 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001228 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001229 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001230 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001231
H. Peter Anvine2c80182005-01-15 22:15:51 +00001232 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001233 p = tokval->t_charptr = tline->text;
1234 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001235 tokval->t_charptr++;
1236 return tokval->t_type = TOKEN_ID;
1237 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001238
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001239 for (r = p, s = ourcopy; *r; r++) {
Philipp Thomas76ec8e72008-05-21 08:53:21 -07001240 if (r >= p+MAX_KEYWORD)
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001241 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001242 *s++ = nasm_tolower(*r);
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001243 }
1244 *s = '\0';
1245 /* right, so we have an identifier sitting in temp storage. now,
1246 * is it actually a register or instruction name, or what? */
1247 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001248 }
1249
H. Peter Anvine2c80182005-01-15 22:15:51 +00001250 if (tline->type == TOK_NUMBER) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001251 bool rn_error;
1252 tokval->t_integer = readnum(tline->text, &rn_error);
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001253 tokval->t_charptr = tline->text;
H. Peter Anvin11627042008-06-09 20:45:19 -07001254 if (rn_error)
1255 return tokval->t_type = TOKEN_ERRNUM;
1256 else
1257 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001258 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001259
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001260 if (tline->type == TOK_FLOAT) {
1261 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001262 }
1263
H. Peter Anvine2c80182005-01-15 22:15:51 +00001264 if (tline->type == TOK_STRING) {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001265 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001266
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001267 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001268 tokval->t_charptr = tline->text;
1269 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001270
H. Peter Anvin11627042008-06-09 20:45:19 -07001271 if (ep[0] != bq || ep[1] != '\0')
1272 return tokval->t_type = TOKEN_ERRSTR;
1273 else
1274 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001275 }
1276
H. Peter Anvine2c80182005-01-15 22:15:51 +00001277 if (tline->type == TOK_OTHER) {
1278 if (!strcmp(tline->text, "<<"))
1279 return tokval->t_type = TOKEN_SHL;
1280 if (!strcmp(tline->text, ">>"))
1281 return tokval->t_type = TOKEN_SHR;
1282 if (!strcmp(tline->text, "//"))
1283 return tokval->t_type = TOKEN_SDIV;
1284 if (!strcmp(tline->text, "%%"))
1285 return tokval->t_type = TOKEN_SMOD;
1286 if (!strcmp(tline->text, "=="))
1287 return tokval->t_type = TOKEN_EQ;
1288 if (!strcmp(tline->text, "<>"))
1289 return tokval->t_type = TOKEN_NE;
1290 if (!strcmp(tline->text, "!="))
1291 return tokval->t_type = TOKEN_NE;
1292 if (!strcmp(tline->text, "<="))
1293 return tokval->t_type = TOKEN_LE;
1294 if (!strcmp(tline->text, ">="))
1295 return tokval->t_type = TOKEN_GE;
1296 if (!strcmp(tline->text, "&&"))
1297 return tokval->t_type = TOKEN_DBL_AND;
1298 if (!strcmp(tline->text, "^^"))
1299 return tokval->t_type = TOKEN_DBL_XOR;
1300 if (!strcmp(tline->text, "||"))
1301 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001302 }
1303
1304 /*
1305 * We have no other options: just return the first character of
1306 * the token text.
1307 */
1308 return tokval->t_type = tline->text[0];
1309}
1310
1311/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001312 * Compare a string to the name of an existing macro; this is a
1313 * simple wrapper which calls either strcmp or nasm_stricmp
1314 * depending on the value of the `casesense' parameter.
1315 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001316static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001317{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001318 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001319}
1320
1321/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001322 * Compare a string to the name of an existing macro; this is a
1323 * simple wrapper which calls either strcmp or nasm_stricmp
1324 * depending on the value of the `casesense' parameter.
1325 */
1326static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1327{
1328 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1329}
1330
1331/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001332 * Return the Context structure associated with a %$ token. Return
1333 * NULL, having _already_ reported an error condition, if the
1334 * context stack isn't deep enough for the supplied number of $
1335 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001336 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001337 * also scanned for such smacro, until it is found; if not -
1338 * only the context that directly results from the number of $'s
1339 * in variable's name.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001340 *
1341 * If "namep" is non-NULL, set it to the pointer to the macro name
1342 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001343 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001344static Context *get_ctx(const char *name, const char **namep,
1345 bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001346{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001347 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001348 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001349 int i;
1350
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001351 if (namep)
1352 *namep = name;
1353
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001354 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001355 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001356
H. Peter Anvine2c80182005-01-15 22:15:51 +00001357 if (!cstk) {
1358 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1359 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001360 }
1361
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001362 name += 2;
1363 ctx = cstk;
1364 i = 0;
1365 while (ctx && *name == '$') {
1366 name++;
1367 i++;
1368 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001369 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001370 if (!ctx) {
1371 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001372 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001373 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001374 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001375
1376 if (namep)
1377 *namep = name;
1378
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001379 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001380 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001381
H. Peter Anvine2c80182005-01-15 22:15:51 +00001382 do {
1383 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001384 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001385 while (m) {
1386 if (!mstrcmp(m->name, name, m->casesense))
1387 return ctx;
1388 m = m->next;
1389 }
1390 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001391 }
1392 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001393 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001394}
1395
1396/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001397 * Check to see if a file is already in a string list
1398 */
1399static bool in_list(const StrList *list, const char *str)
1400{
1401 while (list) {
1402 if (!strcmp(list->str, str))
1403 return true;
1404 list = list->next;
1405 }
1406 return false;
1407}
1408
1409/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001410 * Open an include file. This routine must always return a valid
1411 * file pointer if it returns - it's responsible for throwing an
1412 * ERR_FATAL and bombing out completely if not. It should also try
1413 * the include path one by one until it finds the file or reaches
1414 * the end of the path.
1415 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001416static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
H. Peter Anvin418ca702008-05-30 10:42:30 -07001417 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001418{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001419 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001420 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001421 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001422 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001423 size_t prefix_len = 0;
1424 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001425
H. Peter Anvine2c80182005-01-15 22:15:51 +00001426 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001427 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
1428 memcpy(sl->str, prefix, prefix_len);
1429 memcpy(sl->str+prefix_len, file, len+1);
1430 fp = fopen(sl->str, "r");
H. Peter Anvin418ca702008-05-30 10:42:30 -07001431 if (fp && dhead && !in_list(*dhead, sl->str)) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001432 sl->next = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001433 **dtail = sl;
1434 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001435 } else {
1436 nasm_free(sl);
1437 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001438 if (fp)
1439 return fp;
H. Peter Anvin418ca702008-05-30 10:42:30 -07001440 if (!ip) {
1441 if (!missing_ok)
1442 break;
1443 prefix = NULL;
1444 } else {
1445 prefix = ip->path;
1446 ip = ip->next;
1447 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001448 if (prefix) {
1449 prefix_len = strlen(prefix);
1450 } else {
1451 /* -MG given and file not found */
H. Peter Anvin418ca702008-05-30 10:42:30 -07001452 if (dhead && !in_list(*dhead, file)) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001453 sl = nasm_malloc(len+1+sizeof sl->next);
1454 sl->next = NULL;
1455 strcpy(sl->str, file);
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001456 **dtail = sl;
1457 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001458 }
H. Peter Anvin37a321f2007-09-24 13:41:58 -07001459 return NULL;
1460 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001461 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001462
H. Peter Anvin734b1882002-04-30 21:01:08 +00001463 error(ERR_FATAL, "unable to open include file `%s'", file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001464 return NULL; /* never reached - placate compilers */
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001465}
1466
1467/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001468 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001469 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001470 * return true if _any_ single-line macro of that name is defined.
1471 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001472 * `nparam' or no parameters is defined.
1473 *
1474 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001475 * defined, or nparam is -1, the address of the definition structure
1476 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001477 * is NULL, no action will be taken regarding its contents, and no
1478 * error will occur.
1479 *
1480 * Note that this is also called with nparam zero to resolve
1481 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001482 *
1483 * If you already know which context macro belongs to, you can pass
1484 * the context pointer as first parameter; if you won't but name begins
1485 * with %$ the context will be automatically computed. If all_contexts
1486 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001487 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001488static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001489smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001490 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001491{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001492 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001493 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001494
H. Peter Anvin97a23472007-09-16 17:57:25 -07001495 if (ctx) {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001496 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001497 } else if (name[0] == '%' && name[1] == '$') {
1498 if (cstk)
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001499 ctx = get_ctx(name, &name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001500 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001501 return false; /* got to return _something_ */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001502 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001503 } else {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001504 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001505 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001506 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001507
H. Peter Anvine2c80182005-01-15 22:15:51 +00001508 while (m) {
1509 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001510 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001511 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001512 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001513 *defn = m;
1514 else
1515 *defn = NULL;
1516 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001517 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001518 }
1519 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001520 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001521
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001522 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001523}
1524
1525/*
1526 * Count and mark off the parameters in a multi-line macro call.
1527 * This is called both from within the multi-line macro expansion
1528 * code, and also to mark off the default parameters when provided
1529 * in a %macro definition line.
1530 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001531static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001532{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001533 int paramsize, brace;
1534
1535 *nparam = paramsize = 0;
1536 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001537 while (t) {
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001538 /* +1: we need space for the final NULL */
1539 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001540 paramsize += PARAM_DELTA;
1541 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1542 }
1543 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001544 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001545 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001546 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001547 (*params)[(*nparam)++] = t;
1548 while (tok_isnt_(t, brace ? "}" : ","))
1549 t = t->next;
1550 if (t) { /* got a comma/brace */
1551 t = t->next;
1552 if (brace) {
1553 /*
1554 * Now we've found the closing brace, look further
1555 * for the comma.
1556 */
1557 skip_white_(t);
1558 if (tok_isnt_(t, ",")) {
1559 error(ERR_NONFATAL,
1560 "braces do not enclose all of macro parameter");
1561 while (tok_isnt_(t, ","))
1562 t = t->next;
1563 }
1564 if (t)
1565 t = t->next; /* eat the comma */
1566 }
1567 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001568 }
1569}
1570
1571/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001572 * Determine whether one of the various `if' conditions is true or
1573 * not.
1574 *
1575 * We must free the tline we get passed.
1576 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001577static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001578{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001579 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001580 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001581 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001582 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001583 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001584 enum pp_token_type needtype;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001585
1586 origline = tline;
1587
H. Peter Anvine2c80182005-01-15 22:15:51 +00001588 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001589 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001590 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001591 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001592 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001593 if (!tline)
1594 break;
1595 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001596 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001597 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001598 free_tlist(origline);
1599 return -1;
1600 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001601 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001602 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001603 tline = tline->next;
1604 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001605 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001606
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001607 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001608 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001609 while (tline) {
1610 skip_white_(tline);
1611 if (!tline || (tline->type != TOK_ID &&
1612 (tline->type != TOK_PREPROC_ID ||
1613 tline->text[1] != '$'))) {
1614 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001615 "`%s' expects macro identifiers", pp_directives[ct]);
1616 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001617 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001618 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001619 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001620 tline = tline->next;
1621 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001622 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001623
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001624 case PPC_IFIDN:
1625 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001626 tline = expand_smacro(tline);
1627 t = tt = tline;
1628 while (tok_isnt_(tt, ","))
1629 tt = tt->next;
1630 if (!tt) {
1631 error(ERR_NONFATAL,
1632 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001633 pp_directives[ct]);
1634 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001635 }
1636 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001637 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001638 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1639 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1640 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001641 pp_directives[ct]);
1642 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001643 }
1644 if (t->type == TOK_WHITESPACE) {
1645 t = t->next;
1646 continue;
1647 }
1648 if (tt->type == TOK_WHITESPACE) {
1649 tt = tt->next;
1650 continue;
1651 }
1652 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001653 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001654 break;
1655 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001656 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001657 if (t->type == TOK_STRING) {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001658 size_t l1 = nasm_unquote(t->text, NULL);
1659 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001660
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001661 if (l1 != l2) {
1662 j = false;
1663 break;
1664 }
1665 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1666 j = false;
1667 break;
1668 }
1669 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001670 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001671 break;
1672 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001673
H. Peter Anvine2c80182005-01-15 22:15:51 +00001674 t = t->next;
1675 tt = tt->next;
1676 }
1677 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001678 j = false; /* trailing gunk on one end or other */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001679 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001680
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001681 case PPC_IFMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001682 {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001683 bool found = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001684 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001685
H. Peter Anvine2c80182005-01-15 22:15:51 +00001686 skip_white_(tline);
1687 tline = expand_id(tline);
1688 if (!tok_type_(tline, TOK_ID)) {
1689 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001690 "`%s' expects a macro name", pp_directives[ct]);
1691 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001692 }
1693 searching.name = nasm_strdup(tline->text);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001694 searching.casesense = true;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001695 searching.plus = false;
1696 searching.nolist = false;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001697 searching.in_progress = 0;
Keith Kanios891775e2009-07-11 06:08:54 -05001698 searching.max_depth = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001699 searching.rep_nest = NULL;
1700 searching.nparam_min = 0;
1701 searching.nparam_max = INT_MAX;
1702 tline = expand_smacro(tline->next);
1703 skip_white_(tline);
1704 if (!tline) {
1705 } else if (!tok_type_(tline, TOK_NUMBER)) {
1706 error(ERR_NONFATAL,
1707 "`%s' expects a parameter count or nothing",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001708 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001709 } else {
1710 searching.nparam_min = searching.nparam_max =
1711 readnum(tline->text, &j);
1712 if (j)
1713 error(ERR_NONFATAL,
1714 "unable to parse parameter count `%s'",
1715 tline->text);
1716 }
1717 if (tline && tok_is_(tline->next, "-")) {
1718 tline = tline->next->next;
1719 if (tok_is_(tline, "*"))
1720 searching.nparam_max = INT_MAX;
1721 else if (!tok_type_(tline, TOK_NUMBER))
1722 error(ERR_NONFATAL,
1723 "`%s' expects a parameter count after `-'",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001724 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001725 else {
1726 searching.nparam_max = readnum(tline->text, &j);
1727 if (j)
1728 error(ERR_NONFATAL,
1729 "unable to parse parameter count `%s'",
1730 tline->text);
1731 if (searching.nparam_min > searching.nparam_max)
1732 error(ERR_NONFATAL,
1733 "minimum parameter count exceeds maximum");
1734 }
1735 }
1736 if (tline && tok_is_(tline->next, "+")) {
1737 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001738 searching.plus = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001739 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001740 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001741 while (mmac) {
1742 if (!strcmp(mmac->name, searching.name) &&
1743 (mmac->nparam_min <= searching.nparam_max
1744 || searching.plus)
1745 && (searching.nparam_min <= mmac->nparam_max
1746 || mmac->plus)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001747 found = true;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001748 break;
1749 }
1750 mmac = mmac->next;
1751 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001752 if(tline && tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001753 error(ERR_WARNING|ERR_PASS1,
1754 "trailing garbage after %%ifmacro ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001755 nasm_free(searching.name);
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001756 j = found;
1757 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001758 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001759
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001760 case PPC_IFID:
1761 needtype = TOK_ID;
1762 goto iftype;
1763 case PPC_IFNUM:
1764 needtype = TOK_NUMBER;
1765 goto iftype;
1766 case PPC_IFSTR:
1767 needtype = TOK_STRING;
1768 goto iftype;
1769
1770 iftype:
H. Peter Anvin134b9462008-02-16 17:01:40 -08001771 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001772
H. Peter Anvin927c92b2008-02-16 13:44:52 -08001773 while (tok_type_(t, TOK_WHITESPACE) ||
1774 (needtype == TOK_NUMBER &&
1775 tok_type_(t, TOK_OTHER) &&
1776 (t->text[0] == '-' || t->text[0] == '+') &&
1777 !t->text[1]))
1778 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001779
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001780 j = tok_type_(t, needtype);
1781 break;
1782
1783 case PPC_IFTOKEN:
1784 t = tline = expand_smacro(tline);
1785 while (tok_type_(t, TOK_WHITESPACE))
1786 t = t->next;
1787
1788 j = false;
1789 if (t) {
1790 t = t->next; /* Skip the actual token */
1791 while (tok_type_(t, TOK_WHITESPACE))
1792 t = t->next;
1793 j = !t; /* Should be nothing left */
1794 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001795 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001796
H. Peter Anvin134b9462008-02-16 17:01:40 -08001797 case PPC_IFEMPTY:
1798 t = tline = expand_smacro(tline);
1799 while (tok_type_(t, TOK_WHITESPACE))
1800 t = t->next;
1801
1802 j = !t; /* Should be empty */
1803 break;
1804
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001805 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001806 t = tline = expand_smacro(tline);
1807 tptr = &t;
1808 tokval.t_type = TOKEN_INVALID;
1809 evalresult = evaluate(ppscan, tptr, &tokval,
1810 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001811 if (!evalresult)
1812 return -1;
1813 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001814 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001815 "trailing garbage after expression ignored");
1816 if (!is_simple(evalresult)) {
1817 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001818 "non-constant value given to `%s'", pp_directives[ct]);
1819 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001820 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001821 j = reloc_value(evalresult) != 0;
H. Peter Anvin90e6e812008-07-16 14:38:24 -07001822 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001823
H. Peter Anvine2c80182005-01-15 22:15:51 +00001824 default:
1825 error(ERR_FATAL,
1826 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001827 pp_directives[ct]);
1828 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001829 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001830
1831 free_tlist(origline);
1832 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001833
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001834fail:
1835 free_tlist(origline);
1836 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001837}
1838
1839/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001840 * Common code for defining an smacro
1841 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001842static bool define_smacro(Context *ctx, const char *mname, bool casesense,
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001843 int nparam, Token *expansion)
1844{
1845 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001846 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07001847
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001848 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1849 if (!smac) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07001850 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001851 "single-line macro `%s' defined both with and"
1852 " without parameters", mname);
1853
1854 /* Some instances of the old code considered this a failure,
1855 some others didn't. What is the right thing to do here? */
1856 free_tlist(expansion);
1857 return false; /* Failure */
1858 } else {
1859 /*
1860 * We're redefining, so we have to take over an
1861 * existing SMacro structure. This means freeing
1862 * what was already in it.
1863 */
1864 nasm_free(smac->name);
1865 free_tlist(smac->expansion);
1866 }
1867 } else {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001868 smtbl = ctx ? &ctx->localmac : &smacros;
1869 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001870 smac = nasm_malloc(sizeof(SMacro));
1871 smac->next = *smhead;
1872 *smhead = smac;
1873 }
1874 smac->name = nasm_strdup(mname);
1875 smac->casesense = casesense;
1876 smac->nparam = nparam;
1877 smac->expansion = expansion;
1878 smac->in_progress = false;
1879 return true; /* Success */
1880}
1881
1882/*
1883 * Undefine an smacro
1884 */
1885static void undef_smacro(Context *ctx, const char *mname)
1886{
1887 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001888 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001889
H. Peter Anvin166c2472008-05-28 12:28:58 -07001890 smtbl = ctx ? &ctx->localmac : &smacros;
1891 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07001892
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001893 if (smhead) {
1894 /*
1895 * We now have a macro name... go hunt for it.
1896 */
1897 sp = smhead;
1898 while ((s = *sp) != NULL) {
1899 if (!mstrcmp(s->name, mname, s->casesense)) {
1900 *sp = s->next;
1901 nasm_free(s->name);
1902 free_tlist(s->expansion);
1903 nasm_free(s);
1904 } else {
1905 sp = &s->next;
1906 }
1907 }
1908 }
1909}
1910
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001911/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07001912 * Parse a mmacro specification.
1913 */
1914static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
1915{
1916 bool err;
1917
1918 tline = tline->next;
1919 skip_white_(tline);
1920 tline = expand_id(tline);
1921 if (!tok_type_(tline, TOK_ID)) {
1922 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
1923 return false;
1924 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001925
Keith Kanios891775e2009-07-11 06:08:54 -05001926 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07001927 def->name = nasm_strdup(tline->text);
1928 def->plus = false;
1929 def->nolist = false;
1930 def->in_progress = 0;
Keith Kanios891775e2009-07-11 06:08:54 -05001931 def->max_depth = 0;
H. Peter Anvina26433d2008-07-16 14:40:01 -07001932 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001933 def->nparam_min = 0;
1934 def->nparam_max = 0;
1935
H. Peter Anvina26433d2008-07-16 14:40:01 -07001936 tline = expand_smacro(tline->next);
1937 skip_white_(tline);
1938 if (!tok_type_(tline, TOK_NUMBER)) {
1939 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07001940 } else {
1941 def->nparam_min = def->nparam_max =
1942 readnum(tline->text, &err);
1943 if (err)
1944 error(ERR_NONFATAL,
1945 "unable to parse parameter count `%s'", tline->text);
1946 }
1947 if (tline && tok_is_(tline->next, "-")) {
1948 tline = tline->next->next;
1949 if (tok_is_(tline, "*")) {
1950 def->nparam_max = INT_MAX;
1951 } else if (!tok_type_(tline, TOK_NUMBER)) {
1952 error(ERR_NONFATAL,
1953 "`%s' expects a parameter count after `-'", directive);
1954 } else {
1955 def->nparam_max = readnum(tline->text, &err);
1956 if (err) {
1957 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
1958 tline->text);
1959 }
1960 if (def->nparam_min > def->nparam_max) {
1961 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
1962 }
1963 }
1964 }
1965 if (tline && tok_is_(tline->next, "+")) {
1966 tline = tline->next;
1967 def->plus = true;
1968 }
1969 if (tline && tok_type_(tline->next, TOK_ID) &&
1970 !nasm_stricmp(tline->next->text, ".nolist")) {
1971 tline = tline->next;
1972 def->nolist = true;
1973 }
Keith Kanios891775e2009-07-11 06:08:54 -05001974
1975 /*
1976 * Handle maximum recursion depth.
1977 */
1978 if (tline && tok_is_(tline->next, ",")) {
1979 tline = tline->next->next;
1980 if (tok_is_(tline, "*")) {
1981 def->max_depth = 65536; /* should we eliminate this? */
1982 } else if (!tok_type_(tline, TOK_NUMBER)) {
1983 error(ERR_NONFATAL,
1984 "`%s' expects a maximum recursion depth after `,'", directive);
1985 } else {
1986 def->max_depth = readnum(tline->text, &err);
1987 if (err) {
1988 error(ERR_NONFATAL, "unable to parse maximum recursion depth `%s'",
1989 tline->text);
1990 }
1991 }
1992 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07001993
1994 /*
1995 * Handle default parameters.
1996 */
1997 if (tline && tline->next) {
1998 def->dlist = tline->next;
1999 tline->next = NULL;
2000 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
2001 } else {
2002 def->dlist = NULL;
2003 def->defaults = NULL;
2004 }
2005 def->expansion = NULL;
2006
Victor van den Elzen22343c22008-08-06 14:47:54 +02002007 if(def->defaults &&
2008 def->ndefs > def->nparam_max - def->nparam_min &&
2009 !def->plus)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002010 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2011 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002012
H. Peter Anvina26433d2008-07-16 14:40:01 -07002013 return true;
2014}
2015
2016
2017/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002018 * Decode a size directive
2019 */
2020static int parse_size(const char *str) {
2021 static const char *size_names[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07002022 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002023 static const int sizes[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07002024 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002025
2026 return sizes[bsii(str, size_names, elements(size_names))+1];
2027}
2028
Ed Beroset3ab3f412002-06-11 03:31:49 +00002029/**
2030 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002031 * Find out if a line contains a preprocessor directive, and deal
2032 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002033 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002034 * If a directive _is_ found, it is the responsibility of this routine
2035 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002036 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002037 * @param tline a pointer to the current tokeninzed line linked list
2038 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002039 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002040 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002041static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002042{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002043 enum preproc_token i;
2044 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002045 bool err;
2046 int nparam;
2047 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002048 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002049 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002050 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002051 char *p, *pp;
2052 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002053 Include *inc;
2054 Context *ctx;
2055 Cond *cond;
H. Peter Anvin97a23472007-09-16 17:57:25 -07002056 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002057 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
2058 Line *l;
2059 struct tokenval tokval;
2060 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002061 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002062 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002063 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002064 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002065
2066 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002067
H. Peter Anvineba20a72002-04-30 20:53:55 +00002068 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002069 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002070 (tline->text[1] == '%' || tline->text[1] == '$'
2071 || tline->text[1] == '!'))
2072 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002073
H. Peter Anvin4169a472007-09-12 01:29:43 +00002074 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002075
2076 /*
2077 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00002078 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002079 * we should ignore all directives except for condition
2080 * directives.
2081 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002082 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002083 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2084 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002085 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002086
2087 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002088 * If we're defining a macro or reading a %rep block, we should
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002089 * ignore all directives except for %macro/%imacro (which nest),
2090 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2091 * If we're in a %rep block, another %rep nests, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002092 */
2093 if (defining && i != PP_MACRO && i != PP_IMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00002094 i != PP_ENDMACRO && i != PP_ENDM &&
2095 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2096 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002097 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002098
Charles Crayned4200be2008-07-12 16:42:33 -07002099 if (defining) {
2100 if (i == PP_MACRO || i == PP_IMACRO) {
2101 nested_mac_count++;
2102 return NO_DIRECTIVE_FOUND;
2103 } else if (nested_mac_count > 0) {
2104 if (i == PP_ENDMACRO) {
2105 nested_mac_count--;
2106 return NO_DIRECTIVE_FOUND;
2107 }
2108 }
2109 if (!defining->name) {
2110 if (i == PP_REP) {
2111 nested_rep_count++;
2112 return NO_DIRECTIVE_FOUND;
2113 } else if (nested_rep_count > 0) {
2114 if (i == PP_ENDREP) {
2115 nested_rep_count--;
2116 return NO_DIRECTIVE_FOUND;
2117 }
2118 }
2119 }
2120 }
2121
H. Peter Anvin4169a472007-09-12 01:29:43 +00002122 switch (i) {
2123 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002124 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2125 tline->text);
2126 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002127
H. Peter Anvine2c80182005-01-15 22:15:51 +00002128 case PP_STACKSIZE:
2129 /* Directive to tell NASM what the default stack size is. The
2130 * default is for a 16-bit stack, and this can be overriden with
2131 * %stacksize large.
2132 * the following form:
2133 *
2134 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2135 */
2136 tline = tline->next;
2137 if (tline && tline->type == TOK_WHITESPACE)
2138 tline = tline->next;
2139 if (!tline || tline->type != TOK_ID) {
2140 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2141 free_tlist(origline);
2142 return DIRECTIVE_FOUND;
2143 }
2144 if (nasm_stricmp(tline->text, "flat") == 0) {
2145 /* All subsequent ARG directives are for a 32-bit stack */
2146 StackSize = 4;
2147 StackPointer = "ebp";
2148 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002149 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002150 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2151 /* All subsequent ARG directives are for a 64-bit stack */
2152 StackSize = 8;
2153 StackPointer = "rbp";
2154 ArgOffset = 8;
2155 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002156 } else if (nasm_stricmp(tline->text, "large") == 0) {
2157 /* All subsequent ARG directives are for a 16-bit stack,
2158 * far function call.
2159 */
2160 StackSize = 2;
2161 StackPointer = "bp";
2162 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002163 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002164 } else if (nasm_stricmp(tline->text, "small") == 0) {
2165 /* All subsequent ARG directives are for a 16-bit stack,
2166 * far function call. We don't support near functions.
2167 */
2168 StackSize = 2;
2169 StackPointer = "bp";
2170 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002171 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002172 } else {
2173 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2174 free_tlist(origline);
2175 return DIRECTIVE_FOUND;
2176 }
2177 free_tlist(origline);
2178 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002179
H. Peter Anvine2c80182005-01-15 22:15:51 +00002180 case PP_ARG:
2181 /* TASM like ARG directive to define arguments to functions, in
2182 * the following form:
2183 *
2184 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2185 */
2186 offset = ArgOffset;
2187 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002188 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002189 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002190
H. Peter Anvine2c80182005-01-15 22:15:51 +00002191 /* Find the argument name */
2192 tline = tline->next;
2193 if (tline && tline->type == TOK_WHITESPACE)
2194 tline = tline->next;
2195 if (!tline || tline->type != TOK_ID) {
2196 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2197 free_tlist(origline);
2198 return DIRECTIVE_FOUND;
2199 }
2200 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002201
H. Peter Anvine2c80182005-01-15 22:15:51 +00002202 /* Find the argument size type */
2203 tline = tline->next;
2204 if (!tline || tline->type != TOK_OTHER
2205 || tline->text[0] != ':') {
2206 error(ERR_NONFATAL,
2207 "Syntax error processing `%%arg' directive");
2208 free_tlist(origline);
2209 return DIRECTIVE_FOUND;
2210 }
2211 tline = tline->next;
2212 if (!tline || tline->type != TOK_ID) {
2213 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2214 free_tlist(origline);
2215 return DIRECTIVE_FOUND;
2216 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002217
H. Peter Anvine2c80182005-01-15 22:15:51 +00002218 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002219 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002220 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002221 size = parse_size(tt->text);
2222 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002223 error(ERR_NONFATAL,
2224 "Invalid size type for `%%arg' missing directive");
2225 free_tlist(tt);
2226 free_tlist(origline);
2227 return DIRECTIVE_FOUND;
2228 }
2229 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002230
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002231 /* Round up to even stack slots */
2232 size = (size+StackSize-1) & ~(StackSize-1);
2233
H. Peter Anvine2c80182005-01-15 22:15:51 +00002234 /* Now define the macro for the argument */
2235 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2236 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002237 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002238 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002239
H. Peter Anvine2c80182005-01-15 22:15:51 +00002240 /* Move to the next argument in the list */
2241 tline = tline->next;
2242 if (tline && tline->type == TOK_WHITESPACE)
2243 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002244 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2245 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002246 free_tlist(origline);
2247 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002248
H. Peter Anvine2c80182005-01-15 22:15:51 +00002249 case PP_LOCAL:
2250 /* TASM like LOCAL directive to define local variables for a
2251 * function, in the following form:
2252 *
2253 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2254 *
2255 * The '= LocalSize' at the end is ignored by NASM, but is
2256 * required by TASM to define the local parameter size (and used
2257 * by the TASM macro package).
2258 */
2259 offset = LocalOffset;
2260 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002261 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002262 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002263
H. Peter Anvine2c80182005-01-15 22:15:51 +00002264 /* Find the argument name */
2265 tline = tline->next;
2266 if (tline && tline->type == TOK_WHITESPACE)
2267 tline = tline->next;
2268 if (!tline || tline->type != TOK_ID) {
2269 error(ERR_NONFATAL,
2270 "`%%local' missing argument parameter");
2271 free_tlist(origline);
2272 return DIRECTIVE_FOUND;
2273 }
2274 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002275
H. Peter Anvine2c80182005-01-15 22:15:51 +00002276 /* Find the argument size type */
2277 tline = tline->next;
2278 if (!tline || tline->type != TOK_OTHER
2279 || tline->text[0] != ':') {
2280 error(ERR_NONFATAL,
2281 "Syntax error processing `%%local' directive");
2282 free_tlist(origline);
2283 return DIRECTIVE_FOUND;
2284 }
2285 tline = tline->next;
2286 if (!tline || tline->type != TOK_ID) {
2287 error(ERR_NONFATAL,
2288 "`%%local' missing size type parameter");
2289 free_tlist(origline);
2290 return DIRECTIVE_FOUND;
2291 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002292
H. Peter Anvine2c80182005-01-15 22:15:51 +00002293 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002294 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002295 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002296 size = parse_size(tt->text);
2297 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002298 error(ERR_NONFATAL,
2299 "Invalid size type for `%%local' missing directive");
2300 free_tlist(tt);
2301 free_tlist(origline);
2302 return DIRECTIVE_FOUND;
2303 }
2304 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002305
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002306 /* Round up to even stack slots */
2307 size = (size+StackSize-1) & ~(StackSize-1);
2308
2309 offset += size; /* Negative offset, increment before */
2310
2311 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002312 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2313 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002314 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002315
H. Peter Anvine2c80182005-01-15 22:15:51 +00002316 /* Now define the assign to setup the enter_c macro correctly */
2317 snprintf(directive, sizeof(directive),
2318 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002319 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002320
H. Peter Anvine2c80182005-01-15 22:15:51 +00002321 /* Move to the next argument in the list */
2322 tline = tline->next;
2323 if (tline && tline->type == TOK_WHITESPACE)
2324 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002325 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2326 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002327 free_tlist(origline);
2328 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002329
H. Peter Anvine2c80182005-01-15 22:15:51 +00002330 case PP_CLEAR:
2331 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002332 error(ERR_WARNING|ERR_PASS1,
2333 "trailing garbage after `%%clear' ignored");
H. Peter Anvin97a23472007-09-16 17:57:25 -07002334 free_macros();
2335 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002336 free_tlist(origline);
2337 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002338
H. Peter Anvin418ca702008-05-30 10:42:30 -07002339 case PP_DEPEND:
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002340 t = tline->next = expand_smacro(tline->next);
2341 skip_white_(t);
2342 if (!t || (t->type != TOK_STRING &&
2343 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002344 error(ERR_NONFATAL, "`%%depend' expects a file name");
2345 free_tlist(origline);
2346 return DIRECTIVE_FOUND; /* but we did _something_ */
2347 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002348 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002349 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002350 "trailing garbage after `%%depend' ignored");
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002351 p = t->text;
2352 if (t->type != TOK_INTERNAL_STRING)
2353 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002354 if (dephead && !in_list(*dephead, p)) {
2355 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2356 sl->next = NULL;
2357 strcpy(sl->str, p);
2358 *deptail = sl;
2359 deptail = &sl->next;
2360 }
2361 free_tlist(origline);
2362 return DIRECTIVE_FOUND;
2363
2364 case PP_INCLUDE:
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002365 t = tline->next = expand_smacro(tline->next);
2366 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002367
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002368 if (!t || (t->type != TOK_STRING &&
2369 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002370 error(ERR_NONFATAL, "`%%include' expects a file name");
2371 free_tlist(origline);
2372 return DIRECTIVE_FOUND; /* but we did _something_ */
2373 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002374 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002375 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002376 "trailing garbage after `%%include' ignored");
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002377 p = t->text;
2378 if (t->type != TOK_INTERNAL_STRING)
2379 nasm_unquote(p, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002380 inc = nasm_malloc(sizeof(Include));
2381 inc->next = istk;
2382 inc->conds = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002383 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002384 if (!inc->fp) {
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002385 /* -MG given but file not found */
2386 nasm_free(inc);
2387 } else {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002388 inc->fname = src_set_fname(nasm_strdup(p));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002389 inc->lineno = src_set_linnum(0);
2390 inc->lineinc = 1;
2391 inc->expansion = NULL;
2392 inc->mstk = NULL;
2393 istk = inc;
2394 list->uplevel(LIST_INCLUDE);
2395 }
2396 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002397 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002398
H. Peter Anvind2456592008-06-19 15:04:18 -07002399 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002400 {
H. Peter Anvina70547f2008-07-19 21:44:26 -07002401 static macros_t *use_pkg;
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07002402 const char *pkg_macro;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002403
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002404 tline = tline->next;
2405 skip_white_(tline);
2406 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002407
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002408 if (!tline || (tline->type != TOK_STRING &&
2409 tline->type != TOK_INTERNAL_STRING &&
2410 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002411 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002412 free_tlist(origline);
2413 return DIRECTIVE_FOUND; /* but we did _something_ */
2414 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002415 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002416 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002417 "trailing garbage after `%%use' ignored");
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002418 if (tline->type == TOK_STRING)
2419 nasm_unquote(tline->text, NULL);
2420 use_pkg = nasm_stdmac_find_package(tline->text);
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002421 if (!use_pkg)
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002422 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07002423 /* The first string will be <%define>__USE_*__ */
H. Peter Anvin7e50d232008-06-25 14:54:14 -07002424 pkg_macro = (char *)use_pkg + 1;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002425 if (!smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
2426 /* Not already included, go ahead and include it */
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002427 stdmacpos = use_pkg;
2428 }
H. Peter Anvind2456592008-06-19 15:04:18 -07002429 free_tlist(origline);
2430 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002431 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002432 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002433 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002434 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002435 tline = tline->next;
2436 skip_white_(tline);
2437 tline = expand_id(tline);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002438 if (tline) {
2439 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin42b56392008-10-24 16:24:21 -07002440 error(ERR_NONFATAL, "`%s' expects a context identifier",
2441 pp_directives[i]);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002442 free_tlist(origline);
2443 return DIRECTIVE_FOUND; /* but we did _something_ */
2444 }
2445 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002446 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin42b56392008-10-24 16:24:21 -07002447 "trailing garbage after `%s' ignored",
2448 pp_directives[i]);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002449 p = nasm_strdup(tline->text);
2450 } else {
H. Peter Anvin42b56392008-10-24 16:24:21 -07002451 p = NULL; /* Anonymous */
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002452 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002453
2454 if (i == PP_PUSH) {
2455 ctx = nasm_malloc(sizeof(Context));
2456 ctx->next = cstk;
2457 hash_init(&ctx->localmac, HASH_SMALL);
2458 ctx->name = p;
2459 ctx->number = unique++;
2460 cstk = ctx;
2461 } else {
2462 /* %pop or %repl */
2463 if (!cstk) {
2464 error(ERR_NONFATAL, "`%s': context stack is empty",
2465 pp_directives[i]);
2466 } else if (i == PP_POP) {
2467 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2468 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2469 "expected %s",
2470 cstk->name ? cstk->name : "anonymous", p);
2471 else
2472 ctx_pop();
2473 } else {
2474 /* i == PP_REPL */
2475 nasm_free(cstk->name);
2476 cstk->name = p;
2477 p = NULL;
2478 }
2479 nasm_free(p);
2480 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002481 free_tlist(origline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002482 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002483 case PP_FATAL:
H. Peter Anvin931cab62009-07-07 16:06:21 -07002484 severity = ERR_FATAL;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002485 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002486 case PP_ERROR:
H. Peter Anvin931cab62009-07-07 16:06:21 -07002487 severity = ERR_NONFATAL;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002488 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002489 case PP_WARNING:
H. Peter Anvin931cab62009-07-07 16:06:21 -07002490 severity = ERR_WARNING|ERR_WARN_USER;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002491 goto issue_error;
2492
2493 issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002494 {
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002495 /* Only error out if this is the final pass */
2496 if (pass != 2 && i != PP_FATAL)
2497 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002498
H. Peter Anvine2c80182005-01-15 22:15:51 +00002499 tline->next = expand_smacro(tline->next);
2500 tline = tline->next;
2501 skip_white_(tline);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002502 t = tline ? tline->next : NULL;
2503 skip_white_(t);
2504 if (tok_type_(tline, TOK_STRING) && !t) {
2505 /* The line contains only a quoted string */
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002506 p = tline->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002507 nasm_unquote(p, NULL);
H. Peter Anvin931cab62009-07-07 16:06:21 -07002508 error(severity, "%s", p);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002509 } else {
2510 /* Not a quoted string, or more than a quoted string */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002511 p = detoken(tline, false);
H. Peter Anvin931cab62009-07-07 16:06:21 -07002512 error(severity, "%s", p);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002513 nasm_free(p);
H. Peter Anvind2456592008-06-19 15:04:18 -07002514 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002515 free_tlist(origline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002516 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002517 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002518
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002519 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002520 if (istk->conds && !emitting(istk->conds->state))
2521 j = COND_NEVER;
2522 else {
2523 j = if_condition(tline->next, i);
2524 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002525 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2526 }
2527 cond = nasm_malloc(sizeof(Cond));
2528 cond->next = istk->conds;
2529 cond->state = j;
2530 istk->conds = cond;
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002531 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002532 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002533
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002534 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002535 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00002536 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002537 switch(istk->conds->state) {
2538 case COND_IF_TRUE:
2539 istk->conds->state = COND_DONE;
2540 break;
2541
2542 case COND_DONE:
2543 case COND_NEVER:
2544 break;
2545
2546 case COND_ELSE_TRUE:
2547 case COND_ELSE_FALSE:
H. Peter Anvin917a3492008-09-24 09:14:49 -07002548 error_precond(ERR_WARNING|ERR_PASS1,
2549 "`%%elif' after `%%else' ignored");
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002550 istk->conds->state = COND_NEVER;
2551 break;
2552
2553 case COND_IF_FALSE:
2554 /*
2555 * IMPORTANT: In the case of %if, we will already have
2556 * called expand_mmac_params(); however, if we're
2557 * processing an %elif we must have been in a
2558 * non-emitting mode, which would have inhibited
H. Peter Anvin67c63722008-10-26 23:49:00 -07002559 * the normal invocation of expand_mmac_params().
2560 * Therefore, we have to do it explicitly here.
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002561 */
H. Peter Anvin67c63722008-10-26 23:49:00 -07002562 j = if_condition(expand_mmac_params(tline->next), i);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002563 tline->next = NULL; /* it got freed */
2564 istk->conds->state =
2565 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2566 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002567 }
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002568 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002569 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002570
H. Peter Anvine2c80182005-01-15 22:15:51 +00002571 case PP_ELSE:
2572 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002573 error_precond(ERR_WARNING|ERR_PASS1,
2574 "trailing garbage after `%%else' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002575 if (!istk->conds)
2576 error(ERR_FATAL, "`%%else': no matching `%%if'");
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002577 switch(istk->conds->state) {
2578 case COND_IF_TRUE:
2579 case COND_DONE:
2580 istk->conds->state = COND_ELSE_FALSE;
2581 break;
2582
2583 case COND_NEVER:
2584 break;
2585
2586 case COND_IF_FALSE:
2587 istk->conds->state = COND_ELSE_TRUE;
2588 break;
2589
2590 case COND_ELSE_TRUE:
2591 case COND_ELSE_FALSE:
H. Peter Anvin917a3492008-09-24 09:14:49 -07002592 error_precond(ERR_WARNING|ERR_PASS1,
2593 "`%%else' after `%%else' ignored.");
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002594 istk->conds->state = COND_NEVER;
2595 break;
2596 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002597 free_tlist(origline);
2598 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002599
H. Peter Anvine2c80182005-01-15 22:15:51 +00002600 case PP_ENDIF:
2601 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002602 error_precond(ERR_WARNING|ERR_PASS1,
2603 "trailing garbage after `%%endif' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002604 if (!istk->conds)
2605 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2606 cond = istk->conds;
2607 istk->conds = cond->next;
2608 nasm_free(cond);
2609 free_tlist(origline);
2610 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002611
H. Peter Anvine2c80182005-01-15 22:15:51 +00002612 case PP_MACRO:
2613 case PP_IMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07002614 if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002615 error(ERR_FATAL,
2616 "`%%%smacro': already defining a macro",
2617 (i == PP_IMACRO ? "i" : ""));
H. Peter Anvina26433d2008-07-16 14:40:01 -07002618 return DIRECTIVE_FOUND;
2619 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002620 defining = nasm_malloc(sizeof(MMacro));
H. Peter Anvina26433d2008-07-16 14:40:01 -07002621 defining->casesense = (i == PP_MACRO);
2622 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2623 nasm_free(defining);
2624 defining = NULL;
2625 return DIRECTIVE_FOUND;
2626 }
2627
H. Peter Anvin166c2472008-05-28 12:28:58 -07002628 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002629 while (mmac) {
2630 if (!strcmp(mmac->name, defining->name) &&
2631 (mmac->nparam_min <= defining->nparam_max
2632 || defining->plus)
2633 && (defining->nparam_min <= mmac->nparam_max
2634 || mmac->plus)) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07002635 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002636 "redefining multi-line macro `%s'", defining->name);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002637 return DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002638 }
2639 mmac = mmac->next;
2640 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002641 free_tlist(origline);
2642 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002643
H. Peter Anvine2c80182005-01-15 22:15:51 +00002644 case PP_ENDM:
2645 case PP_ENDMACRO:
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002646 if (! (defining && defining->name)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002647 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2648 return DIRECTIVE_FOUND;
2649 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07002650 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002651 defining->next = *mmhead;
2652 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002653 defining = NULL;
2654 free_tlist(origline);
2655 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002656
H. Peter Anvina26433d2008-07-16 14:40:01 -07002657 case PP_UNMACRO:
2658 case PP_UNIMACRO:
2659 {
2660 MMacro **mmac_p;
2661 MMacro spec;
2662
2663 spec.casesense = (i == PP_UNMACRO);
2664 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2665 return DIRECTIVE_FOUND;
2666 }
2667 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2668 while (mmac_p && *mmac_p) {
2669 mmac = *mmac_p;
2670 if (mmac->casesense == spec.casesense &&
2671 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2672 mmac->nparam_min == spec.nparam_min &&
2673 mmac->nparam_max == spec.nparam_max &&
2674 mmac->plus == spec.plus) {
2675 *mmac_p = mmac->next;
2676 free_mmacro(mmac);
2677 } else {
2678 mmac_p = &mmac->next;
2679 }
2680 }
2681 free_tlist(origline);
2682 free_tlist(spec.dlist);
2683 return DIRECTIVE_FOUND;
2684 }
2685
H. Peter Anvine2c80182005-01-15 22:15:51 +00002686 case PP_ROTATE:
2687 if (tline->next && tline->next->type == TOK_WHITESPACE)
2688 tline = tline->next;
2689 if (tline->next == NULL) {
2690 free_tlist(origline);
2691 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2692 return DIRECTIVE_FOUND;
2693 }
2694 t = expand_smacro(tline->next);
2695 tline->next = NULL;
2696 free_tlist(origline);
2697 tline = t;
2698 tptr = &t;
2699 tokval.t_type = TOKEN_INVALID;
2700 evalresult =
2701 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2702 free_tlist(tline);
2703 if (!evalresult)
2704 return DIRECTIVE_FOUND;
2705 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002706 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002707 "trailing garbage after expression ignored");
2708 if (!is_simple(evalresult)) {
2709 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2710 return DIRECTIVE_FOUND;
2711 }
2712 mmac = istk->mstk;
2713 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2714 mmac = mmac->next_active;
2715 if (!mmac) {
2716 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2717 } else if (mmac->nparam == 0) {
2718 error(ERR_NONFATAL,
2719 "`%%rotate' invoked within macro without parameters");
2720 } else {
H. Peter Anvin25a99342007-09-22 17:45:45 -07002721 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002722
H. Peter Anvin25a99342007-09-22 17:45:45 -07002723 rotate %= (int)mmac->nparam;
2724 if (rotate < 0)
2725 rotate += mmac->nparam;
H. Peter Anvin70653092007-10-19 14:42:29 -07002726
H. Peter Anvin25a99342007-09-22 17:45:45 -07002727 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002728 }
2729 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002730
H. Peter Anvine2c80182005-01-15 22:15:51 +00002731 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002732 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002733 do {
2734 tline = tline->next;
2735 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002736
H. Peter Anvine2c80182005-01-15 22:15:51 +00002737 if (tok_type_(tline, TOK_ID) &&
2738 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002739 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002740 do {
2741 tline = tline->next;
2742 } while (tok_type_(tline, TOK_WHITESPACE));
2743 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002744
H. Peter Anvine2c80182005-01-15 22:15:51 +00002745 if (tline) {
2746 t = expand_smacro(tline);
2747 tptr = &t;
2748 tokval.t_type = TOKEN_INVALID;
2749 evalresult =
2750 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2751 if (!evalresult) {
2752 free_tlist(origline);
2753 return DIRECTIVE_FOUND;
2754 }
2755 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002756 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002757 "trailing garbage after expression ignored");
2758 if (!is_simple(evalresult)) {
2759 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2760 return DIRECTIVE_FOUND;
2761 }
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002762 count = reloc_value(evalresult) + 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002763 } else {
2764 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002765 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002766 }
2767 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002768
H. Peter Anvine2c80182005-01-15 22:15:51 +00002769 tmp_defining = defining;
2770 defining = nasm_malloc(sizeof(MMacro));
2771 defining->name = NULL; /* flags this macro as a %rep block */
H. Peter Anvin70055962007-10-11 00:05:31 -07002772 defining->casesense = false;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002773 defining->plus = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002774 defining->nolist = nolist;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002775 defining->in_progress = count;
Keith Kanios891775e2009-07-11 06:08:54 -05002776 defining->max_depth = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002777 defining->nparam_min = defining->nparam_max = 0;
2778 defining->defaults = NULL;
2779 defining->dlist = NULL;
2780 defining->expansion = NULL;
2781 defining->next_active = istk->mstk;
2782 defining->rep_nest = tmp_defining;
2783 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002784
H. Peter Anvine2c80182005-01-15 22:15:51 +00002785 case PP_ENDREP:
2786 if (!defining || defining->name) {
2787 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2788 return DIRECTIVE_FOUND;
2789 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002790
H. Peter Anvine2c80182005-01-15 22:15:51 +00002791 /*
2792 * Now we have a "macro" defined - although it has no name
2793 * and we won't be entering it in the hash tables - we must
2794 * push a macro-end marker for it on to istk->expansion.
2795 * After that, it will take care of propagating itself (a
2796 * macro-end marker line for a macro which is really a %rep
2797 * block will cause the macro to be re-expanded, complete
2798 * with another macro-end marker to ensure the process
2799 * continues) until the whole expansion is forcibly removed
2800 * from istk->expansion by a %exitrep.
2801 */
2802 l = nasm_malloc(sizeof(Line));
2803 l->next = istk->expansion;
2804 l->finishes = defining;
2805 l->first = NULL;
2806 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002807
H. Peter Anvine2c80182005-01-15 22:15:51 +00002808 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002809
H. Peter Anvine2c80182005-01-15 22:15:51 +00002810 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2811 tmp_defining = defining;
2812 defining = defining->rep_nest;
2813 free_tlist(origline);
2814 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002815
H. Peter Anvine2c80182005-01-15 22:15:51 +00002816 case PP_EXITREP:
2817 /*
2818 * We must search along istk->expansion until we hit a
2819 * macro-end marker for a macro with no name. Then we set
2820 * its `in_progress' flag to 0.
2821 */
2822 for (l = istk->expansion; l; l = l->next)
2823 if (l->finishes && !l->finishes->name)
H. Peter Anvinca348b62008-07-23 10:49:26 -04002824 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002825
H. Peter Anvine2c80182005-01-15 22:15:51 +00002826 if (l)
Charles Crayned4200be2008-07-12 16:42:33 -07002827 l->finishes->in_progress = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002828 else
2829 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2830 free_tlist(origline);
2831 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002832
H. Peter Anvine2c80182005-01-15 22:15:51 +00002833 case PP_XDEFINE:
2834 case PP_IXDEFINE:
2835 case PP_DEFINE:
2836 case PP_IDEFINE:
H. Peter Anvin95e7f952007-10-11 13:38:38 -07002837 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002838
H. Peter Anvine2c80182005-01-15 22:15:51 +00002839 tline = tline->next;
2840 skip_white_(tline);
2841 tline = expand_id(tline);
2842 if (!tline || (tline->type != TOK_ID &&
2843 (tline->type != TOK_PREPROC_ID ||
2844 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002845 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2846 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002847 free_tlist(origline);
2848 return DIRECTIVE_FOUND;
2849 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002850
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002851 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002852 last = tline;
2853 param_start = tline = tline->next;
2854 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002855
H. Peter Anvine2c80182005-01-15 22:15:51 +00002856 /* Expand the macro definition now for %xdefine and %ixdefine */
2857 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2858 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002859
H. Peter Anvine2c80182005-01-15 22:15:51 +00002860 if (tok_is_(tline, "(")) {
2861 /*
2862 * This macro has parameters.
2863 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002864
H. Peter Anvine2c80182005-01-15 22:15:51 +00002865 tline = tline->next;
2866 while (1) {
2867 skip_white_(tline);
2868 if (!tline) {
2869 error(ERR_NONFATAL, "parameter identifier expected");
2870 free_tlist(origline);
2871 return DIRECTIVE_FOUND;
2872 }
2873 if (tline->type != TOK_ID) {
2874 error(ERR_NONFATAL,
2875 "`%s': parameter identifier expected",
2876 tline->text);
2877 free_tlist(origline);
2878 return DIRECTIVE_FOUND;
2879 }
2880 tline->type = TOK_SMAC_PARAM + nparam++;
2881 tline = tline->next;
2882 skip_white_(tline);
2883 if (tok_is_(tline, ",")) {
2884 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04002885 } else {
2886 if (!tok_is_(tline, ")")) {
2887 error(ERR_NONFATAL,
2888 "`)' expected to terminate macro template");
2889 free_tlist(origline);
2890 return DIRECTIVE_FOUND;
2891 }
2892 break;
2893 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002894 }
2895 last = tline;
2896 tline = tline->next;
2897 }
2898 if (tok_type_(tline, TOK_WHITESPACE))
2899 last = tline, tline = tline->next;
2900 macro_start = NULL;
2901 last->next = NULL;
2902 t = tline;
2903 while (t) {
2904 if (t->type == TOK_ID) {
2905 for (tt = param_start; tt; tt = tt->next)
2906 if (tt->type >= TOK_SMAC_PARAM &&
2907 !strcmp(tt->text, t->text))
2908 t->type = tt->type;
2909 }
2910 tt = t->next;
2911 t->next = macro_start;
2912 macro_start = t;
2913 t = tt;
2914 }
2915 /*
2916 * Good. We now have a macro name, a parameter count, and a
2917 * token list (in reverse order) for an expansion. We ought
2918 * to be OK just to create an SMacro, store it, and let
2919 * free_tlist have the rest of the line (which we have
2920 * carefully re-terminated after chopping off the expansion
2921 * from the end).
2922 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002923 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002924 free_tlist(origline);
2925 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002926
H. Peter Anvine2c80182005-01-15 22:15:51 +00002927 case PP_UNDEF:
2928 tline = tline->next;
2929 skip_white_(tline);
2930 tline = expand_id(tline);
2931 if (!tline || (tline->type != TOK_ID &&
2932 (tline->type != TOK_PREPROC_ID ||
2933 tline->text[1] != '$'))) {
2934 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2935 free_tlist(origline);
2936 return DIRECTIVE_FOUND;
2937 }
2938 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07002939 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002940 "trailing garbage after macro name ignored");
2941 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002942
H. Peter Anvine2c80182005-01-15 22:15:51 +00002943 /* Find the context that symbol belongs to */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002944 ctx = get_ctx(tline->text, &mname, false);
2945 undef_smacro(ctx, mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002946 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002947 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002948
H. Peter Anvin9e200162008-06-04 17:23:14 -07002949 case PP_DEFSTR:
2950 case PP_IDEFSTR:
2951 casesense = (i == PP_DEFSTR);
2952
2953 tline = tline->next;
2954 skip_white_(tline);
2955 tline = expand_id(tline);
2956 if (!tline || (tline->type != TOK_ID &&
2957 (tline->type != TOK_PREPROC_ID ||
2958 tline->text[1] != '$'))) {
2959 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2960 pp_directives[i]);
2961 free_tlist(origline);
2962 return DIRECTIVE_FOUND;
2963 }
2964
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002965 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07002966 last = tline;
2967 tline = expand_smacro(tline->next);
2968 last->next = NULL;
2969
2970 while (tok_type_(tline, TOK_WHITESPACE))
2971 tline = delete_Token(tline);
2972
2973 p = detoken(tline, false);
2974 macro_start = nasm_malloc(sizeof(*macro_start));
2975 macro_start->next = NULL;
2976 macro_start->text = nasm_quote(p, strlen(p));
2977 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002978 macro_start->a.mac = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07002979 nasm_free(p);
2980
2981 /*
2982 * We now have a macro name, an implicit parameter count of
2983 * zero, and a string token to use as an expansion. Create
2984 * and store an SMacro.
2985 */
2986 define_smacro(ctx, mname, casesense, 0, macro_start);
2987 free_tlist(origline);
2988 return DIRECTIVE_FOUND;
2989
H. Peter Anvin418ca702008-05-30 10:42:30 -07002990 case PP_PATHSEARCH:
2991 {
2992 FILE *fp;
2993 StrList *xsl = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002994 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07002995
2996 casesense = true;
2997
2998 tline = tline->next;
2999 skip_white_(tline);
3000 tline = expand_id(tline);
3001 if (!tline || (tline->type != TOK_ID &&
3002 (tline->type != TOK_PREPROC_ID ||
3003 tline->text[1] != '$'))) {
3004 error(ERR_NONFATAL,
3005 "`%%pathsearch' expects a macro identifier as first parameter");
3006 free_tlist(origline);
3007 return DIRECTIVE_FOUND;
3008 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003009 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003010 last = tline;
3011 tline = expand_smacro(tline->next);
3012 last->next = NULL;
3013
3014 t = tline;
3015 while (tok_type_(t, TOK_WHITESPACE))
3016 t = t->next;
3017
3018 if (!t || (t->type != TOK_STRING &&
3019 t->type != TOK_INTERNAL_STRING)) {
3020 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
3021 free_tlist(tline);
3022 free_tlist(origline);
3023 return DIRECTIVE_FOUND; /* but we did _something_ */
3024 }
3025 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003026 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003027 "trailing garbage after `%%pathsearch' ignored");
H. Peter Anvin427cc912008-06-01 21:43:03 -07003028 p = t->text;
3029 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003030 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003031
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07003032 fp = inc_fopen(p, &xsl, &xst, true);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003033 if (fp) {
3034 p = xsl->str;
3035 fclose(fp); /* Don't actually care about the file */
3036 }
3037 macro_start = nasm_malloc(sizeof(*macro_start));
3038 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003039 macro_start->text = nasm_quote(p, strlen(p));
H. Peter Anvin418ca702008-05-30 10:42:30 -07003040 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003041 macro_start->a.mac = NULL;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003042 if (xsl)
3043 nasm_free(xsl);
3044
3045 /*
3046 * We now have a macro name, an implicit parameter count of
3047 * zero, and a string token to use as an expansion. Create
3048 * and store an SMacro.
3049 */
3050 define_smacro(ctx, mname, casesense, 0, macro_start);
3051 free_tlist(tline);
3052 free_tlist(origline);
3053 return DIRECTIVE_FOUND;
3054 }
3055
H. Peter Anvine2c80182005-01-15 22:15:51 +00003056 case PP_STRLEN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003057 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003058
H. Peter Anvine2c80182005-01-15 22:15:51 +00003059 tline = tline->next;
3060 skip_white_(tline);
3061 tline = expand_id(tline);
3062 if (!tline || (tline->type != TOK_ID &&
3063 (tline->type != TOK_PREPROC_ID ||
3064 tline->text[1] != '$'))) {
3065 error(ERR_NONFATAL,
3066 "`%%strlen' expects a macro identifier as first parameter");
3067 free_tlist(origline);
3068 return DIRECTIVE_FOUND;
3069 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003070 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003071 last = tline;
3072 tline = expand_smacro(tline->next);
3073 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003074
H. Peter Anvine2c80182005-01-15 22:15:51 +00003075 t = tline;
3076 while (tok_type_(t, TOK_WHITESPACE))
3077 t = t->next;
3078 /* t should now point to the string */
3079 if (t->type != TOK_STRING) {
3080 error(ERR_NONFATAL,
3081 "`%%strlen` requires string as second parameter");
3082 free_tlist(tline);
3083 free_tlist(origline);
3084 return DIRECTIVE_FOUND;
3085 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003086
H. Peter Anvine2c80182005-01-15 22:15:51 +00003087 macro_start = nasm_malloc(sizeof(*macro_start));
3088 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003089 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003090 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003091
H. Peter Anvine2c80182005-01-15 22:15:51 +00003092 /*
3093 * We now have a macro name, an implicit parameter count of
3094 * zero, and a numeric token to use as an expansion. Create
3095 * and store an SMacro.
3096 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003097 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003098 free_tlist(tline);
3099 free_tlist(origline);
3100 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003101
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003102 case PP_STRCAT:
3103 casesense = true;
3104
3105 tline = tline->next;
3106 skip_white_(tline);
3107 tline = expand_id(tline);
3108 if (!tline || (tline->type != TOK_ID &&
3109 (tline->type != TOK_PREPROC_ID ||
3110 tline->text[1] != '$'))) {
3111 error(ERR_NONFATAL,
3112 "`%%strcat' expects a macro identifier as first parameter");
3113 free_tlist(origline);
3114 return DIRECTIVE_FOUND;
3115 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003116 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003117 last = tline;
3118 tline = expand_smacro(tline->next);
3119 last->next = NULL;
3120
3121 len = 0;
3122 for (t = tline; t; t = t->next) {
3123 switch (t->type) {
3124 case TOK_WHITESPACE:
3125 break;
3126 case TOK_STRING:
3127 len += t->a.len = nasm_unquote(t->text, NULL);
3128 break;
H. Peter Anvinaccf4332008-07-01 21:42:08 -07003129 case TOK_OTHER:
3130 if (!strcmp(t->text, ",")) /* permit comma separators */
3131 break;
3132 /* else fall through */
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003133 default:
3134 error(ERR_NONFATAL,
3135 "non-string passed to `%%strcat' (%d)", t->type);
3136 free_tlist(tline);
3137 free_tlist(origline);
3138 return DIRECTIVE_FOUND;
3139 }
3140 }
3141
3142 p = pp = nasm_malloc(len);
3143 t = tline;
3144 for (t = tline; t; t = t->next) {
3145 if (t->type == TOK_STRING) {
3146 memcpy(p, t->text, t->a.len);
3147 p += t->a.len;
3148 }
3149 }
3150
3151 /*
3152 * We now have a macro name, an implicit parameter count of
3153 * zero, and a numeric token to use as an expansion. Create
3154 * and store an SMacro.
3155 */
3156 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3157 macro_start->text = nasm_quote(pp, len);
3158 nasm_free(pp);
3159 define_smacro(ctx, mname, casesense, 0, macro_start);
3160 free_tlist(tline);
3161 free_tlist(origline);
3162 return DIRECTIVE_FOUND;
3163
H. Peter Anvine2c80182005-01-15 22:15:51 +00003164 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003165 {
3166 int64_t a1, a2;
3167 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003168
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003169 casesense = true;
3170
H. Peter Anvine2c80182005-01-15 22:15:51 +00003171 tline = tline->next;
3172 skip_white_(tline);
3173 tline = expand_id(tline);
3174 if (!tline || (tline->type != TOK_ID &&
3175 (tline->type != TOK_PREPROC_ID ||
3176 tline->text[1] != '$'))) {
3177 error(ERR_NONFATAL,
3178 "`%%substr' expects a macro identifier as first parameter");
3179 free_tlist(origline);
3180 return DIRECTIVE_FOUND;
3181 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003182 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003183 last = tline;
3184 tline = expand_smacro(tline->next);
3185 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003186
H. Peter Anvine2c80182005-01-15 22:15:51 +00003187 t = tline->next;
3188 while (tok_type_(t, TOK_WHITESPACE))
3189 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003190
H. Peter Anvine2c80182005-01-15 22:15:51 +00003191 /* t should now point to the string */
3192 if (t->type != TOK_STRING) {
3193 error(ERR_NONFATAL,
3194 "`%%substr` requires string as second parameter");
3195 free_tlist(tline);
3196 free_tlist(origline);
3197 return DIRECTIVE_FOUND;
3198 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003199
H. Peter Anvine2c80182005-01-15 22:15:51 +00003200 tt = t->next;
3201 tptr = &tt;
3202 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003203 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3204 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003205 if (!evalresult) {
3206 free_tlist(tline);
3207 free_tlist(origline);
3208 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003209 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003210 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3211 free_tlist(tline);
3212 free_tlist(origline);
3213 return DIRECTIVE_FOUND;
3214 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003215 a1 = evalresult->value-1;
3216
3217 while (tok_type_(tt, TOK_WHITESPACE))
3218 tt = tt->next;
3219 if (!tt) {
3220 a2 = 1; /* Backwards compatibility: one character */
3221 } else {
3222 tokval.t_type = TOKEN_INVALID;
3223 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3224 pass, error, NULL);
3225 if (!evalresult) {
3226 free_tlist(tline);
3227 free_tlist(origline);
3228 return DIRECTIVE_FOUND;
3229 } else if (!is_simple(evalresult)) {
3230 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3231 free_tlist(tline);
3232 free_tlist(origline);
3233 return DIRECTIVE_FOUND;
3234 }
3235 a2 = evalresult->value;
3236 }
3237
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003238 len = nasm_unquote(t->text, NULL);
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003239 if (a2 < 0)
3240 a2 = a2+1+len-a1;
3241 if (a1+a2 > (int64_t)len)
3242 a2 = len-a1;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003243
H. Peter Anvine2c80182005-01-15 22:15:51 +00003244 macro_start = nasm_malloc(sizeof(*macro_start));
3245 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003246 macro_start->text = nasm_quote((a1 < 0) ? "" : t->text+a1, a2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003247 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003248 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003249
H. Peter Anvine2c80182005-01-15 22:15:51 +00003250 /*
3251 * We now have a macro name, an implicit parameter count of
3252 * zero, and a numeric token to use as an expansion. Create
3253 * and store an SMacro.
3254 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003255 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003256 free_tlist(tline);
3257 free_tlist(origline);
3258 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003259 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003260
H. Peter Anvine2c80182005-01-15 22:15:51 +00003261 case PP_ASSIGN:
3262 case PP_IASSIGN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003263 casesense = (i == PP_ASSIGN);
3264
H. Peter Anvine2c80182005-01-15 22:15:51 +00003265 tline = tline->next;
3266 skip_white_(tline);
3267 tline = expand_id(tline);
3268 if (!tline || (tline->type != TOK_ID &&
3269 (tline->type != TOK_PREPROC_ID ||
3270 tline->text[1] != '$'))) {
3271 error(ERR_NONFATAL,
3272 "`%%%sassign' expects a macro identifier",
3273 (i == PP_IASSIGN ? "i" : ""));
3274 free_tlist(origline);
3275 return DIRECTIVE_FOUND;
3276 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003277 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003278 last = tline;
3279 tline = expand_smacro(tline->next);
3280 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003281
H. Peter Anvine2c80182005-01-15 22:15:51 +00003282 t = tline;
3283 tptr = &t;
3284 tokval.t_type = TOKEN_INVALID;
3285 evalresult =
3286 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3287 free_tlist(tline);
3288 if (!evalresult) {
3289 free_tlist(origline);
3290 return DIRECTIVE_FOUND;
3291 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003292
H. Peter Anvine2c80182005-01-15 22:15:51 +00003293 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003294 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003295 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003296
H. Peter Anvine2c80182005-01-15 22:15:51 +00003297 if (!is_simple(evalresult)) {
3298 error(ERR_NONFATAL,
3299 "non-constant value given to `%%%sassign'",
3300 (i == PP_IASSIGN ? "i" : ""));
3301 free_tlist(origline);
3302 return DIRECTIVE_FOUND;
3303 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003304
H. Peter Anvine2c80182005-01-15 22:15:51 +00003305 macro_start = nasm_malloc(sizeof(*macro_start));
3306 macro_start->next = NULL;
3307 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003308 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003309
H. Peter Anvine2c80182005-01-15 22:15:51 +00003310 /*
3311 * We now have a macro name, an implicit parameter count of
3312 * zero, and a numeric token to use as an expansion. Create
3313 * and store an SMacro.
3314 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003315 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003316 free_tlist(origline);
3317 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003318
H. Peter Anvine2c80182005-01-15 22:15:51 +00003319 case PP_LINE:
3320 /*
3321 * Syntax is `%line nnn[+mmm] [filename]'
3322 */
3323 tline = tline->next;
3324 skip_white_(tline);
3325 if (!tok_type_(tline, TOK_NUMBER)) {
3326 error(ERR_NONFATAL, "`%%line' expects line number");
3327 free_tlist(origline);
3328 return DIRECTIVE_FOUND;
3329 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003330 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003331 m = 1;
3332 tline = tline->next;
3333 if (tok_is_(tline, "+")) {
3334 tline = tline->next;
3335 if (!tok_type_(tline, TOK_NUMBER)) {
3336 error(ERR_NONFATAL, "`%%line' expects line increment");
3337 free_tlist(origline);
3338 return DIRECTIVE_FOUND;
3339 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003340 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003341 tline = tline->next;
3342 }
3343 skip_white_(tline);
3344 src_set_linnum(k);
3345 istk->lineinc = m;
3346 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003347 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003348 }
3349 free_tlist(origline);
3350 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003351
H. Peter Anvine2c80182005-01-15 22:15:51 +00003352 default:
3353 error(ERR_FATAL,
3354 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003355 pp_directives[i]);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003356 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003357 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003358}
3359
3360/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003361 * Ensure that a macro parameter contains a condition code and
3362 * nothing else. Return the condition code index if so, or -1
3363 * otherwise.
3364 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003365static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003366{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003367 Token *tt;
3368 int i, j, k, m;
3369
H. Peter Anvin25a99342007-09-22 17:45:45 -07003370 if (!t)
3371 return -1; /* Probably a %+ without a space */
3372
H. Peter Anvineba20a72002-04-30 20:53:55 +00003373 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003374 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003375 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003376 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003377 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003378 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003379 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003380
3381 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +00003382 j = elements(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003383 while (j - i > 1) {
3384 k = (j + i) / 2;
3385 m = nasm_stricmp(t->text, conditions[k]);
3386 if (m == 0) {
3387 i = k;
3388 j = -2;
3389 break;
3390 } else if (m < 0) {
3391 j = k;
3392 } else
3393 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003394 }
3395 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003396 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003397 return i;
3398}
3399
H. Peter Anvind784a082009-04-20 14:01:18 -07003400static bool paste_tokens(Token **head, bool handle_paste_tokens)
3401{
3402 Token **tail, *t, *tt;
3403 Token **paste_head;
3404 bool did_paste = false;
3405 char *tmp;
3406
3407 /* Now handle token pasting... */
3408 paste_head = NULL;
3409 tail = head;
3410 while ((t = *tail) && (tt = t->next)) {
3411 switch (t->type) {
3412 case TOK_WHITESPACE:
3413 if (tt->type == TOK_WHITESPACE) {
3414 /* Zap adjacent whitespace tokens */
3415 t->next = delete_Token(tt);
3416 } else {
3417 /* Do not advance paste_head here */
3418 tail = &t->next;
3419 }
3420 break;
3421 case TOK_ID:
3422 case TOK_PREPROC_ID:
3423 case TOK_NUMBER:
3424 case TOK_FLOAT:
3425 {
3426 size_t len = 0;
3427 char *tmp, *p;
3428
3429 while (tt && (tt->type == TOK_ID || tt->type == TOK_PREPROC_ID ||
3430 tt->type == TOK_NUMBER || tt->type == TOK_FLOAT ||
3431 tt->type == TOK_OTHER)) {
3432 len += strlen(tt->text);
3433 tt = tt->next;
3434 }
3435
3436 /* Now tt points to the first token after the potential
3437 paste area... */
3438 if (tt != t->next) {
3439 /* We have at least two tokens... */
3440 len += strlen(t->text);
3441 p = tmp = nasm_malloc(len+1);
3442
3443 while (t != tt) {
3444 strcpy(p, t->text);
3445 p = strchr(p, '\0');
3446 t = delete_Token(t);
3447 }
3448
3449 t = *tail = tokenize(tmp);
3450 nasm_free(tmp);
3451
3452 while (t->next) {
3453 tail = &t->next;
3454 t = t->next;
3455 }
3456 t->next = tt; /* Attach the remaining token chain */
3457
3458 did_paste = true;
3459 }
3460 paste_head = tail;
3461 tail = &t->next;
3462 break;
3463 }
3464 case TOK_PASTE: /* %+ */
3465 if (handle_paste_tokens) {
3466 /* Zap %+ and whitespace tokens to the right */
3467 while (t && (t->type == TOK_WHITESPACE ||
3468 t->type == TOK_PASTE))
3469 t = *tail = delete_Token(t);
3470 if (!paste_head || !t)
3471 break; /* Nothing to paste with */
3472 tail = paste_head;
3473 t = *tail;
3474 tt = t->next;
3475 while (tok_type_(tt, TOK_WHITESPACE))
3476 tt = t->next = delete_Token(tt);
3477
3478 if (tt) {
3479 tmp = nasm_strcat(t->text, tt->text);
3480 delete_Token(t);
3481 tt = delete_Token(tt);
3482 t = *tail = tokenize(tmp);
3483 nasm_free(tmp);
3484 while (t->next) {
3485 tail = &t->next;
3486 t = t->next;
3487 }
3488 t->next = tt; /* Attach the remaining token chain */
3489 did_paste = true;
3490 }
3491 paste_head = tail;
3492 tail = &t->next;
3493 break;
3494 }
3495 /* else fall through */
3496 default:
3497 tail = paste_head = &t->next;
3498 break;
3499 }
3500 }
3501 return did_paste;
3502}
H. Peter Anvin76690a12002-04-30 20:52:49 +00003503/*
3504 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003505 * %-n) and MMacro-local identifiers (%%foo) as well as
3506 * macro indirection (%[...]).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003507 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003508static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003509{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003510 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003511 bool changed = false;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003512
3513 tail = &thead;
3514 thead = NULL;
3515
H. Peter Anvine2c80182005-01-15 22:15:51 +00003516 while (tline) {
3517 if (tline->type == TOK_PREPROC_ID &&
3518 (((tline->text[1] == '+' || tline->text[1] == '-')
3519 && tline->text[2]) || tline->text[1] == '%'
3520 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003521 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003522 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003523 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003524 unsigned int n;
3525 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003526 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003527
H. Peter Anvine2c80182005-01-15 22:15:51 +00003528 t = tline;
3529 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003530
H. Peter Anvine2c80182005-01-15 22:15:51 +00003531 mac = istk->mstk;
3532 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3533 mac = mac->next_active;
3534 if (!mac)
3535 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
3536 else
3537 switch (t->text[1]) {
3538 /*
3539 * We have to make a substitution of one of the
3540 * forms %1, %-1, %+1, %%foo, %0.
3541 */
3542 case '0':
3543 type = TOK_NUMBER;
3544 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3545 text = nasm_strdup(tmpbuf);
3546 break;
3547 case '%':
3548 type = TOK_ID;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003549 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvine2c80182005-01-15 22:15:51 +00003550 mac->unique);
3551 text = nasm_strcat(tmpbuf, t->text + 2);
3552 break;
3553 case '-':
3554 n = atoi(t->text + 2) - 1;
3555 if (n >= mac->nparam)
3556 tt = NULL;
3557 else {
3558 if (mac->nparam > 1)
3559 n = (n + mac->rotate) % mac->nparam;
3560 tt = mac->params[n];
3561 }
3562 cc = find_cc(tt);
3563 if (cc == -1) {
3564 error(ERR_NONFATAL,
3565 "macro parameter %d is not a condition code",
3566 n + 1);
3567 text = NULL;
3568 } else {
3569 type = TOK_ID;
3570 if (inverse_ccs[cc] == -1) {
3571 error(ERR_NONFATAL,
3572 "condition code `%s' is not invertible",
3573 conditions[cc]);
3574 text = NULL;
3575 } else
H. Peter Anvin67c63722008-10-26 23:49:00 -07003576 text = nasm_strdup(conditions[inverse_ccs[cc]]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003577 }
3578 break;
3579 case '+':
3580 n = atoi(t->text + 2) - 1;
3581 if (n >= mac->nparam)
3582 tt = NULL;
3583 else {
3584 if (mac->nparam > 1)
3585 n = (n + mac->rotate) % mac->nparam;
3586 tt = mac->params[n];
3587 }
3588 cc = find_cc(tt);
3589 if (cc == -1) {
3590 error(ERR_NONFATAL,
3591 "macro parameter %d is not a condition code",
3592 n + 1);
3593 text = NULL;
3594 } else {
3595 type = TOK_ID;
3596 text = nasm_strdup(conditions[cc]);
3597 }
3598 break;
3599 default:
3600 n = atoi(t->text + 1) - 1;
3601 if (n >= mac->nparam)
3602 tt = NULL;
3603 else {
3604 if (mac->nparam > 1)
3605 n = (n + mac->rotate) % mac->nparam;
3606 tt = mac->params[n];
3607 }
3608 if (tt) {
3609 for (i = 0; i < mac->paramlen[n]; i++) {
3610 *tail = new_Token(NULL, tt->type, tt->text, 0);
3611 tail = &(*tail)->next;
3612 tt = tt->next;
3613 }
3614 }
3615 text = NULL; /* we've done it here */
3616 break;
3617 }
3618 if (!text) {
3619 delete_Token(t);
3620 } else {
3621 *tail = t;
3622 tail = &t->next;
3623 t->type = type;
3624 nasm_free(t->text);
3625 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003626 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003627 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07003628 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003629 continue;
H. Peter Anvin67c63722008-10-26 23:49:00 -07003630 } else if (tline->type == TOK_INDIRECT) {
3631 t = tline;
3632 tline = tline->next;
3633 tt = tokenize(t->text);
3634 tt = expand_mmac_params(tt);
3635 tt = expand_smacro(tt);
3636 *tail = tt;
3637 while (tt) {
3638 tt->a.mac = NULL; /* Necessary? */
3639 tail = &tt->next;
3640 tt = tt->next;
3641 }
3642 delete_Token(t);
H. Peter Anvin6125b622009-04-08 14:02:25 -07003643 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003644 } else {
3645 t = *tail = tline;
3646 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003647 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003648 tail = &t->next;
3649 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003650 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003651 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07003652
H. Peter Anvind784a082009-04-20 14:01:18 -07003653 if (changed)
H. Peter Anvinfc30f8c2009-07-06 18:48:23 -07003654 paste_tokens(&thead, false);
H. Peter Anvin6125b622009-04-08 14:02:25 -07003655
H. Peter Anvin76690a12002-04-30 20:52:49 +00003656 return thead;
3657}
3658
3659/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003660 * Expand all single-line macro calls made in the given line.
3661 * Return the expanded version of the line. The original is deemed
3662 * to be destroyed in the process. (In reality we'll just move
3663 * Tokens from input to output a lot of the time, rather than
3664 * actually bothering to destroy and replicate.)
3665 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003666#define DEADMAN_LIMIT (1 << 20)
3667
H. Peter Anvine2c80182005-01-15 22:15:51 +00003668static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003669{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003670 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003671 struct hash_table *smtbl;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003672 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003673 Token **params;
3674 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07003675 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07003676 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003677 Token *org_tline = tline;
3678 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003679 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003680 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07003681 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003682
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003683 /*
3684 * Trick: we should avoid changing the start token pointer since it can
3685 * be contained in "next" field of other token. Because of this
3686 * we allocate a copy of first token and work with it; at the end of
3687 * routine we copy it back
3688 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003689 if (org_tline) {
3690 tline =
3691 new_Token(org_tline->next, org_tline->type, org_tline->text,
3692 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003693 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003694 nasm_free(org_tline->text);
3695 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003696 }
3697
H. Peter Anvin8287daf2009-07-07 16:00:58 -07003698 expanded = true; /* Always expand %+ at least once */
3699
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003700again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003701 tail = &thead;
3702 thead = NULL;
3703
H. Peter Anvine2c80182005-01-15 22:15:51 +00003704 while (tline) { /* main token loop */
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003705 if (!--deadman) {
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003706 error(ERR_NONFATAL, "interminable macro recursion");
3707 break;
3708 }
3709
H. Peter Anvine2c80182005-01-15 22:15:51 +00003710 if ((mname = tline->text)) {
3711 /* if this token is a local macro, look in local context */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003712 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID)
3713 ctx = get_ctx(mname, &mname, true);
3714 else
3715 ctx = NULL;
3716 smtbl = ctx ? &ctx->localmac : &smacros;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003717 head = (SMacro *) hash_findix(smtbl, mname);
H. Peter Anvin072771e2008-05-22 13:17:51 -07003718
H. Peter Anvine2c80182005-01-15 22:15:51 +00003719 /*
3720 * We've hit an identifier. As in is_mmacro below, we first
3721 * check whether the identifier is a single-line macro at
3722 * all, then think about checking for parameters if
3723 * necessary.
3724 */
H. Peter Anvin97a23472007-09-16 17:57:25 -07003725 for (m = head; m; m = m->next)
3726 if (!mstrcmp(m->name, mname, m->casesense))
3727 break;
3728 if (m) {
3729 mstart = tline;
3730 params = NULL;
3731 paramsize = NULL;
3732 if (m->nparam == 0) {
3733 /*
3734 * Simple case: the macro is parameterless. Discard the
3735 * one token that the macro call took, and push the
3736 * expansion back on the to-do stack.
3737 */
3738 if (!m->expansion) {
3739 if (!strcmp("__FILE__", m->name)) {
3740 int32_t num = 0;
H. Peter Anvin932de6c2008-07-31 18:46:11 -07003741 char *file = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003742 src_get(&num, &file);
3743 tline->text = nasm_quote(file, strlen(file));
H. Peter Anvin97a23472007-09-16 17:57:25 -07003744 tline->type = TOK_STRING;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003745 nasm_free(file);
H. Peter Anvin97a23472007-09-16 17:57:25 -07003746 continue;
3747 }
3748 if (!strcmp("__LINE__", m->name)) {
3749 nasm_free(tline->text);
3750 make_tok_num(tline, src_get_linnum());
3751 continue;
3752 }
3753 if (!strcmp("__BITS__", m->name)) {
3754 nasm_free(tline->text);
3755 make_tok_num(tline, globalbits);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003756 continue;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003757 }
3758 tline = delete_Token(tline);
3759 continue;
3760 }
3761 } else {
3762 /*
3763 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00003764 * exists and takes parameters. We must find the
3765 * parameters in the call, count them, find the SMacro
3766 * that corresponds to that form of the macro call, and
3767 * substitute for the parameters when we expand. What a
3768 * pain.
3769 */
3770 /*tline = tline->next;
3771 skip_white_(tline); */
3772 do {
3773 t = tline->next;
3774 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003775 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003776 t->text = NULL;
3777 t = tline->next = delete_Token(t);
3778 }
3779 tline = t;
3780 } while (tok_type_(tline, TOK_WHITESPACE));
3781 if (!tok_is_(tline, "(")) {
3782 /*
3783 * This macro wasn't called with parameters: ignore
3784 * the call. (Behaviour borrowed from gnu cpp.)
3785 */
3786 tline = mstart;
3787 m = NULL;
3788 } else {
3789 int paren = 0;
3790 int white = 0;
3791 brackets = 0;
3792 nparam = 0;
3793 sparam = PARAM_DELTA;
3794 params = nasm_malloc(sparam * sizeof(Token *));
3795 params[0] = tline->next;
3796 paramsize = nasm_malloc(sparam * sizeof(int));
3797 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003798 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003799 /*
3800 * For some unusual expansions
3801 * which concatenates function call
3802 */
3803 t = tline->next;
3804 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003805 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003806 t->text = NULL;
3807 t = tline->next = delete_Token(t);
3808 }
3809 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003810
H. Peter Anvine2c80182005-01-15 22:15:51 +00003811 if (!tline) {
3812 error(ERR_NONFATAL,
3813 "macro call expects terminating `)'");
3814 break;
3815 }
3816 if (tline->type == TOK_WHITESPACE
3817 && brackets <= 0) {
3818 if (paramsize[nparam])
3819 white++;
3820 else
3821 params[nparam] = tline->next;
3822 continue; /* parameter loop */
3823 }
3824 if (tline->type == TOK_OTHER
3825 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003826 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003827 if (ch == ',' && !paren && brackets <= 0) {
3828 if (++nparam >= sparam) {
3829 sparam += PARAM_DELTA;
3830 params = nasm_realloc(params,
3831 sparam *
3832 sizeof(Token
3833 *));
3834 paramsize =
3835 nasm_realloc(paramsize,
3836 sparam *
3837 sizeof(int));
3838 }
3839 params[nparam] = tline->next;
3840 paramsize[nparam] = 0;
3841 white = 0;
3842 continue; /* parameter loop */
3843 }
3844 if (ch == '{' &&
3845 (brackets > 0 || (brackets == 0 &&
3846 !paramsize[nparam])))
3847 {
3848 if (!(brackets++)) {
3849 params[nparam] = tline->next;
3850 continue; /* parameter loop */
3851 }
3852 }
3853 if (ch == '}' && brackets > 0)
3854 if (--brackets == 0) {
3855 brackets = -1;
3856 continue; /* parameter loop */
3857 }
3858 if (ch == '(' && !brackets)
3859 paren++;
3860 if (ch == ')' && brackets <= 0)
3861 if (--paren < 0)
3862 break;
3863 }
3864 if (brackets < 0) {
3865 brackets = 0;
3866 error(ERR_NONFATAL, "braces do not "
3867 "enclose all of macro parameter");
3868 }
3869 paramsize[nparam] += white + 1;
3870 white = 0;
3871 } /* parameter loop */
3872 nparam++;
3873 while (m && (m->nparam != nparam ||
3874 mstrcmp(m->name, mname,
3875 m->casesense)))
3876 m = m->next;
3877 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003878 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003879 "macro `%s' exists, "
3880 "but not taking %d parameters",
3881 mstart->text, nparam);
3882 }
3883 }
3884 if (m && m->in_progress)
3885 m = NULL;
3886 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07003887 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00003888 * Design question: should we handle !tline, which
3889 * indicates missing ')' here, or expand those
3890 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07003891 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00003892 */
3893 nasm_free(params);
3894 nasm_free(paramsize);
3895 tline = mstart;
3896 } else {
3897 /*
3898 * Expand the macro: we are placed on the last token of the
3899 * call, so that we can easily split the call from the
3900 * following tokens. We also start by pushing an SMAC_END
3901 * token for the cycle removal.
3902 */
3903 t = tline;
3904 if (t) {
3905 tline = t->next;
3906 t->next = NULL;
3907 }
3908 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003909 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003910 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003911 tline = tt;
3912 for (t = m->expansion; t; t = t->next) {
3913 if (t->type >= TOK_SMAC_PARAM) {
3914 Token *pcopy = tline, **ptail = &pcopy;
3915 Token *ttt, *pt;
3916 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003917
H. Peter Anvine2c80182005-01-15 22:15:51 +00003918 ttt = params[t->type - TOK_SMAC_PARAM];
3919 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3920 --i >= 0;) {
3921 pt = *ptail =
3922 new_Token(tline, ttt->type, ttt->text,
3923 0);
3924 ptail = &pt->next;
3925 ttt = ttt->next;
3926 }
3927 tline = pcopy;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -07003928 } else if (t->type == TOK_PREPROC_Q) {
3929 tt = new_Token(tline, TOK_ID, mname, 0);
3930 tline = tt;
3931 } else if (t->type == TOK_PREPROC_QQ) {
3932 tt = new_Token(tline, TOK_ID, m->name, 0);
3933 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003934 } else {
3935 tt = new_Token(tline, t->type, t->text, 0);
3936 tline = tt;
3937 }
3938 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003939
H. Peter Anvine2c80182005-01-15 22:15:51 +00003940 /*
3941 * Having done that, get rid of the macro call, and clean
3942 * up the parameters.
3943 */
3944 nasm_free(params);
3945 nasm_free(paramsize);
3946 free_tlist(mstart);
H. Peter Anvind784a082009-04-20 14:01:18 -07003947 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003948 continue; /* main token loop */
3949 }
3950 }
3951 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003952
H. Peter Anvine2c80182005-01-15 22:15:51 +00003953 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003954 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003955 tline = delete_Token(tline);
3956 } else {
3957 t = *tail = tline;
3958 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003959 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003960 t->next = NULL;
3961 tail = &t->next;
3962 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003963 }
3964
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003965 /*
3966 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00003967 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003968 * TOK_IDs should be concatenated.
3969 * Also we look for %+ tokens and concatenate the tokens before and after
3970 * them (without white spaces in between).
3971 */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07003972 if (expanded && paste_tokens(&thead, true)) {
H. Peter Anvinfc30f8c2009-07-06 18:48:23 -07003973 /*
3974 * If we concatenated something, *and* we had previously expanded
3975 * an actual macro, scan the lines again for macros...
3976 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003977 tline = thead;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07003978 expanded = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003979 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003980 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003981
H. Peter Anvine2c80182005-01-15 22:15:51 +00003982 if (org_tline) {
3983 if (thead) {
3984 *org_tline = *thead;
3985 /* since we just gave text to org_line, don't free it */
3986 thead->text = NULL;
3987 delete_Token(thead);
3988 } else {
3989 /* the expression expanded to empty line;
3990 we can't return NULL for some reasons
3991 we just set the line to a single WHITESPACE token. */
3992 memset(org_tline, 0, sizeof(*org_tline));
3993 org_tline->text = NULL;
3994 org_tline->type = TOK_WHITESPACE;
3995 }
3996 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003997 }
3998
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003999 return thead;
4000}
4001
4002/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004003 * Similar to expand_smacro but used exclusively with macro identifiers
4004 * right before they are fetched in. The reason is that there can be
4005 * identifiers consisting of several subparts. We consider that if there
4006 * are more than one element forming the name, user wants a expansion,
4007 * otherwise it will be left as-is. Example:
4008 *
4009 * %define %$abc cde
4010 *
4011 * the identifier %$abc will be left as-is so that the handler for %define
4012 * will suck it and define the corresponding value. Other case:
4013 *
4014 * %define _%$abc cde
4015 *
4016 * In this case user wants name to be expanded *before* %define starts
4017 * working, so we'll expand %$abc into something (if it has a value;
4018 * otherwise it will be left as-is) then concatenate all successive
4019 * PP_IDs into one.
4020 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004021static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004022{
4023 Token *cur, *oldnext = NULL;
4024
H. Peter Anvin734b1882002-04-30 21:01:08 +00004025 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004026 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004027
4028 cur = tline;
4029 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00004030 (cur->next->type == TOK_ID ||
4031 cur->next->type == TOK_PREPROC_ID
4032 || cur->next->type == TOK_NUMBER))
4033 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004034
4035 /* If identifier consists of just one token, don't expand */
4036 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004037 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004038
H. Peter Anvine2c80182005-01-15 22:15:51 +00004039 if (cur) {
4040 oldnext = cur->next; /* Detach the tail past identifier */
4041 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004042 }
4043
H. Peter Anvin734b1882002-04-30 21:01:08 +00004044 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004045
H. Peter Anvine2c80182005-01-15 22:15:51 +00004046 if (cur) {
4047 /* expand_smacro possibly changhed tline; re-scan for EOL */
4048 cur = tline;
4049 while (cur && cur->next)
4050 cur = cur->next;
4051 if (cur)
4052 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004053 }
4054
4055 return tline;
4056}
4057
4058/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004059 * Determine whether the given line constitutes a multi-line macro
4060 * call, and return the MMacro structure called if so. Doesn't have
4061 * to check for an initial label - that's taken care of in
4062 * expand_mmacro - but must check numbers of parameters. Guaranteed
4063 * to be called with tline->type == TOK_ID, so the putative macro
4064 * name is easy to find.
4065 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004066static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004067{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004068 MMacro *head, *m;
4069 Token **params;
4070 int nparam;
4071
H. Peter Anvin166c2472008-05-28 12:28:58 -07004072 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004073
4074 /*
4075 * Efficiency: first we see if any macro exists with the given
4076 * name. If not, we can return NULL immediately. _Then_ we
4077 * count the parameters, and then we look further along the
4078 * list if necessary to find the proper MMacro.
4079 */
4080 for (m = head; m; m = m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004081 if (!mstrcmp(m->name, tline->text, m->casesense))
4082 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004083 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004084 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004085
4086 /*
4087 * OK, we have a potential macro. Count and demarcate the
4088 * parameters.
4089 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004090 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004091
4092 /*
4093 * So we know how many parameters we've got. Find the MMacro
4094 * structure that handles this number.
4095 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004096 while (m) {
4097 if (m->nparam_min <= nparam
4098 && (m->plus || nparam <= m->nparam_max)) {
4099 /*
4100 * This one is right. Just check if cycle removal
4101 * prohibits us using it before we actually celebrate...
4102 */
Keith Kanios891775e2009-07-11 06:08:54 -05004103 if (m->in_progress > m->max_depth) {
4104 if (m->max_depth > 0) {
4105 error(ERR_WARNING,
4106 "reached maximum recursion depth of %i",
4107 m->max_depth);
4108 }
4109 nasm_free(params);
4110 return NULL;
4111 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004112 /*
4113 * It's right, and we can use it. Add its default
4114 * parameters to the end of our list if necessary.
4115 */
4116 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
4117 params =
4118 nasm_realloc(params,
4119 ((m->nparam_min + m->ndefs +
4120 1) * sizeof(*params)));
4121 while (nparam < m->nparam_min + m->ndefs) {
4122 params[nparam] = m->defaults[nparam - m->nparam_min];
4123 nparam++;
4124 }
4125 }
4126 /*
4127 * If we've gone over the maximum parameter count (and
4128 * we're in Plus mode), ignore parameters beyond
4129 * nparam_max.
4130 */
4131 if (m->plus && nparam > m->nparam_max)
4132 nparam = m->nparam_max;
4133 /*
4134 * Then terminate the parameter list, and leave.
4135 */
4136 if (!params) { /* need this special case */
4137 params = nasm_malloc(sizeof(*params));
4138 nparam = 0;
4139 }
4140 params[nparam] = NULL;
4141 *params_array = params;
4142 return m;
4143 }
4144 /*
4145 * This one wasn't right: look for the next one with the
4146 * same name.
4147 */
4148 for (m = m->next; m; m = m->next)
4149 if (!mstrcmp(m->name, tline->text, m->casesense))
4150 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004151 }
4152
4153 /*
4154 * After all that, we didn't find one with the right number of
4155 * parameters. Issue a warning, and fail to expand the macro.
4156 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004157 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004158 "macro `%s' exists, but not taking %d parameters",
4159 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004160 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004161 return NULL;
4162}
4163
Keith Kanios891775e2009-07-11 06:08:54 -05004164
4165/*
4166 * Save MMacro invocation specific fields in
4167 * preparation for a recursive macro expansion
4168 */
4169static void push_mmacro(MMacro *m)
4170{
4171 MMacroInvocation *i;
4172
4173 i = nasm_malloc(sizeof(MMacroInvocation));
4174 i->prev = m->prev;
4175 i->params = m->params;
4176 i->iline = m->iline;
4177 i->nparam = m->nparam;
4178 i->rotate = m->rotate;
4179 i->paramlen = m->paramlen;
4180 i->unique = m->unique;
4181 m->prev = i;
4182}
4183
4184
4185/*
4186 * Restore MMacro invocation specific fields that were
4187 * saved during a previous recursive macro expansion
4188 */
4189static void pop_mmacro(MMacro *m)
4190{
4191 MMacroInvocation *i;
4192
4193 if(m->prev != NULL){
4194 i = m->prev;
4195 m->prev = i->prev;
4196 m->params = i->params;
4197 m->iline = i->iline;
4198 m->nparam = i->nparam;
4199 m->rotate = i->rotate;
4200 m->paramlen = i->paramlen;
4201 m->unique = i->unique;
4202 nasm_free(i);
4203 }
4204}
4205
4206
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004207/*
4208 * Expand the multi-line macro call made by the given line, if
4209 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00004210 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004211 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004212static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004213{
4214 Token *startline = tline;
4215 Token *label = NULL;
4216 int dont_prepend = 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004217 Token **params, *t, *mtok, *tt;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004218 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004219 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004220 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004221 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004222
4223 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004224 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004225 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004226 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004227 return 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004228 mtok = t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004229 m = is_mmacro(t, &params);
H. Peter Anvinc751e862008-06-09 10:18:45 -07004230 if (m) {
4231 mname = t->text;
4232 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004233 Token *last;
4234 /*
4235 * We have an id which isn't a macro call. We'll assume
4236 * it might be a label; we'll also check to see if a
4237 * colon follows it. Then, if there's another id after
4238 * that lot, we'll check it again for macro-hood.
4239 */
4240 label = last = t;
4241 t = t->next;
4242 if (tok_type_(t, TOK_WHITESPACE))
4243 last = t, t = t->next;
4244 if (tok_is_(t, ":")) {
4245 dont_prepend = 1;
4246 last = t, t = t->next;
4247 if (tok_type_(t, TOK_WHITESPACE))
4248 last = t, t = t->next;
4249 }
4250 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
4251 return 0;
4252 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004253 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004254 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004255 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004256
4257 /*
4258 * Fix up the parameters: this involves stripping leading and
4259 * trailing whitespace, then stripping braces if they are
4260 * present.
4261 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004262 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004263 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004264
H. Peter Anvine2c80182005-01-15 22:15:51 +00004265 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004266 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004267 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004268
H. Peter Anvine2c80182005-01-15 22:15:51 +00004269 t = params[i];
4270 skip_white_(t);
4271 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004272 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004273 params[i] = t;
4274 paramlen[i] = 0;
4275 while (t) {
4276 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4277 break; /* ... because we have hit a comma */
4278 if (comma && t->type == TOK_WHITESPACE
4279 && tok_is_(t->next, ","))
4280 break; /* ... or a space then a comma */
4281 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4282 break; /* ... or a brace */
4283 t = t->next;
4284 paramlen[i]++;
4285 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004286 }
4287
4288 /*
4289 * OK, we have a MMacro structure together with a set of
4290 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00004291 * copies of each Line on to istk->expansion. Substitution of
4292 * parameter tokens and macro-local tokens doesn't get done
4293 * until the single-line macro substitution process; this is
4294 * because delaying them allows us to change the semantics
4295 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004296 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00004297 * First, push an end marker on to istk->expansion, mark this
4298 * macro as in progress, and set up its invocation-specific
4299 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004300 */
4301 ll = nasm_malloc(sizeof(Line));
4302 ll->next = istk->expansion;
4303 ll->finishes = m;
4304 ll->first = NULL;
4305 istk->expansion = ll;
Keith Kanios891775e2009-07-11 06:08:54 -05004306
4307 /*
4308 * Save the previous MMacro expansion in the case of
4309 * macro recursion
4310 */
4311 if (m->max_depth && m->in_progress)
4312 push_mmacro(m);
H. Peter Anvin76690a12002-04-30 20:52:49 +00004313
Keith Kanios891775e2009-07-11 06:08:54 -05004314 m->in_progress ++;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004315 m->params = params;
4316 m->iline = tline;
4317 m->nparam = nparam;
4318 m->rotate = 0;
4319 m->paramlen = paramlen;
4320 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004321 m->lineno = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004322
4323 m->next_active = istk->mstk;
4324 istk->mstk = m;
4325
H. Peter Anvine2c80182005-01-15 22:15:51 +00004326 for (l = m->expansion; l; l = l->next) {
4327 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004328
H. Peter Anvine2c80182005-01-15 22:15:51 +00004329 ll = nasm_malloc(sizeof(Line));
4330 ll->finishes = NULL;
4331 ll->next = istk->expansion;
4332 istk->expansion = ll;
4333 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004334
H. Peter Anvine2c80182005-01-15 22:15:51 +00004335 for (t = l->first; t; t = t->next) {
4336 Token *x = t;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004337 switch (t->type) {
4338 case TOK_PREPROC_Q:
H. Peter Anvinc751e862008-06-09 10:18:45 -07004339 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004340 break;
4341 case TOK_PREPROC_QQ:
4342 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4343 break;
4344 case TOK_PREPROC_ID:
4345 if (t->text[1] == '0' && t->text[2] == '0') {
4346 dont_prepend = -1;
4347 x = label;
4348 if (!x)
4349 continue;
4350 }
4351 /* fall through */
4352 default:
4353 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4354 break;
H. Peter Anvin166c2472008-05-28 12:28:58 -07004355 }
H. Peter Anvince2233b2008-05-25 21:57:00 -07004356 tail = &tt->next;
4357 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004358 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004359 }
4360
4361 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004362 * If we had a label, push it on as the first line of
4363 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004364 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004365 if (label) {
4366 if (dont_prepend < 0)
4367 free_tlist(startline);
4368 else {
4369 ll = nasm_malloc(sizeof(Line));
4370 ll->finishes = NULL;
4371 ll->next = istk->expansion;
4372 istk->expansion = ll;
4373 ll->first = startline;
4374 if (!dont_prepend) {
4375 while (label->next)
4376 label = label->next;
4377 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4378 }
4379 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004380 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004381
H. Peter Anvin734b1882002-04-30 21:01:08 +00004382 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004383
H. Peter Anvineba20a72002-04-30 20:53:55 +00004384 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004385}
4386
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004387/* The function that actually does the error reporting */
4388static void verror(int severity, const char *fmt, va_list arg)
4389{
4390 char buff[1024];
4391
4392 vsnprintf(buff, sizeof(buff), fmt, arg);
4393
4394 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004395 _error(severity, "(%s:%d) %s", istk->mstk->name,
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004396 istk->mstk->lineno, buff);
4397 else
H. Peter Anvin917a3492008-09-24 09:14:49 -07004398 _error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004399}
4400
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004401/*
4402 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07004403 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004404 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004405static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004406{
4407 va_list arg;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004408
4409 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00004410 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004411 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004412
H. Peter Anvin734b1882002-04-30 21:01:08 +00004413 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004414 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004415 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004416}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004417
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004418/*
4419 * Because %else etc are evaluated in the state context
4420 * of the previous branch, errors might get lost with error():
4421 * %if 0 ... %else trailing garbage ... %endif
4422 * So %else etc should report errors with this function.
4423 */
4424static void error_precond(int severity, const char *fmt, ...)
4425{
4426 va_list arg;
4427
4428 /* Only ignore the error if it's really in a dead branch */
4429 if (istk && istk->conds && istk->conds->state == COND_NEVER)
4430 return;
4431
4432 va_start(arg, fmt);
4433 verror(severity, fmt, arg);
4434 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004435}
4436
H. Peter Anvin734b1882002-04-30 21:01:08 +00004437static void
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004438pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004439 ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004440{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004441 Token *t;
4442
H. Peter Anvin99941bf2002-05-14 17:44:03 +00004443 _error = errfunc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004444 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004445 istk = nasm_malloc(sizeof(Include));
4446 istk->next = NULL;
4447 istk->conds = NULL;
4448 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004449 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004450 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00004451 istk->fname = NULL;
4452 src_set_fname(nasm_strdup(file));
4453 src_set_linnum(0);
4454 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004455 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004456 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00004457 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004458 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004459 nested_mac_count = 0;
4460 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004461 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004462 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004463 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004464 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004465 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004466 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004467 }
H. Peter Anvind2456592008-06-19 15:04:18 -07004468 any_extrastdmac = extrastdmac && *extrastdmac;
4469 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004470 list = listgen;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004471 evaluate = eval;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004472
4473 /*
4474 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4475 * The caller, however, will also pass in 3 for preprocess-only so
4476 * we can set __PASS__ accordingly.
4477 */
4478 pass = apass > 2 ? 2 : apass;
4479
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004480 dephead = deptail = deplist;
4481 if (deplist) {
4482 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4483 sl->next = NULL;
4484 strcpy(sl->str, file);
4485 *deptail = sl;
4486 deptail = &sl->next;
4487 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07004488
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004489 /*
4490 * Define the __PASS__ macro. This is defined here unlike
4491 * all the other builtins, because it is special -- it varies between
4492 * passes.
4493 */
H. Peter Anvin7383b402008-09-24 10:20:40 -07004494 t = nasm_malloc(sizeof(*t));
4495 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004496 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07004497 t->a.mac = NULL;
4498 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004499}
4500
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004501static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004502{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004503 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004504 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004505
H. Peter Anvine2c80182005-01-15 22:15:51 +00004506 while (1) {
4507 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004508 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004509 * buffer or from the input file.
4510 */
4511 tline = NULL;
4512 while (istk->expansion && istk->expansion->finishes) {
4513 Line *l = istk->expansion;
4514 if (!l->finishes->name && l->finishes->in_progress > 1) {
4515 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004516
H. Peter Anvine2c80182005-01-15 22:15:51 +00004517 /*
4518 * This is a macro-end marker for a macro with no
4519 * name, which means it's not really a macro at all
4520 * but a %rep block, and the `in_progress' field is
4521 * more than 1, meaning that we still need to
4522 * repeat. (1 means the natural last repetition; 0
4523 * means termination by %exitrep.) We have
4524 * therefore expanded up to the %endrep, and must
4525 * push the whole block on to the expansion buffer
4526 * again. We don't bother to remove the macro-end
4527 * marker: we'd only have to generate another one
4528 * if we did.
4529 */
4530 l->finishes->in_progress--;
4531 for (l = l->finishes->expansion; l; l = l->next) {
4532 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004533
H. Peter Anvine2c80182005-01-15 22:15:51 +00004534 ll = nasm_malloc(sizeof(Line));
4535 ll->next = istk->expansion;
4536 ll->finishes = NULL;
4537 ll->first = NULL;
4538 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004539
H. Peter Anvine2c80182005-01-15 22:15:51 +00004540 for (t = l->first; t; t = t->next) {
4541 if (t->text || t->type == TOK_WHITESPACE) {
4542 tt = *tail =
4543 new_Token(NULL, t->type, t->text, 0);
4544 tail = &tt->next;
4545 }
4546 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004547
H. Peter Anvine2c80182005-01-15 22:15:51 +00004548 istk->expansion = ll;
4549 }
4550 } else {
4551 /*
4552 * Check whether a `%rep' was started and not ended
4553 * within this macro expansion. This can happen and
4554 * should be detected. It's a fatal error because
4555 * I'm too confused to work out how to recover
4556 * sensibly from it.
4557 */
4558 if (defining) {
4559 if (defining->name)
4560 error(ERR_PANIC,
4561 "defining with name in expansion");
4562 else if (istk->mstk->name)
4563 error(ERR_FATAL,
4564 "`%%rep' without `%%endrep' within"
4565 " expansion of macro `%s'",
4566 istk->mstk->name);
4567 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00004568
H. Peter Anvine2c80182005-01-15 22:15:51 +00004569 /*
4570 * FIXME: investigate the relationship at this point between
4571 * istk->mstk and l->finishes
4572 */
4573 {
4574 MMacro *m = istk->mstk;
4575 istk->mstk = m->next_active;
4576 if (m->name) {
4577 /*
4578 * This was a real macro call, not a %rep, and
4579 * therefore the parameter information needs to
4580 * be freed.
4581 */
Keith Kanios891775e2009-07-11 06:08:54 -05004582 if (m->prev != NULL) {
4583 pop_mmacro(m);
4584 } else {
4585 nasm_free(m->params);
4586 free_tlist(m->iline);
4587 nasm_free(m->paramlen);
4588 }
4589 l->finishes->in_progress --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004590 } else
4591 free_mmacro(m);
4592 }
4593 istk->expansion = l->next;
4594 nasm_free(l);
4595 list->downlevel(LIST_MACRO);
4596 }
4597 }
4598 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004599
H. Peter Anvine2c80182005-01-15 22:15:51 +00004600 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004601 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004602 Line *l = istk->expansion;
4603 if (istk->mstk)
4604 istk->mstk->lineno++;
4605 tline = l->first;
4606 istk->expansion = l->next;
4607 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004608 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004609 list->line(LIST_MACRO, p);
4610 nasm_free(p);
4611 break;
4612 }
4613 line = read_line();
4614 if (line) { /* from the current input file */
4615 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004616 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004617 nasm_free(line);
4618 break;
4619 }
4620 /*
4621 * The current file has ended; work down the istk
4622 */
4623 {
4624 Include *i = istk;
4625 fclose(i->fp);
4626 if (i->conds)
4627 error(ERR_FATAL,
4628 "expected `%%endif' before end of file");
4629 /* only set line and file name if there's a next node */
4630 if (i->next) {
4631 src_set_linnum(i->lineno);
4632 nasm_free(src_set_fname(i->fname));
H. Peter Anvin86877b22008-06-20 15:55:45 -07004633 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004634 istk = i->next;
4635 list->downlevel(LIST_INCLUDE);
4636 nasm_free(i);
4637 if (!istk)
4638 return NULL;
Victor van den Elzen4c9d6222008-10-01 13:08:50 +02004639 if (istk->expansion && istk->expansion->finishes)
4640 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004641 }
4642 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004643
H. Peter Anvine2c80182005-01-15 22:15:51 +00004644 /*
4645 * We must expand MMacro parameters and MMacro-local labels
4646 * _before_ we plunge into directive processing, to cope
4647 * with things like `%define something %1' such as STRUC
4648 * uses. Unless we're _defining_ a MMacro, in which case
4649 * those tokens should be left alone to go into the
4650 * definition; and unless we're in a non-emitting
4651 * condition, in which case we don't want to meddle with
4652 * anything.
4653 */
Charles Crayned4200be2008-07-12 16:42:33 -07004654 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin992fe752008-10-19 15:45:05 -07004655 && !(istk->mstk && !istk->mstk->in_progress)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004656 tline = expand_mmac_params(tline);
H. Peter Anvin992fe752008-10-19 15:45:05 -07004657 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004658
H. Peter Anvine2c80182005-01-15 22:15:51 +00004659 /*
4660 * Check the line to see if it's a preprocessor directive.
4661 */
4662 if (do_directive(tline) == DIRECTIVE_FOUND) {
4663 continue;
4664 } else if (defining) {
4665 /*
4666 * We're defining a multi-line macro. We emit nothing
4667 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00004668 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004669 */
4670 Line *l = nasm_malloc(sizeof(Line));
4671 l->next = defining->expansion;
4672 l->first = tline;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004673 l->finishes = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004674 defining->expansion = l;
4675 continue;
4676 } else if (istk->conds && !emitting(istk->conds->state)) {
4677 /*
4678 * We're in a non-emitting branch of a condition block.
4679 * Emit nothing at all, not even a blank line: when we
4680 * emerge from the condition we'll give a line-number
4681 * directive so we keep our place correctly.
4682 */
4683 free_tlist(tline);
4684 continue;
4685 } else if (istk->mstk && !istk->mstk->in_progress) {
4686 /*
4687 * We're in a %rep block which has been terminated, so
4688 * we're walking through to the %endrep without
4689 * emitting anything. Emit nothing at all, not even a
4690 * blank line: when we emerge from the %rep block we'll
4691 * give a line-number directive so we keep our place
4692 * correctly.
4693 */
4694 free_tlist(tline);
4695 continue;
4696 } else {
4697 tline = expand_smacro(tline);
4698 if (!expand_mmacro(tline)) {
4699 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004700 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004701 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004702 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004703 free_tlist(tline);
4704 break;
4705 } else {
4706 continue; /* expand_mmacro calls free_tlist */
4707 }
4708 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004709 }
4710
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004711 return line;
4712}
4713
H. Peter Anvine2c80182005-01-15 22:15:51 +00004714static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004715{
H. Peter Anvine2c80182005-01-15 22:15:51 +00004716 if (defining) {
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02004717 if(defining->name) {
4718 error(ERR_NONFATAL,
4719 "end of file while still defining macro `%s'",
4720 defining->name);
4721 } else {
4722 error(ERR_NONFATAL, "end of file while still in %%rep");
4723 }
4724
H. Peter Anvine2c80182005-01-15 22:15:51 +00004725 free_mmacro(defining);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004726 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004727 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004728 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07004729 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00004730 while (istk) {
4731 Include *i = istk;
4732 istk = istk->next;
4733 fclose(i->fp);
4734 nasm_free(i->fname);
4735 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004736 }
4737 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004738 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07004739 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004740 if (pass == 0) {
H. Peter Anvin86877b22008-06-20 15:55:45 -07004741 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004742 free_llist(predef);
4743 delete_Blocks();
H. Peter Anvin86877b22008-06-20 15:55:45 -07004744 while ((i = ipath)) {
4745 ipath = i->next;
4746 if (i->path)
4747 nasm_free(i->path);
4748 nasm_free(i);
4749 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07004750 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004751}
4752
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004753void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004754{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004755 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004756
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004757 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004758 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004759 i->next = NULL;
4760
H. Peter Anvine2c80182005-01-15 22:15:51 +00004761 if (ipath != NULL) {
4762 IncPath *j = ipath;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004763 while (j->next != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004764 j = j->next;
4765 j->next = i;
4766 } else {
4767 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004768 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004769}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004770
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004771void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004772{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004773 Token *inc, *space, *name;
4774 Line *l;
4775
H. Peter Anvin734b1882002-04-30 21:01:08 +00004776 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4777 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4778 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004779
4780 l = nasm_malloc(sizeof(Line));
4781 l->next = predef;
4782 l->first = inc;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004783 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004784 predef = l;
4785}
4786
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004787void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004788{
4789 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004790 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004791 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004792
4793 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00004794 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4795 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004796 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004797 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00004798 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004799 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004800 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004801
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004802 l = nasm_malloc(sizeof(Line));
4803 l->next = predef;
4804 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004805 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004806 predef = l;
4807}
4808
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004809void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00004810{
4811 Token *def, *space;
4812 Line *l;
4813
H. Peter Anvin734b1882002-04-30 21:01:08 +00004814 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4815 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004816 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00004817
4818 l = nasm_malloc(sizeof(Line));
4819 l->next = predef;
4820 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004821 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00004822 predef = l;
4823}
4824
Keith Kaniosb7a89542007-04-12 02:40:54 +00004825/*
4826 * Added by Keith Kanios:
4827 *
4828 * This function is used to assist with "runtime" preprocessor
4829 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4830 *
4831 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4832 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4833 */
4834
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004835void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00004836{
4837 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07004838
Keith Kaniosb7a89542007-04-12 02:40:54 +00004839 def = tokenize(definition);
4840 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4841 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07004842
Keith Kaniosb7a89542007-04-12 02:40:54 +00004843}
4844
H. Peter Anvina70547f2008-07-19 21:44:26 -07004845void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004846{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004847 extrastdmac = macros;
4848}
4849
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004850static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004851{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004852 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004853 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004854 tok->text = nasm_strdup(numbuf);
4855 tok->type = TOK_NUMBER;
4856}
4857
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004858Preproc nasmpp = {
4859 pp_reset,
4860 pp_getline,
4861 pp_cleanup
4862};