blob: f63594df1376b68aca60be4f7494be52245ca78f [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002 *
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07003 * Copyright 1996-2019 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 Anvinc2f3f262018-12-27 12:37:25 -080065#include "nctype.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000066
67#include "nasm.h"
68#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080069#include "error.h"
H. Peter Anvin4169a472007-09-12 01:29:43 +000070#include "preproc.h"
H. Peter Anvin97a23472007-09-16 17:57:25 -070071#include "hashtbl.h"
H. Peter Anvin8cad14b2008-06-01 17:23:51 -070072#include "quote.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070073#include "stdscan.h"
H. Peter Anvindbb640b2009-07-18 18:57:16 -070074#include "eval.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070075#include "tokens.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070076#include "tables.h"
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -080077#include "listing.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000078
79typedef struct SMacro SMacro;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080080typedef struct MMacro MMacro;
81typedef struct MMacroInvocation MMacroInvocation;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000082typedef struct Context Context;
83typedef struct Token Token;
84typedef struct Line Line;
85typedef struct Include Include;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080086typedef struct Cond Cond;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000087
88/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070089 * Note on the storage of both SMacro and MMacros: the hash table
90 * indexes them case-insensitively, and we then have to go through a
91 * linked list of potential case aliases (and, for MMacros, parameter
92 * ranges); this is to preserve the matching semantics of the earlier
93 * code. If the number of case aliases for a specific macro is a
94 * performance issue, you may want to reconsider your coding style.
95 */
96
97/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -070098 * Function call tp obtain the expansion of an smacro
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -070099 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700100typedef Token *(*ExpandSMacro)(const SMacro *s, Token **params, int nparams);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700101
102/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000103 * Store the definition of a single-line macro.
104 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700105enum sparmflags {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -0700106 SPARM_PLAIN = 0,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700107 SPARM_EVAL = 1, /* Evaluate as a numeric expression (=) */
108 SPARM_STR = 2, /* Convert to quoted string ($) */
109 SPARM_NOSTRIP = 4, /* Don't strip braces (!) */
110 SPARM_GREEDY = 8 /* Greedy final parameter (+) */
111};
112
113struct smac_param {
114 char *name;
115 int namelen;
116 enum sparmflags flags;
117};
118
H. Peter Anvine2c80182005-01-15 22:15:51 +0000119struct SMacro {
H. Peter Anvin8b262472019-02-26 14:00:54 -0800120 SMacro *next; /* MUST BE FIRST - see free_smacro() */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800121 char *name;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700122 Token *expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700123 ExpandSMacro expand;
124 intorptr expandpvt;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700125 struct smac_param *params;
126 int nparam;
127 bool greedy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800128 bool casesense;
129 bool in_progress;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -0700130 bool alias; /* This is an alias macro */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000131};
132
133/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800134 * Store the definition of a multi-line macro. This is also used to
135 * store the interiors of `%rep...%endrep' blocks, which are
136 * effectively self-re-invoking multi-line macros which simply
137 * don't have a name or bother to appear in the hash tables. %rep
138 * blocks are signified by having a NULL `name' field.
139 *
140 * In a MMacro describing a `%rep' block, the `in_progress' field
141 * isn't merely boolean, but gives the number of repeats left to
142 * run.
143 *
144 * The `next' field is used for storing MMacros in hash tables; the
145 * `next_active' field is for stacking them on istk entries.
146 *
147 * When a MMacro is being expanded, `params', `iline', `nparam',
148 * `paramlen', `rotate' and `unique' are local to the invocation.
149 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700150
151/*
152 * Expansion stack. Note that .mmac can point back to the macro itself,
153 * whereas .mstk cannot.
154 */
155struct mstk {
156 MMacro *mstk; /* Any expansion, real macro or not */
157 MMacro *mmac; /* Highest level actual mmacro */
158};
159
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800160struct MMacro {
161 MMacro *next;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700162#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800163 MMacroInvocation *prev; /* previous invocation */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700164#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800165 char *name;
166 int nparam_min, nparam_max;
167 bool casesense;
168 bool plus; /* is the last parameter greedy? */
169 bool nolist; /* is this macro listing-inhibited? */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700170 bool capture_label; /* macro definition has %00; capture label */
171 int32_t in_progress; /* is this macro currently being expanded? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800172 int32_t max_depth; /* maximum number of recursive expansions allowed */
173 Token *dlist; /* All defaults as one list */
174 Token **defaults; /* Parameter default pointers */
175 int ndefs; /* number of default parameters */
176 Line *expansion;
177
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700178 struct mstk mstk; /* Macro expansion stack */
179 struct mstk dstk; /* Macro definitions stack */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800180 Token **params; /* actual parameters */
181 Token *iline; /* invocation line */
182 unsigned int nparam, rotate;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700183 char *iname; /* name invoked as */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800184 int *paramlen;
185 uint64_t unique;
186 int lineno; /* Current line number on expansion */
187 uint64_t condcnt; /* number of if blocks... */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700188
H. Peter Anvin274cda82016-05-10 02:56:29 -0700189 const char *fname; /* File where defined */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700190 int32_t xline; /* First line in macro */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800191};
192
193
194/* Store the definition of a multi-line macro, as defined in a
195 * previous recursive macro expansion.
196 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700197#if 0
198
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800199struct MMacroInvocation {
200 MMacroInvocation *prev; /* previous invocation */
201 Token **params; /* actual parameters */
202 Token *iline; /* invocation line */
203 unsigned int nparam, rotate;
204 int *paramlen;
205 uint64_t unique;
206 uint64_t condcnt;
207};
208
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700209#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800210
211/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000212 * The context stack is composed of a linked list of these.
213 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000214struct Context {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800215 Context *next;
216 char *name;
217 struct hash_table localmac;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -0700218 uint64_t number;
219 unsigned int depth;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000220};
221
222/*
223 * This is the internal form which we break input lines up into.
224 * Typically stored in linked lists.
225 *
H. Peter Anvin8b262472019-02-26 14:00:54 -0800226 * Note that `type' serves a double meaning: TOK_SMAC_START_PARAMS is
227 * not necessarily used as-is, but is also used to encode the number
228 * and expansion type of substituted parameter. So in the definition
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000229 *
H. Peter Anvin8b262472019-02-26 14:00:54 -0800230 * %define a(x,=y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700231 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000232 * the token representing `x' will have its type changed to
H. Peter Anvin8b262472019-02-26 14:00:54 -0800233 * tok_smac_param(0) but the one representing `y' will be
234 * tok_smac_param(1); see the accessor functions below.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000235 *
236 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
237 * which doesn't need quotes around it. Used in the pre-include
238 * mechanism as an alternative to trying to find a sensible type of
239 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000240 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000241enum pp_token_type {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800242 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
243 TOK_PREPROC_ID, TOK_STRING,
H. Peter Anvin8b262472019-02-26 14:00:54 -0800244 TOK_NUMBER, TOK_FLOAT, TOK_OTHER,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700245 TOK_INTERNAL_STRING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800246 TOK_PREPROC_Q, TOK_PREPROC_QQ,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300247 TOK_PASTE, /* %+ */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -0700248 TOK_COND_COMMA, /* %, */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300249 TOK_INDIRECT, /* %[...] */
H. Peter Anvin8b262472019-02-26 14:00:54 -0800250 TOK_SMAC_START_PARAMS, /* MUST BE LAST IN THE LIST!!! */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300251 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000252};
253
H. Peter Anvin8b262472019-02-26 14:00:54 -0800254static inline enum pp_token_type tok_smac_param(int param)
255{
256 return TOK_SMAC_START_PARAMS + param;
257}
258static int smac_nparam(enum pp_token_type toktype)
259{
260 return toktype - TOK_SMAC_START_PARAMS;
261}
262static bool is_smac_param(enum pp_token_type toktype)
263{
264 return toktype >= TOK_SMAC_START_PARAMS;
265}
266
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -0700267#define PP_CONCAT_MASK(x) (1U << (x))
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400268
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400269struct tokseq_match {
270 int mask_head;
271 int mask_tail;
272};
273
H. Peter Anvine2c80182005-01-15 22:15:51 +0000274struct Token {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800275 Token *next;
276 char *text;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700277 size_t len;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800278 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000279};
280
281/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800282 * Multi-line macro definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000283 * these, which is essentially a container to allow several linked
284 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700285 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000286 * Note that in this module, linked lists are treated as stacks
287 * wherever possible. For this reason, Lines are _pushed_ on to the
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800288 * `expansion' field in MMacro structures, so that the linked list,
289 * if walked, would give the macro lines in reverse order; this
290 * means that we can walk the list when expanding a macro, and thus
291 * push the lines on to the `expansion' field in _istk_ in reverse
292 * order (so that when popped back off they are in the right
293 * order). It may seem cockeyed, and it relies on my design having
294 * an even number of steps in, but it works...
295 *
296 * Some of these structures, rather than being actual lines, are
297 * markers delimiting the end of the expansion of a given macro.
298 * This is for use in the cycle-tracking and %rep-handling code.
299 * Such structures have `finishes' non-NULL, and `first' NULL. All
300 * others have `finishes' NULL, but `first' may still be NULL if
301 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000302 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000303struct Line {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800304 Line *next;
305 MMacro *finishes;
306 Token *first;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500307};
308
309/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000310 * To handle an arbitrary level of file inclusion, we maintain a
311 * stack (ie linked list) of these things.
312 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000313struct Include {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800314 Include *next;
315 FILE *fp;
316 Cond *conds;
317 Line *expansion;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700318 const char *fname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700319 struct mstk mstk;
H. Peter Anvin6686de22019-08-10 05:33:14 -0700320 int lineno, lineinc;
321 bool nolist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000322};
323
324/*
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700325 * File real name hash, so we don't have to re-search the include
326 * path for every pass (and potentially more than that if a file
327 * is used more than once.)
328 */
329struct hash_table FileHash;
330
331/*
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -0700332 * Counters to trap on insane macro recursion or processing.
333 * Note: for smacros these count *down*, for mmacros they count *up*.
334 */
335struct deadman {
336 int64_t total; /* Total number of macros/tokens */
337 int64_t levels; /* Descent depth across all macros */
338 bool triggered; /* Already triggered, no need for error msg */
339};
340
341static struct deadman smacro_deadman, mmacro_deadman;
342
343/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000344 * Conditional assembly: we maintain a separate stack of these for
345 * each level of file inclusion. (The only reason we keep the
346 * stacks separate is to ensure that a stray `%endif' in a file
347 * included from within the true branch of a `%if' won't terminate
348 * it and cause confusion: instead, rightly, it'll cause an error.)
349 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -0700350enum cond_state {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000351 /*
352 * These states are for use just after %if or %elif: IF_TRUE
353 * means the condition has evaluated to truth so we are
354 * currently emitting, whereas IF_FALSE means we are not
355 * currently emitting but will start doing so if a %else comes
356 * up. In these states, all directives are admissible: %elif,
357 * %else and %endif. (And of course %if.)
358 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800359 COND_IF_TRUE, COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000360 /*
361 * These states come up after a %else: ELSE_TRUE means we're
362 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
363 * any %elif or %else will cause an error.
364 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800365 COND_ELSE_TRUE, COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000366 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200367 * These states mean that we're not emitting now, and also that
368 * nothing until %endif will be emitted at all. COND_DONE is
369 * used when we've had our moment of emission
370 * and have now started seeing %elifs. COND_NEVER is used when
371 * the condition construct in question is contained within a
372 * non-emitting branch of a larger condition construct,
373 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000374 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800375 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000376};
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -0700377struct Cond {
378 Cond *next;
379 enum cond_state state;
380};
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800381#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000382
H. Peter Anvin70653092007-10-19 14:42:29 -0700383/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000384 * These defines are used as the possible return values for do_directive
385 */
386#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300387#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000388
Keith Kanios852f1ee2009-07-12 00:19:55 -0500389/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000390 * Condition codes. Note that we use c_ prefix not C_ because C_ is
391 * used in nasm.h for the "real" condition codes. At _this_ level,
392 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
393 * ones, so we need a different enum...
394 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700395static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000396 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
397 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000398 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000399};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700400enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000401 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
402 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 -0700403 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
404 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000405};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700406static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000407 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
408 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 +0000409 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000410};
411
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800412/*
413 * Directive names.
414 */
415/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
416static int is_condition(enum preproc_token arg)
417{
418 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
419}
420
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000421/* For TASM compatibility we need to be able to recognise TASM compatible
422 * conditional compilation directives. Using the NASM pre-processor does
423 * not work, so we look for them specifically from the following list and
424 * then jam in the equivalent NASM directive into the input stream.
425 */
426
H. Peter Anvine2c80182005-01-15 22:15:51 +0000427enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000428 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
429 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
430};
431
H. Peter Anvin476d2862007-10-02 22:04:15 -0700432static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000433 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
434 "ifndef", "include", "local"
435};
436
437static int StackSize = 4;
H. Peter Anvin6c8b2be2016-05-24 23:46:50 -0700438static const char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000439static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800440static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000441
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000442static Context *cstk;
443static Include *istk;
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300444static const struct strlist *ipath_list;
H. Peter Anvind2354082019-08-27 16:38:48 -0700445static bool do_aliases;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000446
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300447static struct strlist *deplist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000448
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300449static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000450
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800451static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700452static bool do_predef;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800453static enum preproc_mode pp_mode;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000454
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000455/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800456 * The current set of multi-line macros we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000457 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800458static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000459
460/*
461 * The current set of single-line macros we have defined.
462 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700463static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000464
465/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800466 * The multi-line macro we are currently defining, or the %rep
467 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000468 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800469static MMacro *defining;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000470
Charles Crayned4200be2008-07-12 16:42:33 -0700471static uint64_t nested_mac_count;
472static uint64_t nested_rep_count;
473
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000474/*
475 * The number of macro parameters to allocate space for at a time.
476 */
477#define PARAM_DELTA 16
478
479/*
H. Peter Anvinf7606612016-07-13 14:23:48 -0700480 * The standard macro set: defined in macros.c in a set of arrays.
481 * This gives our position in any macro set, while we are processing it.
482 * The stdmacset is an array of such macro sets.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000483 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700484static macros_t *stdmacpos;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700485static macros_t **stdmacnext;
486static macros_t *stdmacros[8];
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +0300487static macros_t *extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000488
489/*
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -0700490 * Map of which %use packages have been loaded
491 */
492static bool *use_loaded;
493
494/*
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700495 * Tokens are allocated in blocks to improve speed. Set the blocksize
496 * to 0 to use regular nasm_malloc(); this is useful for debugging.
H. Peter Anvin734b1882002-04-30 21:01:08 +0000497 */
498#define TOKEN_BLOCKSIZE 4096
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700499
500#if TOKEN_BLOCKSIZE
501typedef struct Blocks Blocks;
H. Peter Anvince616072002-04-30 21:02:23 +0000502struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000503 Blocks *next;
504 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000505};
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800506static Blocks blocks = { NULL, NULL };
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700507static Token *freeTokens = NULL;
508#endif
H. Peter Anvin734b1882002-04-30 21:01:08 +0000509
510/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000511 * Forward declarations.
512 */
H. Peter Anvinf7606612016-07-13 14:23:48 -0700513static void pp_add_stdmac(macros_t *macros);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000514static Token *expand_mmac_params(Token * tline);
515static Token *expand_smacro(Token * tline);
516static Token *expand_id(Token * tline);
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +0400517static Context *get_ctx(const char *name, const char **namep);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800518static Token *make_tok_num(int64_t val);
519static Token *make_tok_qstr(const char *str);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700520static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700521 const char *text, size_t txtlen);
522static Token *dup_Token(Token *next, const Token *src);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700523static Token *new_White(Token *next);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000524static Token *delete_Token(Token * t);
H. Peter Anvindd88aa92019-09-12 19:39:48 -0700525static const struct use_package *
526get_use_pkg(Token *t, const char *dname, bool *err);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000527
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700528/* Safe test for token type, false on x == NULL */
529static inline bool tok_type(const Token *x, enum pp_token_type t)
530{
531 return x && x->type == t;
532}
533
534/* Whitespace token? */
535static inline bool tok_white(const Token *x)
536{
537 return tok_type(x, TOK_WHITESPACE);
538}
539
540/* Skip past any whitespace */
541static inline Token *skip_white(Token *x)
542{
543 while (tok_white(x))
544 x = x->next;
545
546 return x;
547}
548
549/* Delete any whitespace */
550static Token *zap_white(Token *x)
551{
552 while (tok_white(x))
553 x = delete_Token(x);
554
555 return x;
556}
557
558/* Single special character tests */
559static inline bool tok_is(const Token *x, char c)
560{
561 return x && x->type == TOK_OTHER && x->len == 1 && x->text[0] == c;
562}
563
564/* True if any other kind of token that "c", but not NULL */
565static inline bool tok_isnt(const Token *x, char c)
566{
567 return x && (x->type != TOK_OTHER || x->len != 1 || x->text[0] != c);
568}
H. Peter Anvin76690a12002-04-30 20:52:49 +0000569
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400570/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700571 * In-place reverse a list of tokens.
572 */
573static Token *reverse_tokens(Token *t)
574{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800575 Token *prev = NULL;
576 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700577
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800578 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400579 next = t->next;
580 t->next = prev;
581 prev = t;
582 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800583 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700584
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800585 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700586}
587
588/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300589 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000590 * front of them. We do it here because I could not find any other
591 * place to do it for the moment, and it is a hack (ideally it would
592 * be nice to be able to use the NASM pre-processor to do it).
593 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000594static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000595{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000596 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400597 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000598
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400599 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000600
601 /* Binary search for the directive name */
602 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400603 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400604 q = nasm_skip_word(p);
605 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000606 if (len) {
607 oldchar = p[len];
608 p[len] = 0;
609 while (j - i > 1) {
610 k = (j + i) / 2;
611 m = nasm_stricmp(p, tasm_directives[k]);
612 if (m == 0) {
613 /* We have found a directive, so jam a % in front of it
614 * so that NASM will then recognise it as one if it's own.
615 */
616 p[len] = oldchar;
617 len = strlen(p);
618 oldline = line;
619 line = nasm_malloc(len + 2);
620 line[0] = '%';
621 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700622 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300623 * NASM does not recognise IFDIFI, so we convert
624 * it to %if 0. This is not used in NASM
625 * compatible code, but does need to parse for the
626 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000627 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700628 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000629 } else {
630 memcpy(line + 1, p, len + 1);
631 }
632 nasm_free(oldline);
633 return line;
634 } else if (m < 0) {
635 j = k;
636 } else
637 i = k;
638 }
639 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000640 }
641 return line;
642}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000643
H. Peter Anvin76690a12002-04-30 20:52:49 +0000644/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000645 * The pre-preprocessing stage... This function translates line
646 * number indications as they emerge from GNU cpp (`# lineno "file"
647 * flags') into NASM preprocessor line number indications (`%line
648 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000649 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000650static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000651{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000652 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000653 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000654
H. Peter Anvine2c80182005-01-15 22:15:51 +0000655 if (line[0] == '#' && line[1] == ' ') {
656 oldline = line;
657 fname = oldline + 2;
658 lineno = atoi(fname);
659 fname += strspn(fname, "0123456789 ");
660 if (*fname == '"')
661 fname++;
662 fnlen = strcspn(fname, "\"");
663 line = nasm_malloc(20 + fnlen);
664 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
665 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000666 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000667 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000668 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000669 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000670}
671
672/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000673 * Free a linked list of tokens.
674 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000675static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000676{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400677 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000678 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000679}
680
681/*
682 * Free a linked list of lines.
683 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000684static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000685{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400686 Line *l, *tmp;
687 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000688 free_tlist(l->first);
689 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000690 }
691}
692
693/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700694 * Free an array of linked lists of tokens
695 */
696static void free_tlist_array(Token **array, size_t nlists)
697{
698 Token **listp = array;
699
700 while (nlists--)
701 free_tlist(*listp++);
702
703 nasm_free(array);
704}
705
706/*
707 * Duplicate a linked list of tokens.
708 */
709static Token *dup_tlist(const Token *list, Token ***tailp)
710{
711 Token *newlist = NULL;
712 Token **tailpp = &newlist;
713 const Token *t;
714
715 list_for_each(t, list) {
716 Token *nt;
717 *tailpp = nt = dup_Token(NULL, t);
718 tailpp = &nt->next;
719 }
720
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700721 if (tailp) {
722 **tailp = newlist;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700723 *tailp = tailpp;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700724 }
725
726 return newlist;
727}
728
729/*
730 * Duplicate a linked list of tokens with a maximum count
731 */
732static Token *dup_tlistn(const Token *list, size_t cnt, Token ***tailp)
733{
734 Token *newlist = NULL;
735 Token **tailpp = &newlist;
736 const Token *t;
737
738 list_for_each(t, list) {
739 Token *nt;
740 if (!cnt--)
741 break;
742 *tailpp = nt = dup_Token(NULL, t);
743 tailpp = &nt->next;
744 }
745
746 if (tailp) {
747 **tailp = newlist;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -0700748 if (newlist)
749 *tailp = tailpp;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700750 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700751
752 return newlist;
753}
754
755/*
756 * Duplicate a linked list of tokens in reverse order
757 */
758static Token *dup_tlist_reverse(const Token *list, Token *tail)
759{
760 const Token *t;
761
762 list_for_each(t, list)
763 tail = dup_Token(tail, t);
764
765 return tail;
766}
767
768/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800769 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +0000770 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800771static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000772{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800773 nasm_free(m->name);
774 free_tlist(m->dlist);
775 nasm_free(m->defaults);
776 free_llist(m->expansion);
777 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000778}
779
780/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700781 * Clear or free an SMacro
H. Peter Anvin8b262472019-02-26 14:00:54 -0800782 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700783static void free_smacro_members(SMacro *s)
H. Peter Anvin8b262472019-02-26 14:00:54 -0800784{
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700785 if (s->params) {
786 int i;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -0700787 for (i = 0; i < s->nparam; i++)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700788 nasm_free(s->params[i].name);
789 nasm_free(s->params);
790 }
H. Peter Anvin8b262472019-02-26 14:00:54 -0800791 nasm_free(s->name);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700792 free_tlist(s->expansion);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700793}
794
795static void clear_smacro(SMacro *s)
796{
797 free_smacro_members(s);
798 /* Wipe everything except the next pointer */
799 memset(&s->next + 1, 0, sizeof *s - sizeof s->next);
800}
801
802/*
803 * Free an SMacro
804 */
805static void free_smacro(SMacro *s)
806{
807 free_smacro_members(s);
808 nasm_free(s);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800809}
810
811/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700812 * Free all currently defined macros, and free the hash tables
813 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700814static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700815{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800816 struct hash_iterator it;
817 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700818
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800819 hash_for_each(smt, it, np) {
820 SMacro *tmp;
821 SMacro *s = np->data;
822 nasm_free((void *)np->key);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800823 list_for_each_safe(s, tmp, s)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700824 free_smacro(s);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700825 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700826 hash_free(smt);
827}
828
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800829static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700830{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800831 struct hash_iterator it;
832 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700833
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800834 hash_for_each(mmt, it, np) {
835 MMacro *tmp;
836 MMacro *m = np->data;
837 nasm_free((void *)np->key);
838 list_for_each_safe(m, tmp, m)
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800839 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700840 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800841 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700842}
843
844static void free_macros(void)
845{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700846 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800847 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700848}
849
850/*
851 * Initialize the hash tables
852 */
853static void init_macros(void)
854{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700855}
856
857/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000858 * Pop the context stack.
859 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000860static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000861{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000862 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000863
864 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700865 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000866 nasm_free(c->name);
867 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000868}
869
H. Peter Anvin072771e2008-05-22 13:17:51 -0700870/*
871 * Search for a key in the hash index; adding it if necessary
872 * (in which case we initialize the data pointer to NULL.)
873 */
874static void **
875hash_findi_add(struct hash_table *hash, const char *str)
876{
877 struct hash_insert hi;
878 void **r;
879 char *strx;
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800880 size_t l = strlen(str) + 1;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700881
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800882 r = hash_findib(hash, str, l, &hi);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700883 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300884 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700885
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800886 strx = nasm_malloc(l); /* Use a more efficient allocator here? */
887 memcpy(strx, str, l);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700888 return hash_add(&hi, strx, NULL);
889}
890
891/*
892 * Like hash_findi, but returns the data element rather than a pointer
893 * to it. Used only when not adding a new element, hence no third
894 * argument.
895 */
896static void *
897hash_findix(struct hash_table *hash, const char *str)
898{
899 void **p;
900
901 p = hash_findi(hash, str, NULL);
902 return p ? *p : NULL;
903}
904
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400905/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800906 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400907 * if there no more left -- return NULL
908 */
909static char *line_from_stdmac(void)
910{
911 unsigned char c;
912 const unsigned char *p = stdmacpos;
913 char *line, *q;
914 size_t len = 0;
915
916 if (!stdmacpos)
917 return NULL;
918
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -0700919 /*
920 * 32-126 is ASCII, 127 is end of line, 128-31 are directives
921 * (allowed to wrap around) corresponding to PP_* tokens 0-159.
922 */
923 while ((c = *p++) != 127) {
924 uint8_t ndir = c - 128;
925 if (ndir < 256-96)
926 len += pp_directives_len[ndir] + 1;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400927 else
928 len++;
929 }
930
931 line = nasm_malloc(len + 1);
932 q = line;
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -0700933
934 while ((c = *stdmacpos++) != 127) {
935 uint8_t ndir = c - 128;
936 if (ndir < 256-96) {
937 memcpy(q, pp_directives[ndir], pp_directives_len[ndir]);
938 q += pp_directives_len[ndir];
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400939 *q++ = ' ';
940 } else {
941 *q++ = c;
942 }
943 }
944 stdmacpos = p;
945 *q = '\0';
946
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -0700947 if (*stdmacpos == 127) {
H. Peter Anvinf7606612016-07-13 14:23:48 -0700948 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400949 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700950 if (*stdmacnext) {
951 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400952 } else if (do_predef) {
953 Line *pd, *l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400954
955 /*
956 * Nasty hack: here we push the contents of
957 * `predef' on to the top-level expansion stack,
958 * since this is the most convenient way to
959 * implement the pre-include and pre-define
960 * features.
961 */
962 list_for_each(pd, predef) {
H. Peter Anvin6686de22019-08-10 05:33:14 -0700963 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800964 l->next = istk->expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700965 l->first = dup_tlist(pd->first, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800966 l->finishes = NULL;
967
968 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400969 }
970 do_predef = false;
971 }
972 }
973
974 return line;
975}
976
H. Peter Anvin6686de22019-08-10 05:33:14 -0700977/*
978 * Read a line from a file. Return NULL on end of file.
979 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700980static char *line_from_file(FILE *f)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000981{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700982 int c;
983 unsigned int size, next;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400984 const unsigned int delta = 512;
985 const unsigned int pad = 8;
986 unsigned int nr_cont = 0;
987 bool cont = false;
988 char *buffer, *p;
H. Peter Anvinab6f8312019-08-09 22:31:45 -0700989 int32_t lineno;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000990
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400991 size = delta;
992 p = buffer = nasm_malloc(size);
993
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700994 do {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700995 c = fgetc(f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400996
997 switch (c) {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700998 case EOF:
999 if (p == buffer) {
1000 nasm_free(buffer);
1001 return NULL;
1002 }
1003 c = 0;
1004 break;
1005
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001006 case '\r':
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001007 next = fgetc(f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001008 if (next != '\n')
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001009 ungetc(next, f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001010 if (cont) {
1011 cont = false;
1012 continue;
1013 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001014 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001015 break;
1016
1017 case '\n':
1018 if (cont) {
1019 cont = false;
1020 continue;
1021 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001022 c = 0;
1023 break;
1024
1025 case 032: /* ^Z = legacy MS-DOS end of file mark */
1026 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001027 break;
1028
1029 case '\\':
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001030 next = fgetc(f);
1031 ungetc(next, f);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +04001032 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001033 cont = true;
1034 nr_cont++;
1035 continue;
1036 }
1037 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001038 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001039
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001040 if (p >= (buffer + size - pad)) {
1041 buffer = nasm_realloc(buffer, size + delta);
1042 p = buffer + size - pad;
1043 size += delta;
1044 }
1045
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001046 *p++ = c;
1047 } while (c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001048
H. Peter Anvinab6f8312019-08-09 22:31:45 -07001049 lineno = src_get_linnum() + istk->lineinc +
1050 (nr_cont * istk->lineinc);
1051 src_set_linnum(lineno);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001052
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001053 return buffer;
1054}
1055
1056/*
H. Peter Anvin6686de22019-08-10 05:33:14 -07001057 * Common read routine regardless of source
1058 */
1059static char *read_line(void)
1060{
1061 char *line;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001062 FILE *f = istk->fp;
H. Peter Anvin6686de22019-08-10 05:33:14 -07001063
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001064 if (f)
1065 line = line_from_file(f);
H. Peter Anvin6686de22019-08-10 05:33:14 -07001066 else
1067 line = line_from_stdmac();
1068
1069 if (!line)
1070 return NULL;
1071
1072 if (!istk->nolist)
1073 lfmt->line(LIST_READ, src_get_linnum(), line);
1074
1075 return line;
1076}
1077
1078/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00001079 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001080 * don't need to parse the value out of e.g. numeric tokens: we
1081 * simply split one string into many.
1082 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001083static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001084{
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001085 char c;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001086 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001087 Token *list = NULL;
1088 Token *t, **tail = &list;
1089
H. Peter Anvine2c80182005-01-15 22:15:51 +00001090 while (*line) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001091 char *p = line;
1092 char *ep = NULL; /* End of token, for trimming the end */
1093
H. Peter Anvine2c80182005-01-15 22:15:51 +00001094 if (*p == '%') {
1095 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001096 if (*p == '+' && !nasm_isdigit(p[1])) {
1097 p++;
1098 type = TOK_PASTE;
1099 } else if (nasm_isdigit(*p) ||
1100 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001101 do {
1102 p++;
1103 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001104 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001105 type = TOK_PREPROC_ID;
1106 } else if (*p == '{') {
1107 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001108 while (*p) {
1109 if (*p == '}')
1110 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001111 p[-1] = *p;
1112 p++;
1113 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001114 if (*p != '}')
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001115 nasm_warn(WARN_OTHER, "unterminated %%{ construct");
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001116 ep = &p[-1];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001117 if (*p)
1118 p++;
1119 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001120 } else if (*p == '[') {
1121 int lvl = 1;
1122 line += 2; /* Skip the leading %[ */
1123 p++;
1124 while (lvl && (c = *p++)) {
1125 switch (c) {
1126 case ']':
1127 lvl--;
1128 break;
1129 case '%':
1130 if (*p == '[')
1131 lvl++;
1132 break;
1133 case '\'':
1134 case '\"':
1135 case '`':
Cyrill Gorcunov3144e842017-10-22 10:50:55 +03001136 p = nasm_skip_string(p - 1);
1137 if (*p)
1138 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001139 break;
1140 default:
1141 break;
1142 }
1143 }
1144 p--;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001145 ep = p;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001146 if (*p)
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001147 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001148 if (lvl)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001149 nasm_nonfatalf(ERR_PASS1, "unterminated %%[ construct");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001150 type = TOK_INDIRECT;
1151 } else if (*p == '?') {
1152 type = TOK_PREPROC_Q; /* %? */
1153 p++;
1154 if (*p == '?') {
1155 type = TOK_PREPROC_QQ; /* %?? */
1156 p++;
1157 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001158 } else if (*p == '!') {
1159 type = TOK_PREPROC_ID;
1160 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001161 if (nasm_isidchar(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001162 do {
1163 p++;
1164 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001165 while (nasm_isidchar(*p));
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001166 } else if (nasm_isquote(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001167 p = nasm_skip_string(p);
1168 if (*p)
1169 p++;
1170 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001171 nasm_nonfatalf(ERR_PASS1, "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001172 } else {
1173 /* %! without string or identifier */
1174 type = TOK_OTHER; /* Legacy behavior... */
1175 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07001176 } else if (*p == ',') {
1177 p++;
1178 type = TOK_COND_COMMA;
H. Peter Anvin13506202018-11-28 14:55:58 -08001179 } else if (nasm_isidchar(*p) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001180 ((*p == '!' || *p == '%' || *p == '$') &&
H. Peter Anvin13506202018-11-28 14:55:58 -08001181 nasm_isidchar(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001182 do {
1183 p++;
1184 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001185 while (nasm_isidchar(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001186 type = TOK_PREPROC_ID;
1187 } else {
1188 type = TOK_OTHER;
1189 if (*p == '%')
1190 p++;
1191 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001192 } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001193 type = TOK_ID;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001194 while (nasm_isidchar(*++p))
1195 ;
1196 } else if (nasm_isquote(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001197 /*
1198 * A string token.
1199 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001200 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001201 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001202
H. Peter Anvine2c80182005-01-15 22:15:51 +00001203 if (*p) {
1204 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001205 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001206 nasm_warn(WARN_OTHER, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001207 /* Handling unterminated strings by UNV */
1208 /* type = -1; */
1209 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001210 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001211 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001212 p += 2;
H. Peter Anvin13506202018-11-28 14:55:58 -08001213 } else if (nasm_isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001214 bool is_hex = false;
1215 bool is_float = false;
1216 bool has_e = false;
1217 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001218
H. Peter Anvine2c80182005-01-15 22:15:51 +00001219 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001220 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001221 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001222
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001223 if (*p == '$') {
1224 p++;
1225 is_hex = true;
1226 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001227
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001228 for (;;) {
1229 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001230
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001231 if (!is_hex && (c == 'e' || c == 'E')) {
1232 has_e = true;
1233 if (*p == '+' || *p == '-') {
1234 /*
1235 * e can only be followed by +/- if it is either a
1236 * prefixed hex number or a floating-point number
1237 */
1238 p++;
1239 is_float = true;
1240 }
1241 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1242 is_hex = true;
1243 } else if (c == 'P' || c == 'p') {
1244 is_float = true;
1245 if (*p == '+' || *p == '-')
1246 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001247 } else if (nasm_isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001248 ; /* just advance */
1249 else if (c == '.') {
1250 /*
1251 * we need to deal with consequences of the legacy
1252 * parser, like "1.nolist" being two tokens
1253 * (TOK_NUMBER, TOK_ID) here; at least give it
1254 * a shot for now. In the future, we probably need
1255 * a flex-based scanner with proper pattern matching
1256 * to do it as well as it can be done. Nothing in
1257 * the world is going to help the person who wants
1258 * 0x123.p16 interpreted as two tokens, though.
1259 */
1260 r = p;
1261 while (*r == '_')
1262 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001263
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001264 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1265 (!is_hex && (*r == 'e' || *r == 'E')) ||
1266 (*r == 'p' || *r == 'P')) {
1267 p = r;
1268 is_float = true;
1269 } else
1270 break; /* Terminate the token */
1271 } else
1272 break;
1273 }
1274 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001275
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001276 if (p == line+1 && *line == '$') {
1277 type = TOK_OTHER; /* TOKEN_HERE */
1278 } else {
1279 if (has_e && !is_hex) {
1280 /* 1e13 is floating-point, but 1e13h is not */
1281 is_float = true;
1282 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001283
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001284 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1285 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001286 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001287 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001288 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001289 /*
1290 * Whitespace just before end-of-line is discarded by
1291 * pretending it's a comment; whitespace just before a
1292 * comment gets lumped into the comment.
1293 */
1294 if (!*p || *p == ';') {
1295 type = TOK_COMMENT;
1296 while (*p)
1297 p++;
1298 }
1299 } else if (*p == ';') {
1300 type = TOK_COMMENT;
1301 while (*p)
1302 p++;
1303 } else {
1304 /*
1305 * Anything else is an operator of some kind. We check
1306 * for all the double-character operators (>>, <<, //,
1307 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001308 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001309 */
1310 type = TOK_OTHER;
1311 if ((p[0] == '>' && p[1] == '>') ||
1312 (p[0] == '<' && p[1] == '<') ||
1313 (p[0] == '/' && p[1] == '/') ||
1314 (p[0] == '<' && p[1] == '=') ||
1315 (p[0] == '>' && p[1] == '=') ||
1316 (p[0] == '=' && p[1] == '=') ||
1317 (p[0] == '!' && p[1] == '=') ||
1318 (p[0] == '<' && p[1] == '>') ||
1319 (p[0] == '&' && p[1] == '&') ||
1320 (p[0] == '|' && p[1] == '|') ||
1321 (p[0] == '^' && p[1] == '^')) {
1322 p++;
1323 }
1324 p++;
1325 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001326
H. Peter Anvine2c80182005-01-15 22:15:51 +00001327 /* Handling unterminated string by UNV */
1328 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001329 {
1330 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1331 t->text[p-line] = *line;
1332 tail = &t->next;
1333 }
1334 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001335 if (type != TOK_COMMENT) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001336 if (!ep)
1337 ep = p;
1338 *tail = t = new_Token(NULL, type, line, ep - line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001339 tail = &t->next;
1340 }
1341 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001342 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001343 return list;
1344}
1345
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001346#if TOKEN_BLOCKSIZE
H. Peter Anvince616072002-04-30 21:02:23 +00001347/*
1348 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001349 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001350 * deleted only all at once by the delete_Blocks function.
1351 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001352static void new_Block(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001353{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001354 Blocks *b = &blocks;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001355 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001356
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001357 /* first, get to the end of the linked list */
1358 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001359 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001360 /* now allocate the requested chunk */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001361 b->chunk = nasm_malloc(TOKEN_BLOCKSIZE * sizeof(Token));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001362
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001363 /* now allocate a new block for the next request */
Cyrill Gorcunovc31767c2014-06-28 02:22:17 +04001364 b->next = nasm_zalloc(sizeof(Blocks));
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001365
1366 freeTokens = b->chunk;
1367 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1368 freeTokens[i].next = &freeTokens[i + 1];
1369
1370 freeTokens[i].next = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +00001371}
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001372#endif
H. Peter Anvince616072002-04-30 21:02:23 +00001373
1374/*
1375 * this function deletes all managed blocks of memory
1376 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001377static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001378{
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001379#if TOKEN_BLOCKSIZE
H. Peter Anvine2c80182005-01-15 22:15:51 +00001380 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001381
H. Peter Anvin70653092007-10-19 14:42:29 -07001382 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001383 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001384 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001385 * free it.
1386 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001387 while (b) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001388 if (b->chunk)
1389 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001390 a = b;
1391 b = b->next;
1392 if (a != &blocks)
1393 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001394 }
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04001395 memset(&blocks, 0, sizeof(blocks));
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001396
1397 freeTokens = NULL;
1398#endif
H. Peter Anvine2c80182005-01-15 22:15:51 +00001399}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001400
1401/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001402 * this function creates a new Token and passes a pointer to it
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001403 * back to the caller. It sets the type, text, and next pointer elements.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001404 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001405static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001406 const char *text, size_t txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001407{
1408 Token *t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001409
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001410#if TOKEN_BLOCKSIZE
1411 if (!freeTokens)
1412 new_Block();
H. Peter Anvin734b1882002-04-30 21:01:08 +00001413 t = freeTokens;
1414 freeTokens = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001415#else
1416 t = nasm_malloc(sizeof *t);
1417#endif
H. Peter Anvin734b1882002-04-30 21:01:08 +00001418 t->next = next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001419 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001420 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001421 t->len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001422 t->text = NULL;
1423 } else {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001424 if (txtlen == 0 && text[0])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001425 txtlen = strlen(text);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001426 t->len = txtlen;
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001427 t->text = nasm_malloc(txtlen+1);
1428 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001429 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001430 }
1431 return t;
1432}
1433
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001434static Token *dup_Token(Token *next, const Token *src)
1435{
1436 return new_Token(next, src->type, src->text, src->len);
1437}
1438
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001439static Token *new_White(Token *next)
1440{
1441 return new_Token(next, TOK_WHITESPACE, NULL, 0);
1442}
1443
H. Peter Anvine2c80182005-01-15 22:15:51 +00001444static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001445{
1446 Token *next = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001447
H. Peter Anvin734b1882002-04-30 21:01:08 +00001448 nasm_free(t->text);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001449#if TOKEN_BLOCKSIZE
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001450 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001451 freeTokens = t;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001452#else
1453 nasm_free(t);
1454#endif
H. Peter Anvin734b1882002-04-30 21:01:08 +00001455 return next;
1456}
1457
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001458/*
1459 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001460 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1461 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001462 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001463static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001464{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001465 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001466 char *line, *p;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001467 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001468
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001469 list_for_each(t, tlist) {
Cyrill Gorcunov9b7ee092017-10-22 21:42:59 +03001470 if (t->type == TOK_PREPROC_ID && t->text &&
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001471 t->text[0] == '%' && t->text[1] == '!') {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001472 char *v;
1473 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001474
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001475 v = t->text + 2;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001476 if (nasm_isquote(*v))
1477 nasm_unquote_cstr(v, NULL);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001478
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001479 if (v) {
1480 char *p = getenv(v);
1481 if (!p) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001482 /*!
1483 *!environment [on] nonexistent environment variable
1484 *! warns if a nonexistent environment variable
1485 *! is accessed using the \c{%!} preprocessor
1486 *! construct (see \k{getenv}.) Such environment
1487 *! variables are treated as empty (with this
1488 *! warning issued) starting in NASM 2.15;
1489 *! earlier versions of NASM would treat this as
1490 *! an error.
Cyrill Gorcunovbbb7a1a2016-06-19 12:15:24 +03001491 */
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001492 nasm_warn(WARN_ENVIRONMENT, "nonexistent environment variable `%s'", v);
1493 p = "";
1494 }
1495 t->text = nasm_strdup(p);
1496 t->len = nasm_last_string_len();
Cyrill Gorcunov75004872017-07-26 01:21:16 +03001497 nasm_free(q);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001498 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001499 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001500
H. Peter Anvine2c80182005-01-15 22:15:51 +00001501 /* Expand local macros here and not during preprocessing */
1502 if (expand_locals &&
1503 t->type == TOK_PREPROC_ID && t->text &&
1504 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001505 const char *q;
1506 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001507 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001508 if (ctx) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001509 p = nasm_asprintf("..@%"PRIu64".%s", ctx->number, q);
1510 t->len = nasm_last_string_len();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001511 nasm_free(t->text);
1512 t->text = p;
1513 }
1514 }
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001515 if (t->text) {
1516 if (debug_level(2)) {
1517 unsigned long t_len = t->len;
1518 unsigned long s_len = strlen(t->text);
1519 if (t_len != s_len) {
1520 nasm_panic("assertion failed: token \"%s\" type %u len %lu has t->len %lu\n",
1521 t->text, t->type, s_len, t_len);
1522 t->len = s_len;
1523 }
1524 }
1525 len += t->len;
1526 } else if (t->type == TOK_WHITESPACE) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001527 len++;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001528 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001529 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001530
H. Peter Anvin734b1882002-04-30 21:01:08 +00001531 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001532
1533 list_for_each(t, tlist) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001534 if (t->text) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001535 memcpy(p, t->text, t->len);
1536 p += t->len;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001537 } else if (t->type == TOK_WHITESPACE) {
1538 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001539 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001540 }
1541 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001542
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001543 return line;
1544}
1545
1546/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001547 * A scanner, suitable for use by the expression evaluator, which
1548 * operates on a line of Tokens. Expects a pointer to a pointer to
1549 * the first token in the line to be passed in as its private_data
1550 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001551 *
1552 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001553 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08001554struct ppscan {
1555 Token *tptr;
1556 int ntokens;
1557};
1558
H. Peter Anvine2c80182005-01-15 22:15:51 +00001559static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001560{
H. Peter Anvin8b262472019-02-26 14:00:54 -08001561 struct ppscan *pps = private_data;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001562 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001563 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001564
H. Peter Anvine2c80182005-01-15 22:15:51 +00001565 do {
H. Peter Anvin8b262472019-02-26 14:00:54 -08001566 if (pps->ntokens && (tline = pps->tptr)) {
1567 pps->ntokens--;
1568 pps->tptr = tline->next;
1569 } else {
1570 pps->tptr = NULL;
1571 pps->ntokens = 0;
1572 return tokval->t_type = TOKEN_EOS;
1573 }
1574 } while (tline->type == TOK_WHITESPACE || tline->type == TOK_COMMENT);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001575
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001576 tokval->t_charptr = tline->text;
1577
H. Peter Anvin76690a12002-04-30 20:52:49 +00001578 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001579 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001580 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001581 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001582
H. Peter Anvine2c80182005-01-15 22:15:51 +00001583 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001584 p = tokval->t_charptr = tline->text;
1585 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001586 tokval->t_charptr++;
1587 return tokval->t_type = TOKEN_ID;
1588 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001589
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001590 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001591 if (r >= p+MAX_KEYWORD)
1592 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001593 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001594 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001595 *s = '\0';
1596 /* right, so we have an identifier sitting in temp storage. now,
1597 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001598 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001599 }
1600
H. Peter Anvine2c80182005-01-15 22:15:51 +00001601 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001602 bool rn_error;
1603 tokval->t_integer = readnum(tline->text, &rn_error);
1604 tokval->t_charptr = tline->text;
1605 if (rn_error)
1606 return tokval->t_type = TOKEN_ERRNUM;
1607 else
1608 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001609 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001610
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001611 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001612 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001613 }
1614
H. Peter Anvine2c80182005-01-15 22:15:51 +00001615 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001616 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001617
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001618 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001619 tokval->t_charptr = tline->text;
1620 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001621
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001622 if (ep[0] != bq || ep[1] != '\0')
1623 return tokval->t_type = TOKEN_ERRSTR;
1624 else
1625 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001626 }
1627
H. Peter Anvine2c80182005-01-15 22:15:51 +00001628 if (tline->type == TOK_OTHER) {
1629 if (!strcmp(tline->text, "<<"))
1630 return tokval->t_type = TOKEN_SHL;
1631 if (!strcmp(tline->text, ">>"))
1632 return tokval->t_type = TOKEN_SHR;
1633 if (!strcmp(tline->text, "//"))
1634 return tokval->t_type = TOKEN_SDIV;
1635 if (!strcmp(tline->text, "%%"))
1636 return tokval->t_type = TOKEN_SMOD;
1637 if (!strcmp(tline->text, "=="))
1638 return tokval->t_type = TOKEN_EQ;
1639 if (!strcmp(tline->text, "<>"))
1640 return tokval->t_type = TOKEN_NE;
1641 if (!strcmp(tline->text, "!="))
1642 return tokval->t_type = TOKEN_NE;
1643 if (!strcmp(tline->text, "<="))
1644 return tokval->t_type = TOKEN_LE;
1645 if (!strcmp(tline->text, ">="))
1646 return tokval->t_type = TOKEN_GE;
1647 if (!strcmp(tline->text, "&&"))
1648 return tokval->t_type = TOKEN_DBL_AND;
1649 if (!strcmp(tline->text, "^^"))
1650 return tokval->t_type = TOKEN_DBL_XOR;
1651 if (!strcmp(tline->text, "||"))
1652 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001653 }
1654
1655 /*
1656 * We have no other options: just return the first character of
1657 * the token text.
1658 */
1659 return tokval->t_type = tline->text[0];
1660}
1661
1662/*
H. Peter Anvind2354082019-08-27 16:38:48 -07001663 * 1. An expression (true if nonzero 0)
1664 * 2. The keywords true, on, yes for true
1665 * 3. The keywords false, off, no for false
1666 * 4. An empty line, for true
1667 *
1668 * On error, return defval (usually the previous value)
1669 */
1670static bool pp_get_boolean_option(Token *tline, bool defval)
1671{
1672 static const char * const noyes[] = {
1673 "no", "yes",
1674 "false", "true",
1675 "off", "on"
1676 };
1677 struct ppscan pps;
1678 struct tokenval tokval;
1679 expr *evalresult;
1680
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001681 tline = skip_white(tline);
1682 if (!tline)
H. Peter Anvind2354082019-08-27 16:38:48 -07001683 return true;
1684
1685 if (tline->type == TOK_ID) {
1686 size_t i;
1687 for (i = 0; i < ARRAY_SIZE(noyes); i++)
1688 if (!nasm_stricmp(tline->text, noyes[i]))
1689 return i & 1;
1690 }
1691
1692 pps.tptr = NULL;
1693 pps.tptr = tline;
1694 pps.ntokens = -1;
1695 tokval.t_type = TOKEN_INVALID;
1696 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
1697
1698 if (!evalresult)
1699 return true;
1700
1701 if (tokval.t_type)
1702 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
1703 if (!is_really_simple(evalresult)) {
1704 nasm_nonfatal("boolean flag expression must be a constant");
1705 return defval;
1706 }
1707
1708 return reloc_value(evalresult) != 0;
1709}
1710
1711/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001712 * Compare a string to the name of an existing macro; this is a
1713 * simple wrapper which calls either strcmp or nasm_stricmp
1714 * depending on the value of the `casesense' parameter.
1715 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001716static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001717{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001718 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001719}
1720
1721/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001722 * Compare a string to the name of an existing macro; this is a
1723 * simple wrapper which calls either strcmp or nasm_stricmp
1724 * depending on the value of the `casesense' parameter.
1725 */
1726static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1727{
1728 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1729}
1730
1731/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001732 * Return the Context structure associated with a %$ token. Return
1733 * NULL, having _already_ reported an error condition, if the
1734 * context stack isn't deep enough for the supplied number of $
1735 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001736 *
1737 * If "namep" is non-NULL, set it to the pointer to the macro name
1738 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001739 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001740static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001741{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001742 Context *ctx;
1743 int i;
1744
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001745 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001746 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001747
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001748 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001749 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001750
H. Peter Anvine2c80182005-01-15 22:15:51 +00001751 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001752 nasm_nonfatal("`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001753 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001754 }
1755
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001756 name += 2;
1757 ctx = cstk;
1758 i = 0;
1759 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001760 name++;
1761 i++;
1762 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001763 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001764 if (!ctx) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001765 nasm_nonfatal("`%s': context stack is only"
1766 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001767 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001768 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001769
1770 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001771 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001772
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001773 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001774}
1775
1776/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001777 * Open an include file. This routine must always return a valid
1778 * file pointer if it returns - it's responsible for throwing an
1779 * ERR_FATAL and bombing out completely if not. It should also try
1780 * the include path one by one until it finds the file or reaches
1781 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07001782 *
1783 * Note: for INC_PROBE the function returns NULL at all times;
1784 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001785 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07001786enum incopen_mode {
1787 INC_NEEDED, /* File must exist */
1788 INC_OPTIONAL, /* Missing is OK */
1789 INC_PROBE /* Only an existence probe */
1790};
1791
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001792/* This is conducts a full pathname search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001793static FILE *inc_fopen_search(const char *file, char **slpath,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001794 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001795{
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -08001796 const struct strlist_entry *ip = strlist_head(ipath_list);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001797 FILE *fp;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001798 const char *prefix = "";
night199ukfdb1a1b2018-10-18 23:19:47 +02001799 char *sp;
H. Peter Anvind81a2352016-09-21 14:03:18 -07001800 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001801
H. Peter Anvine2c80182005-01-15 22:15:51 +00001802 while (1) {
night199ukfdb1a1b2018-10-18 23:19:47 +02001803 sp = nasm_catfile(prefix, file);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001804 if (omode == INC_PROBE) {
1805 fp = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001806 found = nasm_file_exists(sp);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001807 } else {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001808 fp = nasm_open_read(sp, fmode);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001809 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001810 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001811 if (found) {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001812 *slpath = sp;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001813 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001814 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001815
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001816 nasm_free(sp);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001817
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001818 if (!ip) {
1819 *slpath = NULL;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001820 return NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001821 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001822
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001823 prefix = ip->str;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001824 ip = ip->next;
1825 }
1826}
1827
1828/*
1829 * Open a file, or test for the presence of one (depending on omode),
1830 * considering the include path.
1831 */
1832static FILE *inc_fopen(const char *file,
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001833 struct strlist *dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001834 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001835 enum incopen_mode omode,
1836 enum file_flags fmode)
1837{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001838 struct hash_insert hi;
1839 void **hp;
1840 char *path;
1841 FILE *fp = NULL;
1842
1843 hp = hash_find(&FileHash, file, &hi);
1844 if (hp) {
1845 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001846 if (path || omode != INC_NEEDED) {
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001847 strlist_add(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001848 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001849 } else {
1850 /* Need to do the actual path search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001851 fp = inc_fopen_search(file, &path, omode, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001852
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001853 /* Positive or negative result */
1854 hash_add(&hi, nasm_strdup(file), path);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001855
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001856 /*
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001857 * Add file to dependency path.
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001858 */
1859 if (path || omode != INC_NEEDED)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001860 strlist_add(dhead, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001861 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001862
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001863 if (!path) {
1864 if (omode == INC_NEEDED)
H. Peter Anvinc5136902018-06-15 18:20:17 -07001865 nasm_fatal("unable to open include file `%s'", file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001866 } else {
1867 if (!fp && omode != INC_PROBE)
1868 fp = nasm_open_read(path, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001869 }
1870
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001871 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001872 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001873
1874 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001875}
1876
1877/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001878 * Opens an include or input file. Public version, for use by modules
1879 * that get a file:lineno pair and need to look at the file again
1880 * (e.g. the CodeView debug backend). Returns NULL on failure.
1881 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001882FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001883{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001884 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001885}
1886
1887/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001888 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001889 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001890 * return true if _any_ single-line macro of that name is defined.
1891 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001892 * `nparam' or no parameters is defined.
1893 *
1894 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001895 * defined, or nparam is -1, the address of the definition structure
1896 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001897 * is NULL, no action will be taken regarding its contents, and no
1898 * error will occur.
1899 *
1900 * Note that this is also called with nparam zero to resolve
1901 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001902 *
1903 * If you already know which context macro belongs to, you can pass
1904 * the context pointer as first parameter; if you won't but name begins
1905 * with %$ the context will be automatically computed. If all_contexts
1906 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001907 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001908static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001909smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvind2354082019-08-27 16:38:48 -07001910 bool nocase, bool find_alias)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001911{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001912 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001913 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001914
H. Peter Anvin97a23472007-09-16 17:57:25 -07001915 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001916 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001917 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001918 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001919 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001920 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001921 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001922 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001923 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001924 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001925 }
H. Peter Anvind2354082019-08-27 16:38:48 -07001926
1927restart:
H. Peter Anvin166c2472008-05-28 12:28:58 -07001928 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001929
H. Peter Anvine2c80182005-01-15 22:15:51 +00001930 while (m) {
1931 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07001932 (nparam <= 0 || m->nparam == 0 || nparam == m->nparam ||
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07001933 (m->greedy && nparam >= m->nparam-1))) {
H. Peter Anvind2354082019-08-27 16:38:48 -07001934 if (m->alias && !find_alias) {
1935 if (do_aliases) {
H. Peter Anvind626f352019-09-12 18:31:29 -07001936 name = m->expansion->text;
H. Peter Anvind2354082019-08-27 16:38:48 -07001937 goto restart;
1938 } else {
1939 continue;
1940 }
1941 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001942 if (defn) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07001943 *defn = (nparam == m->nparam || nparam == -1) ? m : NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001944 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001945 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001946 }
1947 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001948 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001949
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001950 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001951}
1952
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001953/* param should be a natural number [0; INT_MAX] */
1954static int read_param_count(const char *str)
1955{
1956 int result;
1957 bool err;
1958
1959 result = readnum(str, &err);
1960 if (result < 0 || result > INT_MAX) {
1961 result = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001962 nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]",
1963 str, 0, INT_MAX);
1964 } else if (err)
1965 nasm_nonfatal("unable to parse parameter count `%s'", str);
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001966 return result;
1967}
1968
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001969/*
1970 * Count and mark off the parameters in a multi-line macro call.
1971 * This is called both from within the multi-line macro expansion
1972 * code, and also to mark off the default parameters when provided
1973 * in a %macro definition line.
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001974 *
1975 * Note that we need space in the params array for parameter 0 being
1976 * a possible captured label as well as the final NULL.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001977 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001978static void count_mmac_params(Token * t, int *nparamp, Token ***paramsp)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001979{
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07001980 int paramsize;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001981 int nparam = 0;
1982 Token **params;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001983
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001984 paramsize = PARAM_DELTA;
1985 params = nasm_malloc(paramsize * sizeof(*params));
1986 params[0] = NULL;
1987
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001988 while ((t = skip_white(t))) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001989 /* 2 slots for captured label and NULL */
1990 if (nparam+2 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001991 paramsize += PARAM_DELTA;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001992 params = nasm_realloc(params, sizeof(*params) * paramsize);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001993 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001994 params[++nparam] = t;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07001995 if (tok_is(t, '{')) {
1996 int brace = 1;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001997 while (brace && (t = t->next)) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07001998 brace += tok_is(t, '{');
1999 brace -= tok_is(t, '}');
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08002000 }
2001
2002 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002003 /*
2004 * Now we've found the closing brace, look further
2005 * for the comma.
2006 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002007 t = skip_white(t->next);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07002008 if (tok_isnt(t, ','))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002009 nasm_nonfatal("braces do not enclose all of macro parameter");
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07002010 } else {
2011 nasm_nonfatal("expecting closing brace in macro parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002012 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08002013 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002014
2015 while (tok_isnt(t, ','))
2016 t = t->next;
2017
2018 if (t) /* got a comma */
2019 t = t->next; /* eat the comma */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002020 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002021
2022 params[nparam+1] = NULL;
2023 *paramsp = params;
2024 *nparamp = nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002025}
2026
2027/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002028 * Determine whether one of the various `if' conditions is true or
2029 * not.
2030 *
2031 * We must free the tline we get passed.
2032 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002033static enum cond_state if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002034{
H. Peter Anvin70055962007-10-11 00:05:31 -07002035 bool j;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002036 Token *t, *tt, *origline;
2037 struct ppscan pps;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002038 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002039 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002040 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07002041 char *p;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002042 const char *dname = pp_directives[ct];
2043 bool casesense = true;
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002044 enum preproc_token cond = PP_COND(ct);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002045
2046 origline = tline;
2047
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002048 switch (cond) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002049 case PP_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002050 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02002051 while (true) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002052 tline = skip_white(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02002053 if (!tline)
2054 break;
2055 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002056 nasm_nonfatal("`%s' expects context identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002057 dname);
2058 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002059 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02002060 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002061 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002062 tline = tline->next;
2063 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002064 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002065
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002066 case PP_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002067 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002068 while (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002069 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002070 if (!tline || (tline->type != TOK_ID &&
2071 (tline->type != TOK_PREPROC_ID ||
2072 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002073 nasm_nonfatal("`%s' expects macro identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002074 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002075 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002076 }
H. Peter Anvind2354082019-08-27 16:38:48 -07002077 if (smacro_defined(NULL, tline->text, 0, NULL, true, false))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002078 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002079 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002080 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002081 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002082
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002083 case PP_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002084 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002085 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002086 while (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002087 tline = skip_white(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002088 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002089 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002090 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002091 tline->text[1] != '!'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002092 nasm_nonfatal("`%s' expects environment variable names",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002093 dname);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002094 goto fail;
2095 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002096 p = tline->text;
2097 if (tline->type == TOK_PREPROC_ID)
2098 p += 2; /* Skip leading %! */
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08002099 if (nasm_isquote(*p))
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002100 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002101 if (getenv(p))
2102 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002103 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002104 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002105 break;
2106
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002107 case PP_IFIDNI:
2108 casesense = false;
2109 /* fall through */
2110 case PP_IFIDN:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002111 tline = expand_smacro(tline);
2112 t = tt = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002113 while (tok_isnt(tt, ','))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002114 tt = tt->next;
2115 if (!tt) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002116 nasm_nonfatal("`%s' expects two comma-separated arguments",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002117 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002118 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002119 }
2120 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002121 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002122 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
2123 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002124 nasm_nonfatal("`%s': more than one comma on line",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002125 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002126 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002127 }
2128 if (t->type == TOK_WHITESPACE) {
2129 t = t->next;
2130 continue;
2131 }
2132 if (tt->type == TOK_WHITESPACE) {
2133 tt = tt->next;
2134 continue;
2135 }
2136 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002137 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002138 break;
2139 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07002140 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002141 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002142 size_t l1 = nasm_unquote(t->text, NULL);
2143 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07002144
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002145 if (l1 != l2) {
2146 j = false;
2147 break;
2148 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002149 if (mmemcmp(t->text, tt->text, l1, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002150 j = false;
2151 break;
2152 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002153 } else if (mstrcmp(tt->text, t->text, casesense) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002154 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002155 break;
2156 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00002157
H. Peter Anvine2c80182005-01-15 22:15:51 +00002158 t = t->next;
2159 tt = tt->next;
2160 }
2161 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002162 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002163 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002164
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002165 case PP_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04002166 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002167 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002168 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00002169
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002170 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002171 tline = expand_id(tline);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002172 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002173 nasm_nonfatal("`%s' expects a macro name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002174 goto fail;
2175 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002176 nasm_zero(searching);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002177 searching.name = nasm_strdup(tline->text);
2178 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002179 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002180 searching.nparam_max = INT_MAX;
2181 tline = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002182 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002183 if (!tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002184 } else if (!tok_type(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002185 nasm_nonfatal("`%s' expects a parameter count or nothing",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002186 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002187 } else {
2188 searching.nparam_min = searching.nparam_max =
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002189 read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002190 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002191 if (tline && tok_is(tline->next, '-')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002192 tline = tline->next->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002193 if (tok_is(tline, '*'))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002194 searching.nparam_max = INT_MAX;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002195 else if (!tok_type(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002196 nasm_nonfatal("`%s' expects a parameter count after `-'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002197 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002198 else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002199 searching.nparam_max = read_param_count(tline->text);
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002200 if (searching.nparam_min > searching.nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002201 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002202 searching.nparam_max = searching.nparam_min;
2203 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002204 }
2205 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002206 if (tline && tok_is(tline->next, '+')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002207 tline = tline->next;
2208 searching.plus = true;
2209 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002210 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
2211 while (mmac) {
2212 if (!strcmp(mmac->name, searching.name) &&
2213 (mmac->nparam_min <= searching.nparam_max
2214 || searching.plus)
2215 && (searching.nparam_min <= mmac->nparam_max
2216 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002217 found = true;
2218 break;
2219 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002220 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002221 }
2222 if (tline && tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002223 nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002224 nasm_free(searching.name);
2225 j = found;
2226 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002227 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002228
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002229 case PP_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002230 needtype = TOK_ID;
2231 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002232 case PP_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002233 needtype = TOK_NUMBER;
2234 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002235 case PP_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002236 needtype = TOK_STRING;
2237 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002238
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002239iftype:
2240 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002241
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002242 while (tok_white(t) ||
2243 (needtype == TOK_NUMBER && (tok_is(t, '-') | tok_is(t, '+'))))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002244 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002245
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002246 j = tok_type(t, needtype);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002247 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002248
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002249 case PP_IFTOKEN:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002250 tline = expand_smacro(tline);
2251 t = skip_white(tline);
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002252
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002253 j = false;
2254 if (t) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002255 t = skip_white(t->next); /* Skip the actual token + whitespace */
2256 j = !t;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002257 }
2258 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002259
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002260 case PP_IFEMPTY:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002261 tline = expand_smacro(tline);
2262 t = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002263 j = !t; /* Should be empty */
2264 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002265
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002266 case PP_IF:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002267 pps.tptr = tline = expand_smacro(tline);
2268 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002269 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002270 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002271 if (!evalresult)
2272 return -1;
2273 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002274 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002275 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002276 nasm_nonfatal("non-constant value given to `%s'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002277 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002278 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002279 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002280 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002281 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002282
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002283 case PP_IFUSING:
2284 case PP_IFUSABLE:
2285 {
2286 const struct use_package *pkg;
2287 bool err;
2288
2289 pkg = get_use_pkg(tline, dname, &err);
2290 if (err)
2291 goto fail;
2292
2293 j = pkg && ((cond == PP_IFUSABLE) | use_loaded[pkg->index]);
2294 break;
2295 }
2296
H. Peter Anvine2c80182005-01-15 22:15:51 +00002297 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002298 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002299 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002300 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002301
2302 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002303 return (j ^ PP_COND_NEGATIVE(ct)) ? COND_IF_TRUE : COND_IF_FALSE;
H. Peter Anvin70653092007-10-19 14:42:29 -07002304
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002305fail:
2306 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002307 return COND_NEVER;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002308}
2309
2310/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002311 * Default smacro expansion routine: just returns a copy of the
2312 * expansion list.
2313 */
2314static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002315smacro_expand_default(const SMacro *s, Token **params, int nparams)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002316{
2317 (void)params;
2318 (void)nparams;
2319
2320 return dup_tlist(s->expansion, NULL);
2321}
2322
2323/*
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002324 * Emit a macro defintion or undef to the listing file, if
2325 * desired. This is similar to detoken(), but it handles the reverse
2326 * expansion list, does not expand %! or local variable tokens, and
2327 * does some special handling for macro parameters.
2328 */
2329static void
2330list_smacro_def(enum preproc_token op, const Context *ctx, const SMacro *m)
2331{
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002332 Token *t;
2333 size_t namelen, size;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002334 char *def, *p;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002335 char *context_prefix = NULL;
2336 size_t context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002337
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002338 namelen = strlen(m->name);
2339 size = namelen + 2; /* Include room for space after name + NUL */
2340
2341 if (ctx) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07002342 int context_depth = cstk->depth - ctx->depth + 1;
2343 context_prefix =
2344 nasm_asprintf("[%s::%"PRIu64"] %%%-*s",
2345 ctx->name ? ctx->name : "",
2346 ctx->number, context_depth, "");
2347
2348 context_len = nasm_last_string_len();
2349 memset(context_prefix + context_len - context_depth,
2350 '$', context_depth);
2351 size += context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002352 }
2353
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002354 list_for_each(t, m->expansion)
2355 size += t->text ? t->len : 1;
2356
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002357 if (m->nparam) {
2358 /*
2359 * Space for ( and either , or ) around each
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002360 * parameter, plus up to 4 flags.
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002361 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002362 int i;
2363
2364 size += 1 + 4 * m->nparam;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002365 for (i = 0; i < m->nparam; i++)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002366 size += m->params[i].namelen;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002367 }
2368
2369 def = nasm_malloc(size);
2370 p = def+size;
2371 *--p = '\0';
2372
2373 list_for_each(t, m->expansion) {
2374 if (!t->text) {
2375 *--p = ' ';
2376 } else {
2377 p -= t->len;
2378 memcpy(p, t->text, t->len);
2379 }
2380 }
2381
2382 *--p = ' ';
2383
2384 if (m->nparam) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002385 int i;
2386
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002387 *--p = ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002388 for (i = m->nparam-1; i >= 0; i--) {
2389 enum sparmflags flags = m->params[i].flags;
2390 if (flags & SPARM_GREEDY)
2391 *--p = '+';
2392 if (m->params[i].name) {
2393 p -= m->params[i].namelen;
2394 memcpy(p, m->params[i].name, m->params[i].namelen);
2395 }
2396 if (flags & SPARM_NOSTRIP)
2397 *--p = '!';
2398 if (flags & SPARM_STR)
2399 *--p = '&';
2400 if (flags & SPARM_EVAL)
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002401 *--p = '=';
2402 *--p = ',';
2403 }
2404 *p = '('; /* First parameter starts with ( not , */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002405 }
2406
2407 p -= namelen;
2408 memcpy(p, m->name, namelen);
2409
H. Peter Anvin6686de22019-08-10 05:33:14 -07002410 if (context_prefix) {
2411 p -= context_len;
2412 memcpy(p, context_prefix, context_len);
2413 nasm_free(context_prefix);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002414 }
2415
2416 nasm_listmsg("%s %s", pp_directives[op], p);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002417 nasm_free(def);
H. Peter Anvin6686de22019-08-10 05:33:14 -07002418}
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002419
2420/*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002421 * Parse smacro arguments, return argument count. If the tmpl argument
2422 * is set, set the nparam, greedy and params field in the template.
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002423 * *tpp is updated to point to the pointer to the first token after the
2424 * prototype.
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002425 *
2426 * The text values from any argument tokens are "stolen" and the
2427 * corresponding text fields set to NULL.
2428 */
2429static int parse_smacro_template(Token ***tpp, SMacro *tmpl)
2430{
2431 int nparam = 0;
2432 enum sparmflags flags;
2433 struct smac_param *params = NULL;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002434 bool err, done;
2435 bool greedy = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002436 Token **tn = *tpp;
2437 Token *t = *tn;
2438 Token *name;
2439
H. Peter Anvin (Intel)d4607842019-08-20 16:19:37 -07002440 /*
2441 * DO NOT skip whitespace here, or we won't be able to distinguish:
2442 *
2443 * %define foo (a,b) ; no arguments, (a,b) is the expansion
2444 * %define bar(a,b) ; two arguments, empty expansion
2445 *
2446 * This ambiguity was inherited from C.
2447 */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002448
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002449 if (!tok_is(t, '('))
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002450 goto finish;
2451
2452 if (tmpl) {
2453 Token *tx = t;
2454 Token **txpp = &tx;
2455 int sparam;
2456
2457 /* Count parameters first */
2458 sparam = parse_smacro_template(&txpp, NULL);
2459 if (!sparam)
2460 goto finish; /* No parameters, we're done */
2461 nasm_newn(params, sparam);
2462 }
2463
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002464 /* Skip leading paren */
2465 tn = &t->next;
2466 t = *tn;
2467
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002468 name = NULL;
2469 flags = 0;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002470 err = done = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002471
2472 while (!done) {
2473 if (!t || !t->type) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002474 if (name || flags)
2475 nasm_nonfatal("`)' expected to terminate macro template");
2476 else
2477 nasm_nonfatal("parameter identifier expected");
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002478 break;
2479 }
2480
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002481 switch (t->type) {
2482 case TOK_ID:
2483 if (name)
2484 goto bad;
2485 name = t;
2486 break;
2487
2488 case TOK_OTHER:
2489 if (t->text[1])
2490 goto bad;
2491 switch (t->text[0]) {
2492 case '=':
2493 flags |= SPARM_EVAL;
2494 break;
2495 case '&':
2496 flags |= SPARM_STR;
2497 break;
2498 case '!':
2499 flags |= SPARM_NOSTRIP;
2500 break;
2501 case '+':
2502 flags |= SPARM_GREEDY;
2503 greedy = true;
2504 break;
2505 case ',':
2506 if (greedy)
2507 nasm_nonfatal("greedy parameter must be last");
2508 /* fall through */
2509 case ')':
2510 if (params) {
2511 if (name) {
2512 params[nparam].name = name->text;
2513 params[nparam].namelen = name->len;
2514 name->text = NULL;
2515 }
2516 params[nparam].flags = flags;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002517 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002518 nparam++;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002519 name = NULL;
2520 flags = 0;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002521 done = t->text[0] == ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002522 break;
2523 default:
2524 goto bad;
2525 }
2526 break;
2527
2528 case TOK_WHITESPACE:
2529 break;
2530
2531 default:
2532 bad:
2533 if (!err) {
2534 nasm_nonfatal("garbage `%s' in macro parameter list", t->text);
2535 err = true;
2536 }
2537 break;
2538 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002539
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002540 tn = &t->next;
2541 t = *tn;
2542 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002543
2544finish:
2545 while (t && t->type == TOK_WHITESPACE) {
2546 tn = &t->next;
2547 t = t->next;
2548 }
2549 *tpp = tn;
2550 if (tmpl) {
2551 tmpl->nparam = nparam;
2552 tmpl->greedy = greedy;
2553 tmpl->params = params;
2554 }
2555 return nparam;
2556}
2557
2558/*
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002559 * Common code for defining an smacro. The tmpl argument, if not NULL,
2560 * contains any macro parameters that aren't explicit arguments;
2561 * those are the more uncommon macro variants.
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002562 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002563static SMacro *define_smacro(const char *mname, bool casesense,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002564 Token *expansion, SMacro *tmpl)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002565{
2566 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002567 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002568 Context *ctx;
2569 bool defining_alias = false;
2570 unsigned int nparam = 0;
H. Peter Anvin70653092007-10-19 14:42:29 -07002571
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002572 if (tmpl) {
2573 defining_alias = tmpl->alias;
2574 nparam = tmpl->nparam;
2575 }
2576
2577 while (1) {
2578 ctx = get_ctx(mname, &mname);
2579
H. Peter Anvind2354082019-08-27 16:38:48 -07002580 if (!smacro_defined(ctx, mname, nparam, &smac, casesense, true)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002581 /* Create a new macro */
2582 smtbl = ctx ? &ctx->localmac : &smacros;
2583 smhead = (SMacro **) hash_findi_add(smtbl, mname);
2584 nasm_new(smac);
2585 smac->next = *smhead;
2586 *smhead = smac;
2587 break;
2588 } else if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002589 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002590 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002591 /*
2592 * Some instances of the old code considered this a failure,
2593 * some others didn't. What is the right thing to do here?
2594 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002595 goto fail;
H. Peter Anvind2354082019-08-27 16:38:48 -07002596 } else if (!smac->alias || !do_aliases || defining_alias) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002597 /*
2598 * We're redefining, so we have to take over an
2599 * existing SMacro structure. This means freeing
H. Peter Anvin8b262472019-02-26 14:00:54 -08002600 * what was already in it, but not the structure itself.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002601 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002602 clear_smacro(smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002603 break;
2604 } else if (smac->in_progress) {
2605 nasm_nonfatal("macro alias loop");
2606 goto fail;
2607 } else {
2608 /* It is an alias macro; follow the alias link */
2609 SMacro *s;
2610
2611 smac->in_progress = true;
2612 s = define_smacro(smac->expansion->text, casesense,
2613 expansion, tmpl);
2614 smac->in_progress = false;
2615 return s;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002616 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002617 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002618
2619 smac->name = nasm_strdup(mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002620 smac->casesense = casesense;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002621 smac->expansion = expansion;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002622 smac->expand = smacro_expand_default;
2623 if (tmpl) {
2624 smac->nparam = tmpl->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002625 smac->params = tmpl->params;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002626 smac->alias = tmpl->alias;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002627 smac->greedy = tmpl->greedy;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002628 if (tmpl->expand)
2629 smac->expand = tmpl->expand;
2630 }
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07002631 if (list_option('s')) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002632 list_smacro_def((smac->alias ? PP_DEFALIAS : PP_DEFINE)
2633 + !casesense, ctx, smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002634 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08002635 return smac;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002636
2637fail:
2638 free_tlist(expansion);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002639 if (tmpl)
2640 free_smacro_members(tmpl);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002641 return NULL;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002642}
2643
2644/*
2645 * Undefine an smacro
2646 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002647static void undef_smacro(const char *mname, bool undefalias)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002648{
2649 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002650 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002651 Context *ctx;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002652
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002653 ctx = get_ctx(mname, &mname);
H. Peter Anvin166c2472008-05-28 12:28:58 -07002654 smtbl = ctx ? &ctx->localmac : &smacros;
2655 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002656
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002657 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002658 /*
2659 * We now have a macro name... go hunt for it.
2660 */
2661 sp = smhead;
2662 while ((s = *sp) != NULL) {
2663 if (!mstrcmp(s->name, mname, s->casesense)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002664 if (s->alias && !undefalias) {
H. Peter Anvind2354082019-08-27 16:38:48 -07002665 if (!do_aliases)
2666 continue;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002667 if (s->in_progress) {
2668 nasm_nonfatal("macro alias loop");
2669 } else {
2670 s->in_progress = true;
2671 undef_smacro(s->expansion->text, false);
2672 s->in_progress = false;
2673 }
2674 } else {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07002675 if (list_option('d'))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002676 list_smacro_def(s->alias ? PP_UNDEFALIAS : PP_UNDEF,
2677 ctx, s);
2678 *sp = s->next;
2679 free_smacro(s);
2680 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002681 } else {
2682 sp = &s->next;
2683 }
2684 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002685 }
2686}
2687
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002688/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002689 * Parse a mmacro specification.
2690 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002691static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002692{
H. Peter Anvina26433d2008-07-16 14:40:01 -07002693 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002694 tline = skip_white(tline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002695 tline = expand_id(tline);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002696 if (!tok_type(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002697 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002698 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002699 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002700
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002701#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002702 def->prev = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002703#endif
H. Peter Anvina26433d2008-07-16 14:40:01 -07002704 def->name = nasm_strdup(tline->text);
2705 def->plus = false;
2706 def->nolist = false;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002707 def->nparam_min = 0;
2708 def->nparam_max = 0;
2709
H. Peter Anvina26433d2008-07-16 14:40:01 -07002710 tline = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002711 tline = skip_white(tline);
2712 if (!tok_type(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002713 nasm_nonfatal("`%s' expects a parameter count", directive);
2714 else
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002715 def->nparam_min = def->nparam_max = read_param_count(tline->text);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002716 if (tline && tok_is(tline->next, '-')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002717 tline = tline->next->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002718 if (tok_is(tline, '*')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002719 def->nparam_max = INT_MAX;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002720 } else if (!tok_type(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002721 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002722 } else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002723 def->nparam_max = read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002724 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002725 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002726 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002727 }
2728 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002729 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002730 if (tline && tok_is(tline->next, '+')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002731 tline = tline->next;
2732 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002733 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002734 if (tline && tok_type(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002735 !nasm_stricmp(tline->next->text, ".nolist")) {
2736 tline = tline->next;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002737 def->nolist = !list_option('f') || istk->nolist;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002738 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002739
H. Peter Anvina26433d2008-07-16 14:40:01 -07002740 /*
2741 * Handle default parameters.
2742 */
2743 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002744 def->dlist = tline->next;
2745 tline->next = NULL;
2746 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002747 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002748 def->dlist = NULL;
2749 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002750 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002751 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002752
H. Peter Anvin89cee572009-07-15 09:16:54 -04002753 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002754 !def->plus) {
2755 /*
2756 *!macro-defaults [on] macros with more default than optional parameters
2757 *! warns when a macro has more default parameters than optional parameters.
2758 *! See \k{mlmacdef} for why might want to disable this warning.
2759 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002760 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002761 "too many default macro parameters in macro `%s'", def->name);
2762 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002763
H. Peter Anvina26433d2008-07-16 14:40:01 -07002764 return true;
2765}
2766
2767
2768/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002769 * Decode a size directive
2770 */
2771static int parse_size(const char *str) {
2772 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002773 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002774 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002775 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03002776 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002777}
2778
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002779/*
2780 * Process a preprocessor %pragma directive. Currently there are none.
2781 * Gets passed the token list starting with the "preproc" token from
2782 * "%pragma preproc".
2783 */
2784static void do_pragma_preproc(Token *tline)
2785{
2786 /* Skip to the real stuff */
2787 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002788 tline = skip_white(tline);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002789 if (!tline)
2790 return;
2791
2792 (void)tline; /* Nothing else to do at present */
2793}
2794
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002795static bool is_macro_id(const Token *t)
2796{
2797 return t && (t->type == TOK_ID ||
2798 (t->type == TOK_PREPROC_ID && t->text[1] == '$'));
2799}
2800
H. Peter Anvina039fcd2019-09-12 19:27:42 -07002801static char *get_id(Token **tp, const char *dname)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002802{
2803 char *id;
2804 Token *t = *tp;
2805
2806 t = t->next; /* Skip directive */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002807 t = skip_white(t);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002808 t = expand_id(t);
2809
2810 if (!is_macro_id(t)) {
H. Peter Anvina039fcd2019-09-12 19:27:42 -07002811 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002812 return NULL;
2813 }
2814
2815 id = t->text;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002816 t = skip_white(t);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002817 *tp = t;
2818 return id;
2819}
2820
H. Peter Anvina039fcd2019-09-12 19:27:42 -07002821/* Parse a %use package name and find the package. Set *err on syntax error. */
2822static const struct use_package *
2823get_use_pkg(Token *t, const char *dname, bool *err)
2824{
2825 char *id;
2826
2827 *err = false;
2828
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002829 t = skip_white(t);
H. Peter Anvina039fcd2019-09-12 19:27:42 -07002830
2831 t = expand_smacro(t);
2832
2833 id = NULL;
2834 if (t) {
2835 if (t->type == TOK_ID) {
2836 id = t->text;
2837 } else if (t->type == TOK_STRING) {
2838 nasm_unquote_cstr(t->text, NULL);
2839 id = t->text;
2840 }
2841 }
2842
2843 if (!id) {
2844 nasm_nonfatal("`%s' expects a package name", dname);
2845 *err = true;
2846 return NULL;
2847 }
2848
2849 t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002850 t = skip_white(t);
H. Peter Anvina039fcd2019-09-12 19:27:42 -07002851 if (t)
2852 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
2853
2854 return nasm_find_use_package(id);
2855}
2856
Ed Beroset3ab3f412002-06-11 03:31:49 +00002857/**
2858 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002859 * Find out if a line contains a preprocessor directive, and deal
2860 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002861 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002862 * If a directive _is_ found, it is the responsibility of this routine
2863 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002864 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002865 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002866 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00002867 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002868 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002869 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002870static int do_directive(Token *tline, Token **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002871{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002872 enum preproc_token i;
2873 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002874 bool err;
2875 int nparam;
2876 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002877 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002878 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002879 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002880 char *p, *pp;
2881 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002882 const char *mname;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002883 struct ppscan pps;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002884 Include *inc;
2885 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002886 Cond *cond;
2887 MMacro *mmac, **mmhead;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002888 Token *t = NULL, *tt, *macro_start, *last, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002889 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002890 struct tokenval tokval;
2891 expr *evalresult;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002892 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002893 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08002894 errflags severity;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002895 const char *dname; /* Name of directive, for messages */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002896
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002897 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002898 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002899
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002900 tline = skip_white(tline);
2901 if (!tline || !tok_type(tline, TOK_PREPROC_ID) ||
Cyrill Gorcunov4b5b7372018-10-29 22:54:08 +03002902 (tline->text[0] && (tline->text[1] == '%' ||
2903 tline->text[1] == '$' ||
2904 tline->text[1] == '!')))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002905 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002906
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002907 dname = tline->text;
H. Peter Anvin4169a472007-09-12 01:29:43 +00002908 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002909
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002910 casesense = true;
2911 if (PP_HAS_CASE(i) & PP_INSENSITIVE(i)) {
2912 casesense = false;
2913 i--;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002914 }
2915
2916 /*
2917 * If we're in a non-emitting branch of a condition construct,
2918 * or walking to the end of an already terminated %rep block,
2919 * we should ignore all directives except for condition
2920 * directives.
2921 */
2922 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002923 (istk->mstk.mstk && !istk->mstk.mstk->in_progress)) &&
2924 !is_condition(i)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002925 return NO_DIRECTIVE_FOUND;
2926 }
2927
2928 /*
2929 * If we're defining a macro or reading a %rep block, we should
2930 * ignore all directives except for %macro/%imacro (which nest),
2931 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2932 * If we're in a %rep block, another %rep nests, so should be let through.
2933 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002934 if (defining && i != PP_MACRO && i != PP_RMACRO &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002935 i != PP_ENDMACRO && i != PP_ENDM &&
2936 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2937 return NO_DIRECTIVE_FOUND;
2938 }
2939
2940 if (defining) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002941 if (i == PP_MACRO || i == PP_RMACRO) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002942 nested_mac_count++;
2943 return NO_DIRECTIVE_FOUND;
2944 } else if (nested_mac_count > 0) {
2945 if (i == PP_ENDMACRO) {
2946 nested_mac_count--;
2947 return NO_DIRECTIVE_FOUND;
2948 }
2949 }
2950 if (!defining->name) {
2951 if (i == PP_REP) {
2952 nested_rep_count++;
2953 return NO_DIRECTIVE_FOUND;
2954 } else if (nested_rep_count > 0) {
2955 if (i == PP_ENDREP) {
2956 nested_rep_count--;
2957 return NO_DIRECTIVE_FOUND;
2958 }
2959 }
2960 }
2961 }
2962
H. Peter Anvin4169a472007-09-12 01:29:43 +00002963 switch (i) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002964 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002965 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002966 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002967
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002968 case PP_PRAGMA:
2969 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002970 * %pragma namespace options...
2971 *
2972 * The namespace "preproc" is reserved for the preprocessor;
2973 * all other namespaces generate a [pragma] assembly directive.
2974 *
2975 * Invalid %pragmas are ignored and may have different
2976 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002977 */
H. Peter Anvinf5d7d902019-08-10 06:21:00 -07002978 t = tline;
2979 tline = tline->next;
2980 t->next = NULL;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002981 tline = zap_white(expand_smacro(tline));
2982 if (tok_type(tline, TOK_ID)) {
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002983 if (!nasm_stricmp(tline->text, "preproc")) {
2984 /* Preprocessor pragma */
2985 do_pragma_preproc(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07002986 free_tlist(tline);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002987 } else {
2988 /* Build the assembler directive */
H. Peter Anvin06335872019-08-10 06:42:55 -07002989
2990 /* Append bracket to the end of the output */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002991 for (t = tline; t->next; t = t->next)
2992 ;
2993 t->next = new_Token(NULL, TOK_OTHER, "]", 1);
H. Peter Anvin06335872019-08-10 06:42:55 -07002994
2995 /* Prepend "[pragma " */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002996 t = new_White(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07002997 t = new_Token(t, TOK_ID, "pragma", 6);
2998 t = new_Token(t, TOK_OTHER, "[", 1);
2999 tline = t;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07003000 *output = tline;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003001 }
3002 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003003 break;
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003004
H. Peter Anvine2c80182005-01-15 22:15:51 +00003005 case PP_STACKSIZE:
3006 /* Directive to tell NASM what the default stack size is. The
3007 * default is for a 16-bit stack, and this can be overriden with
3008 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003009 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003010 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003011 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003012 nasm_nonfatal("`%s' missing size parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003013 }
3014 if (nasm_stricmp(tline->text, "flat") == 0) {
3015 /* All subsequent ARG directives are for a 32-bit stack */
3016 StackSize = 4;
3017 StackPointer = "ebp";
3018 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003019 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08003020 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
3021 /* All subsequent ARG directives are for a 64-bit stack */
3022 StackSize = 8;
3023 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03003024 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08003025 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003026 } else if (nasm_stricmp(tline->text, "large") == 0) {
3027 /* All subsequent ARG directives are for a 16-bit stack,
3028 * far function call.
3029 */
3030 StackSize = 2;
3031 StackPointer = "bp";
3032 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003033 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003034 } else if (nasm_stricmp(tline->text, "small") == 0) {
3035 /* All subsequent ARG directives are for a 16-bit stack,
3036 * far function call. We don't support near functions.
3037 */
3038 StackSize = 2;
3039 StackPointer = "bp";
3040 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003041 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003042 } else {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003043 nasm_nonfatal("`%s' invalid size type", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003044 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003045 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003046
H. Peter Anvine2c80182005-01-15 22:15:51 +00003047 case PP_ARG:
3048 /* TASM like ARG directive to define arguments to functions, in
3049 * the following form:
3050 *
3051 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
3052 */
3053 offset = ArgOffset;
3054 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003055 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003056 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003057
H. Peter Anvine2c80182005-01-15 22:15:51 +00003058 /* Find the argument name */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003059 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003060 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003061 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003062 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003063 }
3064 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003065
H. Peter Anvine2c80182005-01-15 22:15:51 +00003066 /* Find the argument size type */
3067 tline = tline->next;
3068 if (!tline || tline->type != TOK_OTHER
3069 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003070 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003071 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003072 }
3073 tline = tline->next;
3074 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003075 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003076 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003077 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003078
H. Peter Anvine2c80182005-01-15 22:15:51 +00003079 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00003080 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003081 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003082 size = parse_size(tt->text);
3083 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003084 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003085 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003086 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003087 }
3088 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003089
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003090 /* Round up to even stack slots */
3091 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003092
H. Peter Anvine2c80182005-01-15 22:15:51 +00003093 /* Now define the macro for the argument */
3094 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
3095 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003096 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003097 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003098
H. Peter Anvine2c80182005-01-15 22:15:51 +00003099 /* Move to the next argument in the list */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003100 tline = skip_white(tline->next);
3101 } while (tok_is(tline, ','));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003102 ArgOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003103 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003104
H. Peter Anvine2c80182005-01-15 22:15:51 +00003105 case PP_LOCAL:
3106 /* TASM like LOCAL directive to define local variables for a
3107 * function, in the following form:
3108 *
3109 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
3110 *
3111 * The '= LocalSize' at the end is ignored by NASM, but is
3112 * required by TASM to define the local parameter size (and used
3113 * by the TASM macro package).
3114 */
3115 offset = LocalOffset;
3116 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003117 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003118 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003119
H. Peter Anvine2c80182005-01-15 22:15:51 +00003120 /* Find the argument name */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003121 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003122 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003123 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003124 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003125 }
3126 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003127
H. Peter Anvine2c80182005-01-15 22:15:51 +00003128 /* Find the argument size type */
3129 tline = tline->next;
3130 if (!tline || tline->type != TOK_OTHER
3131 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003132 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003133 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003134 }
3135 tline = tline->next;
3136 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003137 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003138 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003139 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003140
H. Peter Anvine2c80182005-01-15 22:15:51 +00003141 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00003142 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003143 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003144 size = parse_size(tt->text);
3145 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003146 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003147 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003148 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003149 }
3150 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003151
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003152 /* Round up to even stack slots */
3153 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003154
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003155 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003156
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003157 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003158 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
3159 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003160 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003161
H. Peter Anvine2c80182005-01-15 22:15:51 +00003162 /* Now define the assign to setup the enter_c macro correctly */
3163 snprintf(directive, sizeof(directive),
3164 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003165 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003166
H. Peter Anvine2c80182005-01-15 22:15:51 +00003167 /* Move to the next argument in the list */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003168 tline = skip_white(tline->next);
3169 } while (tok_is(tline, ','));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003170 LocalOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003171 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003172
H. Peter Anvine2c80182005-01-15 22:15:51 +00003173 case PP_CLEAR:
3174 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003175 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003176 free_macros();
3177 init_macros();
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003178 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003179
H. Peter Anvin418ca702008-05-30 10:42:30 -07003180 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003181 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003182 t = skip_white(t);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003183 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003184 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003185 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003186 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003187 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003188 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003189 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003190 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003191 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003192 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03003193 strlist_add(deplist, p);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003194 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003195
3196 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003197 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003198 t = skip_white(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07003199
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003200 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003201 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003202 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003203 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003204 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003205 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003206 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003207 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003208 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003209 nasm_unquote_cstr(p, NULL);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003210 nasm_new(inc);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003211 inc->next = istk;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003212 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07003213 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08003214 (pp_mode == PP_DEPS)
3215 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003216 if (!inc->fp) {
3217 /* -MG given but file not found */
3218 nasm_free(inc);
3219 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003220 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003221 inc->lineno = src_set_linnum(0);
3222 inc->lineinc = 1;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003223 inc->nolist = istk->nolist;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003224 istk = inc;
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003225 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003226 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003227 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003228
H. Peter Anvind2456592008-06-19 15:04:18 -07003229 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003230 {
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003231 const struct use_package *pkg;
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003232 bool err;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003233
H. Peter Anvindd88aa92019-09-12 19:39:48 -07003234 pkg = get_use_pkg(tline->next, dname, &err);
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003235 if (err)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003236 goto done;
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003237
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003238 if (!pkg) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003239 nasm_nonfatal("unknown `%s' package: %s", dname, tline->text);
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003240 } else if (!use_loaded[pkg->index]) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003241 /*
3242 * Not already included, go ahead and include it.
3243 * Treat it as an include file for the purpose of
3244 * producing a listing.
3245 */
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003246 use_loaded[pkg->index] = true;
3247 stdmacpos = pkg->macros;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003248 nasm_new(inc);
3249 inc->next = istk;
3250 inc->fname = src_set_fname(NULL);
3251 inc->lineno = src_set_linnum(0);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003252 inc->nolist = !list_option('b') || istk->nolist;
3253 istk = inc;
3254 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003255 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003256 break;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003257 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003258 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003259 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07003260 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003261 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003262 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003263 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003264 if (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003265 if (!tok_type(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003266 nasm_nonfatal("`%s' expects a context identifier",
3267 pp_directives[i]);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003268 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003269 }
3270 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003271 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003272 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003273 p = nasm_strdup(tline->text);
3274 } else {
3275 p = NULL; /* Anonymous */
3276 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07003277
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003278 if (i == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08003279 nasm_new(ctx);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003280 ctx->depth = cstk ? cstk->depth + 1 : 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003281 ctx->next = cstk;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003282 ctx->name = p;
3283 ctx->number = unique++;
3284 cstk = ctx;
3285 } else {
3286 /* %pop or %repl */
3287 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003288 nasm_nonfatal("`%s': context stack is empty",
3289 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003290 } else if (i == PP_POP) {
3291 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003292 nasm_nonfatal("`%s' in wrong context: %s, "
H. Peter Anvin8b262472019-02-26 14:00:54 -08003293 "expected %s",
3294 dname, cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003295 else
3296 ctx_pop();
3297 } else {
3298 /* i == PP_REPL */
3299 nasm_free(cstk->name);
3300 cstk->name = p;
3301 p = NULL;
3302 }
3303 nasm_free(p);
3304 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003305 break;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003306 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003307 severity = ERR_FATAL;
3308 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003309 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003310 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003311 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003312 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003313 /*!
3314 *!user [on] %warning directives
3315 *! controls output of \c{%warning} directives (see \k{pperror}).
3316 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003317 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003318 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003319
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003320issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07003321 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003322 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003323 tline->next = expand_smacro(tline->next);
3324 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003325 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003326 t = tline ? tline->next : NULL;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003327 t = skip_white(t);
3328 if (tok_type(tline, TOK_STRING) && !t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003329 /* The line contains only a quoted string */
3330 p = tline->text;
3331 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08003332 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003333 } else {
3334 /* Not a quoted string, or more than a quoted string */
3335 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08003336 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003337 nasm_free(p);
3338 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003339 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003340 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003341
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003342 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003343 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003344 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003345 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003346 j = if_condition(tline->next, i);
3347 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003348 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003349 cond = nasm_malloc(sizeof(Cond));
3350 cond->next = istk->conds;
3351 cond->state = j;
3352 istk->conds = cond;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003353 if(istk->mstk.mstk)
3354 istk->mstk.mstk->condcnt++;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003355 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003356
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003357 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003358 if (!istk->conds)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003359 nasm_fatal("`%s': no matching `%%if'", dname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003360 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003361 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003362 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003363 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003364
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003365 case COND_DONE:
3366 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003367 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003368
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003369 case COND_ELSE_TRUE:
3370 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003371 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003372 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003373 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003374 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003375
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003376 case COND_IF_FALSE:
3377 /*
3378 * IMPORTANT: In the case of %if, we will already have
3379 * called expand_mmac_params(); however, if we're
3380 * processing an %elif we must have been in a
3381 * non-emitting mode, which would have inhibited
3382 * the normal invocation of expand_mmac_params().
3383 * Therefore, we have to do it explicitly here.
3384 */
3385 j = if_condition(expand_mmac_params(tline->next), i);
3386 tline->next = NULL; /* it got freed */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003387 istk->conds->state = j;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003388 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003389 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003390 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003391
H. Peter Anvine2c80182005-01-15 22:15:51 +00003392 case PP_ELSE:
3393 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003394 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003395 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003396 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07003397 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003398 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003399 case COND_IF_TRUE:
3400 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003401 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003402 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003403
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003404 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003405 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003406
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003407 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003408 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003409 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003410
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003411 case COND_ELSE_TRUE:
3412 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003413 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003414 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003415 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003416 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003417 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003418 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003419
H. Peter Anvine2c80182005-01-15 22:15:51 +00003420 case PP_ENDIF:
3421 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003422 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003423 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003424 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003425 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003426 cond = istk->conds;
3427 istk->conds = cond->next;
3428 nasm_free(cond);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003429 if(istk->mstk.mstk)
3430 istk->mstk.mstk->condcnt--;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003431 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003432
H. Peter Anvin8b262472019-02-26 14:00:54 -08003433 case PP_RMACRO:
3434 case PP_MACRO:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003435 nasm_assert(!defining);
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07003436 nasm_new(defining);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003437 defining->casesense = casesense;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003438 defining->dstk.mmac = defining;
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07003439 if (i == PP_RMACRO)
3440 defining->max_depth = nasm_limit[LIMIT_MACRO_LEVELS];
H. Peter Anvin8b262472019-02-26 14:00:54 -08003441 if (!parse_mmacro_spec(tline, defining, dname)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003442 nasm_free(defining);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003443 goto done;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003444 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003445
H. Peter Anvin4def1a82016-05-09 13:59:44 -07003446 src_get(&defining->xline, &defining->fname);
3447
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003448 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
3449 while (mmac) {
3450 if (!strcmp(mmac->name, defining->name) &&
3451 (mmac->nparam_min <= defining->nparam_max
3452 || defining->plus)
3453 && (defining->nparam_min <= mmac->nparam_max
3454 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003455 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003456 defining->name);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003457 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003458 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003459 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003460 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003461 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003462
H. Peter Anvine2c80182005-01-15 22:15:51 +00003463 case PP_ENDM:
3464 case PP_ENDMACRO:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003465 if (!(defining && defining->name)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003466 nasm_nonfatal("`%s': not defining a macro", tline->text);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003467 goto done;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003468 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003469 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
3470 defining->next = *mmhead;
3471 *mmhead = defining;
3472 defining = NULL;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003473 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003474
H. Peter Anvin89cee572009-07-15 09:16:54 -04003475 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003476 /*
3477 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003478 * macro-end marker for a macro with a name. Then we
3479 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003480 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003481 list_for_each(l, istk->expansion)
3482 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003483 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003484
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003485 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003486 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003487 * Remove all conditional entries relative to this
3488 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003489 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003490 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
3491 cond = istk->conds;
3492 istk->conds = cond->next;
3493 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003494 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003495 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003496 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003497 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003498 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003499 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05003500
H. Peter Anvina26433d2008-07-16 14:40:01 -07003501 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003502 casesense = false;
3503 /* fall through */
3504 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07003505 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003506 MMacro **mmac_p;
3507 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003508
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003509 nasm_zero(spec);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003510 spec.casesense = casesense;
3511 if (!parse_mmacro_spec(tline, &spec, dname)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003512 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003513 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003514 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
3515 while (mmac_p && *mmac_p) {
3516 mmac = *mmac_p;
3517 if (mmac->casesense == spec.casesense &&
3518 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
3519 mmac->nparam_min == spec.nparam_min &&
3520 mmac->nparam_max == spec.nparam_max &&
3521 mmac->plus == spec.plus) {
3522 *mmac_p = mmac->next;
3523 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003524 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003525 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003526 }
3527 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003528 free_tlist(spec.dlist);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003529 break;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003530 }
3531
H. Peter Anvine2c80182005-01-15 22:15:51 +00003532 case PP_ROTATE:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003533 while (tok_white(tline->next))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003534 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003535 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003536 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003537 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003538 return DIRECTIVE_FOUND;
3539 }
3540 t = expand_smacro(tline->next);
3541 tline->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003542 pps.tptr = tline = t;
3543 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003544 tokval.t_type = TOKEN_INVALID;
3545 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003546 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003547 free_tlist(tline);
3548 if (!evalresult)
3549 return DIRECTIVE_FOUND;
3550 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003551 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003552 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003553 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003554 return DIRECTIVE_FOUND;
3555 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003556 mmac = istk->mstk.mmac;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003557 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003558 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003559 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003560 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003561 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003562 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003563
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003564 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003565 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003566 rotate += mmac->nparam;
3567
3568 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003569 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003570 break;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003571
H. Peter Anvine2c80182005-01-15 22:15:51 +00003572 case PP_REP:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003573 {
3574 MMacro *tmp_defining;
3575
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003576 nolist = false;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003577 tline = skip_white(tline->next);
3578 if (tok_type(tline, TOK_ID) &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003579 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003580 nolist = !list_option('f') || istk->nolist;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003581 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003582 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003583
H. Peter Anvine2c80182005-01-15 22:15:51 +00003584 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003585 pps.tptr = expand_smacro(tline);
3586 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003587 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003588 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003589 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003590 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003591 if (!evalresult)
3592 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003593 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003594 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003595 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003596 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003597 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003598 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003599 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003600 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003601 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
3602 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003603 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003604 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003605 /*!
3606 *!negative-rep [on] regative %rep count
3607 *! warns about negative counts given to the \c{%rep}
3608 *! preprocessor directive.
3609 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08003610 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003611 "negative `%%rep' count: %"PRId64, count);
3612 count = 0;
3613 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003614 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003615 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003616 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003617 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003618 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003619 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003620 tmp_defining = defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003621 nasm_new(defining);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003622 defining->nolist = nolist;
3623 defining->in_progress = count;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003624 defining->mstk = istk->mstk;
3625 defining->dstk.mstk = tmp_defining;
3626 defining->dstk.mmac = tmp_defining ? tmp_defining->dstk.mmac : NULL;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003627 src_get(&defining->xline, &defining->fname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003628 break;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003629 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003630
H. Peter Anvine2c80182005-01-15 22:15:51 +00003631 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003632 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003633 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003634 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003635 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003636
H. Peter Anvine2c80182005-01-15 22:15:51 +00003637 /*
3638 * Now we have a "macro" defined - although it has no name
3639 * and we won't be entering it in the hash tables - we must
3640 * push a macro-end marker for it on to istk->expansion.
3641 * After that, it will take care of propagating itself (a
3642 * macro-end marker line for a macro which is really a %rep
3643 * block will cause the macro to be re-expanded, complete
3644 * with another macro-end marker to ensure the process
3645 * continues) until the whole expansion is forcibly removed
3646 * from istk->expansion by a %exitrep.
3647 */
H. Peter Anvin6686de22019-08-10 05:33:14 -07003648 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003649 l->next = istk->expansion;
3650 l->finishes = defining;
3651 l->first = NULL;
3652 istk->expansion = l;
3653
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003654 istk->mstk.mstk = defining;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003655
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003656 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003657 defining = defining->dstk.mstk;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003658 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003659
H. Peter Anvine2c80182005-01-15 22:15:51 +00003660 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003661 /*
3662 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003663 * macro-end marker for a macro with no name. Then we set
3664 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003665 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003666 list_for_each(l, istk->expansion)
3667 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003668 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003669
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003670 if (l)
3671 l->finishes->in_progress = 1;
3672 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003673 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003674 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003675
H. Peter Anvin8b262472019-02-26 14:00:54 -08003676 case PP_DEFINE:
3677 case PP_XDEFINE:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003678 case PP_DEFALIAS:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003679 {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003680 SMacro tmpl;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003681 Token **lastp;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003682
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003683 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003684 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003685
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003686 nasm_zero(tmpl);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003687 lastp = &tline->next;
3688 nparam = parse_smacro_template(&lastp, &tmpl);
3689 tline = *lastp;
3690 *lastp = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003691
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003692 if (unlikely(i == PP_DEFALIAS)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003693 macro_start = tline;
3694 if (!is_macro_id(macro_start)) {
3695 nasm_nonfatal("`%s' expects a macro identifier to alias",
3696 dname);
3697 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003698 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003699 tt = macro_start->next;
3700 macro_start->next = NULL;
3701 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003702 tline = skip_white(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003703 if (tline && tline->type) {
3704 nasm_warn(WARN_OTHER,
3705 "trailing garbage after aliasing identifier ignored");
3706 }
3707 free_tlist(tt);
3708 tmpl.alias = true;
3709 } else {
3710 /* Expand the macro definition now for %xdefine and %ixdefine */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003711 if (i == PP_XDEFINE)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003712 tline = expand_smacro(tline);
3713
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003714 /* Reverse expansion list and mark parameter tokens */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003715 macro_start = NULL;
3716 t = tline;
3717 while (t) {
3718 if (t->type == TOK_ID) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003719 for (i = 0; i < nparam; i++) {
3720 if ((size_t)tmpl.params[i].namelen == t->len &&
3721 !memcmp(t->text, tmpl.params[i].name, t->len)) {
3722 t->type = tok_smac_param(i);
3723 break;
3724 }
3725 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003726 }
3727 tt = t->next;
3728 t->next = macro_start;
3729 macro_start = t;
3730 t = tt;
3731 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003732 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003733
H. Peter Anvine2c80182005-01-15 22:15:51 +00003734 /*
3735 * Good. We now have a macro name, a parameter count, and a
3736 * token list (in reverse order) for an expansion. We ought
3737 * to be OK just to create an SMacro, store it, and let
3738 * free_tlist have the rest of the line (which we have
3739 * carefully re-terminated after chopping off the expansion
3740 * from the end).
3741 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003742 define_smacro(mname, casesense, macro_start, &tmpl);
3743 break;
3744 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003745
H. Peter Anvine2c80182005-01-15 22:15:51 +00003746 case PP_UNDEF:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003747 case PP_UNDEFALIAS:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003748 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003749 goto done;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003750 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003751 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003752
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003753 undef_smacro(mname, i == PP_UNDEFALIAS);
3754 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003755
H. Peter Anvin8b262472019-02-26 14:00:54 -08003756 case PP_DEFSTR:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003757 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003758 goto done;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003759
H. Peter Anvin9e200162008-06-04 17:23:14 -07003760 last = tline;
3761 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003762 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003763
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003764 tline = zap_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003765 p = detoken(tline, false);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003766 macro_start = make_tok_qstr(p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003767 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003768
3769 /*
3770 * We now have a macro name, an implicit parameter count of
3771 * zero, and a string token to use as an expansion. Create
3772 * and store an SMacro.
3773 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003774 define_smacro(mname, casesense, macro_start, NULL);
3775 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003776
H. Peter Anvin8b262472019-02-26 14:00:54 -08003777 case PP_DEFTOK:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003778 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003779 goto done;
3780
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003781 last = tline;
3782 tline = expand_smacro(tline->next);
3783 last->next = NULL;
3784
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003785 t = skip_white(tline);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003786 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003787 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003788 nasm_nonfatal("`%s' requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003789 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003790 goto done;
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003791 }
3792
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003793 /*
3794 * Convert the string to a token stream. Note that smacros
3795 * are stored with the token stream reversed, so we have to
3796 * reverse the output of tokenize().
3797 */
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003798 nasm_unquote_cstr(t->text, NULL);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003799 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003800
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003801 /*
3802 * We now have a macro name, an implicit parameter count of
3803 * zero, and a numeric token to use as an expansion. Create
3804 * and store an SMacro.
3805 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003806 define_smacro(mname, casesense, macro_start, NULL);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003807 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003808 break;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003809
H. Peter Anvin418ca702008-05-30 10:42:30 -07003810 case PP_PATHSEARCH:
3811 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003812 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003813
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003814 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003815 goto done;
3816
H. Peter Anvin418ca702008-05-30 10:42:30 -07003817 last = tline;
3818 tline = expand_smacro(tline->next);
3819 last->next = NULL;
3820
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003821 t = skip_white(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003822 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003823 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003824 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003825 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003826 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003827 }
3828 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003829 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003830 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003831 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003832 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003833
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07003834 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003835 if (!found_path)
3836 found_path = p;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003837 macro_start = make_tok_qstr(found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003838
3839 /*
3840 * We now have a macro name, an implicit parameter count of
3841 * zero, and a string token to use as an expansion. Create
3842 * and store an SMacro.
3843 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003844 define_smacro(mname, casesense, macro_start, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003845 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003846 break;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003847 }
3848
H. Peter Anvine2c80182005-01-15 22:15:51 +00003849 case PP_STRLEN:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003850 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003851 goto done;
3852
H. Peter Anvine2c80182005-01-15 22:15:51 +00003853 last = tline;
3854 tline = expand_smacro(tline->next);
3855 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003856
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003857 t = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003858 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003859 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003860 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003861 free_tlist(tline);
3862 free_tlist(origline);
3863 return DIRECTIVE_FOUND;
3864 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003865
H. Peter Anvin8b262472019-02-26 14:00:54 -08003866 macro_start = make_tok_num(nasm_unquote(t->text, NULL));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003867
H. Peter Anvine2c80182005-01-15 22:15:51 +00003868 /*
3869 * We now have a macro name, an implicit parameter count of
3870 * zero, and a numeric token to use as an expansion. Create
3871 * and store an SMacro.
3872 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003873 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003874 free_tlist(tline);
3875 free_tlist(origline);
3876 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003877
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003878 case PP_STRCAT:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003879 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003880 goto done;
3881
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003882 last = tline;
3883 tline = expand_smacro(tline->next);
3884 last->next = NULL;
3885
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003886 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003887 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003888 switch (t->type) {
3889 case TOK_WHITESPACE:
3890 break;
3891 case TOK_STRING:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003892 len += t->len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003893 break;
3894 case TOK_OTHER:
3895 if (!strcmp(t->text, ",")) /* permit comma separators */
3896 break;
3897 /* else fall through */
3898 default:
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003899 nasm_nonfatal("non-string passed to `%s': %s", dname, t->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003900 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003901 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003902 }
3903 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003904
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003905 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003906 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003907 if (t->type == TOK_STRING) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003908 memcpy(p, t->text, t->len);
3909 p += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003910 }
3911 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003912
3913 /*
3914 * We now have a macro name, an implicit parameter count of
3915 * zero, and a numeric token to use as an expansion. Create
3916 * and store an SMacro.
3917 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08003918 macro_start = make_tok_qstr(pp);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003919 nasm_free(pp);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003920 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003921 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003922 break;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003923
H. Peter Anvine2c80182005-01-15 22:15:51 +00003924 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003925 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003926 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003927 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003928
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003929 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003930 goto done;
3931
H. Peter Anvine2c80182005-01-15 22:15:51 +00003932 last = tline;
3933 tline = expand_smacro(tline->next);
3934 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003935
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003936 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003937 t = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003938
3939 t = skip_white(t);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003940
H. Peter Anvine2c80182005-01-15 22:15:51 +00003941 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003942 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003943 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003944 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003945 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003946 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003947
H. Peter Anvin8b262472019-02-26 14:00:54 -08003948 pps.tptr = t->next;
3949 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003950 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003951 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003952 if (!evalresult) {
3953 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003954 goto done;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003955 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003956 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003957 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003958 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003959 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003960 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003961
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003962 pps.tptr = skip_white(pps.tptr);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003963 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003964 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003965 } else {
3966 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003967 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003968 if (!evalresult) {
3969 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003970 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003971 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003972 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003973 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003974 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003975 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003976 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003977 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003978
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003979 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003980
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003981 /* make start and count being in range */
3982 if (start < 0)
3983 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003984 if (count < 0)
3985 count = len + count + 1 - start;
3986 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003987 count = len - start;
3988 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003989 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003990
H. Peter Anvin8b262472019-02-26 14:00:54 -08003991 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003992 macro_start->len = count;
3993 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start,
3994 &macro_start->len);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003995
H. Peter Anvine2c80182005-01-15 22:15:51 +00003996 /*
3997 * We now have a macro name, an implicit parameter count of
3998 * zero, and a numeric token to use as an expansion. Create
3999 * and store an SMacro.
4000 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004001 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004002 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004003 break;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004004 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004005
H. Peter Anvin8b262472019-02-26 14:00:54 -08004006 case PP_ASSIGN:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004007 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004008 goto done;
4009
H. Peter Anvine2c80182005-01-15 22:15:51 +00004010 last = tline;
4011 tline = expand_smacro(tline->next);
4012 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004013
H. Peter Anvin8b262472019-02-26 14:00:54 -08004014 pps.tptr = tline;
4015 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004016 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004017 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004018 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004019 if (!evalresult)
4020 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004021
H. Peter Anvine2c80182005-01-15 22:15:51 +00004022 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004023 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00004024
H. Peter Anvine2c80182005-01-15 22:15:51 +00004025 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004026 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004027 free_tlist(origline);
4028 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004029 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004030
H. Peter Anvin8b262472019-02-26 14:00:54 -08004031 macro_start = make_tok_num(reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00004032
H. Peter Anvine2c80182005-01-15 22:15:51 +00004033 /*
4034 * We now have a macro name, an implicit parameter count of
4035 * zero, and a numeric token to use as an expansion. Create
4036 * and store an SMacro.
4037 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004038 define_smacro(mname, casesense, macro_start, NULL);
4039 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004040
H. Peter Anvind2354082019-08-27 16:38:48 -07004041 case PP_ALIASES:
4042 tline = tline->next;
4043 tline = expand_smacro(tline);
4044 do_aliases = pp_get_boolean_option(tline, do_aliases);
4045 break;
4046
H. Peter Anvine2c80182005-01-15 22:15:51 +00004047 case PP_LINE:
4048 /*
4049 * Syntax is `%line nnn[+mmm] [filename]'
4050 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004051 if (unlikely(pp_noline))
4052 goto done;
4053
H. Peter Anvine2c80182005-01-15 22:15:51 +00004054 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004055 tline = skip_white(tline);
4056 if (!tok_type(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004057 nasm_nonfatal("`%s' expects line number", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004058 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004059 }
H. Peter Anvin70055962007-10-11 00:05:31 -07004060 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004061 m = 1;
4062 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004063 if (tok_is(tline, '+')) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004064 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004065 if (!tok_type(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004066 nasm_nonfatal("`%s' expects line increment", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004067 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004068 }
H. Peter Anvin70055962007-10-11 00:05:31 -07004069 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004070 tline = tline->next;
4071 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004072 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004073 src_set_linnum(k);
4074 istk->lineinc = m;
4075 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07004076 char *fname = detoken(tline, false);
4077 src_set_fname(fname);
4078 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004079 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004080 break;
4081 }
4082
4083done:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004084 free_tlist(origline);
4085 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004086}
4087
4088/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00004089 * Ensure that a macro parameter contains a condition code and
4090 * nothing else. Return the condition code index if so, or -1
4091 * otherwise.
4092 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004093static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004094{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004095 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004096
H. Peter Anvin25a99342007-09-22 17:45:45 -07004097 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004098 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07004099
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004100 t = skip_white(t);
Cyrill Gorcunov7524cfd2017-10-22 19:01:16 +03004101 if (!t)
4102 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004103 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004104 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004105 tt = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004106 tt = skip_white(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00004107 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004108 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004109
Cyrill Gorcunov19456392012-05-02 00:18:56 +04004110 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00004111}
4112
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004113static inline bool pp_concat_match(const Token *t, unsigned int mask)
4114{
4115 return t && (PP_CONCAT_MASK(t->type) & mask);
4116}
4117
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004118/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004119 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004120 * pasting, if @handle_explicit passed then explicit pasting
4121 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004122 * The @m array can contain a series of token types which are
4123 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004124 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004125static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004126 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07004127{
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004128 Token *tok, *t, *next, **prev_next, **prev_nonspace;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004129 bool pasted = false;
4130 char *buf, *p;
4131 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07004132
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004133 /*
4134 * The last token before pasting. We need it
4135 * to be able to connect new handled tokens.
4136 * In other words if there were a tokens stream
4137 *
4138 * A -> B -> C -> D
4139 *
4140 * and we've joined tokens B and C, the resulting
4141 * stream should be
4142 *
4143 * A -> BC -> D
4144 */
4145 tok = *head;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004146 prev_next = prev_nonspace = head;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004147
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004148 if (tok_white(tok) || tok_type(tok, TOK_PASTE))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004149 prev_nonspace = NULL;
4150
4151 while (tok && (next = tok->next)) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004152 bool did_paste = false;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004153
4154 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07004155 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004156 /* Zap redundant whitespaces */
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004157 tok->next = next = zap_white(next);
H. Peter Anvind784a082009-04-20 14:01:18 -07004158 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004159
4160 case TOK_PASTE:
4161 /* Explicit pasting */
4162 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004163 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004164
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004165 /* Left pasting token is start of line */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004166 if (!prev_nonspace) {
4167 nasm_nonfatal("No lvalue found on pasting");
4168 tok = delete_Token(tok);
4169 break;
4170 }
4171
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004172 did_paste = true;
4173
4174 prev_next = prev_nonspace;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004175 t = *prev_nonspace;
4176
4177 /* Delete leading whitespace */
4178 next = zap_white(t->next);
4179
4180 /* Delete the %+ token itself */
4181 nasm_assert(next == tok);
4182 next = delete_Token(next);
4183
4184 /* Delete trailing whitespace */
4185 next = zap_white(next);
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004186
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004187 /*
4188 * No ending token, this might happen in two
4189 * cases
4190 *
4191 * 1) There indeed no right token at all
4192 * 2) There is a bare "%define ID" statement,
4193 * and @ID does expand to whitespace.
4194 *
4195 * So technically we need to do a grammar analysis
4196 * in another stage of parsing, but for now lets don't
4197 * change the behaviour people used to. Simply allow
4198 * whitespace after paste token.
4199 */
4200 if (!next) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004201 *prev_nonspace = tok = NULL; /* End of line */
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004202 break;
4203 }
4204
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004205 p = buf = nasm_malloc(t->len + next->len + 1);
4206 p = mempcpy(p, t->text, t->len);
4207 p = mempcpy(p, next->text, next->len);
4208 *p = '\0';
4209 delete_Token(t);
4210 t = tokenize(buf);
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004211 nasm_free(buf);
4212
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004213 if (unlikely(!t)) {
4214 /*
4215 * No output at all? Replace with a single whitespace.
4216 * This should never happen.
4217 */
4218 t = new_White(NULL);
4219 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004220
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004221 *prev_nonspace = tok = t;
4222 while (t->next)
4223 t = t->next; /* Find the last token produced */
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004224
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004225 /* Delete the second token and attach to the end of the list */
4226 t->next = delete_Token(next);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004227
4228 /* We want to restart from the head of the pasted token */
4229 next = tok;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004230 break;
4231
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004232 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004233 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004234 for (i = 0; i < mnum; i++) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004235 if (pp_concat_match(tok, m[i].mask_head))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004236 break;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004237 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004238
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004239 if (i >= mnum)
4240 break;
4241
4242 len = tok->len;
4243 while (pp_concat_match(next, m[i].mask_tail)) {
4244 len += next->len;
4245 next = next->next;
4246 }
4247
4248 /* No match or no text to process */
4249 if (len == tok->len)
4250 break;
4251
4252 p = buf = nasm_malloc(len + 1);
4253 while (tok != next) {
4254 p = mempcpy(p, tok->text, tok->len);
4255 tok = delete_Token(tok);
4256 }
4257 *p = '\0';
4258 *prev_next = tok = t = tokenize(buf);
4259 nasm_free(buf);
4260
4261 /*
4262 * Connect pasted into original stream,
4263 * ie A -> new-tokens -> B
4264 */
4265 while (t->next)
4266 t = t->next;
4267 t->next = next;
4268 prev_next = prev_nonspace = &t->next;
4269 did_paste = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004270 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004271 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004272
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004273 if (did_paste) {
4274 pasted = true;
4275 } else {
4276 prev_next = &tok->next;
4277 if (next && next->type != TOK_WHITESPACE && next->type != TOK_PASTE)
4278 prev_nonspace = prev_next;
4279 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004280
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004281 tok = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004282 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004283
4284 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07004285}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004286
4287/*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004288 * Computes the proper rotation of mmacro parameters
4289 */
4290static int mmac_rotate(const MMacro *mac, unsigned int n)
4291{
4292 if (--n < mac->nparam)
4293 n = (n + mac->rotate) % mac->nparam;
4294
4295 return n+1;
4296}
4297
4298/*
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004299 * expands to a list of tokens from %{x:y}
4300 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004301static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004302{
4303 Token *t = tline, **tt, *tm, *head;
4304 char *pos;
4305 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004306
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004307 pos = strchr(tline->text, ':');
4308 nasm_assert(pos);
4309
4310 lst = atoi(pos + 1);
4311 fst = atoi(tline->text + 1);
4312
4313 /*
4314 * only macros params are accounted so
4315 * if someone passes %0 -- we reject such
4316 * value(s)
4317 */
4318 if (lst == 0 || fst == 0)
4319 goto err;
4320
4321 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004322 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
4323 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004324 goto err;
4325
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004326 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
4327 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004328
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004329 /* count from zero */
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004330 fst--, lst--;
4331
4332 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004333 * It will be at least one token. Note we
4334 * need to scan params until separator, otherwise
4335 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004336 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004337 j = (fst + mac->rotate) % mac->nparam;
4338 tm = mac->params[j+1];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03004339 if (!tm)
4340 goto err;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004341 head = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004342 tt = &head->next, tm = tm->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004343 while (tok_isnt(tm, ',')) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004344 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004345 *tt = t, tt = &t->next, tm = tm->next;
4346 }
4347
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004348 if (fst < lst) {
4349 for (i = fst + 1; i <= lst; i++) {
4350 t = new_Token(NULL, TOK_OTHER, ",", 0);
4351 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004352 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004353 tm = mac->params[j+1];
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004354 while (tok_isnt(tm, ',')) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004355 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004356 *tt = t, tt = &t->next, tm = tm->next;
4357 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004358 }
4359 } else {
4360 for (i = fst - 1; i >= lst; i--) {
4361 t = new_Token(NULL, TOK_OTHER, ",", 0);
4362 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004363 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004364 tm = mac->params[j+1];
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004365 while (!tok_isnt(tm, ',')) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004366 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004367 *tt = t, tt = &t->next, tm = tm->next;
4368 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004369 }
4370 }
4371
4372 *last = tt;
4373 return head;
4374
4375err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004376 nasm_nonfatal("`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004377 &tline->text[1]);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004378 return NULL;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004379}
4380
H. Peter Anvin76690a12002-04-30 20:52:49 +00004381/*
4382 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004383 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004384 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004385 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004386static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004387{
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004388 Token **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004389 bool changed = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004390 MMacro *mac = istk->mstk.mmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004391
4392 tail = &thead;
4393 thead = NULL;
4394
H. Peter Anvine2c80182005-01-15 22:15:51 +00004395 while (tline) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004396 bool change;
4397 Token *t = tline;
4398 char *text = t->text;
4399 int type = t->type;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004400
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004401 tline = tline->next;
4402 t->next = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004403
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004404 switch (type) {
4405 case TOK_PREPROC_ID:
4406 {
4407 Token *tt = NULL;
4408
4409 change = false;
4410
4411 if (!text || !text[0])
4412 break;
4413 if (!(nasm_isdigit(text[1]) || text[1] == '%' ||
4414 ((text[1] == '+' || text[1] == '-') && text[2])))
4415 break;
4416
4417 change = true;
4418
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004419 if (!mac) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004420 nasm_nonfatal("`%s': not in a macro call", text);
4421 text = NULL;
4422 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004423 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004424
4425 if (strchr(text, ':')) {
4426 /*
4427 * seems we have a parameters range here
4428 */
4429 Token *head, **last;
4430 head = expand_mmac_params_range(mac, t, &last);
4431 if (head) {
4432 *tail = head;
4433 *last = tline;
4434 text = NULL;
4435 }
4436 break;
4437 }
4438
4439 switch (text[1]) {
4440 /*
4441 * We have to make a substitution of one of the
4442 * forms %1, %-1, %+1, %%foo, %0, %00.
4443 */
4444 case '0':
4445 if (!text[2]) {
4446 type = TOK_NUMBER;
4447 text = nasm_asprintf("%d", mac->nparam);
4448 break;
4449 }
4450 if (text[2] != '0' || text[3])
4451 goto invalid;
4452 /* a possible captured label == mac->params[0] */
4453 /* fall through */
4454 default:
4455 {
4456 unsigned long n;
4457 char *ep;
4458
4459 n = strtoul(text + 1, &ep, 10);
4460 if (unlikely(*ep))
4461 goto invalid;
4462
4463 if (n <= mac->nparam) {
4464 n = mmac_rotate(mac, n);
4465 dup_tlistn(mac->params[n], mac->paramlen[n], &tail);
4466 }
4467 text = NULL;
4468 break;
4469 }
4470 case '%':
4471 type = TOK_ID;
4472 text = nasm_asprintf("..@%"PRIu64".%s", mac->unique, text+2);
4473 break;
4474 case '-':
4475 case '+':
4476 {
4477 int cc;
4478 unsigned long n;
4479 char *ep;
4480
4481 text = NULL;
4482
4483 n = strtoul(t->text + 2, &ep, 10);
4484 if (unlikely(*ep))
4485 goto invalid;
4486
4487 if (n && n < mac->nparam) {
4488 n = mmac_rotate(mac, n);
4489 tt = mac->params[n];
4490 }
4491 cc = find_cc(tt);
4492 if (cc == -1) {
4493 nasm_nonfatal("macro parameter `%s' is not a condition code",
H. Peter Anvind2354082019-08-27 16:38:48 -07004494 t->text);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004495 text = NULL;
4496 break;
4497 }
4498
4499 type = TOK_ID;
4500 if (text[1] == '-') {
4501 int ncc = inverse_ccs[cc];
4502 if (unlikely(ncc == -1)) {
4503 nasm_nonfatal("condition code `%s' is not invertible",
4504 conditions[cc]);
4505 break;
4506 }
4507 cc = ncc;
4508 }
4509 text = nasm_strdup(conditions[cc]);
4510 break;
4511 }
4512
4513 invalid:
4514 nasm_nonfatal("invalid macro parameter: `%s'", text);
4515 text = NULL;
4516 break;
4517 }
4518 break;
4519 }
4520
4521 case TOK_PREPROC_Q:
4522 if (mac) {
4523 type = TOK_ID;
4524 text = nasm_strdup(mac->iname);
4525 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07004526 } else {
4527 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004528 }
4529 break;
4530
4531 case TOK_PREPROC_QQ:
4532 if (mac) {
4533 type = TOK_ID;
4534 text = nasm_strdup(mac->name);
4535 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07004536 } else {
4537 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004538 }
4539 break;
4540
4541 case TOK_INDIRECT:
4542 {
4543 Token *tt;
4544
4545 tt = tokenize(t->text);
4546 tt = expand_mmac_params(tt);
4547 tt = expand_smacro(tt);
4548 /* Why dup_tlist() here? We should own tt... */
4549 dup_tlist(tt, &tail);
4550 text = NULL;
4551 change = true;
4552 break;
4553 }
4554
4555 default:
4556 change = false;
4557 break;
4558 }
4559
4560 if (change) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004561 if (!text) {
4562 delete_Token(t);
4563 } else {
4564 *tail = t;
4565 tail = &t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004566 nasm_free(t->text);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004567 t->len = strlen(text);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004568 t->type = type;
4569 t->text = text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004570 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004571 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004572 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004573 *tail = t;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004574 tail = &t->next;
4575 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004576 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004577
H. Peter Anvineba20a72002-04-30 20:53:55 +00004578 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004579
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004580 if (changed) {
4581 const struct tokseq_match t[] = {
4582 {
4583 PP_CONCAT_MASK(TOK_ID) |
4584 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4585 PP_CONCAT_MASK(TOK_ID) |
4586 PP_CONCAT_MASK(TOK_NUMBER) |
4587 PP_CONCAT_MASK(TOK_FLOAT) |
4588 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4589 },
4590 {
4591 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4592 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4593 }
4594 };
4595 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4596 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004597
H. Peter Anvin76690a12002-04-30 20:52:49 +00004598 return thead;
4599}
4600
H. Peter Anvin322bee02019-08-10 01:38:06 -07004601static Token *expand_smacro_noreset(Token * tline);
H. Peter Anvin322bee02019-08-10 01:38:06 -07004602
H. Peter Anvin76690a12002-04-30 20:52:49 +00004603/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004604 * Expand *one* single-line macro instance. If the first token is not
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004605 * a macro at all, it is simply copied to the output and the pointer
4606 * advanced. tpp should be a pointer to a pointer (usually the next
4607 * pointer of the previous token) to the first token. **tpp is updated
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004608 * to point to the first token of the expansion, and *tpp updated to
4609 * point to the next pointer of the last token of the expansion.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004610 *
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004611 * If the expansion is empty, *tpp will be unchanged but **tpp will
4612 * be advanced past the macro call.
4613 *
H. Peter Anvin322bee02019-08-10 01:38:06 -07004614 * Return the macro expanded, or NULL if no expansion took place.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004615 */
H. Peter Anvin322bee02019-08-10 01:38:06 -07004616static SMacro *expand_one_smacro(Token ***tpp)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004617{
4618 Token **params = NULL;
4619 const char *mname;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004620 Token *mstart = **tpp;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004621 Token *tline = mstart;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004622 SMacro *head, *m;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004623 int i;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004624 Token *t, *tup, *tafter;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004625 int nparam = 0;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004626 bool cond_comma;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004627
4628 if (!tline)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004629 return false; /* Empty line, nothing to do */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004630
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004631 mname = mstart->text;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004632
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004633 smacro_deadman.total--;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004634 smacro_deadman.levels--;
4635
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004636 if (unlikely(smacro_deadman.total < 0 || smacro_deadman.levels < 0)) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07004637 if (unlikely(!smacro_deadman.triggered)) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004638 nasm_nonfatal("interminable macro recursion");
H. Peter Anvin322bee02019-08-10 01:38:06 -07004639 smacro_deadman.triggered = true;
4640 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004641 goto not_a_macro;
4642 } else if (tline->type == TOK_ID) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004643 head = (SMacro *)hash_findix(&smacros, mname);
4644 } else if (tline->type == TOK_PREPROC_ID) {
4645 Context *ctx = get_ctx(mname, &mname);
4646 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4647 } else {
4648 goto not_a_macro;
4649 }
4650
4651 /*
4652 * We've hit an identifier of some sort. First check whether the
4653 * identifier is a single-line macro at all, then think about
4654 * checking for parameters if necessary.
4655 */
4656 list_for_each(m, head) {
H. Peter Anvind2354082019-08-27 16:38:48 -07004657 if (unlikely(m->alias && !do_aliases))
4658 continue;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004659 if (!mstrcmp(m->name, mname, m->casesense))
4660 break;
4661 }
4662
4663 if (!m) {
4664 goto not_a_macro;
4665 }
4666
4667 /* Parse parameters, if applicable */
4668
4669 params = NULL;
4670 nparam = 0;
4671
4672 if (m->nparam == 0) {
4673 /*
4674 * Simple case: the macro is parameterless.
4675 * Nothing to parse; the expansion code will
4676 * drop the macro name token.
4677 */
4678 } else {
4679 /*
4680 * Complicated case: at least one macro with this name
4681 * exists and takes parameters. We must find the
4682 * parameters in the call, count them, find the SMacro
4683 * that corresponds to that form of the macro call, and
4684 * substitute for the parameters when we expand. What a
4685 * pain.
4686 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004687 Token *t;
4688 int paren, brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004689
4690 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004691 tline = skip_white(tline);
4692 if (!tok_is(tline, '(')) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004693 /*
4694 * This macro wasn't called with parameters: ignore
4695 * the call. (Behaviour borrowed from gnu cpp.)
4696 */
4697 goto not_a_macro;
4698 }
4699
4700 paren = 1;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004701 nparam = 1;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004702 brackets = 0;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004703 t = tline; /* tline points to leading ( */
4704
4705 while (paren) {
4706 t = t->next;
4707
4708 if (!t) {
4709 nasm_nonfatal("macro call expects terminating `)'");
4710 goto not_a_macro;
4711 }
4712
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004713 if (t->type != TOK_OTHER || t->len != 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004714 continue;
4715
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004716 switch (t->text[0]) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004717 case ',':
4718 if (!brackets)
4719 nparam++;
4720 break;
4721
4722 case '{':
4723 brackets++;
4724 break;
4725
4726 case '}':
4727 if (brackets > 0)
4728 brackets--;
4729 break;
4730
4731 case '(':
4732 if (!brackets)
4733 paren++;
4734 break;
4735
4736 case ')':
4737 if (!brackets)
4738 paren--;
4739 break;
4740
4741 default:
4742 break; /* Normal token */
4743 }
4744 }
4745
4746 /*
4747 * Look for a macro matching in both name and parameter count.
4748 * We already know any matches cannot be anywhere before the
4749 * current position of "m", so there is no reason to
4750 * backtrack.
4751 */
4752 while (1) {
4753 if (!m) {
4754 /*!
4755 *!macro-params-single [on] single-line macro calls with wrong parameter count
4756 *! warns about \i{single-line macros} being invoked
4757 *! with the wrong number of parameters.
4758 */
4759 nasm_warn(WARN_MACRO_PARAMS_SINGLE,
4760 "single-line macro `%s' exists, "
4761 "but not taking %d parameter%s",
4762 mname, nparam, (nparam == 1) ? "" : "s");
4763 goto not_a_macro;
4764 }
4765
4766 if (!mstrcmp(m->name, mname, m->casesense)) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004767 if (nparam == m->nparam)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004768 break; /* It's good */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004769 if (m->greedy && nparam >= m->nparam-1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004770 break; /* Also good */
4771 }
4772 m = m->next;
4773 }
4774 }
4775
4776 if (m->in_progress)
4777 goto not_a_macro;
4778
4779 /* Expand the macro */
4780 m->in_progress = true;
4781
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004782 if (nparam) {
4783 /* Extract parameters */
4784 Token **phead, **pep;
4785 int white = 0;
4786 int brackets = 0;
4787 int paren;
4788 bool bracketed = false;
4789 bool bad_bracket = false;
4790 enum sparmflags flags;
4791
4792 nparam = m->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004793 paren = 1;
4794 nasm_newn(params, nparam);
4795 i = 0;
4796 flags = m->params[i].flags;
4797 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004798 *pep = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004799
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004800 while (paren) {
4801 bool skip;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004802 char ch;
4803
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004804 tline = tline->next;
4805
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004806 if (!tline)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004807 nasm_nonfatal("macro call expects terminating `)'");
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004808
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004809 ch = 0;
4810 skip = false;
4811
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004812
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004813 switch (tline->type) {
4814 case TOK_OTHER:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004815 if (tline->len == 1)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004816 ch = tline->text[0];
4817 break;
4818
4819 case TOK_WHITESPACE:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004820 if (!(flags & SPARM_NOSTRIP)) {
4821 if (brackets || *phead)
4822 white++; /* Keep interior whitespace */
4823 skip = true;
4824 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004825 break;
4826
4827 default:
4828 break;
4829 }
4830
4831 switch (ch) {
4832 case ',':
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004833 if (!brackets && !(flags & SPARM_GREEDY)) {
4834 i++;
4835 nasm_assert(i < nparam);
4836 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004837 *pep = NULL;
4838 bracketed = false;
4839 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004840 flags = m->params[i].flags;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004841 }
4842 break;
4843
4844 case '{':
4845 if (!bracketed) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004846 bracketed = !*phead && !(flags & SPARM_NOSTRIP);
4847 skip = bracketed;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004848 }
4849 brackets++;
4850 break;
4851
4852 case '}':
4853 if (brackets > 0) {
4854 if (!--brackets)
4855 skip = bracketed;
4856 }
4857 break;
4858
4859 case '(':
4860 if (!brackets)
4861 paren++;
4862 break;
4863
4864 case ')':
4865 if (!brackets) {
4866 paren--;
4867 if (!paren) {
4868 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004869 i++; /* Found last argument */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004870 }
4871 }
4872 break;
4873
4874 default:
4875 break; /* Normal token */
4876 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004877
4878 if (!skip) {
4879 Token *t;
4880
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004881 bad_bracket |= bracketed && !brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004882
4883 if (white) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004884 *pep = t = new_White(NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004885 pep = &t->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004886 white = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004887 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004888 *pep = t = dup_Token(NULL, tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004889 pep = &t->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004890 }
4891 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004892
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004893 /*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004894 * Possible further processing of parameters. Note that the
4895 * ordering matters here.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004896 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004897 for (i = 0; i < nparam; i++) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004898 enum sparmflags flags = m->params[i].flags;
4899
4900 if (flags & SPARM_EVAL) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004901 /* Evaluate this parameter as a number */
4902 struct ppscan pps;
4903 struct tokenval tokval;
4904 expr *evalresult;
4905 Token *eval_param;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004906
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004907 pps.tptr = eval_param = expand_smacro_noreset(params[i]);
4908 pps.ntokens = -1;
4909 tokval.t_type = TOKEN_INVALID;
4910 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004911
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004912 free_tlist(eval_param);
4913 params[i] = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004914
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004915 if (!evalresult) {
4916 /* Nothing meaningful to do */
4917 } else if (tokval.t_type) {
4918 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
4919 } else if (!is_simple(evalresult)) {
4920 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
4921 } else {
4922 params[i] = make_tok_num(reloc_value(evalresult));
4923 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004924 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004925
4926 if (flags & SPARM_STR) {
4927 /* Convert expansion to a quoted string */
4928 char *arg;
4929 Token *qs;
4930
4931 qs = expand_smacro_noreset(params[i]);
4932 arg = detoken(qs, false);
4933 free_tlist(qs);
4934 params[i] = make_tok_qstr(arg);
4935 nasm_free(arg);
4936 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004937 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004938 }
4939
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004940 /* Note: we own the expansion this returns. */
4941 t = m->expand(m, params, nparam);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004942
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004943 tafter = tline->next; /* Skip past the macro call */
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004944 tline->next = NULL; /* Truncate list at the macro call end */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004945 tline = tafter;
4946
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004947 tup = NULL;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004948 cond_comma = false;
4949
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004950 while (t) {
4951 enum pp_token_type type = t->type;
4952 Token *tnext = t->next;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004953
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004954 switch (type) {
4955 case TOK_PREPROC_Q:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004956 delete_Token(t);
4957 t = dup_Token(tline, mstart);
4958 break;
4959
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004960 case TOK_PREPROC_QQ:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004961 {
4962 size_t len = strlen(m->name);
4963 char *p;
4964
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004965 nasm_free(t->text);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004966 t->type = mstart->type;
4967 if (t->type == TOK_PREPROC_ID) {
4968 const char *pep; /* prefix end pointer */
4969 size_t plen; /* prefix length */
4970
4971 get_ctx(mstart->text, &pep);
4972 plen = pep - mstart->text;
4973
4974 t->len = len + plen;
4975 t->text = p = nasm_malloc(t->len + 1);
4976 p = mempcpy(p, mstart->text, plen);
4977 } else {
4978 t->len = len;
4979 t->text = p = nasm_malloc(len + 1);
4980 }
4981 p = mempcpy(p, m->name, len);
4982 *p = '\0';
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004983 t->next = tline;
4984 break;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004985 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004986
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004987 case TOK_COND_COMMA:
4988 delete_Token(t);
4989 t = cond_comma ? new_Token(tline, TOK_OTHER, ",", 1) : NULL;
4990 break;
4991
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004992 case TOK_ID:
4993 case TOK_PREPROC_ID:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004994 {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004995 /*
4996 * Chain this into the target line *before* expanding,
4997 * that way we pick up any arguments to the new macro call,
4998 * if applicable.
4999 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005000 Token **tp = &t;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005001 t->next = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005002 expand_one_smacro(&tp);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005003 tline = *tp; /* First token left after any macro call */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005004 break;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005005 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005006 default:
5007 if (is_smac_param(t->type)) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005008 int param = smac_nparam(t->type);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005009 nasm_assert(!tup && param < nparam);
5010 delete_Token(t);
5011 t = NULL;
5012 tup = tnext;
5013 tnext = dup_tlist_reverse(params[param], NULL);
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005014 cond_comma = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005015 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005016 t->next = tline;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005017 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005018 }
5019
5020 if (t) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005021 Token *endt = tline;
5022
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005023 tline = t;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005024 while (!cond_comma && t && t != endt)
5025 cond_comma = t->type != TOK_WHITESPACE;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005026 }
5027
5028 if (tnext) {
5029 t = tnext;
5030 } else {
5031 t = tup;
5032 tup = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005033 }
5034 }
5035
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005036 **tpp = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005037 for (t = tline; t != tafter; t = t->next)
5038 *tpp = &t->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005039
5040 m->in_progress = false;
5041
5042 /* Don't do this until after expansion or we will clobber mname */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005043 free_tlist(mstart);
H. Peter Anvin322bee02019-08-10 01:38:06 -07005044 goto done;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005045
5046 /*
5047 * No macro expansion needed; roll back to mstart (if necessary)
H. Peter Anvin322bee02019-08-10 01:38:06 -07005048 * and then advance to the next input token. Note that this is
5049 * by far the common case!
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005050 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005051not_a_macro:
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005052 *tpp = &mstart->next;
H. Peter Anvin322bee02019-08-10 01:38:06 -07005053 m = NULL;
5054done:
5055 smacro_deadman.levels++;
5056 if (unlikely(params))
5057 free_tlist_array(params, nparam);
5058 return m;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005059}
5060
5061/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005062 * Expand all single-line macro calls made in the given line.
5063 * Return the expanded version of the line. The original is deemed
5064 * to be destroyed in the process. (In reality we'll just move
5065 * Tokens from input to output a lot of the time, rather than
5066 * actually bothering to destroy and replicate.)
5067 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005068static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005069{
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005070 smacro_deadman.total = nasm_limit[LIMIT_MACRO_TOKENS];
H. Peter Anvin322bee02019-08-10 01:38:06 -07005071 smacro_deadman.levels = nasm_limit[LIMIT_MACRO_LEVELS];
5072 smacro_deadman.triggered = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005073 return expand_smacro_noreset(tline);
5074}
5075
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005076static Token *expand_smacro_noreset(Token *org_tline)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005077{
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005078 Token *tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005079 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005080
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005081 if (!org_tline)
5082 return NULL; /* Empty input */
5083
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005084 /*
5085 * Trick: we should avoid changing the start token pointer since it can
5086 * be contained in "next" field of other token. Because of this
5087 * we allocate a copy of first token and work with it; at the end of
5088 * routine we copy it back
5089 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005090 tline = dup_Token(org_tline->next, org_tline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005091
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005092 /*
5093 * Pretend that we always end up doing expansion on the first pass;
5094 * that way %+ get processed. However, if we process %+ before the
5095 * first pass, we end up with things like MACRO %+ TAIL trying to
5096 * look up the macro "MACROTAIL", which we don't want.
5097 */
5098 expanded = true;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005099 while (true) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005100 static const struct tokseq_match tmatch[] = {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005101 {
5102 PP_CONCAT_MASK(TOK_ID) |
5103 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
5104 PP_CONCAT_MASK(TOK_ID) |
5105 PP_CONCAT_MASK(TOK_PREPROC_ID) |
5106 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
5107 }
5108 };
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005109 Token **tail = &tline;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005110
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005111 while (*tail) /* main token loop */
H. Peter Anvin322bee02019-08-10 01:38:06 -07005112 expanded |= !!expand_one_smacro(&tail);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005113
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005114 if (!expanded)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005115 break; /* Done! */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005116
5117 /*
5118 * Now scan the entire line and look for successive TOK_IDs
5119 * that resulted after expansion (they can't be produced by
5120 * tokenize()). The successive TOK_IDs should be concatenated.
5121 * Also we look for %+ tokens and concatenate the tokens
5122 * before and after them (without white spaces in between).
5123 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005124 if (!paste_tokens(&tline, tmatch, ARRAY_SIZE(tmatch), true))
5125 break; /* Done again! */
5126
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005127 expanded = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005128 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005129
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005130 if (tline) {
5131 *org_tline = *tline;
5132 /* since we just gave text to org_tline, don't free it */
5133 tline->text = NULL;
5134 delete_Token(tline);
5135 } else {
5136 /*
5137 * The expression expanded to empty line;
5138 * we can't return NULL because of the "trick" above.
5139 * Just set the line to a single WHITESPACE token.
5140 */
5141 nasm_zero(*org_tline);
5142 org_tline->type = TOK_WHITESPACE;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005143 }
5144
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005145 return org_tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005146}
5147
5148/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005149 * Similar to expand_smacro but used exclusively with macro identifiers
5150 * right before they are fetched in. The reason is that there can be
5151 * identifiers consisting of several subparts. We consider that if there
5152 * are more than one element forming the name, user wants a expansion,
5153 * otherwise it will be left as-is. Example:
5154 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005155 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005156 *
5157 * the identifier %$abc will be left as-is so that the handler for %define
5158 * will suck it and define the corresponding value. Other case:
5159 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005160 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005161 *
5162 * In this case user wants name to be expanded *before* %define starts
5163 * working, so we'll expand %$abc into something (if it has a value;
5164 * otherwise it will be left as-is) then concatenate all successive
5165 * PP_IDs into one.
5166 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005167static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005168{
5169 Token *cur, *oldnext = NULL;
5170
H. Peter Anvin734b1882002-04-30 21:01:08 +00005171 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005172 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005173
5174 cur = tline;
5175 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005176 (cur->next->type == TOK_ID ||
5177 cur->next->type == TOK_PREPROC_ID
5178 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005179 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005180
5181 /* If identifier consists of just one token, don't expand */
5182 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005183 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005184
H. Peter Anvine2c80182005-01-15 22:15:51 +00005185 if (cur) {
5186 oldnext = cur->next; /* Detach the tail past identifier */
5187 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005188 }
5189
H. Peter Anvin734b1882002-04-30 21:01:08 +00005190 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005191
H. Peter Anvine2c80182005-01-15 22:15:51 +00005192 if (cur) {
5193 /* expand_smacro possibly changhed tline; re-scan for EOL */
5194 cur = tline;
5195 while (cur && cur->next)
5196 cur = cur->next;
5197 if (cur)
5198 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005199 }
5200
5201 return tline;
5202}
5203
5204/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005205 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005206 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005207 * to check for an initial label - that's taken care of in
5208 * expand_mmacro - but must check numbers of parameters. Guaranteed
5209 * to be called with tline->type == TOK_ID, so the putative macro
5210 * name is easy to find.
5211 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005212static MMacro *is_mmacro(Token * tline, int *nparamp, Token ***params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005213{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005214 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005215 Token **params;
5216 int nparam;
5217
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005218 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005219
5220 /*
5221 * Efficiency: first we see if any macro exists with the given
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005222 * name which isn't already excluded by macro cycle removal.
5223 * (The cycle removal test here helps optimize the case of wrapping
5224 * instructions, and is cheap to do here.)
5225 *
5226 * If not, we can return NULL immediately. _Then_ we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005227 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005228 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005229 */
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005230 list_for_each(m, head) {
5231 if (!mstrcmp(m->name, tline->text, m->casesense) &&
5232 (m->in_progress != 1 || m->max_depth > 0))
5233 break; /* Found something that needs consideration */
5234 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005235 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005236 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005237
5238 /*
5239 * OK, we have a potential macro. Count and demarcate the
5240 * parameters.
5241 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00005242 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005243
5244 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005245 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005246 * structure that handles this number.
5247 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005248 while (m) {
5249 if (m->nparam_min <= nparam
5250 && (m->plus || nparam <= m->nparam_max)) {
5251 /*
5252 * This one is right. Just check if cycle removal
5253 * prohibits us using it before we actually celebrate...
5254 */
5255 if (m->in_progress > m->max_depth) {
5256 if (m->max_depth > 0) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08005257 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005258 m->max_depth);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005259 }
5260 nasm_free(params);
5261 return NULL;
5262 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005263 /*
5264 * It's right, and we can use it. Add its default
5265 * parameters to the end of our list if necessary.
5266 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005267 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005268 int newnparam = m->nparam_min + m->ndefs;
5269 params = nasm_realloc(params, sizeof(*params) * (newnparam+2));
5270 memcpy(&params[nparam+1], &m->defaults[nparam+1-m->nparam_min],
5271 (newnparam - nparam) * sizeof(*params));
5272 nparam = newnparam;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005273 }
5274 /*
5275 * If we've gone over the maximum parameter count (and
5276 * we're in Plus mode), ignore parameters beyond
5277 * nparam_max.
5278 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005279 if (m->plus && nparam > m->nparam_max)
5280 nparam = m->nparam_max;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005281
H. Peter Anvin (Intel)7eb18212019-08-20 16:24:46 -07005282 /*
5283 * If nparam was adjusted above, make sure the list is still
5284 * NULL-terminated.
5285 */
5286 params[nparam+1] = NULL;
5287
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005288 /* Done! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005289 *params_array = params;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005290 *nparamp = nparam;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005291 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005292 }
5293 /*
5294 * This one wasn't right: look for the next one with the
5295 * same name.
5296 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005297 list_for_each(m, m->next)
5298 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005299 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005300 }
5301
5302 /*
5303 * After all that, we didn't find one with the right number of
5304 * parameters. Issue a warning, and fail to expand the macro.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005305 *!
5306 *!macro-params-multi [on] multi-line macro calls with wrong parameter count
5307 *! warns about \i{multi-line macros} being invoked
5308 *! with the wrong number of parameters. See \k{mlmacover} for an
5309 *! example of why you might want to disable this warning.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005310 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005311 nasm_warn(WARN_MACRO_PARAMS_MULTI,
5312 "multi-line macro `%s' exists, but not taking %d parameter%s",
5313 tline->text, nparam, (nparam == 1) ? "" : "s");
H. Peter Anvin734b1882002-04-30 21:01:08 +00005314 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005315 return NULL;
5316}
5317
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005318
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005319#if 0
5320
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005321/*
5322 * Save MMacro invocation specific fields in
5323 * preparation for a recursive macro expansion
5324 */
5325static void push_mmacro(MMacro *m)
5326{
5327 MMacroInvocation *i;
5328
5329 i = nasm_malloc(sizeof(MMacroInvocation));
5330 i->prev = m->prev;
5331 i->params = m->params;
5332 i->iline = m->iline;
5333 i->nparam = m->nparam;
5334 i->rotate = m->rotate;
5335 i->paramlen = m->paramlen;
5336 i->unique = m->unique;
5337 i->condcnt = m->condcnt;
5338 m->prev = i;
5339}
5340
5341
5342/*
5343 * Restore MMacro invocation specific fields that were
5344 * saved during a previous recursive macro expansion
5345 */
5346static void pop_mmacro(MMacro *m)
5347{
5348 MMacroInvocation *i;
5349
5350 if (m->prev) {
5351 i = m->prev;
5352 m->prev = i->prev;
5353 m->params = i->params;
5354 m->iline = i->iline;
5355 m->nparam = i->nparam;
5356 m->rotate = i->rotate;
5357 m->paramlen = i->paramlen;
5358 m->unique = i->unique;
5359 m->condcnt = i->condcnt;
5360 nasm_free(i);
5361 }
5362}
5363
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005364#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005365
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005366/*
H. Peter Anvin (Intel)ffe89dd2019-08-20 16:06:36 -07005367 * List an mmacro call with arguments (-Lm option)
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005368 */
5369static void list_mmacro_call(const MMacro *m)
5370{
5371 const char prefix[] = " ;;; [macro] ";
5372 size_t namelen, size;
5373 char *buf, *p;
5374 unsigned int i;
5375 const Token *t;
5376
5377 namelen = strlen(m->iname);
5378 size = namelen + sizeof(prefix); /* Includes final null (from prefix) */
5379
5380 for (i = 1; i <= m->nparam; i++) {
5381 int j = 0;
5382 size += 3; /* Braces and space/comma */
5383 list_for_each(t, m->params[i]) {
5384 if (j++ >= m->paramlen[i])
5385 break;
5386 size += (t->type == TOK_WHITESPACE) ? 1 : t->len;
5387 }
5388 }
5389
5390 buf = p = nasm_malloc(size);
5391 p = mempcpy(p, prefix, sizeof(prefix) - 1);
5392 p = mempcpy(p, m->iname, namelen);
5393 *p++ = ' ';
5394
5395 for (i = 1; i <= m->nparam; i++) {
5396 int j = 0;
5397 *p++ = '{';
5398 list_for_each(t, m->params[i]) {
5399 if (j++ >= m->paramlen[i])
5400 break;
5401 if (!t->text) {
5402 if (t->type == TOK_WHITESPACE)
5403 *p++ = ' ';
5404 } else {
5405 p = mempcpy(p, t->text, t->len);
5406 }
5407 }
5408 *p++ = '}';
5409 *p++ = ',';
5410 }
5411
5412 *--p = '\0'; /* Replace last delimeter with null */
5413 lfmt->line(LIST_MACRO, -1, buf);
5414 nasm_free(buf);
5415}
5416
5417/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005418 * Expand the multi-line macro call made by the given line, if
5419 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005420 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005421 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005422static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005423{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005424 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005425 Token *label = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005426 bool dont_prepend = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005427 Token **params, *t, *tt;
5428 MMacro *m;
5429 Line *l, *ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005430 int i, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07005431 const char *mname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005432 int nparam = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005433
5434 t = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005435 t = skip_white(t);
5436 /* if (!tok_type(t, TOK_ID)) Lino 02/25/02 */
5437 if (!tok_type(t, TOK_ID) && !tok_type(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005438 return 0;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005439 m = is_mmacro(t, &nparam, &params);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005440 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005441 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07005442 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005443 Token *last;
5444 /*
5445 * We have an id which isn't a macro call. We'll assume
5446 * it might be a label; we'll also check to see if a
5447 * colon follows it. Then, if there's another id after
5448 * that lot, we'll check it again for macro-hood.
5449 */
5450 label = last = t;
5451 t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005452 if (tok_white(t))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005453 last = t, t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005454 if (tok_is(t, ':')) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005455 dont_prepend = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005456 last = t, t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005457 if (tok_white(t))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005458 last = t, t = t->next;
5459 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005460 if (!tok_type(t, TOK_ID) || !(m = is_mmacro(t, &nparam, &params)))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005461 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005462 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05005463 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005464 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005465 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005466
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005467 if (unlikely(mmacro_deadman.total >= nasm_limit[LIMIT_MMACROS] ||
5468 mmacro_deadman.levels >= nasm_limit[LIMIT_MACRO_LEVELS])) {
5469 if (!mmacro_deadman.triggered) {
5470 nasm_nonfatal("interminable multiline macro recursion");
5471 mmacro_deadman.triggered = true;
5472 }
5473 return 0;
5474 }
5475
5476 mmacro_deadman.total++;
5477 mmacro_deadman.levels++;
5478
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005479 /*
5480 * Fix up the parameters: this involves stripping leading and
5481 * trailing whitespace, then stripping braces if they are
5482 * present.
5483 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005484 nasm_newn(paramlen, nparam+1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005485
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005486 nasm_assert(params[nparam+1] == NULL);
5487
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005488 for (i = 1; (t = params[i]); i++) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005489 bool braced = false;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005490 int brace = 0;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005491 int white = 0;
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005492 bool comma = !m->plus || i < nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005493
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005494 t = skip_white(t);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005495 if (tok_is(t, '{')) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005496 t = t->next;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005497 brace = 1;
5498 braced = true;
5499 comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005500 }
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005501
5502 params[i] = t;
5503 for (; t; t = t->next) {
5504 if (tok_white(t)) {
5505 white++;
5506 continue;
5507 }
5508
5509 if (t->type == TOK_OTHER && t->len == 1) {
5510 switch (t->text[0]) {
5511 case ',':
5512 if (comma && !brace)
5513 goto endparam;
5514 break;
5515
5516 case '{':
5517 brace++;
5518 break;
5519
5520 case '}':
5521 brace--;
5522 if (braced && !brace) {
5523 paramlen[i] += white;
5524 goto endparam;
5525 }
5526 break;
5527
5528 default:
5529 break;
5530 }
5531 }
5532
5533 paramlen[i] += white + 1;
5534 white = 0;
5535 }
5536 endparam:
5537 ;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005538 }
5539
5540 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005541 * OK, we have a MMacro structure together with a set of
5542 * parameters. We must now go through the expansion and push
5543 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00005544 * parameter tokens and macro-local tokens doesn't get done
5545 * until the single-line macro substitution process; this is
5546 * because delaying them allows us to change the semantics
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005547 * later through %rotate and give the right semantics for
5548 * nested mmacros.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005549 *
5550 * First, push an end marker on to istk->expansion, mark this
5551 * macro as in progress, and set up its invocation-specific
5552 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005553 */
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005554 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005555 ll->next = istk->expansion;
5556 ll->finishes = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005557 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005558
5559 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005560 * Save the previous MMacro expansion in the case of
5561 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005562 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005563#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005564 if (m->max_depth && m->in_progress)
5565 push_mmacro(m);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005566#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005567
5568 m->in_progress ++;
5569 m->params = params;
5570 m->iline = tline;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005571 m->iname = nasm_strdup(mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005572 m->nparam = nparam;
5573 m->rotate = 0;
5574 m->paramlen = paramlen;
5575 m->unique = unique++;
5576 m->lineno = 0;
5577 m->condcnt = 0;
5578
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005579 m->mstk = istk->mstk;
5580 istk->mstk.mstk = istk->mstk.mmac = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005581
5582 list_for_each(l, m->expansion) {
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005583 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005584 ll->next = istk->expansion;
5585 istk->expansion = ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005586 ll->first = dup_tlist(l->first, NULL);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005587 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005588
5589 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005590 * If we had a label, and this macro definition does not include
5591 * a %00, push it on as the first line of, ot
H. Peter Anvineba20a72002-04-30 20:53:55 +00005592 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005593 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005594 if (label) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005595 /*
5596 * We had a label. If this macro contains an %00 parameter,
5597 * save the value as a special parameter (which is what it
5598 * is), otherwise push it as the first line of the macro
5599 * expansion.
5600 */
5601 if (m->capture_label) {
5602 params[0] = dup_Token(NULL, label);
5603 paramlen[0] = 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005604 free_tlist(startline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005605 } else {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005606 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005607 ll->finishes = NULL;
5608 ll->next = istk->expansion;
5609 istk->expansion = ll;
5610 ll->first = startline;
5611 if (!dont_prepend) {
5612 while (label->next)
5613 label = label->next;
5614 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005615 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005616 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005617 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005618
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07005619 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005620
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005621 if (list_option('m') && !m->nolist)
5622 list_mmacro_call(m);
5623
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005624 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005625}
5626
H. Peter Anvin130736c2016-02-17 20:27:41 -08005627/*
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005628 * This function decides if an error message should be suppressed.
5629 * It will never be called with a severity level of ERR_FATAL or
5630 * higher.
H. Peter Anvin130736c2016-02-17 20:27:41 -08005631 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005632static bool pp_suppress_error(errflags severity)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005633{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005634 /*
5635 * If we're in a dead branch of IF or something like it, ignore the error.
5636 * However, because %else etc are evaluated in the state context
5637 * of the previous branch, errors might get lost:
5638 * %if 0 ... %else trailing garbage ... %endif
5639 * So %else etc should set the ERR_PP_PRECOND flag.
5640 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005641 if (istk && istk->conds &&
H. Peter Anvin130736c2016-02-17 20:27:41 -08005642 ((severity & ERR_PP_PRECOND) ?
5643 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07005644 !emitting(istk->conds->state)))
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005645 return true;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005646
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005647 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005648}
5649
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005650static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005651stdmac_file(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005652{
5653 (void)s;
5654 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005655 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005656
5657 return make_tok_qstr(src_get_fname());
5658}
5659
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005660static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005661stdmac_line(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005662{
5663 (void)s;
5664 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005665 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005666
5667 return make_tok_num(src_get_linnum());
5668}
5669
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005670static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005671stdmac_bits(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005672{
5673 (void)s;
5674 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005675 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005676
5677 return make_tok_num(globalbits);
5678}
5679
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005680static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005681stdmac_ptr(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005682{
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005683 const Token *t;
Chang S. Baefea22692019-05-28 12:25:16 -07005684 static const Token ptr_word = { NULL, "word", 4, TOK_ID };
5685 static const Token ptr_dword = { NULL, "dword", 5, TOK_ID };
5686 static const Token ptr_qword = { NULL, "qword", 5, TOK_ID };
H. Peter Anvin8b262472019-02-26 14:00:54 -08005687
5688 (void)s;
5689 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005690 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005691
5692 switch (globalbits) {
5693 case 16:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005694 t = &ptr_word;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005695 break;
5696 case 32:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005697 t = &ptr_dword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005698 break;
5699 case 64:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005700 t = &ptr_qword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005701 break;
5702 default:
5703 panic();
5704 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005705
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005706 return dup_Token(NULL, t);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005707}
5708
H. Peter Anvin8b262472019-02-26 14:00:54 -08005709/* Add magic standard macros */
5710struct magic_macros {
5711 const char *name;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005712 int nparam;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005713 ExpandSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005714};
5715static const struct magic_macros magic_macros[] =
5716{
H. Peter Anvind2354082019-08-27 16:38:48 -07005717 { "__?FILE?__", 0, stdmac_file },
5718 { "__?LINE?__", 0, stdmac_line },
5719 { "__?BITS?__", 0, stdmac_bits },
5720 { "__?PTR?__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08005721 { NULL, 0, NULL }
5722};
5723
5724static void pp_add_magic_stdmac(void)
5725{
5726 const struct magic_macros *m;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005727 SMacro tmpl;
5728
5729 nasm_zero(tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005730
5731 for (m = magic_macros; m->name; m++) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005732 tmpl.nparam = m->nparam;
5733 tmpl.expand = m->func;
5734 define_smacro(m->name, true, NULL, &tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005735 }
5736}
5737
H. Peter Anvin734b1882002-04-30 21:01:08 +00005738static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005739pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005740{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005741 int apass;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005742 struct Include *inc;
H. Peter Anvin7383b402008-09-24 10:20:40 -07005743
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005744 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005745 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07005746 nested_mac_count = 0;
5747 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005748 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005749 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07005750 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005751 pp_mode = mode;
H. Peter Anvind2354082019-08-27 16:38:48 -07005752 do_aliases = true;
H. Peter Anvinf7606612016-07-13 14:23:48 -07005753
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07005754 if (!use_loaded)
5755 use_loaded = nasm_malloc(use_package_count * sizeof(bool));
5756 memset(use_loaded, 0, use_package_count * sizeof(bool));
5757
H. Peter Anvin6686de22019-08-10 05:33:14 -07005758 /* First set up the top level input file */
5759 nasm_new(istk);
5760 istk->fp = nasm_open_read(file, NF_TEXT);
5761 src_set(0, file);
5762 istk->lineinc = 1;
5763 if (!istk->fp)
5764 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
5765
5766 strlist_add(deplist, file);
5767
5768 /*
5769 * Set up the stdmac packages as a virtual include file,
5770 * indicated by a null file pointer.
5771 */
5772 nasm_new(inc);
5773 inc->next = istk;
5774 inc->fname = src_set_fname(NULL);
5775 inc->nolist = !list_option('b');
5776 istk = inc;
5777 lfmt->uplevel(LIST_INCLUDE, 0);
5778
H. Peter Anvin8b262472019-02-26 14:00:54 -08005779 pp_add_magic_stdmac();
5780
H. Peter Anvinf7606612016-07-13 14:23:48 -07005781 if (tasm_compatible_mode)
5782 pp_add_stdmac(nasm_stdmac_tasm);
5783
5784 pp_add_stdmac(nasm_stdmac_nasm);
5785 pp_add_stdmac(nasm_stdmac_version);
5786
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005787 if (extrastdmac)
5788 pp_add_stdmac(extrastdmac);
5789
H. Peter Anvinf7606612016-07-13 14:23:48 -07005790 stdmacpos = stdmacros[0];
5791 stdmacnext = &stdmacros[1];
5792
H. Peter Anvind2456592008-06-19 15:04:18 -07005793 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005794
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005795 /*
H. Peter Anvind2354082019-08-27 16:38:48 -07005796 * Define the __?PASS?__ macro. This is defined here unlike all the
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07005797 * other builtins, because it is special -- it varies between
5798 * passes -- but there is really no particular reason to make it
5799 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005800 *
5801 * 0 = dependencies only
5802 * 1 = preparatory passes
5803 * 2 = final pass
5804 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005805 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005806 switch (mode) {
5807 case PP_NORMAL:
5808 apass = pass_final() ? 2 : 1;
5809 break;
5810 case PP_DEPS:
5811 apass = 0;
5812 break;
5813 case PP_PREPROC:
5814 apass = 3;
5815 break;
5816 default:
5817 panic();
5818 }
5819
H. Peter Anvind2354082019-08-27 16:38:48 -07005820 define_smacro("__?PASS?__", true, make_tok_num(apass), NULL);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005821}
5822
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005823static void pp_init(void)
5824{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005825}
5826
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005827/*
5828 * Get a line of tokens. If we popped the macro expansion/include stack,
5829 * we return a pointer to the dummy token tok_pop; at that point if
5830 * istk is NULL then we have reached end of input;
5831 */
5832static Token tok_pop; /* Dummy token placeholder */
5833
5834static Token *pp_tokline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005835{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005836 while (true) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005837 Line *l = istk->expansion;
5838 Token *tline = NULL;
5839 Token *dtline;
5840
H. Peter Anvine2c80182005-01-15 22:15:51 +00005841 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005842 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005843 * buffer or from the input file.
5844 */
5845 tline = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005846 while (l && l->finishes) {
5847 MMacro *fm = l->finishes;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005848
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005849 if (!fm->name && fm->in_progress > 1) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005850 /*
5851 * This is a macro-end marker for a macro with no
5852 * name, which means it's not really a macro at all
5853 * but a %rep block, and the `in_progress' field is
5854 * more than 1, meaning that we still need to
5855 * repeat. (1 means the natural last repetition; 0
5856 * means termination by %exitrep.) We have
5857 * therefore expanded up to the %endrep, and must
5858 * push the whole block on to the expansion buffer
5859 * again. We don't bother to remove the macro-end
5860 * marker: we'd only have to generate another one
5861 * if we did.
5862 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005863 fm->in_progress--;
5864 list_for_each(l, fm->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005865 Token *t, *tt, **tail;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005866 Line *ll;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005867
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005868 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005869 ll->next = istk->expansion;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005870 tail = &ll->first;
5871
5872 list_for_each(t, l->first) {
5873 if (t->text || t->type == TOK_WHITESPACE) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005874 tt = *tail = dup_Token(NULL, t);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005875 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005876 }
5877 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005878 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005879 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005880 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005881 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005882 MMacro *m = istk->mstk.mstk;
5883
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005884 /*
5885 * Check whether a `%rep' was started and not ended
5886 * within this macro expansion. This can happen and
5887 * should be detected. It's a fatal error because
5888 * I'm too confused to work out how to recover
5889 * sensibly from it.
5890 */
5891 if (defining) {
5892 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005893 nasm_panic("defining with name in expansion");
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005894 else if (m->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005895 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005896 " expansion of macro `%s'", m->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005897 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005898
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005899 /*
5900 * FIXME: investigate the relationship at this point between
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005901 * istk->mstk.mstk and fm
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005902 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005903 istk->mstk = m->mstk;
5904 if (m->name) {
5905 /*
5906 * This was a real macro call, not a %rep, and
5907 * therefore the parameter information needs to
5908 * be freed and the iteration count/nesting
5909 * depth adjusted.
5910 */
5911
5912 if (!--mmacro_deadman.levels) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005913 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005914 * If all mmacro processing done,
5915 * clear all counters and the deadman
5916 * message trigger.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005917 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005918 nasm_zero(mmacro_deadman); /* Clear all counters */
Adam Majer91e72402017-07-25 10:42:01 +02005919 }
5920
Adam Majer91e72402017-07-25 10:42:01 +02005921#if 0
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005922 if (m->prev) {
5923 pop_mmacro(m);
5924 fm->in_progress --;
5925 } else
Adam Majer91e72402017-07-25 10:42:01 +02005926#endif
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005927 {
5928 nasm_free(m->params);
5929 free_tlist(m->iline);
5930 nasm_free(m->paramlen);
5931 fm->in_progress = 0;
5932 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005933 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005934
5935 /*
5936 * FIXME It is incorrect to always free_mmacro here.
5937 * It leads to usage-after-free.
5938 *
5939 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
5940 */
5941#if 0
5942 else
5943 free_mmacro(m);
5944#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005945 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005946 istk->expansion = l->next;
5947 nasm_free(l);
5948 lfmt->downlevel(LIST_MACRO);
5949 return &tok_pop;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005950 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005951
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005952 do { /* until we get a line we can use */
5953 char *line;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005954
5955 if (istk->expansion) { /* from a macro expansion */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005956 Line *l = istk->expansion;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005957 int32_t lineno;
5958
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005959 if (istk->mstk.mstk) {
5960 istk->mstk.mstk->lineno++;
5961 if (istk->mstk.mstk->fname)
5962 lineno = istk->mstk.mstk->lineno +
5963 istk->mstk.mstk->xline;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005964 else
5965 lineno = 0; /* Defined at init time or builtin */
5966 } else {
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005967 lineno = src_get_linnum();
H. Peter Anvin6686de22019-08-10 05:33:14 -07005968 }
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005969
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005970 tline = l->first;
5971 istk->expansion = l->next;
5972 nasm_free(l);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005973
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005974 line = detoken(tline, false);
H. Peter Anvin6686de22019-08-10 05:33:14 -07005975 if (!istk->nolist)
5976 lfmt->line(LIST_MACRO, lineno, line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005977 nasm_free(line);
5978 } else if ((line = read_line())) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005979 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005980 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005981 nasm_free(line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005982 } else {
5983 /*
5984 * The current file has ended; work down the istk
5985 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005986 Include *i = istk;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005987 if (i->fp)
5988 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005989 if (i->conds) {
5990 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07005991 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005992 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005993 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005994 if (i->next)
5995 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005996 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005997 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005998 nasm_free(i);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005999 return &tok_pop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006000 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006001 } while (0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006002
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006003 /*
6004 * We must expand MMacro parameters and MMacro-local labels
6005 * _before_ we plunge into directive processing, to cope
6006 * with things like `%define something %1' such as STRUC
6007 * uses. Unless we're _defining_ a MMacro, in which case
6008 * those tokens should be left alone to go into the
6009 * definition; and unless we're in a non-emitting
6010 * condition, in which case we don't want to meddle with
6011 * anything.
6012 */
6013 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006014 && !(istk->mstk.mstk && !istk->mstk.mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006015 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006016 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006017
H. Peter Anvine2c80182005-01-15 22:15:51 +00006018 /*
6019 * Check the line to see if it's a preprocessor directive.
6020 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006021 if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) {
6022 if (dtline)
6023 return dtline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006024 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006025 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006026 * We're defining a multi-line macro. We emit nothing
6027 * at all, and just
6028 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00006029 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006030 MMacro *mmac = defining->dstk.mmac;
6031
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006032 Line *l = nasm_malloc(sizeof(Line));
6033 l->next = defining->expansion;
6034 l->first = tline;
6035 l->finishes = NULL;
6036 defining->expansion = l;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006037
6038 /*
6039 * Remember if this mmacro expansion contains %00:
6040 * if it does, we will have to handle leading labels
6041 * specially.
6042 */
6043 if (mmac) {
6044 const Token *t;
6045 list_for_each(t, tline) {
6046 if (t->type == TOK_PREPROC_ID && !strcmp(t->text, "%00"))
6047 mmac->capture_label = true;
6048 }
6049 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006050 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006051 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006052 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00006053 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006054 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00006055 * directive so we keep our place correctly.
6056 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006057 free_tlist(tline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006058 } else if (istk->mstk.mstk && !istk->mstk.mstk->in_progress) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006059 /*
6060 * We're in a %rep block which has been terminated, so
6061 * we're walking through to the %endrep without
6062 * emitting anything. Emit nothing at all, not even a
6063 * blank line: when we emerge from the %rep block we'll
6064 * give a line-number directive so we keep our place
6065 * correctly.
6066 */
6067 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006068 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006069 tline = expand_smacro(tline);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006070 if (!expand_mmacro(tline))
6071 return tline;
6072 }
6073 }
6074}
6075
6076static char *pp_getline(void)
6077{
6078 char *line = NULL;
6079 Token *tline;
6080
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006081 while (true) {
6082 tline = pp_tokline();
6083 if (tline == &tok_pop) {
6084 /*
6085 * We popped the macro/include stack. If istk is empty,
6086 * we are at end of input, otherwise just loop back.
6087 */
6088 if (!istk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006089 break;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006090 } else {
6091 /*
6092 * De-tokenize the line and emit it.
6093 */
6094 line = detoken(tline, true);
6095 free_tlist(tline);
6096 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006097 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006098 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006099
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006100 if (list_option('e') && istk && !istk->nolist && line && line[0]) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006101 char *buf = nasm_strcat(" ;;; ", line);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006102 lfmt->line(LIST_MACRO, -1, buf);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006103 nasm_free(buf);
6104 }
6105
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006106 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006107}
6108
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006109static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006110{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006111 if (defining) {
6112 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006113 nasm_nonfatal("end of file while still defining macro `%s'",
6114 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006115 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006116 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006117 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006118
6119 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006120 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006121 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08006122
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006123 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006124 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07006125 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006126 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006127 Include *i = istk;
6128 istk = istk->next;
6129 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03006130 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006131 }
6132 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006133 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07006134 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006135}
6136
6137static void pp_cleanup_session(void)
6138{
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07006139 nasm_free(use_loaded);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006140 free_llist(predef);
6141 predef = NULL;
6142 delete_Blocks();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006143 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006144}
6145
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006146static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006147{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006148 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006149}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00006150
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006151static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006152{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006153 Token *inc, *space, *name;
6154 Line *l;
6155
H. Peter Anvin734b1882002-04-30 21:01:08 +00006156 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006157 space = new_White(name);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006158 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006159
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006160 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006161 l->next = predef;
6162 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006163 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006164 predef = l;
6165}
6166
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006167static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006168{
6169 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006170 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00006171 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006172
6173 equals = strchr(definition, '=');
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006174 space = new_White(NULL);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006175 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006176 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006177 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00006178 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006179 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006180 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006181
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006182 if (space->next->type != TOK_PREPROC_ID &&
6183 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08006184 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006185
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006186 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006187 l->next = predef;
6188 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006189 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006190 predef = l;
6191}
6192
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006193static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00006194{
6195 Token *def, *space;
6196 Line *l;
6197
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006198 space = new_White(NULL);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006199 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006200 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00006201
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006202 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00006203 l->next = predef;
6204 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006205 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00006206 predef = l;
6207}
6208
H. Peter Anvin05990342018-06-11 13:32:42 -07006209/* Insert an early preprocessor command that doesn't need special handling */
6210static void pp_pre_command(const char *what, char *string)
6211{
6212 char *cmd;
6213 Token *def, *space;
6214 Line *l;
6215
6216 def = tokenize(string);
6217 if (what) {
6218 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006219 space = new_White(def);
H. Peter Anvin05990342018-06-11 13:32:42 -07006220 def = new_Token(space, TOK_PREPROC_ID, cmd, 0);
6221 }
6222
6223 l = nasm_malloc(sizeof(Line));
6224 l->next = predef;
6225 l->first = def;
6226 l->finishes = NULL;
6227 predef = l;
6228}
6229
H. Peter Anvinf7606612016-07-13 14:23:48 -07006230static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006231{
H. Peter Anvinf7606612016-07-13 14:23:48 -07006232 macros_t **mp;
6233
6234 /* Find the end of the list and avoid duplicates */
6235 for (mp = stdmacros; *mp; mp++) {
6236 if (*mp == macros)
6237 return; /* Nothing to do */
6238 }
6239
6240 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
6241
6242 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00006243}
6244
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006245static void pp_extra_stdmac(macros_t *macros)
6246{
6247 extrastdmac = macros;
6248}
6249
H. Peter Anvin8b262472019-02-26 14:00:54 -08006250static Token *make_tok_num(int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006251{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04006252 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08006253 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
6254 return new_Token(NULL, TOK_NUMBER, numbuf, len);
6255}
6256
6257static Token *make_tok_qstr(const char *str)
6258{
6259 Token *t = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006260 t->text = nasm_quote_cstr(str, &t->len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006261 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006262}
6263
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006264static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07006265{
6266 if (!m)
6267 return;
6268
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006269 /* We need to print the mstk.mmac list in reverse order */
6270 pp_list_one_macro(m->mstk.mmac, severity);
H. Peter Anvin37368952016-05-09 14:10:32 -07006271
6272 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07006273 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08006274 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07006275 }
6276}
6277
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006278static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006279{
H. Peter Anvinddb29062018-12-11 00:06:29 -08006280 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006281
H. Peter Anvinddb29062018-12-11 00:06:29 -08006282 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
6283 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006284
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03006285 if (istk)
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006286 pp_list_one_macro(istk->mstk.mmac, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006287
H. Peter Anvinddb29062018-12-11 00:06:29 -08006288 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006289}
6290
H. Peter Anvine7469712016-02-18 02:20:59 -08006291const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006292 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006293 pp_reset,
6294 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006295 pp_cleanup_pass,
6296 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006297 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006298 pp_pre_define,
6299 pp_pre_undefine,
6300 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07006301 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006302 pp_include_path,
6303 pp_error_list_macros,
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006304 pp_suppress_error
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006305};