blob: 1f617e063d88c22056527aabe09919ce225426c5 [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
H. Peter Anvine2c80182005-01-15 22:15:51 +0000215struct Token {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000216 Token *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000217 char *text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -0700218 union {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300219 SMacro *mac; /* associated macro for TOK_SMAC_END */
220 size_t len; /* scratch length field */
221 } a; /* Auxiliary data */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000222 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000223};
224
225/*
226 * Multi-line macro definitions are stored as a linked list of
227 * these, which is essentially a container to allow several linked
228 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700229 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000230 * Note that in this module, linked lists are treated as stacks
231 * wherever possible. For this reason, Lines are _pushed_ on to the
232 * `expansion' field in MMacro structures, so that the linked list,
233 * if walked, would give the macro lines in reverse order; this
234 * means that we can walk the list when expanding a macro, and thus
235 * push the lines on to the `expansion' field in _istk_ in reverse
236 * order (so that when popped back off they are in the right
237 * order). It may seem cockeyed, and it relies on my design having
238 * an even number of steps in, but it works...
239 *
240 * Some of these structures, rather than being actual lines, are
241 * markers delimiting the end of the expansion of a given macro.
H. Peter Anvin76690a12002-04-30 20:52:49 +0000242 * This is for use in the cycle-tracking and %rep-handling code.
243 * Such structures have `finishes' non-NULL, and `first' NULL. All
244 * others have `finishes' NULL, but `first' may still be NULL if
245 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000246 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000247struct Line {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000248 Line *next;
249 MMacro *finishes;
250 Token *first;
251};
252
253/*
254 * To handle an arbitrary level of file inclusion, we maintain a
255 * stack (ie linked list) of these things.
256 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000257struct Include {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000258 Include *next;
259 FILE *fp;
260 Cond *conds;
261 Line *expansion;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000262 char *fname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000263 int lineno, lineinc;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300264 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000265};
266
267/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000268 * Include search path. This is simply a list of strings which get
269 * prepended, in turn, to the name of an include file, in an
270 * attempt to find the file if it's not in the current directory.
271 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000272struct IncPath {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000273 IncPath *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000274 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000275};
276
277/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000278 * Conditional assembly: we maintain a separate stack of these for
279 * each level of file inclusion. (The only reason we keep the
280 * stacks separate is to ensure that a stray `%endif' in a file
281 * included from within the true branch of a `%if' won't terminate
282 * it and cause confusion: instead, rightly, it'll cause an error.)
283 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000284struct Cond {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000285 Cond *next;
286 int state;
287};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000288enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000289 /*
290 * These states are for use just after %if or %elif: IF_TRUE
291 * means the condition has evaluated to truth so we are
292 * currently emitting, whereas IF_FALSE means we are not
293 * currently emitting but will start doing so if a %else comes
294 * up. In these states, all directives are admissible: %elif,
295 * %else and %endif. (And of course %if.)
296 */
297 COND_IF_TRUE, COND_IF_FALSE,
298 /*
299 * These states come up after a %else: ELSE_TRUE means we're
300 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
301 * any %elif or %else will cause an error.
302 */
303 COND_ELSE_TRUE, COND_ELSE_FALSE,
304 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200305 * These states mean that we're not emitting now, and also that
306 * nothing until %endif will be emitted at all. COND_DONE is
307 * used when we've had our moment of emission
308 * and have now started seeing %elifs. COND_NEVER is used when
309 * the condition construct in question is contained within a
310 * non-emitting branch of a larger condition construct,
311 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000312 */
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200313 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000314};
315#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
316
H. Peter Anvin70653092007-10-19 14:42:29 -0700317/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000318 * These defines are used as the possible return values for do_directive
319 */
320#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300321#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000322
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000323/*
Keith Kanios852f1ee2009-07-12 00:19:55 -0500324 * This define sets the upper limit for smacro and recursive mmacro
325 * expansions
326 */
327#define DEADMAN_LIMIT (1 << 20)
328
329/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000330 * Condition codes. Note that we use c_ prefix not C_ because C_ is
331 * used in nasm.h for the "real" condition codes. At _this_ level,
332 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
333 * ones, so we need a different enum...
334 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700335static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000336 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
337 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000338 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000339};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700340enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000341 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
342 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 -0700343 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
344 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000345};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700346static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000347 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
348 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 +0000349 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000350};
351
H. Peter Anvin76690a12002-04-30 20:52:49 +0000352/*
353 * Directive names.
354 */
H. Peter Anvin65747262002-05-07 00:10:05 +0000355/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
H. Peter Anvin9bf0aa72007-09-12 02:12:07 +0000356static int is_condition(enum preproc_token arg)
H. Peter Anvin65747262002-05-07 00:10:05 +0000357{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000358 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
H. Peter Anvin65747262002-05-07 00:10:05 +0000359}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000360
361/* For TASM compatibility we need to be able to recognise TASM compatible
362 * conditional compilation directives. Using the NASM pre-processor does
363 * not work, so we look for them specifically from the following list and
364 * then jam in the equivalent NASM directive into the input stream.
365 */
366
H. Peter Anvine2c80182005-01-15 22:15:51 +0000367enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000368 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
369 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
370};
371
H. Peter Anvin476d2862007-10-02 22:04:15 -0700372static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000373 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
374 "ifndef", "include", "local"
375};
376
377static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000378static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000379static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800380static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000381
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000382static Context *cstk;
383static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000384static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000385
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300386static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700387static StrList **dephead, **deptail; /* Dependency list */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000388
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300389static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000390
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000391static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700392static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000393
394static ListGen *list;
395
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000396/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000397 * The current set of multi-line macros we have defined.
398 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700399static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000400
401/*
402 * The current set of single-line macros we have defined.
403 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700404static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000405
406/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000407 * The multi-line macro we are currently defining, or the %rep
408 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000409 */
410static MMacro *defining;
411
Charles Crayned4200be2008-07-12 16:42:33 -0700412static uint64_t nested_mac_count;
413static uint64_t nested_rep_count;
414
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000415/*
416 * The number of macro parameters to allocate space for at a time.
417 */
418#define PARAM_DELTA 16
419
420/*
H. Peter Anvina4835d42008-05-20 14:21:29 -0700421 * The standard macro set: defined in macros.c in the array nasm_stdmac.
422 * This gives our position in the macro set, when we're processing it.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000423 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700424static macros_t *stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000425
426/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000427 * The extra standard macros that come from the object format, if
428 * any.
429 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700430static macros_t *extrastdmac = NULL;
H. Peter Anvincfb71762008-06-20 15:20:16 -0700431static bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000432
433/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000434 * Tokens are allocated in blocks to improve speed
435 */
436#define TOKEN_BLOCKSIZE 4096
437static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000438struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000439 Blocks *next;
440 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000441};
442
443static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000444
445/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000446 * Forward declarations.
447 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000448static Token *expand_mmac_params(Token * tline);
449static Token *expand_smacro(Token * tline);
450static Token *expand_id(Token * tline);
H. Peter Anvinf8ad5322009-02-21 17:55:08 -0800451static Context *get_ctx(const char *name, const char **namep,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300452 bool all_contexts);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700453static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000454static void error(int severity, const char *fmt, ...);
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200455static void error_precond(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000456static void *new_Block(size_t size);
457static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700458static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300459 const char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000460static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000461
462/*
463 * Macros for safe checking of token pointers, avoid *(NULL)
464 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300465#define tok_type_(x,t) ((x) && (x)->type == (t))
466#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
467#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
468#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000469
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300470/*
471 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000472 * front of them. We do it here because I could not find any other
473 * place to do it for the moment, and it is a hack (ideally it would
474 * be nice to be able to use the NASM pre-processor to do it).
475 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000476static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000477{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000478 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400479 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000480
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400481 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000482
483 /* Binary search for the directive name */
484 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +0000485 j = elements(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400486 q = nasm_skip_word(p);
487 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000488 if (len) {
489 oldchar = p[len];
490 p[len] = 0;
491 while (j - i > 1) {
492 k = (j + i) / 2;
493 m = nasm_stricmp(p, tasm_directives[k]);
494 if (m == 0) {
495 /* We have found a directive, so jam a % in front of it
496 * so that NASM will then recognise it as one if it's own.
497 */
498 p[len] = oldchar;
499 len = strlen(p);
500 oldline = line;
501 line = nasm_malloc(len + 2);
502 line[0] = '%';
503 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700504 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300505 * NASM does not recognise IFDIFI, so we convert
506 * it to %if 0. This is not used in NASM
507 * compatible code, but does need to parse for the
508 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000509 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700510 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000511 } else {
512 memcpy(line + 1, p, len + 1);
513 }
514 nasm_free(oldline);
515 return line;
516 } else if (m < 0) {
517 j = k;
518 } else
519 i = k;
520 }
521 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000522 }
523 return line;
524}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000525
H. Peter Anvin76690a12002-04-30 20:52:49 +0000526/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000527 * The pre-preprocessing stage... This function translates line
528 * number indications as they emerge from GNU cpp (`# lineno "file"
529 * flags') into NASM preprocessor line number indications (`%line
530 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000531 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000532static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000533{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000534 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000535 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000536
H. Peter Anvine2c80182005-01-15 22:15:51 +0000537 if (line[0] == '#' && line[1] == ' ') {
538 oldline = line;
539 fname = oldline + 2;
540 lineno = atoi(fname);
541 fname += strspn(fname, "0123456789 ");
542 if (*fname == '"')
543 fname++;
544 fnlen = strcspn(fname, "\"");
545 line = nasm_malloc(20 + fnlen);
546 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
547 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000548 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000549 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000550 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000551 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000552}
553
554/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000555 * Free a linked list of tokens.
556 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000557static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000558{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000559 while (list) {
560 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000561 }
562}
563
564/*
565 * Free a linked list of lines.
566 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000567static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000568{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000569 Line *l;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000570 while (list) {
571 l = list;
572 list = list->next;
573 free_tlist(l->first);
574 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000575 }
576}
577
578/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000579 * Free an MMacro
580 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000581static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000582{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000583 nasm_free(m->name);
584 free_tlist(m->dlist);
585 nasm_free(m->defaults);
586 free_llist(m->expansion);
587 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000588}
589
590/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700591 * Free all currently defined macros, and free the hash tables
592 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700593static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700594{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700595 SMacro *s;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700596 const char *key;
597 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700598
H. Peter Anvin072771e2008-05-22 13:17:51 -0700599 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300600 nasm_free((void *)key);
601 while (s) {
602 SMacro *ns = s->next;
603 nasm_free(s->name);
604 free_tlist(s->expansion);
605 nasm_free(s);
606 s = ns;
607 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700608 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700609 hash_free(smt);
610}
611
612static void free_mmacro_table(struct hash_table *mmt)
613{
614 MMacro *m;
615 const char *key;
616 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700617
618 it = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700619 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300620 nasm_free((void *)key);
621 while (m) {
622 MMacro *nm = m->next;
623 free_mmacro(m);
624 m = nm;
625 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700626 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700627 hash_free(mmt);
628}
629
630static void free_macros(void)
631{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700632 free_smacro_table(&smacros);
633 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700634}
635
636/*
637 * Initialize the hash tables
638 */
639static void init_macros(void)
640{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700641 hash_init(&smacros, HASH_LARGE);
642 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700643}
644
645/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000646 * Pop the context stack.
647 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000648static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000649{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000650 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000651
652 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700653 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000654 nasm_free(c->name);
655 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000656}
657
H. Peter Anvin072771e2008-05-22 13:17:51 -0700658/*
659 * Search for a key in the hash index; adding it if necessary
660 * (in which case we initialize the data pointer to NULL.)
661 */
662static void **
663hash_findi_add(struct hash_table *hash, const char *str)
664{
665 struct hash_insert hi;
666 void **r;
667 char *strx;
668
669 r = hash_findi(hash, str, &hi);
670 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300671 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700672
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300673 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700674 return hash_add(&hi, strx, NULL);
675}
676
677/*
678 * Like hash_findi, but returns the data element rather than a pointer
679 * to it. Used only when not adding a new element, hence no third
680 * argument.
681 */
682static void *
683hash_findix(struct hash_table *hash, const char *str)
684{
685 void **p;
686
687 p = hash_findi(hash, str, NULL);
688 return p ? *p : NULL;
689}
690
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000691#define BUF_DELTA 512
692/*
693 * Read a line from the top file in istk, handling multiple CR/LFs
694 * at the end of the line read, and handling spurious ^Zs. Will
695 * return lines from the standard macro set if this has not already
696 * been done.
697 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000698static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000699{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000700 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000701 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000702
H. Peter Anvine2c80182005-01-15 22:15:51 +0000703 if (stdmacpos) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300704 unsigned char c;
705 const unsigned char *p = stdmacpos;
706 char *ret, *q;
707 size_t len = 0;
708 while ((c = *p++)) {
709 if (c >= 0x80)
710 len += pp_directives_len[c-0x80]+1;
711 else
712 len++;
713 }
714 ret = nasm_malloc(len+1);
715 q = ret;
716 while ((c = *stdmacpos++)) {
717 if (c >= 0x80) {
718 memcpy(q, pp_directives[c-0x80], pp_directives_len[c-0x80]);
719 q += pp_directives_len[c-0x80];
720 *q++ = ' ';
721 } else {
722 *q++ = c;
723 }
724 }
725 stdmacpos = p;
726 *q = '\0';
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700727
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300728 if (!*stdmacpos) {
729 /* This was the last of the standard macro chain... */
730 stdmacpos = NULL;
731 if (any_extrastdmac) {
732 stdmacpos = extrastdmac;
733 any_extrastdmac = false;
734 } else if (do_predef) {
735 Line *pd, *l;
736 Token *head, **tail, *t;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000737
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300738 /*
739 * Nasty hack: here we push the contents of
740 * `predef' on to the top-level expansion stack,
741 * since this is the most convenient way to
742 * implement the pre-include and pre-define
743 * features.
744 */
745 for (pd = predef; pd; pd = pd->next) {
746 head = NULL;
747 tail = &head;
748 for (t = pd->first; t; t = t->next) {
749 *tail = new_Token(NULL, t->type, t->text, 0);
750 tail = &(*tail)->next;
751 }
752 l = nasm_malloc(sizeof(Line));
753 l->next = istk->expansion;
754 l->first = head;
755 l->finishes = NULL;
756 istk->expansion = l;
757 }
758 do_predef = false;
759 }
760 }
761 return ret;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000762 }
763
764 bufsize = BUF_DELTA;
765 buffer = nasm_malloc(BUF_DELTA);
766 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000767 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000768 while (1) {
769 q = fgets(p, bufsize - (p - buffer), istk->fp);
770 if (!q)
771 break;
772 p += strlen(p);
773 if (p > buffer && p[-1] == '\n') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300774 /*
775 * Convert backslash-CRLF line continuation sequences into
776 * nothing at all (for DOS and Windows)
777 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000778 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
779 p -= 3;
780 *p = 0;
781 continued_count++;
782 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300783 /*
784 * Also convert backslash-LF line continuation sequences into
785 * nothing at all (for Unix)
786 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000787 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
788 p -= 2;
789 *p = 0;
790 continued_count++;
791 } else {
792 break;
793 }
794 }
795 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000796 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000797 bufsize += BUF_DELTA;
798 buffer = nasm_realloc(buffer, bufsize);
799 p = buffer + offset; /* prevent stale-pointer problems */
800 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000801 }
802
H. Peter Anvine2c80182005-01-15 22:15:51 +0000803 if (!q && p == buffer) {
804 nasm_free(buffer);
805 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000806 }
807
H. Peter Anvine2c80182005-01-15 22:15:51 +0000808 src_set_linnum(src_get_linnum() + istk->lineinc +
809 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000810
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000811 /*
812 * Play safe: remove CRs as well as LFs, if any of either are
813 * present at the end of the line.
814 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000815 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000816 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000817
818 /*
819 * Handle spurious ^Z, which may be inserted into source files
820 * by some file transfer utilities.
821 */
822 buffer[strcspn(buffer, "\032")] = '\0';
823
H. Peter Anvin734b1882002-04-30 21:01:08 +0000824 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000825
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000826 return buffer;
827}
828
829/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000830 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000831 * don't need to parse the value out of e.g. numeric tokens: we
832 * simply split one string into many.
833 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000834static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000835{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700836 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000837 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000838 Token *list = NULL;
839 Token *t, **tail = &list;
840
H. Peter Anvine2c80182005-01-15 22:15:51 +0000841 while (*line) {
842 p = line;
843 if (*p == '%') {
844 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300845 if (*p == '+' && !nasm_isdigit(p[1])) {
846 p++;
847 type = TOK_PASTE;
848 } else if (nasm_isdigit(*p) ||
849 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000850 do {
851 p++;
852 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700853 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000854 type = TOK_PREPROC_ID;
855 } else if (*p == '{') {
856 p++;
857 while (*p && *p != '}') {
858 p[-1] = *p;
859 p++;
860 }
861 p[-1] = '\0';
862 if (*p)
863 p++;
864 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300865 } else if (*p == '[') {
866 int lvl = 1;
867 line += 2; /* Skip the leading %[ */
868 p++;
869 while (lvl && (c = *p++)) {
870 switch (c) {
871 case ']':
872 lvl--;
873 break;
874 case '%':
875 if (*p == '[')
876 lvl++;
877 break;
878 case '\'':
879 case '\"':
880 case '`':
881 p = nasm_skip_string(p)+1;
882 break;
883 default:
884 break;
885 }
886 }
887 p--;
888 if (*p)
889 *p++ = '\0';
890 if (lvl)
891 error(ERR_NONFATAL, "unterminated %[ construct");
892 type = TOK_INDIRECT;
893 } else if (*p == '?') {
894 type = TOK_PREPROC_Q; /* %? */
895 p++;
896 if (*p == '?') {
897 type = TOK_PREPROC_QQ; /* %?? */
898 p++;
899 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000900 } else if (isidchar(*p) ||
901 ((*p == '!' || *p == '%' || *p == '$') &&
902 isidchar(p[1]))) {
903 do {
904 p++;
905 }
906 while (isidchar(*p));
907 type = TOK_PREPROC_ID;
908 } else {
909 type = TOK_OTHER;
910 if (*p == '%')
911 p++;
912 }
913 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
914 type = TOK_ID;
915 p++;
916 while (*p && isidchar(*p))
917 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700918 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000919 /*
920 * A string token.
921 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000922 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300923 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000924
H. Peter Anvine2c80182005-01-15 22:15:51 +0000925 if (*p) {
926 p++;
927 } else {
H. Peter Anvin917a3492008-09-24 09:14:49 -0700928 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000929 /* Handling unterminated strings by UNV */
930 /* type = -1; */
931 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +0200932 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300933 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +0200934 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000935 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300936 bool is_hex = false;
937 bool is_float = false;
938 bool has_e = false;
939 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700940
H. Peter Anvine2c80182005-01-15 22:15:51 +0000941 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700942 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000943 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700944
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300945 if (*p == '$') {
946 p++;
947 is_hex = true;
948 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700949
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300950 for (;;) {
951 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700952
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300953 if (!is_hex && (c == 'e' || c == 'E')) {
954 has_e = true;
955 if (*p == '+' || *p == '-') {
956 /*
957 * e can only be followed by +/- if it is either a
958 * prefixed hex number or a floating-point number
959 */
960 p++;
961 is_float = true;
962 }
963 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
964 is_hex = true;
965 } else if (c == 'P' || c == 'p') {
966 is_float = true;
967 if (*p == '+' || *p == '-')
968 p++;
969 } else if (isnumchar(c) || c == '_')
970 ; /* just advance */
971 else if (c == '.') {
972 /*
973 * we need to deal with consequences of the legacy
974 * parser, like "1.nolist" being two tokens
975 * (TOK_NUMBER, TOK_ID) here; at least give it
976 * a shot for now. In the future, we probably need
977 * a flex-based scanner with proper pattern matching
978 * to do it as well as it can be done. Nothing in
979 * the world is going to help the person who wants
980 * 0x123.p16 interpreted as two tokens, though.
981 */
982 r = p;
983 while (*r == '_')
984 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700985
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300986 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
987 (!is_hex && (*r == 'e' || *r == 'E')) ||
988 (*r == 'p' || *r == 'P')) {
989 p = r;
990 is_float = true;
991 } else
992 break; /* Terminate the token */
993 } else
994 break;
995 }
996 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700997
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300998 if (p == line+1 && *line == '$') {
999 type = TOK_OTHER; /* TOKEN_HERE */
1000 } else {
1001 if (has_e && !is_hex) {
1002 /* 1e13 is floating-point, but 1e13h is not */
1003 is_float = true;
1004 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001005
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001006 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1007 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001008 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001009 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001010 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001011 /*
1012 * Whitespace just before end-of-line is discarded by
1013 * pretending it's a comment; whitespace just before a
1014 * comment gets lumped into the comment.
1015 */
1016 if (!*p || *p == ';') {
1017 type = TOK_COMMENT;
1018 while (*p)
1019 p++;
1020 }
1021 } else if (*p == ';') {
1022 type = TOK_COMMENT;
1023 while (*p)
1024 p++;
1025 } else {
1026 /*
1027 * Anything else is an operator of some kind. We check
1028 * for all the double-character operators (>>, <<, //,
1029 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001030 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001031 */
1032 type = TOK_OTHER;
1033 if ((p[0] == '>' && p[1] == '>') ||
1034 (p[0] == '<' && p[1] == '<') ||
1035 (p[0] == '/' && p[1] == '/') ||
1036 (p[0] == '<' && p[1] == '=') ||
1037 (p[0] == '>' && p[1] == '=') ||
1038 (p[0] == '=' && p[1] == '=') ||
1039 (p[0] == '!' && p[1] == '=') ||
1040 (p[0] == '<' && p[1] == '>') ||
1041 (p[0] == '&' && p[1] == '&') ||
1042 (p[0] == '|' && p[1] == '|') ||
1043 (p[0] == '^' && p[1] == '^')) {
1044 p++;
1045 }
1046 p++;
1047 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001048
H. Peter Anvine2c80182005-01-15 22:15:51 +00001049 /* Handling unterminated string by UNV */
1050 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001051 {
1052 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1053 t->text[p-line] = *line;
1054 tail = &t->next;
1055 }
1056 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001057 if (type != TOK_COMMENT) {
1058 *tail = t = new_Token(NULL, type, line, p - line);
1059 tail = &t->next;
1060 }
1061 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001062 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001063 return list;
1064}
1065
H. Peter Anvince616072002-04-30 21:02:23 +00001066/*
1067 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001068 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001069 * deleted only all at once by the delete_Blocks function.
1070 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001071static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001072{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001073 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001074
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001075 /* first, get to the end of the linked list */
1076 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001077 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001078 /* now allocate the requested chunk */
1079 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001080
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001081 /* now allocate a new block for the next request */
1082 b->next = nasm_malloc(sizeof(Blocks));
1083 /* and initialize the contents of the new block */
1084 b->next->next = NULL;
1085 b->next->chunk = NULL;
1086 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001087}
1088
1089/*
1090 * this function deletes all managed blocks of memory
1091 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001092static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001093{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001094 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001095
H. Peter Anvin70653092007-10-19 14:42:29 -07001096 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001097 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001098 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001099 * free it.
1100 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001101 while (b) {
1102 if (b->chunk)
1103 nasm_free(b->chunk);
1104 a = b;
1105 b = b->next;
1106 if (a != &blocks)
1107 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001108 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001109}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001110
1111/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001112 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001113 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001114 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001115 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001116static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001117 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001118{
1119 Token *t;
1120 int i;
1121
H. Peter Anvin89cee572009-07-15 09:16:54 -04001122 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001123 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1124 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1125 freeTokens[i].next = &freeTokens[i + 1];
1126 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001127 }
1128 t = freeTokens;
1129 freeTokens = t->next;
1130 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001131 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001132 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001133 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001134 t->text = NULL;
1135 } else {
1136 if (txtlen == 0)
1137 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001138 t->text = nasm_malloc(txtlen+1);
1139 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001140 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001141 }
1142 return t;
1143}
1144
H. Peter Anvine2c80182005-01-15 22:15:51 +00001145static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001146{
1147 Token *next = t->next;
1148 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001149 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001150 freeTokens = t;
1151 return next;
1152}
1153
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001154/*
1155 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001156 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1157 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001158 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001159static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001160{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001161 Token *t;
1162 int len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001163 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001164 const char *q;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001165
1166 len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001167 for (t = tlist; t; t = t->next) {
1168 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001169 char *p = getenv(t->text + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001170 nasm_free(t->text);
1171 if (p)
1172 t->text = nasm_strdup(p);
1173 else
1174 t->text = NULL;
1175 }
1176 /* Expand local macros here and not during preprocessing */
1177 if (expand_locals &&
1178 t->type == TOK_PREPROC_ID && t->text &&
1179 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001180 const char *q;
1181 char *p;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001182 Context *ctx = get_ctx(t->text, &q, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001183 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001184 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001185 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001186 p = nasm_strcat(buffer, q);
1187 nasm_free(t->text);
1188 t->text = p;
1189 }
1190 }
1191 if (t->type == TOK_WHITESPACE) {
1192 len++;
1193 } else if (t->text) {
1194 len += strlen(t->text);
1195 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001196 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001197 p = line = nasm_malloc(len + 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001198 for (t = tlist; t; t = t->next) {
1199 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001200 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001201 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001202 q = t->text;
1203 while (*q)
1204 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001205 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001206 }
1207 *p = '\0';
1208 return line;
1209}
1210
1211/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001212 * A scanner, suitable for use by the expression evaluator, which
1213 * operates on a line of Tokens. Expects a pointer to a pointer to
1214 * the first token in the line to be passed in as its private_data
1215 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001216 *
1217 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001218 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001219static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001220{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001221 Token **tlineptr = private_data;
1222 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001223 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001224
H. Peter Anvine2c80182005-01-15 22:15:51 +00001225 do {
1226 tline = *tlineptr;
1227 *tlineptr = tline ? tline->next : NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001228 }
1229 while (tline && (tline->type == TOK_WHITESPACE ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001230 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001231
1232 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001233 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001234
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001235 tokval->t_charptr = tline->text;
1236
H. Peter Anvin76690a12002-04-30 20:52:49 +00001237 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001238 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001239 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001240 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001241
H. Peter Anvine2c80182005-01-15 22:15:51 +00001242 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001243 p = tokval->t_charptr = tline->text;
1244 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001245 tokval->t_charptr++;
1246 return tokval->t_type = TOKEN_ID;
1247 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001248
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001249 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001250 if (r >= p+MAX_KEYWORD)
1251 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001252 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001253 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001254 *s = '\0';
1255 /* right, so we have an identifier sitting in temp storage. now,
1256 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001257 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001258 }
1259
H. Peter Anvine2c80182005-01-15 22:15:51 +00001260 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001261 bool rn_error;
1262 tokval->t_integer = readnum(tline->text, &rn_error);
1263 tokval->t_charptr = tline->text;
1264 if (rn_error)
1265 return tokval->t_type = TOKEN_ERRNUM;
1266 else
1267 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001268 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001269
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001270 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001271 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001272 }
1273
H. Peter Anvine2c80182005-01-15 22:15:51 +00001274 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001275 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001276
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001277 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001278 tokval->t_charptr = tline->text;
1279 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001280
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001281 if (ep[0] != bq || ep[1] != '\0')
1282 return tokval->t_type = TOKEN_ERRSTR;
1283 else
1284 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001285 }
1286
H. Peter Anvine2c80182005-01-15 22:15:51 +00001287 if (tline->type == TOK_OTHER) {
1288 if (!strcmp(tline->text, "<<"))
1289 return tokval->t_type = TOKEN_SHL;
1290 if (!strcmp(tline->text, ">>"))
1291 return tokval->t_type = TOKEN_SHR;
1292 if (!strcmp(tline->text, "//"))
1293 return tokval->t_type = TOKEN_SDIV;
1294 if (!strcmp(tline->text, "%%"))
1295 return tokval->t_type = TOKEN_SMOD;
1296 if (!strcmp(tline->text, "=="))
1297 return tokval->t_type = TOKEN_EQ;
1298 if (!strcmp(tline->text, "<>"))
1299 return tokval->t_type = TOKEN_NE;
1300 if (!strcmp(tline->text, "!="))
1301 return tokval->t_type = TOKEN_NE;
1302 if (!strcmp(tline->text, "<="))
1303 return tokval->t_type = TOKEN_LE;
1304 if (!strcmp(tline->text, ">="))
1305 return tokval->t_type = TOKEN_GE;
1306 if (!strcmp(tline->text, "&&"))
1307 return tokval->t_type = TOKEN_DBL_AND;
1308 if (!strcmp(tline->text, "^^"))
1309 return tokval->t_type = TOKEN_DBL_XOR;
1310 if (!strcmp(tline->text, "||"))
1311 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001312 }
1313
1314 /*
1315 * We have no other options: just return the first character of
1316 * the token text.
1317 */
1318 return tokval->t_type = tline->text[0];
1319}
1320
1321/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001322 * Compare a string to the name of an existing macro; this is a
1323 * simple wrapper which calls either strcmp or nasm_stricmp
1324 * depending on the value of the `casesense' parameter.
1325 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001326static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001327{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001328 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001329}
1330
1331/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001332 * Compare a string to the name of an existing macro; this is a
1333 * simple wrapper which calls either strcmp or nasm_stricmp
1334 * depending on the value of the `casesense' parameter.
1335 */
1336static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1337{
1338 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1339}
1340
1341/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001342 * Return the Context structure associated with a %$ token. Return
1343 * NULL, having _already_ reported an error condition, if the
1344 * context stack isn't deep enough for the supplied number of $
1345 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001346 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001347 * also scanned for such smacro, until it is found; if not -
1348 * only the context that directly results from the number of $'s
1349 * in variable's name.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001350 *
1351 * If "namep" is non-NULL, set it to the pointer to the macro name
1352 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001353 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001354static Context *get_ctx(const char *name, const char **namep,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001355 bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001356{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001357 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001358 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001359 int i;
1360
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001361 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001362 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001363
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001364 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001365 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001366
H. Peter Anvine2c80182005-01-15 22:15:51 +00001367 if (!cstk) {
1368 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1369 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001370 }
1371
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001372 name += 2;
1373 ctx = cstk;
1374 i = 0;
1375 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001376 name++;
1377 i++;
1378 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001379 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001380 if (!ctx) {
1381 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001382 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001383 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001384 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001385
1386 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001387 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001388
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001389 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001390 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001391
H. Peter Anvine2c80182005-01-15 22:15:51 +00001392 do {
1393 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001394 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001395 while (m) {
1396 if (!mstrcmp(m->name, name, m->casesense))
1397 return ctx;
1398 m = m->next;
1399 }
1400 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001401 }
1402 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001403 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001404}
1405
1406/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001407 * Check to see if a file is already in a string list
1408 */
1409static bool in_list(const StrList *list, const char *str)
1410{
1411 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001412 if (!strcmp(list->str, str))
1413 return true;
1414 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001415 }
1416 return false;
1417}
1418
1419/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001420 * Open an include file. This routine must always return a valid
1421 * file pointer if it returns - it's responsible for throwing an
1422 * ERR_FATAL and bombing out completely if not. It should also try
1423 * the include path one by one until it finds the file or reaches
1424 * the end of the path.
1425 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001426static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001427 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001428{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001429 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001430 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001431 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001432 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001433 size_t prefix_len = 0;
1434 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001435
H. Peter Anvine2c80182005-01-15 22:15:51 +00001436 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001437 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001438 memcpy(sl->str, prefix, prefix_len);
1439 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001440 fp = fopen(sl->str, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001441 if (fp && dhead && !in_list(*dhead, sl->str)) {
1442 sl->next = NULL;
1443 **dtail = sl;
1444 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001445 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001446 nasm_free(sl);
1447 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001448 if (fp)
1449 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001450 if (!ip) {
1451 if (!missing_ok)
1452 break;
1453 prefix = NULL;
1454 } else {
1455 prefix = ip->path;
1456 ip = ip->next;
1457 }
1458 if (prefix) {
1459 prefix_len = strlen(prefix);
1460 } else {
1461 /* -MG given and file not found */
1462 if (dhead && !in_list(*dhead, file)) {
1463 sl = nasm_malloc(len+1+sizeof sl->next);
1464 sl->next = NULL;
1465 strcpy(sl->str, file);
1466 **dtail = sl;
1467 *dtail = &sl->next;
1468 }
1469 return NULL;
1470 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001471 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001472
H. Peter Anvin734b1882002-04-30 21:01:08 +00001473 error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001474 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001475}
1476
1477/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001478 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001479 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001480 * return true if _any_ single-line macro of that name is defined.
1481 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001482 * `nparam' or no parameters is defined.
1483 *
1484 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001485 * defined, or nparam is -1, the address of the definition structure
1486 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001487 * is NULL, no action will be taken regarding its contents, and no
1488 * error will occur.
1489 *
1490 * Note that this is also called with nparam zero to resolve
1491 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001492 *
1493 * If you already know which context macro belongs to, you can pass
1494 * the context pointer as first parameter; if you won't but name begins
1495 * with %$ the context will be automatically computed. If all_contexts
1496 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001497 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001498static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001499smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001500 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001501{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001502 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001503 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001504
H. Peter Anvin97a23472007-09-16 17:57:25 -07001505 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001506 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001507 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001508 if (cstk)
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001509 ctx = get_ctx(name, &name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001510 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001511 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001512 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001513 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001514 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001515 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001516 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001517
H. Peter Anvine2c80182005-01-15 22:15:51 +00001518 while (m) {
1519 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001520 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001521 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001522 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001523 *defn = m;
1524 else
1525 *defn = NULL;
1526 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001527 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001528 }
1529 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001530 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001531
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001532 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001533}
1534
1535/*
1536 * Count and mark off the parameters in a multi-line macro call.
1537 * This is called both from within the multi-line macro expansion
1538 * code, and also to mark off the default parameters when provided
1539 * in a %macro definition line.
1540 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001541static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001542{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001543 int paramsize, brace;
1544
1545 *nparam = paramsize = 0;
1546 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001547 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001548 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001549 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001550 paramsize += PARAM_DELTA;
1551 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1552 }
1553 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001554 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001555 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001556 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001557 (*params)[(*nparam)++] = t;
1558 while (tok_isnt_(t, brace ? "}" : ","))
1559 t = t->next;
1560 if (t) { /* got a comma/brace */
1561 t = t->next;
1562 if (brace) {
1563 /*
1564 * Now we've found the closing brace, look further
1565 * for the comma.
1566 */
1567 skip_white_(t);
1568 if (tok_isnt_(t, ",")) {
1569 error(ERR_NONFATAL,
1570 "braces do not enclose all of macro parameter");
1571 while (tok_isnt_(t, ","))
1572 t = t->next;
1573 }
1574 if (t)
1575 t = t->next; /* eat the comma */
1576 }
1577 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001578 }
1579}
1580
1581/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001582 * Determine whether one of the various `if' conditions is true or
1583 * not.
1584 *
1585 * We must free the tline we get passed.
1586 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001587static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001588{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001589 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001590 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001591 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001592 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001593 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001594 enum pp_token_type needtype;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001595
1596 origline = tline;
1597
H. Peter Anvine2c80182005-01-15 22:15:51 +00001598 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001599 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001600 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001601 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001602 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001603 if (!tline)
1604 break;
1605 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001606 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001607 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001608 free_tlist(origline);
1609 return -1;
1610 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001611 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001612 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001613 tline = tline->next;
1614 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001615 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001616
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001617 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001618 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001619 while (tline) {
1620 skip_white_(tline);
1621 if (!tline || (tline->type != TOK_ID &&
1622 (tline->type != TOK_PREPROC_ID ||
1623 tline->text[1] != '$'))) {
1624 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001625 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001626 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001627 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001628 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001629 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001630 tline = tline->next;
1631 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001632 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001633
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001634 case PPC_IFIDN:
1635 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001636 tline = expand_smacro(tline);
1637 t = tt = tline;
1638 while (tok_isnt_(tt, ","))
1639 tt = tt->next;
1640 if (!tt) {
1641 error(ERR_NONFATAL,
1642 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001643 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001644 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001645 }
1646 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001647 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001648 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1649 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1650 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001651 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001652 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001653 }
1654 if (t->type == TOK_WHITESPACE) {
1655 t = t->next;
1656 continue;
1657 }
1658 if (tt->type == TOK_WHITESPACE) {
1659 tt = tt->next;
1660 continue;
1661 }
1662 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001663 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001664 break;
1665 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001666 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001667 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001668 size_t l1 = nasm_unquote(t->text, NULL);
1669 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001670
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001671 if (l1 != l2) {
1672 j = false;
1673 break;
1674 }
1675 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1676 j = false;
1677 break;
1678 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001679 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001680 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001681 break;
1682 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001683
H. Peter Anvine2c80182005-01-15 22:15:51 +00001684 t = t->next;
1685 tt = tt->next;
1686 }
1687 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001688 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001689 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001690
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001691 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001692 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001693 bool found = false;
1694 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001695
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001696 skip_white_(tline);
1697 tline = expand_id(tline);
1698 if (!tok_type_(tline, TOK_ID)) {
1699 error(ERR_NONFATAL,
1700 "`%s' expects a macro name", pp_directives[ct]);
1701 goto fail;
1702 }
1703 searching.name = nasm_strdup(tline->text);
1704 searching.casesense = true;
1705 searching.plus = false;
1706 searching.nolist = false;
1707 searching.in_progress = 0;
1708 searching.max_depth = 0;
1709 searching.rep_nest = NULL;
1710 searching.nparam_min = 0;
1711 searching.nparam_max = INT_MAX;
1712 tline = expand_smacro(tline->next);
1713 skip_white_(tline);
1714 if (!tline) {
1715 } else if (!tok_type_(tline, TOK_NUMBER)) {
1716 error(ERR_NONFATAL,
1717 "`%s' expects a parameter count or nothing",
1718 pp_directives[ct]);
1719 } else {
1720 searching.nparam_min = searching.nparam_max =
1721 readnum(tline->text, &j);
1722 if (j)
1723 error(ERR_NONFATAL,
1724 "unable to parse parameter count `%s'",
1725 tline->text);
1726 }
1727 if (tline && tok_is_(tline->next, "-")) {
1728 tline = tline->next->next;
1729 if (tok_is_(tline, "*"))
1730 searching.nparam_max = INT_MAX;
1731 else if (!tok_type_(tline, TOK_NUMBER))
1732 error(ERR_NONFATAL,
1733 "`%s' expects a parameter count after `-'",
1734 pp_directives[ct]);
1735 else {
1736 searching.nparam_max = readnum(tline->text, &j);
1737 if (j)
1738 error(ERR_NONFATAL,
1739 "unable to parse parameter count `%s'",
1740 tline->text);
1741 if (searching.nparam_min > searching.nparam_max)
1742 error(ERR_NONFATAL,
1743 "minimum parameter count exceeds maximum");
1744 }
1745 }
1746 if (tline && tok_is_(tline->next, "+")) {
1747 tline = tline->next;
1748 searching.plus = true;
1749 }
1750 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1751 while (mmac) {
1752 if (!strcmp(mmac->name, searching.name) &&
1753 (mmac->nparam_min <= searching.nparam_max
1754 || searching.plus)
1755 && (searching.nparam_min <= mmac->nparam_max
1756 || mmac->plus)) {
1757 found = true;
1758 break;
1759 }
1760 mmac = mmac->next;
1761 }
1762 if (tline && tline->next)
1763 error(ERR_WARNING|ERR_PASS1,
1764 "trailing garbage after %%ifmacro ignored");
1765 nasm_free(searching.name);
1766 j = found;
1767 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001768 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001769
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001770 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001771 needtype = TOK_ID;
1772 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001773 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001774 needtype = TOK_NUMBER;
1775 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001776 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001777 needtype = TOK_STRING;
1778 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001779
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001780iftype:
1781 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001782
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001783 while (tok_type_(t, TOK_WHITESPACE) ||
1784 (needtype == TOK_NUMBER &&
1785 tok_type_(t, TOK_OTHER) &&
1786 (t->text[0] == '-' || t->text[0] == '+') &&
1787 !t->text[1]))
1788 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001789
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001790 j = tok_type_(t, needtype);
1791 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001792
1793 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001794 t = tline = expand_smacro(tline);
1795 while (tok_type_(t, TOK_WHITESPACE))
1796 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001797
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001798 j = false;
1799 if (t) {
1800 t = t->next; /* Skip the actual token */
1801 while (tok_type_(t, TOK_WHITESPACE))
1802 t = t->next;
1803 j = !t; /* Should be nothing left */
1804 }
1805 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001806
H. Peter Anvin134b9462008-02-16 17:01:40 -08001807 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001808 t = tline = expand_smacro(tline);
1809 while (tok_type_(t, TOK_WHITESPACE))
1810 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001811
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001812 j = !t; /* Should be empty */
1813 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001814
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001815 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001816 t = tline = expand_smacro(tline);
1817 tptr = &t;
1818 tokval.t_type = TOKEN_INVALID;
1819 evalresult = evaluate(ppscan, tptr, &tokval,
1820 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001821 if (!evalresult)
1822 return -1;
1823 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001824 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001825 "trailing garbage after expression ignored");
1826 if (!is_simple(evalresult)) {
1827 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001828 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001829 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001830 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001831 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001832 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001833
H. Peter Anvine2c80182005-01-15 22:15:51 +00001834 default:
1835 error(ERR_FATAL,
1836 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001837 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001838 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001839 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001840
1841 free_tlist(origline);
1842 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001843
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001844fail:
1845 free_tlist(origline);
1846 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001847}
1848
1849/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001850 * Common code for defining an smacro
1851 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001852static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001853 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001854{
1855 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001856 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07001857
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001858 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001859 if (!smac) {
1860 error(ERR_WARNING|ERR_PASS1,
1861 "single-line macro `%s' defined both with and"
1862 " without parameters", mname);
1863 /*
1864 * Some instances of the old code considered this a failure,
1865 * some others didn't. What is the right thing to do here?
1866 */
1867 free_tlist(expansion);
1868 return false; /* Failure */
1869 } else {
1870 /*
1871 * We're redefining, so we have to take over an
1872 * existing SMacro structure. This means freeing
1873 * what was already in it.
1874 */
1875 nasm_free(smac->name);
1876 free_tlist(smac->expansion);
1877 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001878 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001879 smtbl = ctx ? &ctx->localmac : &smacros;
1880 smhead = (SMacro **) hash_findi_add(smtbl, mname);
1881 smac = nasm_malloc(sizeof(SMacro));
1882 smac->next = *smhead;
1883 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001884 }
1885 smac->name = nasm_strdup(mname);
1886 smac->casesense = casesense;
1887 smac->nparam = nparam;
1888 smac->expansion = expansion;
1889 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001890 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001891}
1892
1893/*
1894 * Undefine an smacro
1895 */
1896static void undef_smacro(Context *ctx, const char *mname)
1897{
1898 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001899 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001900
H. Peter Anvin166c2472008-05-28 12:28:58 -07001901 smtbl = ctx ? &ctx->localmac : &smacros;
1902 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07001903
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001904 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001905 /*
1906 * We now have a macro name... go hunt for it.
1907 */
1908 sp = smhead;
1909 while ((s = *sp) != NULL) {
1910 if (!mstrcmp(s->name, mname, s->casesense)) {
1911 *sp = s->next;
1912 nasm_free(s->name);
1913 free_tlist(s->expansion);
1914 nasm_free(s);
1915 } else {
1916 sp = &s->next;
1917 }
1918 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001919 }
1920}
1921
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001922/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07001923 * Parse a mmacro specification.
1924 */
1925static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
1926{
1927 bool err;
1928
1929 tline = tline->next;
1930 skip_white_(tline);
1931 tline = expand_id(tline);
1932 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001933 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
1934 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07001935 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001936
H. Peter Anvin89cee572009-07-15 09:16:54 -04001937 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07001938 def->name = nasm_strdup(tline->text);
1939 def->plus = false;
1940 def->nolist = false;
1941 def->in_progress = 0;
1942 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001943 def->nparam_min = 0;
1944 def->nparam_max = 0;
1945
H. Peter Anvina26433d2008-07-16 14:40:01 -07001946 tline = expand_smacro(tline->next);
1947 skip_white_(tline);
1948 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001949 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07001950 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001951 def->nparam_min = def->nparam_max =
1952 readnum(tline->text, &err);
1953 if (err)
1954 error(ERR_NONFATAL,
1955 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07001956 }
1957 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001958 tline = tline->next->next;
1959 if (tok_is_(tline, "*")) {
1960 def->nparam_max = INT_MAX;
1961 } else if (!tok_type_(tline, TOK_NUMBER)) {
1962 error(ERR_NONFATAL,
1963 "`%s' expects a parameter count after `-'", directive);
1964 } else {
1965 def->nparam_max = readnum(tline->text, &err);
1966 if (err) {
1967 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
1968 tline->text);
1969 }
1970 if (def->nparam_min > def->nparam_max) {
1971 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
1972 }
1973 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07001974 }
1975 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001976 tline = tline->next;
1977 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07001978 }
1979 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001980 !nasm_stricmp(tline->next->text, ".nolist")) {
1981 tline = tline->next;
1982 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07001983 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001984
H. Peter Anvina26433d2008-07-16 14:40:01 -07001985 /*
1986 * Handle default parameters.
1987 */
1988 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001989 def->dlist = tline->next;
1990 tline->next = NULL;
1991 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07001992 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001993 def->dlist = NULL;
1994 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07001995 }
1996 def->expansion = NULL;
1997
H. Peter Anvin89cee572009-07-15 09:16:54 -04001998 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001999 !def->plus)
2000 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2001 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002002
H. Peter Anvina26433d2008-07-16 14:40:01 -07002003 return true;
2004}
2005
2006
2007/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002008 * Decode a size directive
2009 */
2010static int parse_size(const char *str) {
2011 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002012 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002013 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002014 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002015
2016 return sizes[bsii(str, size_names, elements(size_names))+1];
2017}
2018
H. Peter Anvinf9c9a672009-07-14 15:14:05 -04002019/*
2020 * nasm_unquote with error if the string contains NUL characters.
2021 * If the string contains NUL characters, issue an error and return
2022 * the C len, i.e. truncate at the NUL.
2023 */
2024static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
2025{
2026 size_t len = nasm_unquote(qstr, NULL);
2027 size_t clen = strlen(qstr);
2028
2029 if (len != clen)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002030 error(ERR_NONFATAL, "NUL character in `%s' directive",
2031 pp_directives[directive]);
H. Peter Anvinf9c9a672009-07-14 15:14:05 -04002032
2033 return clen;
2034}
2035
Ed Beroset3ab3f412002-06-11 03:31:49 +00002036/**
2037 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002038 * Find out if a line contains a preprocessor directive, and deal
2039 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002040 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002041 * If a directive _is_ found, it is the responsibility of this routine
2042 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002043 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002044 * @param tline a pointer to the current tokeninzed line linked list
2045 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002046 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002047 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002048static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002049{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002050 enum preproc_token i;
2051 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002052 bool err;
2053 int nparam;
2054 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002055 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002056 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002057 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002058 char *p, *pp;
2059 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002060 Include *inc;
2061 Context *ctx;
2062 Cond *cond;
H. Peter Anvin97a23472007-09-16 17:57:25 -07002063 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002064 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
2065 Line *l;
2066 struct tokenval tokval;
2067 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002068 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002069 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002070 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002071 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002072
2073 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002074
H. Peter Anvineba20a72002-04-30 20:53:55 +00002075 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002076 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002077 (tline->text[1] == '%' || tline->text[1] == '$'
2078 || tline->text[1] == '!'))
2079 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002080
H. Peter Anvin4169a472007-09-12 01:29:43 +00002081 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002082
2083 /*
Cyrill Gorcunovf09116f2010-02-28 11:52:53 +03002084 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2085 * since they are known to be buggy at moment, we need to fix them
2086 * in future release (2.09-2.10)
2087 */
2088 if (i == PP_RMACRO || i == PP_RMACRO || i == PP_EXITMACRO) {
2089 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2090 tline->text);
2091 return NO_DIRECTIVE_FOUND;
2092 }
2093
2094 /*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002095 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00002096 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002097 * we should ignore all directives except for condition
2098 * directives.
2099 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002100 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002101 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2102 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002103 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002104
2105 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002106 * If we're defining a macro or reading a %rep block, we should
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002107 * ignore all directives except for %macro/%imacro (which nest),
2108 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2109 * If we're in a %rep block, another %rep nests, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002110 */
2111 if (defining && i != PP_MACRO && i != PP_IMACRO &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002112 i != PP_RMACRO && i != PP_IRMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00002113 i != PP_ENDMACRO && i != PP_ENDM &&
2114 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2115 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002116 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002117
Charles Crayned4200be2008-07-12 16:42:33 -07002118 if (defining) {
Keith Kanios852f1ee2009-07-12 00:19:55 -05002119 if (i == PP_MACRO || i == PP_IMACRO ||
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002120 i == PP_RMACRO || i == PP_IRMACRO) {
Charles Crayned4200be2008-07-12 16:42:33 -07002121 nested_mac_count++;
2122 return NO_DIRECTIVE_FOUND;
2123 } else if (nested_mac_count > 0) {
2124 if (i == PP_ENDMACRO) {
2125 nested_mac_count--;
2126 return NO_DIRECTIVE_FOUND;
2127 }
2128 }
2129 if (!defining->name) {
2130 if (i == PP_REP) {
2131 nested_rep_count++;
2132 return NO_DIRECTIVE_FOUND;
2133 } else if (nested_rep_count > 0) {
2134 if (i == PP_ENDREP) {
2135 nested_rep_count--;
2136 return NO_DIRECTIVE_FOUND;
2137 }
2138 }
2139 }
2140 }
2141
H. Peter Anvin4169a472007-09-12 01:29:43 +00002142 switch (i) {
2143 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002144 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2145 tline->text);
2146 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002147
H. Peter Anvine2c80182005-01-15 22:15:51 +00002148 case PP_STACKSIZE:
2149 /* Directive to tell NASM what the default stack size is. The
2150 * default is for a 16-bit stack, and this can be overriden with
2151 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002152 */
2153 tline = tline->next;
2154 if (tline && tline->type == TOK_WHITESPACE)
2155 tline = tline->next;
2156 if (!tline || tline->type != TOK_ID) {
2157 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2158 free_tlist(origline);
2159 return DIRECTIVE_FOUND;
2160 }
2161 if (nasm_stricmp(tline->text, "flat") == 0) {
2162 /* All subsequent ARG directives are for a 32-bit stack */
2163 StackSize = 4;
2164 StackPointer = "ebp";
2165 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002166 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002167 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2168 /* All subsequent ARG directives are for a 64-bit stack */
2169 StackSize = 8;
2170 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002171 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002172 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002173 } else if (nasm_stricmp(tline->text, "large") == 0) {
2174 /* All subsequent ARG directives are for a 16-bit stack,
2175 * far function call.
2176 */
2177 StackSize = 2;
2178 StackPointer = "bp";
2179 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002180 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002181 } else if (nasm_stricmp(tline->text, "small") == 0) {
2182 /* All subsequent ARG directives are for a 16-bit stack,
2183 * far function call. We don't support near functions.
2184 */
2185 StackSize = 2;
2186 StackPointer = "bp";
2187 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002188 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002189 } else {
2190 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2191 free_tlist(origline);
2192 return DIRECTIVE_FOUND;
2193 }
2194 free_tlist(origline);
2195 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002196
H. Peter Anvine2c80182005-01-15 22:15:51 +00002197 case PP_ARG:
2198 /* TASM like ARG directive to define arguments to functions, in
2199 * the following form:
2200 *
2201 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2202 */
2203 offset = ArgOffset;
2204 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002205 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002206 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002207
H. Peter Anvine2c80182005-01-15 22:15:51 +00002208 /* Find the argument name */
2209 tline = tline->next;
2210 if (tline && tline->type == TOK_WHITESPACE)
2211 tline = tline->next;
2212 if (!tline || tline->type != TOK_ID) {
2213 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2214 free_tlist(origline);
2215 return DIRECTIVE_FOUND;
2216 }
2217 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002218
H. Peter Anvine2c80182005-01-15 22:15:51 +00002219 /* Find the argument size type */
2220 tline = tline->next;
2221 if (!tline || tline->type != TOK_OTHER
2222 || tline->text[0] != ':') {
2223 error(ERR_NONFATAL,
2224 "Syntax error processing `%%arg' directive");
2225 free_tlist(origline);
2226 return DIRECTIVE_FOUND;
2227 }
2228 tline = tline->next;
2229 if (!tline || tline->type != TOK_ID) {
2230 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2231 free_tlist(origline);
2232 return DIRECTIVE_FOUND;
2233 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002234
H. Peter Anvine2c80182005-01-15 22:15:51 +00002235 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002236 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002237 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002238 size = parse_size(tt->text);
2239 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002240 error(ERR_NONFATAL,
2241 "Invalid size type for `%%arg' missing directive");
2242 free_tlist(tt);
2243 free_tlist(origline);
2244 return DIRECTIVE_FOUND;
2245 }
2246 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002247
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002248 /* Round up to even stack slots */
2249 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002250
H. Peter Anvine2c80182005-01-15 22:15:51 +00002251 /* Now define the macro for the argument */
2252 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2253 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002254 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002255 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002256
H. Peter Anvine2c80182005-01-15 22:15:51 +00002257 /* Move to the next argument in the list */
2258 tline = tline->next;
2259 if (tline && tline->type == TOK_WHITESPACE)
2260 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002261 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002262 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002263 free_tlist(origline);
2264 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002265
H. Peter Anvine2c80182005-01-15 22:15:51 +00002266 case PP_LOCAL:
2267 /* TASM like LOCAL directive to define local variables for a
2268 * function, in the following form:
2269 *
2270 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2271 *
2272 * The '= LocalSize' at the end is ignored by NASM, but is
2273 * required by TASM to define the local parameter size (and used
2274 * by the TASM macro package).
2275 */
2276 offset = LocalOffset;
2277 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002278 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002279 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002280
H. Peter Anvine2c80182005-01-15 22:15:51 +00002281 /* Find the argument name */
2282 tline = tline->next;
2283 if (tline && tline->type == TOK_WHITESPACE)
2284 tline = tline->next;
2285 if (!tline || tline->type != TOK_ID) {
2286 error(ERR_NONFATAL,
2287 "`%%local' missing argument parameter");
2288 free_tlist(origline);
2289 return DIRECTIVE_FOUND;
2290 }
2291 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002292
H. Peter Anvine2c80182005-01-15 22:15:51 +00002293 /* Find the argument size type */
2294 tline = tline->next;
2295 if (!tline || tline->type != TOK_OTHER
2296 || tline->text[0] != ':') {
2297 error(ERR_NONFATAL,
2298 "Syntax error processing `%%local' directive");
2299 free_tlist(origline);
2300 return DIRECTIVE_FOUND;
2301 }
2302 tline = tline->next;
2303 if (!tline || tline->type != TOK_ID) {
2304 error(ERR_NONFATAL,
2305 "`%%local' missing size type parameter");
2306 free_tlist(origline);
2307 return DIRECTIVE_FOUND;
2308 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002309
H. Peter Anvine2c80182005-01-15 22:15:51 +00002310 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002311 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002312 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002313 size = parse_size(tt->text);
2314 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002315 error(ERR_NONFATAL,
2316 "Invalid size type for `%%local' missing directive");
2317 free_tlist(tt);
2318 free_tlist(origline);
2319 return DIRECTIVE_FOUND;
2320 }
2321 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002322
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002323 /* Round up to even stack slots */
2324 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002325
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002326 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002327
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002328 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002329 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2330 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002331 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002332
H. Peter Anvine2c80182005-01-15 22:15:51 +00002333 /* Now define the assign to setup the enter_c macro correctly */
2334 snprintf(directive, sizeof(directive),
2335 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002336 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002337
H. Peter Anvine2c80182005-01-15 22:15:51 +00002338 /* Move to the next argument in the list */
2339 tline = tline->next;
2340 if (tline && tline->type == TOK_WHITESPACE)
2341 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002342 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002343 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002344 free_tlist(origline);
2345 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002346
H. Peter Anvine2c80182005-01-15 22:15:51 +00002347 case PP_CLEAR:
2348 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002349 error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002350 "trailing garbage after `%%clear' ignored");
2351 free_macros();
2352 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002353 free_tlist(origline);
2354 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002355
H. Peter Anvin418ca702008-05-30 10:42:30 -07002356 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002357 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002358 skip_white_(t);
2359 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002360 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002361 error(ERR_NONFATAL, "`%%depend' expects a file name");
2362 free_tlist(origline);
2363 return DIRECTIVE_FOUND; /* but we did _something_ */
2364 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002365 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002366 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002367 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002368 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002369 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002370 nasm_unquote_cstr(p, i);
2371 if (dephead && !in_list(*dephead, p)) {
2372 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2373 sl->next = NULL;
2374 strcpy(sl->str, p);
2375 *deptail = sl;
2376 deptail = &sl->next;
2377 }
2378 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002379 return DIRECTIVE_FOUND;
2380
2381 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002382 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002383 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002384
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002385 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002386 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002387 error(ERR_NONFATAL, "`%%include' expects a file name");
2388 free_tlist(origline);
2389 return DIRECTIVE_FOUND; /* but we did _something_ */
2390 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002391 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002392 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002393 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002394 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002395 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002396 nasm_unquote_cstr(p, i);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002397 inc = nasm_malloc(sizeof(Include));
2398 inc->next = istk;
2399 inc->conds = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002400 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002401 if (!inc->fp) {
2402 /* -MG given but file not found */
2403 nasm_free(inc);
2404 } else {
2405 inc->fname = src_set_fname(nasm_strdup(p));
2406 inc->lineno = src_set_linnum(0);
2407 inc->lineinc = 1;
2408 inc->expansion = NULL;
2409 inc->mstk = NULL;
2410 istk = inc;
2411 list->uplevel(LIST_INCLUDE);
2412 }
2413 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002414 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002415
H. Peter Anvind2456592008-06-19 15:04:18 -07002416 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002417 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002418 static macros_t *use_pkg;
2419 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002420
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002421 tline = tline->next;
2422 skip_white_(tline);
2423 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002424
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002425 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002426 tline->type != TOK_INTERNAL_STRING &&
2427 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002428 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002429 free_tlist(origline);
2430 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002431 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002432 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002433 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002434 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002435 if (tline->type == TOK_STRING)
2436 nasm_unquote_cstr(tline->text, i);
2437 use_pkg = nasm_stdmac_find_package(tline->text);
2438 if (!use_pkg)
2439 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2440 else
2441 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002442 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002443 /* Not already included, go ahead and include it */
2444 stdmacpos = use_pkg;
2445 }
2446 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002447 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002448 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002449 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002450 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002451 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002452 tline = tline->next;
2453 skip_white_(tline);
2454 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002455 if (tline) {
2456 if (!tok_type_(tline, TOK_ID)) {
2457 error(ERR_NONFATAL, "`%s' expects a context identifier",
2458 pp_directives[i]);
2459 free_tlist(origline);
2460 return DIRECTIVE_FOUND; /* but we did _something_ */
2461 }
2462 if (tline->next)
2463 error(ERR_WARNING|ERR_PASS1,
2464 "trailing garbage after `%s' ignored",
2465 pp_directives[i]);
2466 p = nasm_strdup(tline->text);
2467 } else {
2468 p = NULL; /* Anonymous */
2469 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002470
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002471 if (i == PP_PUSH) {
2472 ctx = nasm_malloc(sizeof(Context));
2473 ctx->next = cstk;
2474 hash_init(&ctx->localmac, HASH_SMALL);
2475 ctx->name = p;
2476 ctx->number = unique++;
2477 cstk = ctx;
2478 } else {
2479 /* %pop or %repl */
2480 if (!cstk) {
2481 error(ERR_NONFATAL, "`%s': context stack is empty",
2482 pp_directives[i]);
2483 } else if (i == PP_POP) {
2484 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2485 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2486 "expected %s",
2487 cstk->name ? cstk->name : "anonymous", p);
2488 else
2489 ctx_pop();
2490 } else {
2491 /* i == PP_REPL */
2492 nasm_free(cstk->name);
2493 cstk->name = p;
2494 p = NULL;
2495 }
2496 nasm_free(p);
2497 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002498 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002499 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002500 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002501 severity = ERR_FATAL;
2502 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002503 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002504 severity = ERR_NONFATAL;
2505 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002506 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002507 severity = ERR_WARNING|ERR_WARN_USER;
2508 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002509
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002510issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002511 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002512 /* Only error out if this is the final pass */
2513 if (pass != 2 && i != PP_FATAL)
2514 return DIRECTIVE_FOUND;
2515
2516 tline->next = expand_smacro(tline->next);
2517 tline = tline->next;
2518 skip_white_(tline);
2519 t = tline ? tline->next : NULL;
2520 skip_white_(t);
2521 if (tok_type_(tline, TOK_STRING) && !t) {
2522 /* The line contains only a quoted string */
2523 p = tline->text;
2524 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2525 error(severity, "%s", p);
2526 } else {
2527 /* Not a quoted string, or more than a quoted string */
2528 p = detoken(tline, false);
2529 error(severity, "%s", p);
2530 nasm_free(p);
2531 }
2532 free_tlist(origline);
2533 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002534 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002535
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002536 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002537 if (istk->conds && !emitting(istk->conds->state))
2538 j = COND_NEVER;
2539 else {
2540 j = if_condition(tline->next, i);
2541 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002542 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2543 }
2544 cond = nasm_malloc(sizeof(Cond));
2545 cond->next = istk->conds;
2546 cond->state = j;
2547 istk->conds = cond;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002548 if(istk->mstk)
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002549 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002550 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002551 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002552
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002553 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002554 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00002555 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002556 switch(istk->conds->state) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002557 case COND_IF_TRUE:
2558 istk->conds->state = COND_DONE;
2559 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002560
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002561 case COND_DONE:
2562 case COND_NEVER:
2563 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002564
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002565 case COND_ELSE_TRUE:
2566 case COND_ELSE_FALSE:
2567 error_precond(ERR_WARNING|ERR_PASS1,
2568 "`%%elif' after `%%else' ignored");
2569 istk->conds->state = COND_NEVER;
2570 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002571
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002572 case COND_IF_FALSE:
2573 /*
2574 * IMPORTANT: In the case of %if, we will already have
2575 * called expand_mmac_params(); however, if we're
2576 * processing an %elif we must have been in a
2577 * non-emitting mode, which would have inhibited
2578 * the normal invocation of expand_mmac_params().
2579 * Therefore, we have to do it explicitly here.
2580 */
2581 j = if_condition(expand_mmac_params(tline->next), i);
2582 tline->next = NULL; /* it got freed */
2583 istk->conds->state =
2584 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2585 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002586 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002587 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002588 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002589
H. Peter Anvine2c80182005-01-15 22:15:51 +00002590 case PP_ELSE:
2591 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002592 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002593 "trailing garbage after `%%else' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002594 if (!istk->conds)
2595 error(ERR_FATAL, "`%%else': no matching `%%if'");
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002596 switch(istk->conds->state) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002597 case COND_IF_TRUE:
2598 case COND_DONE:
2599 istk->conds->state = COND_ELSE_FALSE;
2600 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002601
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002602 case COND_NEVER:
2603 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002604
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002605 case COND_IF_FALSE:
2606 istk->conds->state = COND_ELSE_TRUE;
2607 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002608
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002609 case COND_ELSE_TRUE:
2610 case COND_ELSE_FALSE:
2611 error_precond(ERR_WARNING|ERR_PASS1,
2612 "`%%else' after `%%else' ignored.");
2613 istk->conds->state = COND_NEVER;
2614 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002615 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002616 free_tlist(origline);
2617 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002618
H. Peter Anvine2c80182005-01-15 22:15:51 +00002619 case PP_ENDIF:
2620 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002621 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002622 "trailing garbage after `%%endif' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002623 if (!istk->conds)
2624 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2625 cond = istk->conds;
2626 istk->conds = cond->next;
2627 nasm_free(cond);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002628 if(istk->mstk)
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002629 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002630 free_tlist(origline);
2631 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002632
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002633 case PP_RMACRO:
2634 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002635 case PP_MACRO:
2636 case PP_IMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07002637 if (defining) {
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002638 error(ERR_FATAL, "`%s': already defining a macro",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002639 pp_directives[i]);
2640 return DIRECTIVE_FOUND;
2641 }
2642 defining = nasm_malloc(sizeof(MMacro));
2643 defining->max_depth =
2644 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2645 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2646 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2647 nasm_free(defining);
2648 defining = NULL;
2649 return DIRECTIVE_FOUND;
2650 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002651
H. Peter Anvin166c2472008-05-28 12:28:58 -07002652 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002653 while (mmac) {
2654 if (!strcmp(mmac->name, defining->name) &&
2655 (mmac->nparam_min <= defining->nparam_max
2656 || defining->plus)
2657 && (defining->nparam_min <= mmac->nparam_max
2658 || mmac->plus)) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07002659 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002660 "redefining multi-line macro `%s'", defining->name);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002661 return DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002662 }
2663 mmac = mmac->next;
2664 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002665 free_tlist(origline);
2666 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002667
H. Peter Anvine2c80182005-01-15 22:15:51 +00002668 case PP_ENDM:
2669 case PP_ENDMACRO:
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002670 if (! (defining && defining->name)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002671 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2672 return DIRECTIVE_FOUND;
2673 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002674 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002675 defining->next = *mmhead;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002676 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002677 defining = NULL;
2678 free_tlist(origline);
2679 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002680
H. Peter Anvin89cee572009-07-15 09:16:54 -04002681 case PP_EXITMACRO:
Keith Kanios852f1ee2009-07-12 00:19:55 -05002682 /*
2683 * We must search along istk->expansion until we hit a
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002684 * macro-end marker for a macro with a name. Then we
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002685 * bypass all lines between exitmacro and endmacro.
Keith Kanios852f1ee2009-07-12 00:19:55 -05002686 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002687 for (l = istk->expansion; l; l = l->next)
Keith Kanios852f1ee2009-07-12 00:19:55 -05002688 if (l->finishes && l->finishes->name)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002689 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002690
2691 if (l) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002692 /*
2693 * Remove all conditional entries relative to this
2694 * macro invocation. (safe to do in this context)
2695 */
Keith Kanios3c0d91f2009-10-25 13:28:03 -05002696 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2697 cond = istk->conds;
2698 istk->conds = cond->next;
2699 nasm_free(cond);
2700 }
2701 istk->expansion = l;
Keith Kanios852f1ee2009-07-12 00:19:55 -05002702 } else {
2703 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002704 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002705 free_tlist(origline);
2706 return DIRECTIVE_FOUND;
2707
H. Peter Anvina26433d2008-07-16 14:40:01 -07002708 case PP_UNMACRO:
2709 case PP_UNIMACRO:
2710 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002711 MMacro **mmac_p;
2712 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002713
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002714 spec.casesense = (i == PP_UNMACRO);
2715 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2716 return DIRECTIVE_FOUND;
2717 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002718 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2719 while (mmac_p && *mmac_p) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002720 mmac = *mmac_p;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002721 if (mmac->casesense == spec.casesense &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002722 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
H. Peter Anvina26433d2008-07-16 14:40:01 -07002723 mmac->nparam_min == spec.nparam_min &&
2724 mmac->nparam_max == spec.nparam_max &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002725 mmac->plus == spec.plus) {
2726 *mmac_p = mmac->next;
2727 free_mmacro(mmac);
2728 } else {
2729 mmac_p = &mmac->next;
2730 }
2731 }
2732 free_tlist(origline);
2733 free_tlist(spec.dlist);
2734 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002735 }
2736
H. Peter Anvine2c80182005-01-15 22:15:51 +00002737 case PP_ROTATE:
2738 if (tline->next && tline->next->type == TOK_WHITESPACE)
2739 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002740 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002741 free_tlist(origline);
2742 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2743 return DIRECTIVE_FOUND;
2744 }
2745 t = expand_smacro(tline->next);
2746 tline->next = NULL;
2747 free_tlist(origline);
2748 tline = t;
2749 tptr = &t;
2750 tokval.t_type = TOKEN_INVALID;
2751 evalresult =
2752 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2753 free_tlist(tline);
2754 if (!evalresult)
2755 return DIRECTIVE_FOUND;
2756 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002757 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002758 "trailing garbage after expression ignored");
2759 if (!is_simple(evalresult)) {
2760 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2761 return DIRECTIVE_FOUND;
2762 }
2763 mmac = istk->mstk;
2764 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2765 mmac = mmac->next_active;
2766 if (!mmac) {
2767 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2768 } else if (mmac->nparam == 0) {
2769 error(ERR_NONFATAL,
2770 "`%%rotate' invoked within macro without parameters");
2771 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002772 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002773
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002774 rotate %= (int)mmac->nparam;
2775 if (rotate < 0)
2776 rotate += mmac->nparam;
H. Peter Anvin70653092007-10-19 14:42:29 -07002777
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002778 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002779 }
2780 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002781
H. Peter Anvine2c80182005-01-15 22:15:51 +00002782 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002783 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002784 do {
2785 tline = tline->next;
2786 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002787
H. Peter Anvine2c80182005-01-15 22:15:51 +00002788 if (tok_type_(tline, TOK_ID) &&
2789 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002790 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002791 do {
2792 tline = tline->next;
2793 } while (tok_type_(tline, TOK_WHITESPACE));
2794 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002795
H. Peter Anvine2c80182005-01-15 22:15:51 +00002796 if (tline) {
2797 t = expand_smacro(tline);
2798 tptr = &t;
2799 tokval.t_type = TOKEN_INVALID;
2800 evalresult =
2801 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2802 if (!evalresult) {
2803 free_tlist(origline);
2804 return DIRECTIVE_FOUND;
2805 }
2806 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002807 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002808 "trailing garbage after expression ignored");
2809 if (!is_simple(evalresult)) {
2810 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2811 return DIRECTIVE_FOUND;
2812 }
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002813 count = reloc_value(evalresult) + 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002814 } else {
2815 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002816 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002817 }
2818 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002819
H. Peter Anvine2c80182005-01-15 22:15:51 +00002820 tmp_defining = defining;
2821 defining = nasm_malloc(sizeof(MMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002822 defining->prev = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002823 defining->name = NULL; /* flags this macro as a %rep block */
H. Peter Anvin70055962007-10-11 00:05:31 -07002824 defining->casesense = false;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002825 defining->plus = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002826 defining->nolist = nolist;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002827 defining->in_progress = count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002828 defining->max_depth = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002829 defining->nparam_min = defining->nparam_max = 0;
2830 defining->defaults = NULL;
2831 defining->dlist = NULL;
2832 defining->expansion = NULL;
2833 defining->next_active = istk->mstk;
2834 defining->rep_nest = tmp_defining;
2835 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002836
H. Peter Anvine2c80182005-01-15 22:15:51 +00002837 case PP_ENDREP:
2838 if (!defining || defining->name) {
2839 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2840 return DIRECTIVE_FOUND;
2841 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002842
H. Peter Anvine2c80182005-01-15 22:15:51 +00002843 /*
2844 * Now we have a "macro" defined - although it has no name
2845 * and we won't be entering it in the hash tables - we must
2846 * push a macro-end marker for it on to istk->expansion.
2847 * After that, it will take care of propagating itself (a
2848 * macro-end marker line for a macro which is really a %rep
2849 * block will cause the macro to be re-expanded, complete
2850 * with another macro-end marker to ensure the process
2851 * continues) until the whole expansion is forcibly removed
2852 * from istk->expansion by a %exitrep.
2853 */
2854 l = nasm_malloc(sizeof(Line));
2855 l->next = istk->expansion;
2856 l->finishes = defining;
2857 l->first = NULL;
2858 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002859
H. Peter Anvine2c80182005-01-15 22:15:51 +00002860 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002861
H. Peter Anvine2c80182005-01-15 22:15:51 +00002862 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2863 tmp_defining = defining;
2864 defining = defining->rep_nest;
2865 free_tlist(origline);
2866 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002867
H. Peter Anvine2c80182005-01-15 22:15:51 +00002868 case PP_EXITREP:
2869 /*
2870 * We must search along istk->expansion until we hit a
2871 * macro-end marker for a macro with no name. Then we set
2872 * its `in_progress' flag to 0.
2873 */
2874 for (l = istk->expansion; l; l = l->next)
2875 if (l->finishes && !l->finishes->name)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002876 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002877
H. Peter Anvine2c80182005-01-15 22:15:51 +00002878 if (l)
Charles Crayned4200be2008-07-12 16:42:33 -07002879 l->finishes->in_progress = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002880 else
2881 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2882 free_tlist(origline);
2883 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002884
H. Peter Anvine2c80182005-01-15 22:15:51 +00002885 case PP_XDEFINE:
2886 case PP_IXDEFINE:
2887 case PP_DEFINE:
2888 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002889 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002890
H. Peter Anvine2c80182005-01-15 22:15:51 +00002891 tline = tline->next;
2892 skip_white_(tline);
2893 tline = expand_id(tline);
2894 if (!tline || (tline->type != TOK_ID &&
2895 (tline->type != TOK_PREPROC_ID ||
2896 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002897 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002898 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002899 free_tlist(origline);
2900 return DIRECTIVE_FOUND;
2901 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002902
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002903 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002904 last = tline;
2905 param_start = tline = tline->next;
2906 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002907
H. Peter Anvine2c80182005-01-15 22:15:51 +00002908 /* Expand the macro definition now for %xdefine and %ixdefine */
2909 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2910 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002911
H. Peter Anvine2c80182005-01-15 22:15:51 +00002912 if (tok_is_(tline, "(")) {
2913 /*
2914 * This macro has parameters.
2915 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002916
H. Peter Anvine2c80182005-01-15 22:15:51 +00002917 tline = tline->next;
2918 while (1) {
2919 skip_white_(tline);
2920 if (!tline) {
2921 error(ERR_NONFATAL, "parameter identifier expected");
2922 free_tlist(origline);
2923 return DIRECTIVE_FOUND;
2924 }
2925 if (tline->type != TOK_ID) {
2926 error(ERR_NONFATAL,
2927 "`%s': parameter identifier expected",
2928 tline->text);
2929 free_tlist(origline);
2930 return DIRECTIVE_FOUND;
2931 }
2932 tline->type = TOK_SMAC_PARAM + nparam++;
2933 tline = tline->next;
2934 skip_white_(tline);
2935 if (tok_is_(tline, ",")) {
2936 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04002937 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002938 if (!tok_is_(tline, ")")) {
2939 error(ERR_NONFATAL,
2940 "`)' expected to terminate macro template");
2941 free_tlist(origline);
2942 return DIRECTIVE_FOUND;
2943 }
2944 break;
2945 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002946 }
2947 last = tline;
2948 tline = tline->next;
2949 }
2950 if (tok_type_(tline, TOK_WHITESPACE))
2951 last = tline, tline = tline->next;
2952 macro_start = NULL;
2953 last->next = NULL;
2954 t = tline;
2955 while (t) {
2956 if (t->type == TOK_ID) {
2957 for (tt = param_start; tt; tt = tt->next)
2958 if (tt->type >= TOK_SMAC_PARAM &&
2959 !strcmp(tt->text, t->text))
2960 t->type = tt->type;
2961 }
2962 tt = t->next;
2963 t->next = macro_start;
2964 macro_start = t;
2965 t = tt;
2966 }
2967 /*
2968 * Good. We now have a macro name, a parameter count, and a
2969 * token list (in reverse order) for an expansion. We ought
2970 * to be OK just to create an SMacro, store it, and let
2971 * free_tlist have the rest of the line (which we have
2972 * carefully re-terminated after chopping off the expansion
2973 * from the end).
2974 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002975 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002976 free_tlist(origline);
2977 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002978
H. Peter Anvine2c80182005-01-15 22:15:51 +00002979 case PP_UNDEF:
2980 tline = tline->next;
2981 skip_white_(tline);
2982 tline = expand_id(tline);
2983 if (!tline || (tline->type != TOK_ID &&
2984 (tline->type != TOK_PREPROC_ID ||
2985 tline->text[1] != '$'))) {
2986 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2987 free_tlist(origline);
2988 return DIRECTIVE_FOUND;
2989 }
2990 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07002991 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002992 "trailing garbage after macro name ignored");
2993 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002994
H. Peter Anvine2c80182005-01-15 22:15:51 +00002995 /* Find the context that symbol belongs to */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002996 ctx = get_ctx(tline->text, &mname, false);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002997 undef_smacro(ctx, mname);
2998 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002999 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003000
H. Peter Anvin9e200162008-06-04 17:23:14 -07003001 case PP_DEFSTR:
3002 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003003 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003004
3005 tline = tline->next;
3006 skip_white_(tline);
3007 tline = expand_id(tline);
3008 if (!tline || (tline->type != TOK_ID &&
3009 (tline->type != TOK_PREPROC_ID ||
3010 tline->text[1] != '$'))) {
3011 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003012 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003013 free_tlist(origline);
3014 return DIRECTIVE_FOUND;
3015 }
3016
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003017 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003018 last = tline;
3019 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003020 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003021
3022 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003023 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003024
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003025 p = detoken(tline, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003026 macro_start = nasm_malloc(sizeof(*macro_start));
3027 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003028 macro_start->text = nasm_quote(p, strlen(p));
3029 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003030 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003031 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003032
3033 /*
3034 * We now have a macro name, an implicit parameter count of
3035 * zero, and a string token to use as an expansion. Create
3036 * and store an SMacro.
3037 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003038 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003039 free_tlist(origline);
3040 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003041
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003042 case PP_DEFTOK:
3043 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003044 casesense = (i == PP_DEFTOK);
3045
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003046 tline = tline->next;
3047 skip_white_(tline);
3048 tline = expand_id(tline);
3049 if (!tline || (tline->type != TOK_ID &&
3050 (tline->type != TOK_PREPROC_ID ||
3051 tline->text[1] != '$'))) {
3052 error(ERR_NONFATAL,
3053 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003054 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003055 free_tlist(origline);
3056 return DIRECTIVE_FOUND;
3057 }
3058 ctx = get_ctx(tline->text, &mname, false);
3059 last = tline;
3060 tline = expand_smacro(tline->next);
3061 last->next = NULL;
3062
3063 t = tline;
3064 while (tok_type_(t, TOK_WHITESPACE))
3065 t = t->next;
3066 /* t should now point to the string */
3067 if (t->type != TOK_STRING) {
3068 error(ERR_NONFATAL,
3069 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003070 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003071 free_tlist(tline);
3072 free_tlist(origline);
3073 return DIRECTIVE_FOUND;
3074 }
3075
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003076 nasm_unquote_cstr(t->text, i);
3077 macro_start = tokenize(t->text);
3078
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003079 /*
3080 * We now have a macro name, an implicit parameter count of
3081 * zero, and a numeric token to use as an expansion. Create
3082 * and store an SMacro.
3083 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003084 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003085 free_tlist(tline);
3086 free_tlist(origline);
3087 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003088
H. Peter Anvin418ca702008-05-30 10:42:30 -07003089 case PP_PATHSEARCH:
3090 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003091 FILE *fp;
3092 StrList *xsl = NULL;
3093 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003094
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003095 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003096
3097 tline = tline->next;
3098 skip_white_(tline);
3099 tline = expand_id(tline);
3100 if (!tline || (tline->type != TOK_ID &&
3101 (tline->type != TOK_PREPROC_ID ||
3102 tline->text[1] != '$'))) {
3103 error(ERR_NONFATAL,
3104 "`%%pathsearch' expects a macro identifier as first parameter");
3105 free_tlist(origline);
3106 return DIRECTIVE_FOUND;
3107 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003108 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003109 last = tline;
3110 tline = expand_smacro(tline->next);
3111 last->next = NULL;
3112
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003113 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003114 while (tok_type_(t, TOK_WHITESPACE))
3115 t = t->next;
3116
3117 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003118 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07003119 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003120 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003121 free_tlist(origline);
3122 return DIRECTIVE_FOUND; /* but we did _something_ */
3123 }
3124 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003125 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003126 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003127 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003128 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003129 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003130
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003131 fp = inc_fopen(p, &xsl, &xst, true);
3132 if (fp) {
3133 p = xsl->str;
3134 fclose(fp); /* Don't actually care about the file */
3135 }
H. Peter Anvin418ca702008-05-30 10:42:30 -07003136 macro_start = nasm_malloc(sizeof(*macro_start));
3137 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003138 macro_start->text = nasm_quote(p, strlen(p));
3139 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003140 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003141 if (xsl)
3142 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003143
3144 /*
3145 * We now have a macro name, an implicit parameter count of
3146 * zero, and a string token to use as an expansion. Create
3147 * and store an SMacro.
3148 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003149 define_smacro(ctx, mname, casesense, 0, macro_start);
3150 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003151 free_tlist(origline);
3152 return DIRECTIVE_FOUND;
3153 }
3154
H. Peter Anvine2c80182005-01-15 22:15:51 +00003155 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003156 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003157
H. Peter Anvine2c80182005-01-15 22:15:51 +00003158 tline = tline->next;
3159 skip_white_(tline);
3160 tline = expand_id(tline);
3161 if (!tline || (tline->type != TOK_ID &&
3162 (tline->type != TOK_PREPROC_ID ||
3163 tline->text[1] != '$'))) {
3164 error(ERR_NONFATAL,
3165 "`%%strlen' expects a macro identifier as first parameter");
3166 free_tlist(origline);
3167 return DIRECTIVE_FOUND;
3168 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003169 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003170 last = tline;
3171 tline = expand_smacro(tline->next);
3172 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003173
H. Peter Anvine2c80182005-01-15 22:15:51 +00003174 t = tline;
3175 while (tok_type_(t, TOK_WHITESPACE))
3176 t = t->next;
3177 /* t should now point to the string */
3178 if (t->type != TOK_STRING) {
3179 error(ERR_NONFATAL,
3180 "`%%strlen` requires string as second parameter");
3181 free_tlist(tline);
3182 free_tlist(origline);
3183 return DIRECTIVE_FOUND;
3184 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003185
H. Peter Anvine2c80182005-01-15 22:15:51 +00003186 macro_start = nasm_malloc(sizeof(*macro_start));
3187 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003188 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003189 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003190
H. Peter Anvine2c80182005-01-15 22:15:51 +00003191 /*
3192 * We now have a macro name, an implicit parameter count of
3193 * zero, and a numeric token to use as an expansion. Create
3194 * and store an SMacro.
3195 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003196 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003197 free_tlist(tline);
3198 free_tlist(origline);
3199 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003200
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003201 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003202 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003203
3204 tline = tline->next;
3205 skip_white_(tline);
3206 tline = expand_id(tline);
3207 if (!tline || (tline->type != TOK_ID &&
3208 (tline->type != TOK_PREPROC_ID ||
3209 tline->text[1] != '$'))) {
3210 error(ERR_NONFATAL,
3211 "`%%strcat' expects a macro identifier as first parameter");
3212 free_tlist(origline);
3213 return DIRECTIVE_FOUND;
3214 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003215 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003216 last = tline;
3217 tline = expand_smacro(tline->next);
3218 last->next = NULL;
3219
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003220 len = 0;
3221 for (t = tline; t; t = t->next) {
3222 switch (t->type) {
3223 case TOK_WHITESPACE:
3224 break;
3225 case TOK_STRING:
3226 len += t->a.len = nasm_unquote(t->text, NULL);
3227 break;
3228 case TOK_OTHER:
3229 if (!strcmp(t->text, ",")) /* permit comma separators */
3230 break;
3231 /* else fall through */
3232 default:
3233 error(ERR_NONFATAL,
3234 "non-string passed to `%%strcat' (%d)", t->type);
3235 free_tlist(tline);
3236 free_tlist(origline);
3237 return DIRECTIVE_FOUND;
3238 }
3239 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003240
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003241 p = pp = nasm_malloc(len);
3242 for (t = tline; t; t = t->next) {
3243 if (t->type == TOK_STRING) {
3244 memcpy(p, t->text, t->a.len);
3245 p += t->a.len;
3246 }
3247 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003248
3249 /*
3250 * We now have a macro name, an implicit parameter count of
3251 * zero, and a numeric token to use as an expansion. Create
3252 * and store an SMacro.
3253 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003254 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3255 macro_start->text = nasm_quote(pp, len);
3256 nasm_free(pp);
3257 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003258 free_tlist(tline);
3259 free_tlist(origline);
3260 return DIRECTIVE_FOUND;
3261
H. Peter Anvine2c80182005-01-15 22:15:51 +00003262 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003263 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003264 int64_t a1, a2;
3265 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003266
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003267 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003268
H. Peter Anvine2c80182005-01-15 22:15:51 +00003269 tline = tline->next;
3270 skip_white_(tline);
3271 tline = expand_id(tline);
3272 if (!tline || (tline->type != TOK_ID &&
3273 (tline->type != TOK_PREPROC_ID ||
3274 tline->text[1] != '$'))) {
3275 error(ERR_NONFATAL,
3276 "`%%substr' expects a macro identifier as first parameter");
3277 free_tlist(origline);
3278 return DIRECTIVE_FOUND;
3279 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003280 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003281 last = tline;
3282 tline = expand_smacro(tline->next);
3283 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003284
H. Peter Anvine2c80182005-01-15 22:15:51 +00003285 t = tline->next;
3286 while (tok_type_(t, TOK_WHITESPACE))
3287 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003288
H. Peter Anvine2c80182005-01-15 22:15:51 +00003289 /* t should now point to the string */
3290 if (t->type != TOK_STRING) {
3291 error(ERR_NONFATAL,
3292 "`%%substr` requires string as second parameter");
3293 free_tlist(tline);
3294 free_tlist(origline);
3295 return DIRECTIVE_FOUND;
3296 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003297
H. Peter Anvine2c80182005-01-15 22:15:51 +00003298 tt = t->next;
3299 tptr = &tt;
3300 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003301 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003302 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003303 if (!evalresult) {
3304 free_tlist(tline);
3305 free_tlist(origline);
3306 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003307 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003308 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3309 free_tlist(tline);
3310 free_tlist(origline);
3311 return DIRECTIVE_FOUND;
3312 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003313 a1 = evalresult->value-1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003314
3315 while (tok_type_(tt, TOK_WHITESPACE))
3316 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003317 if (!tt) {
3318 a2 = 1; /* Backwards compatibility: one character */
3319 } else {
3320 tokval.t_type = TOKEN_INVALID;
3321 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3322 pass, error, NULL);
3323 if (!evalresult) {
3324 free_tlist(tline);
3325 free_tlist(origline);
3326 return DIRECTIVE_FOUND;
3327 } else if (!is_simple(evalresult)) {
3328 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3329 free_tlist(tline);
3330 free_tlist(origline);
3331 return DIRECTIVE_FOUND;
3332 }
3333 a2 = evalresult->value;
3334 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003335
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003336 len = nasm_unquote(t->text, NULL);
3337 if (a2 < 0)
3338 a2 = a2+1+len-a1;
3339 if (a1+a2 > (int64_t)len)
3340 a2 = len-a1;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003341
H. Peter Anvine2c80182005-01-15 22:15:51 +00003342 macro_start = nasm_malloc(sizeof(*macro_start));
3343 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003344 macro_start->text = nasm_quote((a1 < 0) ? "" : t->text+a1, a2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003345 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003346 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003347
H. Peter Anvine2c80182005-01-15 22:15:51 +00003348 /*
3349 * We now have a macro name, an implicit parameter count of
3350 * zero, and a numeric token to use as an expansion. Create
3351 * and store an SMacro.
3352 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003353 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003354 free_tlist(tline);
3355 free_tlist(origline);
3356 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003357 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003358
H. Peter Anvine2c80182005-01-15 22:15:51 +00003359 case PP_ASSIGN:
3360 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003361 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003362
H. Peter Anvine2c80182005-01-15 22:15:51 +00003363 tline = tline->next;
3364 skip_white_(tline);
3365 tline = expand_id(tline);
3366 if (!tline || (tline->type != TOK_ID &&
3367 (tline->type != TOK_PREPROC_ID ||
3368 tline->text[1] != '$'))) {
3369 error(ERR_NONFATAL,
3370 "`%%%sassign' expects a macro identifier",
3371 (i == PP_IASSIGN ? "i" : ""));
3372 free_tlist(origline);
3373 return DIRECTIVE_FOUND;
3374 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003375 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003376 last = tline;
3377 tline = expand_smacro(tline->next);
3378 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003379
H. Peter Anvine2c80182005-01-15 22:15:51 +00003380 t = tline;
3381 tptr = &t;
3382 tokval.t_type = TOKEN_INVALID;
3383 evalresult =
3384 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3385 free_tlist(tline);
3386 if (!evalresult) {
3387 free_tlist(origline);
3388 return DIRECTIVE_FOUND;
3389 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003390
H. Peter Anvine2c80182005-01-15 22:15:51 +00003391 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003392 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003393 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003394
H. Peter Anvine2c80182005-01-15 22:15:51 +00003395 if (!is_simple(evalresult)) {
3396 error(ERR_NONFATAL,
3397 "non-constant value given to `%%%sassign'",
3398 (i == PP_IASSIGN ? "i" : ""));
3399 free_tlist(origline);
3400 return DIRECTIVE_FOUND;
3401 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003402
H. Peter Anvine2c80182005-01-15 22:15:51 +00003403 macro_start = nasm_malloc(sizeof(*macro_start));
3404 macro_start->next = NULL;
3405 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003406 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003407
H. Peter Anvine2c80182005-01-15 22:15:51 +00003408 /*
3409 * We now have a macro name, an implicit parameter count of
3410 * zero, and a numeric token to use as an expansion. Create
3411 * and store an SMacro.
3412 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003413 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003414 free_tlist(origline);
3415 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003416
H. Peter Anvine2c80182005-01-15 22:15:51 +00003417 case PP_LINE:
3418 /*
3419 * Syntax is `%line nnn[+mmm] [filename]'
3420 */
3421 tline = tline->next;
3422 skip_white_(tline);
3423 if (!tok_type_(tline, TOK_NUMBER)) {
3424 error(ERR_NONFATAL, "`%%line' expects line number");
3425 free_tlist(origline);
3426 return DIRECTIVE_FOUND;
3427 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003428 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003429 m = 1;
3430 tline = tline->next;
3431 if (tok_is_(tline, "+")) {
3432 tline = tline->next;
3433 if (!tok_type_(tline, TOK_NUMBER)) {
3434 error(ERR_NONFATAL, "`%%line' expects line increment");
3435 free_tlist(origline);
3436 return DIRECTIVE_FOUND;
3437 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003438 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003439 tline = tline->next;
3440 }
3441 skip_white_(tline);
3442 src_set_linnum(k);
3443 istk->lineinc = m;
3444 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003445 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003446 }
3447 free_tlist(origline);
3448 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003449
H. Peter Anvine2c80182005-01-15 22:15:51 +00003450 default:
3451 error(ERR_FATAL,
3452 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003453 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003454 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003455 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003456}
3457
3458/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003459 * Ensure that a macro parameter contains a condition code and
3460 * nothing else. Return the condition code index if so, or -1
3461 * otherwise.
3462 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003463static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003464{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003465 Token *tt;
3466 int i, j, k, m;
3467
H. Peter Anvin25a99342007-09-22 17:45:45 -07003468 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003469 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003470
H. Peter Anvineba20a72002-04-30 20:53:55 +00003471 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003472 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003473 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003474 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003475 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003476 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003477 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003478
3479 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +00003480 j = elements(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003481 while (j - i > 1) {
3482 k = (j + i) / 2;
3483 m = nasm_stricmp(t->text, conditions[k]);
3484 if (m == 0) {
3485 i = k;
3486 j = -2;
3487 break;
3488 } else if (m < 0) {
3489 j = k;
3490 } else
3491 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003492 }
3493 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003494 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003495 return i;
3496}
3497
H. Peter Anvind784a082009-04-20 14:01:18 -07003498static bool paste_tokens(Token **head, bool handle_paste_tokens)
3499{
3500 Token **tail, *t, *tt;
3501 Token **paste_head;
3502 bool did_paste = false;
3503 char *tmp;
3504
3505 /* Now handle token pasting... */
3506 paste_head = NULL;
3507 tail = head;
3508 while ((t = *tail) && (tt = t->next)) {
3509 switch (t->type) {
3510 case TOK_WHITESPACE:
3511 if (tt->type == TOK_WHITESPACE) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003512 /* Zap adjacent whitespace tokens */
H. Peter Anvind784a082009-04-20 14:01:18 -07003513 t->next = delete_Token(tt);
3514 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003515 /* Do not advance paste_head here */
3516 tail = &t->next;
3517 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003518 break;
3519 case TOK_ID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003520 case TOK_PREPROC_ID:
H. Peter Anvind784a082009-04-20 14:01:18 -07003521 case TOK_NUMBER:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003522 case TOK_FLOAT:
3523 {
3524 size_t len = 0;
3525 char *tmp, *p;
H. Peter Anvind784a082009-04-20 14:01:18 -07003526
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003527 while (tt && (tt->type == TOK_ID || tt->type == TOK_PREPROC_ID ||
3528 tt->type == TOK_NUMBER || tt->type == TOK_FLOAT ||
3529 tt->type == TOK_OTHER)) {
3530 len += strlen(tt->text);
3531 tt = tt->next;
3532 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003533
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003534 /*
3535 * Now tt points to the first token after
3536 * the potential paste area...
3537 */
3538 if (tt != t->next) {
3539 /* We have at least two tokens... */
3540 len += strlen(t->text);
3541 p = tmp = nasm_malloc(len+1);
H. Peter Anvind784a082009-04-20 14:01:18 -07003542
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003543 while (t != tt) {
3544 strcpy(p, t->text);
3545 p = strchr(p, '\0');
3546 t = delete_Token(t);
3547 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003548
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003549 t = *tail = tokenize(tmp);
3550 nasm_free(tmp);
H. Peter Anvind784a082009-04-20 14:01:18 -07003551
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003552 while (t->next) {
3553 tail = &t->next;
3554 t = t->next;
3555 }
3556 t->next = tt; /* Attach the remaining token chain */
H. Peter Anvind784a082009-04-20 14:01:18 -07003557
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003558 did_paste = true;
3559 }
3560 paste_head = tail;
3561 tail = &t->next;
3562 break;
3563 }
3564 case TOK_PASTE: /* %+ */
3565 if (handle_paste_tokens) {
3566 /* Zap %+ and whitespace tokens to the right */
3567 while (t && (t->type == TOK_WHITESPACE ||
3568 t->type == TOK_PASTE))
3569 t = *tail = delete_Token(t);
3570 if (!paste_head || !t)
3571 break; /* Nothing to paste with */
3572 tail = paste_head;
3573 t = *tail;
3574 tt = t->next;
3575 while (tok_type_(tt, TOK_WHITESPACE))
3576 tt = t->next = delete_Token(tt);
H. Peter Anvind784a082009-04-20 14:01:18 -07003577
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003578 if (tt) {
3579 tmp = nasm_strcat(t->text, tt->text);
3580 delete_Token(t);
3581 tt = delete_Token(tt);
3582 t = *tail = tokenize(tmp);
3583 nasm_free(tmp);
3584 while (t->next) {
3585 tail = &t->next;
3586 t = t->next;
3587 }
3588 t->next = tt; /* Attach the remaining token chain */
3589 did_paste = true;
3590 }
3591 paste_head = tail;
3592 tail = &t->next;
3593 break;
3594 }
3595 /* else fall through */
3596 default:
3597 tail = paste_head = &t->next;
3598 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003599 }
3600 }
3601 return did_paste;
3602}
H. Peter Anvin76690a12002-04-30 20:52:49 +00003603/*
3604 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003605 * %-n) and MMacro-local identifiers (%%foo) as well as
3606 * macro indirection (%[...]).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003607 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003608static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003609{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003610 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003611 bool changed = false;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003612
3613 tail = &thead;
3614 thead = NULL;
3615
H. Peter Anvine2c80182005-01-15 22:15:51 +00003616 while (tline) {
3617 if (tline->type == TOK_PREPROC_ID &&
3618 (((tline->text[1] == '+' || tline->text[1] == '-')
3619 && tline->text[2]) || tline->text[1] == '%'
3620 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003621 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003622 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003623 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003624 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003625 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003626 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003627
H. Peter Anvine2c80182005-01-15 22:15:51 +00003628 t = tline;
3629 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003630
H. Peter Anvine2c80182005-01-15 22:15:51 +00003631 mac = istk->mstk;
3632 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3633 mac = mac->next_active;
3634 if (!mac)
3635 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
3636 else
3637 switch (t->text[1]) {
3638 /*
3639 * We have to make a substitution of one of the
3640 * forms %1, %-1, %+1, %%foo, %0.
3641 */
3642 case '0':
3643 type = TOK_NUMBER;
3644 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3645 text = nasm_strdup(tmpbuf);
3646 break;
3647 case '%':
3648 type = TOK_ID;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003649 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvine2c80182005-01-15 22:15:51 +00003650 mac->unique);
3651 text = nasm_strcat(tmpbuf, t->text + 2);
3652 break;
3653 case '-':
3654 n = atoi(t->text + 2) - 1;
3655 if (n >= mac->nparam)
3656 tt = NULL;
3657 else {
3658 if (mac->nparam > 1)
3659 n = (n + mac->rotate) % mac->nparam;
3660 tt = mac->params[n];
3661 }
3662 cc = find_cc(tt);
3663 if (cc == -1) {
3664 error(ERR_NONFATAL,
3665 "macro parameter %d is not a condition code",
3666 n + 1);
3667 text = NULL;
3668 } else {
3669 type = TOK_ID;
3670 if (inverse_ccs[cc] == -1) {
3671 error(ERR_NONFATAL,
3672 "condition code `%s' is not invertible",
3673 conditions[cc]);
3674 text = NULL;
3675 } else
H. Peter Anvin67c63722008-10-26 23:49:00 -07003676 text = nasm_strdup(conditions[inverse_ccs[cc]]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003677 }
3678 break;
3679 case '+':
3680 n = atoi(t->text + 2) - 1;
3681 if (n >= mac->nparam)
3682 tt = NULL;
3683 else {
3684 if (mac->nparam > 1)
3685 n = (n + mac->rotate) % mac->nparam;
3686 tt = mac->params[n];
3687 }
3688 cc = find_cc(tt);
3689 if (cc == -1) {
3690 error(ERR_NONFATAL,
3691 "macro parameter %d is not a condition code",
3692 n + 1);
3693 text = NULL;
3694 } else {
3695 type = TOK_ID;
3696 text = nasm_strdup(conditions[cc]);
3697 }
3698 break;
3699 default:
3700 n = atoi(t->text + 1) - 1;
3701 if (n >= mac->nparam)
3702 tt = NULL;
3703 else {
3704 if (mac->nparam > 1)
3705 n = (n + mac->rotate) % mac->nparam;
3706 tt = mac->params[n];
3707 }
3708 if (tt) {
3709 for (i = 0; i < mac->paramlen[n]; i++) {
3710 *tail = new_Token(NULL, tt->type, tt->text, 0);
3711 tail = &(*tail)->next;
3712 tt = tt->next;
3713 }
3714 }
3715 text = NULL; /* we've done it here */
3716 break;
3717 }
3718 if (!text) {
3719 delete_Token(t);
3720 } else {
3721 *tail = t;
3722 tail = &t->next;
3723 t->type = type;
3724 nasm_free(t->text);
3725 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003726 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003727 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003728 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003729 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003730 } else if (tline->type == TOK_INDIRECT) {
3731 t = tline;
3732 tline = tline->next;
3733 tt = tokenize(t->text);
3734 tt = expand_mmac_params(tt);
3735 tt = expand_smacro(tt);
3736 *tail = tt;
3737 while (tt) {
3738 tt->a.mac = NULL; /* Necessary? */
3739 tail = &tt->next;
3740 tt = tt->next;
3741 }
3742 delete_Token(t);
3743 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003744 } else {
3745 t = *tail = tline;
3746 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003747 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003748 tail = &t->next;
3749 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003750 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003751 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07003752
H. Peter Anvind784a082009-04-20 14:01:18 -07003753 if (changed)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003754 paste_tokens(&thead, false);
H. Peter Anvin6125b622009-04-08 14:02:25 -07003755
H. Peter Anvin76690a12002-04-30 20:52:49 +00003756 return thead;
3757}
3758
3759/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003760 * Expand all single-line macro calls made in the given line.
3761 * Return the expanded version of the line. The original is deemed
3762 * to be destroyed in the process. (In reality we'll just move
3763 * Tokens from input to output a lot of the time, rather than
3764 * actually bothering to destroy and replicate.)
3765 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003766
H. Peter Anvine2c80182005-01-15 22:15:51 +00003767static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003768{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003769 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003770 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003771 Token **params;
3772 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07003773 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07003774 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003775 Token *org_tline = tline;
3776 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003777 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003778 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07003779 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003780
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003781 /*
3782 * Trick: we should avoid changing the start token pointer since it can
3783 * be contained in "next" field of other token. Because of this
3784 * we allocate a copy of first token and work with it; at the end of
3785 * routine we copy it back
3786 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003787 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04003788 tline = new_Token(org_tline->next, org_tline->type,
3789 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003790 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003791 nasm_free(org_tline->text);
3792 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003793 }
3794
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003795 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07003796
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003797again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003798 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04003799 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003800
H. Peter Anvine2c80182005-01-15 22:15:51 +00003801 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003802 if (!--deadman) {
3803 error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03003804 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003805 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003806
H. Peter Anvine2c80182005-01-15 22:15:51 +00003807 if ((mname = tline->text)) {
3808 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03003809 if (tline->type == TOK_ID) {
3810 head = (SMacro *)hash_findix(&smacros, mname);
3811 } else if (tline->type == TOK_PREPROC_ID) {
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003812 ctx = get_ctx(mname, &mname, true);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03003813 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
3814 } else
3815 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07003816
H. Peter Anvine2c80182005-01-15 22:15:51 +00003817 /*
3818 * We've hit an identifier. As in is_mmacro below, we first
3819 * check whether the identifier is a single-line macro at
3820 * all, then think about checking for parameters if
3821 * necessary.
3822 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003823 for (m = head; m; m = m->next)
3824 if (!mstrcmp(m->name, mname, m->casesense))
3825 break;
3826 if (m) {
3827 mstart = tline;
3828 params = NULL;
3829 paramsize = NULL;
3830 if (m->nparam == 0) {
3831 /*
3832 * Simple case: the macro is parameterless. Discard the
3833 * one token that the macro call took, and push the
3834 * expansion back on the to-do stack.
3835 */
3836 if (!m->expansion) {
3837 if (!strcmp("__FILE__", m->name)) {
3838 int32_t num = 0;
3839 char *file = NULL;
3840 src_get(&num, &file);
3841 tline->text = nasm_quote(file, strlen(file));
3842 tline->type = TOK_STRING;
3843 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003844 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003845 }
3846 if (!strcmp("__LINE__", m->name)) {
3847 nasm_free(tline->text);
3848 make_tok_num(tline, src_get_linnum());
3849 continue;
3850 }
3851 if (!strcmp("__BITS__", m->name)) {
3852 nasm_free(tline->text);
3853 make_tok_num(tline, globalbits);
3854 continue;
3855 }
3856 tline = delete_Token(tline);
3857 continue;
3858 }
3859 } else {
3860 /*
3861 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00003862 * exists and takes parameters. We must find the
3863 * parameters in the call, count them, find the SMacro
3864 * that corresponds to that form of the macro call, and
3865 * substitute for the parameters when we expand. What a
3866 * pain.
3867 */
3868 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003869 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003870 do {
3871 t = tline->next;
3872 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003873 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003874 t->text = NULL;
3875 t = tline->next = delete_Token(t);
3876 }
3877 tline = t;
3878 } while (tok_type_(tline, TOK_WHITESPACE));
3879 if (!tok_is_(tline, "(")) {
3880 /*
3881 * This macro wasn't called with parameters: ignore
3882 * the call. (Behaviour borrowed from gnu cpp.)
3883 */
3884 tline = mstart;
3885 m = NULL;
3886 } else {
3887 int paren = 0;
3888 int white = 0;
3889 brackets = 0;
3890 nparam = 0;
3891 sparam = PARAM_DELTA;
3892 params = nasm_malloc(sparam * sizeof(Token *));
3893 params[0] = tline->next;
3894 paramsize = nasm_malloc(sparam * sizeof(int));
3895 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003896 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003897 /*
3898 * For some unusual expansions
3899 * which concatenates function call
3900 */
3901 t = tline->next;
3902 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003903 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003904 t->text = NULL;
3905 t = tline->next = delete_Token(t);
3906 }
3907 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003908
H. Peter Anvine2c80182005-01-15 22:15:51 +00003909 if (!tline) {
3910 error(ERR_NONFATAL,
3911 "macro call expects terminating `)'");
3912 break;
3913 }
3914 if (tline->type == TOK_WHITESPACE
3915 && brackets <= 0) {
3916 if (paramsize[nparam])
3917 white++;
3918 else
3919 params[nparam] = tline->next;
3920 continue; /* parameter loop */
3921 }
3922 if (tline->type == TOK_OTHER
3923 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003924 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003925 if (ch == ',' && !paren && brackets <= 0) {
3926 if (++nparam >= sparam) {
3927 sparam += PARAM_DELTA;
3928 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04003929 sparam * sizeof(Token *));
3930 paramsize = nasm_realloc(paramsize,
3931 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003932 }
3933 params[nparam] = tline->next;
3934 paramsize[nparam] = 0;
3935 white = 0;
3936 continue; /* parameter loop */
3937 }
3938 if (ch == '{' &&
3939 (brackets > 0 || (brackets == 0 &&
3940 !paramsize[nparam])))
3941 {
3942 if (!(brackets++)) {
3943 params[nparam] = tline->next;
3944 continue; /* parameter loop */
3945 }
3946 }
3947 if (ch == '}' && brackets > 0)
3948 if (--brackets == 0) {
3949 brackets = -1;
3950 continue; /* parameter loop */
3951 }
3952 if (ch == '(' && !brackets)
3953 paren++;
3954 if (ch == ')' && brackets <= 0)
3955 if (--paren < 0)
3956 break;
3957 }
3958 if (brackets < 0) {
3959 brackets = 0;
3960 error(ERR_NONFATAL, "braces do not "
3961 "enclose all of macro parameter");
3962 }
3963 paramsize[nparam] += white + 1;
3964 white = 0;
3965 } /* parameter loop */
3966 nparam++;
3967 while (m && (m->nparam != nparam ||
3968 mstrcmp(m->name, mname,
3969 m->casesense)))
3970 m = m->next;
3971 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003972 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003973 "macro `%s' exists, "
3974 "but not taking %d parameters",
3975 mstart->text, nparam);
3976 }
3977 }
3978 if (m && m->in_progress)
3979 m = NULL;
3980 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07003981 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00003982 * Design question: should we handle !tline, which
3983 * indicates missing ')' here, or expand those
3984 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07003985 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00003986 */
3987 nasm_free(params);
3988 nasm_free(paramsize);
3989 tline = mstart;
3990 } else {
3991 /*
3992 * Expand the macro: we are placed on the last token of the
3993 * call, so that we can easily split the call from the
3994 * following tokens. We also start by pushing an SMAC_END
3995 * token for the cycle removal.
3996 */
3997 t = tline;
3998 if (t) {
3999 tline = t->next;
4000 t->next = NULL;
4001 }
4002 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004003 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004004 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004005 tline = tt;
4006 for (t = m->expansion; t; t = t->next) {
4007 if (t->type >= TOK_SMAC_PARAM) {
4008 Token *pcopy = tline, **ptail = &pcopy;
4009 Token *ttt, *pt;
4010 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004011
H. Peter Anvine2c80182005-01-15 22:15:51 +00004012 ttt = params[t->type - TOK_SMAC_PARAM];
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004013 i = paramsize[t->type - TOK_SMAC_PARAM];
4014 while (--i >= 0) {
4015 pt = *ptail = new_Token(tline, ttt->type,
4016 ttt->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004017 ptail = &pt->next;
4018 ttt = ttt->next;
4019 }
4020 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004021 } else if (t->type == TOK_PREPROC_Q) {
4022 tt = new_Token(tline, TOK_ID, mname, 0);
4023 tline = tt;
4024 } else if (t->type == TOK_PREPROC_QQ) {
4025 tt = new_Token(tline, TOK_ID, m->name, 0);
4026 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004027 } else {
4028 tt = new_Token(tline, t->type, t->text, 0);
4029 tline = tt;
4030 }
4031 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004032
H. Peter Anvine2c80182005-01-15 22:15:51 +00004033 /*
4034 * Having done that, get rid of the macro call, and clean
4035 * up the parameters.
4036 */
4037 nasm_free(params);
4038 nasm_free(paramsize);
4039 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004040 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004041 continue; /* main token loop */
4042 }
4043 }
4044 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004045
H. Peter Anvine2c80182005-01-15 22:15:51 +00004046 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004047 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004048 tline = delete_Token(tline);
4049 } else {
4050 t = *tail = tline;
4051 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004052 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004053 t->next = NULL;
4054 tail = &t->next;
4055 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004056 }
4057
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004058 /*
4059 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004060 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004061 * TOK_IDs should be concatenated.
4062 * Also we look for %+ tokens and concatenate the tokens before and after
4063 * them (without white spaces in between).
4064 */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004065 if (expanded && paste_tokens(&thead, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004066 /*
4067 * If we concatenated something, *and* we had previously expanded
4068 * an actual macro, scan the lines again for macros...
4069 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004070 tline = thead;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004071 expanded = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004072 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004073 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004074
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004075err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004076 if (org_tline) {
4077 if (thead) {
4078 *org_tline = *thead;
4079 /* since we just gave text to org_line, don't free it */
4080 thead->text = NULL;
4081 delete_Token(thead);
4082 } else {
4083 /* the expression expanded to empty line;
4084 we can't return NULL for some reasons
4085 we just set the line to a single WHITESPACE token. */
4086 memset(org_tline, 0, sizeof(*org_tline));
4087 org_tline->text = NULL;
4088 org_tline->type = TOK_WHITESPACE;
4089 }
4090 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004091 }
4092
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004093 return thead;
4094}
4095
4096/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004097 * Similar to expand_smacro but used exclusively with macro identifiers
4098 * right before they are fetched in. The reason is that there can be
4099 * identifiers consisting of several subparts. We consider that if there
4100 * are more than one element forming the name, user wants a expansion,
4101 * otherwise it will be left as-is. Example:
4102 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004103 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004104 *
4105 * the identifier %$abc will be left as-is so that the handler for %define
4106 * will suck it and define the corresponding value. Other case:
4107 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004108 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004109 *
4110 * In this case user wants name to be expanded *before* %define starts
4111 * working, so we'll expand %$abc into something (if it has a value;
4112 * otherwise it will be left as-is) then concatenate all successive
4113 * PP_IDs into one.
4114 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004115static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004116{
4117 Token *cur, *oldnext = NULL;
4118
H. Peter Anvin734b1882002-04-30 21:01:08 +00004119 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004120 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004121
4122 cur = tline;
4123 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00004124 (cur->next->type == TOK_ID ||
4125 cur->next->type == TOK_PREPROC_ID
4126 || cur->next->type == TOK_NUMBER))
4127 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004128
4129 /* If identifier consists of just one token, don't expand */
4130 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004131 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004132
H. Peter Anvine2c80182005-01-15 22:15:51 +00004133 if (cur) {
4134 oldnext = cur->next; /* Detach the tail past identifier */
4135 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004136 }
4137
H. Peter Anvin734b1882002-04-30 21:01:08 +00004138 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004139
H. Peter Anvine2c80182005-01-15 22:15:51 +00004140 if (cur) {
4141 /* expand_smacro possibly changhed tline; re-scan for EOL */
4142 cur = tline;
4143 while (cur && cur->next)
4144 cur = cur->next;
4145 if (cur)
4146 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004147 }
4148
4149 return tline;
4150}
4151
4152/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004153 * Determine whether the given line constitutes a multi-line macro
4154 * call, and return the MMacro structure called if so. Doesn't have
4155 * to check for an initial label - that's taken care of in
4156 * expand_mmacro - but must check numbers of parameters. Guaranteed
4157 * to be called with tline->type == TOK_ID, so the putative macro
4158 * name is easy to find.
4159 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004160static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004161{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004162 MMacro *head, *m;
4163 Token **params;
4164 int nparam;
4165
H. Peter Anvin166c2472008-05-28 12:28:58 -07004166 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004167
4168 /*
4169 * Efficiency: first we see if any macro exists with the given
4170 * name. If not, we can return NULL immediately. _Then_ we
4171 * count the parameters, and then we look further along the
4172 * list if necessary to find the proper MMacro.
4173 */
4174 for (m = head; m; m = m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004175 if (!mstrcmp(m->name, tline->text, m->casesense))
4176 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004177 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004178 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004179
4180 /*
4181 * OK, we have a potential macro. Count and demarcate the
4182 * parameters.
4183 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004184 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004185
4186 /*
4187 * So we know how many parameters we've got. Find the MMacro
4188 * structure that handles this number.
4189 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004190 while (m) {
4191 if (m->nparam_min <= nparam
4192 && (m->plus || nparam <= m->nparam_max)) {
4193 /*
4194 * This one is right. Just check if cycle removal
4195 * prohibits us using it before we actually celebrate...
4196 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004197 if (m->in_progress > m->max_depth) {
4198 if (m->max_depth > 0) {
4199 error(ERR_WARNING,
4200 "reached maximum recursion depth of %i",
4201 m->max_depth);
4202 }
4203 nasm_free(params);
4204 return NULL;
4205 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004206 /*
4207 * It's right, and we can use it. Add its default
4208 * parameters to the end of our list if necessary.
4209 */
4210 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
4211 params =
4212 nasm_realloc(params,
4213 ((m->nparam_min + m->ndefs +
4214 1) * sizeof(*params)));
4215 while (nparam < m->nparam_min + m->ndefs) {
4216 params[nparam] = m->defaults[nparam - m->nparam_min];
4217 nparam++;
4218 }
4219 }
4220 /*
4221 * If we've gone over the maximum parameter count (and
4222 * we're in Plus mode), ignore parameters beyond
4223 * nparam_max.
4224 */
4225 if (m->plus && nparam > m->nparam_max)
4226 nparam = m->nparam_max;
4227 /*
4228 * Then terminate the parameter list, and leave.
4229 */
4230 if (!params) { /* need this special case */
4231 params = nasm_malloc(sizeof(*params));
4232 nparam = 0;
4233 }
4234 params[nparam] = NULL;
4235 *params_array = params;
4236 return m;
4237 }
4238 /*
4239 * This one wasn't right: look for the next one with the
4240 * same name.
4241 */
4242 for (m = m->next; m; m = m->next)
4243 if (!mstrcmp(m->name, tline->text, m->casesense))
4244 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004245 }
4246
4247 /*
4248 * After all that, we didn't find one with the right number of
4249 * parameters. Issue a warning, and fail to expand the macro.
4250 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004251 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004252 "macro `%s' exists, but not taking %d parameters",
4253 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004254 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004255 return NULL;
4256}
4257
Keith Kanios891775e2009-07-11 06:08:54 -05004258
4259/*
4260 * Save MMacro invocation specific fields in
4261 * preparation for a recursive macro expansion
4262 */
4263static void push_mmacro(MMacro *m)
4264{
4265 MMacroInvocation *i;
4266
H. Peter Anvin89cee572009-07-15 09:16:54 -04004267 i = nasm_malloc(sizeof(MMacroInvocation));
4268 i->prev = m->prev;
4269 i->params = m->params;
4270 i->iline = m->iline;
4271 i->nparam = m->nparam;
4272 i->rotate = m->rotate;
4273 i->paramlen = m->paramlen;
4274 i->unique = m->unique;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004275 i->condcnt = m->condcnt;
H. Peter Anvin89cee572009-07-15 09:16:54 -04004276 m->prev = i;
Keith Kanios891775e2009-07-11 06:08:54 -05004277}
4278
4279
4280/*
4281 * Restore MMacro invocation specific fields that were
4282 * saved during a previous recursive macro expansion
4283 */
4284static void pop_mmacro(MMacro *m)
4285{
4286 MMacroInvocation *i;
4287
H. Peter Anvin89cee572009-07-15 09:16:54 -04004288 if (m->prev) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004289 i = m->prev;
4290 m->prev = i->prev;
4291 m->params = i->params;
4292 m->iline = i->iline;
4293 m->nparam = i->nparam;
4294 m->rotate = i->rotate;
4295 m->paramlen = i->paramlen;
4296 m->unique = i->unique;
4297 m->condcnt = i->condcnt;
4298 nasm_free(i);
H. Peter Anvin89cee572009-07-15 09:16:54 -04004299 }
Keith Kanios891775e2009-07-11 06:08:54 -05004300}
4301
4302
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004303/*
4304 * Expand the multi-line macro call made by the given line, if
4305 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00004306 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004307 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004308static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004309{
4310 Token *startline = tline;
4311 Token *label = NULL;
4312 int dont_prepend = 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004313 Token **params, *t, *mtok, *tt;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004314 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004315 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004316 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004317 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004318
4319 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004320 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004321 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004322 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004323 return 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004324 mtok = t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004325 m = is_mmacro(t, &params);
H. Peter Anvinc751e862008-06-09 10:18:45 -07004326 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004327 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004328 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004329 Token *last;
4330 /*
4331 * We have an id which isn't a macro call. We'll assume
4332 * it might be a label; we'll also check to see if a
4333 * colon follows it. Then, if there's another id after
4334 * that lot, we'll check it again for macro-hood.
4335 */
4336 label = last = t;
4337 t = t->next;
4338 if (tok_type_(t, TOK_WHITESPACE))
4339 last = t, t = t->next;
4340 if (tok_is_(t, ":")) {
4341 dont_prepend = 1;
4342 last = t, t = t->next;
4343 if (tok_type_(t, TOK_WHITESPACE))
4344 last = t, t = t->next;
4345 }
H. Peter Anvin89cee572009-07-15 09:16:54 -04004346 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004347 return 0;
4348 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004349 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004350 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004351 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004352
4353 /*
4354 * Fix up the parameters: this involves stripping leading and
4355 * trailing whitespace, then stripping braces if they are
4356 * present.
4357 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004358 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004359 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004360
H. Peter Anvine2c80182005-01-15 22:15:51 +00004361 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004362 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004363 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004364
H. Peter Anvine2c80182005-01-15 22:15:51 +00004365 t = params[i];
4366 skip_white_(t);
4367 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004368 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004369 params[i] = t;
4370 paramlen[i] = 0;
4371 while (t) {
4372 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4373 break; /* ... because we have hit a comma */
4374 if (comma && t->type == TOK_WHITESPACE
4375 && tok_is_(t->next, ","))
4376 break; /* ... or a space then a comma */
4377 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4378 break; /* ... or a brace */
4379 t = t->next;
4380 paramlen[i]++;
4381 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004382 }
4383
4384 /*
4385 * OK, we have a MMacro structure together with a set of
4386 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00004387 * copies of each Line on to istk->expansion. Substitution of
4388 * parameter tokens and macro-local tokens doesn't get done
4389 * until the single-line macro substitution process; this is
4390 * because delaying them allows us to change the semantics
4391 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004392 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00004393 * First, push an end marker on to istk->expansion, mark this
4394 * macro as in progress, and set up its invocation-specific
4395 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004396 */
4397 ll = nasm_malloc(sizeof(Line));
4398 ll->next = istk->expansion;
4399 ll->finishes = m;
4400 ll->first = NULL;
4401 istk->expansion = ll;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004402
H. Peter Anvin89cee572009-07-15 09:16:54 -04004403 /*
4404 * Save the previous MMacro expansion in the case of
4405 * macro recursion
4406 */
4407 if (m->max_depth && m->in_progress)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004408 push_mmacro(m);
H. Peter Anvin76690a12002-04-30 20:52:49 +00004409
Keith Kanios891775e2009-07-11 06:08:54 -05004410 m->in_progress ++;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004411 m->params = params;
4412 m->iline = tline;
4413 m->nparam = nparam;
4414 m->rotate = 0;
4415 m->paramlen = paramlen;
4416 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004417 m->lineno = 0;
Keith Kanios3c0d91f2009-10-25 13:28:03 -05004418 m->condcnt = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004419
4420 m->next_active = istk->mstk;
4421 istk->mstk = m;
4422
H. Peter Anvine2c80182005-01-15 22:15:51 +00004423 for (l = m->expansion; l; l = l->next) {
4424 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004425
H. Peter Anvine2c80182005-01-15 22:15:51 +00004426 ll = nasm_malloc(sizeof(Line));
4427 ll->finishes = NULL;
4428 ll->next = istk->expansion;
4429 istk->expansion = ll;
4430 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004431
H. Peter Anvine2c80182005-01-15 22:15:51 +00004432 for (t = l->first; t; t = t->next) {
4433 Token *x = t;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004434 switch (t->type) {
4435 case TOK_PREPROC_Q:
4436 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
4437 break;
4438 case TOK_PREPROC_QQ:
4439 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4440 break;
4441 case TOK_PREPROC_ID:
4442 if (t->text[1] == '0' && t->text[2] == '0') {
4443 dont_prepend = -1;
4444 x = label;
4445 if (!x)
4446 continue;
4447 }
4448 /* fall through */
4449 default:
4450 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4451 break;
4452 }
4453 tail = &tt->next;
4454 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004455 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004456 }
4457
4458 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004459 * If we had a label, push it on as the first line of
4460 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004461 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004462 if (label) {
4463 if (dont_prepend < 0)
4464 free_tlist(startline);
4465 else {
4466 ll = nasm_malloc(sizeof(Line));
4467 ll->finishes = NULL;
4468 ll->next = istk->expansion;
4469 istk->expansion = ll;
4470 ll->first = startline;
4471 if (!dont_prepend) {
4472 while (label->next)
4473 label = label->next;
4474 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4475 }
4476 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004477 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004478
H. Peter Anvin734b1882002-04-30 21:01:08 +00004479 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004480
H. Peter Anvineba20a72002-04-30 20:53:55 +00004481 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004482}
4483
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004484/* The function that actually does the error reporting */
4485static void verror(int severity, const char *fmt, va_list arg)
4486{
4487 char buff[1024];
4488
4489 vsnprintf(buff, sizeof(buff), fmt, arg);
4490
4491 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004492 nasm_error(severity, "(%s:%d) %s", istk->mstk->name,
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004493 istk->mstk->lineno, buff);
4494 else
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004495 nasm_error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004496}
4497
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004498/*
4499 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07004500 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004501 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004502static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004503{
4504 va_list arg;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004505
4506 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00004507 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004508 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004509
H. Peter Anvin734b1882002-04-30 21:01:08 +00004510 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004511 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004512 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004513}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004514
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004515/*
4516 * Because %else etc are evaluated in the state context
4517 * of the previous branch, errors might get lost with error():
4518 * %if 0 ... %else trailing garbage ... %endif
4519 * So %else etc should report errors with this function.
4520 */
4521static void error_precond(int severity, const char *fmt, ...)
4522{
4523 va_list arg;
4524
4525 /* Only ignore the error if it's really in a dead branch */
4526 if (istk && istk->conds && istk->conds->state == COND_NEVER)
4527 return;
4528
4529 va_start(arg, fmt);
4530 verror(severity, fmt, arg);
4531 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004532}
4533
H. Peter Anvin734b1882002-04-30 21:01:08 +00004534static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004535pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004536{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004537 Token *t;
4538
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004539 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004540 istk = nasm_malloc(sizeof(Include));
4541 istk->next = NULL;
4542 istk->conds = NULL;
4543 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004544 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004545 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00004546 istk->fname = NULL;
4547 src_set_fname(nasm_strdup(file));
4548 src_set_linnum(0);
4549 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004550 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004551 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00004552 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004553 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004554 nested_mac_count = 0;
4555 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004556 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004557 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004558 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004559 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004560 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004561 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004562 }
H. Peter Anvind2456592008-06-19 15:04:18 -07004563 any_extrastdmac = extrastdmac && *extrastdmac;
4564 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004565 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004566
4567 /*
4568 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4569 * The caller, however, will also pass in 3 for preprocess-only so
4570 * we can set __PASS__ accordingly.
4571 */
4572 pass = apass > 2 ? 2 : apass;
4573
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004574 dephead = deptail = deplist;
4575 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004576 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4577 sl->next = NULL;
4578 strcpy(sl->str, file);
4579 *deptail = sl;
4580 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004581 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07004582
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004583 /*
4584 * Define the __PASS__ macro. This is defined here unlike
4585 * all the other builtins, because it is special -- it varies between
4586 * passes.
4587 */
H. Peter Anvin7383b402008-09-24 10:20:40 -07004588 t = nasm_malloc(sizeof(*t));
4589 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004590 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07004591 t->a.mac = NULL;
4592 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004593}
4594
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004595static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004596{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004597 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004598 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004599
H. Peter Anvine2c80182005-01-15 22:15:51 +00004600 while (1) {
4601 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004602 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004603 * buffer or from the input file.
4604 */
4605 tline = NULL;
4606 while (istk->expansion && istk->expansion->finishes) {
4607 Line *l = istk->expansion;
4608 if (!l->finishes->name && l->finishes->in_progress > 1) {
4609 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004610
H. Peter Anvine2c80182005-01-15 22:15:51 +00004611 /*
4612 * This is a macro-end marker for a macro with no
4613 * name, which means it's not really a macro at all
4614 * but a %rep block, and the `in_progress' field is
4615 * more than 1, meaning that we still need to
4616 * repeat. (1 means the natural last repetition; 0
4617 * means termination by %exitrep.) We have
4618 * therefore expanded up to the %endrep, and must
4619 * push the whole block on to the expansion buffer
4620 * again. We don't bother to remove the macro-end
4621 * marker: we'd only have to generate another one
4622 * if we did.
4623 */
4624 l->finishes->in_progress--;
4625 for (l = l->finishes->expansion; l; l = l->next) {
4626 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004627
H. Peter Anvine2c80182005-01-15 22:15:51 +00004628 ll = nasm_malloc(sizeof(Line));
4629 ll->next = istk->expansion;
4630 ll->finishes = NULL;
4631 ll->first = NULL;
4632 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004633
H. Peter Anvine2c80182005-01-15 22:15:51 +00004634 for (t = l->first; t; t = t->next) {
4635 if (t->text || t->type == TOK_WHITESPACE) {
4636 tt = *tail =
4637 new_Token(NULL, t->type, t->text, 0);
4638 tail = &tt->next;
4639 }
4640 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004641
H. Peter Anvine2c80182005-01-15 22:15:51 +00004642 istk->expansion = ll;
4643 }
4644 } else {
4645 /*
4646 * Check whether a `%rep' was started and not ended
4647 * within this macro expansion. This can happen and
4648 * should be detected. It's a fatal error because
4649 * I'm too confused to work out how to recover
4650 * sensibly from it.
4651 */
4652 if (defining) {
4653 if (defining->name)
4654 error(ERR_PANIC,
4655 "defining with name in expansion");
4656 else if (istk->mstk->name)
4657 error(ERR_FATAL,
4658 "`%%rep' without `%%endrep' within"
4659 " expansion of macro `%s'",
4660 istk->mstk->name);
4661 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00004662
H. Peter Anvine2c80182005-01-15 22:15:51 +00004663 /*
4664 * FIXME: investigate the relationship at this point between
4665 * istk->mstk and l->finishes
4666 */
4667 {
4668 MMacro *m = istk->mstk;
4669 istk->mstk = m->next_active;
4670 if (m->name) {
4671 /*
4672 * This was a real macro call, not a %rep, and
4673 * therefore the parameter information needs to
4674 * be freed.
4675 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004676 if (m->prev) {
4677 pop_mmacro(m);
4678 l->finishes->in_progress --;
4679 } else {
Keith Kanios891775e2009-07-11 06:08:54 -05004680 nasm_free(m->params);
4681 free_tlist(m->iline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004682 nasm_free(m->paramlen);
4683 l->finishes->in_progress = 0;
4684 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004685 } else
4686 free_mmacro(m);
4687 }
4688 istk->expansion = l->next;
4689 nasm_free(l);
4690 list->downlevel(LIST_MACRO);
4691 }
4692 }
4693 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004694
H. Peter Anvine2c80182005-01-15 22:15:51 +00004695 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004696 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004697 Line *l = istk->expansion;
4698 if (istk->mstk)
4699 istk->mstk->lineno++;
4700 tline = l->first;
4701 istk->expansion = l->next;
4702 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004703 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004704 list->line(LIST_MACRO, p);
4705 nasm_free(p);
4706 break;
4707 }
4708 line = read_line();
4709 if (line) { /* from the current input file */
4710 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004711 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004712 nasm_free(line);
4713 break;
4714 }
4715 /*
4716 * The current file has ended; work down the istk
4717 */
4718 {
4719 Include *i = istk;
4720 fclose(i->fp);
4721 if (i->conds)
4722 error(ERR_FATAL,
4723 "expected `%%endif' before end of file");
4724 /* only set line and file name if there's a next node */
4725 if (i->next) {
4726 src_set_linnum(i->lineno);
4727 nasm_free(src_set_fname(i->fname));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004728 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004729 istk = i->next;
4730 list->downlevel(LIST_INCLUDE);
4731 nasm_free(i);
4732 if (!istk)
4733 return NULL;
Victor van den Elzen4c9d6222008-10-01 13:08:50 +02004734 if (istk->expansion && istk->expansion->finishes)
4735 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004736 }
4737 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004738
H. Peter Anvine2c80182005-01-15 22:15:51 +00004739 /*
4740 * We must expand MMacro parameters and MMacro-local labels
4741 * _before_ we plunge into directive processing, to cope
4742 * with things like `%define something %1' such as STRUC
4743 * uses. Unless we're _defining_ a MMacro, in which case
4744 * those tokens should be left alone to go into the
4745 * definition; and unless we're in a non-emitting
4746 * condition, in which case we don't want to meddle with
4747 * anything.
4748 */
Charles Crayned4200be2008-07-12 16:42:33 -07004749 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin992fe752008-10-19 15:45:05 -07004750 && !(istk->mstk && !istk->mstk->in_progress)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004751 tline = expand_mmac_params(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004752 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004753
H. Peter Anvine2c80182005-01-15 22:15:51 +00004754 /*
4755 * Check the line to see if it's a preprocessor directive.
4756 */
4757 if (do_directive(tline) == DIRECTIVE_FOUND) {
4758 continue;
4759 } else if (defining) {
4760 /*
4761 * We're defining a multi-line macro. We emit nothing
4762 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00004763 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004764 */
4765 Line *l = nasm_malloc(sizeof(Line));
4766 l->next = defining->expansion;
4767 l->first = tline;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004768 l->finishes = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004769 defining->expansion = l;
4770 continue;
4771 } else if (istk->conds && !emitting(istk->conds->state)) {
4772 /*
4773 * We're in a non-emitting branch of a condition block.
4774 * Emit nothing at all, not even a blank line: when we
4775 * emerge from the condition we'll give a line-number
4776 * directive so we keep our place correctly.
4777 */
4778 free_tlist(tline);
4779 continue;
4780 } else if (istk->mstk && !istk->mstk->in_progress) {
4781 /*
4782 * We're in a %rep block which has been terminated, so
4783 * we're walking through to the %endrep without
4784 * emitting anything. Emit nothing at all, not even a
4785 * blank line: when we emerge from the %rep block we'll
4786 * give a line-number directive so we keep our place
4787 * correctly.
4788 */
4789 free_tlist(tline);
4790 continue;
4791 } else {
4792 tline = expand_smacro(tline);
4793 if (!expand_mmacro(tline)) {
4794 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004795 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004796 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004797 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004798 free_tlist(tline);
4799 break;
4800 } else {
4801 continue; /* expand_mmacro calls free_tlist */
4802 }
4803 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004804 }
4805
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004806 return line;
4807}
4808
H. Peter Anvine2c80182005-01-15 22:15:51 +00004809static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004810{
H. Peter Anvine2c80182005-01-15 22:15:51 +00004811 if (defining) {
H. Peter Anvin89cee572009-07-15 09:16:54 -04004812 if (defining->name) {
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02004813 error(ERR_NONFATAL,
4814 "end of file while still defining macro `%s'",
4815 defining->name);
4816 } else {
4817 error(ERR_NONFATAL, "end of file while still in %%rep");
4818 }
4819
H. Peter Anvine2c80182005-01-15 22:15:51 +00004820 free_mmacro(defining);
Cyrill Gorcunova327c652010-02-14 17:19:38 +03004821 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004822 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004823 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004824 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07004825 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00004826 while (istk) {
4827 Include *i = istk;
4828 istk = istk->next;
4829 fclose(i->fp);
4830 nasm_free(i->fname);
4831 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004832 }
4833 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004834 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07004835 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004836 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004837 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004838 free_llist(predef);
4839 delete_Blocks();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004840 while ((i = ipath)) {
4841 ipath = i->next;
4842 if (i->path)
4843 nasm_free(i->path);
4844 nasm_free(i);
4845 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07004846 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004847}
4848
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004849void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004850{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004851 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004852
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004853 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004854 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004855 i->next = NULL;
4856
H. Peter Anvin89cee572009-07-15 09:16:54 -04004857 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004858 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04004859 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004860 j = j->next;
4861 j->next = i;
4862 } else {
4863 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004864 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004865}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004866
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004867void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004868{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004869 Token *inc, *space, *name;
4870 Line *l;
4871
H. Peter Anvin734b1882002-04-30 21:01:08 +00004872 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4873 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4874 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004875
4876 l = nasm_malloc(sizeof(Line));
4877 l->next = predef;
4878 l->first = inc;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004879 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004880 predef = l;
4881}
4882
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004883void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004884{
4885 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004886 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004887 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004888
4889 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00004890 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4891 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004892 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004893 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00004894 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004895 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004896 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004897
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004898 l = nasm_malloc(sizeof(Line));
4899 l->next = predef;
4900 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004901 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004902 predef = l;
4903}
4904
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004905void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00004906{
4907 Token *def, *space;
4908 Line *l;
4909
H. Peter Anvin734b1882002-04-30 21:01:08 +00004910 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4911 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004912 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00004913
4914 l = nasm_malloc(sizeof(Line));
4915 l->next = predef;
4916 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004917 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00004918 predef = l;
4919}
4920
Keith Kaniosb7a89542007-04-12 02:40:54 +00004921/*
4922 * Added by Keith Kanios:
4923 *
4924 * This function is used to assist with "runtime" preprocessor
4925 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4926 *
4927 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4928 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4929 */
4930
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004931void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00004932{
4933 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07004934
Keith Kaniosb7a89542007-04-12 02:40:54 +00004935 def = tokenize(definition);
H. Peter Anvin89cee572009-07-15 09:16:54 -04004936 if (do_directive(def) == NO_DIRECTIVE_FOUND)
Keith Kaniosb7a89542007-04-12 02:40:54 +00004937 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07004938
Keith Kaniosb7a89542007-04-12 02:40:54 +00004939}
4940
H. Peter Anvina70547f2008-07-19 21:44:26 -07004941void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004942{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004943 extrastdmac = macros;
4944}
4945
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004946static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004947{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004948 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004949 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004950 tok->text = nasm_strdup(numbuf);
4951 tok->type = TOK_NUMBER;
4952}
4953
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004954Preproc nasmpp = {
4955 pp_reset,
4956 pp_getline,
4957 pp_cleanup
4958};