blob: bac5a77a3dbf1b0222263b1a7e86669cfe65d5a3 [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;
H. Peter Anvin89cee572009-07-15 09:16:54 -0400134 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? */
H. Peter Anvin89cee572009-07-15 09:16:54 -0400141 int32_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 {
H. Peter Anvin89cee572009-07-15 09:16:54 -0400162 MMacroInvocation *prev; /* previous invocation */
163 Token **params; /* actual parameters */
164 Token *iline; /* invocation line */
Keith Kanios891775e2009-07-11 06:08:54 -0500165 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/*
Keith Kanios852f1ee2009-07-12 00:19:55 -0500321 * This define sets the upper limit for smacro and recursive mmacro
322 * expansions
323 */
324#define DEADMAN_LIMIT (1 << 20)
325
326/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000327 * Condition codes. Note that we use c_ prefix not C_ because C_ is
328 * used in nasm.h for the "real" condition codes. At _this_ level,
329 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
330 * ones, so we need a different enum...
331 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700332static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000333 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
334 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000335 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000336};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700337enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000338 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
339 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 -0700340 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
341 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000342};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700343static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000344 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
345 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 +0000346 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000347};
348
H. Peter Anvin76690a12002-04-30 20:52:49 +0000349/*
350 * Directive names.
351 */
H. Peter Anvin65747262002-05-07 00:10:05 +0000352/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
H. Peter Anvin9bf0aa72007-09-12 02:12:07 +0000353static int is_condition(enum preproc_token arg)
H. Peter Anvin65747262002-05-07 00:10:05 +0000354{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000355 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
H. Peter Anvin65747262002-05-07 00:10:05 +0000356}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000357
358/* For TASM compatibility we need to be able to recognise TASM compatible
359 * conditional compilation directives. Using the NASM pre-processor does
360 * not work, so we look for them specifically from the following list and
361 * then jam in the equivalent NASM directive into the input stream.
362 */
363
H. Peter Anvine2c80182005-01-15 22:15:51 +0000364enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000365 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
366 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
367};
368
H. Peter Anvin476d2862007-10-02 22:04:15 -0700369static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000370 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
371 "ifndef", "include", "local"
372};
373
374static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000375static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000376static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800377static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000378
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000379static Context *cstk;
380static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000381static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000382
H. Peter Anvine2c80182005-01-15 22:15:51 +0000383static efunc _error; /* Pointer to client-provided error reporting function */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000384static evalfunc evaluate;
385
H. Peter Anvine2c80182005-01-15 22:15:51 +0000386static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700387static StrList **dephead, **deptail; /* Dependency list */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000388
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -0700389static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000390
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000391static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700392static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000393
394static ListGen *list;
395
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000396/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000397 * The current set of multi-line macros we have defined.
398 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700399static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000400
401/*
402 * The current set of single-line macros we have defined.
403 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700404static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000405
406/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000407 * The multi-line macro we are currently defining, or the %rep
408 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000409 */
410static MMacro *defining;
411
Charles Crayned4200be2008-07-12 16:42:33 -0700412static uint64_t nested_mac_count;
413static uint64_t nested_rep_count;
414
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000415/*
416 * The number of macro parameters to allocate space for at a time.
417 */
418#define PARAM_DELTA 16
419
420/*
H. Peter Anvina4835d42008-05-20 14:21:29 -0700421 * The standard macro set: defined in macros.c in the array nasm_stdmac.
422 * This gives our position in the macro set, when we're processing it.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000423 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700424static macros_t *stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000425
426/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000427 * The extra standard macros that come from the object format, if
428 * any.
429 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700430static macros_t *extrastdmac = NULL;
H. Peter Anvincfb71762008-06-20 15:20:16 -0700431static bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000432
433/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000434 * Tokens are allocated in blocks to improve speed
435 */
436#define TOKEN_BLOCKSIZE 4096
437static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000438struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000439 Blocks *next;
440 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000441};
442
443static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000444
445/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000446 * Forward declarations.
447 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000448static Token *expand_mmac_params(Token * tline);
449static Token *expand_smacro(Token * tline);
450static Token *expand_id(Token * tline);
H. Peter Anvinf8ad5322009-02-21 17:55:08 -0800451static Context *get_ctx(const char *name, const char **namep,
452 bool all_contexts);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700453static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000454static void error(int severity, const char *fmt, ...);
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200455static void error_precond(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000456static void *new_Block(size_t size);
457static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700458static Token *new_Token(Token * next, enum pp_token_type type,
459 const char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000460static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000461
462/*
463 * Macros for safe checking of token pointers, avoid *(NULL)
464 */
465#define tok_type_(x,t) ((x) && (x)->type == (t))
466#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
467#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
468#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000469
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000470/* Handle TASM specific directives, which do not contain a % in
471 * front of them. We do it here because I could not find any other
472 * place to do it for the moment, and it is a hack (ideally it would
473 * be nice to be able to use the NASM pre-processor to do it).
474 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000475static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000476{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000477 int32_t i, j, k, m, len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000478 char *p = line, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000479
480 /* Skip whitespace */
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700481 while (nasm_isspace(*p) && *p != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000482 p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000483
484 /* Binary search for the directive name */
485 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +0000486 j = elements(tasm_directives);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000487 len = 0;
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700488 while (!nasm_isspace(p[len]) && p[len] != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000489 len++;
490 if (len) {
491 oldchar = p[len];
492 p[len] = 0;
493 while (j - i > 1) {
494 k = (j + i) / 2;
495 m = nasm_stricmp(p, tasm_directives[k]);
496 if (m == 0) {
497 /* We have found a directive, so jam a % in front of it
498 * so that NASM will then recognise it as one if it's own.
499 */
500 p[len] = oldchar;
501 len = strlen(p);
502 oldline = line;
503 line = nasm_malloc(len + 2);
504 line[0] = '%';
505 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700506 /*
507 * NASM does not recognise IFDIFI, so we convert
508 * it to %if 0. This is not used in NASM
509 * compatible code, but does need to parse for the
510 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000511 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700512 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000513 } else {
514 memcpy(line + 1, p, len + 1);
515 }
516 nasm_free(oldline);
517 return line;
518 } else if (m < 0) {
519 j = k;
520 } else
521 i = k;
522 }
523 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000524 }
525 return line;
526}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000527
H. Peter Anvin76690a12002-04-30 20:52:49 +0000528/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000529 * The pre-preprocessing stage... This function translates line
530 * number indications as they emerge from GNU cpp (`# lineno "file"
531 * flags') into NASM preprocessor line number indications (`%line
532 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000533 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000534static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000535{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000536 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000537 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000538
H. Peter Anvine2c80182005-01-15 22:15:51 +0000539 if (line[0] == '#' && line[1] == ' ') {
540 oldline = line;
541 fname = oldline + 2;
542 lineno = atoi(fname);
543 fname += strspn(fname, "0123456789 ");
544 if (*fname == '"')
545 fname++;
546 fnlen = strcspn(fname, "\"");
547 line = nasm_malloc(20 + fnlen);
548 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
549 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000550 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000551 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000552 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000553 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000554}
555
556/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000557 * Free a linked list of tokens.
558 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000559static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000560{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000561 while (list) {
562 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000563 }
564}
565
566/*
567 * Free a linked list of lines.
568 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000569static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000570{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000571 Line *l;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000572 while (list) {
573 l = list;
574 list = list->next;
575 free_tlist(l->first);
576 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000577 }
578}
579
580/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000581 * Free an MMacro
582 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000583static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000584{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000585 nasm_free(m->name);
586 free_tlist(m->dlist);
587 nasm_free(m->defaults);
588 free_llist(m->expansion);
589 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000590}
591
592/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700593 * Free all currently defined macros, and free the hash tables
594 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700595static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700596{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700597 SMacro *s;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700598 const char *key;
599 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700600
H. Peter Anvin072771e2008-05-22 13:17:51 -0700601 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
H. Peter Anvin97a23472007-09-16 17:57:25 -0700602 nasm_free((void *)key);
603 while (s) {
604 SMacro *ns = s->next;
605 nasm_free(s->name);
606 free_tlist(s->expansion);
607 nasm_free(s);
608 s = ns;
609 }
610 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700611 hash_free(smt);
612}
613
614static void free_mmacro_table(struct hash_table *mmt)
615{
616 MMacro *m;
617 const char *key;
618 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700619
620 it = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700621 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
H. Peter Anvin97a23472007-09-16 17:57:25 -0700622 nasm_free((void *)key);
623 while (m) {
624 MMacro *nm = m->next;
625 free_mmacro(m);
626 m = nm;
627 }
628 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700629 hash_free(mmt);
630}
631
632static void free_macros(void)
633{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700634 free_smacro_table(&smacros);
635 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700636}
637
638/*
639 * Initialize the hash tables
640 */
641static void init_macros(void)
642{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700643 hash_init(&smacros, HASH_LARGE);
644 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700645}
646
647/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000648 * Pop the context stack.
649 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000650static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000651{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000652 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000653
654 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700655 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000656 nasm_free(c->name);
657 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000658}
659
H. Peter Anvin072771e2008-05-22 13:17:51 -0700660/*
661 * Search for a key in the hash index; adding it if necessary
662 * (in which case we initialize the data pointer to NULL.)
663 */
664static void **
665hash_findi_add(struct hash_table *hash, const char *str)
666{
667 struct hash_insert hi;
668 void **r;
669 char *strx;
670
671 r = hash_findi(hash, str, &hi);
672 if (r)
673 return r;
674
675 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
676 return hash_add(&hi, strx, NULL);
677}
678
679/*
680 * Like hash_findi, but returns the data element rather than a pointer
681 * to it. Used only when not adding a new element, hence no third
682 * argument.
683 */
684static void *
685hash_findix(struct hash_table *hash, const char *str)
686{
687 void **p;
688
689 p = hash_findi(hash, str, NULL);
690 return p ? *p : NULL;
691}
692
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000693#define BUF_DELTA 512
694/*
695 * Read a line from the top file in istk, handling multiple CR/LFs
696 * at the end of the line read, and handling spurious ^Zs. Will
697 * return lines from the standard macro set if this has not already
698 * been done.
699 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000700static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000701{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000702 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000703 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000704
H. Peter Anvine2c80182005-01-15 22:15:51 +0000705 if (stdmacpos) {
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700706 unsigned char c;
H. Peter Anvin7e50d232008-06-25 14:54:14 -0700707 const unsigned char *p = stdmacpos;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700708 char *ret, *q;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700709 size_t len = 0;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700710 while ((c = *p++)) {
711 if (c >= 0x80)
712 len += pp_directives_len[c-0x80]+1;
713 else
714 len++;
715 }
716 ret = nasm_malloc(len+1);
H. Peter Anvincda81632008-06-21 15:15:40 -0700717 q = ret;
718 while ((c = *stdmacpos++)) {
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700719 if (c >= 0x80) {
720 memcpy(q, pp_directives[c-0x80], pp_directives_len[c-0x80]);
721 q += pp_directives_len[c-0x80];
722 *q++ = ' ';
723 } else {
724 *q++ = c;
725 }
726 }
H. Peter Anvincda81632008-06-21 15:15:40 -0700727 stdmacpos = p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700728 *q = '\0';
729
H. Peter Anvind2456592008-06-19 15:04:18 -0700730 if (!*stdmacpos) {
731 /* This was the last of the standard macro chain... */
732 stdmacpos = NULL;
733 if (any_extrastdmac) {
734 stdmacpos = extrastdmac;
735 any_extrastdmac = false;
736 } else if (do_predef) {
737 Line *pd, *l;
738 Token *head, **tail, *t;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000739
H. Peter Anvind2456592008-06-19 15:04:18 -0700740 /*
741 * Nasty hack: here we push the contents of
742 * `predef' on to the top-level expansion stack,
743 * since this is the most convenient way to
744 * implement the pre-include and pre-define
745 * features.
746 */
747 for (pd = predef; pd; pd = pd->next) {
748 head = NULL;
749 tail = &head;
750 for (t = pd->first; t; t = t->next) {
751 *tail = new_Token(NULL, t->type, t->text, 0);
752 tail = &(*tail)->next;
753 }
754 l = nasm_malloc(sizeof(Line));
755 l->next = istk->expansion;
756 l->first = head;
H. Peter Anvin538002d2008-06-28 18:30:27 -0700757 l->finishes = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700758 istk->expansion = l;
759 }
760 do_predef = false;
761 }
762 }
763 return ret;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000764 }
765
766 bufsize = BUF_DELTA;
767 buffer = nasm_malloc(BUF_DELTA);
768 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000769 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000770 while (1) {
771 q = fgets(p, bufsize - (p - buffer), istk->fp);
772 if (!q)
773 break;
774 p += strlen(p);
775 if (p > buffer && p[-1] == '\n') {
776 /* Convert backslash-CRLF line continuation sequences into
777 nothing at all (for DOS and Windows) */
778 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
779 p -= 3;
780 *p = 0;
781 continued_count++;
782 }
783 /* Also convert backslash-LF line continuation sequences into
784 nothing at all (for Unix) */
785 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
786 p -= 2;
787 *p = 0;
788 continued_count++;
789 } else {
790 break;
791 }
792 }
793 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000794 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000795 bufsize += BUF_DELTA;
796 buffer = nasm_realloc(buffer, bufsize);
797 p = buffer + offset; /* prevent stale-pointer problems */
798 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000799 }
800
H. Peter Anvine2c80182005-01-15 22:15:51 +0000801 if (!q && p == buffer) {
802 nasm_free(buffer);
803 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000804 }
805
H. Peter Anvine2c80182005-01-15 22:15:51 +0000806 src_set_linnum(src_get_linnum() + istk->lineinc +
807 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000808
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000809 /*
810 * Play safe: remove CRs as well as LFs, if any of either are
811 * present at the end of the line.
812 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000813 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000814 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000815
816 /*
817 * Handle spurious ^Z, which may be inserted into source files
818 * by some file transfer utilities.
819 */
820 buffer[strcspn(buffer, "\032")] = '\0';
821
H. Peter Anvin734b1882002-04-30 21:01:08 +0000822 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000823
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000824 return buffer;
825}
826
827/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000828 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000829 * don't need to parse the value out of e.g. numeric tokens: we
830 * simply split one string into many.
831 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000832static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000833{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700834 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000835 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000836 Token *list = NULL;
837 Token *t, **tail = &list;
838
H. Peter Anvine2c80182005-01-15 22:15:51 +0000839 while (*line) {
840 p = line;
841 if (*p == '%') {
842 p++;
H. Peter Anvind784a082009-04-20 14:01:18 -0700843 if (*p == '+' && !nasm_isdigit(p[1])) {
844 p++;
845 type = TOK_PASTE;
846 } else if (nasm_isdigit(*p) ||
847 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000848 do {
849 p++;
850 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700851 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000852 type = TOK_PREPROC_ID;
853 } else if (*p == '{') {
854 p++;
855 while (*p && *p != '}') {
856 p[-1] = *p;
857 p++;
858 }
859 p[-1] = '\0';
860 if (*p)
861 p++;
862 type = TOK_PREPROC_ID;
H. Peter Anvin992fe752008-10-19 15:45:05 -0700863 } else if (*p == '[') {
864 int lvl = 1;
865 line += 2; /* Skip the leading %[ */
866 p++;
H. Peter Anvinec033012008-10-19 22:12:06 -0700867 while (lvl && (c = *p++)) {
H. Peter Anvinca544db2008-10-19 19:30:11 -0700868 switch (c) {
869 case ']':
870 lvl--;
871 break;
872 case '%':
H. Peter Anvinca544db2008-10-19 19:30:11 -0700873 if (*p == '[')
874 lvl++;
875 break;
876 case '\'':
877 case '\"':
878 case '`':
H. Peter Anvinec033012008-10-19 22:12:06 -0700879 p = nasm_skip_string(p)+1;
H. Peter Anvinca544db2008-10-19 19:30:11 -0700880 break;
881 default:
882 break;
H. Peter Anvin992fe752008-10-19 15:45:05 -0700883 }
H. Peter Anvin992fe752008-10-19 15:45:05 -0700884 }
H. Peter Anvinec033012008-10-19 22:12:06 -0700885 p--;
H. Peter Anvin992fe752008-10-19 15:45:05 -0700886 if (*p)
887 *p++ = '\0';
H. Peter Anvine1265812008-10-19 22:14:30 -0700888 if (lvl)
889 error(ERR_NONFATAL, "unterminated %[ construct");
H. Peter Anvin992fe752008-10-19 15:45:05 -0700890 type = TOK_INDIRECT;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700891 } else if (*p == '?') {
892 type = TOK_PREPROC_Q; /* %? */
893 p++;
894 if (*p == '?') {
895 type = TOK_PREPROC_QQ; /* %?? */
896 p++;
897 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000898 } else if (isidchar(*p) ||
899 ((*p == '!' || *p == '%' || *p == '$') &&
900 isidchar(p[1]))) {
901 do {
902 p++;
903 }
904 while (isidchar(*p));
905 type = TOK_PREPROC_ID;
906 } else {
907 type = TOK_OTHER;
908 if (*p == '%')
909 p++;
910 }
911 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
912 type = TOK_ID;
913 p++;
914 while (*p && isidchar(*p))
915 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700916 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000917 /*
918 * A string token.
919 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000920 type = TOK_STRING;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700921 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000922
H. Peter Anvine2c80182005-01-15 22:15:51 +0000923 if (*p) {
924 p++;
925 } else {
H. Peter Anvin917a3492008-09-24 09:14:49 -0700926 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000927 /* Handling unterminated strings by UNV */
928 /* type = -1; */
929 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +0200930 } else if (p[0] == '$' && p[1] == '$') {
H. Peter Anvin8e1f8112009-04-17 14:20:44 -0700931 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +0200932 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000933 } else if (isnumstart(*p)) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700934 bool is_hex = false;
935 bool is_float = false;
936 bool has_e = false;
937 char c, *r;
938
H. Peter Anvine2c80182005-01-15 22:15:51 +0000939 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700940 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000941 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700942
943 if (*p == '$') {
944 p++;
945 is_hex = true;
946 }
947
948 for (;;) {
949 c = *p++;
950
951 if (!is_hex && (c == 'e' || c == 'E')) {
952 has_e = true;
953 if (*p == '+' || *p == '-') {
954 /* e can only be followed by +/- if it is either a
955 prefixed hex number or a floating-point number */
956 p++;
957 is_float = true;
958 }
959 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
960 is_hex = true;
961 } else if (c == 'P' || c == 'p') {
962 is_float = true;
963 if (*p == '+' || *p == '-')
964 p++;
965 } else if (isnumchar(c) || c == '_')
966 ; /* just advance */
967 else if (c == '.') {
968 /* we need to deal with consequences of the legacy
969 parser, like "1.nolist" being two tokens
970 (TOK_NUMBER, TOK_ID) here; at least give it
971 a shot for now. In the future, we probably need
972 a flex-based scanner with proper pattern matching
973 to do it as well as it can be done. Nothing in
974 the world is going to help the person who wants
975 0x123.p16 interpreted as two tokens, though. */
976 r = p;
977 while (*r == '_')
978 r++;
979
H. Peter Anvinf221b9e2008-06-21 11:03:51 -0700980 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700981 (!is_hex && (*r == 'e' || *r == 'E')) ||
982 (*r == 'p' || *r == 'P')) {
983 p = r;
984 is_float = true;
985 } else
986 break; /* Terminate the token */
987 } else
988 break;
989 }
990 p--; /* Point to first character beyond number */
991
H. Peter Anvin8e1f8112009-04-17 14:20:44 -0700992 if (p == line+1 && *line == '$') {
993 type = TOK_OTHER; /* TOKEN_HERE */
994 } else {
995 if (has_e && !is_hex) {
996 /* 1e13 is floating-point, but 1e13h is not */
997 is_float = true;
998 }
H. Peter Anvind784a082009-04-20 14:01:18 -0700999
H. Peter Anvin8e1f8112009-04-17 14:20:44 -07001000 type = is_float ? TOK_FLOAT : TOK_NUMBER;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001001 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001002 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001003 type = TOK_WHITESPACE;
1004 p++;
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001005 while (*p && nasm_isspace(*p))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001006 p++;
1007 /*
1008 * Whitespace just before end-of-line is discarded by
1009 * pretending it's a comment; whitespace just before a
1010 * comment gets lumped into the comment.
1011 */
1012 if (!*p || *p == ';') {
1013 type = TOK_COMMENT;
1014 while (*p)
1015 p++;
1016 }
1017 } else if (*p == ';') {
1018 type = TOK_COMMENT;
1019 while (*p)
1020 p++;
1021 } else {
1022 /*
1023 * Anything else is an operator of some kind. We check
1024 * for all the double-character operators (>>, <<, //,
1025 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001026 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001027 */
1028 type = TOK_OTHER;
1029 if ((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[0] == '=' && p[1] == '=') ||
1035 (p[0] == '!' && p[1] == '=') ||
1036 (p[0] == '<' && p[1] == '>') ||
1037 (p[0] == '&' && p[1] == '&') ||
1038 (p[0] == '|' && p[1] == '|') ||
1039 (p[0] == '^' && p[1] == '^')) {
1040 p++;
1041 }
1042 p++;
1043 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001044
H. Peter Anvine2c80182005-01-15 22:15:51 +00001045 /* Handling unterminated string by UNV */
1046 /*if (type == -1)
H. Peter Anvin89cee572009-07-15 09:16:54 -04001047 {
1048 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1049 t->text[p-line] = *line;
1050 tail = &t->next;
1051 }
1052 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001053 if (type != TOK_COMMENT) {
1054 *tail = t = new_Token(NULL, type, line, p - line);
1055 tail = &t->next;
1056 }
1057 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001058 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001059 return list;
1060}
1061
H. Peter Anvince616072002-04-30 21:02:23 +00001062/*
1063 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001064 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001065 * deleted only all at once by the delete_Blocks function.
1066 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001067static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001068{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001069 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001070
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001071 /* first, get to the end of the linked list */
1072 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001073 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001074 /* now allocate the requested chunk */
1075 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001076
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001077 /* now allocate a new block for the next request */
1078 b->next = nasm_malloc(sizeof(Blocks));
1079 /* and initialize the contents of the new block */
1080 b->next->next = NULL;
1081 b->next->chunk = NULL;
1082 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001083}
1084
1085/*
1086 * this function deletes all managed blocks of memory
1087 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001088static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001089{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001090 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001091
H. Peter Anvin70653092007-10-19 14:42:29 -07001092 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001093 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001094 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001095 * free it.
1096 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001097 while (b) {
1098 if (b->chunk)
1099 nasm_free(b->chunk);
1100 a = b;
1101 b = b->next;
1102 if (a != &blocks)
1103 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001104 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001105}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001106
1107/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001108 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001109 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001110 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001111 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001112static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvinc751e862008-06-09 10:18:45 -07001113 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001114{
1115 Token *t;
1116 int i;
1117
H. Peter Anvin89cee572009-07-15 09:16:54 -04001118 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001119 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1120 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1121 freeTokens[i].next = &freeTokens[i + 1];
1122 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001123 }
1124 t = freeTokens;
1125 freeTokens = t->next;
1126 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001127 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001128 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001129 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001130 t->text = NULL;
1131 } else {
1132 if (txtlen == 0)
1133 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001134 t->text = nasm_malloc(txtlen+1);
1135 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001136 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001137 }
1138 return t;
1139}
1140
H. Peter Anvine2c80182005-01-15 22:15:51 +00001141static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001142{
1143 Token *next = t->next;
1144 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001145 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001146 freeTokens = t;
1147 return next;
1148}
1149
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001150/*
1151 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001152 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1153 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001154 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001155static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001156{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001157 Token *t;
1158 int len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001159 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001160 const char *q;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001161
1162 len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001163 for (t = tlist; t; t = t->next) {
1164 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001165 char *p = getenv(t->text + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001166 nasm_free(t->text);
1167 if (p)
1168 t->text = nasm_strdup(p);
1169 else
1170 t->text = NULL;
1171 }
1172 /* Expand local macros here and not during preprocessing */
1173 if (expand_locals &&
1174 t->type == TOK_PREPROC_ID && t->text &&
1175 t->text[0] == '%' && t->text[1] == '$') {
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001176 const char *q;
1177 char *p;
1178 Context *ctx = get_ctx(t->text, &q, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001179 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001180 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001181 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001182 p = nasm_strcat(buffer, q);
1183 nasm_free(t->text);
1184 t->text = p;
1185 }
1186 }
1187 if (t->type == TOK_WHITESPACE) {
1188 len++;
1189 } else if (t->text) {
1190 len += strlen(t->text);
1191 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001192 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001193 p = line = nasm_malloc(len + 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001194 for (t = tlist; t; t = t->next) {
1195 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001196 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001197 } else if (t->text) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001198 q = t->text;
1199 while (*q)
1200 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001201 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001202 }
1203 *p = '\0';
1204 return line;
1205}
1206
1207/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001208 * A scanner, suitable for use by the expression evaluator, which
1209 * operates on a line of Tokens. Expects a pointer to a pointer to
1210 * the first token in the line to be passed in as its private_data
1211 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001212 *
1213 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001214 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001215static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001216{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001217 Token **tlineptr = private_data;
1218 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001219 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001220
H. Peter Anvine2c80182005-01-15 22:15:51 +00001221 do {
1222 tline = *tlineptr;
1223 *tlineptr = tline ? tline->next : NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001224 }
1225 while (tline && (tline->type == TOK_WHITESPACE ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001226 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001227
1228 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001229 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001230
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001231 tokval->t_charptr = tline->text;
1232
H. Peter Anvin76690a12002-04-30 20:52:49 +00001233 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001234 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001235 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001236 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001237
H. Peter Anvine2c80182005-01-15 22:15:51 +00001238 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001239 p = tokval->t_charptr = tline->text;
1240 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001241 tokval->t_charptr++;
1242 return tokval->t_type = TOKEN_ID;
1243 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001244
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001245 for (r = p, s = ourcopy; *r; r++) {
Philipp Thomas76ec8e72008-05-21 08:53:21 -07001246 if (r >= p+MAX_KEYWORD)
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001247 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001248 *s++ = nasm_tolower(*r);
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001249 }
1250 *s = '\0';
1251 /* right, so we have an identifier sitting in temp storage. now,
1252 * is it actually a register or instruction name, or what? */
1253 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001254 }
1255
H. Peter Anvine2c80182005-01-15 22:15:51 +00001256 if (tline->type == TOK_NUMBER) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001257 bool rn_error;
1258 tokval->t_integer = readnum(tline->text, &rn_error);
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001259 tokval->t_charptr = tline->text;
H. Peter Anvin11627042008-06-09 20:45:19 -07001260 if (rn_error)
1261 return tokval->t_type = TOKEN_ERRNUM;
1262 else
1263 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001264 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001265
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001266 if (tline->type == TOK_FLOAT) {
1267 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001268 }
1269
H. Peter Anvine2c80182005-01-15 22:15:51 +00001270 if (tline->type == TOK_STRING) {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001271 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001272
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001273 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001274 tokval->t_charptr = tline->text;
1275 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001276
H. Peter Anvin11627042008-06-09 20:45:19 -07001277 if (ep[0] != bq || ep[1] != '\0')
1278 return tokval->t_type = TOKEN_ERRSTR;
1279 else
1280 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001281 }
1282
H. Peter Anvine2c80182005-01-15 22:15:51 +00001283 if (tline->type == TOK_OTHER) {
1284 if (!strcmp(tline->text, "<<"))
1285 return tokval->t_type = TOKEN_SHL;
1286 if (!strcmp(tline->text, ">>"))
1287 return tokval->t_type = TOKEN_SHR;
1288 if (!strcmp(tline->text, "//"))
1289 return tokval->t_type = TOKEN_SDIV;
1290 if (!strcmp(tline->text, "%%"))
1291 return tokval->t_type = TOKEN_SMOD;
1292 if (!strcmp(tline->text, "=="))
1293 return tokval->t_type = TOKEN_EQ;
1294 if (!strcmp(tline->text, "<>"))
1295 return tokval->t_type = TOKEN_NE;
1296 if (!strcmp(tline->text, "!="))
1297 return tokval->t_type = TOKEN_NE;
1298 if (!strcmp(tline->text, "<="))
1299 return tokval->t_type = TOKEN_LE;
1300 if (!strcmp(tline->text, ">="))
1301 return tokval->t_type = TOKEN_GE;
1302 if (!strcmp(tline->text, "&&"))
1303 return tokval->t_type = TOKEN_DBL_AND;
1304 if (!strcmp(tline->text, "^^"))
1305 return tokval->t_type = TOKEN_DBL_XOR;
1306 if (!strcmp(tline->text, "||"))
1307 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001308 }
1309
1310 /*
1311 * We have no other options: just return the first character of
1312 * the token text.
1313 */
1314 return tokval->t_type = tline->text[0];
1315}
1316
1317/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001318 * Compare a string to the name of an existing macro; this is a
1319 * simple wrapper which calls either strcmp or nasm_stricmp
1320 * depending on the value of the `casesense' parameter.
1321 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001322static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001323{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001324 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001325}
1326
1327/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001328 * Compare a string to the name of an existing macro; this is a
1329 * simple wrapper which calls either strcmp or nasm_stricmp
1330 * depending on the value of the `casesense' parameter.
1331 */
1332static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1333{
1334 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1335}
1336
1337/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001338 * Return the Context structure associated with a %$ token. Return
1339 * NULL, having _already_ reported an error condition, if the
1340 * context stack isn't deep enough for the supplied number of $
1341 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001342 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001343 * also scanned for such smacro, until it is found; if not -
1344 * only the context that directly results from the number of $'s
1345 * in variable's name.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001346 *
1347 * If "namep" is non-NULL, set it to the pointer to the macro name
1348 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001349 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001350static Context *get_ctx(const char *name, const char **namep,
1351 bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001352{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001353 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001354 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001355 int i;
1356
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001357 if (namep)
1358 *namep = name;
1359
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001360 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001361 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001362
H. Peter Anvine2c80182005-01-15 22:15:51 +00001363 if (!cstk) {
1364 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1365 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001366 }
1367
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001368 name += 2;
1369 ctx = cstk;
1370 i = 0;
1371 while (ctx && *name == '$') {
1372 name++;
1373 i++;
1374 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001375 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001376 if (!ctx) {
1377 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001378 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001379 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001380 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001381
1382 if (namep)
1383 *namep = name;
1384
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001385 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001386 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001387
H. Peter Anvine2c80182005-01-15 22:15:51 +00001388 do {
1389 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001390 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001391 while (m) {
1392 if (!mstrcmp(m->name, name, m->casesense))
1393 return ctx;
1394 m = m->next;
1395 }
1396 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001397 }
1398 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001399 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001400}
1401
1402/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001403 * Check to see if a file is already in a string list
1404 */
1405static bool in_list(const StrList *list, const char *str)
1406{
1407 while (list) {
1408 if (!strcmp(list->str, str))
1409 return true;
1410 list = list->next;
1411 }
1412 return false;
1413}
1414
1415/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001416 * Open an include file. This routine must always return a valid
1417 * file pointer if it returns - it's responsible for throwing an
1418 * ERR_FATAL and bombing out completely if not. It should also try
1419 * the include path one by one until it finds the file or reaches
1420 * the end of the path.
1421 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001422static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
H. Peter Anvin418ca702008-05-30 10:42:30 -07001423 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001424{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001425 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001426 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001427 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001428 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001429 size_t prefix_len = 0;
1430 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001431
H. Peter Anvine2c80182005-01-15 22:15:51 +00001432 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001433 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
1434 memcpy(sl->str, prefix, prefix_len);
1435 memcpy(sl->str+prefix_len, file, len+1);
1436 fp = fopen(sl->str, "r");
H. Peter Anvin418ca702008-05-30 10:42:30 -07001437 if (fp && dhead && !in_list(*dhead, sl->str)) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001438 sl->next = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001439 **dtail = sl;
1440 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001441 } else {
1442 nasm_free(sl);
1443 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001444 if (fp)
1445 return fp;
H. Peter Anvin418ca702008-05-30 10:42:30 -07001446 if (!ip) {
1447 if (!missing_ok)
1448 break;
1449 prefix = NULL;
1450 } else {
1451 prefix = ip->path;
1452 ip = ip->next;
1453 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001454 if (prefix) {
1455 prefix_len = strlen(prefix);
1456 } else {
1457 /* -MG given and file not found */
H. Peter Anvin418ca702008-05-30 10:42:30 -07001458 if (dhead && !in_list(*dhead, file)) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001459 sl = nasm_malloc(len+1+sizeof sl->next);
1460 sl->next = NULL;
1461 strcpy(sl->str, file);
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001462 **dtail = sl;
1463 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001464 }
H. Peter Anvin37a321f2007-09-24 13:41:58 -07001465 return NULL;
1466 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001467 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001468
H. Peter Anvin734b1882002-04-30 21:01:08 +00001469 error(ERR_FATAL, "unable to open include file `%s'", file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001470 return NULL; /* never reached - placate compilers */
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001471}
1472
1473/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001474 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001475 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001476 * return true if _any_ single-line macro of that name is defined.
1477 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001478 * `nparam' or no parameters is defined.
1479 *
1480 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001481 * defined, or nparam is -1, the address of the definition structure
1482 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001483 * is NULL, no action will be taken regarding its contents, and no
1484 * error will occur.
1485 *
1486 * Note that this is also called with nparam zero to resolve
1487 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001488 *
1489 * If you already know which context macro belongs to, you can pass
1490 * the context pointer as first parameter; if you won't but name begins
1491 * with %$ the context will be automatically computed. If all_contexts
1492 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001493 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001494static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001495smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001496 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001497{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001498 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001499 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001500
H. Peter Anvin97a23472007-09-16 17:57:25 -07001501 if (ctx) {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001502 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001503 } else if (name[0] == '%' && name[1] == '$') {
1504 if (cstk)
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001505 ctx = get_ctx(name, &name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001506 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001507 return false; /* got to return _something_ */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001508 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001509 } else {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001510 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001511 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001512 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001513
H. Peter Anvine2c80182005-01-15 22:15:51 +00001514 while (m) {
1515 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001516 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001517 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001518 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001519 *defn = m;
1520 else
1521 *defn = NULL;
1522 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001523 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001524 }
1525 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001526 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001527
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001528 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001529}
1530
1531/*
1532 * Count and mark off the parameters in a multi-line macro call.
1533 * This is called both from within the multi-line macro expansion
1534 * code, and also to mark off the default parameters when provided
1535 * in a %macro definition line.
1536 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001537static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001538{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001539 int paramsize, brace;
1540
1541 *nparam = paramsize = 0;
1542 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001543 while (t) {
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001544 /* +1: we need space for the final NULL */
1545 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001546 paramsize += PARAM_DELTA;
1547 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1548 }
1549 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001550 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001551 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001552 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001553 (*params)[(*nparam)++] = t;
1554 while (tok_isnt_(t, brace ? "}" : ","))
1555 t = t->next;
1556 if (t) { /* got a comma/brace */
1557 t = t->next;
1558 if (brace) {
1559 /*
1560 * Now we've found the closing brace, look further
1561 * for the comma.
1562 */
1563 skip_white_(t);
1564 if (tok_isnt_(t, ",")) {
1565 error(ERR_NONFATAL,
1566 "braces do not enclose all of macro parameter");
1567 while (tok_isnt_(t, ","))
1568 t = t->next;
1569 }
1570 if (t)
1571 t = t->next; /* eat the comma */
1572 }
1573 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001574 }
1575}
1576
1577/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001578 * Determine whether one of the various `if' conditions is true or
1579 * not.
1580 *
1581 * We must free the tline we get passed.
1582 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001583static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001584{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001585 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001586 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001587 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001588 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001589 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001590 enum pp_token_type needtype;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001591
1592 origline = tline;
1593
H. Peter Anvine2c80182005-01-15 22:15:51 +00001594 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001595 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001596 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001597 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001598 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001599 if (!tline)
1600 break;
1601 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001602 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001603 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001604 free_tlist(origline);
1605 return -1;
1606 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001607 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001608 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001609 tline = tline->next;
1610 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001611 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001612
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001613 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001614 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001615 while (tline) {
1616 skip_white_(tline);
1617 if (!tline || (tline->type != TOK_ID &&
1618 (tline->type != TOK_PREPROC_ID ||
1619 tline->text[1] != '$'))) {
1620 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001621 "`%s' expects macro identifiers", pp_directives[ct]);
1622 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001623 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001624 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001625 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001626 tline = tline->next;
1627 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001628 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001629
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001630 case PPC_IFIDN:
1631 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001632 tline = expand_smacro(tline);
1633 t = tt = tline;
1634 while (tok_isnt_(tt, ","))
1635 tt = tt->next;
1636 if (!tt) {
1637 error(ERR_NONFATAL,
1638 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001639 pp_directives[ct]);
1640 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001641 }
1642 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001643 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001644 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1645 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1646 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001647 pp_directives[ct]);
1648 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001649 }
1650 if (t->type == TOK_WHITESPACE) {
1651 t = t->next;
1652 continue;
1653 }
1654 if (tt->type == TOK_WHITESPACE) {
1655 tt = tt->next;
1656 continue;
1657 }
1658 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001659 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001660 break;
1661 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001662 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001663 if (t->type == TOK_STRING) {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001664 size_t l1 = nasm_unquote(t->text, NULL);
1665 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001666
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001667 if (l1 != l2) {
1668 j = false;
1669 break;
1670 }
1671 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1672 j = false;
1673 break;
1674 }
1675 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001676 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001677 break;
1678 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001679
H. Peter Anvine2c80182005-01-15 22:15:51 +00001680 t = t->next;
1681 tt = tt->next;
1682 }
1683 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001684 j = false; /* trailing gunk on one end or other */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001685 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001686
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001687 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001688 {
1689 bool found = false;
1690 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001691
H. Peter Anvin89cee572009-07-15 09:16:54 -04001692 skip_white_(tline);
1693 tline = expand_id(tline);
1694 if (!tok_type_(tline, TOK_ID)) {
1695 error(ERR_NONFATAL,
1696 "`%s' expects a macro name", pp_directives[ct]);
1697 goto fail;
1698 }
1699 searching.name = nasm_strdup(tline->text);
1700 searching.casesense = true;
1701 searching.plus = false;
1702 searching.nolist = false;
1703 searching.in_progress = 0;
1704 searching.max_depth = 0;
1705 searching.rep_nest = NULL;
1706 searching.nparam_min = 0;
1707 searching.nparam_max = INT_MAX;
1708 tline = expand_smacro(tline->next);
1709 skip_white_(tline);
1710 if (!tline) {
1711 } else if (!tok_type_(tline, TOK_NUMBER)) {
1712 error(ERR_NONFATAL,
1713 "`%s' expects a parameter count or nothing",
1714 pp_directives[ct]);
1715 } else {
1716 searching.nparam_min = searching.nparam_max =
1717 readnum(tline->text, &j);
1718 if (j)
1719 error(ERR_NONFATAL,
1720 "unable to parse parameter count `%s'",
1721 tline->text);
1722 }
1723 if (tline && tok_is_(tline->next, "-")) {
1724 tline = tline->next->next;
1725 if (tok_is_(tline, "*"))
1726 searching.nparam_max = INT_MAX;
1727 else if (!tok_type_(tline, TOK_NUMBER))
1728 error(ERR_NONFATAL,
1729 "`%s' expects a parameter count after `-'",
1730 pp_directives[ct]);
1731 else {
1732 searching.nparam_max = readnum(tline->text, &j);
1733 if (j)
1734 error(ERR_NONFATAL,
1735 "unable to parse parameter count `%s'",
1736 tline->text);
1737 if (searching.nparam_min > searching.nparam_max)
1738 error(ERR_NONFATAL,
1739 "minimum parameter count exceeds maximum");
H. Peter Anvin97a23472007-09-16 17:57:25 -07001740 }
H. Peter Anvin89cee572009-07-15 09:16:54 -04001741 }
1742 if (tline && tok_is_(tline->next, "+")) {
1743 tline = tline->next;
1744 searching.plus = true;
1745 }
1746 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1747 while (mmac) {
1748 if (!strcmp(mmac->name, searching.name) &&
1749 (mmac->nparam_min <= searching.nparam_max
1750 || searching.plus)
1751 && (searching.nparam_min <= mmac->nparam_max
1752 || mmac->plus)) {
1753 found = true;
1754 break;
1755 }
1756 mmac = mmac->next;
1757 }
1758 if (tline && tline->next)
1759 error(ERR_WARNING|ERR_PASS1,
1760 "trailing garbage after %%ifmacro ignored");
1761 nasm_free(searching.name);
1762 j = found;
1763 break;
1764 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001765
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001766 case PPC_IFID:
1767 needtype = TOK_ID;
1768 goto iftype;
1769 case PPC_IFNUM:
1770 needtype = TOK_NUMBER;
1771 goto iftype;
1772 case PPC_IFSTR:
1773 needtype = TOK_STRING;
1774 goto iftype;
1775
1776 iftype:
H. Peter Anvin134b9462008-02-16 17:01:40 -08001777 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001778
H. Peter Anvin927c92b2008-02-16 13:44:52 -08001779 while (tok_type_(t, TOK_WHITESPACE) ||
1780 (needtype == TOK_NUMBER &&
1781 tok_type_(t, TOK_OTHER) &&
1782 (t->text[0] == '-' || t->text[0] == '+') &&
1783 !t->text[1]))
1784 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001785
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001786 j = tok_type_(t, needtype);
1787 break;
1788
1789 case PPC_IFTOKEN:
1790 t = tline = expand_smacro(tline);
1791 while (tok_type_(t, TOK_WHITESPACE))
1792 t = t->next;
1793
1794 j = false;
1795 if (t) {
1796 t = t->next; /* Skip the actual token */
1797 while (tok_type_(t, TOK_WHITESPACE))
1798 t = t->next;
1799 j = !t; /* Should be nothing left */
1800 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001801 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001802
H. Peter Anvin134b9462008-02-16 17:01:40 -08001803 case PPC_IFEMPTY:
1804 t = tline = expand_smacro(tline);
1805 while (tok_type_(t, TOK_WHITESPACE))
1806 t = t->next;
1807
1808 j = !t; /* Should be empty */
1809 break;
1810
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001811 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001812 t = tline = expand_smacro(tline);
1813 tptr = &t;
1814 tokval.t_type = TOKEN_INVALID;
1815 evalresult = evaluate(ppscan, tptr, &tokval,
1816 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001817 if (!evalresult)
1818 return -1;
1819 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001820 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001821 "trailing garbage after expression ignored");
1822 if (!is_simple(evalresult)) {
1823 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001824 "non-constant value given to `%s'", pp_directives[ct]);
1825 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001826 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001827 j = reloc_value(evalresult) != 0;
H. Peter Anvin90e6e812008-07-16 14:38:24 -07001828 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001829
H. Peter Anvine2c80182005-01-15 22:15:51 +00001830 default:
1831 error(ERR_FATAL,
1832 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001833 pp_directives[ct]);
1834 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001835 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001836
1837 free_tlist(origline);
1838 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001839
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001840fail:
1841 free_tlist(origline);
1842 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001843}
1844
1845/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001846 * Common code for defining an smacro
1847 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001848static bool define_smacro(Context *ctx, const char *mname, bool casesense,
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001849 int nparam, Token *expansion)
1850{
1851 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001852 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07001853
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001854 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1855 if (!smac) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07001856 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001857 "single-line macro `%s' defined both with and"
1858 " without parameters", mname);
1859
1860 /* Some instances of the old code considered this a failure,
1861 some others didn't. What is the right thing to do here? */
1862 free_tlist(expansion);
1863 return false; /* Failure */
1864 } else {
1865 /*
1866 * We're redefining, so we have to take over an
1867 * existing SMacro structure. This means freeing
1868 * what was already in it.
1869 */
1870 nasm_free(smac->name);
1871 free_tlist(smac->expansion);
1872 }
1873 } else {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001874 smtbl = ctx ? &ctx->localmac : &smacros;
1875 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001876 smac = nasm_malloc(sizeof(SMacro));
1877 smac->next = *smhead;
1878 *smhead = smac;
1879 }
1880 smac->name = nasm_strdup(mname);
1881 smac->casesense = casesense;
1882 smac->nparam = nparam;
1883 smac->expansion = expansion;
1884 smac->in_progress = false;
1885 return true; /* Success */
1886}
1887
1888/*
1889 * Undefine an smacro
1890 */
1891static void undef_smacro(Context *ctx, const char *mname)
1892{
1893 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001894 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001895
H. Peter Anvin166c2472008-05-28 12:28:58 -07001896 smtbl = ctx ? &ctx->localmac : &smacros;
1897 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07001898
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001899 if (smhead) {
1900 /*
1901 * We now have a macro name... go hunt for it.
1902 */
1903 sp = smhead;
1904 while ((s = *sp) != NULL) {
1905 if (!mstrcmp(s->name, mname, s->casesense)) {
1906 *sp = s->next;
1907 nasm_free(s->name);
1908 free_tlist(s->expansion);
1909 nasm_free(s);
1910 } else {
1911 sp = &s->next;
1912 }
1913 }
1914 }
1915}
1916
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001917/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07001918 * Parse a mmacro specification.
1919 */
1920static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
1921{
1922 bool err;
1923
1924 tline = tline->next;
1925 skip_white_(tline);
1926 tline = expand_id(tline);
1927 if (!tok_type_(tline, TOK_ID)) {
1928 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
1929 return false;
1930 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001931
H. Peter Anvin89cee572009-07-15 09:16:54 -04001932 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07001933 def->name = nasm_strdup(tline->text);
1934 def->plus = false;
1935 def->nolist = false;
1936 def->in_progress = 0;
1937 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001938 def->nparam_min = 0;
1939 def->nparam_max = 0;
1940
H. Peter Anvina26433d2008-07-16 14:40:01 -07001941 tline = expand_smacro(tline->next);
1942 skip_white_(tline);
1943 if (!tok_type_(tline, TOK_NUMBER)) {
1944 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07001945 } else {
1946 def->nparam_min = def->nparam_max =
1947 readnum(tline->text, &err);
1948 if (err)
1949 error(ERR_NONFATAL,
1950 "unable to parse parameter count `%s'", tline->text);
1951 }
1952 if (tline && tok_is_(tline->next, "-")) {
1953 tline = tline->next->next;
1954 if (tok_is_(tline, "*")) {
1955 def->nparam_max = INT_MAX;
1956 } else if (!tok_type_(tline, TOK_NUMBER)) {
1957 error(ERR_NONFATAL,
1958 "`%s' expects a parameter count after `-'", directive);
1959 } else {
1960 def->nparam_max = readnum(tline->text, &err);
1961 if (err) {
1962 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
1963 tline->text);
1964 }
1965 if (def->nparam_min > def->nparam_max) {
1966 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
1967 }
1968 }
1969 }
1970 if (tline && tok_is_(tline->next, "+")) {
1971 tline = tline->next;
1972 def->plus = true;
1973 }
1974 if (tline && tok_type_(tline->next, TOK_ID) &&
1975 !nasm_stricmp(tline->next->text, ".nolist")) {
1976 tline = tline->next;
1977 def->nolist = true;
1978 }
Keith Kanios891775e2009-07-11 06:08:54 -05001979
H. Peter Anvina26433d2008-07-16 14:40:01 -07001980 /*
1981 * Handle default parameters.
1982 */
1983 if (tline && tline->next) {
1984 def->dlist = tline->next;
1985 tline->next = NULL;
1986 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
1987 } else {
1988 def->dlist = NULL;
1989 def->defaults = NULL;
1990 }
1991 def->expansion = NULL;
1992
H. Peter Anvin89cee572009-07-15 09:16:54 -04001993 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
1994 !def->plus)
1995 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
1996 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001997
H. Peter Anvina26433d2008-07-16 14:40:01 -07001998 return true;
1999}
2000
2001
2002/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002003 * Decode a size directive
2004 */
2005static int parse_size(const char *str) {
2006 static const char *size_names[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07002007 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002008 static const int sizes[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07002009 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002010
2011 return sizes[bsii(str, size_names, elements(size_names))+1];
2012}
2013
H. Peter Anvinf9c9a672009-07-14 15:14:05 -04002014/*
2015 * nasm_unquote with error if the string contains NUL characters.
2016 * If the string contains NUL characters, issue an error and return
2017 * the C len, i.e. truncate at the NUL.
2018 */
2019static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
2020{
2021 size_t len = nasm_unquote(qstr, NULL);
2022 size_t clen = strlen(qstr);
2023
2024 if (len != clen)
H. Peter Anvincd0943e2009-07-14 15:17:11 -04002025 error(ERR_NONFATAL, "NUL character in `%s' directive",
2026 pp_directives[directive]);
H. Peter Anvinf9c9a672009-07-14 15:14:05 -04002027
2028 return clen;
2029}
2030
Ed Beroset3ab3f412002-06-11 03:31:49 +00002031/**
2032 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002033 * Find out if a line contains a preprocessor directive, and deal
2034 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002035 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002036 * If a directive _is_ found, it is the responsibility of this routine
2037 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002038 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002039 * @param tline a pointer to the current tokeninzed line linked list
2040 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002041 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002042 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002043static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002044{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002045 enum preproc_token i;
2046 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002047 bool err;
2048 int nparam;
2049 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002050 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002051 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002052 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002053 char *p, *pp;
2054 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002055 Include *inc;
2056 Context *ctx;
2057 Cond *cond;
H. Peter Anvin97a23472007-09-16 17:57:25 -07002058 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002059 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
2060 Line *l;
2061 struct tokenval tokval;
2062 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002063 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002064 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002065 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002066 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002067
2068 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002069
H. Peter Anvineba20a72002-04-30 20:53:55 +00002070 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002071 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002072 (tline->text[1] == '%' || tline->text[1] == '$'
2073 || tline->text[1] == '!'))
2074 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002075
H. Peter Anvin4169a472007-09-12 01:29:43 +00002076 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002077
2078 /*
2079 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00002080 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002081 * we should ignore all directives except for condition
2082 * directives.
2083 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002084 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002085 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2086 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002087 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002088
2089 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002090 * If we're defining a macro or reading a %rep block, we should
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002091 * ignore all directives except for %macro/%imacro (which nest),
2092 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2093 * If we're in a %rep block, another %rep nests, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002094 */
2095 if (defining && i != PP_MACRO && i != PP_IMACRO &&
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002096 i != PP_RMACRO && i != PP_IRMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00002097 i != PP_ENDMACRO && i != PP_ENDM &&
2098 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2099 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002100 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002101
Charles Crayned4200be2008-07-12 16:42:33 -07002102 if (defining) {
Keith Kanios852f1ee2009-07-12 00:19:55 -05002103 if (i == PP_MACRO || i == PP_IMACRO ||
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002104 i == PP_RMACRO || i == PP_IRMACRO) {
Charles Crayned4200be2008-07-12 16:42:33 -07002105 nested_mac_count++;
2106 return NO_DIRECTIVE_FOUND;
2107 } else if (nested_mac_count > 0) {
2108 if (i == PP_ENDMACRO) {
2109 nested_mac_count--;
2110 return NO_DIRECTIVE_FOUND;
2111 }
2112 }
2113 if (!defining->name) {
2114 if (i == PP_REP) {
2115 nested_rep_count++;
2116 return NO_DIRECTIVE_FOUND;
2117 } else if (nested_rep_count > 0) {
2118 if (i == PP_ENDREP) {
2119 nested_rep_count--;
2120 return NO_DIRECTIVE_FOUND;
2121 }
2122 }
2123 }
2124 }
2125
H. Peter Anvin4169a472007-09-12 01:29:43 +00002126 switch (i) {
2127 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002128 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2129 tline->text);
2130 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002131
H. Peter Anvine2c80182005-01-15 22:15:51 +00002132 case PP_STACKSIZE:
2133 /* Directive to tell NASM what the default stack size is. The
2134 * default is for a 16-bit stack, and this can be overriden with
2135 * %stacksize large.
2136 * the following form:
2137 *
2138 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2139 */
2140 tline = tline->next;
2141 if (tline && tline->type == TOK_WHITESPACE)
2142 tline = tline->next;
2143 if (!tline || tline->type != TOK_ID) {
2144 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2145 free_tlist(origline);
2146 return DIRECTIVE_FOUND;
2147 }
2148 if (nasm_stricmp(tline->text, "flat") == 0) {
2149 /* All subsequent ARG directives are for a 32-bit stack */
2150 StackSize = 4;
2151 StackPointer = "ebp";
2152 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002153 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002154 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2155 /* All subsequent ARG directives are for a 64-bit stack */
2156 StackSize = 8;
2157 StackPointer = "rbp";
2158 ArgOffset = 8;
2159 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002160 } else if (nasm_stricmp(tline->text, "large") == 0) {
2161 /* All subsequent ARG directives are for a 16-bit stack,
2162 * far function call.
2163 */
2164 StackSize = 2;
2165 StackPointer = "bp";
2166 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002167 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002168 } else if (nasm_stricmp(tline->text, "small") == 0) {
2169 /* All subsequent ARG directives are for a 16-bit stack,
2170 * far function call. We don't support near functions.
2171 */
2172 StackSize = 2;
2173 StackPointer = "bp";
2174 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002175 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002176 } else {
2177 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2178 free_tlist(origline);
2179 return DIRECTIVE_FOUND;
2180 }
2181 free_tlist(origline);
2182 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002183
H. Peter Anvine2c80182005-01-15 22:15:51 +00002184 case PP_ARG:
2185 /* TASM like ARG directive to define arguments to functions, in
2186 * the following form:
2187 *
2188 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2189 */
2190 offset = ArgOffset;
2191 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002192 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002193 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002194
H. Peter Anvine2c80182005-01-15 22:15:51 +00002195 /* Find the argument name */
2196 tline = tline->next;
2197 if (tline && tline->type == TOK_WHITESPACE)
2198 tline = tline->next;
2199 if (!tline || tline->type != TOK_ID) {
2200 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2201 free_tlist(origline);
2202 return DIRECTIVE_FOUND;
2203 }
2204 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002205
H. Peter Anvine2c80182005-01-15 22:15:51 +00002206 /* Find the argument size type */
2207 tline = tline->next;
2208 if (!tline || tline->type != TOK_OTHER
2209 || tline->text[0] != ':') {
2210 error(ERR_NONFATAL,
2211 "Syntax error processing `%%arg' directive");
2212 free_tlist(origline);
2213 return DIRECTIVE_FOUND;
2214 }
2215 tline = tline->next;
2216 if (!tline || tline->type != TOK_ID) {
2217 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2218 free_tlist(origline);
2219 return DIRECTIVE_FOUND;
2220 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002221
H. Peter Anvine2c80182005-01-15 22:15:51 +00002222 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002223 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002224 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002225 size = parse_size(tt->text);
2226 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002227 error(ERR_NONFATAL,
2228 "Invalid size type for `%%arg' missing directive");
2229 free_tlist(tt);
2230 free_tlist(origline);
2231 return DIRECTIVE_FOUND;
2232 }
2233 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002234
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002235 /* Round up to even stack slots */
2236 size = (size+StackSize-1) & ~(StackSize-1);
2237
H. Peter Anvine2c80182005-01-15 22:15:51 +00002238 /* Now define the macro for the argument */
2239 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2240 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002241 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002242 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002243
H. Peter Anvine2c80182005-01-15 22:15:51 +00002244 /* Move to the next argument in the list */
2245 tline = tline->next;
2246 if (tline && tline->type == TOK_WHITESPACE)
2247 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002248 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2249 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002250 free_tlist(origline);
2251 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002252
H. Peter Anvine2c80182005-01-15 22:15:51 +00002253 case PP_LOCAL:
2254 /* TASM like LOCAL directive to define local variables for a
2255 * function, in the following form:
2256 *
2257 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2258 *
2259 * The '= LocalSize' at the end is ignored by NASM, but is
2260 * required by TASM to define the local parameter size (and used
2261 * by the TASM macro package).
2262 */
2263 offset = LocalOffset;
2264 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002265 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002266 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002267
H. Peter Anvine2c80182005-01-15 22:15:51 +00002268 /* Find the argument name */
2269 tline = tline->next;
2270 if (tline && tline->type == TOK_WHITESPACE)
2271 tline = tline->next;
2272 if (!tline || tline->type != TOK_ID) {
2273 error(ERR_NONFATAL,
2274 "`%%local' missing argument parameter");
2275 free_tlist(origline);
2276 return DIRECTIVE_FOUND;
2277 }
2278 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002279
H. Peter Anvine2c80182005-01-15 22:15:51 +00002280 /* Find the argument size type */
2281 tline = tline->next;
2282 if (!tline || tline->type != TOK_OTHER
2283 || tline->text[0] != ':') {
2284 error(ERR_NONFATAL,
2285 "Syntax error processing `%%local' directive");
2286 free_tlist(origline);
2287 return DIRECTIVE_FOUND;
2288 }
2289 tline = tline->next;
2290 if (!tline || tline->type != TOK_ID) {
2291 error(ERR_NONFATAL,
2292 "`%%local' missing size type parameter");
2293 free_tlist(origline);
2294 return DIRECTIVE_FOUND;
2295 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002296
H. Peter Anvine2c80182005-01-15 22:15:51 +00002297 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002298 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002299 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002300 size = parse_size(tt->text);
2301 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002302 error(ERR_NONFATAL,
2303 "Invalid size type for `%%local' missing directive");
2304 free_tlist(tt);
2305 free_tlist(origline);
2306 return DIRECTIVE_FOUND;
2307 }
2308 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002309
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002310 /* Round up to even stack slots */
2311 size = (size+StackSize-1) & ~(StackSize-1);
2312
2313 offset += size; /* Negative offset, increment before */
2314
2315 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002316 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2317 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002318 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002319
H. Peter Anvine2c80182005-01-15 22:15:51 +00002320 /* Now define the assign to setup the enter_c macro correctly */
2321 snprintf(directive, sizeof(directive),
2322 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002323 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002324
H. Peter Anvine2c80182005-01-15 22:15:51 +00002325 /* Move to the next argument in the list */
2326 tline = tline->next;
2327 if (tline && tline->type == TOK_WHITESPACE)
2328 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002329 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2330 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002331 free_tlist(origline);
2332 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002333
H. Peter Anvine2c80182005-01-15 22:15:51 +00002334 case PP_CLEAR:
2335 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002336 error(ERR_WARNING|ERR_PASS1,
2337 "trailing garbage after `%%clear' ignored");
H. Peter Anvin97a23472007-09-16 17:57:25 -07002338 free_macros();
2339 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002340 free_tlist(origline);
2341 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002342
H. Peter Anvin418ca702008-05-30 10:42:30 -07002343 case PP_DEPEND:
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002344 t = tline->next = expand_smacro(tline->next);
2345 skip_white_(t);
2346 if (!t || (t->type != TOK_STRING &&
H. Peter Anvin89cee572009-07-15 09:16:54 -04002347 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002348 error(ERR_NONFATAL, "`%%depend' expects a file name");
2349 free_tlist(origline);
2350 return DIRECTIVE_FOUND; /* but we did _something_ */
2351 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002352 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002353 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002354 "trailing garbage after `%%depend' ignored");
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002355 p = t->text;
2356 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinf9c9a672009-07-14 15:14:05 -04002357 nasm_unquote_cstr(p, i);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002358 if (dephead && !in_list(*dephead, p)) {
2359 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2360 sl->next = NULL;
2361 strcpy(sl->str, p);
2362 *deptail = sl;
2363 deptail = &sl->next;
2364 }
2365 free_tlist(origline);
2366 return DIRECTIVE_FOUND;
2367
2368 case PP_INCLUDE:
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002369 t = tline->next = expand_smacro(tline->next);
2370 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002371
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002372 if (!t || (t->type != TOK_STRING &&
H. Peter Anvin89cee572009-07-15 09:16:54 -04002373 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002374 error(ERR_NONFATAL, "`%%include' expects a file name");
2375 free_tlist(origline);
2376 return DIRECTIVE_FOUND; /* but we did _something_ */
2377 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002378 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002379 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002380 "trailing garbage after `%%include' ignored");
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002381 p = t->text;
2382 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinf9c9a672009-07-14 15:14:05 -04002383 nasm_unquote_cstr(p, i);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002384 inc = nasm_malloc(sizeof(Include));
2385 inc->next = istk;
2386 inc->conds = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002387 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002388 if (!inc->fp) {
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002389 /* -MG given but file not found */
2390 nasm_free(inc);
2391 } else {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002392 inc->fname = src_set_fname(nasm_strdup(p));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002393 inc->lineno = src_set_linnum(0);
2394 inc->lineinc = 1;
2395 inc->expansion = NULL;
2396 inc->mstk = NULL;
2397 istk = inc;
2398 list->uplevel(LIST_INCLUDE);
2399 }
2400 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002401 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002402
H. Peter Anvind2456592008-06-19 15:04:18 -07002403 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002404 {
H. Peter Anvina70547f2008-07-19 21:44:26 -07002405 static macros_t *use_pkg;
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07002406 const char *pkg_macro;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002407
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002408 tline = tline->next;
2409 skip_white_(tline);
2410 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002411
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002412 if (!tline || (tline->type != TOK_STRING &&
2413 tline->type != TOK_INTERNAL_STRING &&
2414 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002415 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002416 free_tlist(origline);
2417 return DIRECTIVE_FOUND; /* but we did _something_ */
2418 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002419 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002420 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002421 "trailing garbage after `%%use' ignored");
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002422 if (tline->type == TOK_STRING)
H. Peter Anvinf9c9a672009-07-14 15:14:05 -04002423 nasm_unquote_cstr(tline->text, i);
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002424 use_pkg = nasm_stdmac_find_package(tline->text);
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002425 if (!use_pkg)
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002426 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07002427 /* The first string will be <%define>__USE_*__ */
H. Peter Anvin7e50d232008-06-25 14:54:14 -07002428 pkg_macro = (char *)use_pkg + 1;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002429 if (!smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
2430 /* Not already included, go ahead and include it */
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002431 stdmacpos = use_pkg;
2432 }
H. Peter Anvind2456592008-06-19 15:04:18 -07002433 free_tlist(origline);
2434 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002435 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002436 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002437 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002438 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002439 tline = tline->next;
2440 skip_white_(tline);
2441 tline = expand_id(tline);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002442 if (tline) {
2443 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin42b56392008-10-24 16:24:21 -07002444 error(ERR_NONFATAL, "`%s' expects a context identifier",
2445 pp_directives[i]);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002446 free_tlist(origline);
2447 return DIRECTIVE_FOUND; /* but we did _something_ */
2448 }
2449 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002450 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin42b56392008-10-24 16:24:21 -07002451 "trailing garbage after `%s' ignored",
2452 pp_directives[i]);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002453 p = nasm_strdup(tline->text);
2454 } else {
H. Peter Anvin42b56392008-10-24 16:24:21 -07002455 p = NULL; /* Anonymous */
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002456 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002457
2458 if (i == PP_PUSH) {
2459 ctx = nasm_malloc(sizeof(Context));
2460 ctx->next = cstk;
2461 hash_init(&ctx->localmac, HASH_SMALL);
2462 ctx->name = p;
2463 ctx->number = unique++;
2464 cstk = ctx;
2465 } else {
2466 /* %pop or %repl */
2467 if (!cstk) {
2468 error(ERR_NONFATAL, "`%s': context stack is empty",
2469 pp_directives[i]);
2470 } else if (i == PP_POP) {
2471 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2472 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2473 "expected %s",
2474 cstk->name ? cstk->name : "anonymous", p);
2475 else
2476 ctx_pop();
2477 } else {
2478 /* i == PP_REPL */
2479 nasm_free(cstk->name);
2480 cstk->name = p;
2481 p = NULL;
2482 }
2483 nasm_free(p);
2484 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002485 free_tlist(origline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002486 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002487 case PP_FATAL:
H. Peter Anvin931cab62009-07-07 16:06:21 -07002488 severity = ERR_FATAL;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002489 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002490 case PP_ERROR:
H. Peter Anvin931cab62009-07-07 16:06:21 -07002491 severity = ERR_NONFATAL;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002492 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002493 case PP_WARNING:
H. Peter Anvin931cab62009-07-07 16:06:21 -07002494 severity = ERR_WARNING|ERR_WARN_USER;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002495 goto issue_error;
2496
2497 issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002498 {
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002499 /* Only error out if this is the final pass */
2500 if (pass != 2 && i != PP_FATAL)
2501 return DIRECTIVE_FOUND;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002502
2503 tline->next = expand_smacro(tline->next);
2504 tline = tline->next;
2505 skip_white_(tline);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002506 t = tline ? tline->next : NULL;
2507 skip_white_(t);
H. Peter Anvin89cee572009-07-15 09:16:54 -04002508 if (tok_type_(tline, TOK_STRING) && !t) {
H. Peter Anvin7df04172008-06-10 18:27:38 -07002509 /* The line contains only a quoted string */
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002510 p = tline->text;
H. Peter Anvinf9c9a672009-07-14 15:14:05 -04002511 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin931cab62009-07-07 16:06:21 -07002512 error(severity, "%s", p);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002513 } else {
2514 /* Not a quoted string, or more than a quoted string */
H. Peter Anvin89cee572009-07-15 09:16:54 -04002515 p = detoken(tline, false);
H. Peter Anvin931cab62009-07-07 16:06:21 -07002516 error(severity, "%s", p);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002517 nasm_free(p);
H. Peter Anvind2456592008-06-19 15:04:18 -07002518 }
H. Peter Anvin89cee572009-07-15 09:16:54 -04002519 free_tlist(origline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002520 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002521 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002522
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002523 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002524 if (istk->conds && !emitting(istk->conds->state))
2525 j = COND_NEVER;
2526 else {
2527 j = if_condition(tline->next, i);
2528 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002529 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2530 }
2531 cond = nasm_malloc(sizeof(Cond));
2532 cond->next = istk->conds;
2533 cond->state = j;
2534 istk->conds = cond;
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002535 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002536 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002537
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002538 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002539 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00002540 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002541 switch(istk->conds->state) {
H. Peter Anvin89cee572009-07-15 09:16:54 -04002542 case COND_IF_TRUE:
2543 istk->conds->state = COND_DONE;
2544 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002545
H. Peter Anvin89cee572009-07-15 09:16:54 -04002546 case COND_DONE:
2547 case COND_NEVER:
2548 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002549
H. Peter Anvin89cee572009-07-15 09:16:54 -04002550 case COND_ELSE_TRUE:
2551 case COND_ELSE_FALSE:
2552 error_precond(ERR_WARNING|ERR_PASS1,
2553 "`%%elif' after `%%else' ignored");
2554 istk->conds->state = COND_NEVER;
2555 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002556
H. Peter Anvin89cee572009-07-15 09:16:54 -04002557 case COND_IF_FALSE:
2558 /*
2559 * IMPORTANT: In the case of %if, we will already have
2560 * called expand_mmac_params(); however, if we're
2561 * processing an %elif we must have been in a
2562 * non-emitting mode, which would have inhibited
2563 * the normal invocation of expand_mmac_params().
2564 * Therefore, we have to do it explicitly here.
2565 */
2566 j = if_condition(expand_mmac_params(tline->next), i);
2567 tline->next = NULL; /* it got freed */
2568 istk->conds->state =
2569 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2570 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002571 }
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002572 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002573 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002574
H. Peter Anvine2c80182005-01-15 22:15:51 +00002575 case PP_ELSE:
2576 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002577 error_precond(ERR_WARNING|ERR_PASS1,
2578 "trailing garbage after `%%else' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002579 if (!istk->conds)
2580 error(ERR_FATAL, "`%%else': no matching `%%if'");
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002581 switch(istk->conds->state) {
H. Peter Anvin89cee572009-07-15 09:16:54 -04002582 case COND_IF_TRUE:
2583 case COND_DONE:
2584 istk->conds->state = COND_ELSE_FALSE;
2585 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002586
H. Peter Anvin89cee572009-07-15 09:16:54 -04002587 case COND_NEVER:
2588 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002589
H. Peter Anvin89cee572009-07-15 09:16:54 -04002590 case COND_IF_FALSE:
2591 istk->conds->state = COND_ELSE_TRUE;
2592 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002593
H. Peter Anvin89cee572009-07-15 09:16:54 -04002594 case COND_ELSE_TRUE:
2595 case COND_ELSE_FALSE:
2596 error_precond(ERR_WARNING|ERR_PASS1,
2597 "`%%else' after `%%else' ignored.");
2598 istk->conds->state = COND_NEVER;
2599 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002600 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002601 free_tlist(origline);
2602 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002603
H. Peter Anvine2c80182005-01-15 22:15:51 +00002604 case PP_ENDIF:
2605 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002606 error_precond(ERR_WARNING|ERR_PASS1,
2607 "trailing garbage after `%%endif' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002608 if (!istk->conds)
2609 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2610 cond = istk->conds;
2611 istk->conds = cond->next;
2612 nasm_free(cond);
2613 free_tlist(origline);
2614 return DIRECTIVE_FOUND;
Keith Kanios0af5ee22009-07-11 14:26:34 -05002615
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002616 case PP_RMACRO:
2617 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002618 case PP_MACRO:
2619 case PP_IMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07002620 if (defining) {
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002621 error(ERR_FATAL, "`%s': already defining a macro",
2622 pp_directives[i]);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002623 return DIRECTIVE_FOUND;
2624 }
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002625 defining = nasm_malloc(sizeof(MMacro));
2626 defining->max_depth =
2627 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2628 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002629 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2630 nasm_free(defining);
2631 defining = NULL;
2632 return DIRECTIVE_FOUND;
2633 }
2634
H. Peter Anvin166c2472008-05-28 12:28:58 -07002635 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002636 while (mmac) {
2637 if (!strcmp(mmac->name, defining->name) &&
2638 (mmac->nparam_min <= defining->nparam_max
2639 || defining->plus)
2640 && (defining->nparam_min <= mmac->nparam_max
2641 || mmac->plus)) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07002642 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002643 "redefining multi-line macro `%s'", defining->name);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002644 return DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002645 }
2646 mmac = mmac->next;
2647 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002648 free_tlist(origline);
2649 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002650
H. Peter Anvine2c80182005-01-15 22:15:51 +00002651 case PP_ENDM:
2652 case PP_ENDMACRO:
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002653 if (! (defining && defining->name)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002654 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2655 return DIRECTIVE_FOUND;
2656 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002657 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002658 defining->next = *mmhead;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002659 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002660 defining = NULL;
2661 free_tlist(origline);
2662 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002663
H. Peter Anvin89cee572009-07-15 09:16:54 -04002664 case PP_EXITMACRO:
Keith Kanios852f1ee2009-07-12 00:19:55 -05002665 /*
2666 * We must search along istk->expansion until we hit a
2667 * macro-end marker for a macro with a name. Then we set
2668 * its `in_progress' flag to 0.
2669 */
H. Peter Anvin89cee572009-07-15 09:16:54 -04002670 for (l = istk->expansion; l; l = l->next)
Keith Kanios852f1ee2009-07-12 00:19:55 -05002671 if (l->finishes && l->finishes->name)
2672 break;
2673
2674 if (l) {
H. Peter Anvin89cee572009-07-15 09:16:54 -04002675 l->finishes->in_progress = 0;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002676 } else {
2677 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
H. Peter Anvin89cee572009-07-15 09:16:54 -04002678 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002679 free_tlist(origline);
2680 return DIRECTIVE_FOUND;
2681
H. Peter Anvina26433d2008-07-16 14:40:01 -07002682 case PP_UNMACRO:
2683 case PP_UNIMACRO:
2684 {
2685 MMacro **mmac_p;
2686 MMacro spec;
2687
2688 spec.casesense = (i == PP_UNMACRO);
2689 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2690 return DIRECTIVE_FOUND;
2691 }
2692 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2693 while (mmac_p && *mmac_p) {
2694 mmac = *mmac_p;
2695 if (mmac->casesense == spec.casesense &&
2696 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2697 mmac->nparam_min == spec.nparam_min &&
2698 mmac->nparam_max == spec.nparam_max &&
2699 mmac->plus == spec.plus) {
2700 *mmac_p = mmac->next;
2701 free_mmacro(mmac);
2702 } else {
2703 mmac_p = &mmac->next;
2704 }
2705 }
2706 free_tlist(origline);
2707 free_tlist(spec.dlist);
2708 return DIRECTIVE_FOUND;
2709 }
2710
H. Peter Anvine2c80182005-01-15 22:15:51 +00002711 case PP_ROTATE:
2712 if (tline->next && tline->next->type == TOK_WHITESPACE)
2713 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002714 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002715 free_tlist(origline);
2716 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2717 return DIRECTIVE_FOUND;
2718 }
2719 t = expand_smacro(tline->next);
2720 tline->next = NULL;
2721 free_tlist(origline);
2722 tline = t;
2723 tptr = &t;
2724 tokval.t_type = TOKEN_INVALID;
2725 evalresult =
2726 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2727 free_tlist(tline);
2728 if (!evalresult)
2729 return DIRECTIVE_FOUND;
2730 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002731 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002732 "trailing garbage after expression ignored");
2733 if (!is_simple(evalresult)) {
2734 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2735 return DIRECTIVE_FOUND;
2736 }
2737 mmac = istk->mstk;
2738 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2739 mmac = mmac->next_active;
2740 if (!mmac) {
2741 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2742 } else if (mmac->nparam == 0) {
2743 error(ERR_NONFATAL,
2744 "`%%rotate' invoked within macro without parameters");
2745 } else {
H. Peter Anvin25a99342007-09-22 17:45:45 -07002746 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002747
H. Peter Anvin25a99342007-09-22 17:45:45 -07002748 rotate %= (int)mmac->nparam;
2749 if (rotate < 0)
2750 rotate += mmac->nparam;
H. Peter Anvin70653092007-10-19 14:42:29 -07002751
H. Peter Anvin25a99342007-09-22 17:45:45 -07002752 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002753 }
2754 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002755
H. Peter Anvine2c80182005-01-15 22:15:51 +00002756 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002757 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002758 do {
2759 tline = tline->next;
2760 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002761
H. Peter Anvine2c80182005-01-15 22:15:51 +00002762 if (tok_type_(tline, TOK_ID) &&
2763 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002764 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002765 do {
2766 tline = tline->next;
2767 } while (tok_type_(tline, TOK_WHITESPACE));
2768 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002769
H. Peter Anvine2c80182005-01-15 22:15:51 +00002770 if (tline) {
2771 t = expand_smacro(tline);
2772 tptr = &t;
2773 tokval.t_type = TOKEN_INVALID;
2774 evalresult =
2775 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2776 if (!evalresult) {
2777 free_tlist(origline);
2778 return DIRECTIVE_FOUND;
2779 }
2780 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002781 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002782 "trailing garbage after expression ignored");
2783 if (!is_simple(evalresult)) {
2784 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2785 return DIRECTIVE_FOUND;
2786 }
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002787 count = reloc_value(evalresult) + 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002788 } else {
2789 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002790 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002791 }
2792 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002793
H. Peter Anvine2c80182005-01-15 22:15:51 +00002794 tmp_defining = defining;
2795 defining = nasm_malloc(sizeof(MMacro));
H. Peter Anvin89cee572009-07-15 09:16:54 -04002796 defining->prev = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002797 defining->name = NULL; /* flags this macro as a %rep block */
H. Peter Anvin70055962007-10-11 00:05:31 -07002798 defining->casesense = false;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002799 defining->plus = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002800 defining->nolist = nolist;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002801 defining->in_progress = count;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002802 defining->max_depth = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002803 defining->nparam_min = defining->nparam_max = 0;
2804 defining->defaults = NULL;
2805 defining->dlist = NULL;
2806 defining->expansion = NULL;
2807 defining->next_active = istk->mstk;
2808 defining->rep_nest = tmp_defining;
2809 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002810
H. Peter Anvine2c80182005-01-15 22:15:51 +00002811 case PP_ENDREP:
2812 if (!defining || defining->name) {
2813 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2814 return DIRECTIVE_FOUND;
2815 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002816
H. Peter Anvine2c80182005-01-15 22:15:51 +00002817 /*
2818 * Now we have a "macro" defined - although it has no name
2819 * and we won't be entering it in the hash tables - we must
2820 * push a macro-end marker for it on to istk->expansion.
2821 * After that, it will take care of propagating itself (a
2822 * macro-end marker line for a macro which is really a %rep
2823 * block will cause the macro to be re-expanded, complete
2824 * with another macro-end marker to ensure the process
2825 * continues) until the whole expansion is forcibly removed
2826 * from istk->expansion by a %exitrep.
2827 */
2828 l = nasm_malloc(sizeof(Line));
2829 l->next = istk->expansion;
2830 l->finishes = defining;
2831 l->first = NULL;
2832 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002833
H. Peter Anvine2c80182005-01-15 22:15:51 +00002834 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002835
H. Peter Anvine2c80182005-01-15 22:15:51 +00002836 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2837 tmp_defining = defining;
2838 defining = defining->rep_nest;
2839 free_tlist(origline);
2840 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002841
H. Peter Anvine2c80182005-01-15 22:15:51 +00002842 case PP_EXITREP:
2843 /*
2844 * We must search along istk->expansion until we hit a
2845 * macro-end marker for a macro with no name. Then we set
2846 * its `in_progress' flag to 0.
2847 */
2848 for (l = istk->expansion; l; l = l->next)
2849 if (l->finishes && !l->finishes->name)
H. Peter Anvinca348b62008-07-23 10:49:26 -04002850 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002851
H. Peter Anvine2c80182005-01-15 22:15:51 +00002852 if (l)
Charles Crayned4200be2008-07-12 16:42:33 -07002853 l->finishes->in_progress = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002854 else
2855 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2856 free_tlist(origline);
2857 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002858
H. Peter Anvine2c80182005-01-15 22:15:51 +00002859 case PP_XDEFINE:
2860 case PP_IXDEFINE:
2861 case PP_DEFINE:
2862 case PP_IDEFINE:
H. Peter Anvin95e7f952007-10-11 13:38:38 -07002863 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002864
H. Peter Anvine2c80182005-01-15 22:15:51 +00002865 tline = tline->next;
2866 skip_white_(tline);
2867 tline = expand_id(tline);
2868 if (!tline || (tline->type != TOK_ID &&
2869 (tline->type != TOK_PREPROC_ID ||
2870 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002871 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2872 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002873 free_tlist(origline);
2874 return DIRECTIVE_FOUND;
2875 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002876
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002877 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002878 last = tline;
2879 param_start = tline = tline->next;
2880 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002881
H. Peter Anvine2c80182005-01-15 22:15:51 +00002882 /* Expand the macro definition now for %xdefine and %ixdefine */
2883 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2884 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002885
H. Peter Anvine2c80182005-01-15 22:15:51 +00002886 if (tok_is_(tline, "(")) {
2887 /*
2888 * This macro has parameters.
2889 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002890
H. Peter Anvine2c80182005-01-15 22:15:51 +00002891 tline = tline->next;
2892 while (1) {
2893 skip_white_(tline);
2894 if (!tline) {
2895 error(ERR_NONFATAL, "parameter identifier expected");
2896 free_tlist(origline);
2897 return DIRECTIVE_FOUND;
2898 }
2899 if (tline->type != TOK_ID) {
2900 error(ERR_NONFATAL,
2901 "`%s': parameter identifier expected",
2902 tline->text);
2903 free_tlist(origline);
2904 return DIRECTIVE_FOUND;
2905 }
2906 tline->type = TOK_SMAC_PARAM + nparam++;
2907 tline = tline->next;
2908 skip_white_(tline);
2909 if (tok_is_(tline, ",")) {
2910 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04002911 } else {
2912 if (!tok_is_(tline, ")")) {
2913 error(ERR_NONFATAL,
2914 "`)' expected to terminate macro template");
2915 free_tlist(origline);
2916 return DIRECTIVE_FOUND;
2917 }
2918 break;
2919 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002920 }
2921 last = tline;
2922 tline = tline->next;
2923 }
2924 if (tok_type_(tline, TOK_WHITESPACE))
2925 last = tline, tline = tline->next;
2926 macro_start = NULL;
2927 last->next = NULL;
2928 t = tline;
2929 while (t) {
2930 if (t->type == TOK_ID) {
2931 for (tt = param_start; tt; tt = tt->next)
2932 if (tt->type >= TOK_SMAC_PARAM &&
2933 !strcmp(tt->text, t->text))
2934 t->type = tt->type;
2935 }
2936 tt = t->next;
2937 t->next = macro_start;
2938 macro_start = t;
2939 t = tt;
2940 }
2941 /*
2942 * Good. We now have a macro name, a parameter count, and a
2943 * token list (in reverse order) for an expansion. We ought
2944 * to be OK just to create an SMacro, store it, and let
2945 * free_tlist have the rest of the line (which we have
2946 * carefully re-terminated after chopping off the expansion
2947 * from the end).
2948 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002949 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002950 free_tlist(origline);
2951 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002952
H. Peter Anvine2c80182005-01-15 22:15:51 +00002953 case PP_UNDEF:
2954 tline = tline->next;
2955 skip_white_(tline);
2956 tline = expand_id(tline);
2957 if (!tline || (tline->type != TOK_ID &&
2958 (tline->type != TOK_PREPROC_ID ||
2959 tline->text[1] != '$'))) {
2960 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2961 free_tlist(origline);
2962 return DIRECTIVE_FOUND;
2963 }
2964 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07002965 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002966 "trailing garbage after macro name ignored");
2967 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002968
H. Peter Anvine2c80182005-01-15 22:15:51 +00002969 /* Find the context that symbol belongs to */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002970 ctx = get_ctx(tline->text, &mname, false);
2971 undef_smacro(ctx, mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002972 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002973 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002974
H. Peter Anvin9e200162008-06-04 17:23:14 -07002975 case PP_DEFSTR:
2976 case PP_IDEFSTR:
2977 casesense = (i == PP_DEFSTR);
2978
2979 tline = tline->next;
2980 skip_white_(tline);
2981 tline = expand_id(tline);
2982 if (!tline || (tline->type != TOK_ID &&
2983 (tline->type != TOK_PREPROC_ID ||
2984 tline->text[1] != '$'))) {
2985 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2986 pp_directives[i]);
2987 free_tlist(origline);
2988 return DIRECTIVE_FOUND;
2989 }
2990
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002991 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07002992 last = tline;
2993 tline = expand_smacro(tline->next);
2994 last->next = NULL;
2995
2996 while (tok_type_(tline, TOK_WHITESPACE))
2997 tline = delete_Token(tline);
2998
2999 p = detoken(tline, false);
3000 macro_start = nasm_malloc(sizeof(*macro_start));
3001 macro_start->next = NULL;
3002 macro_start->text = nasm_quote(p, strlen(p));
3003 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003004 macro_start->a.mac = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003005 nasm_free(p);
3006
3007 /*
3008 * We now have a macro name, an implicit parameter count of
3009 * zero, and a string token to use as an expansion. Create
3010 * and store an SMacro.
3011 */
3012 define_smacro(ctx, mname, casesense, 0, macro_start);
3013 free_tlist(origline);
3014 return DIRECTIVE_FOUND;
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003015
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003016 case PP_DEFTOK:
3017 case PP_IDEFTOK:
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003018 casesense = (i == PP_DEFTOK);
H. Peter Anvin89cee572009-07-15 09:16:54 -04003019
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003020 tline = tline->next;
3021 skip_white_(tline);
3022 tline = expand_id(tline);
3023 if (!tline || (tline->type != TOK_ID &&
3024 (tline->type != TOK_PREPROC_ID ||
3025 tline->text[1] != '$'))) {
3026 error(ERR_NONFATAL,
3027 "`%s' expects a macro identifier as first parameter",
H. Peter Anvin89cee572009-07-15 09:16:54 -04003028 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003029 free_tlist(origline);
3030 return DIRECTIVE_FOUND;
3031 }
3032 ctx = get_ctx(tline->text, &mname, false);
3033 last = tline;
3034 tline = expand_smacro(tline->next);
3035 last->next = NULL;
3036
3037 t = tline;
3038 while (tok_type_(t, TOK_WHITESPACE))
3039 t = t->next;
3040 /* t should now point to the string */
3041 if (t->type != TOK_STRING) {
3042 error(ERR_NONFATAL,
3043 "`%s` requires string as second parameter",
H. Peter Anvin89cee572009-07-15 09:16:54 -04003044 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003045 free_tlist(tline);
3046 free_tlist(origline);
3047 return DIRECTIVE_FOUND;
3048 }
3049
H. Peter Anvinf9c9a672009-07-14 15:14:05 -04003050 nasm_unquote_cstr(t->text, i);
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003051 macro_start = tokenize(t->text);
H. Peter Anvin89cee572009-07-15 09:16:54 -04003052
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003053 /*
3054 * We now have a macro name, an implicit parameter count of
3055 * zero, and a numeric token to use as an expansion. Create
3056 * and store an SMacro.
3057 */
3058 define_smacro(ctx, mname, casesense, 0, macro_start);
3059 free_tlist(tline);
3060 free_tlist(origline);
3061 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003062
H. Peter Anvin418ca702008-05-30 10:42:30 -07003063 case PP_PATHSEARCH:
3064 {
3065 FILE *fp;
3066 StrList *xsl = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07003067 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003068
3069 casesense = true;
3070
3071 tline = tline->next;
3072 skip_white_(tline);
3073 tline = expand_id(tline);
3074 if (!tline || (tline->type != TOK_ID &&
3075 (tline->type != TOK_PREPROC_ID ||
3076 tline->text[1] != '$'))) {
3077 error(ERR_NONFATAL,
3078 "`%%pathsearch' expects a macro identifier as first parameter");
3079 free_tlist(origline);
3080 return DIRECTIVE_FOUND;
3081 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003082 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003083 last = tline;
3084 tline = expand_smacro(tline->next);
3085 last->next = NULL;
3086
3087 t = tline;
3088 while (tok_type_(t, TOK_WHITESPACE))
3089 t = t->next;
3090
3091 if (!t || (t->type != TOK_STRING &&
3092 t->type != TOK_INTERNAL_STRING)) {
3093 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
3094 free_tlist(tline);
3095 free_tlist(origline);
3096 return DIRECTIVE_FOUND; /* but we did _something_ */
3097 }
3098 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003099 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003100 "trailing garbage after `%%pathsearch' ignored");
H. Peter Anvin427cc912008-06-01 21:43:03 -07003101 p = t->text;
3102 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003103 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003104
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07003105 fp = inc_fopen(p, &xsl, &xst, true);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003106 if (fp) {
3107 p = xsl->str;
3108 fclose(fp); /* Don't actually care about the file */
3109 }
3110 macro_start = nasm_malloc(sizeof(*macro_start));
3111 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003112 macro_start->text = nasm_quote(p, strlen(p));
H. Peter Anvin418ca702008-05-30 10:42:30 -07003113 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003114 macro_start->a.mac = NULL;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003115 if (xsl)
3116 nasm_free(xsl);
3117
3118 /*
3119 * We now have a macro name, an implicit parameter count of
3120 * zero, and a string token to use as an expansion. Create
3121 * and store an SMacro.
3122 */
3123 define_smacro(ctx, mname, casesense, 0, macro_start);
3124 free_tlist(tline);
3125 free_tlist(origline);
3126 return DIRECTIVE_FOUND;
3127 }
3128
H. Peter Anvine2c80182005-01-15 22:15:51 +00003129 case PP_STRLEN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003130 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003131
H. Peter Anvine2c80182005-01-15 22:15:51 +00003132 tline = tline->next;
3133 skip_white_(tline);
3134 tline = expand_id(tline);
3135 if (!tline || (tline->type != TOK_ID &&
3136 (tline->type != TOK_PREPROC_ID ||
3137 tline->text[1] != '$'))) {
3138 error(ERR_NONFATAL,
3139 "`%%strlen' expects a macro identifier as first parameter");
3140 free_tlist(origline);
3141 return DIRECTIVE_FOUND;
3142 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003143 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003144 last = tline;
3145 tline = expand_smacro(tline->next);
3146 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003147
H. Peter Anvine2c80182005-01-15 22:15:51 +00003148 t = tline;
3149 while (tok_type_(t, TOK_WHITESPACE))
3150 t = t->next;
3151 /* t should now point to the string */
3152 if (t->type != TOK_STRING) {
3153 error(ERR_NONFATAL,
3154 "`%%strlen` requires string as second parameter");
3155 free_tlist(tline);
3156 free_tlist(origline);
3157 return DIRECTIVE_FOUND;
3158 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003159
H. Peter Anvine2c80182005-01-15 22:15:51 +00003160 macro_start = nasm_malloc(sizeof(*macro_start));
3161 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003162 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003163 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003164
H. Peter Anvine2c80182005-01-15 22:15:51 +00003165 /*
3166 * We now have a macro name, an implicit parameter count of
3167 * zero, and a numeric token to use as an expansion. Create
3168 * and store an SMacro.
3169 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003170 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003171 free_tlist(tline);
3172 free_tlist(origline);
3173 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003174
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003175 case PP_STRCAT:
3176 casesense = true;
3177
3178 tline = tline->next;
3179 skip_white_(tline);
3180 tline = expand_id(tline);
3181 if (!tline || (tline->type != TOK_ID &&
3182 (tline->type != TOK_PREPROC_ID ||
3183 tline->text[1] != '$'))) {
3184 error(ERR_NONFATAL,
3185 "`%%strcat' expects a macro identifier as first parameter");
3186 free_tlist(origline);
3187 return DIRECTIVE_FOUND;
3188 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003189 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003190 last = tline;
3191 tline = expand_smacro(tline->next);
3192 last->next = NULL;
3193
3194 len = 0;
3195 for (t = tline; t; t = t->next) {
3196 switch (t->type) {
3197 case TOK_WHITESPACE:
3198 break;
3199 case TOK_STRING:
3200 len += t->a.len = nasm_unquote(t->text, NULL);
3201 break;
H. Peter Anvinaccf4332008-07-01 21:42:08 -07003202 case TOK_OTHER:
3203 if (!strcmp(t->text, ",")) /* permit comma separators */
3204 break;
3205 /* else fall through */
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003206 default:
3207 error(ERR_NONFATAL,
3208 "non-string passed to `%%strcat' (%d)", t->type);
3209 free_tlist(tline);
3210 free_tlist(origline);
3211 return DIRECTIVE_FOUND;
3212 }
3213 }
3214
3215 p = pp = nasm_malloc(len);
3216 t = tline;
3217 for (t = tline; t; t = t->next) {
3218 if (t->type == TOK_STRING) {
3219 memcpy(p, t->text, t->a.len);
3220 p += t->a.len;
3221 }
3222 }
3223
3224 /*
3225 * We now have a macro name, an implicit parameter count of
3226 * zero, and a numeric token to use as an expansion. Create
3227 * and store an SMacro.
3228 */
3229 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3230 macro_start->text = nasm_quote(pp, len);
3231 nasm_free(pp);
3232 define_smacro(ctx, mname, casesense, 0, macro_start);
3233 free_tlist(tline);
3234 free_tlist(origline);
3235 return DIRECTIVE_FOUND;
3236
H. Peter Anvine2c80182005-01-15 22:15:51 +00003237 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003238 {
3239 int64_t a1, a2;
3240 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003241
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003242 casesense = true;
3243
H. Peter Anvine2c80182005-01-15 22:15:51 +00003244 tline = tline->next;
3245 skip_white_(tline);
3246 tline = expand_id(tline);
3247 if (!tline || (tline->type != TOK_ID &&
3248 (tline->type != TOK_PREPROC_ID ||
3249 tline->text[1] != '$'))) {
3250 error(ERR_NONFATAL,
3251 "`%%substr' expects a macro identifier as first parameter");
3252 free_tlist(origline);
3253 return DIRECTIVE_FOUND;
3254 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003255 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003256 last = tline;
3257 tline = expand_smacro(tline->next);
3258 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003259
H. Peter Anvine2c80182005-01-15 22:15:51 +00003260 t = tline->next;
3261 while (tok_type_(t, TOK_WHITESPACE))
3262 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003263
H. Peter Anvine2c80182005-01-15 22:15:51 +00003264 /* t should now point to the string */
3265 if (t->type != TOK_STRING) {
3266 error(ERR_NONFATAL,
3267 "`%%substr` requires string as second parameter");
3268 free_tlist(tline);
3269 free_tlist(origline);
3270 return DIRECTIVE_FOUND;
3271 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003272
H. Peter Anvine2c80182005-01-15 22:15:51 +00003273 tt = t->next;
3274 tptr = &tt;
3275 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003276 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3277 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003278 if (!evalresult) {
3279 free_tlist(tline);
3280 free_tlist(origline);
3281 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003282 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003283 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3284 free_tlist(tline);
3285 free_tlist(origline);
3286 return DIRECTIVE_FOUND;
3287 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003288 a1 = evalresult->value-1;
3289
3290 while (tok_type_(tt, TOK_WHITESPACE))
3291 tt = tt->next;
3292 if (!tt) {
3293 a2 = 1; /* Backwards compatibility: one character */
3294 } else {
3295 tokval.t_type = TOKEN_INVALID;
3296 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3297 pass, error, NULL);
3298 if (!evalresult) {
3299 free_tlist(tline);
3300 free_tlist(origline);
3301 return DIRECTIVE_FOUND;
3302 } else if (!is_simple(evalresult)) {
3303 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3304 free_tlist(tline);
3305 free_tlist(origline);
3306 return DIRECTIVE_FOUND;
3307 }
3308 a2 = evalresult->value;
3309 }
3310
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003311 len = nasm_unquote(t->text, NULL);
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003312 if (a2 < 0)
3313 a2 = a2+1+len-a1;
3314 if (a1+a2 > (int64_t)len)
3315 a2 = len-a1;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003316
H. Peter Anvine2c80182005-01-15 22:15:51 +00003317 macro_start = nasm_malloc(sizeof(*macro_start));
3318 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003319 macro_start->text = nasm_quote((a1 < 0) ? "" : t->text+a1, a2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003320 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003321 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003322
H. Peter Anvine2c80182005-01-15 22:15:51 +00003323 /*
3324 * We now have a macro name, an implicit parameter count of
3325 * zero, and a numeric token to use as an expansion. Create
3326 * and store an SMacro.
3327 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003328 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003329 free_tlist(tline);
3330 free_tlist(origline);
3331 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003332 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003333
H. Peter Anvine2c80182005-01-15 22:15:51 +00003334 case PP_ASSIGN:
3335 case PP_IASSIGN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003336 casesense = (i == PP_ASSIGN);
3337
H. Peter Anvine2c80182005-01-15 22:15:51 +00003338 tline = tline->next;
3339 skip_white_(tline);
3340 tline = expand_id(tline);
3341 if (!tline || (tline->type != TOK_ID &&
3342 (tline->type != TOK_PREPROC_ID ||
3343 tline->text[1] != '$'))) {
3344 error(ERR_NONFATAL,
3345 "`%%%sassign' expects a macro identifier",
3346 (i == PP_IASSIGN ? "i" : ""));
3347 free_tlist(origline);
3348 return DIRECTIVE_FOUND;
3349 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003350 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003351 last = tline;
3352 tline = expand_smacro(tline->next);
3353 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003354
H. Peter Anvine2c80182005-01-15 22:15:51 +00003355 t = tline;
3356 tptr = &t;
3357 tokval.t_type = TOKEN_INVALID;
3358 evalresult =
3359 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3360 free_tlist(tline);
3361 if (!evalresult) {
3362 free_tlist(origline);
3363 return DIRECTIVE_FOUND;
3364 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003365
H. Peter Anvine2c80182005-01-15 22:15:51 +00003366 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003367 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003368 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003369
H. Peter Anvine2c80182005-01-15 22:15:51 +00003370 if (!is_simple(evalresult)) {
3371 error(ERR_NONFATAL,
3372 "non-constant value given to `%%%sassign'",
3373 (i == PP_IASSIGN ? "i" : ""));
3374 free_tlist(origline);
3375 return DIRECTIVE_FOUND;
3376 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003377
H. Peter Anvine2c80182005-01-15 22:15:51 +00003378 macro_start = nasm_malloc(sizeof(*macro_start));
3379 macro_start->next = NULL;
3380 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003381 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003382
H. Peter Anvine2c80182005-01-15 22:15:51 +00003383 /*
3384 * We now have a macro name, an implicit parameter count of
3385 * zero, and a numeric token to use as an expansion. Create
3386 * and store an SMacro.
3387 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003388 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003389 free_tlist(origline);
3390 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003391
H. Peter Anvine2c80182005-01-15 22:15:51 +00003392 case PP_LINE:
3393 /*
3394 * Syntax is `%line nnn[+mmm] [filename]'
3395 */
3396 tline = tline->next;
3397 skip_white_(tline);
3398 if (!tok_type_(tline, TOK_NUMBER)) {
3399 error(ERR_NONFATAL, "`%%line' expects line number");
3400 free_tlist(origline);
3401 return DIRECTIVE_FOUND;
3402 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003403 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003404 m = 1;
3405 tline = tline->next;
3406 if (tok_is_(tline, "+")) {
3407 tline = tline->next;
3408 if (!tok_type_(tline, TOK_NUMBER)) {
3409 error(ERR_NONFATAL, "`%%line' expects line increment");
3410 free_tlist(origline);
3411 return DIRECTIVE_FOUND;
3412 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003413 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003414 tline = tline->next;
3415 }
3416 skip_white_(tline);
3417 src_set_linnum(k);
3418 istk->lineinc = m;
3419 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003420 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003421 }
3422 free_tlist(origline);
3423 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003424
H. Peter Anvine2c80182005-01-15 22:15:51 +00003425 default:
3426 error(ERR_FATAL,
3427 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003428 pp_directives[i]);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003429 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003430 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003431}
3432
3433/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003434 * Ensure that a macro parameter contains a condition code and
3435 * nothing else. Return the condition code index if so, or -1
3436 * otherwise.
3437 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003438static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003439{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003440 Token *tt;
3441 int i, j, k, m;
3442
H. Peter Anvin25a99342007-09-22 17:45:45 -07003443 if (!t)
H. Peter Anvin89cee572009-07-15 09:16:54 -04003444 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003445
H. Peter Anvineba20a72002-04-30 20:53:55 +00003446 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003447 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003448 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003449 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003450 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003451 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003452 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003453
3454 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +00003455 j = elements(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003456 while (j - i > 1) {
3457 k = (j + i) / 2;
3458 m = nasm_stricmp(t->text, conditions[k]);
3459 if (m == 0) {
3460 i = k;
3461 j = -2;
3462 break;
3463 } else if (m < 0) {
3464 j = k;
3465 } else
3466 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003467 }
3468 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003469 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003470 return i;
3471}
3472
H. Peter Anvind784a082009-04-20 14:01:18 -07003473static bool paste_tokens(Token **head, bool handle_paste_tokens)
3474{
3475 Token **tail, *t, *tt;
3476 Token **paste_head;
3477 bool did_paste = false;
3478 char *tmp;
3479
3480 /* Now handle token pasting... */
3481 paste_head = NULL;
3482 tail = head;
3483 while ((t = *tail) && (tt = t->next)) {
3484 switch (t->type) {
3485 case TOK_WHITESPACE:
3486 if (tt->type == TOK_WHITESPACE) {
3487 /* Zap adjacent whitespace tokens */
3488 t->next = delete_Token(tt);
3489 } else {
3490 /* Do not advance paste_head here */
3491 tail = &t->next;
3492 }
3493 break;
3494 case TOK_ID:
3495 case TOK_PREPROC_ID:
3496 case TOK_NUMBER:
3497 case TOK_FLOAT:
3498 {
3499 size_t len = 0;
3500 char *tmp, *p;
3501
3502 while (tt && (tt->type == TOK_ID || tt->type == TOK_PREPROC_ID ||
3503 tt->type == TOK_NUMBER || tt->type == TOK_FLOAT ||
3504 tt->type == TOK_OTHER)) {
3505 len += strlen(tt->text);
3506 tt = tt->next;
3507 }
3508
3509 /* Now tt points to the first token after the potential
3510 paste area... */
3511 if (tt != t->next) {
3512 /* We have at least two tokens... */
3513 len += strlen(t->text);
3514 p = tmp = nasm_malloc(len+1);
3515
3516 while (t != tt) {
3517 strcpy(p, t->text);
3518 p = strchr(p, '\0');
3519 t = delete_Token(t);
3520 }
3521
3522 t = *tail = tokenize(tmp);
3523 nasm_free(tmp);
3524
3525 while (t->next) {
3526 tail = &t->next;
3527 t = t->next;
3528 }
3529 t->next = tt; /* Attach the remaining token chain */
3530
3531 did_paste = true;
3532 }
3533 paste_head = tail;
3534 tail = &t->next;
3535 break;
3536 }
3537 case TOK_PASTE: /* %+ */
3538 if (handle_paste_tokens) {
3539 /* Zap %+ and whitespace tokens to the right */
3540 while (t && (t->type == TOK_WHITESPACE ||
3541 t->type == TOK_PASTE))
3542 t = *tail = delete_Token(t);
3543 if (!paste_head || !t)
3544 break; /* Nothing to paste with */
3545 tail = paste_head;
3546 t = *tail;
3547 tt = t->next;
3548 while (tok_type_(tt, TOK_WHITESPACE))
3549 tt = t->next = delete_Token(tt);
3550
3551 if (tt) {
3552 tmp = nasm_strcat(t->text, tt->text);
3553 delete_Token(t);
3554 tt = delete_Token(tt);
3555 t = *tail = tokenize(tmp);
3556 nasm_free(tmp);
3557 while (t->next) {
3558 tail = &t->next;
3559 t = t->next;
3560 }
3561 t->next = tt; /* Attach the remaining token chain */
3562 did_paste = true;
3563 }
3564 paste_head = tail;
3565 tail = &t->next;
3566 break;
3567 }
3568 /* else fall through */
3569 default:
3570 tail = paste_head = &t->next;
3571 break;
3572 }
3573 }
3574 return did_paste;
3575}
H. Peter Anvin76690a12002-04-30 20:52:49 +00003576/*
3577 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003578 * %-n) and MMacro-local identifiers (%%foo) as well as
3579 * macro indirection (%[...]).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003580 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003581static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003582{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003583 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003584 bool changed = false;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003585
3586 tail = &thead;
3587 thead = NULL;
3588
H. Peter Anvine2c80182005-01-15 22:15:51 +00003589 while (tline) {
3590 if (tline->type == TOK_PREPROC_ID &&
3591 (((tline->text[1] == '+' || tline->text[1] == '-')
3592 && tline->text[2]) || tline->text[1] == '%'
3593 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003594 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003595 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003596 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003597 unsigned int n;
3598 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003599 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003600
H. Peter Anvine2c80182005-01-15 22:15:51 +00003601 t = tline;
3602 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003603
H. Peter Anvine2c80182005-01-15 22:15:51 +00003604 mac = istk->mstk;
3605 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3606 mac = mac->next_active;
3607 if (!mac)
3608 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
3609 else
3610 switch (t->text[1]) {
3611 /*
3612 * We have to make a substitution of one of the
3613 * forms %1, %-1, %+1, %%foo, %0.
3614 */
3615 case '0':
3616 type = TOK_NUMBER;
3617 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3618 text = nasm_strdup(tmpbuf);
3619 break;
3620 case '%':
3621 type = TOK_ID;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003622 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvine2c80182005-01-15 22:15:51 +00003623 mac->unique);
3624 text = nasm_strcat(tmpbuf, t->text + 2);
3625 break;
3626 case '-':
3627 n = atoi(t->text + 2) - 1;
3628 if (n >= mac->nparam)
3629 tt = NULL;
3630 else {
3631 if (mac->nparam > 1)
3632 n = (n + mac->rotate) % mac->nparam;
3633 tt = mac->params[n];
3634 }
3635 cc = find_cc(tt);
3636 if (cc == -1) {
3637 error(ERR_NONFATAL,
3638 "macro parameter %d is not a condition code",
3639 n + 1);
3640 text = NULL;
3641 } else {
3642 type = TOK_ID;
3643 if (inverse_ccs[cc] == -1) {
3644 error(ERR_NONFATAL,
3645 "condition code `%s' is not invertible",
3646 conditions[cc]);
3647 text = NULL;
3648 } else
H. Peter Anvin67c63722008-10-26 23:49:00 -07003649 text = nasm_strdup(conditions[inverse_ccs[cc]]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003650 }
3651 break;
3652 case '+':
3653 n = atoi(t->text + 2) - 1;
3654 if (n >= mac->nparam)
3655 tt = NULL;
3656 else {
3657 if (mac->nparam > 1)
3658 n = (n + mac->rotate) % mac->nparam;
3659 tt = mac->params[n];
3660 }
3661 cc = find_cc(tt);
3662 if (cc == -1) {
3663 error(ERR_NONFATAL,
3664 "macro parameter %d is not a condition code",
3665 n + 1);
3666 text = NULL;
3667 } else {
3668 type = TOK_ID;
3669 text = nasm_strdup(conditions[cc]);
3670 }
3671 break;
3672 default:
3673 n = atoi(t->text + 1) - 1;
3674 if (n >= mac->nparam)
3675 tt = NULL;
3676 else {
3677 if (mac->nparam > 1)
3678 n = (n + mac->rotate) % mac->nparam;
3679 tt = mac->params[n];
3680 }
3681 if (tt) {
3682 for (i = 0; i < mac->paramlen[n]; i++) {
3683 *tail = new_Token(NULL, tt->type, tt->text, 0);
3684 tail = &(*tail)->next;
3685 tt = tt->next;
3686 }
3687 }
3688 text = NULL; /* we've done it here */
3689 break;
3690 }
3691 if (!text) {
3692 delete_Token(t);
3693 } else {
3694 *tail = t;
3695 tail = &t->next;
3696 t->type = type;
3697 nasm_free(t->text);
3698 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003699 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003700 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07003701 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003702 continue;
H. Peter Anvin67c63722008-10-26 23:49:00 -07003703 } else if (tline->type == TOK_INDIRECT) {
3704 t = tline;
3705 tline = tline->next;
3706 tt = tokenize(t->text);
3707 tt = expand_mmac_params(tt);
3708 tt = expand_smacro(tt);
3709 *tail = tt;
3710 while (tt) {
3711 tt->a.mac = NULL; /* Necessary? */
3712 tail = &tt->next;
3713 tt = tt->next;
3714 }
3715 delete_Token(t);
H. Peter Anvin6125b622009-04-08 14:02:25 -07003716 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003717 } else {
3718 t = *tail = tline;
3719 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003720 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003721 tail = &t->next;
3722 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003723 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003724 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07003725
H. Peter Anvind784a082009-04-20 14:01:18 -07003726 if (changed)
H. Peter Anvinfc30f8c2009-07-06 18:48:23 -07003727 paste_tokens(&thead, false);
H. Peter Anvin6125b622009-04-08 14:02:25 -07003728
H. Peter Anvin76690a12002-04-30 20:52:49 +00003729 return thead;
3730}
3731
3732/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003733 * Expand all single-line macro calls made in the given line.
3734 * Return the expanded version of the line. The original is deemed
3735 * to be destroyed in the process. (In reality we'll just move
3736 * Tokens from input to output a lot of the time, rather than
3737 * actually bothering to destroy and replicate.)
3738 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003739
H. Peter Anvine2c80182005-01-15 22:15:51 +00003740static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003741{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003742 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003743 struct hash_table *smtbl;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003744 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003745 Token **params;
3746 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07003747 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07003748 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003749 Token *org_tline = tline;
3750 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003751 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003752 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07003753 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003754
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003755 /*
3756 * Trick: we should avoid changing the start token pointer since it can
3757 * be contained in "next" field of other token. Because of this
3758 * we allocate a copy of first token and work with it; at the end of
3759 * routine we copy it back
3760 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003761 if (org_tline) {
3762 tline =
3763 new_Token(org_tline->next, org_tline->type, org_tline->text,
3764 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003765 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003766 nasm_free(org_tline->text);
3767 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003768 }
3769
H. Peter Anvin8287daf2009-07-07 16:00:58 -07003770 expanded = true; /* Always expand %+ at least once */
3771
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003772again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003773 tail = &thead;
3774 thead = NULL;
3775
H. Peter Anvine2c80182005-01-15 22:15:51 +00003776 while (tline) { /* main token loop */
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003777 if (!--deadman) {
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003778 error(ERR_NONFATAL, "interminable macro recursion");
3779 break;
3780 }
3781
H. Peter Anvine2c80182005-01-15 22:15:51 +00003782 if ((mname = tline->text)) {
3783 /* if this token is a local macro, look in local context */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003784 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID)
3785 ctx = get_ctx(mname, &mname, true);
3786 else
3787 ctx = NULL;
3788 smtbl = ctx ? &ctx->localmac : &smacros;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003789 head = (SMacro *) hash_findix(smtbl, mname);
H. Peter Anvin072771e2008-05-22 13:17:51 -07003790
H. Peter Anvine2c80182005-01-15 22:15:51 +00003791 /*
3792 * We've hit an identifier. As in is_mmacro below, we first
3793 * check whether the identifier is a single-line macro at
3794 * all, then think about checking for parameters if
3795 * necessary.
3796 */
H. Peter Anvin97a23472007-09-16 17:57:25 -07003797 for (m = head; m; m = m->next)
3798 if (!mstrcmp(m->name, mname, m->casesense))
3799 break;
3800 if (m) {
3801 mstart = tline;
3802 params = NULL;
3803 paramsize = NULL;
3804 if (m->nparam == 0) {
3805 /*
3806 * Simple case: the macro is parameterless. Discard the
3807 * one token that the macro call took, and push the
3808 * expansion back on the to-do stack.
3809 */
3810 if (!m->expansion) {
3811 if (!strcmp("__FILE__", m->name)) {
3812 int32_t num = 0;
H. Peter Anvin932de6c2008-07-31 18:46:11 -07003813 char *file = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003814 src_get(&num, &file);
3815 tline->text = nasm_quote(file, strlen(file));
H. Peter Anvin97a23472007-09-16 17:57:25 -07003816 tline->type = TOK_STRING;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003817 nasm_free(file);
H. Peter Anvin97a23472007-09-16 17:57:25 -07003818 continue;
3819 }
3820 if (!strcmp("__LINE__", m->name)) {
3821 nasm_free(tline->text);
3822 make_tok_num(tline, src_get_linnum());
3823 continue;
3824 }
3825 if (!strcmp("__BITS__", m->name)) {
3826 nasm_free(tline->text);
3827 make_tok_num(tline, globalbits);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003828 continue;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003829 }
3830 tline = delete_Token(tline);
3831 continue;
3832 }
3833 } else {
3834 /*
3835 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00003836 * exists and takes parameters. We must find the
3837 * parameters in the call, count them, find the SMacro
3838 * that corresponds to that form of the macro call, and
3839 * substitute for the parameters when we expand. What a
3840 * pain.
3841 */
3842 /*tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003843 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003844 do {
3845 t = tline->next;
3846 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003847 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003848 t->text = NULL;
3849 t = tline->next = delete_Token(t);
3850 }
3851 tline = t;
3852 } while (tok_type_(tline, TOK_WHITESPACE));
3853 if (!tok_is_(tline, "(")) {
3854 /*
3855 * This macro wasn't called with parameters: ignore
3856 * the call. (Behaviour borrowed from gnu cpp.)
3857 */
3858 tline = mstart;
3859 m = NULL;
3860 } else {
3861 int paren = 0;
3862 int white = 0;
3863 brackets = 0;
3864 nparam = 0;
3865 sparam = PARAM_DELTA;
3866 params = nasm_malloc(sparam * sizeof(Token *));
3867 params[0] = tline->next;
3868 paramsize = nasm_malloc(sparam * sizeof(int));
3869 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003870 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003871 /*
3872 * For some unusual expansions
3873 * which concatenates function call
3874 */
3875 t = tline->next;
3876 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003877 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003878 t->text = NULL;
3879 t = tline->next = delete_Token(t);
3880 }
3881 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003882
H. Peter Anvine2c80182005-01-15 22:15:51 +00003883 if (!tline) {
3884 error(ERR_NONFATAL,
3885 "macro call expects terminating `)'");
3886 break;
3887 }
3888 if (tline->type == TOK_WHITESPACE
3889 && brackets <= 0) {
3890 if (paramsize[nparam])
3891 white++;
3892 else
3893 params[nparam] = tline->next;
3894 continue; /* parameter loop */
3895 }
3896 if (tline->type == TOK_OTHER
3897 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003898 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003899 if (ch == ',' && !paren && brackets <= 0) {
3900 if (++nparam >= sparam) {
3901 sparam += PARAM_DELTA;
3902 params = nasm_realloc(params,
3903 sparam *
3904 sizeof(Token
3905 *));
3906 paramsize =
3907 nasm_realloc(paramsize,
3908 sparam *
3909 sizeof(int));
3910 }
3911 params[nparam] = tline->next;
3912 paramsize[nparam] = 0;
3913 white = 0;
3914 continue; /* parameter loop */
3915 }
3916 if (ch == '{' &&
3917 (brackets > 0 || (brackets == 0 &&
3918 !paramsize[nparam])))
3919 {
3920 if (!(brackets++)) {
3921 params[nparam] = tline->next;
3922 continue; /* parameter loop */
3923 }
3924 }
3925 if (ch == '}' && brackets > 0)
3926 if (--brackets == 0) {
3927 brackets = -1;
3928 continue; /* parameter loop */
3929 }
3930 if (ch == '(' && !brackets)
3931 paren++;
3932 if (ch == ')' && brackets <= 0)
3933 if (--paren < 0)
3934 break;
3935 }
3936 if (brackets < 0) {
3937 brackets = 0;
3938 error(ERR_NONFATAL, "braces do not "
3939 "enclose all of macro parameter");
3940 }
3941 paramsize[nparam] += white + 1;
3942 white = 0;
3943 } /* parameter loop */
3944 nparam++;
3945 while (m && (m->nparam != nparam ||
3946 mstrcmp(m->name, mname,
3947 m->casesense)))
3948 m = m->next;
3949 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003950 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003951 "macro `%s' exists, "
3952 "but not taking %d parameters",
3953 mstart->text, nparam);
3954 }
3955 }
3956 if (m && m->in_progress)
3957 m = NULL;
3958 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07003959 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00003960 * Design question: should we handle !tline, which
3961 * indicates missing ')' here, or expand those
3962 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07003963 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00003964 */
3965 nasm_free(params);
3966 nasm_free(paramsize);
3967 tline = mstart;
3968 } else {
3969 /*
3970 * Expand the macro: we are placed on the last token of the
3971 * call, so that we can easily split the call from the
3972 * following tokens. We also start by pushing an SMAC_END
3973 * token for the cycle removal.
3974 */
3975 t = tline;
3976 if (t) {
3977 tline = t->next;
3978 t->next = NULL;
3979 }
3980 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003981 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003982 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003983 tline = tt;
3984 for (t = m->expansion; t; t = t->next) {
3985 if (t->type >= TOK_SMAC_PARAM) {
3986 Token *pcopy = tline, **ptail = &pcopy;
3987 Token *ttt, *pt;
3988 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003989
H. Peter Anvine2c80182005-01-15 22:15:51 +00003990 ttt = params[t->type - TOK_SMAC_PARAM];
3991 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3992 --i >= 0;) {
3993 pt = *ptail =
3994 new_Token(tline, ttt->type, ttt->text,
3995 0);
3996 ptail = &pt->next;
3997 ttt = ttt->next;
3998 }
3999 tline = pcopy;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -07004000 } else if (t->type == TOK_PREPROC_Q) {
4001 tt = new_Token(tline, TOK_ID, mname, 0);
4002 tline = tt;
4003 } else if (t->type == TOK_PREPROC_QQ) {
4004 tt = new_Token(tline, TOK_ID, m->name, 0);
4005 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004006 } else {
4007 tt = new_Token(tline, t->type, t->text, 0);
4008 tline = tt;
4009 }
4010 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004011
H. Peter Anvine2c80182005-01-15 22:15:51 +00004012 /*
4013 * Having done that, get rid of the macro call, and clean
4014 * up the parameters.
4015 */
4016 nasm_free(params);
4017 nasm_free(paramsize);
4018 free_tlist(mstart);
H. Peter Anvind784a082009-04-20 14:01:18 -07004019 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004020 continue; /* main token loop */
4021 }
4022 }
4023 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004024
H. Peter Anvine2c80182005-01-15 22:15:51 +00004025 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004026 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004027 tline = delete_Token(tline);
4028 } else {
4029 t = *tail = tline;
4030 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004031 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004032 t->next = NULL;
4033 tail = &t->next;
4034 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004035 }
4036
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004037 /*
4038 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004039 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004040 * TOK_IDs should be concatenated.
4041 * Also we look for %+ tokens and concatenate the tokens before and after
4042 * them (without white spaces in between).
4043 */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004044 if (expanded && paste_tokens(&thead, true)) {
H. Peter Anvinfc30f8c2009-07-06 18:48:23 -07004045 /*
4046 * If we concatenated something, *and* we had previously expanded
4047 * an actual macro, scan the lines again for macros...
4048 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004049 tline = thead;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004050 expanded = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004051 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004052 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004053
H. Peter Anvine2c80182005-01-15 22:15:51 +00004054 if (org_tline) {
4055 if (thead) {
4056 *org_tline = *thead;
4057 /* since we just gave text to org_line, don't free it */
4058 thead->text = NULL;
4059 delete_Token(thead);
4060 } else {
4061 /* the expression expanded to empty line;
4062 we can't return NULL for some reasons
4063 we just set the line to a single WHITESPACE token. */
4064 memset(org_tline, 0, sizeof(*org_tline));
4065 org_tline->text = NULL;
4066 org_tline->type = TOK_WHITESPACE;
4067 }
4068 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004069 }
4070
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004071 return thead;
4072}
4073
4074/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004075 * Similar to expand_smacro but used exclusively with macro identifiers
4076 * right before they are fetched in. The reason is that there can be
4077 * identifiers consisting of several subparts. We consider that if there
4078 * are more than one element forming the name, user wants a expansion,
4079 * otherwise it will be left as-is. Example:
4080 *
4081 * %define %$abc cde
4082 *
4083 * the identifier %$abc will be left as-is so that the handler for %define
4084 * will suck it and define the corresponding value. Other case:
4085 *
4086 * %define _%$abc cde
4087 *
4088 * In this case user wants name to be expanded *before* %define starts
4089 * working, so we'll expand %$abc into something (if it has a value;
4090 * otherwise it will be left as-is) then concatenate all successive
4091 * PP_IDs into one.
4092 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004093static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004094{
4095 Token *cur, *oldnext = NULL;
4096
H. Peter Anvin734b1882002-04-30 21:01:08 +00004097 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004098 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004099
4100 cur = tline;
4101 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00004102 (cur->next->type == TOK_ID ||
4103 cur->next->type == TOK_PREPROC_ID
4104 || cur->next->type == TOK_NUMBER))
4105 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004106
4107 /* If identifier consists of just one token, don't expand */
4108 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004109 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004110
H. Peter Anvine2c80182005-01-15 22:15:51 +00004111 if (cur) {
4112 oldnext = cur->next; /* Detach the tail past identifier */
4113 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004114 }
4115
H. Peter Anvin734b1882002-04-30 21:01:08 +00004116 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004117
H. Peter Anvine2c80182005-01-15 22:15:51 +00004118 if (cur) {
4119 /* expand_smacro possibly changhed tline; re-scan for EOL */
4120 cur = tline;
4121 while (cur && cur->next)
4122 cur = cur->next;
4123 if (cur)
4124 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004125 }
4126
4127 return tline;
4128}
4129
4130/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004131 * Determine whether the given line constitutes a multi-line macro
4132 * call, and return the MMacro structure called if so. Doesn't have
4133 * to check for an initial label - that's taken care of in
4134 * expand_mmacro - but must check numbers of parameters. Guaranteed
4135 * to be called with tline->type == TOK_ID, so the putative macro
4136 * name is easy to find.
4137 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004138static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004139{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004140 MMacro *head, *m;
4141 Token **params;
4142 int nparam;
4143
H. Peter Anvin166c2472008-05-28 12:28:58 -07004144 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004145
4146 /*
4147 * Efficiency: first we see if any macro exists with the given
4148 * name. If not, we can return NULL immediately. _Then_ we
4149 * count the parameters, and then we look further along the
4150 * list if necessary to find the proper MMacro.
4151 */
4152 for (m = head; m; m = m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004153 if (!mstrcmp(m->name, tline->text, m->casesense))
4154 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004155 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004156 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004157
4158 /*
4159 * OK, we have a potential macro. Count and demarcate the
4160 * parameters.
4161 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004162 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004163
4164 /*
4165 * So we know how many parameters we've got. Find the MMacro
4166 * structure that handles this number.
4167 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004168 while (m) {
4169 if (m->nparam_min <= nparam
4170 && (m->plus || nparam <= m->nparam_max)) {
4171 /*
4172 * This one is right. Just check if cycle removal
4173 * prohibits us using it before we actually celebrate...
4174 */
H. Peter Anvin89cee572009-07-15 09:16:54 -04004175 if (m->in_progress > m->max_depth) {
4176 if (m->max_depth > 0) {
4177 error(ERR_WARNING,
4178 "reached maximum recursion depth of %i",
4179 m->max_depth);
4180 }
4181 nasm_free(params);
4182 return NULL;
4183 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004184 /*
4185 * It's right, and we can use it. Add its default
4186 * parameters to the end of our list if necessary.
4187 */
4188 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
4189 params =
4190 nasm_realloc(params,
4191 ((m->nparam_min + m->ndefs +
4192 1) * sizeof(*params)));
4193 while (nparam < m->nparam_min + m->ndefs) {
4194 params[nparam] = m->defaults[nparam - m->nparam_min];
4195 nparam++;
4196 }
4197 }
4198 /*
4199 * If we've gone over the maximum parameter count (and
4200 * we're in Plus mode), ignore parameters beyond
4201 * nparam_max.
4202 */
4203 if (m->plus && nparam > m->nparam_max)
4204 nparam = m->nparam_max;
4205 /*
4206 * Then terminate the parameter list, and leave.
4207 */
4208 if (!params) { /* need this special case */
4209 params = nasm_malloc(sizeof(*params));
4210 nparam = 0;
4211 }
4212 params[nparam] = NULL;
4213 *params_array = params;
4214 return m;
4215 }
4216 /*
4217 * This one wasn't right: look for the next one with the
4218 * same name.
4219 */
4220 for (m = m->next; m; m = m->next)
4221 if (!mstrcmp(m->name, tline->text, m->casesense))
4222 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004223 }
4224
4225 /*
4226 * After all that, we didn't find one with the right number of
4227 * parameters. Issue a warning, and fail to expand the macro.
4228 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004229 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004230 "macro `%s' exists, but not taking %d parameters",
4231 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004232 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004233 return NULL;
4234}
4235
Keith Kanios891775e2009-07-11 06:08:54 -05004236
4237/*
4238 * Save MMacro invocation specific fields in
4239 * preparation for a recursive macro expansion
4240 */
4241static void push_mmacro(MMacro *m)
4242{
4243 MMacroInvocation *i;
4244
H. Peter Anvin89cee572009-07-15 09:16:54 -04004245 i = nasm_malloc(sizeof(MMacroInvocation));
4246 i->prev = m->prev;
4247 i->params = m->params;
4248 i->iline = m->iline;
4249 i->nparam = m->nparam;
4250 i->rotate = m->rotate;
4251 i->paramlen = m->paramlen;
4252 i->unique = m->unique;
4253 m->prev = i;
Keith Kanios891775e2009-07-11 06:08:54 -05004254}
4255
4256
4257/*
4258 * Restore MMacro invocation specific fields that were
4259 * saved during a previous recursive macro expansion
4260 */
4261static void pop_mmacro(MMacro *m)
4262{
4263 MMacroInvocation *i;
4264
H. Peter Anvin89cee572009-07-15 09:16:54 -04004265 if (m->prev) {
4266 i = m->prev;
4267 m->prev = i->prev;
4268 m->params = i->params;
4269 m->iline = i->iline;
4270 m->nparam = i->nparam;
4271 m->rotate = i->rotate;
4272 m->paramlen = i->paramlen;
4273 m->unique = i->unique;
4274 nasm_free(i);
4275 }
Keith Kanios891775e2009-07-11 06:08:54 -05004276}
4277
4278
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004279/*
4280 * Expand the multi-line macro call made by the given line, if
4281 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00004282 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004283 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004284static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004285{
4286 Token *startline = tline;
4287 Token *label = NULL;
4288 int dont_prepend = 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004289 Token **params, *t, *mtok, *tt;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004290 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004291 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004292 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004293 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004294
4295 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004296 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004297 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004298 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004299 return 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004300 mtok = t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004301 m = is_mmacro(t, &params);
H. Peter Anvinc751e862008-06-09 10:18:45 -07004302 if (m) {
4303 mname = t->text;
4304 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004305 Token *last;
4306 /*
4307 * We have an id which isn't a macro call. We'll assume
4308 * it might be a label; we'll also check to see if a
4309 * colon follows it. Then, if there's another id after
4310 * that lot, we'll check it again for macro-hood.
4311 */
4312 label = last = t;
4313 t = t->next;
4314 if (tok_type_(t, TOK_WHITESPACE))
4315 last = t, t = t->next;
4316 if (tok_is_(t, ":")) {
4317 dont_prepend = 1;
4318 last = t, t = t->next;
4319 if (tok_type_(t, TOK_WHITESPACE))
4320 last = t, t = t->next;
4321 }
H. Peter Anvin89cee572009-07-15 09:16:54 -04004322 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004323 return 0;
4324 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004325 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004326 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004327 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004328
4329 /*
4330 * Fix up the parameters: this involves stripping leading and
4331 * trailing whitespace, then stripping braces if they are
4332 * present.
4333 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004334 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004335 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004336
H. Peter Anvine2c80182005-01-15 22:15:51 +00004337 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004338 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004339 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004340
H. Peter Anvine2c80182005-01-15 22:15:51 +00004341 t = params[i];
4342 skip_white_(t);
4343 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004344 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004345 params[i] = t;
4346 paramlen[i] = 0;
4347 while (t) {
4348 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4349 break; /* ... because we have hit a comma */
4350 if (comma && t->type == TOK_WHITESPACE
4351 && tok_is_(t->next, ","))
4352 break; /* ... or a space then a comma */
4353 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4354 break; /* ... or a brace */
4355 t = t->next;
4356 paramlen[i]++;
4357 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004358 }
4359
4360 /*
4361 * OK, we have a MMacro structure together with a set of
4362 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00004363 * copies of each Line on to istk->expansion. Substitution of
4364 * parameter tokens and macro-local tokens doesn't get done
4365 * until the single-line macro substitution process; this is
4366 * because delaying them allows us to change the semantics
4367 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004368 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00004369 * First, push an end marker on to istk->expansion, mark this
4370 * macro as in progress, and set up its invocation-specific
4371 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004372 */
4373 ll = nasm_malloc(sizeof(Line));
4374 ll->next = istk->expansion;
4375 ll->finishes = m;
4376 ll->first = NULL;
4377 istk->expansion = ll;
Keith Kanios891775e2009-07-11 06:08:54 -05004378
H. Peter Anvin89cee572009-07-15 09:16:54 -04004379 /*
4380 * Save the previous MMacro expansion in the case of
4381 * macro recursion
4382 */
4383 if (m->max_depth && m->in_progress)
4384 push_mmacro(m);
H. Peter Anvin76690a12002-04-30 20:52:49 +00004385
Keith Kanios891775e2009-07-11 06:08:54 -05004386 m->in_progress ++;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004387 m->params = params;
4388 m->iline = tline;
4389 m->nparam = nparam;
4390 m->rotate = 0;
4391 m->paramlen = paramlen;
4392 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004393 m->lineno = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004394
4395 m->next_active = istk->mstk;
4396 istk->mstk = m;
4397
H. Peter Anvine2c80182005-01-15 22:15:51 +00004398 for (l = m->expansion; l; l = l->next) {
4399 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004400
H. Peter Anvine2c80182005-01-15 22:15:51 +00004401 ll = nasm_malloc(sizeof(Line));
4402 ll->finishes = NULL;
4403 ll->next = istk->expansion;
4404 istk->expansion = ll;
4405 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004406
H. Peter Anvine2c80182005-01-15 22:15:51 +00004407 for (t = l->first; t; t = t->next) {
4408 Token *x = t;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004409 switch (t->type) {
4410 case TOK_PREPROC_Q:
H. Peter Anvinc751e862008-06-09 10:18:45 -07004411 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004412 break;
4413 case TOK_PREPROC_QQ:
4414 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4415 break;
4416 case TOK_PREPROC_ID:
4417 if (t->text[1] == '0' && t->text[2] == '0') {
4418 dont_prepend = -1;
4419 x = label;
4420 if (!x)
4421 continue;
4422 }
4423 /* fall through */
4424 default:
4425 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4426 break;
H. Peter Anvin166c2472008-05-28 12:28:58 -07004427 }
H. Peter Anvince2233b2008-05-25 21:57:00 -07004428 tail = &tt->next;
4429 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004430 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004431 }
4432
4433 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004434 * If we had a label, push it on as the first line of
4435 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004436 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004437 if (label) {
4438 if (dont_prepend < 0)
4439 free_tlist(startline);
4440 else {
4441 ll = nasm_malloc(sizeof(Line));
4442 ll->finishes = NULL;
4443 ll->next = istk->expansion;
4444 istk->expansion = ll;
4445 ll->first = startline;
4446 if (!dont_prepend) {
4447 while (label->next)
4448 label = label->next;
4449 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4450 }
4451 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004452 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004453
H. Peter Anvin734b1882002-04-30 21:01:08 +00004454 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004455
H. Peter Anvineba20a72002-04-30 20:53:55 +00004456 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004457}
4458
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004459/* The function that actually does the error reporting */
4460static void verror(int severity, const char *fmt, va_list arg)
4461{
4462 char buff[1024];
4463
4464 vsnprintf(buff, sizeof(buff), fmt, arg);
4465
4466 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004467 _error(severity, "(%s:%d) %s", istk->mstk->name,
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004468 istk->mstk->lineno, buff);
4469 else
H. Peter Anvin917a3492008-09-24 09:14:49 -07004470 _error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004471}
4472
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004473/*
4474 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07004475 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004476 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004477static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004478{
4479 va_list arg;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004480
4481 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00004482 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004483 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004484
H. Peter Anvin734b1882002-04-30 21:01:08 +00004485 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004486 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004487 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004488}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004489
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004490/*
4491 * Because %else etc are evaluated in the state context
4492 * of the previous branch, errors might get lost with error():
4493 * %if 0 ... %else trailing garbage ... %endif
4494 * So %else etc should report errors with this function.
4495 */
4496static void error_precond(int severity, const char *fmt, ...)
4497{
4498 va_list arg;
4499
4500 /* Only ignore the error if it's really in a dead branch */
4501 if (istk && istk->conds && istk->conds->state == COND_NEVER)
4502 return;
4503
4504 va_start(arg, fmt);
4505 verror(severity, fmt, arg);
4506 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004507}
4508
H. Peter Anvin734b1882002-04-30 21:01:08 +00004509static void
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004510pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004511 ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004512{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004513 Token *t;
4514
H. Peter Anvin99941bf2002-05-14 17:44:03 +00004515 _error = errfunc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004516 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004517 istk = nasm_malloc(sizeof(Include));
4518 istk->next = NULL;
4519 istk->conds = NULL;
4520 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004521 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004522 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00004523 istk->fname = NULL;
4524 src_set_fname(nasm_strdup(file));
4525 src_set_linnum(0);
4526 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004527 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004528 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00004529 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004530 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004531 nested_mac_count = 0;
4532 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004533 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004534 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004535 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004536 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004537 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004538 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004539 }
H. Peter Anvind2456592008-06-19 15:04:18 -07004540 any_extrastdmac = extrastdmac && *extrastdmac;
4541 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004542 list = listgen;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004543 evaluate = eval;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004544
4545 /*
4546 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4547 * The caller, however, will also pass in 3 for preprocess-only so
4548 * we can set __PASS__ accordingly.
4549 */
4550 pass = apass > 2 ? 2 : apass;
4551
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004552 dephead = deptail = deplist;
4553 if (deplist) {
4554 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4555 sl->next = NULL;
4556 strcpy(sl->str, file);
4557 *deptail = sl;
4558 deptail = &sl->next;
4559 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07004560
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004561 /*
4562 * Define the __PASS__ macro. This is defined here unlike
4563 * all the other builtins, because it is special -- it varies between
4564 * passes.
4565 */
H. Peter Anvin7383b402008-09-24 10:20:40 -07004566 t = nasm_malloc(sizeof(*t));
4567 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004568 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07004569 t->a.mac = NULL;
4570 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004571}
4572
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004573static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004574{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004575 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004576 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004577
H. Peter Anvine2c80182005-01-15 22:15:51 +00004578 while (1) {
4579 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004580 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004581 * buffer or from the input file.
4582 */
4583 tline = NULL;
4584 while (istk->expansion && istk->expansion->finishes) {
4585 Line *l = istk->expansion;
4586 if (!l->finishes->name && l->finishes->in_progress > 1) {
4587 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004588
H. Peter Anvine2c80182005-01-15 22:15:51 +00004589 /*
4590 * This is a macro-end marker for a macro with no
4591 * name, which means it's not really a macro at all
4592 * but a %rep block, and the `in_progress' field is
4593 * more than 1, meaning that we still need to
4594 * repeat. (1 means the natural last repetition; 0
4595 * means termination by %exitrep.) We have
4596 * therefore expanded up to the %endrep, and must
4597 * push the whole block on to the expansion buffer
4598 * again. We don't bother to remove the macro-end
4599 * marker: we'd only have to generate another one
4600 * if we did.
4601 */
4602 l->finishes->in_progress--;
4603 for (l = l->finishes->expansion; l; l = l->next) {
4604 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004605
H. Peter Anvine2c80182005-01-15 22:15:51 +00004606 ll = nasm_malloc(sizeof(Line));
4607 ll->next = istk->expansion;
4608 ll->finishes = NULL;
4609 ll->first = NULL;
4610 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004611
H. Peter Anvine2c80182005-01-15 22:15:51 +00004612 for (t = l->first; t; t = t->next) {
4613 if (t->text || t->type == TOK_WHITESPACE) {
4614 tt = *tail =
4615 new_Token(NULL, t->type, t->text, 0);
4616 tail = &tt->next;
4617 }
4618 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004619
H. Peter Anvine2c80182005-01-15 22:15:51 +00004620 istk->expansion = ll;
4621 }
4622 } else {
4623 /*
4624 * Check whether a `%rep' was started and not ended
4625 * within this macro expansion. This can happen and
4626 * should be detected. It's a fatal error because
4627 * I'm too confused to work out how to recover
4628 * sensibly from it.
4629 */
4630 if (defining) {
4631 if (defining->name)
4632 error(ERR_PANIC,
4633 "defining with name in expansion");
4634 else if (istk->mstk->name)
4635 error(ERR_FATAL,
4636 "`%%rep' without `%%endrep' within"
4637 " expansion of macro `%s'",
4638 istk->mstk->name);
4639 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00004640
H. Peter Anvine2c80182005-01-15 22:15:51 +00004641 /*
4642 * FIXME: investigate the relationship at this point between
4643 * istk->mstk and l->finishes
4644 */
4645 {
4646 MMacro *m = istk->mstk;
4647 istk->mstk = m->next_active;
4648 if (m->name) {
4649 /*
4650 * This was a real macro call, not a %rep, and
4651 * therefore the parameter information needs to
4652 * be freed.
4653 */
H. Peter Anvin89cee572009-07-15 09:16:54 -04004654 if (m->prev) {
4655 pop_mmacro(m);
4656 l->finishes->in_progress --;
4657 } else {
Keith Kanios891775e2009-07-11 06:08:54 -05004658 nasm_free(m->params);
4659 free_tlist(m->iline);
H. Peter Anvin89cee572009-07-15 09:16:54 -04004660 nasm_free(m->paramlen);
4661 l->finishes->in_progress = 0;
4662 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004663 } else
4664 free_mmacro(m);
4665 }
4666 istk->expansion = l->next;
4667 nasm_free(l);
4668 list->downlevel(LIST_MACRO);
4669 }
4670 }
4671 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004672
H. Peter Anvine2c80182005-01-15 22:15:51 +00004673 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004674 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004675 Line *l = istk->expansion;
4676 if (istk->mstk)
4677 istk->mstk->lineno++;
4678 tline = l->first;
4679 istk->expansion = l->next;
4680 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004681 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004682 list->line(LIST_MACRO, p);
4683 nasm_free(p);
4684 break;
4685 }
4686 line = read_line();
4687 if (line) { /* from the current input file */
4688 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004689 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004690 nasm_free(line);
4691 break;
4692 }
4693 /*
4694 * The current file has ended; work down the istk
4695 */
4696 {
4697 Include *i = istk;
4698 fclose(i->fp);
4699 if (i->conds)
4700 error(ERR_FATAL,
4701 "expected `%%endif' before end of file");
4702 /* only set line and file name if there's a next node */
4703 if (i->next) {
4704 src_set_linnum(i->lineno);
4705 nasm_free(src_set_fname(i->fname));
H. Peter Anvin86877b22008-06-20 15:55:45 -07004706 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004707 istk = i->next;
4708 list->downlevel(LIST_INCLUDE);
4709 nasm_free(i);
4710 if (!istk)
4711 return NULL;
Victor van den Elzen4c9d6222008-10-01 13:08:50 +02004712 if (istk->expansion && istk->expansion->finishes)
4713 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004714 }
4715 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004716
H. Peter Anvine2c80182005-01-15 22:15:51 +00004717 /*
4718 * We must expand MMacro parameters and MMacro-local labels
4719 * _before_ we plunge into directive processing, to cope
4720 * with things like `%define something %1' such as STRUC
4721 * uses. Unless we're _defining_ a MMacro, in which case
4722 * those tokens should be left alone to go into the
4723 * definition; and unless we're in a non-emitting
4724 * condition, in which case we don't want to meddle with
4725 * anything.
4726 */
Charles Crayned4200be2008-07-12 16:42:33 -07004727 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin992fe752008-10-19 15:45:05 -07004728 && !(istk->mstk && !istk->mstk->in_progress)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004729 tline = expand_mmac_params(tline);
H. Peter Anvin992fe752008-10-19 15:45:05 -07004730 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004731
H. Peter Anvine2c80182005-01-15 22:15:51 +00004732 /*
4733 * Check the line to see if it's a preprocessor directive.
4734 */
4735 if (do_directive(tline) == DIRECTIVE_FOUND) {
4736 continue;
4737 } else if (defining) {
4738 /*
4739 * We're defining a multi-line macro. We emit nothing
4740 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00004741 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004742 */
4743 Line *l = nasm_malloc(sizeof(Line));
4744 l->next = defining->expansion;
4745 l->first = tline;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004746 l->finishes = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004747 defining->expansion = l;
4748 continue;
4749 } else if (istk->conds && !emitting(istk->conds->state)) {
4750 /*
4751 * We're in a non-emitting branch of a condition block.
4752 * Emit nothing at all, not even a blank line: when we
4753 * emerge from the condition we'll give a line-number
4754 * directive so we keep our place correctly.
4755 */
4756 free_tlist(tline);
4757 continue;
4758 } else if (istk->mstk && !istk->mstk->in_progress) {
4759 /*
4760 * We're in a %rep block which has been terminated, so
4761 * we're walking through to the %endrep without
4762 * emitting anything. Emit nothing at all, not even a
4763 * blank line: when we emerge from the %rep block we'll
4764 * give a line-number directive so we keep our place
4765 * correctly.
4766 */
4767 free_tlist(tline);
4768 continue;
4769 } else {
4770 tline = expand_smacro(tline);
4771 if (!expand_mmacro(tline)) {
4772 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004773 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004774 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004775 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004776 free_tlist(tline);
4777 break;
4778 } else {
4779 continue; /* expand_mmacro calls free_tlist */
4780 }
4781 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004782 }
4783
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004784 return line;
4785}
4786
H. Peter Anvine2c80182005-01-15 22:15:51 +00004787static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004788{
H. Peter Anvine2c80182005-01-15 22:15:51 +00004789 if (defining) {
H. Peter Anvin89cee572009-07-15 09:16:54 -04004790 if (defining->name) {
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02004791 error(ERR_NONFATAL,
4792 "end of file while still defining macro `%s'",
4793 defining->name);
4794 } else {
4795 error(ERR_NONFATAL, "end of file while still in %%rep");
4796 }
4797
H. Peter Anvine2c80182005-01-15 22:15:51 +00004798 free_mmacro(defining);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004799 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004800 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004801 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07004802 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00004803 while (istk) {
4804 Include *i = istk;
4805 istk = istk->next;
4806 fclose(i->fp);
4807 nasm_free(i->fname);
4808 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004809 }
4810 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004811 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07004812 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004813 if (pass == 0) {
H. Peter Anvin86877b22008-06-20 15:55:45 -07004814 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004815 free_llist(predef);
4816 delete_Blocks();
H. Peter Anvin86877b22008-06-20 15:55:45 -07004817 while ((i = ipath)) {
4818 ipath = i->next;
4819 if (i->path)
4820 nasm_free(i->path);
4821 nasm_free(i);
4822 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07004823 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004824}
4825
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004826void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004827{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004828 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004829
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004830 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004831 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004832 i->next = NULL;
4833
H. Peter Anvin89cee572009-07-15 09:16:54 -04004834 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004835 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04004836 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004837 j = j->next;
4838 j->next = i;
4839 } else {
4840 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004841 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004842}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004843
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004844void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004845{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004846 Token *inc, *space, *name;
4847 Line *l;
4848
H. Peter Anvin734b1882002-04-30 21:01:08 +00004849 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4850 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4851 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004852
4853 l = nasm_malloc(sizeof(Line));
4854 l->next = predef;
4855 l->first = inc;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004856 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004857 predef = l;
4858}
4859
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004860void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004861{
4862 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004863 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004864 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004865
4866 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00004867 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4868 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004869 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004870 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00004871 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004872 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004873 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004874
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004875 l = nasm_malloc(sizeof(Line));
4876 l->next = predef;
4877 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004878 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004879 predef = l;
4880}
4881
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004882void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00004883{
4884 Token *def, *space;
4885 Line *l;
4886
H. Peter Anvin734b1882002-04-30 21:01:08 +00004887 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4888 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004889 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00004890
4891 l = nasm_malloc(sizeof(Line));
4892 l->next = predef;
4893 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004894 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00004895 predef = l;
4896}
4897
Keith Kaniosb7a89542007-04-12 02:40:54 +00004898/*
4899 * Added by Keith Kanios:
4900 *
4901 * This function is used to assist with "runtime" preprocessor
4902 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4903 *
4904 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4905 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4906 */
4907
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004908void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00004909{
4910 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07004911
Keith Kaniosb7a89542007-04-12 02:40:54 +00004912 def = tokenize(definition);
H. Peter Anvin89cee572009-07-15 09:16:54 -04004913 if (do_directive(def) == NO_DIRECTIVE_FOUND)
Keith Kaniosb7a89542007-04-12 02:40:54 +00004914 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07004915
Keith Kaniosb7a89542007-04-12 02:40:54 +00004916}
4917
H. Peter Anvina70547f2008-07-19 21:44:26 -07004918void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004919{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004920 extrastdmac = macros;
4921}
4922
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004923static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004924{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004925 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004926 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004927 tok->text = nasm_strdup(numbuf);
4928 tok->type = TOK_NUMBER;
4929}
4930
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004931Preproc nasmpp = {
4932 pp_reset,
4933 pp_getline,
4934 pp_cleanup
4935};