blob: eb7902a8a8850befde2340125c210920c2f00a33 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002 *
3 * Copyright 1996-2010 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
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.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
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"
H. Peter Anvindbb640b2009-07-18 18:57:16 -070080#include "eval.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070081#include "tokens.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070082#include "tables.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000083
84typedef struct SMacro SMacro;
85typedef struct MMacro MMacro;
Keith Kanios891775e2009-07-11 06:08:54 -050086typedef struct MMacroInvocation MMacroInvocation;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000087typedef struct Context Context;
88typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000089typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000090typedef struct Line Line;
91typedef struct Include Include;
92typedef struct Cond Cond;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000093typedef struct IncPath IncPath;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000094
95/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070096 * Note on the storage of both SMacro and MMacros: the hash table
97 * indexes them case-insensitively, and we then have to go through a
98 * linked list of potential case aliases (and, for MMacros, parameter
99 * ranges); this is to preserve the matching semantics of the earlier
100 * code. If the number of case aliases for a specific macro is a
101 * performance issue, you may want to reconsider your coding style.
102 */
103
104/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000105 * Store the definition of a single-line macro.
106 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000107struct SMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000108 SMacro *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000109 char *name;
H. Peter Anvin70055962007-10-11 00:05:31 -0700110 bool casesense;
H. Peter Anvin16ed4382007-10-11 10:06:19 -0700111 bool in_progress;
H. Peter Anvin70055962007-10-11 00:05:31 -0700112 unsigned int nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000113 Token *expansion;
114};
115
116/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000117 * Store the definition of a multi-line macro. This is also used to
118 * store the interiors of `%rep...%endrep' blocks, which are
119 * effectively self-re-invoking multi-line macros which simply
120 * don't have a name or bother to appear in the hash tables. %rep
121 * blocks are signified by having a NULL `name' field.
122 *
123 * In a MMacro describing a `%rep' block, the `in_progress' field
124 * isn't merely boolean, but gives the number of repeats left to
125 * run.
126 *
127 * The `next' field is used for storing MMacros in hash tables; the
128 * `next_active' field is for stacking them on istk entries.
129 *
130 * When a MMacro is being expanded, `params', `iline', `nparam',
131 * `paramlen', `rotate' and `unique' are local to the invocation.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000132 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000133struct MMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000134 MMacro *next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300135 MMacroInvocation *prev; /* previous invocation */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000136 char *name;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -0700137 int nparam_min, nparam_max;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700138 bool casesense;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300139 bool plus; /* is the last parameter greedy? */
140 bool nolist; /* is this macro listing-inhibited? */
141 int64_t in_progress; /* is this macro currently being expanded? */
142 int32_t max_depth; /* maximum number of recursive expansions allowed */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000143 Token *dlist; /* All defaults as one list */
144 Token **defaults; /* Parameter default pointers */
145 int ndefs; /* number of default parameters */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000146 Line *expansion;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000147
148 MMacro *next_active;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000149 MMacro *rep_nest; /* used for nesting %rep */
150 Token **params; /* actual parameters */
151 Token *iline; /* invocation line */
H. Peter Anvin25a99342007-09-22 17:45:45 -0700152 unsigned int nparam, rotate;
153 int *paramlen;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -0700154 uint64_t unique;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000155 int lineno; /* Current line number on expansion */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300156 uint64_t condcnt; /* number of if blocks... */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000157};
158
Keith Kanios891775e2009-07-11 06:08:54 -0500159
160/* Store the definition of a multi-line macro, as defined in a
161 * previous recursive macro expansion.
162 */
163struct MMacroInvocation {
H. Peter Anvin89cee572009-07-15 09:16:54 -0400164 MMacroInvocation *prev; /* previous invocation */
165 Token **params; /* actual parameters */
166 Token *iline; /* invocation line */
Keith Kanios891775e2009-07-11 06:08:54 -0500167 unsigned int nparam, rotate;
168 int *paramlen;
169 uint64_t unique;
Keith Kanios3c0d91f2009-10-25 13:28:03 -0500170 uint64_t condcnt;
Keith Kanios891775e2009-07-11 06:08:54 -0500171};
172
173
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000174/*
175 * The context stack is composed of a linked list of these.
176 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000177struct Context {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000178 Context *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000179 char *name;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700180 struct hash_table localmac;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000181 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000182};
183
184/*
185 * This is the internal form which we break input lines up into.
186 * Typically stored in linked lists.
187 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000188 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
189 * necessarily used as-is, but is intended to denote the number of
190 * the substituted parameter. So in the definition
191 *
192 * %define a(x,y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700193 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000194 * the token representing `x' will have its type changed to
195 * TOK_SMAC_PARAM, but the one representing `y' will be
196 * TOK_SMAC_PARAM+1.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000197 *
198 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
199 * which doesn't need quotes around it. Used in the pre-include
200 * mechanism as an alternative to trying to find a sensible type of
201 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000202 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000203enum pp_token_type {
204 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
205 TOK_PREPROC_ID, TOK_STRING,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700206 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
207 TOK_INTERNAL_STRING,
208 TOK_PREPROC_Q, TOK_PREPROC_QQ,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300209 TOK_PASTE, /* %+ */
210 TOK_INDIRECT, /* %[...] */
211 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
212 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000213};
214
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400215#define PP_CONCAT_MASK(x) (1 << (x))
216
H. Peter Anvine2c80182005-01-15 22:15:51 +0000217struct Token {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000218 Token *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000219 char *text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -0700220 union {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300221 SMacro *mac; /* associated macro for TOK_SMAC_END */
222 size_t len; /* scratch length field */
223 } a; /* Auxiliary data */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000224 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000225};
226
227/*
228 * Multi-line macro definitions are stored as a linked list of
229 * these, which is essentially a container to allow several linked
230 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700231 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000232 * Note that in this module, linked lists are treated as stacks
233 * wherever possible. For this reason, Lines are _pushed_ on to the
234 * `expansion' field in MMacro structures, so that the linked list,
235 * if walked, would give the macro lines in reverse order; this
236 * means that we can walk the list when expanding a macro, and thus
237 * push the lines on to the `expansion' field in _istk_ in reverse
238 * order (so that when popped back off they are in the right
239 * order). It may seem cockeyed, and it relies on my design having
240 * an even number of steps in, but it works...
241 *
242 * Some of these structures, rather than being actual lines, are
243 * markers delimiting the end of the expansion of a given macro.
H. Peter Anvin76690a12002-04-30 20:52:49 +0000244 * This is for use in the cycle-tracking and %rep-handling code.
245 * Such structures have `finishes' non-NULL, and `first' NULL. All
246 * others have `finishes' NULL, but `first' may still be NULL if
247 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000248 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000249struct Line {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000250 Line *next;
251 MMacro *finishes;
252 Token *first;
253};
254
255/*
256 * To handle an arbitrary level of file inclusion, we maintain a
257 * stack (ie linked list) of these things.
258 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000259struct Include {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000260 Include *next;
261 FILE *fp;
262 Cond *conds;
263 Line *expansion;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000264 char *fname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000265 int lineno, lineinc;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300266 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000267};
268
269/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000270 * Include search path. This is simply a list of strings which get
271 * prepended, in turn, to the name of an include file, in an
272 * attempt to find the file if it's not in the current directory.
273 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000274struct IncPath {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000275 IncPath *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000276 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000277};
278
279/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000280 * Conditional assembly: we maintain a separate stack of these for
281 * each level of file inclusion. (The only reason we keep the
282 * stacks separate is to ensure that a stray `%endif' in a file
283 * included from within the true branch of a `%if' won't terminate
284 * it and cause confusion: instead, rightly, it'll cause an error.)
285 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000286struct Cond {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000287 Cond *next;
288 int state;
289};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000290enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000291 /*
292 * These states are for use just after %if or %elif: IF_TRUE
293 * means the condition has evaluated to truth so we are
294 * currently emitting, whereas IF_FALSE means we are not
295 * currently emitting but will start doing so if a %else comes
296 * up. In these states, all directives are admissible: %elif,
297 * %else and %endif. (And of course %if.)
298 */
299 COND_IF_TRUE, COND_IF_FALSE,
300 /*
301 * These states come up after a %else: ELSE_TRUE means we're
302 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
303 * any %elif or %else will cause an error.
304 */
305 COND_ELSE_TRUE, COND_ELSE_FALSE,
306 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200307 * These states mean that we're not emitting now, and also that
308 * nothing until %endif will be emitted at all. COND_DONE is
309 * used when we've had our moment of emission
310 * and have now started seeing %elifs. COND_NEVER is used when
311 * the condition construct in question is contained within a
312 * non-emitting branch of a larger condition construct,
313 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000314 */
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200315 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000316};
317#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
318
H. Peter Anvin70653092007-10-19 14:42:29 -0700319/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000320 * These defines are used as the possible return values for do_directive
321 */
322#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300323#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000324
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000325/*
Keith Kanios852f1ee2009-07-12 00:19:55 -0500326 * This define sets the upper limit for smacro and recursive mmacro
327 * expansions
328 */
329#define DEADMAN_LIMIT (1 << 20)
330
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +0400331/* max reps */
332#define REP_LIMIT ((INT64_C(1) << 62))
333
Keith Kanios852f1ee2009-07-12 00:19:55 -0500334/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000335 * Condition codes. Note that we use c_ prefix not C_ because C_ is
336 * used in nasm.h for the "real" condition codes. At _this_ level,
337 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
338 * ones, so we need a different enum...
339 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700340static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000341 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
342 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000343 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000344};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700345enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000346 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
347 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 -0700348 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
349 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000350};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700351static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000352 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
353 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 +0000354 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000355};
356
H. Peter Anvin76690a12002-04-30 20:52:49 +0000357/*
358 * Directive names.
359 */
H. Peter Anvin65747262002-05-07 00:10:05 +0000360/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
H. Peter Anvin9bf0aa72007-09-12 02:12:07 +0000361static int is_condition(enum preproc_token arg)
H. Peter Anvin65747262002-05-07 00:10:05 +0000362{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000363 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
H. Peter Anvin65747262002-05-07 00:10:05 +0000364}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000365
366/* For TASM compatibility we need to be able to recognise TASM compatible
367 * conditional compilation directives. Using the NASM pre-processor does
368 * not work, so we look for them specifically from the following list and
369 * then jam in the equivalent NASM directive into the input stream.
370 */
371
H. Peter Anvine2c80182005-01-15 22:15:51 +0000372enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000373 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
374 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
375};
376
H. Peter Anvin476d2862007-10-02 22:04:15 -0700377static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000378 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
379 "ifndef", "include", "local"
380};
381
382static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000383static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000384static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800385static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000386
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000387static Context *cstk;
388static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000389static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000390
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300391static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700392static StrList **dephead, **deptail; /* Dependency list */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000393
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300394static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000395
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000396static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700397static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000398
399static ListGen *list;
400
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000401/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000402 * The current set of multi-line macros we have defined.
403 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700404static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000405
406/*
407 * The current set of single-line macros we have defined.
408 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700409static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000410
411/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000412 * The multi-line macro we are currently defining, or the %rep
413 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000414 */
415static MMacro *defining;
416
Charles Crayned4200be2008-07-12 16:42:33 -0700417static uint64_t nested_mac_count;
418static uint64_t nested_rep_count;
419
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000420/*
421 * The number of macro parameters to allocate space for at a time.
422 */
423#define PARAM_DELTA 16
424
425/*
H. Peter Anvina4835d42008-05-20 14:21:29 -0700426 * The standard macro set: defined in macros.c in the array nasm_stdmac.
427 * This gives our position in the macro set, when we're processing it.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000428 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700429static macros_t *stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000430
431/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000432 * The extra standard macros that come from the object format, if
433 * any.
434 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700435static macros_t *extrastdmac = NULL;
H. Peter Anvincfb71762008-06-20 15:20:16 -0700436static bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000437
438/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000439 * Tokens are allocated in blocks to improve speed
440 */
441#define TOKEN_BLOCKSIZE 4096
442static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000443struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000444 Blocks *next;
445 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000446};
447
448static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000449
450/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000451 * Forward declarations.
452 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000453static Token *expand_mmac_params(Token * tline);
454static Token *expand_smacro(Token * tline);
455static Token *expand_id(Token * tline);
H. Peter Anvinf8ad5322009-02-21 17:55:08 -0800456static Context *get_ctx(const char *name, const char **namep,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300457 bool all_contexts);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700458static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000459static void error(int severity, const char *fmt, ...);
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200460static void error_precond(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000461static void *new_Block(size_t size);
462static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700463static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300464 const char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000465static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000466
467/*
468 * Macros for safe checking of token pointers, avoid *(NULL)
469 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300470#define tok_type_(x,t) ((x) && (x)->type == (t))
471#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
472#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
473#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000474
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300475/*
H. Peter Anvin077fb932010-07-20 14:56:30 -0700476 * nasm_unquote with error if the string contains NUL characters.
477 * If the string contains NUL characters, issue an error and return
478 * the C len, i.e. truncate at the NUL.
479 */
480static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
481{
482 size_t len = nasm_unquote(qstr, NULL);
483 size_t clen = strlen(qstr);
484
485 if (len != clen)
486 error(ERR_NONFATAL, "NUL character in `%s' directive",
487 pp_directives[directive]);
488
489 return clen;
490}
491
492/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700493 * In-place reverse a list of tokens.
494 */
495static Token *reverse_tokens(Token *t)
496{
497 Token *prev = NULL;
498 Token *next;
499
500 while (t) {
501 next = t->next;
502 t->next = prev;
503 prev = t;
504 t = next;
505 }
506
507 return prev;
508}
509
510/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300511 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000512 * front of them. We do it here because I could not find any other
513 * place to do it for the moment, and it is a hack (ideally it would
514 * be nice to be able to use the NASM pre-processor to do it).
515 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000516static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000517{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000518 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400519 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000520
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400521 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000522
523 /* Binary search for the directive name */
524 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400525 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400526 q = nasm_skip_word(p);
527 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000528 if (len) {
529 oldchar = p[len];
530 p[len] = 0;
531 while (j - i > 1) {
532 k = (j + i) / 2;
533 m = nasm_stricmp(p, tasm_directives[k]);
534 if (m == 0) {
535 /* We have found a directive, so jam a % in front of it
536 * so that NASM will then recognise it as one if it's own.
537 */
538 p[len] = oldchar;
539 len = strlen(p);
540 oldline = line;
541 line = nasm_malloc(len + 2);
542 line[0] = '%';
543 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700544 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300545 * NASM does not recognise IFDIFI, so we convert
546 * it to %if 0. This is not used in NASM
547 * compatible code, but does need to parse for the
548 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000549 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700550 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000551 } else {
552 memcpy(line + 1, p, len + 1);
553 }
554 nasm_free(oldline);
555 return line;
556 } else if (m < 0) {
557 j = k;
558 } else
559 i = k;
560 }
561 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000562 }
563 return line;
564}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000565
H. Peter Anvin76690a12002-04-30 20:52:49 +0000566/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000567 * The pre-preprocessing stage... This function translates line
568 * number indications as they emerge from GNU cpp (`# lineno "file"
569 * flags') into NASM preprocessor line number indications (`%line
570 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000571 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000572static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000573{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000574 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000575 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000576
H. Peter Anvine2c80182005-01-15 22:15:51 +0000577 if (line[0] == '#' && line[1] == ' ') {
578 oldline = line;
579 fname = oldline + 2;
580 lineno = atoi(fname);
581 fname += strspn(fname, "0123456789 ");
582 if (*fname == '"')
583 fname++;
584 fnlen = strcspn(fname, "\"");
585 line = nasm_malloc(20 + fnlen);
586 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
587 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000588 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000589 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000590 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000591 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000592}
593
594/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000595 * Free a linked list of tokens.
596 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000597static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000598{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400599 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000600 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000601}
602
603/*
604 * Free a linked list of lines.
605 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000606static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000607{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400608 Line *l, *tmp;
609 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000610 free_tlist(l->first);
611 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000612 }
613}
614
615/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000616 * Free an MMacro
617 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000618static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000619{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000620 nasm_free(m->name);
621 free_tlist(m->dlist);
622 nasm_free(m->defaults);
623 free_llist(m->expansion);
624 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000625}
626
627/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700628 * Free all currently defined macros, and free the hash tables
629 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700630static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700631{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400632 SMacro *s, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700633 const char *key;
634 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700635
H. Peter Anvin072771e2008-05-22 13:17:51 -0700636 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300637 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400638 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300639 nasm_free(s->name);
640 free_tlist(s->expansion);
641 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300642 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700643 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700644 hash_free(smt);
645}
646
647static void free_mmacro_table(struct hash_table *mmt)
648{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400649 MMacro *m, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700650 const char *key;
651 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700652
653 it = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700654 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300655 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400656 list_for_each_safe(m ,tmp, m)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300657 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700658 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700659 hash_free(mmt);
660}
661
662static void free_macros(void)
663{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700664 free_smacro_table(&smacros);
665 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700666}
667
668/*
669 * Initialize the hash tables
670 */
671static void init_macros(void)
672{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700673 hash_init(&smacros, HASH_LARGE);
674 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700675}
676
677/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000678 * Pop the context stack.
679 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000680static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000681{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000682 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000683
684 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700685 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000686 nasm_free(c->name);
687 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000688}
689
H. Peter Anvin072771e2008-05-22 13:17:51 -0700690/*
691 * Search for a key in the hash index; adding it if necessary
692 * (in which case we initialize the data pointer to NULL.)
693 */
694static void **
695hash_findi_add(struct hash_table *hash, const char *str)
696{
697 struct hash_insert hi;
698 void **r;
699 char *strx;
700
701 r = hash_findi(hash, str, &hi);
702 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300703 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700704
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300705 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700706 return hash_add(&hi, strx, NULL);
707}
708
709/*
710 * Like hash_findi, but returns the data element rather than a pointer
711 * to it. Used only when not adding a new element, hence no third
712 * argument.
713 */
714static void *
715hash_findix(struct hash_table *hash, const char *str)
716{
717 void **p;
718
719 p = hash_findi(hash, str, NULL);
720 return p ? *p : NULL;
721}
722
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400723/*
724 * read line from standart macros set,
725 * if there no more left -- return NULL
726 */
727static char *line_from_stdmac(void)
728{
729 unsigned char c;
730 const unsigned char *p = stdmacpos;
731 char *line, *q;
732 size_t len = 0;
733
734 if (!stdmacpos)
735 return NULL;
736
737 while ((c = *p++)) {
738 if (c >= 0x80)
739 len += pp_directives_len[c - 0x80] + 1;
740 else
741 len++;
742 }
743
744 line = nasm_malloc(len + 1);
745 q = line;
746 while ((c = *stdmacpos++)) {
747 if (c >= 0x80) {
748 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
749 q += pp_directives_len[c - 0x80];
750 *q++ = ' ';
751 } else {
752 *q++ = c;
753 }
754 }
755 stdmacpos = p;
756 *q = '\0';
757
758 if (!*stdmacpos) {
759 /* This was the last of the standard macro chain... */
760 stdmacpos = NULL;
761 if (any_extrastdmac) {
762 stdmacpos = extrastdmac;
763 any_extrastdmac = false;
764 } else if (do_predef) {
765 Line *pd, *l;
766 Token *head, **tail, *t;
767
768 /*
769 * Nasty hack: here we push the contents of
770 * `predef' on to the top-level expansion stack,
771 * since this is the most convenient way to
772 * implement the pre-include and pre-define
773 * features.
774 */
775 list_for_each(pd, predef) {
776 head = NULL;
777 tail = &head;
778 list_for_each(t, pd->first) {
779 *tail = new_Token(NULL, t->type, t->text, 0);
780 tail = &(*tail)->next;
781 }
782
783 l = nasm_malloc(sizeof(Line));
784 l->next = istk->expansion;
785 l->first = head;
786 l->finishes = NULL;
787
788 istk->expansion = l;
789 }
790 do_predef = false;
791 }
792 }
793
794 return line;
795}
796
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000797#define BUF_DELTA 512
798/*
799 * Read a line from the top file in istk, handling multiple CR/LFs
800 * at the end of the line read, and handling spurious ^Zs. Will
801 * return lines from the standard macro set if this has not already
802 * been done.
803 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000804static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000805{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000806 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000807 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000808
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400809 /*
810 * standart macros set (predefined) goes first
811 */
812 p = line_from_stdmac();
813 if (p)
814 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700815
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400816 /*
817 * regular read from a file
818 */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000819 bufsize = BUF_DELTA;
820 buffer = nasm_malloc(BUF_DELTA);
821 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000822 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000823 while (1) {
824 q = fgets(p, bufsize - (p - buffer), istk->fp);
825 if (!q)
826 break;
827 p += strlen(p);
828 if (p > buffer && p[-1] == '\n') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300829 /*
830 * Convert backslash-CRLF line continuation sequences into
831 * nothing at all (for DOS and Windows)
832 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000833 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
834 p -= 3;
835 *p = 0;
836 continued_count++;
837 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300838 /*
839 * Also convert backslash-LF line continuation sequences into
840 * nothing at all (for Unix)
841 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000842 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
843 p -= 2;
844 *p = 0;
845 continued_count++;
846 } else {
847 break;
848 }
849 }
850 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000851 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000852 bufsize += BUF_DELTA;
853 buffer = nasm_realloc(buffer, bufsize);
854 p = buffer + offset; /* prevent stale-pointer problems */
855 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000856 }
857
H. Peter Anvine2c80182005-01-15 22:15:51 +0000858 if (!q && p == buffer) {
859 nasm_free(buffer);
860 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000861 }
862
H. Peter Anvine2c80182005-01-15 22:15:51 +0000863 src_set_linnum(src_get_linnum() + istk->lineinc +
864 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000865
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000866 /*
867 * Play safe: remove CRs as well as LFs, if any of either are
868 * present at the end of the line.
869 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000870 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000871 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000872
873 /*
874 * Handle spurious ^Z, which may be inserted into source files
875 * by some file transfer utilities.
876 */
877 buffer[strcspn(buffer, "\032")] = '\0';
878
H. Peter Anvin734b1882002-04-30 21:01:08 +0000879 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000880
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000881 return buffer;
882}
883
884/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000885 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000886 * don't need to parse the value out of e.g. numeric tokens: we
887 * simply split one string into many.
888 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000889static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000890{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700891 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000892 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000893 Token *list = NULL;
894 Token *t, **tail = &list;
895
H. Peter Anvine2c80182005-01-15 22:15:51 +0000896 while (*line) {
897 p = line;
898 if (*p == '%') {
899 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300900 if (*p == '+' && !nasm_isdigit(p[1])) {
901 p++;
902 type = TOK_PASTE;
903 } else if (nasm_isdigit(*p) ||
904 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000905 do {
906 p++;
907 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700908 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000909 type = TOK_PREPROC_ID;
910 } else if (*p == '{') {
911 p++;
912 while (*p && *p != '}') {
913 p[-1] = *p;
914 p++;
915 }
916 p[-1] = '\0';
917 if (*p)
918 p++;
919 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300920 } else if (*p == '[') {
921 int lvl = 1;
922 line += 2; /* Skip the leading %[ */
923 p++;
924 while (lvl && (c = *p++)) {
925 switch (c) {
926 case ']':
927 lvl--;
928 break;
929 case '%':
930 if (*p == '[')
931 lvl++;
932 break;
933 case '\'':
934 case '\"':
935 case '`':
Cyrill Gorcunovc6360a72010-07-13 13:32:19 +0400936 p = nasm_skip_string(p - 1) + 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300937 break;
938 default:
939 break;
940 }
941 }
942 p--;
943 if (*p)
944 *p++ = '\0';
945 if (lvl)
946 error(ERR_NONFATAL, "unterminated %[ construct");
947 type = TOK_INDIRECT;
948 } else if (*p == '?') {
949 type = TOK_PREPROC_Q; /* %? */
950 p++;
951 if (*p == '?') {
952 type = TOK_PREPROC_QQ; /* %?? */
953 p++;
954 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +0400955 } else if (*p == '!') {
956 type = TOK_PREPROC_ID;
957 p++;
958 if (isidchar(*p)) {
959 do {
960 p++;
961 } while (isidchar(*p));
962 } else if (*p == '\'' || *p == '\"' || *p == '`') {
963 p = nasm_skip_string(p);
964 if (*p)
965 p++;
966 else
967 error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string");
968 } else {
969 /* %! without string or identifier */
970 type = TOK_OTHER; /* Legacy behavior... */
971 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000972 } else if (isidchar(*p) ||
973 ((*p == '!' || *p == '%' || *p == '$') &&
974 isidchar(p[1]))) {
975 do {
976 p++;
977 }
978 while (isidchar(*p));
979 type = TOK_PREPROC_ID;
980 } else {
981 type = TOK_OTHER;
982 if (*p == '%')
983 p++;
984 }
985 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
986 type = TOK_ID;
987 p++;
988 while (*p && isidchar(*p))
989 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700990 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000991 /*
992 * A string token.
993 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000994 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300995 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000996
H. Peter Anvine2c80182005-01-15 22:15:51 +0000997 if (*p) {
998 p++;
999 } else {
H. Peter Anvin917a3492008-09-24 09:14:49 -07001000 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001001 /* Handling unterminated strings by UNV */
1002 /* type = -1; */
1003 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001004 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001005 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001006 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001007 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001008 bool is_hex = false;
1009 bool is_float = false;
1010 bool has_e = false;
1011 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001012
H. Peter Anvine2c80182005-01-15 22:15:51 +00001013 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001014 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001015 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001016
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001017 if (*p == '$') {
1018 p++;
1019 is_hex = true;
1020 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001021
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001022 for (;;) {
1023 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001024
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001025 if (!is_hex && (c == 'e' || c == 'E')) {
1026 has_e = true;
1027 if (*p == '+' || *p == '-') {
1028 /*
1029 * e can only be followed by +/- if it is either a
1030 * prefixed hex number or a floating-point number
1031 */
1032 p++;
1033 is_float = true;
1034 }
1035 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1036 is_hex = true;
1037 } else if (c == 'P' || c == 'p') {
1038 is_float = true;
1039 if (*p == '+' || *p == '-')
1040 p++;
1041 } else if (isnumchar(c) || c == '_')
1042 ; /* just advance */
1043 else if (c == '.') {
1044 /*
1045 * we need to deal with consequences of the legacy
1046 * parser, like "1.nolist" being two tokens
1047 * (TOK_NUMBER, TOK_ID) here; at least give it
1048 * a shot for now. In the future, we probably need
1049 * a flex-based scanner with proper pattern matching
1050 * to do it as well as it can be done. Nothing in
1051 * the world is going to help the person who wants
1052 * 0x123.p16 interpreted as two tokens, though.
1053 */
1054 r = p;
1055 while (*r == '_')
1056 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001057
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001058 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1059 (!is_hex && (*r == 'e' || *r == 'E')) ||
1060 (*r == 'p' || *r == 'P')) {
1061 p = r;
1062 is_float = true;
1063 } else
1064 break; /* Terminate the token */
1065 } else
1066 break;
1067 }
1068 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001069
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001070 if (p == line+1 && *line == '$') {
1071 type = TOK_OTHER; /* TOKEN_HERE */
1072 } else {
1073 if (has_e && !is_hex) {
1074 /* 1e13 is floating-point, but 1e13h is not */
1075 is_float = true;
1076 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001077
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001078 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1079 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001080 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001081 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001082 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001083 /*
1084 * Whitespace just before end-of-line is discarded by
1085 * pretending it's a comment; whitespace just before a
1086 * comment gets lumped into the comment.
1087 */
1088 if (!*p || *p == ';') {
1089 type = TOK_COMMENT;
1090 while (*p)
1091 p++;
1092 }
1093 } else if (*p == ';') {
1094 type = TOK_COMMENT;
1095 while (*p)
1096 p++;
1097 } else {
1098 /*
1099 * Anything else is an operator of some kind. We check
1100 * for all the double-character operators (>>, <<, //,
1101 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001102 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001103 */
1104 type = TOK_OTHER;
1105 if ((p[0] == '>' && p[1] == '>') ||
1106 (p[0] == '<' && p[1] == '<') ||
1107 (p[0] == '/' && p[1] == '/') ||
1108 (p[0] == '<' && p[1] == '=') ||
1109 (p[0] == '>' && p[1] == '=') ||
1110 (p[0] == '=' && p[1] == '=') ||
1111 (p[0] == '!' && p[1] == '=') ||
1112 (p[0] == '<' && p[1] == '>') ||
1113 (p[0] == '&' && p[1] == '&') ||
1114 (p[0] == '|' && p[1] == '|') ||
1115 (p[0] == '^' && p[1] == '^')) {
1116 p++;
1117 }
1118 p++;
1119 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001120
H. Peter Anvine2c80182005-01-15 22:15:51 +00001121 /* Handling unterminated string by UNV */
1122 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001123 {
1124 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1125 t->text[p-line] = *line;
1126 tail = &t->next;
1127 }
1128 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001129 if (type != TOK_COMMENT) {
1130 *tail = t = new_Token(NULL, type, line, p - line);
1131 tail = &t->next;
1132 }
1133 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001134 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001135 return list;
1136}
1137
H. Peter Anvince616072002-04-30 21:02:23 +00001138/*
1139 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001140 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001141 * deleted only all at once by the delete_Blocks function.
1142 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001143static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001144{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001145 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001146
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001147 /* first, get to the end of the linked list */
1148 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001149 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001150 /* now allocate the requested chunk */
1151 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001152
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001153 /* now allocate a new block for the next request */
1154 b->next = nasm_malloc(sizeof(Blocks));
1155 /* and initialize the contents of the new block */
1156 b->next->next = NULL;
1157 b->next->chunk = NULL;
1158 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001159}
1160
1161/*
1162 * this function deletes all managed blocks of memory
1163 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001164static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001165{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001166 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001167
H. Peter Anvin70653092007-10-19 14:42:29 -07001168 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001169 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001170 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001171 * free it.
1172 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001173 while (b) {
1174 if (b->chunk)
1175 nasm_free(b->chunk);
1176 a = b;
1177 b = b->next;
1178 if (a != &blocks)
1179 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001180 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001181}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001182
1183/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001184 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001185 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001186 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001187 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001188static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001189 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001190{
1191 Token *t;
1192 int i;
1193
H. Peter Anvin89cee572009-07-15 09:16:54 -04001194 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001195 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1196 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1197 freeTokens[i].next = &freeTokens[i + 1];
1198 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001199 }
1200 t = freeTokens;
1201 freeTokens = t->next;
1202 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001203 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001204 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001205 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001206 t->text = NULL;
1207 } else {
1208 if (txtlen == 0)
1209 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001210 t->text = nasm_malloc(txtlen+1);
1211 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001212 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001213 }
1214 return t;
1215}
1216
H. Peter Anvine2c80182005-01-15 22:15:51 +00001217static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001218{
1219 Token *next = t->next;
1220 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001221 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001222 freeTokens = t;
1223 return next;
1224}
1225
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001226/*
1227 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001228 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1229 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001230 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001231static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001232{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001233 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001234 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001235 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001236 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001237
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001238 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001239 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001240 char *v;
1241 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001242
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001243 v = t->text + 2;
1244 if (*v == '\'' || *v == '\"' || *v == '`') {
1245 size_t len = nasm_unquote(v, NULL);
1246 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001247
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001248 if (len != clen) {
1249 error(ERR_NONFATAL | ERR_PASS1,
1250 "NUL character in %! string");
1251 v = NULL;
1252 }
1253 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001254
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001255 if (v) {
1256 char *p = getenv(v);
1257 if (!p) {
1258 error(ERR_NONFATAL | ERR_PASS1,
1259 "nonexistent environment variable `%s'", v);
1260 p = "";
1261 }
1262 t->text = nasm_strdup(p);
1263 }
1264 nasm_free(q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001265 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001266
H. Peter Anvine2c80182005-01-15 22:15:51 +00001267 /* Expand local macros here and not during preprocessing */
1268 if (expand_locals &&
1269 t->type == TOK_PREPROC_ID && t->text &&
1270 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001271 const char *q;
1272 char *p;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001273 Context *ctx = get_ctx(t->text, &q, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001274 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001275 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001276 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001277 p = nasm_strcat(buffer, q);
1278 nasm_free(t->text);
1279 t->text = p;
1280 }
1281 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001282 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001283 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001284 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001285 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001286 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001287
H. Peter Anvin734b1882002-04-30 21:01:08 +00001288 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001289
1290 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001291 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001292 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001293 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001294 q = t->text;
1295 while (*q)
1296 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001297 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001298 }
1299 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001300
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001301 return line;
1302}
1303
1304/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001305 * A scanner, suitable for use by the expression evaluator, which
1306 * operates on a line of Tokens. Expects a pointer to a pointer to
1307 * the first token in the line to be passed in as its private_data
1308 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001309 *
1310 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001311 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001312static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001313{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001314 Token **tlineptr = private_data;
1315 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001316 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001317
H. Peter Anvine2c80182005-01-15 22:15:51 +00001318 do {
1319 tline = *tlineptr;
1320 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001321 } while (tline && (tline->type == TOK_WHITESPACE ||
1322 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001323
1324 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001325 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001326
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001327 tokval->t_charptr = tline->text;
1328
H. Peter Anvin76690a12002-04-30 20:52:49 +00001329 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001330 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001331 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001332 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001333
H. Peter Anvine2c80182005-01-15 22:15:51 +00001334 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001335 p = tokval->t_charptr = tline->text;
1336 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001337 tokval->t_charptr++;
1338 return tokval->t_type = TOKEN_ID;
1339 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001340
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001341 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001342 if (r >= p+MAX_KEYWORD)
1343 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001344 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001345 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001346 *s = '\0';
1347 /* right, so we have an identifier sitting in temp storage. now,
1348 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001349 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001350 }
1351
H. Peter Anvine2c80182005-01-15 22:15:51 +00001352 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001353 bool rn_error;
1354 tokval->t_integer = readnum(tline->text, &rn_error);
1355 tokval->t_charptr = tline->text;
1356 if (rn_error)
1357 return tokval->t_type = TOKEN_ERRNUM;
1358 else
1359 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001360 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001361
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001362 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001363 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001364 }
1365
H. Peter Anvine2c80182005-01-15 22:15:51 +00001366 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001367 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001368
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001369 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001370 tokval->t_charptr = tline->text;
1371 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001372
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001373 if (ep[0] != bq || ep[1] != '\0')
1374 return tokval->t_type = TOKEN_ERRSTR;
1375 else
1376 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001377 }
1378
H. Peter Anvine2c80182005-01-15 22:15:51 +00001379 if (tline->type == TOK_OTHER) {
1380 if (!strcmp(tline->text, "<<"))
1381 return tokval->t_type = TOKEN_SHL;
1382 if (!strcmp(tline->text, ">>"))
1383 return tokval->t_type = TOKEN_SHR;
1384 if (!strcmp(tline->text, "//"))
1385 return tokval->t_type = TOKEN_SDIV;
1386 if (!strcmp(tline->text, "%%"))
1387 return tokval->t_type = TOKEN_SMOD;
1388 if (!strcmp(tline->text, "=="))
1389 return tokval->t_type = TOKEN_EQ;
1390 if (!strcmp(tline->text, "<>"))
1391 return tokval->t_type = TOKEN_NE;
1392 if (!strcmp(tline->text, "!="))
1393 return tokval->t_type = TOKEN_NE;
1394 if (!strcmp(tline->text, "<="))
1395 return tokval->t_type = TOKEN_LE;
1396 if (!strcmp(tline->text, ">="))
1397 return tokval->t_type = TOKEN_GE;
1398 if (!strcmp(tline->text, "&&"))
1399 return tokval->t_type = TOKEN_DBL_AND;
1400 if (!strcmp(tline->text, "^^"))
1401 return tokval->t_type = TOKEN_DBL_XOR;
1402 if (!strcmp(tline->text, "||"))
1403 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001404 }
1405
1406 /*
1407 * We have no other options: just return the first character of
1408 * the token text.
1409 */
1410 return tokval->t_type = tline->text[0];
1411}
1412
1413/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001414 * Compare a string to the name of an existing macro; this is a
1415 * simple wrapper which calls either strcmp or nasm_stricmp
1416 * depending on the value of the `casesense' parameter.
1417 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001418static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001419{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001420 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001421}
1422
1423/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001424 * Compare a string to the name of an existing macro; this is a
1425 * simple wrapper which calls either strcmp or nasm_stricmp
1426 * depending on the value of the `casesense' parameter.
1427 */
1428static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1429{
1430 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1431}
1432
1433/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001434 * Return the Context structure associated with a %$ token. Return
1435 * NULL, having _already_ reported an error condition, if the
1436 * context stack isn't deep enough for the supplied number of $
1437 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001438 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001439 * also scanned for such smacro, until it is found; if not -
1440 * only the context that directly results from the number of $'s
1441 * in variable's name.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001442 *
1443 * If "namep" is non-NULL, set it to the pointer to the macro name
1444 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001445 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001446static Context *get_ctx(const char *name, const char **namep,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001447 bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001448{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001449 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001450 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001451 int i;
1452
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001453 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001454 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001455
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001456 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001457 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001458
H. Peter Anvine2c80182005-01-15 22:15:51 +00001459 if (!cstk) {
1460 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1461 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001462 }
1463
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001464 name += 2;
1465 ctx = cstk;
1466 i = 0;
1467 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001468 name++;
1469 i++;
1470 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001471 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001472 if (!ctx) {
1473 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001474 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001475 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001476 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001477
1478 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001479 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001480
Keith Kanios404589e2010-08-10 20:12:57 -05001481 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001482 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001483
Cyrill Gorcunovd90693c2010-08-11 20:31:46 +04001484 /*
1485 * NOTE: In 2.10 we will not need lookup in extarnal
1486 * contexts, so this is a gentle way to inform users
1487 * about their source code need to be updated
1488 */
1489
1490 /* first round -- check the current context */
1491 m = hash_findix(&ctx->localmac, name);
1492 while (m) {
1493 if (!mstrcmp(m->name, name, m->casesense))
1494 return ctx;
1495 m = m->next;
1496 }
1497
1498 /* second round - external contexts */
1499 while ((ctx = ctx->next)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001500 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001501 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001502 while (m) {
Keith Kanios404589e2010-08-10 20:12:57 -05001503 if (!mstrcmp(m->name, name, m->casesense)) {
Keith Kanios88d947e2010-08-14 12:47:45 -05001504 /* NOTE: deprecated as of 2.10 */
Cyrill Gorcunovd90693c2010-08-11 20:31:46 +04001505 static int once = 0;
1506 if (!once) {
1507 error(ERR_WARNING, "context-local macro expansion"
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001508 " fall-through (automatic searching of outer"
1509 " contexts) will be deprecated starting in"
1510 " NASM 2.10, please see the NASM Manual for"
1511 " more information");
Cyrill Gorcunovd90693c2010-08-11 20:31:46 +04001512 once = 1;
1513 }
Keith Kanios88d947e2010-08-14 12:47:45 -05001514 error(ERR_WARNING, "`%s': context-local macro expansion fall-through", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001515 return ctx;
Cyrill Gorcunovd90693c2010-08-11 20:31:46 +04001516 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001517 m = m->next;
1518 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001519 }
Cyrill Gorcunovd90693c2010-08-11 20:31:46 +04001520
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001521 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001522}
1523
1524/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001525 * Check to see if a file is already in a string list
1526 */
1527static bool in_list(const StrList *list, const char *str)
1528{
1529 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001530 if (!strcmp(list->str, str))
1531 return true;
1532 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001533 }
1534 return false;
1535}
1536
1537/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001538 * Open an include file. This routine must always return a valid
1539 * file pointer if it returns - it's responsible for throwing an
1540 * ERR_FATAL and bombing out completely if not. It should also try
1541 * the include path one by one until it finds the file or reaches
1542 * the end of the path.
1543 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001544static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001545 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001546{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001547 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001548 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001549 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001550 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001551 size_t prefix_len = 0;
1552 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001553
H. Peter Anvine2c80182005-01-15 22:15:51 +00001554 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001555 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001556 memcpy(sl->str, prefix, prefix_len);
1557 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001558 fp = fopen(sl->str, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001559 if (fp && dhead && !in_list(*dhead, sl->str)) {
1560 sl->next = NULL;
1561 **dtail = sl;
1562 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001563 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001564 nasm_free(sl);
1565 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001566 if (fp)
1567 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001568 if (!ip) {
1569 if (!missing_ok)
1570 break;
1571 prefix = NULL;
1572 } else {
1573 prefix = ip->path;
1574 ip = ip->next;
1575 }
1576 if (prefix) {
1577 prefix_len = strlen(prefix);
1578 } else {
1579 /* -MG given and file not found */
1580 if (dhead && !in_list(*dhead, file)) {
1581 sl = nasm_malloc(len+1+sizeof sl->next);
1582 sl->next = NULL;
1583 strcpy(sl->str, file);
1584 **dtail = sl;
1585 *dtail = &sl->next;
1586 }
1587 return NULL;
1588 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001589 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001590
H. Peter Anvin734b1882002-04-30 21:01:08 +00001591 error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001592 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001593}
1594
1595/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001596 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001597 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001598 * return true if _any_ single-line macro of that name is defined.
1599 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001600 * `nparam' or no parameters is defined.
1601 *
1602 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001603 * defined, or nparam is -1, the address of the definition structure
1604 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001605 * is NULL, no action will be taken regarding its contents, and no
1606 * error will occur.
1607 *
1608 * Note that this is also called with nparam zero to resolve
1609 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001610 *
1611 * If you already know which context macro belongs to, you can pass
1612 * the context pointer as first parameter; if you won't but name begins
1613 * with %$ the context will be automatically computed. If all_contexts
1614 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001615 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001616static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001617smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001618 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001619{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001620 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001621 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001622
H. Peter Anvin97a23472007-09-16 17:57:25 -07001623 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001624 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001625 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001626 if (cstk)
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001627 ctx = get_ctx(name, &name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001628 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001629 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001630 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001631 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001632 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001633 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001634 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001635
H. Peter Anvine2c80182005-01-15 22:15:51 +00001636 while (m) {
1637 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001638 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001639 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001640 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001641 *defn = m;
1642 else
1643 *defn = NULL;
1644 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001645 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001646 }
1647 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001648 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001649
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001650 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001651}
1652
1653/*
1654 * Count and mark off the parameters in a multi-line macro call.
1655 * This is called both from within the multi-line macro expansion
1656 * code, and also to mark off the default parameters when provided
1657 * in a %macro definition line.
1658 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001659static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001660{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001661 int paramsize, brace;
1662
1663 *nparam = paramsize = 0;
1664 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001665 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001666 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001667 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001668 paramsize += PARAM_DELTA;
1669 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1670 }
1671 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001672 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001673 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001674 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001675 (*params)[(*nparam)++] = t;
1676 while (tok_isnt_(t, brace ? "}" : ","))
1677 t = t->next;
1678 if (t) { /* got a comma/brace */
1679 t = t->next;
1680 if (brace) {
1681 /*
1682 * Now we've found the closing brace, look further
1683 * for the comma.
1684 */
1685 skip_white_(t);
1686 if (tok_isnt_(t, ",")) {
1687 error(ERR_NONFATAL,
1688 "braces do not enclose all of macro parameter");
1689 while (tok_isnt_(t, ","))
1690 t = t->next;
1691 }
1692 if (t)
1693 t = t->next; /* eat the comma */
1694 }
1695 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001696 }
1697}
1698
1699/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001700 * Determine whether one of the various `if' conditions is true or
1701 * not.
1702 *
1703 * We must free the tline we get passed.
1704 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001705static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001706{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001707 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001708 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001709 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001710 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001711 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001712 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001713 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001714
1715 origline = tline;
1716
H. Peter Anvine2c80182005-01-15 22:15:51 +00001717 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001718 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001719 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001720 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001721 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001722 if (!tline)
1723 break;
1724 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001725 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001726 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001727 free_tlist(origline);
1728 return -1;
1729 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001730 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001731 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001732 tline = tline->next;
1733 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001734 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001735
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001736 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001737 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001738 while (tline) {
1739 skip_white_(tline);
1740 if (!tline || (tline->type != TOK_ID &&
1741 (tline->type != TOK_PREPROC_ID ||
1742 tline->text[1] != '$'))) {
1743 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001744 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001745 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001746 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001747 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001748 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001749 tline = tline->next;
1750 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001751 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001752
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001753 case PPC_IFENV:
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001754 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001755 j = false; /* have we matched yet? */
1756 while (tline) {
1757 skip_white_(tline);
1758 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001759 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001760 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001761 tline->text[1] != '!'))) {
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001762 error(ERR_NONFATAL,
1763 "`%s' expects environment variable names",
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001764 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001765 goto fail;
1766 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001767 p = tline->text;
1768 if (tline->type == TOK_PREPROC_ID)
1769 p += 2; /* Skip leading %! */
1770 if (*p == '\'' || *p == '\"' || *p == '`')
1771 nasm_unquote_cstr(p, ct);
1772 if (getenv(p))
1773 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001774 tline = tline->next;
1775 }
1776 break;
1777
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001778 case PPC_IFIDN:
1779 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001780 tline = expand_smacro(tline);
1781 t = tt = tline;
1782 while (tok_isnt_(tt, ","))
1783 tt = tt->next;
1784 if (!tt) {
1785 error(ERR_NONFATAL,
1786 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001787 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001788 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001789 }
1790 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001791 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001792 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1793 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1794 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001795 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001796 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001797 }
1798 if (t->type == TOK_WHITESPACE) {
1799 t = t->next;
1800 continue;
1801 }
1802 if (tt->type == TOK_WHITESPACE) {
1803 tt = tt->next;
1804 continue;
1805 }
1806 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001807 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001808 break;
1809 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001810 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001811 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001812 size_t l1 = nasm_unquote(t->text, NULL);
1813 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001814
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001815 if (l1 != l2) {
1816 j = false;
1817 break;
1818 }
1819 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1820 j = false;
1821 break;
1822 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001823 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001824 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001825 break;
1826 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001827
H. Peter Anvine2c80182005-01-15 22:15:51 +00001828 t = t->next;
1829 tt = tt->next;
1830 }
1831 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001832 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001833 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001834
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001835 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001836 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001837 bool found = false;
1838 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001839
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001840 skip_white_(tline);
1841 tline = expand_id(tline);
1842 if (!tok_type_(tline, TOK_ID)) {
1843 error(ERR_NONFATAL,
1844 "`%s' expects a macro name", pp_directives[ct]);
1845 goto fail;
1846 }
1847 searching.name = nasm_strdup(tline->text);
1848 searching.casesense = true;
1849 searching.plus = false;
1850 searching.nolist = false;
1851 searching.in_progress = 0;
1852 searching.max_depth = 0;
1853 searching.rep_nest = NULL;
1854 searching.nparam_min = 0;
1855 searching.nparam_max = INT_MAX;
1856 tline = expand_smacro(tline->next);
1857 skip_white_(tline);
1858 if (!tline) {
1859 } else if (!tok_type_(tline, TOK_NUMBER)) {
1860 error(ERR_NONFATAL,
1861 "`%s' expects a parameter count or nothing",
1862 pp_directives[ct]);
1863 } else {
1864 searching.nparam_min = searching.nparam_max =
1865 readnum(tline->text, &j);
1866 if (j)
1867 error(ERR_NONFATAL,
1868 "unable to parse parameter count `%s'",
1869 tline->text);
1870 }
1871 if (tline && tok_is_(tline->next, "-")) {
1872 tline = tline->next->next;
1873 if (tok_is_(tline, "*"))
1874 searching.nparam_max = INT_MAX;
1875 else if (!tok_type_(tline, TOK_NUMBER))
1876 error(ERR_NONFATAL,
1877 "`%s' expects a parameter count after `-'",
1878 pp_directives[ct]);
1879 else {
1880 searching.nparam_max = readnum(tline->text, &j);
1881 if (j)
1882 error(ERR_NONFATAL,
1883 "unable to parse parameter count `%s'",
1884 tline->text);
1885 if (searching.nparam_min > searching.nparam_max)
1886 error(ERR_NONFATAL,
1887 "minimum parameter count exceeds maximum");
1888 }
1889 }
1890 if (tline && tok_is_(tline->next, "+")) {
1891 tline = tline->next;
1892 searching.plus = true;
1893 }
1894 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1895 while (mmac) {
1896 if (!strcmp(mmac->name, searching.name) &&
1897 (mmac->nparam_min <= searching.nparam_max
1898 || searching.plus)
1899 && (searching.nparam_min <= mmac->nparam_max
1900 || mmac->plus)) {
1901 found = true;
1902 break;
1903 }
1904 mmac = mmac->next;
1905 }
1906 if (tline && tline->next)
1907 error(ERR_WARNING|ERR_PASS1,
1908 "trailing garbage after %%ifmacro ignored");
1909 nasm_free(searching.name);
1910 j = found;
1911 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001912 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001913
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001914 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001915 needtype = TOK_ID;
1916 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001917 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001918 needtype = TOK_NUMBER;
1919 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001920 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001921 needtype = TOK_STRING;
1922 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001923
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001924iftype:
1925 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001926
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001927 while (tok_type_(t, TOK_WHITESPACE) ||
1928 (needtype == TOK_NUMBER &&
1929 tok_type_(t, TOK_OTHER) &&
1930 (t->text[0] == '-' || t->text[0] == '+') &&
1931 !t->text[1]))
1932 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001933
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001934 j = tok_type_(t, needtype);
1935 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001936
1937 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001938 t = tline = expand_smacro(tline);
1939 while (tok_type_(t, TOK_WHITESPACE))
1940 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001941
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001942 j = false;
1943 if (t) {
1944 t = t->next; /* Skip the actual token */
1945 while (tok_type_(t, TOK_WHITESPACE))
1946 t = t->next;
1947 j = !t; /* Should be nothing left */
1948 }
1949 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001950
H. Peter Anvin134b9462008-02-16 17:01:40 -08001951 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001952 t = tline = expand_smacro(tline);
1953 while (tok_type_(t, TOK_WHITESPACE))
1954 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001955
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001956 j = !t; /* Should be empty */
1957 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001958
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001959 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001960 t = tline = expand_smacro(tline);
1961 tptr = &t;
1962 tokval.t_type = TOKEN_INVALID;
1963 evalresult = evaluate(ppscan, tptr, &tokval,
1964 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001965 if (!evalresult)
1966 return -1;
1967 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001968 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001969 "trailing garbage after expression ignored");
1970 if (!is_simple(evalresult)) {
1971 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001972 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001973 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001974 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001975 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001976 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001977
H. Peter Anvine2c80182005-01-15 22:15:51 +00001978 default:
1979 error(ERR_FATAL,
1980 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001981 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001982 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001983 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001984
1985 free_tlist(origline);
1986 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001987
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001988fail:
1989 free_tlist(origline);
1990 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001991}
1992
1993/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001994 * Common code for defining an smacro
1995 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001996static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001997 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001998{
1999 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002000 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002001
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002002 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002003 if (!smac) {
2004 error(ERR_WARNING|ERR_PASS1,
2005 "single-line macro `%s' defined both with and"
2006 " without parameters", mname);
2007 /*
2008 * Some instances of the old code considered this a failure,
2009 * some others didn't. What is the right thing to do here?
2010 */
2011 free_tlist(expansion);
2012 return false; /* Failure */
2013 } else {
2014 /*
2015 * We're redefining, so we have to take over an
2016 * existing SMacro structure. This means freeing
2017 * what was already in it.
2018 */
2019 nasm_free(smac->name);
2020 free_tlist(smac->expansion);
2021 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002022 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002023 smtbl = ctx ? &ctx->localmac : &smacros;
2024 smhead = (SMacro **) hash_findi_add(smtbl, mname);
2025 smac = nasm_malloc(sizeof(SMacro));
2026 smac->next = *smhead;
2027 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002028 }
2029 smac->name = nasm_strdup(mname);
2030 smac->casesense = casesense;
2031 smac->nparam = nparam;
2032 smac->expansion = expansion;
2033 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002034 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002035}
2036
2037/*
2038 * Undefine an smacro
2039 */
2040static void undef_smacro(Context *ctx, const char *mname)
2041{
2042 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002043 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002044
H. Peter Anvin166c2472008-05-28 12:28:58 -07002045 smtbl = ctx ? &ctx->localmac : &smacros;
2046 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002047
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002048 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002049 /*
2050 * We now have a macro name... go hunt for it.
2051 */
2052 sp = smhead;
2053 while ((s = *sp) != NULL) {
2054 if (!mstrcmp(s->name, mname, s->casesense)) {
2055 *sp = s->next;
2056 nasm_free(s->name);
2057 free_tlist(s->expansion);
2058 nasm_free(s);
2059 } else {
2060 sp = &s->next;
2061 }
2062 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002063 }
2064}
2065
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002066/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002067 * Parse a mmacro specification.
2068 */
2069static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
2070{
2071 bool err;
2072
2073 tline = tline->next;
2074 skip_white_(tline);
2075 tline = expand_id(tline);
2076 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002077 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2078 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002079 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002080
H. Peter Anvin89cee572009-07-15 09:16:54 -04002081 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002082 def->name = nasm_strdup(tline->text);
2083 def->plus = false;
2084 def->nolist = false;
2085 def->in_progress = 0;
2086 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002087 def->nparam_min = 0;
2088 def->nparam_max = 0;
2089
H. Peter Anvina26433d2008-07-16 14:40:01 -07002090 tline = expand_smacro(tline->next);
2091 skip_white_(tline);
2092 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002093 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002094 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002095 def->nparam_min = def->nparam_max =
2096 readnum(tline->text, &err);
2097 if (err)
2098 error(ERR_NONFATAL,
2099 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002100 }
2101 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002102 tline = tline->next->next;
2103 if (tok_is_(tline, "*")) {
2104 def->nparam_max = INT_MAX;
2105 } else if (!tok_type_(tline, TOK_NUMBER)) {
2106 error(ERR_NONFATAL,
2107 "`%s' expects a parameter count after `-'", directive);
2108 } else {
2109 def->nparam_max = readnum(tline->text, &err);
2110 if (err) {
2111 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2112 tline->text);
2113 }
2114 if (def->nparam_min > def->nparam_max) {
2115 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2116 }
2117 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002118 }
2119 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002120 tline = tline->next;
2121 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002122 }
2123 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002124 !nasm_stricmp(tline->next->text, ".nolist")) {
2125 tline = tline->next;
2126 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002127 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002128
H. Peter Anvina26433d2008-07-16 14:40:01 -07002129 /*
2130 * Handle default parameters.
2131 */
2132 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002133 def->dlist = tline->next;
2134 tline->next = NULL;
2135 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002136 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002137 def->dlist = NULL;
2138 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002139 }
2140 def->expansion = NULL;
2141
H. Peter Anvin89cee572009-07-15 09:16:54 -04002142 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002143 !def->plus)
2144 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2145 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002146
H. Peter Anvina26433d2008-07-16 14:40:01 -07002147 return true;
2148}
2149
2150
2151/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002152 * Decode a size directive
2153 */
2154static int parse_size(const char *str) {
2155 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002156 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002157 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002158 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002159
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002160 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002161}
2162
Ed Beroset3ab3f412002-06-11 03:31:49 +00002163/**
2164 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002165 * Find out if a line contains a preprocessor directive, and deal
2166 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002167 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002168 * If a directive _is_ found, it is the responsibility of this routine
2169 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002170 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002171 * @param tline a pointer to the current tokeninzed line linked list
2172 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002173 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002174 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002175static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002176{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002177 enum preproc_token i;
2178 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002179 bool err;
2180 int nparam;
2181 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002182 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002183 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002184 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002185 char *p, *pp;
2186 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002187 Include *inc;
2188 Context *ctx;
2189 Cond *cond;
H. Peter Anvin97a23472007-09-16 17:57:25 -07002190 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002191 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
2192 Line *l;
2193 struct tokenval tokval;
2194 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002195 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002196 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002197 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002198 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002199
2200 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002201
H. Peter Anvineba20a72002-04-30 20:53:55 +00002202 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002203 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002204 (tline->text[1] == '%' || tline->text[1] == '$'
2205 || tline->text[1] == '!'))
2206 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002207
H. Peter Anvin4169a472007-09-12 01:29:43 +00002208 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002209
2210 /*
Cyrill Gorcunovf09116f2010-02-28 11:52:53 +03002211 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2212 * since they are known to be buggy at moment, we need to fix them
2213 * in future release (2.09-2.10)
2214 */
2215 if (i == PP_RMACRO || i == PP_RMACRO || i == PP_EXITMACRO) {
2216 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2217 tline->text);
2218 return NO_DIRECTIVE_FOUND;
2219 }
2220
2221 /*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002222 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00002223 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002224 * we should ignore all directives except for condition
2225 * directives.
2226 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002227 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002228 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2229 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002230 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002231
2232 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002233 * If we're defining a macro or reading a %rep block, we should
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002234 * ignore all directives except for %macro/%imacro (which nest),
2235 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2236 * If we're in a %rep block, another %rep nests, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002237 */
2238 if (defining && i != PP_MACRO && i != PP_IMACRO &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002239 i != PP_RMACRO && i != PP_IRMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00002240 i != PP_ENDMACRO && i != PP_ENDM &&
2241 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2242 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002243 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002244
Charles Crayned4200be2008-07-12 16:42:33 -07002245 if (defining) {
Keith Kanios852f1ee2009-07-12 00:19:55 -05002246 if (i == PP_MACRO || i == PP_IMACRO ||
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002247 i == PP_RMACRO || i == PP_IRMACRO) {
Charles Crayned4200be2008-07-12 16:42:33 -07002248 nested_mac_count++;
2249 return NO_DIRECTIVE_FOUND;
2250 } else if (nested_mac_count > 0) {
2251 if (i == PP_ENDMACRO) {
2252 nested_mac_count--;
2253 return NO_DIRECTIVE_FOUND;
2254 }
2255 }
2256 if (!defining->name) {
2257 if (i == PP_REP) {
2258 nested_rep_count++;
2259 return NO_DIRECTIVE_FOUND;
2260 } else if (nested_rep_count > 0) {
2261 if (i == PP_ENDREP) {
2262 nested_rep_count--;
2263 return NO_DIRECTIVE_FOUND;
2264 }
2265 }
2266 }
2267 }
2268
H. Peter Anvin4169a472007-09-12 01:29:43 +00002269 switch (i) {
2270 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002271 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2272 tline->text);
2273 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002274
H. Peter Anvine2c80182005-01-15 22:15:51 +00002275 case PP_STACKSIZE:
2276 /* Directive to tell NASM what the default stack size is. The
2277 * default is for a 16-bit stack, and this can be overriden with
2278 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002279 */
2280 tline = tline->next;
2281 if (tline && tline->type == TOK_WHITESPACE)
2282 tline = tline->next;
2283 if (!tline || tline->type != TOK_ID) {
2284 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2285 free_tlist(origline);
2286 return DIRECTIVE_FOUND;
2287 }
2288 if (nasm_stricmp(tline->text, "flat") == 0) {
2289 /* All subsequent ARG directives are for a 32-bit stack */
2290 StackSize = 4;
2291 StackPointer = "ebp";
2292 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002293 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002294 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2295 /* All subsequent ARG directives are for a 64-bit stack */
2296 StackSize = 8;
2297 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002298 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002299 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002300 } else if (nasm_stricmp(tline->text, "large") == 0) {
2301 /* All subsequent ARG directives are for a 16-bit stack,
2302 * far function call.
2303 */
2304 StackSize = 2;
2305 StackPointer = "bp";
2306 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002307 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002308 } else if (nasm_stricmp(tline->text, "small") == 0) {
2309 /* All subsequent ARG directives are for a 16-bit stack,
2310 * far function call. We don't support near functions.
2311 */
2312 StackSize = 2;
2313 StackPointer = "bp";
2314 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002315 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002316 } else {
2317 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2318 free_tlist(origline);
2319 return DIRECTIVE_FOUND;
2320 }
2321 free_tlist(origline);
2322 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002323
H. Peter Anvine2c80182005-01-15 22:15:51 +00002324 case PP_ARG:
2325 /* TASM like ARG directive to define arguments to functions, in
2326 * the following form:
2327 *
2328 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2329 */
2330 offset = ArgOffset;
2331 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002332 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002333 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002334
H. Peter Anvine2c80182005-01-15 22:15:51 +00002335 /* Find the argument name */
2336 tline = tline->next;
2337 if (tline && tline->type == TOK_WHITESPACE)
2338 tline = tline->next;
2339 if (!tline || tline->type != TOK_ID) {
2340 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2341 free_tlist(origline);
2342 return DIRECTIVE_FOUND;
2343 }
2344 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002345
H. Peter Anvine2c80182005-01-15 22:15:51 +00002346 /* Find the argument size type */
2347 tline = tline->next;
2348 if (!tline || tline->type != TOK_OTHER
2349 || tline->text[0] != ':') {
2350 error(ERR_NONFATAL,
2351 "Syntax error processing `%%arg' directive");
2352 free_tlist(origline);
2353 return DIRECTIVE_FOUND;
2354 }
2355 tline = tline->next;
2356 if (!tline || tline->type != TOK_ID) {
2357 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2358 free_tlist(origline);
2359 return DIRECTIVE_FOUND;
2360 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002361
H. Peter Anvine2c80182005-01-15 22:15:51 +00002362 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002363 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002364 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002365 size = parse_size(tt->text);
2366 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002367 error(ERR_NONFATAL,
2368 "Invalid size type for `%%arg' missing directive");
2369 free_tlist(tt);
2370 free_tlist(origline);
2371 return DIRECTIVE_FOUND;
2372 }
2373 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002374
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002375 /* Round up to even stack slots */
2376 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002377
H. Peter Anvine2c80182005-01-15 22:15:51 +00002378 /* Now define the macro for the argument */
2379 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2380 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002381 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002382 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002383
H. Peter Anvine2c80182005-01-15 22:15:51 +00002384 /* Move to the next argument in the list */
2385 tline = tline->next;
2386 if (tline && tline->type == TOK_WHITESPACE)
2387 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002388 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002389 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002390 free_tlist(origline);
2391 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002392
H. Peter Anvine2c80182005-01-15 22:15:51 +00002393 case PP_LOCAL:
2394 /* TASM like LOCAL directive to define local variables for a
2395 * function, in the following form:
2396 *
2397 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2398 *
2399 * The '= LocalSize' at the end is ignored by NASM, but is
2400 * required by TASM to define the local parameter size (and used
2401 * by the TASM macro package).
2402 */
2403 offset = LocalOffset;
2404 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002405 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002406 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002407
H. Peter Anvine2c80182005-01-15 22:15:51 +00002408 /* Find the argument name */
2409 tline = tline->next;
2410 if (tline && tline->type == TOK_WHITESPACE)
2411 tline = tline->next;
2412 if (!tline || tline->type != TOK_ID) {
2413 error(ERR_NONFATAL,
2414 "`%%local' missing argument parameter");
2415 free_tlist(origline);
2416 return DIRECTIVE_FOUND;
2417 }
2418 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002419
H. Peter Anvine2c80182005-01-15 22:15:51 +00002420 /* Find the argument size type */
2421 tline = tline->next;
2422 if (!tline || tline->type != TOK_OTHER
2423 || tline->text[0] != ':') {
2424 error(ERR_NONFATAL,
2425 "Syntax error processing `%%local' directive");
2426 free_tlist(origline);
2427 return DIRECTIVE_FOUND;
2428 }
2429 tline = tline->next;
2430 if (!tline || tline->type != TOK_ID) {
2431 error(ERR_NONFATAL,
2432 "`%%local' missing size type parameter");
2433 free_tlist(origline);
2434 return DIRECTIVE_FOUND;
2435 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002436
H. Peter Anvine2c80182005-01-15 22:15:51 +00002437 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002438 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002439 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002440 size = parse_size(tt->text);
2441 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002442 error(ERR_NONFATAL,
2443 "Invalid size type for `%%local' missing directive");
2444 free_tlist(tt);
2445 free_tlist(origline);
2446 return DIRECTIVE_FOUND;
2447 }
2448 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002449
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002450 /* Round up to even stack slots */
2451 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002452
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002453 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002454
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002455 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002456 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2457 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002458 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002459
H. Peter Anvine2c80182005-01-15 22:15:51 +00002460 /* Now define the assign to setup the enter_c macro correctly */
2461 snprintf(directive, sizeof(directive),
2462 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002463 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002464
H. Peter Anvine2c80182005-01-15 22:15:51 +00002465 /* Move to the next argument in the list */
2466 tline = tline->next;
2467 if (tline && tline->type == TOK_WHITESPACE)
2468 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002469 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002470 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002471 free_tlist(origline);
2472 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002473
H. Peter Anvine2c80182005-01-15 22:15:51 +00002474 case PP_CLEAR:
2475 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002476 error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002477 "trailing garbage after `%%clear' ignored");
2478 free_macros();
2479 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002480 free_tlist(origline);
2481 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002482
H. Peter Anvin418ca702008-05-30 10:42:30 -07002483 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002484 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002485 skip_white_(t);
2486 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002487 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002488 error(ERR_NONFATAL, "`%%depend' expects a file name");
2489 free_tlist(origline);
2490 return DIRECTIVE_FOUND; /* but we did _something_ */
2491 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002492 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002493 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002494 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002495 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002496 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002497 nasm_unquote_cstr(p, i);
2498 if (dephead && !in_list(*dephead, p)) {
2499 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2500 sl->next = NULL;
2501 strcpy(sl->str, p);
2502 *deptail = sl;
2503 deptail = &sl->next;
2504 }
2505 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002506 return DIRECTIVE_FOUND;
2507
2508 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002509 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002510 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002511
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002512 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002513 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002514 error(ERR_NONFATAL, "`%%include' expects a file name");
2515 free_tlist(origline);
2516 return DIRECTIVE_FOUND; /* but we did _something_ */
2517 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002518 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002519 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002520 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002521 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002522 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002523 nasm_unquote_cstr(p, i);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002524 inc = nasm_malloc(sizeof(Include));
2525 inc->next = istk;
2526 inc->conds = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002527 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002528 if (!inc->fp) {
2529 /* -MG given but file not found */
2530 nasm_free(inc);
2531 } else {
2532 inc->fname = src_set_fname(nasm_strdup(p));
2533 inc->lineno = src_set_linnum(0);
2534 inc->lineinc = 1;
2535 inc->expansion = NULL;
2536 inc->mstk = NULL;
2537 istk = inc;
2538 list->uplevel(LIST_INCLUDE);
2539 }
2540 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002541 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002542
H. Peter Anvind2456592008-06-19 15:04:18 -07002543 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002544 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002545 static macros_t *use_pkg;
2546 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002547
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002548 tline = tline->next;
2549 skip_white_(tline);
2550 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002551
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002552 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002553 tline->type != TOK_INTERNAL_STRING &&
2554 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002555 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002556 free_tlist(origline);
2557 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002558 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002559 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002560 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002561 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002562 if (tline->type == TOK_STRING)
2563 nasm_unquote_cstr(tline->text, i);
2564 use_pkg = nasm_stdmac_find_package(tline->text);
2565 if (!use_pkg)
2566 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2567 else
2568 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002569 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002570 /* Not already included, go ahead and include it */
2571 stdmacpos = use_pkg;
2572 }
2573 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002574 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002575 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002576 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002577 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002578 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002579 tline = tline->next;
2580 skip_white_(tline);
2581 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002582 if (tline) {
2583 if (!tok_type_(tline, TOK_ID)) {
2584 error(ERR_NONFATAL, "`%s' expects a context identifier",
2585 pp_directives[i]);
2586 free_tlist(origline);
2587 return DIRECTIVE_FOUND; /* but we did _something_ */
2588 }
2589 if (tline->next)
2590 error(ERR_WARNING|ERR_PASS1,
2591 "trailing garbage after `%s' ignored",
2592 pp_directives[i]);
2593 p = nasm_strdup(tline->text);
2594 } else {
2595 p = NULL; /* Anonymous */
2596 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002597
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002598 if (i == PP_PUSH) {
2599 ctx = nasm_malloc(sizeof(Context));
2600 ctx->next = cstk;
2601 hash_init(&ctx->localmac, HASH_SMALL);
2602 ctx->name = p;
2603 ctx->number = unique++;
2604 cstk = ctx;
2605 } else {
2606 /* %pop or %repl */
2607 if (!cstk) {
2608 error(ERR_NONFATAL, "`%s': context stack is empty",
2609 pp_directives[i]);
2610 } else if (i == PP_POP) {
2611 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2612 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2613 "expected %s",
2614 cstk->name ? cstk->name : "anonymous", p);
2615 else
2616 ctx_pop();
2617 } else {
2618 /* i == PP_REPL */
2619 nasm_free(cstk->name);
2620 cstk->name = p;
2621 p = NULL;
2622 }
2623 nasm_free(p);
2624 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002625 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002626 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002627 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002628 severity = ERR_FATAL;
2629 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002630 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002631 severity = ERR_NONFATAL;
2632 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002633 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002634 severity = ERR_WARNING|ERR_WARN_USER;
2635 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002636
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002637issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002638 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002639 /* Only error out if this is the final pass */
2640 if (pass != 2 && i != PP_FATAL)
2641 return DIRECTIVE_FOUND;
2642
2643 tline->next = expand_smacro(tline->next);
2644 tline = tline->next;
2645 skip_white_(tline);
2646 t = tline ? tline->next : NULL;
2647 skip_white_(t);
2648 if (tok_type_(tline, TOK_STRING) && !t) {
2649 /* The line contains only a quoted string */
2650 p = tline->text;
2651 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2652 error(severity, "%s", p);
2653 } else {
2654 /* Not a quoted string, or more than a quoted string */
2655 p = detoken(tline, false);
2656 error(severity, "%s", p);
2657 nasm_free(p);
2658 }
2659 free_tlist(origline);
2660 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002661 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002662
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002663 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002664 if (istk->conds && !emitting(istk->conds->state))
2665 j = COND_NEVER;
2666 else {
2667 j = if_condition(tline->next, i);
2668 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002669 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2670 }
2671 cond = nasm_malloc(sizeof(Cond));
2672 cond->next = istk->conds;
2673 cond->state = j;
2674 istk->conds = cond;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002675 if(istk->mstk)
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002676 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002677 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002678 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002679
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002680 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002681 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00002682 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002683 switch(istk->conds->state) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002684 case COND_IF_TRUE:
2685 istk->conds->state = COND_DONE;
2686 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002687
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002688 case COND_DONE:
2689 case COND_NEVER:
2690 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002691
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002692 case COND_ELSE_TRUE:
2693 case COND_ELSE_FALSE:
2694 error_precond(ERR_WARNING|ERR_PASS1,
2695 "`%%elif' after `%%else' ignored");
2696 istk->conds->state = COND_NEVER;
2697 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002698
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002699 case COND_IF_FALSE:
2700 /*
2701 * IMPORTANT: In the case of %if, we will already have
2702 * called expand_mmac_params(); however, if we're
2703 * processing an %elif we must have been in a
2704 * non-emitting mode, which would have inhibited
2705 * the normal invocation of expand_mmac_params().
2706 * Therefore, we have to do it explicitly here.
2707 */
2708 j = if_condition(expand_mmac_params(tline->next), i);
2709 tline->next = NULL; /* it got freed */
2710 istk->conds->state =
2711 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2712 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002713 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002714 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002715 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002716
H. Peter Anvine2c80182005-01-15 22:15:51 +00002717 case PP_ELSE:
2718 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002719 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002720 "trailing garbage after `%%else' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002721 if (!istk->conds)
2722 error(ERR_FATAL, "`%%else': no matching `%%if'");
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002723 switch(istk->conds->state) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002724 case COND_IF_TRUE:
2725 case COND_DONE:
2726 istk->conds->state = COND_ELSE_FALSE;
2727 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002728
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002729 case COND_NEVER:
2730 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002731
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002732 case COND_IF_FALSE:
2733 istk->conds->state = COND_ELSE_TRUE;
2734 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002735
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002736 case COND_ELSE_TRUE:
2737 case COND_ELSE_FALSE:
2738 error_precond(ERR_WARNING|ERR_PASS1,
2739 "`%%else' after `%%else' ignored.");
2740 istk->conds->state = COND_NEVER;
2741 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002742 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002743 free_tlist(origline);
2744 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002745
H. Peter Anvine2c80182005-01-15 22:15:51 +00002746 case PP_ENDIF:
2747 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002748 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002749 "trailing garbage after `%%endif' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002750 if (!istk->conds)
2751 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2752 cond = istk->conds;
2753 istk->conds = cond->next;
2754 nasm_free(cond);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002755 if(istk->mstk)
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002756 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002757 free_tlist(origline);
2758 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002759
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002760 case PP_RMACRO:
2761 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002762 case PP_MACRO:
2763 case PP_IMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07002764 if (defining) {
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002765 error(ERR_FATAL, "`%s': already defining a macro",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002766 pp_directives[i]);
2767 return DIRECTIVE_FOUND;
2768 }
2769 defining = nasm_malloc(sizeof(MMacro));
2770 defining->max_depth =
2771 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2772 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2773 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2774 nasm_free(defining);
2775 defining = NULL;
2776 return DIRECTIVE_FOUND;
2777 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002778
H. Peter Anvin166c2472008-05-28 12:28:58 -07002779 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002780 while (mmac) {
2781 if (!strcmp(mmac->name, defining->name) &&
2782 (mmac->nparam_min <= defining->nparam_max
2783 || defining->plus)
2784 && (defining->nparam_min <= mmac->nparam_max
2785 || mmac->plus)) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07002786 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002787 "redefining multi-line macro `%s'", defining->name);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002788 return DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002789 }
2790 mmac = mmac->next;
2791 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002792 free_tlist(origline);
2793 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002794
H. Peter Anvine2c80182005-01-15 22:15:51 +00002795 case PP_ENDM:
2796 case PP_ENDMACRO:
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002797 if (! (defining && defining->name)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002798 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2799 return DIRECTIVE_FOUND;
2800 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002801 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002802 defining->next = *mmhead;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002803 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002804 defining = NULL;
2805 free_tlist(origline);
2806 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002807
H. Peter Anvin89cee572009-07-15 09:16:54 -04002808 case PP_EXITMACRO:
Keith Kanios852f1ee2009-07-12 00:19:55 -05002809 /*
2810 * We must search along istk->expansion until we hit a
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002811 * macro-end marker for a macro with a name. Then we
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002812 * bypass all lines between exitmacro and endmacro.
Keith Kanios852f1ee2009-07-12 00:19:55 -05002813 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04002814 list_for_each(l, istk->expansion)
Keith Kanios852f1ee2009-07-12 00:19:55 -05002815 if (l->finishes && l->finishes->name)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002816 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002817
2818 if (l) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002819 /*
2820 * Remove all conditional entries relative to this
2821 * macro invocation. (safe to do in this context)
2822 */
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002823 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2824 cond = istk->conds;
2825 istk->conds = cond->next;
2826 nasm_free(cond);
2827 }
2828 istk->expansion = l;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002829 } else {
2830 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002831 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002832 free_tlist(origline);
2833 return DIRECTIVE_FOUND;
2834
H. Peter Anvina26433d2008-07-16 14:40:01 -07002835 case PP_UNMACRO:
2836 case PP_UNIMACRO:
2837 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002838 MMacro **mmac_p;
2839 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002840
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002841 spec.casesense = (i == PP_UNMACRO);
2842 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2843 return DIRECTIVE_FOUND;
2844 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002845 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2846 while (mmac_p && *mmac_p) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002847 mmac = *mmac_p;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002848 if (mmac->casesense == spec.casesense &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002849 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
H. Peter Anvina26433d2008-07-16 14:40:01 -07002850 mmac->nparam_min == spec.nparam_min &&
2851 mmac->nparam_max == spec.nparam_max &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002852 mmac->plus == spec.plus) {
2853 *mmac_p = mmac->next;
2854 free_mmacro(mmac);
2855 } else {
2856 mmac_p = &mmac->next;
2857 }
2858 }
2859 free_tlist(origline);
2860 free_tlist(spec.dlist);
2861 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002862 }
2863
H. Peter Anvine2c80182005-01-15 22:15:51 +00002864 case PP_ROTATE:
2865 if (tline->next && tline->next->type == TOK_WHITESPACE)
2866 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002867 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002868 free_tlist(origline);
2869 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2870 return DIRECTIVE_FOUND;
2871 }
2872 t = expand_smacro(tline->next);
2873 tline->next = NULL;
2874 free_tlist(origline);
2875 tline = t;
2876 tptr = &t;
2877 tokval.t_type = TOKEN_INVALID;
2878 evalresult =
2879 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2880 free_tlist(tline);
2881 if (!evalresult)
2882 return DIRECTIVE_FOUND;
2883 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002884 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002885 "trailing garbage after expression ignored");
2886 if (!is_simple(evalresult)) {
2887 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2888 return DIRECTIVE_FOUND;
2889 }
2890 mmac = istk->mstk;
2891 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2892 mmac = mmac->next_active;
2893 if (!mmac) {
2894 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2895 } else if (mmac->nparam == 0) {
2896 error(ERR_NONFATAL,
2897 "`%%rotate' invoked within macro without parameters");
2898 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002899 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002900
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002901 rotate %= (int)mmac->nparam;
2902 if (rotate < 0)
2903 rotate += mmac->nparam;
H. Peter Anvin70653092007-10-19 14:42:29 -07002904
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002905 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002906 }
2907 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002908
H. Peter Anvine2c80182005-01-15 22:15:51 +00002909 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002910 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002911 do {
2912 tline = tline->next;
2913 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002914
H. Peter Anvine2c80182005-01-15 22:15:51 +00002915 if (tok_type_(tline, TOK_ID) &&
2916 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002917 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002918 do {
2919 tline = tline->next;
2920 } while (tok_type_(tline, TOK_WHITESPACE));
2921 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002922
H. Peter Anvine2c80182005-01-15 22:15:51 +00002923 if (tline) {
2924 t = expand_smacro(tline);
2925 tptr = &t;
2926 tokval.t_type = TOKEN_INVALID;
2927 evalresult =
2928 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2929 if (!evalresult) {
2930 free_tlist(origline);
2931 return DIRECTIVE_FOUND;
2932 }
2933 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002934 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002935 "trailing garbage after expression ignored");
2936 if (!is_simple(evalresult)) {
2937 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2938 return DIRECTIVE_FOUND;
2939 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002940 count = reloc_value(evalresult);
2941 if (count >= REP_LIMIT) {
Cyrill Gorcunov71f4f842010-08-09 20:17:17 +04002942 error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002943 count = 0;
2944 } else
2945 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002946 } else {
2947 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002948 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002949 }
2950 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002951
H. Peter Anvine2c80182005-01-15 22:15:51 +00002952 tmp_defining = defining;
2953 defining = nasm_malloc(sizeof(MMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002954 defining->prev = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002955 defining->name = NULL; /* flags this macro as a %rep block */
H. Peter Anvin70055962007-10-11 00:05:31 -07002956 defining->casesense = false;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002957 defining->plus = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002958 defining->nolist = nolist;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002959 defining->in_progress = count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002960 defining->max_depth = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002961 defining->nparam_min = defining->nparam_max = 0;
2962 defining->defaults = NULL;
2963 defining->dlist = NULL;
2964 defining->expansion = NULL;
2965 defining->next_active = istk->mstk;
2966 defining->rep_nest = tmp_defining;
2967 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002968
H. Peter Anvine2c80182005-01-15 22:15:51 +00002969 case PP_ENDREP:
2970 if (!defining || defining->name) {
2971 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2972 return DIRECTIVE_FOUND;
2973 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002974
H. Peter Anvine2c80182005-01-15 22:15:51 +00002975 /*
2976 * Now we have a "macro" defined - although it has no name
2977 * and we won't be entering it in the hash tables - we must
2978 * push a macro-end marker for it on to istk->expansion.
2979 * After that, it will take care of propagating itself (a
2980 * macro-end marker line for a macro which is really a %rep
2981 * block will cause the macro to be re-expanded, complete
2982 * with another macro-end marker to ensure the process
2983 * continues) until the whole expansion is forcibly removed
2984 * from istk->expansion by a %exitrep.
2985 */
2986 l = nasm_malloc(sizeof(Line));
2987 l->next = istk->expansion;
2988 l->finishes = defining;
2989 l->first = NULL;
2990 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002991
H. Peter Anvine2c80182005-01-15 22:15:51 +00002992 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002993
H. Peter Anvine2c80182005-01-15 22:15:51 +00002994 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2995 tmp_defining = defining;
2996 defining = defining->rep_nest;
2997 free_tlist(origline);
2998 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002999
H. Peter Anvine2c80182005-01-15 22:15:51 +00003000 case PP_EXITREP:
3001 /*
3002 * We must search along istk->expansion until we hit a
3003 * macro-end marker for a macro with no name. Then we set
3004 * its `in_progress' flag to 0.
3005 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003006 list_for_each(l, istk->expansion)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003007 if (l->finishes && !l->finishes->name)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003008 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003009
H. Peter Anvine2c80182005-01-15 22:15:51 +00003010 if (l)
Charles Crayned4200be2008-07-12 16:42:33 -07003011 l->finishes->in_progress = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003012 else
3013 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
3014 free_tlist(origline);
3015 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003016
H. Peter Anvine2c80182005-01-15 22:15:51 +00003017 case PP_XDEFINE:
3018 case PP_IXDEFINE:
3019 case PP_DEFINE:
3020 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003021 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003022
H. Peter Anvine2c80182005-01-15 22:15:51 +00003023 tline = tline->next;
3024 skip_white_(tline);
3025 tline = expand_id(tline);
3026 if (!tline || (tline->type != TOK_ID &&
3027 (tline->type != TOK_PREPROC_ID ||
3028 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003029 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003030 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003031 free_tlist(origline);
3032 return DIRECTIVE_FOUND;
3033 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003034
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003035 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003036 last = tline;
3037 param_start = tline = tline->next;
3038 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003039
H. Peter Anvine2c80182005-01-15 22:15:51 +00003040 /* Expand the macro definition now for %xdefine and %ixdefine */
3041 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3042 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003043
H. Peter Anvine2c80182005-01-15 22:15:51 +00003044 if (tok_is_(tline, "(")) {
3045 /*
3046 * This macro has parameters.
3047 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003048
H. Peter Anvine2c80182005-01-15 22:15:51 +00003049 tline = tline->next;
3050 while (1) {
3051 skip_white_(tline);
3052 if (!tline) {
3053 error(ERR_NONFATAL, "parameter identifier expected");
3054 free_tlist(origline);
3055 return DIRECTIVE_FOUND;
3056 }
3057 if (tline->type != TOK_ID) {
3058 error(ERR_NONFATAL,
3059 "`%s': parameter identifier expected",
3060 tline->text);
3061 free_tlist(origline);
3062 return DIRECTIVE_FOUND;
3063 }
3064 tline->type = TOK_SMAC_PARAM + nparam++;
3065 tline = tline->next;
3066 skip_white_(tline);
3067 if (tok_is_(tline, ",")) {
3068 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003069 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003070 if (!tok_is_(tline, ")")) {
3071 error(ERR_NONFATAL,
3072 "`)' expected to terminate macro template");
3073 free_tlist(origline);
3074 return DIRECTIVE_FOUND;
3075 }
3076 break;
3077 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003078 }
3079 last = tline;
3080 tline = tline->next;
3081 }
3082 if (tok_type_(tline, TOK_WHITESPACE))
3083 last = tline, tline = tline->next;
3084 macro_start = NULL;
3085 last->next = NULL;
3086 t = tline;
3087 while (t) {
3088 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003089 list_for_each(tt, param_start)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003090 if (tt->type >= TOK_SMAC_PARAM &&
3091 !strcmp(tt->text, t->text))
3092 t->type = tt->type;
3093 }
3094 tt = t->next;
3095 t->next = macro_start;
3096 macro_start = t;
3097 t = tt;
3098 }
3099 /*
3100 * Good. We now have a macro name, a parameter count, and a
3101 * token list (in reverse order) for an expansion. We ought
3102 * to be OK just to create an SMacro, store it, and let
3103 * free_tlist have the rest of the line (which we have
3104 * carefully re-terminated after chopping off the expansion
3105 * from the end).
3106 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003107 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003108 free_tlist(origline);
3109 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003110
H. Peter Anvine2c80182005-01-15 22:15:51 +00003111 case PP_UNDEF:
3112 tline = tline->next;
3113 skip_white_(tline);
3114 tline = expand_id(tline);
3115 if (!tline || (tline->type != TOK_ID &&
3116 (tline->type != TOK_PREPROC_ID ||
3117 tline->text[1] != '$'))) {
3118 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3119 free_tlist(origline);
3120 return DIRECTIVE_FOUND;
3121 }
3122 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07003123 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003124 "trailing garbage after macro name ignored");
3125 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003126
H. Peter Anvine2c80182005-01-15 22:15:51 +00003127 /* Find the context that symbol belongs to */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003128 ctx = get_ctx(tline->text, &mname, false);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003129 undef_smacro(ctx, mname);
3130 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003131 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003132
H. Peter Anvin9e200162008-06-04 17:23:14 -07003133 case PP_DEFSTR:
3134 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003135 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003136
3137 tline = tline->next;
3138 skip_white_(tline);
3139 tline = expand_id(tline);
3140 if (!tline || (tline->type != TOK_ID &&
3141 (tline->type != TOK_PREPROC_ID ||
3142 tline->text[1] != '$'))) {
3143 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003144 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003145 free_tlist(origline);
3146 return DIRECTIVE_FOUND;
3147 }
3148
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003149 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003150 last = tline;
3151 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003152 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003153
3154 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003155 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003156
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003157 p = detoken(tline, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003158 macro_start = nasm_malloc(sizeof(*macro_start));
3159 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003160 macro_start->text = nasm_quote(p, strlen(p));
3161 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003162 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003163 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003164
3165 /*
3166 * We now have a macro name, an implicit parameter count of
3167 * zero, and a string token to use as an expansion. Create
3168 * and store an SMacro.
3169 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003170 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003171 free_tlist(origline);
3172 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003173
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003174 case PP_DEFTOK:
3175 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003176 casesense = (i == PP_DEFTOK);
3177
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003178 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 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003186 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003187 free_tlist(origline);
3188 return DIRECTIVE_FOUND;
3189 }
3190 ctx = get_ctx(tline->text, &mname, false);
3191 last = tline;
3192 tline = expand_smacro(tline->next);
3193 last->next = NULL;
3194
3195 t = tline;
3196 while (tok_type_(t, TOK_WHITESPACE))
3197 t = t->next;
3198 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003199 if (!tok_type_(t, TOK_STRING)) {
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003200 error(ERR_NONFATAL,
3201 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003202 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003203 free_tlist(tline);
3204 free_tlist(origline);
3205 return DIRECTIVE_FOUND;
3206 }
3207
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003208 /*
3209 * Convert the string to a token stream. Note that smacros
3210 * are stored with the token stream reversed, so we have to
3211 * reverse the output of tokenize().
3212 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003213 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003214 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003215
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003216 /*
3217 * We now have a macro name, an implicit parameter count of
3218 * zero, and a numeric token to use as an expansion. Create
3219 * and store an SMacro.
3220 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003221 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003222 free_tlist(tline);
3223 free_tlist(origline);
3224 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003225
H. Peter Anvin418ca702008-05-30 10:42:30 -07003226 case PP_PATHSEARCH:
3227 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003228 FILE *fp;
3229 StrList *xsl = NULL;
3230 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003231
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003232 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003233
3234 tline = tline->next;
3235 skip_white_(tline);
3236 tline = expand_id(tline);
3237 if (!tline || (tline->type != TOK_ID &&
3238 (tline->type != TOK_PREPROC_ID ||
3239 tline->text[1] != '$'))) {
3240 error(ERR_NONFATAL,
3241 "`%%pathsearch' expects a macro identifier as first parameter");
3242 free_tlist(origline);
3243 return DIRECTIVE_FOUND;
3244 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003245 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003246 last = tline;
3247 tline = expand_smacro(tline->next);
3248 last->next = NULL;
3249
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003250 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003251 while (tok_type_(t, TOK_WHITESPACE))
3252 t = t->next;
3253
3254 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003255 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07003256 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003257 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003258 free_tlist(origline);
3259 return DIRECTIVE_FOUND; /* but we did _something_ */
3260 }
3261 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003262 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003263 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003264 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003265 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003266 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003267
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003268 fp = inc_fopen(p, &xsl, &xst, true);
3269 if (fp) {
3270 p = xsl->str;
3271 fclose(fp); /* Don't actually care about the file */
3272 }
H. Peter Anvin418ca702008-05-30 10:42:30 -07003273 macro_start = nasm_malloc(sizeof(*macro_start));
3274 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003275 macro_start->text = nasm_quote(p, strlen(p));
3276 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003277 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003278 if (xsl)
3279 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003280
3281 /*
3282 * We now have a macro name, an implicit parameter count of
3283 * zero, and a string token to use as an expansion. Create
3284 * and store an SMacro.
3285 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003286 define_smacro(ctx, mname, casesense, 0, macro_start);
3287 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003288 free_tlist(origline);
3289 return DIRECTIVE_FOUND;
3290 }
3291
H. Peter Anvine2c80182005-01-15 22:15:51 +00003292 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003293 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003294
H. Peter Anvine2c80182005-01-15 22:15:51 +00003295 tline = tline->next;
3296 skip_white_(tline);
3297 tline = expand_id(tline);
3298 if (!tline || (tline->type != TOK_ID &&
3299 (tline->type != TOK_PREPROC_ID ||
3300 tline->text[1] != '$'))) {
3301 error(ERR_NONFATAL,
3302 "`%%strlen' expects a macro identifier as first parameter");
3303 free_tlist(origline);
3304 return DIRECTIVE_FOUND;
3305 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003306 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003307 last = tline;
3308 tline = expand_smacro(tline->next);
3309 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003310
H. Peter Anvine2c80182005-01-15 22:15:51 +00003311 t = tline;
3312 while (tok_type_(t, TOK_WHITESPACE))
3313 t = t->next;
3314 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003315 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003316 error(ERR_NONFATAL,
3317 "`%%strlen` requires string as second parameter");
3318 free_tlist(tline);
3319 free_tlist(origline);
3320 return DIRECTIVE_FOUND;
3321 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003322
H. Peter Anvine2c80182005-01-15 22:15:51 +00003323 macro_start = nasm_malloc(sizeof(*macro_start));
3324 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003325 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003326 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003327
H. Peter Anvine2c80182005-01-15 22:15:51 +00003328 /*
3329 * We now have a macro name, an implicit parameter count of
3330 * zero, and a numeric token to use as an expansion. Create
3331 * and store an SMacro.
3332 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003333 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003334 free_tlist(tline);
3335 free_tlist(origline);
3336 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003337
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003338 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003339 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003340
3341 tline = tline->next;
3342 skip_white_(tline);
3343 tline = expand_id(tline);
3344 if (!tline || (tline->type != TOK_ID &&
3345 (tline->type != TOK_PREPROC_ID ||
3346 tline->text[1] != '$'))) {
3347 error(ERR_NONFATAL,
3348 "`%%strcat' expects a macro identifier as first parameter");
3349 free_tlist(origline);
3350 return DIRECTIVE_FOUND;
3351 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003352 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003353 last = tline;
3354 tline = expand_smacro(tline->next);
3355 last->next = NULL;
3356
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003357 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003358 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003359 switch (t->type) {
3360 case TOK_WHITESPACE:
3361 break;
3362 case TOK_STRING:
3363 len += t->a.len = nasm_unquote(t->text, NULL);
3364 break;
3365 case TOK_OTHER:
3366 if (!strcmp(t->text, ",")) /* permit comma separators */
3367 break;
3368 /* else fall through */
3369 default:
3370 error(ERR_NONFATAL,
3371 "non-string passed to `%%strcat' (%d)", t->type);
3372 free_tlist(tline);
3373 free_tlist(origline);
3374 return DIRECTIVE_FOUND;
3375 }
3376 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003377
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003378 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003379 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003380 if (t->type == TOK_STRING) {
3381 memcpy(p, t->text, t->a.len);
3382 p += t->a.len;
3383 }
3384 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003385
3386 /*
3387 * We now have a macro name, an implicit parameter count of
3388 * zero, and a numeric token to use as an expansion. Create
3389 * and store an SMacro.
3390 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003391 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3392 macro_start->text = nasm_quote(pp, len);
3393 nasm_free(pp);
3394 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003395 free_tlist(tline);
3396 free_tlist(origline);
3397 return DIRECTIVE_FOUND;
3398
H. Peter Anvine2c80182005-01-15 22:15:51 +00003399 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003400 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003401 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003402 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003403
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003404 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003405
H. Peter Anvine2c80182005-01-15 22:15:51 +00003406 tline = tline->next;
3407 skip_white_(tline);
3408 tline = expand_id(tline);
3409 if (!tline || (tline->type != TOK_ID &&
3410 (tline->type != TOK_PREPROC_ID ||
3411 tline->text[1] != '$'))) {
3412 error(ERR_NONFATAL,
3413 "`%%substr' expects a macro identifier as first parameter");
3414 free_tlist(origline);
3415 return DIRECTIVE_FOUND;
3416 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003417 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003418 last = tline;
3419 tline = expand_smacro(tline->next);
3420 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003421
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003422 if (tline) /* skip expanded id */
3423 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003424 while (tok_type_(t, TOK_WHITESPACE))
3425 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003426
H. Peter Anvine2c80182005-01-15 22:15:51 +00003427 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003428 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003429 error(ERR_NONFATAL,
3430 "`%%substr` requires string as second parameter");
3431 free_tlist(tline);
3432 free_tlist(origline);
3433 return DIRECTIVE_FOUND;
3434 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003435
H. Peter Anvine2c80182005-01-15 22:15:51 +00003436 tt = t->next;
3437 tptr = &tt;
3438 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003439 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003440 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003441 if (!evalresult) {
3442 free_tlist(tline);
3443 free_tlist(origline);
3444 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003445 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003446 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3447 free_tlist(tline);
3448 free_tlist(origline);
3449 return DIRECTIVE_FOUND;
3450 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003451 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003452
3453 while (tok_type_(tt, TOK_WHITESPACE))
3454 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003455 if (!tt) {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003456 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003457 } else {
3458 tokval.t_type = TOKEN_INVALID;
3459 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3460 pass, error, NULL);
3461 if (!evalresult) {
3462 free_tlist(tline);
3463 free_tlist(origline);
3464 return DIRECTIVE_FOUND;
3465 } else if (!is_simple(evalresult)) {
3466 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3467 free_tlist(tline);
3468 free_tlist(origline);
3469 return DIRECTIVE_FOUND;
3470 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003471 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003472 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003473
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003474 len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003475
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003476 /* make start and count being in range */
3477 if (start < 0)
3478 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003479 if (count < 0)
3480 count = len + count + 1 - start;
3481 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003482 count = len - start;
3483 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003484 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003485
H. Peter Anvine2c80182005-01-15 22:15:51 +00003486 macro_start = nasm_malloc(sizeof(*macro_start));
3487 macro_start->next = NULL;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003488 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003489 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003490 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003491
H. Peter Anvine2c80182005-01-15 22:15:51 +00003492 /*
3493 * We now have a macro name, an implicit parameter count of
3494 * zero, and a numeric token to use as an expansion. Create
3495 * and store an SMacro.
3496 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003497 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003498 free_tlist(tline);
3499 free_tlist(origline);
3500 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003501 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003502
H. Peter Anvine2c80182005-01-15 22:15:51 +00003503 case PP_ASSIGN:
3504 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003505 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003506
H. Peter Anvine2c80182005-01-15 22:15:51 +00003507 tline = tline->next;
3508 skip_white_(tline);
3509 tline = expand_id(tline);
3510 if (!tline || (tline->type != TOK_ID &&
3511 (tline->type != TOK_PREPROC_ID ||
3512 tline->text[1] != '$'))) {
3513 error(ERR_NONFATAL,
3514 "`%%%sassign' expects a macro identifier",
3515 (i == PP_IASSIGN ? "i" : ""));
3516 free_tlist(origline);
3517 return DIRECTIVE_FOUND;
3518 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003519 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003520 last = tline;
3521 tline = expand_smacro(tline->next);
3522 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003523
H. Peter Anvine2c80182005-01-15 22:15:51 +00003524 t = tline;
3525 tptr = &t;
3526 tokval.t_type = TOKEN_INVALID;
3527 evalresult =
3528 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3529 free_tlist(tline);
3530 if (!evalresult) {
3531 free_tlist(origline);
3532 return DIRECTIVE_FOUND;
3533 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003534
H. Peter Anvine2c80182005-01-15 22:15:51 +00003535 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003536 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003537 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003538
H. Peter Anvine2c80182005-01-15 22:15:51 +00003539 if (!is_simple(evalresult)) {
3540 error(ERR_NONFATAL,
3541 "non-constant value given to `%%%sassign'",
3542 (i == PP_IASSIGN ? "i" : ""));
3543 free_tlist(origline);
3544 return DIRECTIVE_FOUND;
3545 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003546
H. Peter Anvine2c80182005-01-15 22:15:51 +00003547 macro_start = nasm_malloc(sizeof(*macro_start));
3548 macro_start->next = NULL;
3549 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003550 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003551
H. Peter Anvine2c80182005-01-15 22:15:51 +00003552 /*
3553 * We now have a macro name, an implicit parameter count of
3554 * zero, and a numeric token to use as an expansion. Create
3555 * and store an SMacro.
3556 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003557 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003558 free_tlist(origline);
3559 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003560
H. Peter Anvine2c80182005-01-15 22:15:51 +00003561 case PP_LINE:
3562 /*
3563 * Syntax is `%line nnn[+mmm] [filename]'
3564 */
3565 tline = tline->next;
3566 skip_white_(tline);
3567 if (!tok_type_(tline, TOK_NUMBER)) {
3568 error(ERR_NONFATAL, "`%%line' expects line number");
3569 free_tlist(origline);
3570 return DIRECTIVE_FOUND;
3571 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003572 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003573 m = 1;
3574 tline = tline->next;
3575 if (tok_is_(tline, "+")) {
3576 tline = tline->next;
3577 if (!tok_type_(tline, TOK_NUMBER)) {
3578 error(ERR_NONFATAL, "`%%line' expects line increment");
3579 free_tlist(origline);
3580 return DIRECTIVE_FOUND;
3581 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003582 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003583 tline = tline->next;
3584 }
3585 skip_white_(tline);
3586 src_set_linnum(k);
3587 istk->lineinc = m;
3588 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003589 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003590 }
3591 free_tlist(origline);
3592 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003593
H. Peter Anvine2c80182005-01-15 22:15:51 +00003594 default:
3595 error(ERR_FATAL,
3596 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003597 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003598 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003599 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003600}
3601
3602/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003603 * Ensure that a macro parameter contains a condition code and
3604 * nothing else. Return the condition code index if so, or -1
3605 * otherwise.
3606 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003607static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003608{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003609 Token *tt;
3610 int i, j, k, m;
3611
H. Peter Anvin25a99342007-09-22 17:45:45 -07003612 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003613 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003614
H. Peter Anvineba20a72002-04-30 20:53:55 +00003615 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003616 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003617 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003618 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003619 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003620 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003621 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003622
3623 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +04003624 j = ARRAY_SIZE(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003625 while (j - i > 1) {
3626 k = (j + i) / 2;
3627 m = nasm_stricmp(t->text, conditions[k]);
3628 if (m == 0) {
3629 i = k;
3630 j = -2;
3631 break;
3632 } else if (m < 0) {
3633 j = k;
3634 } else
3635 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003636 }
3637 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003638 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003639 return i;
3640}
3641
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003642static bool paste_tokens(Token **head, int mask_head, int mask_tail,
3643 bool handle_paste_tokens)
H. Peter Anvind784a082009-04-20 14:01:18 -07003644{
3645 Token **tail, *t, *tt;
3646 Token **paste_head;
3647 bool did_paste = false;
3648 char *tmp;
3649
3650 /* Now handle token pasting... */
3651 paste_head = NULL;
3652 tail = head;
3653 while ((t = *tail) && (tt = t->next)) {
3654 switch (t->type) {
3655 case TOK_WHITESPACE:
3656 if (tt->type == TOK_WHITESPACE) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003657 /* Zap adjacent whitespace tokens */
H. Peter Anvind784a082009-04-20 14:01:18 -07003658 t->next = delete_Token(tt);
3659 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003660 /* Do not advance paste_head here */
3661 tail = &t->next;
3662 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003663 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003664 case TOK_PASTE: /* %+ */
3665 if (handle_paste_tokens) {
3666 /* Zap %+ and whitespace tokens to the right */
3667 while (t && (t->type == TOK_WHITESPACE ||
3668 t->type == TOK_PASTE))
3669 t = *tail = delete_Token(t);
3670 if (!paste_head || !t)
3671 break; /* Nothing to paste with */
3672 tail = paste_head;
3673 t = *tail;
3674 tt = t->next;
3675 while (tok_type_(tt, TOK_WHITESPACE))
3676 tt = t->next = delete_Token(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003677 if (tt) {
3678 tmp = nasm_strcat(t->text, tt->text);
3679 delete_Token(t);
3680 tt = delete_Token(tt);
3681 t = *tail = tokenize(tmp);
3682 nasm_free(tmp);
3683 while (t->next) {
3684 tail = &t->next;
3685 t = t->next;
3686 }
3687 t->next = tt; /* Attach the remaining token chain */
3688 did_paste = true;
3689 }
3690 paste_head = tail;
3691 tail = &t->next;
3692 break;
3693 }
3694 /* else fall through */
3695 default:
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003696 /*
3697 * Concatenation of tokens might look nontrivial
3698 * but in real it's pretty simple -- the caller
3699 * prepares the masks of tokens to be concatenated
3700 * and we simply find matched sequences and slip
3701 * them together
3702 */
3703 if (PP_CONCAT_MASK(t->type) & mask_head) {
3704 size_t len = 0;
3705 char *tmp, *p;
3706
3707 while (tt && (PP_CONCAT_MASK(tt->type) & mask_tail)) {
3708 len += strlen(tt->text);
3709 tt = tt->next;
3710 }
3711
3712 /*
3713 * Now tt points to the first token after
3714 * the potential paste area...
3715 */
3716 if (tt != t->next) {
3717 /* We have at least two tokens... */
3718 len += strlen(t->text);
3719 p = tmp = nasm_malloc(len+1);
3720 while (t != tt) {
3721 strcpy(p, t->text);
3722 p = strchr(p, '\0');
3723 t = delete_Token(t);
3724 }
3725 t = *tail = tokenize(tmp);
3726 nasm_free(tmp);
3727 while (t->next) {
3728 tail = &t->next;
3729 t = t->next;
3730 }
3731 t->next = tt; /* Attach the remaining token chain */
3732 did_paste = true;
3733 }
Cyrill Gorcunovadabc152010-07-10 02:11:41 +04003734 paste_head = tail;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003735 tail = &t->next;
3736 } else {
3737 tail = &t->next;
3738 if (!tok_type_(t->next, TOK_WHITESPACE))
3739 paste_head = tail;
3740 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003741 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003742 }
3743 }
3744 return did_paste;
3745}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003746
3747/*
3748 * expands to a list of tokens from %{x:y}
3749 */
3750static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
3751{
3752 Token *t = tline, **tt, *tm, *head;
3753 char *pos;
3754 int fst, lst, j, i;
3755
3756 pos = strchr(tline->text, ':');
3757 nasm_assert(pos);
3758
3759 lst = atoi(pos + 1);
3760 fst = atoi(tline->text + 1);
3761
3762 /*
3763 * only macros params are accounted so
3764 * if someone passes %0 -- we reject such
3765 * value(s)
3766 */
3767 if (lst == 0 || fst == 0)
3768 goto err;
3769
3770 /* the values should be sane */
Cyrill Gorcunov2f403752010-06-05 10:47:10 +04003771 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3772 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003773 goto err;
3774
Cyrill Gorcunovefb35832010-06-20 01:52:19 +04003775 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3776 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003777
3778 /* counted from zero */
3779 fst--, lst--;
3780
3781 /*
3782 * it will be at least one token
3783 */
3784 tm = mac->params[(fst + mac->rotate) % mac->nparam];
3785 t = new_Token(NULL, tm->type, tm->text, 0);
3786 head = t, tt = &t->next;
3787 if (fst < lst) {
3788 for (i = fst + 1; i <= lst; i++) {
3789 t = new_Token(NULL, TOK_OTHER, ",", 0);
3790 *tt = t, tt = &t->next;
3791 j = (i + mac->rotate) % mac->nparam;
3792 tm = mac->params[j];
3793 t = new_Token(NULL, tm->type, tm->text, 0);
3794 *tt = t, tt = &t->next;
3795 }
3796 } else {
3797 for (i = fst - 1; i >= lst; i--) {
3798 t = new_Token(NULL, TOK_OTHER, ",", 0);
3799 *tt = t, tt = &t->next;
3800 j = (i + mac->rotate) % mac->nparam;
3801 tm = mac->params[j];
3802 t = new_Token(NULL, tm->type, tm->text, 0);
3803 *tt = t, tt = &t->next;
3804 }
3805 }
3806
3807 *last = tt;
3808 return head;
3809
3810err:
3811 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
3812 &tline->text[1]);
3813 return tline;
3814}
3815
H. Peter Anvin76690a12002-04-30 20:52:49 +00003816/*
3817 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003818 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003819 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003820 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003821static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003822{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003823 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003824 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003825 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003826
3827 tail = &thead;
3828 thead = NULL;
3829
H. Peter Anvine2c80182005-01-15 22:15:51 +00003830 while (tline) {
3831 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003832 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3833 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3834 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003835 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003836 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003837 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003838 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003839 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003840 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003841
H. Peter Anvine2c80182005-01-15 22:15:51 +00003842 t = tline;
3843 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003844
H. Peter Anvine2c80182005-01-15 22:15:51 +00003845 mac = istk->mstk;
3846 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3847 mac = mac->next_active;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003848 if (!mac) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003849 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003850 } else {
3851 pos = strchr(t->text, ':');
3852 if (!pos) {
3853 switch (t->text[1]) {
3854 /*
3855 * We have to make a substitution of one of the
3856 * forms %1, %-1, %+1, %%foo, %0.
3857 */
3858 case '0':
3859 type = TOK_NUMBER;
3860 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3861 text = nasm_strdup(tmpbuf);
3862 break;
3863 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00003864 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003865 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
3866 mac->unique);
3867 text = nasm_strcat(tmpbuf, t->text + 2);
3868 break;
3869 case '-':
3870 n = atoi(t->text + 2) - 1;
3871 if (n >= mac->nparam)
3872 tt = NULL;
3873 else {
3874 if (mac->nparam > 1)
3875 n = (n + mac->rotate) % mac->nparam;
3876 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003877 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003878 cc = find_cc(tt);
3879 if (cc == -1) {
3880 error(ERR_NONFATAL,
3881 "macro parameter %d is not a condition code",
3882 n + 1);
3883 text = NULL;
3884 } else {
3885 type = TOK_ID;
3886 if (inverse_ccs[cc] == -1) {
3887 error(ERR_NONFATAL,
3888 "condition code `%s' is not invertible",
3889 conditions[cc]);
3890 text = NULL;
3891 } else
3892 text = nasm_strdup(conditions[inverse_ccs[cc]]);
3893 }
3894 break;
3895 case '+':
3896 n = atoi(t->text + 2) - 1;
3897 if (n >= mac->nparam)
3898 tt = NULL;
3899 else {
3900 if (mac->nparam > 1)
3901 n = (n + mac->rotate) % mac->nparam;
3902 tt = mac->params[n];
3903 }
3904 cc = find_cc(tt);
3905 if (cc == -1) {
3906 error(ERR_NONFATAL,
3907 "macro parameter %d is not a condition code",
3908 n + 1);
3909 text = NULL;
3910 } else {
3911 type = TOK_ID;
3912 text = nasm_strdup(conditions[cc]);
3913 }
3914 break;
3915 default:
3916 n = atoi(t->text + 1) - 1;
3917 if (n >= mac->nparam)
3918 tt = NULL;
3919 else {
3920 if (mac->nparam > 1)
3921 n = (n + mac->rotate) % mac->nparam;
3922 tt = mac->params[n];
3923 }
3924 if (tt) {
3925 for (i = 0; i < mac->paramlen[n]; i++) {
3926 *tail = new_Token(NULL, tt->type, tt->text, 0);
3927 tail = &(*tail)->next;
3928 tt = tt->next;
3929 }
3930 }
3931 text = NULL; /* we've done it here */
3932 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003933 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003934 } else {
3935 /*
3936 * seems we have a parameters range here
3937 */
3938 Token *head, **last;
3939 head = expand_mmac_params_range(mac, t, &last);
3940 if (head != t) {
3941 *tail = head;
3942 *last = tline;
3943 tline = head;
3944 text = NULL;
3945 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003946 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003947 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003948 if (!text) {
3949 delete_Token(t);
3950 } else {
3951 *tail = t;
3952 tail = &t->next;
3953 t->type = type;
3954 nasm_free(t->text);
3955 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003956 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003957 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003958 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003959 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003960 } else if (tline->type == TOK_INDIRECT) {
3961 t = tline;
3962 tline = tline->next;
3963 tt = tokenize(t->text);
3964 tt = expand_mmac_params(tt);
3965 tt = expand_smacro(tt);
3966 *tail = tt;
3967 while (tt) {
3968 tt->a.mac = NULL; /* Necessary? */
3969 tail = &tt->next;
3970 tt = tt->next;
3971 }
3972 delete_Token(t);
3973 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003974 } else {
3975 t = *tail = tline;
3976 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003977 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003978 tail = &t->next;
3979 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003980 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003981 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07003982
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003983 if (changed) {
3984 int mask_head = PP_CONCAT_MASK(TOK_ID) |
3985 PP_CONCAT_MASK(TOK_NUMBER) |
3986 PP_CONCAT_MASK(TOK_FLOAT);
3987 int mask_tail = PP_CONCAT_MASK(TOK_ID) |
3988 PP_CONCAT_MASK(TOK_NUMBER) |
3989 PP_CONCAT_MASK(TOK_FLOAT) |
3990 PP_CONCAT_MASK(TOK_OTHER);
3991 paste_tokens(&thead, mask_head, mask_tail, false);
3992 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07003993
H. Peter Anvin76690a12002-04-30 20:52:49 +00003994 return thead;
3995}
3996
3997/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003998 * Expand all single-line macro calls made in the given line.
3999 * Return the expanded version of the line. The original is deemed
4000 * to be destroyed in the process. (In reality we'll just move
4001 * Tokens from input to output a lot of the time, rather than
4002 * actually bothering to destroy and replicate.)
4003 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004004
H. Peter Anvine2c80182005-01-15 22:15:51 +00004005static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004006{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004007 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004008 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004009 Token **params;
4010 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004011 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004012 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004013 Token *org_tline = tline;
4014 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004015 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004016 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004017 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004018
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004019 /*
4020 * Trick: we should avoid changing the start token pointer since it can
4021 * be contained in "next" field of other token. Because of this
4022 * we allocate a copy of first token and work with it; at the end of
4023 * routine we copy it back
4024 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004025 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004026 tline = new_Token(org_tline->next, org_tline->type,
4027 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004028 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004029 nasm_free(org_tline->text);
4030 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004031 }
4032
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004033 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004034
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004035again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004036 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004037 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004038
H. Peter Anvine2c80182005-01-15 22:15:51 +00004039 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004040 if (!--deadman) {
4041 error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004042 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004043 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004044
H. Peter Anvine2c80182005-01-15 22:15:51 +00004045 if ((mname = tline->text)) {
4046 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004047 if (tline->type == TOK_ID) {
4048 head = (SMacro *)hash_findix(&smacros, mname);
4049 } else if (tline->type == TOK_PREPROC_ID) {
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004050 ctx = get_ctx(mname, &mname, true);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004051 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4052 } else
4053 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004054
H. Peter Anvine2c80182005-01-15 22:15:51 +00004055 /*
4056 * We've hit an identifier. As in is_mmacro below, we first
4057 * check whether the identifier is a single-line macro at
4058 * all, then think about checking for parameters if
4059 * necessary.
4060 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004061 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004062 if (!mstrcmp(m->name, mname, m->casesense))
4063 break;
4064 if (m) {
4065 mstart = tline;
4066 params = NULL;
4067 paramsize = NULL;
4068 if (m->nparam == 0) {
4069 /*
4070 * Simple case: the macro is parameterless. Discard the
4071 * one token that the macro call took, and push the
4072 * expansion back on the to-do stack.
4073 */
4074 if (!m->expansion) {
4075 if (!strcmp("__FILE__", m->name)) {
4076 int32_t num = 0;
4077 char *file = NULL;
4078 src_get(&num, &file);
4079 tline->text = nasm_quote(file, strlen(file));
4080 tline->type = TOK_STRING;
4081 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004082 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004083 }
4084 if (!strcmp("__LINE__", m->name)) {
4085 nasm_free(tline->text);
4086 make_tok_num(tline, src_get_linnum());
4087 continue;
4088 }
4089 if (!strcmp("__BITS__", m->name)) {
4090 nasm_free(tline->text);
4091 make_tok_num(tline, globalbits);
4092 continue;
4093 }
4094 tline = delete_Token(tline);
4095 continue;
4096 }
4097 } else {
4098 /*
4099 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004100 * exists and takes parameters. We must find the
4101 * parameters in the call, count them, find the SMacro
4102 * that corresponds to that form of the macro call, and
4103 * substitute for the parameters when we expand. What a
4104 * pain.
4105 */
4106 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004107 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004108 do {
4109 t = tline->next;
4110 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004111 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004112 t->text = NULL;
4113 t = tline->next = delete_Token(t);
4114 }
4115 tline = t;
4116 } while (tok_type_(tline, TOK_WHITESPACE));
4117 if (!tok_is_(tline, "(")) {
4118 /*
4119 * This macro wasn't called with parameters: ignore
4120 * the call. (Behaviour borrowed from gnu cpp.)
4121 */
4122 tline = mstart;
4123 m = NULL;
4124 } else {
4125 int paren = 0;
4126 int white = 0;
4127 brackets = 0;
4128 nparam = 0;
4129 sparam = PARAM_DELTA;
4130 params = nasm_malloc(sparam * sizeof(Token *));
4131 params[0] = tline->next;
4132 paramsize = nasm_malloc(sparam * sizeof(int));
4133 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004134 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004135 /*
4136 * For some unusual expansions
4137 * which concatenates function call
4138 */
4139 t = tline->next;
4140 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004141 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004142 t->text = NULL;
4143 t = tline->next = delete_Token(t);
4144 }
4145 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004146
H. Peter Anvine2c80182005-01-15 22:15:51 +00004147 if (!tline) {
4148 error(ERR_NONFATAL,
4149 "macro call expects terminating `)'");
4150 break;
4151 }
4152 if (tline->type == TOK_WHITESPACE
4153 && brackets <= 0) {
4154 if (paramsize[nparam])
4155 white++;
4156 else
4157 params[nparam] = tline->next;
4158 continue; /* parameter loop */
4159 }
4160 if (tline->type == TOK_OTHER
4161 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004162 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004163 if (ch == ',' && !paren && brackets <= 0) {
4164 if (++nparam >= sparam) {
4165 sparam += PARAM_DELTA;
4166 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004167 sparam * sizeof(Token *));
4168 paramsize = nasm_realloc(paramsize,
4169 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004170 }
4171 params[nparam] = tline->next;
4172 paramsize[nparam] = 0;
4173 white = 0;
4174 continue; /* parameter loop */
4175 }
4176 if (ch == '{' &&
4177 (brackets > 0 || (brackets == 0 &&
4178 !paramsize[nparam])))
4179 {
4180 if (!(brackets++)) {
4181 params[nparam] = tline->next;
4182 continue; /* parameter loop */
4183 }
4184 }
4185 if (ch == '}' && brackets > 0)
4186 if (--brackets == 0) {
4187 brackets = -1;
4188 continue; /* parameter loop */
4189 }
4190 if (ch == '(' && !brackets)
4191 paren++;
4192 if (ch == ')' && brackets <= 0)
4193 if (--paren < 0)
4194 break;
4195 }
4196 if (brackets < 0) {
4197 brackets = 0;
4198 error(ERR_NONFATAL, "braces do not "
4199 "enclose all of macro parameter");
4200 }
4201 paramsize[nparam] += white + 1;
4202 white = 0;
4203 } /* parameter loop */
4204 nparam++;
4205 while (m && (m->nparam != nparam ||
4206 mstrcmp(m->name, mname,
4207 m->casesense)))
4208 m = m->next;
4209 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004210 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004211 "macro `%s' exists, "
4212 "but not taking %d parameters",
4213 mstart->text, nparam);
4214 }
4215 }
4216 if (m && m->in_progress)
4217 m = NULL;
4218 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004219 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004220 * Design question: should we handle !tline, which
4221 * indicates missing ')' here, or expand those
4222 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004223 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004224 */
4225 nasm_free(params);
4226 nasm_free(paramsize);
4227 tline = mstart;
4228 } else {
4229 /*
4230 * Expand the macro: we are placed on the last token of the
4231 * call, so that we can easily split the call from the
4232 * following tokens. We also start by pushing an SMAC_END
4233 * token for the cycle removal.
4234 */
4235 t = tline;
4236 if (t) {
4237 tline = t->next;
4238 t->next = NULL;
4239 }
4240 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004241 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004242 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004243 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004244 list_for_each(t, m->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004245 if (t->type >= TOK_SMAC_PARAM) {
4246 Token *pcopy = tline, **ptail = &pcopy;
4247 Token *ttt, *pt;
4248 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004249
H. Peter Anvine2c80182005-01-15 22:15:51 +00004250 ttt = params[t->type - TOK_SMAC_PARAM];
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004251 i = paramsize[t->type - TOK_SMAC_PARAM];
4252 while (--i >= 0) {
4253 pt = *ptail = new_Token(tline, ttt->type,
4254 ttt->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004255 ptail = &pt->next;
4256 ttt = ttt->next;
4257 }
4258 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004259 } else if (t->type == TOK_PREPROC_Q) {
4260 tt = new_Token(tline, TOK_ID, mname, 0);
4261 tline = tt;
4262 } else if (t->type == TOK_PREPROC_QQ) {
4263 tt = new_Token(tline, TOK_ID, m->name, 0);
4264 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004265 } else {
4266 tt = new_Token(tline, t->type, t->text, 0);
4267 tline = tt;
4268 }
4269 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004270
H. Peter Anvine2c80182005-01-15 22:15:51 +00004271 /*
4272 * Having done that, get rid of the macro call, and clean
4273 * up the parameters.
4274 */
4275 nasm_free(params);
4276 nasm_free(paramsize);
4277 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004278 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004279 continue; /* main token loop */
4280 }
4281 }
4282 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004283
H. Peter Anvine2c80182005-01-15 22:15:51 +00004284 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004285 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004286 tline = delete_Token(tline);
4287 } else {
4288 t = *tail = tline;
4289 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004290 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004291 t->next = NULL;
4292 tail = &t->next;
4293 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004294 }
4295
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004296 /*
4297 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004298 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004299 * TOK_IDs should be concatenated.
4300 * Also we look for %+ tokens and concatenate the tokens before and after
4301 * them (without white spaces in between).
4302 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004303 if (expanded) {
4304 int mask_head = PP_CONCAT_MASK(TOK_ID) |
4305 PP_CONCAT_MASK(TOK_PREPROC_ID);
4306 int mask_tail = PP_CONCAT_MASK(TOK_ID) |
4307 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4308 PP_CONCAT_MASK(TOK_NUMBER);
4309 if (paste_tokens(&thead, mask_head, mask_tail, true)) {
4310 /*
4311 * If we concatenated something, *and* we had previously expanded
4312 * an actual macro, scan the lines again for macros...
4313 */
4314 tline = thead;
4315 expanded = false;
4316 goto again;
4317 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004318 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004319
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004320err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004321 if (org_tline) {
4322 if (thead) {
4323 *org_tline = *thead;
4324 /* since we just gave text to org_line, don't free it */
4325 thead->text = NULL;
4326 delete_Token(thead);
4327 } else {
4328 /* the expression expanded to empty line;
4329 we can't return NULL for some reasons
4330 we just set the line to a single WHITESPACE token. */
4331 memset(org_tline, 0, sizeof(*org_tline));
4332 org_tline->text = NULL;
4333 org_tline->type = TOK_WHITESPACE;
4334 }
4335 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004336 }
4337
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004338 return thead;
4339}
4340
4341/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004342 * Similar to expand_smacro but used exclusively with macro identifiers
4343 * right before they are fetched in. The reason is that there can be
4344 * identifiers consisting of several subparts. We consider that if there
4345 * are more than one element forming the name, user wants a expansion,
4346 * otherwise it will be left as-is. Example:
4347 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004348 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004349 *
4350 * the identifier %$abc will be left as-is so that the handler for %define
4351 * will suck it and define the corresponding value. Other case:
4352 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004353 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004354 *
4355 * In this case user wants name to be expanded *before* %define starts
4356 * working, so we'll expand %$abc into something (if it has a value;
4357 * otherwise it will be left as-is) then concatenate all successive
4358 * PP_IDs into one.
4359 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004360static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004361{
4362 Token *cur, *oldnext = NULL;
4363
H. Peter Anvin734b1882002-04-30 21:01:08 +00004364 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004365 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004366
4367 cur = tline;
4368 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00004369 (cur->next->type == TOK_ID ||
4370 cur->next->type == TOK_PREPROC_ID
4371 || cur->next->type == TOK_NUMBER))
4372 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004373
4374 /* If identifier consists of just one token, don't expand */
4375 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004376 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004377
H. Peter Anvine2c80182005-01-15 22:15:51 +00004378 if (cur) {
4379 oldnext = cur->next; /* Detach the tail past identifier */
4380 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004381 }
4382
H. Peter Anvin734b1882002-04-30 21:01:08 +00004383 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004384
H. Peter Anvine2c80182005-01-15 22:15:51 +00004385 if (cur) {
4386 /* expand_smacro possibly changhed tline; re-scan for EOL */
4387 cur = tline;
4388 while (cur && cur->next)
4389 cur = cur->next;
4390 if (cur)
4391 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004392 }
4393
4394 return tline;
4395}
4396
4397/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004398 * Determine whether the given line constitutes a multi-line macro
4399 * call, and return the MMacro structure called if so. Doesn't have
4400 * to check for an initial label - that's taken care of in
4401 * expand_mmacro - but must check numbers of parameters. Guaranteed
4402 * to be called with tline->type == TOK_ID, so the putative macro
4403 * name is easy to find.
4404 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004405static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004406{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004407 MMacro *head, *m;
4408 Token **params;
4409 int nparam;
4410
H. Peter Anvin166c2472008-05-28 12:28:58 -07004411 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004412
4413 /*
4414 * Efficiency: first we see if any macro exists with the given
4415 * name. If not, we can return NULL immediately. _Then_ we
4416 * count the parameters, and then we look further along the
4417 * list if necessary to find the proper MMacro.
4418 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004419 list_for_each(m, head)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004420 if (!mstrcmp(m->name, tline->text, m->casesense))
4421 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004422 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004423 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004424
4425 /*
4426 * OK, we have a potential macro. Count and demarcate the
4427 * parameters.
4428 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004429 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004430
4431 /*
4432 * So we know how many parameters we've got. Find the MMacro
4433 * structure that handles this number.
4434 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004435 while (m) {
4436 if (m->nparam_min <= nparam
4437 && (m->plus || nparam <= m->nparam_max)) {
4438 /*
4439 * This one is right. Just check if cycle removal
4440 * prohibits us using it before we actually celebrate...
4441 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004442 if (m->in_progress > m->max_depth) {
4443 if (m->max_depth > 0) {
4444 error(ERR_WARNING,
4445 "reached maximum recursion depth of %i",
4446 m->max_depth);
4447 }
4448 nasm_free(params);
4449 return NULL;
4450 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004451 /*
4452 * It's right, and we can use it. Add its default
4453 * parameters to the end of our list if necessary.
4454 */
4455 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
4456 params =
4457 nasm_realloc(params,
4458 ((m->nparam_min + m->ndefs +
4459 1) * sizeof(*params)));
4460 while (nparam < m->nparam_min + m->ndefs) {
4461 params[nparam] = m->defaults[nparam - m->nparam_min];
4462 nparam++;
4463 }
4464 }
4465 /*
4466 * If we've gone over the maximum parameter count (and
4467 * we're in Plus mode), ignore parameters beyond
4468 * nparam_max.
4469 */
4470 if (m->plus && nparam > m->nparam_max)
4471 nparam = m->nparam_max;
4472 /*
4473 * Then terminate the parameter list, and leave.
4474 */
4475 if (!params) { /* need this special case */
4476 params = nasm_malloc(sizeof(*params));
4477 nparam = 0;
4478 }
4479 params[nparam] = NULL;
4480 *params_array = params;
4481 return m;
4482 }
4483 /*
4484 * This one wasn't right: look for the next one with the
4485 * same name.
4486 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004487 list_for_each(m, m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004488 if (!mstrcmp(m->name, tline->text, m->casesense))
4489 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004490 }
4491
4492 /*
4493 * After all that, we didn't find one with the right number of
4494 * parameters. Issue a warning, and fail to expand the macro.
4495 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004496 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004497 "macro `%s' exists, but not taking %d parameters",
4498 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004499 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004500 return NULL;
4501}
4502
Keith Kanios891775e2009-07-11 06:08:54 -05004503
4504/*
4505 * Save MMacro invocation specific fields in
4506 * preparation for a recursive macro expansion
4507 */
4508static void push_mmacro(MMacro *m)
4509{
4510 MMacroInvocation *i;
4511
H. Peter Anvin89cee572009-07-15 09:16:54 -04004512 i = nasm_malloc(sizeof(MMacroInvocation));
4513 i->prev = m->prev;
4514 i->params = m->params;
4515 i->iline = m->iline;
4516 i->nparam = m->nparam;
4517 i->rotate = m->rotate;
4518 i->paramlen = m->paramlen;
4519 i->unique = m->unique;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004520 i->condcnt = m->condcnt;
H. Peter Anvin89cee572009-07-15 09:16:54 -04004521 m->prev = i;
Keith Kanios891775e2009-07-11 06:08:54 -05004522}
4523
4524
4525/*
4526 * Restore MMacro invocation specific fields that were
4527 * saved during a previous recursive macro expansion
4528 */
4529static void pop_mmacro(MMacro *m)
4530{
4531 MMacroInvocation *i;
4532
H. Peter Anvin89cee572009-07-15 09:16:54 -04004533 if (m->prev) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004534 i = m->prev;
4535 m->prev = i->prev;
4536 m->params = i->params;
4537 m->iline = i->iline;
4538 m->nparam = i->nparam;
4539 m->rotate = i->rotate;
4540 m->paramlen = i->paramlen;
4541 m->unique = i->unique;
4542 m->condcnt = i->condcnt;
4543 nasm_free(i);
H. Peter Anvin89cee572009-07-15 09:16:54 -04004544 }
Keith Kanios891775e2009-07-11 06:08:54 -05004545}
4546
4547
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004548/*
4549 * Expand the multi-line macro call made by the given line, if
4550 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00004551 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004552 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004553static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004554{
4555 Token *startline = tline;
4556 Token *label = NULL;
4557 int dont_prepend = 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004558 Token **params, *t, *mtok, *tt;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004559 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004560 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004561 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004562 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004563
4564 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004565 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004566 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004567 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004568 return 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004569 mtok = t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004570 m = is_mmacro(t, &params);
H. Peter Anvinc751e862008-06-09 10:18:45 -07004571 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004572 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004573 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004574 Token *last;
4575 /*
4576 * We have an id which isn't a macro call. We'll assume
4577 * it might be a label; we'll also check to see if a
4578 * colon follows it. Then, if there's another id after
4579 * that lot, we'll check it again for macro-hood.
4580 */
4581 label = last = t;
4582 t = t->next;
4583 if (tok_type_(t, TOK_WHITESPACE))
4584 last = t, t = t->next;
4585 if (tok_is_(t, ":")) {
4586 dont_prepend = 1;
4587 last = t, t = t->next;
4588 if (tok_type_(t, TOK_WHITESPACE))
4589 last = t, t = t->next;
4590 }
H. Peter Anvin89cee572009-07-15 09:16:54 -04004591 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004592 return 0;
4593 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004594 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004595 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004596 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004597
4598 /*
4599 * Fix up the parameters: this involves stripping leading and
4600 * trailing whitespace, then stripping braces if they are
4601 * present.
4602 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004603 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004604 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004605
H. Peter Anvine2c80182005-01-15 22:15:51 +00004606 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004607 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004608 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004609
H. Peter Anvine2c80182005-01-15 22:15:51 +00004610 t = params[i];
4611 skip_white_(t);
4612 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004613 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004614 params[i] = t;
4615 paramlen[i] = 0;
4616 while (t) {
4617 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4618 break; /* ... because we have hit a comma */
4619 if (comma && t->type == TOK_WHITESPACE
4620 && tok_is_(t->next, ","))
4621 break; /* ... or a space then a comma */
4622 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4623 break; /* ... or a brace */
4624 t = t->next;
4625 paramlen[i]++;
4626 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004627 }
4628
4629 /*
4630 * OK, we have a MMacro structure together with a set of
4631 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00004632 * copies of each Line on to istk->expansion. Substitution of
4633 * parameter tokens and macro-local tokens doesn't get done
4634 * until the single-line macro substitution process; this is
4635 * because delaying them allows us to change the semantics
4636 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004637 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00004638 * First, push an end marker on to istk->expansion, mark this
4639 * macro as in progress, and set up its invocation-specific
4640 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004641 */
4642 ll = nasm_malloc(sizeof(Line));
4643 ll->next = istk->expansion;
4644 ll->finishes = m;
4645 ll->first = NULL;
4646 istk->expansion = ll;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004647
H. Peter Anvin89cee572009-07-15 09:16:54 -04004648 /*
4649 * Save the previous MMacro expansion in the case of
4650 * macro recursion
4651 */
4652 if (m->max_depth && m->in_progress)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004653 push_mmacro(m);
H. Peter Anvin76690a12002-04-30 20:52:49 +00004654
Keith Kanios891775e2009-07-11 06:08:54 -05004655 m->in_progress ++;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004656 m->params = params;
4657 m->iline = tline;
4658 m->nparam = nparam;
4659 m->rotate = 0;
4660 m->paramlen = paramlen;
4661 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004662 m->lineno = 0;
Keith Kanios3c0d91f2009-10-25 13:28:03 -05004663 m->condcnt = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004664
4665 m->next_active = istk->mstk;
4666 istk->mstk = m;
4667
Cyrill Gorcunov367d59e2010-04-09 15:43:03 +04004668 list_for_each(l, m->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004669 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004670
H. Peter Anvine2c80182005-01-15 22:15:51 +00004671 ll = nasm_malloc(sizeof(Line));
4672 ll->finishes = NULL;
4673 ll->next = istk->expansion;
4674 istk->expansion = ll;
4675 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004676
Cyrill Gorcunov367d59e2010-04-09 15:43:03 +04004677 list_for_each(t, l->first) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004678 Token *x = t;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004679 switch (t->type) {
4680 case TOK_PREPROC_Q:
4681 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
4682 break;
4683 case TOK_PREPROC_QQ:
4684 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4685 break;
4686 case TOK_PREPROC_ID:
4687 if (t->text[1] == '0' && t->text[2] == '0') {
4688 dont_prepend = -1;
4689 x = label;
4690 if (!x)
4691 continue;
4692 }
4693 /* fall through */
4694 default:
4695 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4696 break;
4697 }
4698 tail = &tt->next;
4699 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004700 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004701 }
4702
4703 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004704 * If we had a label, push it on as the first line of
4705 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004706 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004707 if (label) {
4708 if (dont_prepend < 0)
4709 free_tlist(startline);
4710 else {
4711 ll = nasm_malloc(sizeof(Line));
4712 ll->finishes = NULL;
4713 ll->next = istk->expansion;
4714 istk->expansion = ll;
4715 ll->first = startline;
4716 if (!dont_prepend) {
4717 while (label->next)
4718 label = label->next;
4719 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4720 }
4721 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004722 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004723
H. Peter Anvin734b1882002-04-30 21:01:08 +00004724 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004725
H. Peter Anvineba20a72002-04-30 20:53:55 +00004726 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004727}
4728
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004729/* The function that actually does the error reporting */
4730static void verror(int severity, const char *fmt, va_list arg)
4731{
4732 char buff[1024];
Cyrill Gorcunov4a350082010-09-24 15:24:42 +04004733 MMacro *mmac = NULL;
4734 int delta = 0;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004735
4736 vsnprintf(buff, sizeof(buff), fmt, arg);
4737
Cyrill Gorcunov4a350082010-09-24 15:24:42 +04004738 /* get %macro name */
4739 if (istk && istk->mstk) {
4740 mmac = istk->mstk;
4741 /* but %rep blocks should be skipped */
4742 while (mmac && !mmac->name)
4743 mmac = mmac->next_active, delta++;
4744 }
4745
4746 if (mmac)
4747 nasm_error(severity, "(%s:%d) %s",
4748 mmac->name, mmac->lineno - delta, buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004749 else
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004750 nasm_error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004751}
4752
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004753/*
4754 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07004755 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004756 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004757static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004758{
4759 va_list arg;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004760
4761 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00004762 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004763 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004764
H. Peter Anvin734b1882002-04-30 21:01:08 +00004765 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004766 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004767 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004768}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004769
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004770/*
4771 * Because %else etc are evaluated in the state context
4772 * of the previous branch, errors might get lost with error():
4773 * %if 0 ... %else trailing garbage ... %endif
4774 * So %else etc should report errors with this function.
4775 */
4776static void error_precond(int severity, const char *fmt, ...)
4777{
4778 va_list arg;
4779
4780 /* Only ignore the error if it's really in a dead branch */
4781 if (istk && istk->conds && istk->conds->state == COND_NEVER)
4782 return;
4783
4784 va_start(arg, fmt);
4785 verror(severity, fmt, arg);
4786 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004787}
4788
H. Peter Anvin734b1882002-04-30 21:01:08 +00004789static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004790pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004791{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004792 Token *t;
4793
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004794 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004795 istk = nasm_malloc(sizeof(Include));
4796 istk->next = NULL;
4797 istk->conds = NULL;
4798 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004799 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004800 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00004801 istk->fname = NULL;
4802 src_set_fname(nasm_strdup(file));
4803 src_set_linnum(0);
4804 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004805 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004806 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00004807 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004808 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004809 nested_mac_count = 0;
4810 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004811 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004812 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004813 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004814 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004815 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004816 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004817 }
H. Peter Anvind2456592008-06-19 15:04:18 -07004818 any_extrastdmac = extrastdmac && *extrastdmac;
4819 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004820 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004821
4822 /*
4823 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4824 * The caller, however, will also pass in 3 for preprocess-only so
4825 * we can set __PASS__ accordingly.
4826 */
4827 pass = apass > 2 ? 2 : apass;
4828
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004829 dephead = deptail = deplist;
4830 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004831 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4832 sl->next = NULL;
4833 strcpy(sl->str, file);
4834 *deptail = sl;
4835 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004836 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07004837
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004838 /*
4839 * Define the __PASS__ macro. This is defined here unlike
4840 * all the other builtins, because it is special -- it varies between
4841 * passes.
4842 */
H. Peter Anvin7383b402008-09-24 10:20:40 -07004843 t = nasm_malloc(sizeof(*t));
4844 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004845 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07004846 t->a.mac = NULL;
4847 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004848}
4849
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004850static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004851{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004852 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004853 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004854
H. Peter Anvine2c80182005-01-15 22:15:51 +00004855 while (1) {
4856 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004857 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004858 * buffer or from the input file.
4859 */
4860 tline = NULL;
4861 while (istk->expansion && istk->expansion->finishes) {
4862 Line *l = istk->expansion;
4863 if (!l->finishes->name && l->finishes->in_progress > 1) {
4864 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004865
H. Peter Anvine2c80182005-01-15 22:15:51 +00004866 /*
4867 * This is a macro-end marker for a macro with no
4868 * name, which means it's not really a macro at all
4869 * but a %rep block, and the `in_progress' field is
4870 * more than 1, meaning that we still need to
4871 * repeat. (1 means the natural last repetition; 0
4872 * means termination by %exitrep.) We have
4873 * therefore expanded up to the %endrep, and must
4874 * push the whole block on to the expansion buffer
4875 * again. We don't bother to remove the macro-end
4876 * marker: we'd only have to generate another one
4877 * if we did.
4878 */
4879 l->finishes->in_progress--;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004880 list_for_each(l, l->finishes->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004881 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004882
H. Peter Anvine2c80182005-01-15 22:15:51 +00004883 ll = nasm_malloc(sizeof(Line));
4884 ll->next = istk->expansion;
4885 ll->finishes = NULL;
4886 ll->first = NULL;
4887 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004888
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004889 list_for_each(t, l->first) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004890 if (t->text || t->type == TOK_WHITESPACE) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004891 tt = *tail = new_Token(NULL, t->type, t->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004892 tail = &tt->next;
4893 }
4894 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004895
H. Peter Anvine2c80182005-01-15 22:15:51 +00004896 istk->expansion = ll;
4897 }
4898 } else {
4899 /*
4900 * Check whether a `%rep' was started and not ended
4901 * within this macro expansion. This can happen and
4902 * should be detected. It's a fatal error because
4903 * I'm too confused to work out how to recover
4904 * sensibly from it.
4905 */
4906 if (defining) {
4907 if (defining->name)
4908 error(ERR_PANIC,
4909 "defining with name in expansion");
4910 else if (istk->mstk->name)
4911 error(ERR_FATAL,
4912 "`%%rep' without `%%endrep' within"
4913 " expansion of macro `%s'",
4914 istk->mstk->name);
4915 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00004916
H. Peter Anvine2c80182005-01-15 22:15:51 +00004917 /*
4918 * FIXME: investigate the relationship at this point between
4919 * istk->mstk and l->finishes
4920 */
4921 {
4922 MMacro *m = istk->mstk;
4923 istk->mstk = m->next_active;
4924 if (m->name) {
4925 /*
4926 * This was a real macro call, not a %rep, and
4927 * therefore the parameter information needs to
4928 * be freed.
4929 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004930 if (m->prev) {
4931 pop_mmacro(m);
4932 l->finishes->in_progress --;
4933 } else {
Keith Kanios891775e2009-07-11 06:08:54 -05004934 nasm_free(m->params);
4935 free_tlist(m->iline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004936 nasm_free(m->paramlen);
4937 l->finishes->in_progress = 0;
4938 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004939 } else
4940 free_mmacro(m);
4941 }
4942 istk->expansion = l->next;
4943 nasm_free(l);
4944 list->downlevel(LIST_MACRO);
4945 }
4946 }
4947 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004948
H. Peter Anvine2c80182005-01-15 22:15:51 +00004949 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004950 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004951 Line *l = istk->expansion;
4952 if (istk->mstk)
4953 istk->mstk->lineno++;
4954 tline = l->first;
4955 istk->expansion = l->next;
4956 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004957 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004958 list->line(LIST_MACRO, p);
4959 nasm_free(p);
4960 break;
4961 }
4962 line = read_line();
4963 if (line) { /* from the current input file */
4964 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004965 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004966 nasm_free(line);
4967 break;
4968 }
4969 /*
4970 * The current file has ended; work down the istk
4971 */
4972 {
4973 Include *i = istk;
4974 fclose(i->fp);
Cyrill Gorcunov5ace91d2010-09-12 02:00:05 +04004975 if (i->conds) {
4976 /* nasm_error can't be conditionally suppressed */
4977 nasm_error(ERR_FATAL,
4978 "expected `%%endif' before end of file");
4979 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004980 /* only set line and file name if there's a next node */
4981 if (i->next) {
4982 src_set_linnum(i->lineno);
4983 nasm_free(src_set_fname(i->fname));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004984 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004985 istk = i->next;
4986 list->downlevel(LIST_INCLUDE);
4987 nasm_free(i);
4988 if (!istk)
4989 return NULL;
Victor van den Elzen4c9d6222008-10-01 13:08:50 +02004990 if (istk->expansion && istk->expansion->finishes)
4991 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004992 }
4993 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004994
H. Peter Anvine2c80182005-01-15 22:15:51 +00004995 /*
4996 * We must expand MMacro parameters and MMacro-local labels
4997 * _before_ we plunge into directive processing, to cope
4998 * with things like `%define something %1' such as STRUC
4999 * uses. Unless we're _defining_ a MMacro, in which case
5000 * those tokens should be left alone to go into the
5001 * definition; and unless we're in a non-emitting
5002 * condition, in which case we don't want to meddle with
5003 * anything.
5004 */
Charles Crayned4200be2008-07-12 16:42:33 -07005005 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin992fe752008-10-19 15:45:05 -07005006 && !(istk->mstk && !istk->mstk->in_progress)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005007 tline = expand_mmac_params(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005008 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00005009
H. Peter Anvine2c80182005-01-15 22:15:51 +00005010 /*
5011 * Check the line to see if it's a preprocessor directive.
5012 */
5013 if (do_directive(tline) == DIRECTIVE_FOUND) {
5014 continue;
5015 } else if (defining) {
5016 /*
5017 * We're defining a multi-line macro. We emit nothing
5018 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00005019 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005020 */
5021 Line *l = nasm_malloc(sizeof(Line));
5022 l->next = defining->expansion;
5023 l->first = tline;
H. Peter Anvin538002d2008-06-28 18:30:27 -07005024 l->finishes = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005025 defining->expansion = l;
5026 continue;
5027 } else if (istk->conds && !emitting(istk->conds->state)) {
5028 /*
5029 * We're in a non-emitting branch of a condition block.
5030 * Emit nothing at all, not even a blank line: when we
5031 * emerge from the condition we'll give a line-number
5032 * directive so we keep our place correctly.
5033 */
5034 free_tlist(tline);
5035 continue;
5036 } else if (istk->mstk && !istk->mstk->in_progress) {
5037 /*
5038 * We're in a %rep block which has been terminated, so
5039 * we're walking through to the %endrep without
5040 * emitting anything. Emit nothing at all, not even a
5041 * blank line: when we emerge from the %rep block we'll
5042 * give a line-number directive so we keep our place
5043 * correctly.
5044 */
5045 free_tlist(tline);
5046 continue;
5047 } else {
5048 tline = expand_smacro(tline);
5049 if (!expand_mmacro(tline)) {
5050 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005051 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005052 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07005053 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005054 free_tlist(tline);
5055 break;
5056 } else {
5057 continue; /* expand_mmacro calls free_tlist */
5058 }
5059 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005060 }
5061
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005062 return line;
5063}
5064
H. Peter Anvine2c80182005-01-15 22:15:51 +00005065static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005066{
H. Peter Anvine2c80182005-01-15 22:15:51 +00005067 if (defining) {
H. Peter Anvin89cee572009-07-15 09:16:54 -04005068 if (defining->name) {
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02005069 error(ERR_NONFATAL,
5070 "end of file while still defining macro `%s'",
5071 defining->name);
5072 } else {
5073 error(ERR_NONFATAL, "end of file while still in %%rep");
5074 }
5075
H. Peter Anvine2c80182005-01-15 22:15:51 +00005076 free_mmacro(defining);
Cyrill Gorcunova327c652010-02-14 17:19:38 +03005077 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005078 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005079 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005080 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005081 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00005082 while (istk) {
5083 Include *i = istk;
5084 istk = istk->next;
5085 fclose(i->fp);
5086 nasm_free(i->fname);
5087 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005088 }
5089 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005090 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07005091 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00005092 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005093 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005094 free_llist(predef);
5095 delete_Blocks();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005096 while ((i = ipath)) {
5097 ipath = i->next;
5098 if (i->path)
5099 nasm_free(i->path);
5100 nasm_free(i);
5101 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005102 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005103}
5104
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005105void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005106{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005107 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005108
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005109 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005110 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005111 i->next = NULL;
5112
H. Peter Anvin89cee572009-07-15 09:16:54 -04005113 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005114 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005115 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005116 j = j->next;
5117 j->next = i;
5118 } else {
5119 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005120 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005121}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005122
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005123void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005124{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005125 Token *inc, *space, *name;
5126 Line *l;
5127
H. Peter Anvin734b1882002-04-30 21:01:08 +00005128 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5129 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5130 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005131
5132 l = nasm_malloc(sizeof(Line));
5133 l->next = predef;
5134 l->first = inc;
H. Peter Anvin538002d2008-06-28 18:30:27 -07005135 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005136 predef = l;
5137}
5138
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005139void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005140{
5141 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005142 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005143 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005144
5145 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005146 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5147 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005148 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005149 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005150 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005151 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005152 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005153
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005154 l = nasm_malloc(sizeof(Line));
5155 l->next = predef;
5156 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07005157 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005158 predef = l;
5159}
5160
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005161void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005162{
5163 Token *def, *space;
5164 Line *l;
5165
H. Peter Anvin734b1882002-04-30 21:01:08 +00005166 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5167 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005168 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005169
5170 l = nasm_malloc(sizeof(Line));
5171 l->next = predef;
5172 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07005173 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005174 predef = l;
5175}
5176
Keith Kaniosb7a89542007-04-12 02:40:54 +00005177/*
5178 * Added by Keith Kanios:
5179 *
5180 * This function is used to assist with "runtime" preprocessor
5181 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
5182 *
5183 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5184 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5185 */
5186
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005187void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005188{
5189 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07005190
Keith Kaniosb7a89542007-04-12 02:40:54 +00005191 def = tokenize(definition);
H. Peter Anvin89cee572009-07-15 09:16:54 -04005192 if (do_directive(def) == NO_DIRECTIVE_FOUND)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005193 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07005194
Keith Kaniosb7a89542007-04-12 02:40:54 +00005195}
5196
H. Peter Anvina70547f2008-07-19 21:44:26 -07005197void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005198{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005199 extrastdmac = macros;
5200}
5201
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005202static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005203{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005204 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005205 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005206 tok->text = nasm_strdup(numbuf);
5207 tok->type = TOK_NUMBER;
5208}
5209
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005210Preproc nasmpp = {
5211 pp_reset,
5212 pp_getline,
5213 pp_cleanup
5214};