blob: 663e066bd0bbeea9af18910aad3529fd7b5a2201 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002 *
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07003 * Copyright 1996-2020 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
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -070079/*
80 * Preprocessor execution options that can be controlled by %pragma or
81 * other directives. This structure is initialized to zero on each
82 * pass; this *must* reflect the default initial state.
83 */
84static struct pp_opts {
85 bool noaliases;
86 bool sane_empty_expansion;
87} ppopt;
88
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000089typedef struct SMacro SMacro;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080090typedef struct MMacro MMacro;
91typedef struct MMacroInvocation MMacroInvocation;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000092typedef struct Context Context;
93typedef struct Token Token;
94typedef struct Line Line;
95typedef struct Include Include;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080096typedef struct Cond Cond;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000097
98/*
H. Peter Anvin8571f062019-09-23 16:40:03 -070099 * This is the internal form which we break input lines up into.
100 * Typically stored in linked lists.
101 *
102 * Note that `type' serves a double meaning: TOK_SMAC_START_PARAMS is
103 * not necessarily used as-is, but is also used to encode the number
104 * and expansion type of substituted parameter. So in the definition
105 *
106 * %define a(x,=y) ( (x) & ~(y) )
107 *
108 * the token representing `x' will have its type changed to
109 * tok_smac_param(0) but the one representing `y' will be
110 * tok_smac_param(1); see the accessor functions below.
111 *
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700112 * TOK_INTERNAL_STRING is a string which has been unquoted, but should
113 * be treated as if it was a quoted string. The code is free to change
114 * one into the other at will. TOK_NAKED_STRING is a text token which
115 * should be treated as a string, but which MUST NOT be turned into a
116 * quoted string. TOK_INTERNAL_STRINGs can contain any character,
117 * including NUL, but TOK_NAKED_STRING must be a valid C string.
H. Peter Anvin8571f062019-09-23 16:40:03 -0700118 */
119enum pp_token_type {
120 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT,
121 TOK_CORRUPT, /* Token text modified in an unsafe manner, now bogus */
122 TOK_BLOCK, /* Storage block pointer, not a real token */
123 TOK_ID,
124 TOK_PREPROC_ID, TOK_MMACRO_PARAM, TOK_LOCAL_SYMBOL,
125 TOK_LOCAL_MACRO, TOK_ENVIRON, TOK_STRING,
126 TOK_NUMBER, TOK_FLOAT, TOK_OTHER,
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700127 TOK_INTERNAL_STRING, TOK_NAKED_STRING,
H. Peter Anvin8571f062019-09-23 16:40:03 -0700128 TOK_PREPROC_Q, TOK_PREPROC_QQ,
129 TOK_PASTE, /* %+ */
130 TOK_COND_COMMA, /* %, */
131 TOK_INDIRECT, /* %[...] */
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700132 TOK_XDEF_PARAM, /* Used during %xdefine processing */
H. Peter Anvin8571f062019-09-23 16:40:03 -0700133 TOK_SMAC_START_PARAMS, /* MUST BE LAST IN THE LIST!!! */
134 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
135};
136
137static inline enum pp_token_type tok_smac_param(int param)
138{
139 return TOK_SMAC_START_PARAMS + param;
140}
141static int smac_nparam(enum pp_token_type toktype)
142{
143 return toktype - TOK_SMAC_START_PARAMS;
144}
145static bool is_smac_param(enum pp_token_type toktype)
146{
147 return toktype >= TOK_SMAC_START_PARAMS;
148}
149
150#define PP_CONCAT_MASK(x) (1U << (x))
151
152struct tokseq_match {
153 int mask_head;
154 int mask_tail;
155};
156
157/*
158 * This is tuned so struct Token should be 64 bytes on 64-bit
159 * systems and 32 bytes on 32-bit systems. It enables them
160 * to be nicely cache aligned, and the text to still be kept
161 * inline for nearly all tokens.
162 *
163 * We prohibit tokens of length > MAX_TEXT even though
164 * length here is an unsigned int; this avoids problems
165 * if the length is passed through an interface with type "int",
166 * and is absurdly large anyway.
167 *
168 * For the text mode, in pointer mode the pointer is stored at the end
169 * of the union and the pad field is cleared. This allows short tokens
170 * to be unconditionally tested for by only looking at the first text
171 * bytes and not examining the type or len fields.
172 */
173#define INLINE_TEXT (7*sizeof(char *)-sizeof(enum pp_token_type)-sizeof(unsigned int)-1)
174#define MAX_TEXT (INT_MAX-2)
175
176struct Token {
177 Token *next;
178 enum pp_token_type type;
179 unsigned int len;
180 union {
181 char a[INLINE_TEXT+1];
182 struct {
183 char pad[INLINE_TEXT+1 - sizeof(char *)];
184 char *ptr;
185 } p;
186 } text;
187};
188
189/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700190 * Note on the storage of both SMacro and MMacros: the hash table
191 * indexes them case-insensitively, and we then have to go through a
192 * linked list of potential case aliases (and, for MMacros, parameter
193 * ranges); this is to preserve the matching semantics of the earlier
194 * code. If the number of case aliases for a specific macro is a
195 * performance issue, you may want to reconsider your coding style.
196 */
197
198/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700199 * Function call tp obtain the expansion of an smacro
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700200 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700201typedef Token *(*ExpandSMacro)(const SMacro *s, Token **params, int nparams);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700202
203/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000204 * Store the definition of a single-line macro.
205 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700206enum sparmflags {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -0700207 SPARM_PLAIN = 0,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700208 SPARM_EVAL = 1, /* Evaluate as a numeric expression (=) */
209 SPARM_STR = 2, /* Convert to quoted string ($) */
210 SPARM_NOSTRIP = 4, /* Don't strip braces (!) */
211 SPARM_GREEDY = 8 /* Greedy final parameter (+) */
212};
213
214struct smac_param {
H. Peter Anvin8571f062019-09-23 16:40:03 -0700215 Token name;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700216 enum sparmflags flags;
217};
218
H. Peter Anvine2c80182005-01-15 22:15:51 +0000219struct SMacro {
H. Peter Anvin8b262472019-02-26 14:00:54 -0800220 SMacro *next; /* MUST BE FIRST - see free_smacro() */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800221 char *name;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700222 Token *expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700223 ExpandSMacro expand;
224 intorptr expandpvt;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700225 struct smac_param *params;
226 int nparam;
227 bool greedy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800228 bool casesense;
229 bool in_progress;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -0700230 bool alias; /* This is an alias macro */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000231};
232
233/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800234 * Store the definition of a multi-line macro. This is also used to
235 * store the interiors of `%rep...%endrep' blocks, which are
236 * effectively self-re-invoking multi-line macros which simply
237 * don't have a name or bother to appear in the hash tables. %rep
238 * blocks are signified by having a NULL `name' field.
239 *
240 * In a MMacro describing a `%rep' block, the `in_progress' field
241 * isn't merely boolean, but gives the number of repeats left to
242 * run.
243 *
244 * The `next' field is used for storing MMacros in hash tables; the
245 * `next_active' field is for stacking them on istk entries.
246 *
247 * When a MMacro is being expanded, `params', `iline', `nparam',
248 * `paramlen', `rotate' and `unique' are local to the invocation.
249 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700250
251/*
252 * Expansion stack. Note that .mmac can point back to the macro itself,
253 * whereas .mstk cannot.
254 */
255struct mstk {
256 MMacro *mstk; /* Any expansion, real macro or not */
257 MMacro *mmac; /* Highest level actual mmacro */
258};
259
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800260struct MMacro {
261 MMacro *next;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700262#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800263 MMacroInvocation *prev; /* previous invocation */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700264#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800265 char *name;
266 int nparam_min, nparam_max;
267 bool casesense;
268 bool plus; /* is the last parameter greedy? */
269 bool nolist; /* is this macro listing-inhibited? */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700270 bool capture_label; /* macro definition has %00; capture label */
271 int32_t in_progress; /* is this macro currently being expanded? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800272 int32_t max_depth; /* maximum number of recursive expansions allowed */
273 Token *dlist; /* All defaults as one list */
274 Token **defaults; /* Parameter default pointers */
275 int ndefs; /* number of default parameters */
276 Line *expansion;
277
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700278 struct mstk mstk; /* Macro expansion stack */
279 struct mstk dstk; /* Macro definitions stack */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800280 Token **params; /* actual parameters */
281 Token *iline; /* invocation line */
282 unsigned int nparam, rotate;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700283 char *iname; /* name invoked as */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800284 int *paramlen;
285 uint64_t unique;
286 int lineno; /* Current line number on expansion */
287 uint64_t condcnt; /* number of if blocks... */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700288
H. Peter Anvin274cda82016-05-10 02:56:29 -0700289 const char *fname; /* File where defined */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700290 int32_t xline; /* First line in macro */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800291};
292
293
294/* Store the definition of a multi-line macro, as defined in a
295 * previous recursive macro expansion.
296 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700297#if 0
298
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800299struct MMacroInvocation {
300 MMacroInvocation *prev; /* previous invocation */
301 Token **params; /* actual parameters */
302 Token *iline; /* invocation line */
303 unsigned int nparam, rotate;
304 int *paramlen;
305 uint64_t unique;
306 uint64_t condcnt;
307};
308
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700309#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800310
311/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000312 * The context stack is composed of a linked list of these.
313 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000314struct Context {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800315 Context *next;
H. Peter Anvin8571f062019-09-23 16:40:03 -0700316 const char *name;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800317 struct hash_table localmac;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -0700318 uint64_t number;
319 unsigned int depth;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000320};
321
H. Peter Anvin8571f062019-09-23 16:40:03 -0700322
323static inline const char *tok_text(const struct Token *t)
324{
325 return (t->len <= INLINE_TEXT) ? t->text.a : t->text.p.ptr;
326}
327
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000328/*
H. Peter Anvin8571f062019-09-23 16:40:03 -0700329 * Returns a mutable pointer to the text buffer. The text can be changed,
330 * but the length MUST NOT CHANGE, in either direction; nor is it permitted
331 * to pad with null characters to create an artificially shorter string.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000332 */
H. Peter Anvin8571f062019-09-23 16:40:03 -0700333static inline char *tok_text_buf(struct Token *t)
H. Peter Anvin8b262472019-02-26 14:00:54 -0800334{
H. Peter Anvin8571f062019-09-23 16:40:03 -0700335 return (t->len <= INLINE_TEXT) ? t->text.a : t->text.p.ptr;
H. Peter Anvin8b262472019-02-26 14:00:54 -0800336}
337
H. Peter Anvin8571f062019-09-23 16:40:03 -0700338static inline unsigned int tok_check_len(size_t len)
339{
340 if (unlikely(len > MAX_TEXT))
341 nasm_fatal("impossibly large token");
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400342
H. Peter Anvin8571f062019-09-23 16:40:03 -0700343 return len;
344}
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400345
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700346static inline bool tok_text_match(const struct Token *a, const struct Token *b)
347{
348 return a->len == b->len && !memcmp(tok_text(a), tok_text(b), a->len);
349}
350
351static inline bool tok_match(const struct Token *a, const struct Token *b)
352{
353 return a->type == b->type && tok_text_match(a, b);
354}
355
H. Peter Anvin8571f062019-09-23 16:40:03 -0700356/* strlen() variant useful for set_text() and its variants */
357static size_t tok_strlen(const char *str)
358{
359 return strnlen(str, MAX_TEXT+1);
360}
361
362/*
363 * Set the text field to a copy of the given string; the length if
364 * not given should be obtained with tok_strlen().
365 */
366static Token *set_text(struct Token *t, const char *text, size_t len)
367{
368 char *textp;
369
370 if (t->len > INLINE_TEXT)
371 nasm_free(t->text.p.ptr);
372
H. Peter Anvin (Intel)00335e42020-06-14 19:49:19 -0700373 nasm_zero(t->text);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700374
H. Peter Anvin (Intel)00335e42020-06-14 19:49:19 -0700375 t->len = len = tok_check_len(len);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700376 textp = (len > INLINE_TEXT)
377 ? (t->text.p.ptr = nasm_malloc(len+1)) : t->text.a;
H. Peter Anvin (Intel)00335e42020-06-14 19:49:19 -0700378 memcpy(textp, text, len);
379 textp[len] = '\0';
H. Peter Anvin8571f062019-09-23 16:40:03 -0700380 return t;
381}
382
383/*
384 * Set the text field to the existing pre-allocated string, either
385 * taking over or freeing the allocation in the process.
386 */
H. Peter Anvin (Intel)00335e42020-06-14 19:49:19 -0700387static Token *set_text_free(struct Token *t, char *text, size_t len)
H. Peter Anvin8571f062019-09-23 16:40:03 -0700388{
389 if (t->len > INLINE_TEXT)
390 nasm_free(t->text.p.ptr);
391
H. Peter Anvin (Intel)00335e42020-06-14 19:49:19 -0700392 nasm_zero(t->text);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700393
H. Peter Anvin (Intel)00335e42020-06-14 19:49:19 -0700394 t->len = len = tok_check_len(len);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700395 if (len > INLINE_TEXT) {
396 t->text.p.ptr = text;
H. Peter Anvin (Intel)00335e42020-06-14 19:49:19 -0700397 text[len] = '\0';
H. Peter Anvin8571f062019-09-23 16:40:03 -0700398 } else {
H. Peter Anvin (Intel)00335e42020-06-14 19:49:19 -0700399 memcpy(t->text.a, text, len);
400 t->text.a[len] = '\0';
H. Peter Anvin8571f062019-09-23 16:40:03 -0700401 nasm_free(text);
402 }
403
404 return t;
405}
406
407/*
408 * Allocate a new buffer containing a copy of the text field
409 * of the token.
410 */
411static char *dup_text(const struct Token *t)
412{
413 size_t size = t->len + 1;
414 char *p = nasm_malloc(size);
415
416 return memcpy(p, tok_text(t), size);
417}
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000418
419/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800420 * Multi-line macro definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000421 * these, which is essentially a container to allow several linked
422 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700423 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000424 * Note that in this module, linked lists are treated as stacks
425 * wherever possible. For this reason, Lines are _pushed_ on to the
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800426 * `expansion' field in MMacro structures, so that the linked list,
427 * if walked, would give the macro lines in reverse order; this
428 * means that we can walk the list when expanding a macro, and thus
429 * push the lines on to the `expansion' field in _istk_ in reverse
430 * order (so that when popped back off they are in the right
431 * order). It may seem cockeyed, and it relies on my design having
432 * an even number of steps in, but it works...
433 *
434 * Some of these structures, rather than being actual lines, are
435 * markers delimiting the end of the expansion of a given macro.
436 * This is for use in the cycle-tracking and %rep-handling code.
437 * Such structures have `finishes' non-NULL, and `first' NULL. All
438 * others have `finishes' NULL, but `first' may still be NULL if
439 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000440 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000441struct Line {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800442 Line *next;
443 MMacro *finishes;
444 Token *first;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500445};
446
447/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000448 * To handle an arbitrary level of file inclusion, we maintain a
449 * stack (ie linked list) of these things.
450 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000451struct Include {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800452 Include *next;
453 FILE *fp;
454 Cond *conds;
455 Line *expansion;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700456 const char *fname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700457 struct mstk mstk;
H. Peter Anvin6686de22019-08-10 05:33:14 -0700458 int lineno, lineinc;
459 bool nolist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000460};
461
462/*
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700463 * File real name hash, so we don't have to re-search the include
464 * path for every pass (and potentially more than that if a file
465 * is used more than once.)
466 */
467struct hash_table FileHash;
468
469/*
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -0700470 * Counters to trap on insane macro recursion or processing.
471 * Note: for smacros these count *down*, for mmacros they count *up*.
472 */
473struct deadman {
474 int64_t total; /* Total number of macros/tokens */
475 int64_t levels; /* Descent depth across all macros */
476 bool triggered; /* Already triggered, no need for error msg */
477};
478
479static struct deadman smacro_deadman, mmacro_deadman;
480
481/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000482 * Conditional assembly: we maintain a separate stack of these for
483 * each level of file inclusion. (The only reason we keep the
484 * stacks separate is to ensure that a stray `%endif' in a file
485 * included from within the true branch of a `%if' won't terminate
486 * it and cause confusion: instead, rightly, it'll cause an error.)
487 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -0700488enum cond_state {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000489 /*
490 * These states are for use just after %if or %elif: IF_TRUE
491 * means the condition has evaluated to truth so we are
492 * currently emitting, whereas IF_FALSE means we are not
493 * currently emitting but will start doing so if a %else comes
494 * up. In these states, all directives are admissible: %elif,
495 * %else and %endif. (And of course %if.)
496 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800497 COND_IF_TRUE, COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000498 /*
499 * These states come up after a %else: ELSE_TRUE means we're
500 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
501 * any %elif or %else will cause an error.
502 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800503 COND_ELSE_TRUE, COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000504 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200505 * These states mean that we're not emitting now, and also that
506 * nothing until %endif will be emitted at all. COND_DONE is
507 * used when we've had our moment of emission
508 * and have now started seeing %elifs. COND_NEVER is used when
509 * the condition construct in question is contained within a
510 * non-emitting branch of a larger condition construct,
511 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000512 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800513 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000514};
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -0700515struct Cond {
516 Cond *next;
517 enum cond_state state;
518};
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800519#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000520
H. Peter Anvin70653092007-10-19 14:42:29 -0700521/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000522 * These defines are used as the possible return values for do_directive
523 */
524#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300525#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000526
Keith Kanios852f1ee2009-07-12 00:19:55 -0500527/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000528 * Condition codes. Note that we use c_ prefix not C_ because C_ is
529 * used in nasm.h for the "real" condition codes. At _this_ level,
530 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
531 * ones, so we need a different enum...
532 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700533static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000534 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
535 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000536 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000537};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700538enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000539 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
540 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 -0700541 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
542 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000543};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700544static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000545 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
546 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 +0000547 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000548};
549
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800550/*
551 * Directive names.
552 */
553/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
554static int is_condition(enum preproc_token arg)
555{
556 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
557}
558
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000559/* For TASM compatibility we need to be able to recognise TASM compatible
560 * conditional compilation directives. Using the NASM pre-processor does
561 * not work, so we look for them specifically from the following list and
562 * then jam in the equivalent NASM directive into the input stream.
563 */
564
H. Peter Anvine2c80182005-01-15 22:15:51 +0000565enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000566 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
567 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
568};
569
H. Peter Anvin476d2862007-10-02 22:04:15 -0700570static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000571 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
572 "ifndef", "include", "local"
573};
574
575static int StackSize = 4;
H. Peter Anvin6c8b2be2016-05-24 23:46:50 -0700576static const char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000577static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800578static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000579
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000580static Context *cstk;
581static Include *istk;
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300582static const struct strlist *ipath_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000583
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300584static struct strlist *deplist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000585
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300586static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000587
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800588static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700589static bool do_predef;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800590static enum preproc_mode pp_mode;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000591
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000592/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800593 * The current set of multi-line macros we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000594 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800595static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000596
597/*
598 * The current set of single-line macros we have defined.
599 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700600static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000601
602/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800603 * The multi-line macro we are currently defining, or the %rep
604 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000605 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800606static MMacro *defining;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000607
Charles Crayned4200be2008-07-12 16:42:33 -0700608static uint64_t nested_mac_count;
609static uint64_t nested_rep_count;
610
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000611/*
612 * The number of macro parameters to allocate space for at a time.
613 */
614#define PARAM_DELTA 16
615
616/*
H. Peter Anvinf7606612016-07-13 14:23:48 -0700617 * The standard macro set: defined in macros.c in a set of arrays.
618 * This gives our position in any macro set, while we are processing it.
619 * The stdmacset is an array of such macro sets.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000620 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700621static macros_t *stdmacpos;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700622static macros_t **stdmacnext;
623static macros_t *stdmacros[8];
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +0300624static macros_t *extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000625
626/*
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -0700627 * Map of which %use packages have been loaded
628 */
629static bool *use_loaded;
630
631/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000632 * Forward declarations.
633 */
H. Peter Anvinf7606612016-07-13 14:23:48 -0700634static void pp_add_stdmac(macros_t *macros);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000635static Token *expand_mmac_params(Token * tline);
636static Token *expand_smacro(Token * tline);
637static Token *expand_id(Token * tline);
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +0400638static Context *get_ctx(const char *name, const char **namep);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700639static Token *make_tok_num(Token *next, int64_t val);
640static Token *make_tok_qstr(Token *next, const char *str);
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -0700641static Token *make_tok_qstr_len(Token *next, const char *str, size_t len);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700642static Token *make_tok_char(Token *next, char op);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700643static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700644 const char *text, size_t txtlen);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700645static Token *new_Token_free(Token * next, enum pp_token_type type,
646 char *text, size_t txtlen);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700647static Token *dup_Token(Token *next, const Token *src);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700648static Token *new_White(Token *next);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700649static Token *delete_Token(Token *t);
650static Token *steal_Token(Token *dst, Token *src);
H. Peter Anvindd88aa92019-09-12 19:39:48 -0700651static const struct use_package *
H. Peter Anvin8571f062019-09-23 16:40:03 -0700652get_use_pkg(Token *t, const char *dname, const char **name);
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -0700653static void mark_smac_params(Token *tline, const SMacro *tmpl,
654 enum pp_token_type type);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000655
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700656/* Safe test for token type, false on x == NULL */
657static inline bool tok_type(const Token *x, enum pp_token_type t)
658{
659 return x && x->type == t;
660}
661
662/* Whitespace token? */
663static inline bool tok_white(const Token *x)
664{
665 return tok_type(x, TOK_WHITESPACE);
666}
667
668/* Skip past any whitespace */
669static inline Token *skip_white(Token *x)
670{
671 while (tok_white(x))
672 x = x->next;
673
674 return x;
675}
676
677/* Delete any whitespace */
678static Token *zap_white(Token *x)
679{
680 while (tok_white(x))
681 x = delete_Token(x);
682
683 return x;
684}
685
H. Peter Anvin8571f062019-09-23 16:40:03 -0700686/*
687 * Single special character tests. The use of & rather than && is intentional; it
688 * tells the compiler that it is safe to access text.a[1] unconditionally; hopefully
689 * a smart compiler should turn it into a 16-bit memory reference.
690 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700691static inline bool tok_is(const Token *x, char c)
692{
H. Peter Anvin8571f062019-09-23 16:40:03 -0700693 return x && ((x->text.a[0] == c) & !x->text.a[1]);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700694}
695
696/* True if any other kind of token that "c", but not NULL */
697static inline bool tok_isnt(const Token *x, char c)
698{
H. Peter Anvin8571f062019-09-23 16:40:03 -0700699 return x && !((x->text.a[0] == c) & !x->text.a[1]);
700}
701
702/*
703 * Unquote a token if it is a string, and set its type to
704 * TOK_INTERNAL_STRING.
705 */
706static const char *unquote_token(Token *t)
707{
708 if (t->type != TOK_STRING)
709 return tok_text(t);
710
711 t->type = TOK_INTERNAL_STRING;
712
713 if (t->len > INLINE_TEXT) {
714 char *p = t->text.p.ptr;
715
716 t->len = nasm_unquote(p, NULL);
717
718 if (t->len <= INLINE_TEXT) {
719 nasm_zero(t->text.a);
720 memcpy(t->text.a, p, t->len);
721 nasm_free(p);
722 return t->text.a;
723 } else {
724 return p;
725 }
726 } else {
727 t->len = nasm_unquote(t->text.a, NULL);
728 return t->text.a;
729 }
730}
731
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700732/*
733 * Same as unquote_token(), but error out if the resulting string
734 * contains unacceptable control characters.
735 */
H. Peter Anvin8571f062019-09-23 16:40:03 -0700736static const char *unquote_token_cstr(Token *t)
737{
738 if (t->type != TOK_STRING)
739 return tok_text(t);
740
741 t->type = TOK_INTERNAL_STRING;
742
743 if (t->len > INLINE_TEXT) {
744 char *p = t->text.p.ptr;
745
746 t->len = nasm_unquote_cstr(p, NULL);
747
748 if (t->len <= INLINE_TEXT) {
749 nasm_zero(t->text.a);
750 memcpy(t->text.a, p, t->len);
751 nasm_free(p);
752 return t->text.a;
753 } else {
754 return p;
755 }
756 } else {
757 t->len = nasm_unquote_cstr(t->text.a, NULL);
758 return t->text.a;
759 }
760}
761
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700762/*
763 * Convert a TOK_INTERNAL_STRING token to a quoted
764 * TOK_STRING tokens.
765 */
766static Token *quote_any_token(Token *t);
767static inline Token *quote_token(Token *t)
768{
769 if (likely(!tok_is(t, TOK_INTERNAL_STRING)))
770 return t;
771
772 return quote_any_token(t);
773}
774
775/*
776 * Convert *any* kind of token to a quoted
777 * TOK_STRING token.
778 */
779static Token *quote_any_token(Token *t)
H. Peter Anvin8571f062019-09-23 16:40:03 -0700780{
781 size_t len;
782 char *p;
783
784 p = nasm_quote(tok_text(t), &len);
785 t->type = TOK_STRING;
786 return set_text_free(t, p, len);
787}
788
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400789/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700790 * In-place reverse a list of tokens.
791 */
792static Token *reverse_tokens(Token *t)
793{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800794 Token *prev = NULL;
795 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700796
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800797 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400798 next = t->next;
799 t->next = prev;
800 prev = t;
801 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800802 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700803
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800804 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700805}
806
807/*
H. Peter Anvin8571f062019-09-23 16:40:03 -0700808 * getenv() variant operating on an input token
809 */
810static const char *pp_getenv(const Token *t, bool warn)
811{
812 const char *txt = tok_text(t);
813 const char *v;
814 char *buf = NULL;
815 bool is_string = false;
816
817 if (!t)
818 return NULL;
819
820 switch (t->type) {
821 case TOK_ENVIRON:
822 txt += 2; /* Skip leading %! */
823 is_string = nasm_isquote(*txt);
824 break;
825
826 case TOK_STRING:
827 is_string = true;
828 break;
829
830 case TOK_INTERNAL_STRING:
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700831 case TOK_NAKED_STRING:
H. Peter Anvin8571f062019-09-23 16:40:03 -0700832 case TOK_ID:
833 is_string = false;
834 break;
835
836 default:
837 return NULL;
838 }
839
840 if (is_string) {
841 buf = nasm_strdup(txt);
842 nasm_unquote_cstr(buf, NULL);
843 txt = buf;
844 }
845
846 v = getenv(txt);
847 if (warn && !v) {
848 /*!
849 *!environment [on] nonexistent environment variable
850 *! warns if a nonexistent environment variable
851 *! is accessed using the \c{%!} preprocessor
852 *! construct (see \k{getenv}.) Such environment
853 *! variables are treated as empty (with this
854 *! warning issued) starting in NASM 2.15;
855 *! earlier versions of NASM would treat this as
856 *! an error.
857 */
858 nasm_warn(WARN_ENVIRONMENT, "nonexistent environment variable `%s'", txt);
859 v = "";
860 }
861
862 if (buf)
863 nasm_free(buf);
864
865 return v;
866}
867
868/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300869 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000870 * front of them. We do it here because I could not find any other
871 * place to do it for the moment, and it is a hack (ideally it would
872 * be nice to be able to use the NASM pre-processor to do it).
873 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000874static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000875{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000876 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400877 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000878
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400879 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000880
881 /* Binary search for the directive name */
882 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400883 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400884 q = nasm_skip_word(p);
885 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000886 if (len) {
887 oldchar = p[len];
888 p[len] = 0;
889 while (j - i > 1) {
890 k = (j + i) / 2;
891 m = nasm_stricmp(p, tasm_directives[k]);
892 if (m == 0) {
893 /* We have found a directive, so jam a % in front of it
894 * so that NASM will then recognise it as one if it's own.
895 */
896 p[len] = oldchar;
897 len = strlen(p);
898 oldline = line;
899 line = nasm_malloc(len + 2);
900 line[0] = '%';
901 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700902 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300903 * NASM does not recognise IFDIFI, so we convert
904 * it to %if 0. This is not used in NASM
905 * compatible code, but does need to parse for the
906 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000907 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700908 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000909 } else {
910 memcpy(line + 1, p, len + 1);
911 }
912 nasm_free(oldline);
913 return line;
914 } else if (m < 0) {
915 j = k;
916 } else
917 i = k;
918 }
919 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000920 }
921 return line;
922}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000923
H. Peter Anvin76690a12002-04-30 20:52:49 +0000924/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000925 * The pre-preprocessing stage... This function translates line
926 * number indications as they emerge from GNU cpp (`# lineno "file"
927 * flags') into NASM preprocessor line number indications (`%line
928 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000929 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000930static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000931{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000932 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000933 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000934
H. Peter Anvine2c80182005-01-15 22:15:51 +0000935 if (line[0] == '#' && line[1] == ' ') {
936 oldline = line;
937 fname = oldline + 2;
938 lineno = atoi(fname);
939 fname += strspn(fname, "0123456789 ");
940 if (*fname == '"')
941 fname++;
942 fnlen = strcspn(fname, "\"");
943 line = nasm_malloc(20 + fnlen);
944 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
945 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000946 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000947 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000948 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000949 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000950}
951
952/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000953 * Free a linked list of tokens.
954 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000955static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000956{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400957 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000958 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000959}
960
961/*
962 * Free a linked list of lines.
963 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000964static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000965{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400966 Line *l, *tmp;
967 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000968 free_tlist(l->first);
969 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000970 }
971}
972
973/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700974 * Free an array of linked lists of tokens
975 */
976static void free_tlist_array(Token **array, size_t nlists)
977{
978 Token **listp = array;
979
980 while (nlists--)
981 free_tlist(*listp++);
982
983 nasm_free(array);
984}
985
986/*
987 * Duplicate a linked list of tokens.
988 */
989static Token *dup_tlist(const Token *list, Token ***tailp)
990{
991 Token *newlist = NULL;
992 Token **tailpp = &newlist;
993 const Token *t;
994
995 list_for_each(t, list) {
996 Token *nt;
997 *tailpp = nt = dup_Token(NULL, t);
998 tailpp = &nt->next;
999 }
1000
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001001 if (tailp) {
1002 **tailp = newlist;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07001003 *tailp = tailpp;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001004 }
1005
1006 return newlist;
1007}
1008
1009/*
1010 * Duplicate a linked list of tokens with a maximum count
1011 */
1012static Token *dup_tlistn(const Token *list, size_t cnt, Token ***tailp)
1013{
1014 Token *newlist = NULL;
1015 Token **tailpp = &newlist;
1016 const Token *t;
1017
1018 list_for_each(t, list) {
1019 Token *nt;
1020 if (!cnt--)
1021 break;
1022 *tailpp = nt = dup_Token(NULL, t);
1023 tailpp = &nt->next;
1024 }
1025
1026 if (tailp) {
1027 **tailp = newlist;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07001028 if (newlist)
1029 *tailp = tailpp;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001030 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07001031
1032 return newlist;
1033}
1034
1035/*
1036 * Duplicate a linked list of tokens in reverse order
1037 */
1038static Token *dup_tlist_reverse(const Token *list, Token *tail)
1039{
1040 const Token *t;
1041
1042 list_for_each(t, list)
1043 tail = dup_Token(tail, t);
1044
1045 return tail;
1046}
1047
1048/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001049 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +00001050 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001051static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001052{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001053 nasm_free(m->name);
1054 free_tlist(m->dlist);
1055 nasm_free(m->defaults);
1056 free_llist(m->expansion);
1057 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001058}
1059
1060/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001061 * Clear or free an SMacro
H. Peter Anvin8b262472019-02-26 14:00:54 -08001062 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001063static void free_smacro_members(SMacro *s)
H. Peter Anvin8b262472019-02-26 14:00:54 -08001064{
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07001065 if (s->params) {
1066 int i;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001067 for (i = 0; i < s->nparam; i++) {
1068 if (s->params[i].name.len > INLINE_TEXT)
1069 nasm_free(s->params[i].name.text.p.ptr);
1070 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07001071 nasm_free(s->params);
1072 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08001073 nasm_free(s->name);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001074 free_tlist(s->expansion);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001075}
1076
1077static void clear_smacro(SMacro *s)
1078{
1079 free_smacro_members(s);
1080 /* Wipe everything except the next pointer */
1081 memset(&s->next + 1, 0, sizeof *s - sizeof s->next);
1082}
1083
1084/*
1085 * Free an SMacro
1086 */
1087static void free_smacro(SMacro *s)
1088{
1089 free_smacro_members(s);
1090 nasm_free(s);
H. Peter Anvin8b262472019-02-26 14:00:54 -08001091}
1092
1093/*
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07001094 * Free all currently defined macros, and free the hash tables if empty
H. Peter Anvin97a23472007-09-16 17:57:25 -07001095 */
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07001096enum clear_what {
1097 CLEAR_NONE = 0,
1098 CLEAR_DEFINE = 1, /* Clear smacros */
1099 CLEAR_DEFALIAS = 2, /* Clear smacro aliases */
1100 CLEAR_ALLDEFINE = CLEAR_DEFINE|CLEAR_DEFALIAS,
1101 CLEAR_MMACRO = 4,
1102 CLEAR_ALL = CLEAR_ALLDEFINE|CLEAR_MMACRO
1103};
1104
1105static void clear_smacro_table(struct hash_table *smt, enum clear_what what)
H. Peter Anvin97a23472007-09-16 17:57:25 -07001106{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001107 struct hash_iterator it;
1108 const struct hash_node *np;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07001109 bool empty = true;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001110
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07001111 /*
1112 * Walk the hash table and clear out anything we don't want
1113 */
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001114 hash_for_each(smt, it, np) {
1115 SMacro *tmp;
1116 SMacro *s = np->data;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07001117 SMacro **head = (SMacro **)&np->data;
1118
1119 list_for_each_safe(s, tmp, s) {
1120 if (what & ((enum clear_what)s->alias + 1)) {
1121 *head = s->next;
1122 free_smacro(s);
1123 } else {
1124 empty = false;
1125 }
1126 }
H. Peter Anvin97a23472007-09-16 17:57:25 -07001127 }
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07001128
1129 /*
1130 * Free the hash table and keys if and only if it is now empty.
1131 * Note: we cannot free keys even for an empty list above, as that
1132 * mucks up the hash algorithm.
1133 */
1134 if (empty)
1135 hash_free_all(smt, true);
1136}
1137
1138static void free_smacro_table(struct hash_table *smt)
1139{
1140 clear_smacro_table(smt, CLEAR_ALLDEFINE);
H. Peter Anvin072771e2008-05-22 13:17:51 -07001141}
1142
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001143static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -07001144{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001145 struct hash_iterator it;
1146 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001147
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001148 hash_for_each(mmt, it, np) {
1149 MMacro *tmp;
1150 MMacro *m = np->data;
1151 nasm_free((void *)np->key);
1152 list_for_each_safe(m, tmp, m)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001153 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001154 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001155 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -07001156}
1157
1158static void free_macros(void)
1159{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001160 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001161 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001162}
1163
1164/*
1165 * Initialize the hash tables
1166 */
1167static void init_macros(void)
1168{
H. Peter Anvin97a23472007-09-16 17:57:25 -07001169}
1170
1171/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001172 * Pop the context stack.
1173 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001174static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001175{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001176 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001177
1178 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001179 free_smacro_table(&c->localmac);
H. Peter Anvin8571f062019-09-23 16:40:03 -07001180 nasm_free((char *)c->name);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001181 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001182}
1183
H. Peter Anvin072771e2008-05-22 13:17:51 -07001184/*
1185 * Search for a key in the hash index; adding it if necessary
1186 * (in which case we initialize the data pointer to NULL.)
1187 */
1188static void **
1189hash_findi_add(struct hash_table *hash, const char *str)
1190{
1191 struct hash_insert hi;
1192 void **r;
1193 char *strx;
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001194 size_t l = strlen(str) + 1;
H. Peter Anvin072771e2008-05-22 13:17:51 -07001195
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001196 r = hash_findib(hash, str, l, &hi);
H. Peter Anvin072771e2008-05-22 13:17:51 -07001197 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001198 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -07001199
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001200 strx = nasm_malloc(l); /* Use a more efficient allocator here? */
1201 memcpy(strx, str, l);
H. Peter Anvin072771e2008-05-22 13:17:51 -07001202 return hash_add(&hi, strx, NULL);
1203}
1204
1205/*
1206 * Like hash_findi, but returns the data element rather than a pointer
1207 * to it. Used only when not adding a new element, hence no third
1208 * argument.
1209 */
1210static void *
1211hash_findix(struct hash_table *hash, const char *str)
1212{
1213 void **p;
1214
1215 p = hash_findi(hash, str, NULL);
1216 return p ? *p : NULL;
1217}
1218
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001219/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001220 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001221 * if there no more left -- return NULL
1222 */
1223static char *line_from_stdmac(void)
1224{
1225 unsigned char c;
1226 const unsigned char *p = stdmacpos;
1227 char *line, *q;
1228 size_t len = 0;
1229
1230 if (!stdmacpos)
1231 return NULL;
1232
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -07001233 /*
1234 * 32-126 is ASCII, 127 is end of line, 128-31 are directives
1235 * (allowed to wrap around) corresponding to PP_* tokens 0-159.
1236 */
1237 while ((c = *p++) != 127) {
1238 uint8_t ndir = c - 128;
1239 if (ndir < 256-96)
1240 len += pp_directives_len[ndir] + 1;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001241 else
1242 len++;
1243 }
1244
1245 line = nasm_malloc(len + 1);
1246 q = line;
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -07001247
1248 while ((c = *stdmacpos++) != 127) {
1249 uint8_t ndir = c - 128;
1250 if (ndir < 256-96) {
1251 memcpy(q, pp_directives[ndir], pp_directives_len[ndir]);
1252 q += pp_directives_len[ndir];
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001253 *q++ = ' ';
1254 } else {
1255 *q++ = c;
1256 }
1257 }
1258 stdmacpos = p;
1259 *q = '\0';
1260
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -07001261 if (*stdmacpos == 127) {
H. Peter Anvinf7606612016-07-13 14:23:48 -07001262 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001263 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -07001264 if (*stdmacnext) {
1265 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001266 } else if (do_predef) {
1267 Line *pd, *l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001268
1269 /*
1270 * Nasty hack: here we push the contents of
1271 * `predef' on to the top-level expansion stack,
1272 * since this is the most convenient way to
1273 * implement the pre-include and pre-define
1274 * features.
1275 */
1276 list_for_each(pd, predef) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07001277 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001278 l->next = istk->expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07001279 l->first = dup_tlist(pd->first, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001280 l->finishes = NULL;
1281
1282 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001283 }
1284 do_predef = false;
1285 }
1286 }
1287
1288 return line;
1289}
1290
H. Peter Anvin6686de22019-08-10 05:33:14 -07001291/*
1292 * Read a line from a file. Return NULL on end of file.
1293 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001294static char *line_from_file(FILE *f)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001295{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001296 int c;
1297 unsigned int size, next;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001298 const unsigned int delta = 512;
1299 const unsigned int pad = 8;
1300 unsigned int nr_cont = 0;
1301 bool cont = false;
1302 char *buffer, *p;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07001303 int32_t lineno;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001304
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001305 size = delta;
1306 p = buffer = nasm_malloc(size);
1307
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001308 do {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001309 c = fgetc(f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001310
1311 switch (c) {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001312 case EOF:
1313 if (p == buffer) {
1314 nasm_free(buffer);
1315 return NULL;
1316 }
1317 c = 0;
1318 break;
1319
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001320 case '\r':
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001321 next = fgetc(f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001322 if (next != '\n')
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001323 ungetc(next, f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001324 if (cont) {
1325 cont = false;
1326 continue;
1327 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001328 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001329 break;
1330
1331 case '\n':
1332 if (cont) {
1333 cont = false;
1334 continue;
1335 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001336 c = 0;
1337 break;
1338
1339 case 032: /* ^Z = legacy MS-DOS end of file mark */
1340 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001341 break;
1342
1343 case '\\':
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001344 next = fgetc(f);
1345 ungetc(next, f);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +04001346 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001347 cont = true;
1348 nr_cont++;
1349 continue;
1350 }
1351 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001352 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001353
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001354 if (p >= (buffer + size - pad)) {
1355 buffer = nasm_realloc(buffer, size + delta);
1356 p = buffer + size - pad;
1357 size += delta;
1358 }
1359
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001360 *p++ = c;
1361 } while (c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001362
H. Peter Anvinab6f8312019-08-09 22:31:45 -07001363 lineno = src_get_linnum() + istk->lineinc +
1364 (nr_cont * istk->lineinc);
1365 src_set_linnum(lineno);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001366
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001367 return buffer;
1368}
1369
1370/*
H. Peter Anvin6686de22019-08-10 05:33:14 -07001371 * Common read routine regardless of source
1372 */
1373static char *read_line(void)
1374{
1375 char *line;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001376 FILE *f = istk->fp;
H. Peter Anvin6686de22019-08-10 05:33:14 -07001377
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001378 if (f)
1379 line = line_from_file(f);
H. Peter Anvin6686de22019-08-10 05:33:14 -07001380 else
1381 line = line_from_stdmac();
1382
1383 if (!line)
1384 return NULL;
1385
1386 if (!istk->nolist)
1387 lfmt->line(LIST_READ, src_get_linnum(), line);
1388
1389 return line;
1390}
1391
1392/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00001393 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001394 * don't need to parse the value out of e.g. numeric tokens: we
1395 * simply split one string into many.
1396 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07001397static Token *tokenize(const char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001398{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001399 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001400 Token *list = NULL;
1401 Token *t, **tail = &list;
1402
H. Peter Anvine2c80182005-01-15 22:15:51 +00001403 while (*line) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001404 const char *p = line;
1405 const char *ep = NULL; /* End of token, for trimming the end */
1406 size_t toklen;
1407 char firstchar = *p; /* Can be used to override the first char */
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001408
H. Peter Anvine2c80182005-01-15 22:15:51 +00001409 if (*p == '%') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001410 /*
1411 * Preprocessor construct; find the end of the token.
1412 * Classification is handled later, because %{...} can be
1413 * used to create any preprocessor token.
1414 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001415 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001416 if (*p == '+' && !nasm_isdigit(p[1])) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001417 /* Paste token */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001418 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001419 } else if (nasm_isdigit(*p) ||
1420 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001421 do {
1422 p++;
1423 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001424 while (nasm_isdigit(*p));
H. Peter Anvin8571f062019-09-23 16:40:03 -07001425 } else if (*p == '{' || *p == '[') {
1426 /* %{...} or %[...] */
1427 char firstchar = *p;
1428 char endchar = *p + 2; /* } or ] */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001429 int lvl = 1;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001430 line += (*p++ == '{'); /* Skip { but not [ (yet) */
1431 while (lvl) {
1432 if (*p == firstchar) {
1433 lvl++;
1434 } else if (*p == endchar) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001435 lvl--;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001436 } else if (nasm_isquote(*p)) {
1437 p = nasm_skip_string(p);
1438 }
1439
1440 /*
1441 * *p can have been advanced to a null character by
1442 * nasm_skip_string()
1443 */
1444 if (!*p) {
1445 nasm_warn(WARN_OTHER, "unterminated %%%c construct",
1446 firstchar);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001447 break;
1448 }
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001449 p++;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001450 }
1451 ep = lvl ? p : p-1; /* Terminal character not part of token */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001452 } else if (*p == '?') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001453 /* %? or %?? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001454 p++;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001455 if (*p == '?')
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001456 p++;
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001457 } else if (*p == '!') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001458 /* Environment variable reference */
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001459 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001460 if (nasm_isidchar(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001461 do {
1462 p++;
1463 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001464 while (nasm_isidchar(*p));
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001465 } else if (nasm_isquote(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001466 p = nasm_skip_string(p);
1467 if (*p)
1468 p++;
1469 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001470 nasm_nonfatalf(ERR_PASS1, "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001471 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001472 /* %! without anything else... */
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001473 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07001474 } else if (*p == ',') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001475 /* Conditional comma */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07001476 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001477 } else if (nasm_isidchar(*p) ||
H. Peter Anvin8571f062019-09-23 16:40:03 -07001478 ((*p == '%' || *p == '$') && nasm_isidchar(p[1]))) {
1479 /* Identifier or some sort */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001480 do {
1481 p++;
1482 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001483 while (nasm_isidchar(*p));
H. Peter Anvin8571f062019-09-23 16:40:03 -07001484 } else if (*p == '%') {
1485 /* %% operator */
1486 p++;
1487 }
1488
1489 if (!ep)
1490 ep = p;
1491 toklen = ep - line;
1492
1493 /* Classify here, to handle %{...} correctly */
1494 if (toklen < 2) {
1495 type = TOK_OTHER; /* % operator */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001496 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001497 char c0 = line[1];
1498
1499 switch (c0) {
1500 case '+':
1501 type = (toklen == 2) ? TOK_PASTE : TOK_MMACRO_PARAM;
1502 break;
1503
1504 case '-':
1505 type = TOK_MMACRO_PARAM;
1506 break;
1507
1508 case '?':
1509 if (toklen == 2)
1510 type = TOK_PREPROC_Q;
1511 else if (toklen == 3 && line[2] == '?')
1512 type = TOK_PREPROC_QQ;
1513 else
1514 type = TOK_PREPROC_ID;
1515 break;
1516
1517 case '!':
1518 type = (toklen == 2) ? TOK_OTHER : TOK_ENVIRON;
1519 break;
1520
1521 case '%':
1522 type = (toklen == 2) ? TOK_OTHER : TOK_LOCAL_SYMBOL;
1523 break;
1524
1525 case '$':
1526 type = (toklen == 2) ? TOK_OTHER : TOK_LOCAL_MACRO;
1527 break;
1528
1529 case '[':
1530 line += 2; /* Skip %[ */
1531 firstchar = *line; /* Don't clobber */
1532 toklen -= 2;
1533 type = TOK_INDIRECT;
1534 break;
1535
1536 case ',':
1537 type = (toklen == 2) ? TOK_COND_COMMA : TOK_PREPROC_ID;
1538 break;
1539
1540 case '\'':
1541 case '\"':
1542 case '`':
1543 /* %{'string'} */
1544 type = TOK_PREPROC_ID;
1545 break;
1546
1547 case ':':
1548 type = TOK_MMACRO_PARAM; /* %{:..} */
1549 break;
1550
1551 default:
1552 if (nasm_isdigit(c0))
1553 type = TOK_MMACRO_PARAM;
1554 else if (nasm_isidchar(c0) || toklen > 2)
1555 type = TOK_PREPROC_ID;
1556 else
1557 type = TOK_OTHER;
1558 break;
1559 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001560 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001561 } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001562 /*
1563 * An identifier. This includes the ? operator, which is
1564 * treated as a keyword, not as a special character
1565 * operator
1566 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001567 type = TOK_ID;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001568 while (nasm_isidchar(*++p))
1569 ;
1570 } else if (nasm_isquote(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001571 /*
1572 * A string token.
1573 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001574 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001575 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001576
H. Peter Anvine2c80182005-01-15 22:15:51 +00001577 if (*p) {
1578 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001579 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001580 nasm_warn(WARN_OTHER, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001581 /* Handling unterminated strings by UNV */
1582 /* type = -1; */
1583 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001584 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001585 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001586 p += 2;
H. Peter Anvin13506202018-11-28 14:55:58 -08001587 } else if (nasm_isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001588 bool is_hex = false;
1589 bool is_float = false;
1590 bool has_e = false;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001591 char c;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001592
H. Peter Anvine2c80182005-01-15 22:15:51 +00001593 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001594 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001595 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001596
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001597 if (*p == '$') {
1598 p++;
1599 is_hex = true;
1600 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001601
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001602 for (;;) {
1603 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001604
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001605 if (!is_hex && (c == 'e' || c == 'E')) {
1606 has_e = true;
1607 if (*p == '+' || *p == '-') {
1608 /*
1609 * e can only be followed by +/- if it is either a
1610 * prefixed hex number or a floating-point number
1611 */
1612 p++;
1613 is_float = true;
1614 }
1615 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1616 is_hex = true;
1617 } else if (c == 'P' || c == 'p') {
1618 is_float = true;
1619 if (*p == '+' || *p == '-')
1620 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001621 } else if (nasm_isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001622 ; /* just advance */
1623 else if (c == '.') {
1624 /*
1625 * we need to deal with consequences of the legacy
1626 * parser, like "1.nolist" being two tokens
1627 * (TOK_NUMBER, TOK_ID) here; at least give it
1628 * a shot for now. In the future, we probably need
1629 * a flex-based scanner with proper pattern matching
1630 * to do it as well as it can be done. Nothing in
1631 * the world is going to help the person who wants
1632 * 0x123.p16 interpreted as two tokens, though.
1633 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07001634 const char *r = p;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001635 while (*r == '_')
1636 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001637
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001638 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1639 (!is_hex && (*r == 'e' || *r == 'E')) ||
1640 (*r == 'p' || *r == 'P')) {
1641 p = r;
1642 is_float = true;
1643 } else
1644 break; /* Terminate the token */
1645 } else
1646 break;
1647 }
1648 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001649
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001650 if (p == line+1 && *line == '$') {
1651 type = TOK_OTHER; /* TOKEN_HERE */
1652 } else {
1653 if (has_e && !is_hex) {
1654 /* 1e13 is floating-point, but 1e13h is not */
1655 is_float = true;
1656 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001657
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001658 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1659 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001660 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001661 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001662 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001663 /*
1664 * Whitespace just before end-of-line is discarded by
1665 * pretending it's a comment; whitespace just before a
1666 * comment gets lumped into the comment.
1667 */
1668 if (!*p || *p == ';') {
1669 type = TOK_COMMENT;
1670 while (*p)
1671 p++;
1672 }
1673 } else if (*p == ';') {
1674 type = TOK_COMMENT;
1675 while (*p)
1676 p++;
1677 } else {
1678 /*
1679 * Anything else is an operator of some kind. We check
1680 * for all the double-character operators (>>, <<, //,
H. Peter Anvin8571f062019-09-23 16:40:03 -07001681 * %%, <=, >=, ==, !=, <>, &&, ||, ^^) and the triple-
1682 * character operators (<<<, >>>, <=>) but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001683 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001684 */
1685 type = TOK_OTHER;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001686 switch (*p++) {
1687 case '>':
1688 if (*p == '>') {
1689 p++;
1690 if (*p == '>')
1691 p++;
H. Peter Anvind03a6c82019-10-07 21:29:05 -07001692 } else if (*p == '=') {
1693 p++;
1694 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07001695 break;
1696
1697 case '<':
1698 if (*p == '<') {
1699 p++;
1700 if (*p == '<')
1701 p++;
1702 } else if (*p == '=') {
1703 p++;
1704 if (*p == '>')
1705 p++;
1706 } else if (*p == '>') {
1707 p++;
1708 }
1709 break;
1710
1711 case '!':
1712 if (*p == '=')
1713 p++;
1714 break;
1715
1716 case '/':
1717 case '=':
1718 case '&':
1719 case '|':
1720 case '^':
1721 /* These operators can be doubled but nothing else */
1722 if (*p == p[-1])
1723 p++;
1724 break;
1725
1726 default:
1727 break;
1728 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001729 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001730
H. Peter Anvin8571f062019-09-23 16:40:03 -07001731 if (type == TOK_WHITESPACE) {
1732 *tail = t = new_White(NULL);
1733 tail = &t->next;
1734 } else if (type != TOK_COMMENT) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001735 if (!ep)
1736 ep = p;
1737 *tail = t = new_Token(NULL, type, line, ep - line);
H. Peter Anvin8571f062019-09-23 16:40:03 -07001738 *tok_text_buf(t) = firstchar; /* E.g. %{foo} -> {foo -> %foo */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001739 tail = &t->next;
1740 }
1741 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001742 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001743 return list;
1744}
1745
H. Peter Anvin8571f062019-09-23 16:40:03 -07001746/*
1747 * Tokens are allocated in blocks to improve speed. Set the blocksize
1748 * to 0 to use regular nasm_malloc(); this is useful for debugging.
1749 *
1750 * alloc_Token() returns a zero-initialized token structure.
1751 */
1752#define TOKEN_BLOCKSIZE 4096
1753
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001754#if TOKEN_BLOCKSIZE
H. Peter Anvin8571f062019-09-23 16:40:03 -07001755
1756static Token *freeTokens = NULL;
1757static Token *tokenblocks = NULL;
1758
1759static Token *alloc_Token(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001760{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001761 Token *t = freeTokens;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001762
H. Peter Anvin8571f062019-09-23 16:40:03 -07001763 if (unlikely(!t)) {
1764 Token *block;
1765 size_t i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001766
H. Peter Anvin8571f062019-09-23 16:40:03 -07001767 nasm_newn(block, TOKEN_BLOCKSIZE);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001768
H. Peter Anvin8571f062019-09-23 16:40:03 -07001769 /*
1770 * The first entry in each array are a linked list of
1771 * block allocations and is not used for data.
1772 */
1773 block[0].next = tokenblocks;
1774 block[0].type = TOK_BLOCK;
1775 tokenblocks = block;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001776
H. Peter Anvin8571f062019-09-23 16:40:03 -07001777 /*
1778 * Add the rest to the free list
1779 */
1780 for (i = 2; i < TOKEN_BLOCKSIZE - 1; i++)
1781 block[i].next = &block[i+1];
1782
1783 freeTokens = &block[2];
1784
1785 /*
1786 * Return the topmost usable token
1787 */
1788 return &block[1];
1789 }
1790
1791 freeTokens = t->next;
1792 t->next = NULL;
1793 return t;
H. Peter Anvince616072002-04-30 21:02:23 +00001794}
1795
H. Peter Anvin8571f062019-09-23 16:40:03 -07001796static Token *delete_Token(Token *t)
1797{
1798 Token *next = t->next;
1799
1800 nasm_zero(*t);
1801 t->next = freeTokens;
1802 freeTokens = t;
1803
1804 return next;
1805}
1806
H. Peter Anvine2c80182005-01-15 22:15:51 +00001807static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001808{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001809 Token *block, *blocktmp;
H. Peter Anvince616072002-04-30 21:02:23 +00001810
H. Peter Anvin8571f062019-09-23 16:40:03 -07001811 list_for_each_safe(block, blocktmp, tokenblocks)
1812 nasm_free(block);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001813
H. Peter Anvin8571f062019-09-23 16:40:03 -07001814 freeTokens = tokenblocks = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001815}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001816
H. Peter Anvin8571f062019-09-23 16:40:03 -07001817#else
1818
1819static inline Token *alloc_Token(void)
1820{
1821 Token *t;
1822 nasm_new(*t);
1823 return t;
1824}
1825
1826static Token *delete_Token(Token *t)
1827{
1828 Token *next = t->next;
1829 nasm_free(t);
1830 return next;
1831}
1832
1833static inline void delete_Blocks(void)
1834{
1835 /* Nothing to do */
1836}
1837
1838#endif
1839
H. Peter Anvin734b1882002-04-30 21:01:08 +00001840/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001841 * this function creates a new Token and passes a pointer to it
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001842 * back to the caller. It sets the type, text, and next pointer elements.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001843 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001844static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001845 const char *text, size_t txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001846{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001847 Token *t = alloc_Token();
1848 char *textp;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001849
H. Peter Anvin734b1882002-04-30 21:01:08 +00001850 t->next = next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001851 t->type = type;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001852 if (type == TOK_WHITESPACE) {
1853 t->len = 1;
1854 t->text.a[0] = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001855 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001856 if (text && text[0] && !txtlen)
1857 txtlen = tok_strlen(text);
1858
1859 t->len = tok_check_len(txtlen);
1860
1861 if (text) {
1862 textp = (txtlen > INLINE_TEXT)
1863 ? (t->text.p.ptr = nasm_malloc(txtlen+1)) : t->text.a;
1864 memcpy(textp, text, txtlen);
1865 textp[txtlen] = '\0'; /* In case we needed malloc() */
1866 } else {
1867 /*
1868 * Allocate a buffer but do not fill it. The caller
1869 * can fill in text, but must not change the length.
1870 * The filled in text must be exactly txtlen once
1871 * the buffer is filled and before the token is added
1872 * to any line lists.
1873 */
1874 if (txtlen > INLINE_TEXT)
1875 t->text.p.ptr = nasm_zalloc(txtlen+1);
1876 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001877 }
1878 return t;
1879}
1880
H. Peter Anvin8571f062019-09-23 16:40:03 -07001881/*
1882 * Same as new_Token(), but text belongs to the new token and is
1883 * either taken over or freed. This function MUST be called
1884 * with valid txt and txtlen, unlike new_Token().
1885 */
1886static Token *new_Token_free(Token * next, enum pp_token_type type,
1887 char *text, size_t txtlen)
1888{
1889 Token *t = alloc_Token();
1890
1891 t->next = next;
1892 t->type = type;
1893 t->len = tok_check_len(txtlen);
1894
1895 if (txtlen <= INLINE_TEXT) {
1896 memcpy(t->text.a, text, txtlen);
1897 free(text);
1898 } else {
1899 t->text.p.ptr = text;
1900 }
1901
1902 return t;
1903}
1904
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001905static Token *dup_Token(Token *next, const Token *src)
1906{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001907 Token *t = alloc_Token();
1908
1909 memcpy(t, src, sizeof *src);
1910 t->next = next;
1911
1912 if (t->len > INLINE_TEXT) {
1913 t->text.p.ptr = nasm_malloc(t->len + 1);
1914 memcpy(t->text.p.ptr, src->text.p.ptr, t->len+1);
1915 }
1916
1917 return t;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001918}
1919
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001920static Token *new_White(Token *next)
1921{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001922 Token *t = alloc_Token();
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001923
H. Peter Anvin8571f062019-09-23 16:40:03 -07001924 t->next = next;
1925 t->type = TOK_WHITESPACE;
1926 t->len = 1;
1927 t->text.a[0] = ' ';
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001928
H. Peter Anvin8571f062019-09-23 16:40:03 -07001929 return t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001930}
1931
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001932/*
H. Peter Anvin8571f062019-09-23 16:40:03 -07001933 * This *transfers* the content from one token to another, leaving the
1934 * next pointer of the latter intact. Unlike dup_Token(), the old
1935 * token is destroyed, except for its next pointer, and the text
1936 * pointer allocation, if any, is simply transferred.
1937 */
1938static Token *steal_Token(Token *dst, Token *src)
1939{
1940 /* Overwrite everything except the next pointers */
1941 memcpy((char *)dst + sizeof(Token *), (char *)src + sizeof(Token *),
1942 sizeof(Token) - sizeof(Token *));
1943
1944 /* Clear the donor token */
1945 memset((char *)src + sizeof(Token *), 0, sizeof(Token) - sizeof(Token *));
1946
1947 return dst;
1948}
1949
1950/*
1951 * Convert a line of tokens back into text. This modifies the list
1952 * by expanding environment variables.
1953 *
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001954 * If expand_locals is not zero, identifiers of the form "%$*xxx"
H. Peter Anvin8571f062019-09-23 16:40:03 -07001955 * are also transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001956 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001957static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001958{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001959 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001960 char *line, *p;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001961 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001962
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001963 list_for_each(t, tlist) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001964 switch (t->type) {
1965 case TOK_ENVIRON:
1966 {
1967 const char *v = pp_getenv(t, true);
1968 set_text(t, v, tok_strlen(v));
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07001969 t->type = TOK_NAKED_STRING;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001970 break;
1971 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001972
H. Peter Anvin8571f062019-09-23 16:40:03 -07001973 case TOK_LOCAL_MACRO:
1974 case TOK_LOCAL_SYMBOL:
1975 if (expand_locals) {
1976 const char *q;
1977 char *p;
1978 Context *ctx = get_ctx(tok_text(t), &q);
1979 if (ctx) {
1980 p = nasm_asprintf("..@%"PRIu64".%s", ctx->number, q);
1981 set_text_free(t, p, nasm_last_string_len());
1982 t->type = TOK_ID;
1983 }
1984 }
1985 break;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001986
H. Peter Anvin8571f062019-09-23 16:40:03 -07001987 default:
1988 break; /* No modifications */
1989 }
1990
1991 if (debug_level(2)) {
1992 unsigned int t_len = t->len;
1993 unsigned int s_len = tok_strlen(tok_text(t));
1994 if (t_len != s_len) {
1995 nasm_panic("assertion failed: token \"%s\" type %u len %u has t->len %u\n",
1996 tok_text(t), t->type, s_len, t_len);
1997 t->len = s_len;
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001998 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001999 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07002000
H. Peter Anvin8571f062019-09-23 16:40:03 -07002001 len += t->len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002002 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04002003
H. Peter Anvin734b1882002-04-30 21:01:08 +00002004 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04002005
H. Peter Anvin8571f062019-09-23 16:40:03 -07002006 list_for_each(t, tlist)
2007 p = mempcpy(p, tok_text(t), t->len);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002008 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04002009
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002010 return line;
2011}
2012
2013/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002014 * A scanner, suitable for use by the expression evaluator, which
2015 * operates on a line of Tokens. Expects a pointer to a pointer to
2016 * the first token in the line to be passed in as its private_data
2017 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07002018 *
2019 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00002020 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08002021struct ppscan {
2022 Token *tptr;
2023 int ntokens;
2024};
2025
H. Peter Anvine2c80182005-01-15 22:15:51 +00002026static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002027{
H. Peter Anvin8b262472019-02-26 14:00:54 -08002028 struct ppscan *pps = private_data;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002029 Token *tline;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002030 const char *txt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002031
H. Peter Anvine2c80182005-01-15 22:15:51 +00002032 do {
H. Peter Anvin8b262472019-02-26 14:00:54 -08002033 if (pps->ntokens && (tline = pps->tptr)) {
2034 pps->ntokens--;
2035 pps->tptr = tline->next;
2036 } else {
2037 pps->tptr = NULL;
2038 pps->ntokens = 0;
2039 return tokval->t_type = TOKEN_EOS;
2040 }
2041 } while (tline->type == TOK_WHITESPACE || tline->type == TOK_COMMENT);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002042
H. Peter Anvin8571f062019-09-23 16:40:03 -07002043 txt = tok_text(tline);
2044 tokval->t_charptr = (char *)txt; /* Fix this */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07002045
H. Peter Anvin8571f062019-09-23 16:40:03 -07002046 if (txt[0] == '$') {
2047 if (!txt[1]) {
2048 return tokval->t_type = TOKEN_HERE;
2049 } else if (txt[1] == '$' && !txt[2]) {
2050 return tokval->t_type = TOKEN_BASE;
2051 } else if (tline->type == TOK_ID) {
2052 tokval->t_charptr++;
2053 return tokval->t_type = TOKEN_ID;
2054 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002055 }
2056
H. Peter Anvin8571f062019-09-23 16:40:03 -07002057 switch (tline->type) {
2058 default:
2059 if (tline->len == 1)
2060 return tokval->t_type = txt[0];
2061 /* fall through */
2062 case TOK_ID:
2063 return nasm_token_hash(txt, tokval);
2064
2065 case TOK_NUMBER:
2066 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002067 bool rn_error;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002068 tokval->t_integer = readnum(txt, &rn_error);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002069 if (rn_error)
2070 return tokval->t_type = TOKEN_ERRNUM;
2071 else
2072 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07002073 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002074
H. Peter Anvin8571f062019-09-23 16:40:03 -07002075 case TOK_FLOAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002076 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002077
2078 case TOK_STRING:
2079 tokval->t_charptr = (char *)unquote_token(tline);
2080 tokval->t_inttwo = tline->len;
2081 return tokval->t_type = TOKEN_STR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002082 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002083}
2084
2085/*
H. Peter Anvind2354082019-08-27 16:38:48 -07002086 * 1. An expression (true if nonzero 0)
2087 * 2. The keywords true, on, yes for true
2088 * 3. The keywords false, off, no for false
2089 * 4. An empty line, for true
2090 *
2091 * On error, return defval (usually the previous value)
2092 */
2093static bool pp_get_boolean_option(Token *tline, bool defval)
2094{
2095 static const char * const noyes[] = {
2096 "no", "yes",
2097 "false", "true",
2098 "off", "on"
2099 };
2100 struct ppscan pps;
2101 struct tokenval tokval;
2102 expr *evalresult;
2103
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002104 tline = skip_white(tline);
2105 if (!tline)
H. Peter Anvind2354082019-08-27 16:38:48 -07002106 return true;
2107
2108 if (tline->type == TOK_ID) {
2109 size_t i;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002110 const char *txt = tok_text(tline);
2111
H. Peter Anvind2354082019-08-27 16:38:48 -07002112 for (i = 0; i < ARRAY_SIZE(noyes); i++)
H. Peter Anvin8571f062019-09-23 16:40:03 -07002113 if (!nasm_stricmp(txt, noyes[i]))
H. Peter Anvind2354082019-08-27 16:38:48 -07002114 return i & 1;
2115 }
2116
2117 pps.tptr = NULL;
2118 pps.tptr = tline;
2119 pps.ntokens = -1;
2120 tokval.t_type = TOKEN_INVALID;
2121 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
2122
2123 if (!evalresult)
2124 return true;
2125
2126 if (tokval.t_type)
2127 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
2128 if (!is_really_simple(evalresult)) {
2129 nasm_nonfatal("boolean flag expression must be a constant");
2130 return defval;
2131 }
2132
2133 return reloc_value(evalresult) != 0;
2134}
2135
2136/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002137 * Compare a string to the name of an existing macro; this is a
2138 * simple wrapper which calls either strcmp or nasm_stricmp
2139 * depending on the value of the `casesense' parameter.
2140 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002141static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002142{
H. Peter Anvin734b1882002-04-30 21:01:08 +00002143 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002144}
2145
2146/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07002147 * Compare a string to the name of an existing macro; this is a
2148 * simple wrapper which calls either strcmp or nasm_stricmp
2149 * depending on the value of the `casesense' parameter.
2150 */
2151static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
2152{
2153 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
2154}
2155
2156/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002157 * Return the Context structure associated with a %$ token. Return
2158 * NULL, having _already_ reported an error condition, if the
2159 * context stack isn't deep enough for the supplied number of $
2160 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002161 *
2162 * If "namep" is non-NULL, set it to the pointer to the macro name
2163 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002164 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04002165static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002166{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002167 Context *ctx;
2168 int i;
2169
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002170 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002171 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002172
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002173 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00002174 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002175
H. Peter Anvine2c80182005-01-15 22:15:51 +00002176 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002177 nasm_nonfatal("`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002178 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002179 }
2180
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002181 name += 2;
2182 ctx = cstk;
2183 i = 0;
2184 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002185 name++;
2186 i++;
2187 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002188 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002189 if (!ctx) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002190 nasm_nonfatal("`%s': context stack is only"
2191 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002192 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002193 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002194
2195 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002196 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002197
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04002198 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002199}
2200
2201/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002202 * Open an include file. This routine must always return a valid
2203 * file pointer if it returns - it's responsible for throwing an
2204 * ERR_FATAL and bombing out completely if not. It should also try
2205 * the include path one by one until it finds the file or reaches
2206 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07002207 *
2208 * Note: for INC_PROBE the function returns NULL at all times;
2209 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002210 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07002211enum incopen_mode {
2212 INC_NEEDED, /* File must exist */
2213 INC_OPTIONAL, /* Missing is OK */
2214 INC_PROBE /* Only an existence probe */
2215};
2216
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002217/* This is conducts a full pathname search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002218static FILE *inc_fopen_search(const char *file, char **slpath,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002219 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002220{
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -08002221 const struct strlist_entry *ip = strlist_head(ipath_list);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002222 FILE *fp;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002223 const char *prefix = "";
night199ukfdb1a1b2018-10-18 23:19:47 +02002224 char *sp;
H. Peter Anvind81a2352016-09-21 14:03:18 -07002225 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002226
H. Peter Anvine2c80182005-01-15 22:15:51 +00002227 while (1) {
night199ukfdb1a1b2018-10-18 23:19:47 +02002228 sp = nasm_catfile(prefix, file);
H. Peter Anvind81a2352016-09-21 14:03:18 -07002229 if (omode == INC_PROBE) {
2230 fp = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002231 found = nasm_file_exists(sp);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07002232 } else {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002233 fp = nasm_open_read(sp, fmode);
H. Peter Anvind81a2352016-09-21 14:03:18 -07002234 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002235 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07002236 if (found) {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002237 *slpath = sp;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002238 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002239 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002240
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002241 nasm_free(sp);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002242
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002243 if (!ip) {
2244 *slpath = NULL;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002245 return NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002246 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002247
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002248 prefix = ip->str;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002249 ip = ip->next;
2250 }
2251}
2252
2253/*
2254 * Open a file, or test for the presence of one (depending on omode),
2255 * considering the include path.
2256 */
2257static FILE *inc_fopen(const char *file,
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002258 struct strlist *dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002259 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002260 enum incopen_mode omode,
2261 enum file_flags fmode)
2262{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002263 struct hash_insert hi;
2264 void **hp;
2265 char *path;
2266 FILE *fp = NULL;
2267
2268 hp = hash_find(&FileHash, file, &hi);
2269 if (hp) {
2270 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03002271 if (path || omode != INC_NEEDED) {
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002272 strlist_add(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03002273 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002274 } else {
2275 /* Need to do the actual path search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002276 fp = inc_fopen_search(file, &path, omode, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002277
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002278 /* Positive or negative result */
2279 hash_add(&hi, nasm_strdup(file), path);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002280
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002281 /*
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002282 * Add file to dependency path.
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002283 */
2284 if (path || omode != INC_NEEDED)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002285 strlist_add(dhead, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00002286 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002287
H. Peter Anvin (Intel)5d68f982020-06-01 12:32:35 -07002288 if (path && !fp && omode != INC_PROBE)
2289 fp = nasm_open_read(path, fmode);
2290
2291 if (omode == INC_NEEDED && !fp) {
2292 if (!path)
2293 errno = ENOENT;
2294
2295 nasm_nonfatal("unable to open include file `%s': %s",
2296 file, strerror(errno));
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002297 }
2298
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002299 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002300 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002301
2302 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002303}
2304
2305/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07002306 * Opens an include or input file. Public version, for use by modules
2307 * that get a file:lineno pair and need to look at the file again
2308 * (e.g. the CodeView debug backend). Returns NULL on failure.
2309 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07002310FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07002311{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002312 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07002313}
2314
2315/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002316 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002317 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002318 * return true if _any_ single-line macro of that name is defined.
2319 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002320 * `nparam' or no parameters is defined.
2321 *
2322 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002323 * defined, or nparam is -1, the address of the definition structure
2324 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002325 * is NULL, no action will be taken regarding its contents, and no
2326 * error will occur.
2327 *
2328 * Note that this is also called with nparam zero to resolve
2329 * `ifdef'.
2330 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002331static bool
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002332smacro_defined(Context *ctx, const char *name, int nparam, SMacro **defn,
H. Peter Anvind2354082019-08-27 16:38:48 -07002333 bool nocase, bool find_alias)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002334{
H. Peter Anvin166c2472008-05-28 12:28:58 -07002335 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002336 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002337
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002338 smtbl = ctx ? &ctx->localmac : &smacros;
H. Peter Anvind2354082019-08-27 16:38:48 -07002339
2340restart:
H. Peter Anvin166c2472008-05-28 12:28:58 -07002341 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002342
H. Peter Anvine2c80182005-01-15 22:15:51 +00002343 while (m) {
2344 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002345 (nparam <= 0 || m->nparam == 0 || nparam == m->nparam ||
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002346 (m->greedy && nparam >= m->nparam-1))) {
H. Peter Anvind2354082019-08-27 16:38:48 -07002347 if (m->alias && !find_alias) {
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002348 if (!ppopt.noaliases) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002349 name = tok_text(m->expansion);
H. Peter Anvind2354082019-08-27 16:38:48 -07002350 goto restart;
2351 } else {
2352 continue;
2353 }
2354 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002355 if (defn) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002356 *defn = (nparam == m->nparam || nparam == -1) ? m : NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002357 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002358 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002359 }
2360 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002361 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002362
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002363 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002364}
2365
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002366/* param should be a natural number [0; INT_MAX] */
2367static int read_param_count(const char *str)
2368{
2369 int result;
2370 bool err;
2371
2372 result = readnum(str, &err);
2373 if (result < 0 || result > INT_MAX) {
2374 result = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002375 nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]",
2376 str, 0, INT_MAX);
2377 } else if (err)
2378 nasm_nonfatal("unable to parse parameter count `%s'", str);
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002379 return result;
2380}
2381
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002382/*
2383 * Count and mark off the parameters in a multi-line macro call.
2384 * This is called both from within the multi-line macro expansion
2385 * code, and also to mark off the default parameters when provided
2386 * in a %macro definition line.
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002387 *
2388 * Note that we need space in the params array for parameter 0 being
2389 * a possible captured label as well as the final NULL.
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002390 *
2391 * Returns a pointer to the pointer to a terminal comma if present;
2392 * used to drop an empty terminal argument for legacy reasons.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002393 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002394static Token **count_mmac_params(Token *tline, int *nparamp, Token ***paramsp)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002395{
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07002396 int paramsize;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002397 int nparam = 0;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002398 Token *t;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002399 Token **comma = NULL, **maybe_comma = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002400 Token **params;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002401
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002402 paramsize = PARAM_DELTA;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002403 nasm_newn(params, paramsize);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002404
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002405 t = skip_white(tline);
2406 if (t) {
2407 while (true) {
2408 /* Need two slots for captured label and NULL */
2409 if (unlikely(nparam+2 >= paramsize)) {
2410 paramsize += PARAM_DELTA;
2411 params = nasm_realloc(params, sizeof(*params) * paramsize);
2412 }
2413 params[++nparam] = t;
2414 if (tok_is(t, '{')) {
2415 int brace = 1;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002416
2417 comma = NULL; /* Non-empty parameter */
2418
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002419 while (brace && (t = t->next)) {
2420 brace += tok_is(t, '{');
2421 brace -= tok_is(t, '}');
2422 }
H. Peter Anvin (Intel)f8639bd2020-06-04 16:29:53 -07002423
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002424 if (t) {
2425 /*
2426 * Now we've found the closing brace, look further
2427 * for the comma.
2428 */
2429 t = skip_white(t->next);
2430 if (tok_isnt(t, ','))
2431 nasm_nonfatal("braces do not enclose all of macro parameter");
2432 } else {
2433 nasm_nonfatal("expecting closing brace in macro parameter");
2434 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08002435 }
2436
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002437 /* Advance to the next comma */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002438 maybe_comma = &t->next;
2439 while (tok_isnt(t, ',')) {
2440 if (!tok_white(t))
2441 comma = NULL; /* Non-empty parameter */
2442 maybe_comma = &t->next;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002443 t = t->next;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002444 }
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002445
2446 if (!t)
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002447 break; /* End of string, no comma */
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002448
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002449 comma = maybe_comma; /* Point to comma pointer */
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002450 t = skip_white(t->next); /* Eat the comma and whitespace */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08002451 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002452 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002453
2454 params[nparam+1] = NULL;
2455 *paramsp = params;
2456 *nparamp = nparam;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002457
2458 return comma;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002459}
2460
2461/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002462 * Determine whether one of the various `if' conditions is true or
2463 * not.
2464 *
2465 * We must free the tline we get passed.
2466 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002467static enum cond_state if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002468{
H. Peter Anvin70055962007-10-11 00:05:31 -07002469 bool j;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002470 Token *t, *tt, *origline;
2471 struct ppscan pps;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002472 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002473 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002474 enum pp_token_type needtype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002475 const char *dname = pp_directives[ct];
2476 bool casesense = true;
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002477 enum preproc_token cond = PP_COND(ct);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002478
2479 origline = tline;
2480
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002481 switch (cond) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002482 case PP_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002483 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02002484 while (true) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002485 tline = skip_white(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02002486 if (!tline)
2487 break;
2488 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002489 nasm_nonfatal("`%s' expects context identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002490 dname);
2491 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002492 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002493 if (cstk && cstk->name && !nasm_stricmp(tok_text(tline), cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002494 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002495 tline = tline->next;
2496 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002497 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002498
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002499 case PP_IFDEF:
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002500 case PP_IFDEFALIAS:
2501 {
2502 bool alias = cond == PP_IFDEFALIAS;
2503 SMacro *smac;
2504 Context *ctx;
2505 const char *mname;
2506
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002507 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002508 while (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002509 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002510 if (!tline || (tline->type != TOK_ID &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07002511 tline->type != TOK_LOCAL_MACRO)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002512 nasm_nonfatal("`%s' expects macro identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002513 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002514 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002515 }
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002516
2517 mname = tok_text(tline);
2518 ctx = get_ctx(mname, &mname);
H. Peter Anvin (Intel)b91e7732020-06-05 12:22:26 -07002519 if (smacro_defined(ctx, mname, -1, &smac, true, alias) && smac
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002520 && smac->alias == alias) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002521 j = true;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002522 break;
2523 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002524 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002525 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002526 break;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002527 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002528
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002529 case PP_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002530 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002531 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002532 while (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002533 tline = skip_white(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002534 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002535 tline->type != TOK_STRING &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07002536 tline->type != TOK_INTERNAL_STRING &&
2537 tline->type != TOK_ENVIRON)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002538 nasm_nonfatal("`%s' expects environment variable names",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002539 dname);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002540 goto fail;
2541 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002542
2543 j |= !!pp_getenv(tline, false);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002544 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002545 }
2546 break;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002547
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002548 case PP_IFIDNI:
2549 casesense = false;
2550 /* fall through */
2551 case PP_IFIDN:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002552 tline = expand_smacro(tline);
2553 t = tt = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002554 while (tok_isnt(tt, ','))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002555 tt = tt->next;
2556 if (!tt) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002557 nasm_nonfatal("`%s' expects two comma-separated arguments",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002558 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002559 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002560 }
2561 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002562 j = true; /* assume equality unless proved not */
H. Peter Anvin8571f062019-09-23 16:40:03 -07002563 while (tok_isnt(t, ',') && tt) {
2564 unsigned int l1, l2;
2565 const char *t1, *t2;
2566
2567 if (tok_is(tt, ',')) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002568 nasm_nonfatal("`%s': more than one comma on line",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002569 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002570 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002571 }
2572 if (t->type == TOK_WHITESPACE) {
2573 t = t->next;
2574 continue;
2575 }
2576 if (tt->type == TOK_WHITESPACE) {
2577 tt = tt->next;
2578 continue;
2579 }
2580 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002581 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002582 break;
2583 }
H. Peter Anvind2456592008-06-19 15:04:18 -07002584
H. Peter Anvin8571f062019-09-23 16:40:03 -07002585 t1 = unquote_token(t);
2586 t2 = unquote_token(tt);
2587 l1 = t->len;
2588 l2 = tt->len;
2589
2590 if (l1 != l2 || mmemcmp(t1, t2, l1, casesense)) {
2591 j = false;
2592 break;
2593 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00002594
H. Peter Anvine2c80182005-01-15 22:15:51 +00002595 t = t->next;
2596 tt = tt->next;
2597 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002598 if (!tok_is(t, ',') || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002599 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002600 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002601
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002602 case PP_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04002603 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002604 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002605 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00002606
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002607 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002608 tline = expand_id(tline);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002609 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002610 nasm_nonfatal("`%s' expects a macro name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002611 goto fail;
2612 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002613 nasm_zero(searching);
H. Peter Anvin8571f062019-09-23 16:40:03 -07002614 searching.name = dup_text(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002615 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002616 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002617 searching.nparam_max = INT_MAX;
2618 tline = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002619 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002620 if (!tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002621 } else if (!tok_type(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002622 nasm_nonfatal("`%s' expects a parameter count or nothing",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002623 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002624 } else {
2625 searching.nparam_min = searching.nparam_max =
H. Peter Anvin8571f062019-09-23 16:40:03 -07002626 read_param_count(tok_text(tline));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002627 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002628 if (tline && tok_is(tline->next, '-')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002629 tline = tline->next->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002630 if (tok_is(tline, '*'))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002631 searching.nparam_max = INT_MAX;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002632 else if (!tok_type(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002633 nasm_nonfatal("`%s' expects a parameter count after `-'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002634 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002635 else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002636 searching.nparam_max = read_param_count(tok_text(tline));
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002637 if (searching.nparam_min > searching.nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002638 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002639 searching.nparam_max = searching.nparam_min;
2640 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002641 }
2642 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002643 if (tline && tok_is(tline->next, '+')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002644 tline = tline->next;
2645 searching.plus = true;
2646 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002647 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
2648 while (mmac) {
2649 if (!strcmp(mmac->name, searching.name) &&
2650 (mmac->nparam_min <= searching.nparam_max
2651 || searching.plus)
2652 && (searching.nparam_min <= mmac->nparam_max
2653 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002654 found = true;
2655 break;
2656 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002657 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002658 }
2659 if (tline && tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002660 nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002661 nasm_free(searching.name);
2662 j = found;
2663 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002664 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002665
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002666 case PP_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002667 needtype = TOK_ID;
2668 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002669 case PP_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002670 needtype = TOK_NUMBER;
2671 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002672 case PP_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002673 needtype = TOK_STRING;
2674 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002675
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002676iftype:
2677 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002678
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002679 while (tok_white(t) ||
2680 (needtype == TOK_NUMBER && (tok_is(t, '-') | tok_is(t, '+'))))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002681 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002682
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002683 j = tok_type(t, needtype);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002684 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002685
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002686 case PP_IFTOKEN:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002687 tline = expand_smacro(tline);
2688 t = skip_white(tline);
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002689
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002690 j = false;
2691 if (t) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002692 t = skip_white(t->next); /* Skip the actual token + whitespace */
2693 j = !t;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002694 }
2695 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002696
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002697 case PP_IFEMPTY:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002698 tline = expand_smacro(tline);
2699 t = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002700 j = !t; /* Should be empty */
2701 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002702
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002703 case PP_IF:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002704 pps.tptr = tline = expand_smacro(tline);
2705 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002706 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002707 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002708 if (!evalresult)
2709 return -1;
2710 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002711 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002712 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002713 nasm_nonfatal("non-constant value given to `%s'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002714 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002715 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002716 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002717 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002718 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002719
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002720 case PP_IFUSING:
2721 case PP_IFUSABLE:
2722 {
2723 const struct use_package *pkg;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002724 const char *name;
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002725
H. Peter Anvin8571f062019-09-23 16:40:03 -07002726 pkg = get_use_pkg(tline, dname, &name);
2727 if (!name)
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002728 goto fail;
2729
2730 j = pkg && ((cond == PP_IFUSABLE) | use_loaded[pkg->index]);
2731 break;
2732 }
2733
H. Peter Anvine2c80182005-01-15 22:15:51 +00002734 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002735 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002736 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002737 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002738
2739 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002740 return (j ^ PP_COND_NEGATIVE(ct)) ? COND_IF_TRUE : COND_IF_FALSE;
H. Peter Anvin70653092007-10-19 14:42:29 -07002741
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002742fail:
2743 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002744 return COND_NEVER;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002745}
2746
2747/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002748 * Default smacro expansion routine: just returns a copy of the
2749 * expansion list.
2750 */
2751static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002752smacro_expand_default(const SMacro *s, Token **params, int nparams)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002753{
2754 (void)params;
2755 (void)nparams;
2756
2757 return dup_tlist(s->expansion, NULL);
2758}
2759
2760/*
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002761 * Emit a macro defintion or undef to the listing file, if
2762 * desired. This is similar to detoken(), but it handles the reverse
2763 * expansion list, does not expand %! or local variable tokens, and
2764 * does some special handling for macro parameters.
2765 */
2766static void
2767list_smacro_def(enum preproc_token op, const Context *ctx, const SMacro *m)
2768{
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002769 Token *t;
2770 size_t namelen, size;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002771 char *def, *p;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002772 char *context_prefix = NULL;
2773 size_t context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002774
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002775 namelen = strlen(m->name);
2776 size = namelen + 2; /* Include room for space after name + NUL */
2777
2778 if (ctx) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07002779 int context_depth = cstk->depth - ctx->depth + 1;
2780 context_prefix =
2781 nasm_asprintf("[%s::%"PRIu64"] %%%-*s",
2782 ctx->name ? ctx->name : "",
2783 ctx->number, context_depth, "");
2784
2785 context_len = nasm_last_string_len();
2786 memset(context_prefix + context_len - context_depth,
2787 '$', context_depth);
2788 size += context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002789 }
2790
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002791 list_for_each(t, m->expansion)
H. Peter Anvin8571f062019-09-23 16:40:03 -07002792 size += t->len;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002793
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002794 if (m->nparam) {
2795 /*
2796 * Space for ( and either , or ) around each
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002797 * parameter, plus up to 4 flags.
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002798 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002799 int i;
2800
2801 size += 1 + 4 * m->nparam;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002802 for (i = 0; i < m->nparam; i++)
H. Peter Anvin8571f062019-09-23 16:40:03 -07002803 size += m->params[i].name.len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002804 }
2805
2806 def = nasm_malloc(size);
2807 p = def+size;
2808 *--p = '\0';
2809
2810 list_for_each(t, m->expansion) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002811 p -= t->len;
2812 memcpy(p, tok_text(t), t->len);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002813 }
2814
2815 *--p = ' ';
2816
2817 if (m->nparam) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002818 int i;
2819
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002820 *--p = ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002821 for (i = m->nparam-1; i >= 0; i--) {
2822 enum sparmflags flags = m->params[i].flags;
2823 if (flags & SPARM_GREEDY)
2824 *--p = '+';
H. Peter Anvin8571f062019-09-23 16:40:03 -07002825 p -= m->params[i].name.len;
2826 memcpy(p, tok_text(&m->params[i].name), m->params[i].name.len);
2827
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002828 if (flags & SPARM_NOSTRIP)
2829 *--p = '!';
2830 if (flags & SPARM_STR)
2831 *--p = '&';
2832 if (flags & SPARM_EVAL)
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002833 *--p = '=';
2834 *--p = ',';
2835 }
2836 *p = '('; /* First parameter starts with ( not , */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002837 }
2838
2839 p -= namelen;
2840 memcpy(p, m->name, namelen);
2841
H. Peter Anvin6686de22019-08-10 05:33:14 -07002842 if (context_prefix) {
2843 p -= context_len;
2844 memcpy(p, context_prefix, context_len);
2845 nasm_free(context_prefix);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002846 }
2847
2848 nasm_listmsg("%s %s", pp_directives[op], p);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002849 nasm_free(def);
H. Peter Anvin6686de22019-08-10 05:33:14 -07002850}
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002851
2852/*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002853 * Parse smacro arguments, return argument count. If the tmpl argument
2854 * is set, set the nparam, greedy and params field in the template.
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002855 * *tpp is updated to point to the pointer to the first token after the
2856 * prototype.
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002857 *
2858 * The text values from any argument tokens are "stolen" and the
2859 * corresponding text fields set to NULL.
2860 */
2861static int parse_smacro_template(Token ***tpp, SMacro *tmpl)
2862{
2863 int nparam = 0;
2864 enum sparmflags flags;
2865 struct smac_param *params = NULL;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002866 bool err, done;
2867 bool greedy = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002868 Token **tn = *tpp;
2869 Token *t = *tn;
2870 Token *name;
2871
H. Peter Anvin (Intel)d4607842019-08-20 16:19:37 -07002872 /*
2873 * DO NOT skip whitespace here, or we won't be able to distinguish:
2874 *
2875 * %define foo (a,b) ; no arguments, (a,b) is the expansion
2876 * %define bar(a,b) ; two arguments, empty expansion
2877 *
2878 * This ambiguity was inherited from C.
2879 */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002880
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002881 if (!tok_is(t, '('))
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002882 goto finish;
2883
2884 if (tmpl) {
2885 Token *tx = t;
2886 Token **txpp = &tx;
2887 int sparam;
2888
2889 /* Count parameters first */
2890 sparam = parse_smacro_template(&txpp, NULL);
2891 if (!sparam)
2892 goto finish; /* No parameters, we're done */
2893 nasm_newn(params, sparam);
2894 }
2895
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002896 /* Skip leading paren */
2897 tn = &t->next;
2898 t = *tn;
2899
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002900 name = NULL;
2901 flags = 0;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002902 err = done = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002903
2904 while (!done) {
2905 if (!t || !t->type) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002906 if (name || flags)
2907 nasm_nonfatal("`)' expected to terminate macro template");
2908 else
2909 nasm_nonfatal("parameter identifier expected");
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002910 break;
2911 }
2912
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002913 switch (t->type) {
2914 case TOK_ID:
2915 if (name)
2916 goto bad;
2917 name = t;
2918 break;
2919
2920 case TOK_OTHER:
H. Peter Anvin8571f062019-09-23 16:40:03 -07002921 if (t->len != 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002922 goto bad;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002923 switch (t->text.a[0]) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002924 case '=':
2925 flags |= SPARM_EVAL;
2926 break;
2927 case '&':
2928 flags |= SPARM_STR;
2929 break;
2930 case '!':
2931 flags |= SPARM_NOSTRIP;
2932 break;
2933 case '+':
2934 flags |= SPARM_GREEDY;
2935 greedy = true;
2936 break;
2937 case ',':
2938 if (greedy)
2939 nasm_nonfatal("greedy parameter must be last");
2940 /* fall through */
2941 case ')':
2942 if (params) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002943 if (name)
2944 steal_Token(&params[nparam].name, name);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002945 params[nparam].flags = flags;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002946 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002947 nparam++;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002948 name = NULL;
2949 flags = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002950 done = t->text.a[0] == ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002951 break;
2952 default:
2953 goto bad;
2954 }
2955 break;
2956
2957 case TOK_WHITESPACE:
2958 break;
2959
2960 default:
2961 bad:
2962 if (!err) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002963 nasm_nonfatal("garbage `%s' in macro parameter list", tok_text(t));
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002964 err = true;
2965 }
2966 break;
2967 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002968
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002969 tn = &t->next;
2970 t = *tn;
2971 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002972
2973finish:
2974 while (t && t->type == TOK_WHITESPACE) {
2975 tn = &t->next;
2976 t = t->next;
2977 }
2978 *tpp = tn;
2979 if (tmpl) {
2980 tmpl->nparam = nparam;
2981 tmpl->greedy = greedy;
2982 tmpl->params = params;
2983 }
2984 return nparam;
2985}
2986
2987/*
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002988 * Common code for defining an smacro. The tmpl argument, if not NULL,
2989 * contains any macro parameters that aren't explicit arguments;
2990 * those are the more uncommon macro variants.
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002991 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002992static SMacro *define_smacro(const char *mname, bool casesense,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002993 Token *expansion, SMacro *tmpl)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002994{
2995 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002996 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002997 Context *ctx;
2998 bool defining_alias = false;
2999 unsigned int nparam = 0;
H. Peter Anvin70653092007-10-19 14:42:29 -07003000
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003001 if (tmpl) {
3002 defining_alias = tmpl->alias;
3003 nparam = tmpl->nparam;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003004 if (nparam && !defining_alias)
3005 mark_smac_params(expansion, tmpl, 0);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003006 }
3007
3008 while (1) {
3009 ctx = get_ctx(mname, &mname);
3010
H. Peter Anvind2354082019-08-27 16:38:48 -07003011 if (!smacro_defined(ctx, mname, nparam, &smac, casesense, true)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003012 /* Create a new macro */
3013 smtbl = ctx ? &ctx->localmac : &smacros;
3014 smhead = (SMacro **) hash_findi_add(smtbl, mname);
3015 nasm_new(smac);
3016 smac->next = *smhead;
3017 *smhead = smac;
3018 break;
3019 } else if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003020 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003021 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003022 /*
3023 * Some instances of the old code considered this a failure,
3024 * some others didn't. What is the right thing to do here?
3025 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003026 goto fail;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003027 } else if (!smac->alias || ppopt.noaliases || defining_alias) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003028 /*
3029 * We're redefining, so we have to take over an
3030 * existing SMacro structure. This means freeing
H. Peter Anvin8b262472019-02-26 14:00:54 -08003031 * what was already in it, but not the structure itself.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003032 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003033 clear_smacro(smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003034 break;
3035 } else if (smac->in_progress) {
3036 nasm_nonfatal("macro alias loop");
3037 goto fail;
3038 } else {
3039 /* It is an alias macro; follow the alias link */
3040 SMacro *s;
3041
3042 smac->in_progress = true;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003043 s = define_smacro(tok_text(smac->expansion), casesense,
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003044 expansion, tmpl);
3045 smac->in_progress = false;
3046 return s;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003047 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003048 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003049
3050 smac->name = nasm_strdup(mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003051 smac->casesense = casesense;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003052 smac->expansion = expansion;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003053 smac->expand = smacro_expand_default;
3054 if (tmpl) {
3055 smac->nparam = tmpl->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003056 smac->params = tmpl->params;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003057 smac->alias = tmpl->alias;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003058 smac->greedy = tmpl->greedy;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003059 if (tmpl->expand)
3060 smac->expand = tmpl->expand;
3061 }
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07003062 if (list_option('s')) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07003063 list_smacro_def((smac->alias ? PP_DEFALIAS : PP_DEFINE)
3064 + !casesense, ctx, smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003065 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08003066 return smac;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003067
3068fail:
3069 free_tlist(expansion);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003070 if (tmpl)
3071 free_smacro_members(tmpl);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003072 return NULL;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003073}
3074
3075/*
3076 * Undefine an smacro
3077 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003078static void undef_smacro(const char *mname, bool undefalias)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003079{
3080 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003081 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003082 Context *ctx;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003083
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003084 ctx = get_ctx(mname, &mname);
H. Peter Anvin166c2472008-05-28 12:28:58 -07003085 smtbl = ctx ? &ctx->localmac : &smacros;
3086 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07003087
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003088 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003089 /*
3090 * We now have a macro name... go hunt for it.
3091 */
3092 sp = smhead;
3093 while ((s = *sp) != NULL) {
3094 if (!mstrcmp(s->name, mname, s->casesense)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003095 if (s->alias && !undefalias) {
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003096 if (!ppopt.noaliases) {
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003097 if (s->in_progress) {
3098 nasm_nonfatal("macro alias loop");
3099 } else {
3100 s->in_progress = true;
3101 undef_smacro(tok_text(s->expansion), false);
3102 s->in_progress = false;
3103 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003104 }
3105 } else {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07003106 if (list_option('d'))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003107 list_smacro_def(s->alias ? PP_UNDEFALIAS : PP_UNDEF,
3108 ctx, s);
3109 *sp = s->next;
3110 free_smacro(s);
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003111 continue;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003112 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003113 }
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003114 sp = &s->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003115 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003116 }
3117}
3118
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003119/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07003120 * Parse a mmacro specification.
3121 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003122static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07003123{
H. Peter Anvina26433d2008-07-16 14:40:01 -07003124 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003125 tline = skip_white(tline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003126 tline = expand_id(tline);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003127 if (!tok_type(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003128 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003129 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003130 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02003131
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003132#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003133 def->prev = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003134#endif
H. Peter Anvin8571f062019-09-23 16:40:03 -07003135 def->name = dup_text(tline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003136 def->plus = false;
3137 def->nolist = false;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02003138 def->nparam_min = 0;
3139 def->nparam_max = 0;
3140
H. Peter Anvina26433d2008-07-16 14:40:01 -07003141 tline = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003142 tline = skip_white(tline);
3143 if (!tok_type(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003144 nasm_nonfatal("`%s' expects a parameter count", directive);
3145 else
H. Peter Anvin8571f062019-09-23 16:40:03 -07003146 def->nparam_min = def->nparam_max = read_param_count(tok_text(tline));
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003147 if (tline && tok_is(tline->next, '-')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003148 tline = tline->next->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003149 if (tok_is(tline, '*')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003150 def->nparam_max = INT_MAX;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003151 } else if (!tok_type(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003152 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003153 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003154 def->nparam_max = read_param_count(tok_text(tline));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003155 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003156 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03003157 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003158 }
3159 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003160 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003161 if (tline && tok_is(tline->next, '+')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003162 tline = tline->next;
3163 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003164 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003165 if (tline && tok_type(tline->next, TOK_ID) &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07003166 tline->next->len == 7 &&
3167 !nasm_stricmp(tline->next->text.a, ".nolist")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003168 tline = tline->next;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003169 def->nolist = !list_option('f') || istk->nolist;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003170 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003171
H. Peter Anvina26433d2008-07-16 14:40:01 -07003172 /*
3173 * Handle default parameters.
3174 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07003175 def->ndefs = 0;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003176 if (tline && tline->next) {
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07003177 Token **comma;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003178 def->dlist = tline->next;
3179 tline->next = NULL;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07003180 comma = count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
3181 if (!ppopt.sane_empty_expansion && comma) {
3182 *comma = NULL;
3183 def->ndefs--;
3184 nasm_warn(WARN_MACRO_PARAMS_LEGACY,
3185 "dropping trailing empty default parameter in defintion of multi-line macro `%s'",
3186 def->name);
3187 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003188 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003189 def->dlist = NULL;
3190 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003191 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003192 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003193
H. Peter Anvin89cee572009-07-15 09:16:54 -04003194 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003195 !def->plus) {
3196 /*
3197 *!macro-defaults [on] macros with more default than optional parameters
3198 *! warns when a macro has more default parameters than optional parameters.
3199 *! See \k{mlmacdef} for why might want to disable this warning.
3200 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003201 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003202 "too many default macro parameters in macro `%s'", def->name);
3203 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02003204
H. Peter Anvina26433d2008-07-16 14:40:01 -07003205 return true;
3206}
3207
3208
3209/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003210 * Decode a size directive
3211 */
3212static int parse_size(const char *str) {
3213 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003214 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003215 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003216 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03003217 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003218}
3219
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003220/*
3221 * Process a preprocessor %pragma directive. Currently there are none.
3222 * Gets passed the token list starting with the "preproc" token from
3223 * "%pragma preproc".
3224 */
3225static void do_pragma_preproc(Token *tline)
3226{
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003227 const char *txt;
3228
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003229 /* Skip to the real stuff */
3230 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003231 tline = skip_white(tline);
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003232
3233 if (!tok_type(tline, TOK_ID))
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003234 return;
3235
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003236 txt = tok_text(tline);
3237 if (!nasm_stricmp(txt, "sane_empty_expansion")) {
3238 tline = skip_white(tline->next);
3239 ppopt.sane_empty_expansion =
3240 pp_get_boolean_option(tline, ppopt.sane_empty_expansion);
3241 } else {
3242 /* Unknown pragma, ignore for now */
3243 }
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003244}
3245
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003246static bool is_macro_id(const Token *t)
3247{
H. Peter Anvin8571f062019-09-23 16:40:03 -07003248 return tok_type(t, TOK_ID) || tok_type(t, TOK_LOCAL_MACRO);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003249}
3250
H. Peter Anvin8571f062019-09-23 16:40:03 -07003251static const char *get_id(Token **tp, const char *dname)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003252{
H. Peter Anvin8571f062019-09-23 16:40:03 -07003253 const char *id;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003254 Token *t = *tp;
3255
3256 t = t->next; /* Skip directive */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003257 t = skip_white(t);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003258 t = expand_id(t);
3259
3260 if (!is_macro_id(t)) {
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003261 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003262 return NULL;
3263 }
3264
H. Peter Anvin8571f062019-09-23 16:40:03 -07003265 id = tok_text(t);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003266 t = skip_white(t);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003267 *tp = t;
3268 return id;
3269}
3270
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003271/* Parse a %use package name and find the package. Set *err on syntax error. */
3272static const struct use_package *
H. Peter Anvin8571f062019-09-23 16:40:03 -07003273get_use_pkg(Token *t, const char *dname, const char **name)
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003274{
H. Peter Anvin8571f062019-09-23 16:40:03 -07003275 const char *id;
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003276
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003277 t = skip_white(t);
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003278 t = expand_smacro(t);
3279
H. Peter Anvin8571f062019-09-23 16:40:03 -07003280 *name = NULL;
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003281
H. Peter Anvin8571f062019-09-23 16:40:03 -07003282 if (!t) {
3283 nasm_nonfatal("`%s' expects a package name, got end of line", dname);
3284 return NULL;
3285 } else if (t->type != TOK_ID && t->type != TOK_STRING) {
3286 nasm_nonfatal("`%s' expects a package name, got `%s'",
3287 dname, tok_text(t));
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003288 return NULL;
3289 }
3290
H. Peter Anvin8571f062019-09-23 16:40:03 -07003291 *name = id = unquote_token(t);
3292
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003293 t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003294 t = skip_white(t);
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003295 if (t)
3296 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
3297
3298 return nasm_find_use_package(id);
3299}
3300
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003301/*
3302 * Mark parameter tokens in an smacro definition. If the type argument
3303 * is 0, create smac param tokens, otherwise use the type specified;
3304 * normally this is used for TOK_XDEF_PARAM, which is used to protect
3305 * parameter tokens during expansion during %xdefine.
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003306 *
3307 * tmpl may not be NULL here.
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003308 */
3309static void mark_smac_params(Token *tline, const SMacro *tmpl,
3310 enum pp_token_type type)
3311{
3312 const struct smac_param *params = tmpl->params;
3313 int nparam = tmpl->nparam;
3314 Token *t;
3315 int i;
3316
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003317 list_for_each(t, tline) {
3318 if (t->type != TOK_ID && t->type != TOK_XDEF_PARAM)
3319 continue;
3320
3321 for (i = 0; i < nparam; i++) {
3322 if (tok_text_match(t, &params[i].name))
3323 t->type = type ? type : tok_smac_param(i);
3324 }
3325 }
3326}
3327
Ed Beroset3ab3f412002-06-11 03:31:49 +00003328/**
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003329 * %clear selected macro sets either globally or in contexts
3330 */
3331static void do_clear(enum clear_what what, bool context)
3332{
3333 if (context) {
3334 if (what & CLEAR_ALLDEFINE) {
3335 Context *ctx;
3336 list_for_each(ctx, cstk)
3337 clear_smacro_table(&ctx->localmac, what);
3338 }
3339 /* Nothing else can be context-local */
3340 } else {
3341 if (what & CLEAR_ALLDEFINE)
3342 clear_smacro_table(&smacros, what);
3343 if (what & CLEAR_MMACRO)
3344 free_mmacro_table(&mmacros);
3345 }
3346}
3347
3348/**
Ed Beroset3ab3f412002-06-11 03:31:49 +00003349 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003350 * Find out if a line contains a preprocessor directive, and deal
3351 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07003352 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00003353 * If a directive _is_ found, it is the responsibility of this routine
3354 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003355 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00003356 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003357 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00003358 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07003359 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003360 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07003361static int do_directive(Token *tline, Token **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003362{
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003363 enum preproc_token op;
H. Peter Anvin4169a472007-09-12 01:29:43 +00003364 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07003365 bool err;
H. Peter Anvin70055962007-10-11 00:05:31 -07003366 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003367 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07003368 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003369 int offset;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003370 const char *p;
3371 char *q, *qbuf;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003372 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003373 const char *mname;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003374 struct ppscan pps;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003375 Include *inc;
3376 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003377 Cond *cond;
3378 MMacro *mmac, **mmhead;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003379 Token *t = NULL, *tt, *macro_start, *last, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003380 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003381 struct tokenval tokval;
3382 expr *evalresult;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003383 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003384 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08003385 errflags severity;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003386 const char *dname; /* Name of directive, for messages */
H. Peter Anvin76690a12002-04-30 20:52:49 +00003387
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003388 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00003389 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003390
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003391 tline = skip_white(tline);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003392 if (!tline || !tok_type(tline, TOK_PREPROC_ID))
3393 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003394
H. Peter Anvin8571f062019-09-23 16:40:03 -07003395 dname = tok_text(tline);
3396 if (dname[1] == '%')
3397 return NO_DIRECTIVE_FOUND;
3398
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003399 op = pp_token_hash(dname);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003400
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003401 casesense = true;
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003402 if (PP_HAS_CASE(op) & PP_INSENSITIVE(op)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003403 casesense = false;
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003404 op--;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003405 }
3406
3407 /*
3408 * If we're in a non-emitting branch of a condition construct,
3409 * or walking to the end of an already terminated %rep block,
3410 * we should ignore all directives except for condition
3411 * directives.
3412 */
3413 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003414 (istk->mstk.mstk && !istk->mstk.mstk->in_progress)) &&
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003415 !is_condition(op)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003416 return NO_DIRECTIVE_FOUND;
3417 }
3418
3419 /*
3420 * If we're defining a macro or reading a %rep block, we should
3421 * ignore all directives except for %macro/%imacro (which nest),
3422 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
3423 * If we're in a %rep block, another %rep nests, so should be let through.
3424 */
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003425 if (defining && op != PP_MACRO && op != PP_RMACRO &&
3426 op != PP_ENDMACRO && op != PP_ENDM &&
3427 (defining->name || (op != PP_ENDREP && op != PP_REP))) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003428 return NO_DIRECTIVE_FOUND;
3429 }
3430
3431 if (defining) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003432 if (op == PP_MACRO || op == PP_RMACRO) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003433 nested_mac_count++;
3434 return NO_DIRECTIVE_FOUND;
3435 } else if (nested_mac_count > 0) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003436 if (op == PP_ENDMACRO) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003437 nested_mac_count--;
3438 return NO_DIRECTIVE_FOUND;
3439 }
3440 }
3441 if (!defining->name) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003442 if (op == PP_REP) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003443 nested_rep_count++;
3444 return NO_DIRECTIVE_FOUND;
3445 } else if (nested_rep_count > 0) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003446 if (op == PP_ENDREP) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003447 nested_rep_count--;
3448 return NO_DIRECTIVE_FOUND;
3449 }
3450 }
3451 }
3452 }
3453
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003454 switch (op) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003455 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003456 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003457 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003458
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003459 case PP_PRAGMA:
3460 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003461 * %pragma namespace options...
3462 *
3463 * The namespace "preproc" is reserved for the preprocessor;
3464 * all other namespaces generate a [pragma] assembly directive.
3465 *
3466 * Invalid %pragmas are ignored and may have different
3467 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003468 */
H. Peter Anvinf5d7d902019-08-10 06:21:00 -07003469 t = tline;
3470 tline = tline->next;
3471 t->next = NULL;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003472 tline = zap_white(expand_smacro(tline));
3473 if (tok_type(tline, TOK_ID)) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003474 if (!nasm_stricmp(tok_text(tline), "preproc")) {
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003475 /* Preprocessor pragma */
3476 do_pragma_preproc(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07003477 free_tlist(tline);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003478 } else {
3479 /* Build the assembler directive */
H. Peter Anvin06335872019-08-10 06:42:55 -07003480
3481 /* Append bracket to the end of the output */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003482 for (t = tline; t->next; t = t->next)
3483 ;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003484 t->next = make_tok_char(NULL, ']');
H. Peter Anvin06335872019-08-10 06:42:55 -07003485
3486 /* Prepend "[pragma " */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003487 t = new_White(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07003488 t = new_Token(t, TOK_ID, "pragma", 6);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003489 t = make_tok_char(t, '[');
H. Peter Anvin06335872019-08-10 06:42:55 -07003490 tline = t;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07003491 *output = tline;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003492 }
3493 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003494 break;
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003495
H. Peter Anvine2c80182005-01-15 22:15:51 +00003496 case PP_STACKSIZE:
3497 /* Directive to tell NASM what the default stack size is. The
3498 * default is for a 16-bit stack, and this can be overriden with
3499 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003500 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003501 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003502 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003503 nasm_nonfatal("`%s' missing size parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003504 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07003505 if (nasm_stricmp(tok_text(tline), "flat") == 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003506 /* All subsequent ARG directives are for a 32-bit stack */
3507 StackSize = 4;
3508 StackPointer = "ebp";
3509 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003510 LocalOffset = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003511 } else if (nasm_stricmp(tok_text(tline), "flat64") == 0) {
Charles Crayne7eaf9192007-11-08 22:11:14 -08003512 /* All subsequent ARG directives are for a 64-bit stack */
3513 StackSize = 8;
3514 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03003515 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08003516 LocalOffset = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003517 } else if (nasm_stricmp(tok_text(tline), "large") == 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003518 /* All subsequent ARG directives are for a 16-bit stack,
3519 * far function call.
3520 */
3521 StackSize = 2;
3522 StackPointer = "bp";
3523 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003524 LocalOffset = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003525 } else if (nasm_stricmp(tok_text(tline), "small") == 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003526 /* All subsequent ARG directives are for a 16-bit stack,
3527 * far function call. We don't support near functions.
3528 */
3529 StackSize = 2;
3530 StackPointer = "bp";
3531 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003532 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003533 } else {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003534 nasm_nonfatal("`%s' invalid size type", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003535 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003536 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003537
H. Peter Anvine2c80182005-01-15 22:15:51 +00003538 case PP_ARG:
3539 /* TASM like ARG directive to define arguments to functions, in
3540 * the following form:
3541 *
3542 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
3543 */
3544 offset = ArgOffset;
3545 do {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003546 const char *arg;
3547 char directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003548 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003549
H. Peter Anvine2c80182005-01-15 22:15:51 +00003550 /* Find the argument name */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003551 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003552 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003553 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003554 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003555 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07003556 arg = tok_text(tline);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003557
H. Peter Anvine2c80182005-01-15 22:15:51 +00003558 /* Find the argument size type */
3559 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003560 if (!tok_is(tline, ':')) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003561 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003562 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003563 }
3564 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003565 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003566 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003567 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003568 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003569
H. Peter Anvine2c80182005-01-15 22:15:51 +00003570 /* Allow macro expansion of type parameter */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003571 tt = tokenize(tok_text(tline));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003572 tt = expand_smacro(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003573 size = parse_size(tok_text(tt));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003574 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003575 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003576 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003577 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003578 }
3579 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003580
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003581 /* Round up to even stack slots */
3582 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003583
H. Peter Anvine2c80182005-01-15 22:15:51 +00003584 /* Now define the macro for the argument */
3585 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
3586 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003587 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003588 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003589
H. Peter Anvine2c80182005-01-15 22:15:51 +00003590 /* Move to the next argument in the list */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003591 tline = skip_white(tline->next);
3592 } while (tok_is(tline, ','));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003593 ArgOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003594 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003595
H. Peter Anvine2c80182005-01-15 22:15:51 +00003596 case PP_LOCAL:
3597 /* TASM like LOCAL directive to define local variables for a
3598 * function, in the following form:
3599 *
3600 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
3601 *
3602 * The '= LocalSize' at the end is ignored by NASM, but is
3603 * required by TASM to define the local parameter size (and used
3604 * by the TASM macro package).
3605 */
3606 offset = LocalOffset;
3607 do {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003608 const char *local;
3609 char directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003610 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003611
H. Peter Anvine2c80182005-01-15 22:15:51 +00003612 /* Find the argument name */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003613 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003614 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003615 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003616 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003617 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07003618 local = tok_text(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003619
H. Peter Anvine2c80182005-01-15 22:15:51 +00003620 /* Find the argument size type */
3621 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003622 if (!tok_is(tline, ':')) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003623 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003624 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003625 }
3626 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003627 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003628 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003629 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003630 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003631
H. Peter Anvine2c80182005-01-15 22:15:51 +00003632 /* Allow macro expansion of type parameter */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003633 tt = tokenize(tok_text(tline));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003634 tt = expand_smacro(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003635 size = parse_size(tok_text(tt));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003636 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003637 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003638 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003639 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003640 }
3641 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003642
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003643 /* Round up to even stack slots */
3644 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003645
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003646 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003647
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003648 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003649 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
3650 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003651 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003652
H. Peter Anvine2c80182005-01-15 22:15:51 +00003653 /* Now define the assign to setup the enter_c macro correctly */
3654 snprintf(directive, sizeof(directive),
3655 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003656 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003657
H. Peter Anvine2c80182005-01-15 22:15:51 +00003658 /* Move to the next argument in the list */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003659 tline = skip_white(tline->next);
3660 } while (tok_is(tline, ','));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003661 LocalOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003662 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003663
H. Peter Anvine2c80182005-01-15 22:15:51 +00003664 case PP_CLEAR:
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003665 {
3666 bool context = false;
3667
3668 t = tline->next = expand_smacro(tline->next);
3669 t = skip_white(t);
3670 if (!t) {
3671 /* Emulate legacy behavior */
3672 do_clear(CLEAR_DEFINE|CLEAR_MMACRO, false);
3673 } else {
3674 while ((t = skip_white(t)) && t->type == TOK_ID) {
3675 const char *txt = tok_text(t);
3676 if (!nasm_stricmp(txt, "all")) {
3677 do_clear(CLEAR_ALL, context);
3678 } else if (!nasm_stricmp(txt, "define") ||
3679 !nasm_stricmp(txt, "def") ||
3680 !nasm_stricmp(txt, "smacro")) {
3681 do_clear(CLEAR_DEFINE, context);
3682 } else if (!nasm_stricmp(txt, "defalias") ||
3683 !nasm_stricmp(txt, "alias") ||
3684 !nasm_stricmp(txt, "salias")) {
3685 do_clear(CLEAR_DEFALIAS, context);
3686 } else if (!nasm_stricmp(txt, "alldef") ||
3687 !nasm_stricmp(txt, "alldefine")) {
3688 do_clear(CLEAR_ALLDEFINE, context);
3689 } else if (!nasm_stricmp(txt, "macro") ||
3690 !nasm_stricmp(txt, "mmacro")) {
3691 do_clear(CLEAR_MMACRO, context);
3692 } else if (!nasm_stricmp(txt, "context") ||
3693 !nasm_stricmp(txt, "ctx")) {
3694 context = true;
3695 } else if (!nasm_stricmp(txt, "global")) {
3696 context = false;
3697 } else if (!nasm_stricmp(txt, "nothing") ||
3698 !nasm_stricmp(txt, "none") ||
3699 !nasm_stricmp(txt, "ignore") ||
3700 !nasm_stricmp(txt, "-") ||
3701 !nasm_stricmp(txt, "--")) {
3702 /* Do nothing */
3703 } else {
3704 nasm_nonfatal("invalid option to %s: %s", dname, txt);
3705 t = NULL;
3706 }
3707 }
3708 }
3709
3710 t = skip_white(t);
3711 if (t)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003712 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003713 break;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003714 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003715
H. Peter Anvin418ca702008-05-30 10:42:30 -07003716 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003717 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003718 t = skip_white(t);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003719 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003720 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003721 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003722 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003723 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003724 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003725 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003726
3727 strlist_add(deplist, unquote_token_cstr(t));
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003728 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003729
3730 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003731 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003732 t = skip_white(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07003733
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003734 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003735 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003736 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003737 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003738 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003739 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003740 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003741 p = unquote_token_cstr(t);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003742 nasm_new(inc);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003743 inc->next = istk;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003744 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07003745 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08003746 (pp_mode == PP_DEPS)
3747 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003748 if (!inc->fp) {
3749 /* -MG given but file not found */
3750 nasm_free(inc);
3751 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003752 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003753 inc->lineno = src_set_linnum(0);
3754 inc->lineinc = 1;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003755 inc->nolist = istk->nolist;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003756 istk = inc;
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003757 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003758 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003759 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003760
H. Peter Anvind2456592008-06-19 15:04:18 -07003761 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003762 {
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003763 const struct use_package *pkg;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003764 const char *name;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003765
H. Peter Anvin8571f062019-09-23 16:40:03 -07003766 pkg = get_use_pkg(tline->next, dname, &name);
3767 if (!name)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003768 goto done;
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003769 if (!pkg) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003770 nasm_nonfatal("unknown `%s' package: `%s'", dname, name);
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003771 } else if (!use_loaded[pkg->index]) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003772 /*
3773 * Not already included, go ahead and include it.
3774 * Treat it as an include file for the purpose of
3775 * producing a listing.
3776 */
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003777 use_loaded[pkg->index] = true;
3778 stdmacpos = pkg->macros;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003779 nasm_new(inc);
3780 inc->next = istk;
3781 inc->fname = src_set_fname(NULL);
3782 inc->lineno = src_set_linnum(0);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003783 inc->nolist = !list_option('b') || istk->nolist;
3784 istk = inc;
3785 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003786 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003787 break;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003788 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003789 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003790 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07003791 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003792 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003793 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003794 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003795 if (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003796 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003797 nasm_nonfatal("`%s' expects a context identifier", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003798 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003799 }
3800 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003801 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003802 dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003803 p = tok_text(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003804 } else {
3805 p = NULL; /* Anonymous */
3806 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07003807
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003808 if (op == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08003809 nasm_new(ctx);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003810 ctx->depth = cstk ? cstk->depth + 1 : 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003811 ctx->next = cstk;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003812 ctx->name = p ? nasm_strdup(p) : NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003813 ctx->number = unique++;
3814 cstk = ctx;
3815 } else {
3816 /* %pop or %repl */
3817 if (!cstk) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003818 nasm_nonfatal("`%s': context stack is empty", dname);
3819 } else if (op == PP_POP) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003820 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003821 nasm_nonfatal("`%s' in wrong context: %s, "
H. Peter Anvin8b262472019-02-26 14:00:54 -08003822 "expected %s",
3823 dname, cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003824 else
3825 ctx_pop();
3826 } else {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003827 /* op == PP_REPL */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003828 nasm_free((char *)cstk->name);
3829 cstk->name = p ? nasm_strdup(p) : NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003830 p = NULL;
3831 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003832 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003833 break;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003834 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003835 severity = ERR_FATAL;
3836 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003837 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003838 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003839 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003840 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003841 /*!
3842 *!user [on] %warning directives
3843 *! controls output of \c{%warning} directives (see \k{pperror}).
3844 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003845 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003846 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003847
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003848issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07003849 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003850 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003851 tline->next = expand_smacro(tline->next);
3852 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003853 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003854 t = tline ? tline->next : NULL;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003855 t = skip_white(t);
3856 if (tok_type(tline, TOK_STRING) && !t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003857 /* The line contains only a quoted string */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003858 p = unquote_token(tline); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08003859 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003860 } else {
3861 /* Not a quoted string, or more than a quoted string */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003862 q = detoken(tline, false);
3863 nasm_error(severity, "%s", q);
3864 nasm_free(q);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003865 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003866 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003867 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003868
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003869 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003870 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003871 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003872 else {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003873 j = if_condition(tline->next, op);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003874 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003875 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003876 cond = nasm_malloc(sizeof(Cond));
3877 cond->next = istk->conds;
3878 cond->state = j;
3879 istk->conds = cond;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003880 if(istk->mstk.mstk)
3881 istk->mstk.mstk->condcnt++;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003882 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003883
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003884 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003885 if (!istk->conds)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003886 nasm_fatal("`%s': no matching `%%if'", dname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003887 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003888 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003889 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003890 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003891
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003892 case COND_DONE:
3893 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003894 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003895
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003896 case COND_ELSE_TRUE:
3897 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003898 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003899 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003900 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003901 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003902
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003903 case COND_IF_FALSE:
3904 /*
3905 * IMPORTANT: In the case of %if, we will already have
3906 * called expand_mmac_params(); however, if we're
3907 * processing an %elif we must have been in a
3908 * non-emitting mode, which would have inhibited
3909 * the normal invocation of expand_mmac_params().
3910 * Therefore, we have to do it explicitly here.
3911 */
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003912 j = if_condition(expand_mmac_params(tline->next), op);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003913 tline->next = NULL; /* it got freed */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003914 istk->conds->state = j;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003915 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003916 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003917 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003918
H. Peter Anvine2c80182005-01-15 22:15:51 +00003919 case PP_ELSE:
3920 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003921 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003922 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003923 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07003924 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003925 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003926 case COND_IF_TRUE:
3927 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003928 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003929 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003930
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003931 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003932 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003933
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003934 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003935 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003936 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003937
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003938 case COND_ELSE_TRUE:
3939 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003940 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003941 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003942 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003943 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003944 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003945 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003946
H. Peter Anvine2c80182005-01-15 22:15:51 +00003947 case PP_ENDIF:
3948 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003949 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003950 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003951 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003952 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003953 cond = istk->conds;
3954 istk->conds = cond->next;
3955 nasm_free(cond);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003956 if(istk->mstk.mstk)
3957 istk->mstk.mstk->condcnt--;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003958 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003959
H. Peter Anvin8b262472019-02-26 14:00:54 -08003960 case PP_RMACRO:
3961 case PP_MACRO:
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07003962 {
3963 MMacro *def;
3964
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003965 nasm_assert(!defining);
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07003966 nasm_new(def);
3967 def->casesense = casesense;
3968 def->dstk.mmac = defining;
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003969 if (op == PP_RMACRO)
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07003970 def->max_depth = nasm_limit[LIMIT_MACRO_LEVELS];
3971 if (!parse_mmacro_spec(tline, def, dname)) {
3972 nasm_free(def);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003973 goto done;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003974 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003975
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07003976 defining = def;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07003977 src_get(&defining->xline, &defining->fname);
3978
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003979 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
3980 while (mmac) {
3981 if (!strcmp(mmac->name, defining->name) &&
3982 (mmac->nparam_min <= defining->nparam_max
3983 || defining->plus)
3984 && (defining->nparam_min <= mmac->nparam_max
3985 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003986 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003987 defining->name);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003988 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003989 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003990 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003991 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003992 break;
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07003993 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003994
H. Peter Anvine2c80182005-01-15 22:15:51 +00003995 case PP_ENDM:
3996 case PP_ENDMACRO:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003997 if (!(defining && defining->name)) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003998 nasm_nonfatal("`%s': not defining a macro", tok_text(tline));
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003999 goto done;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004000 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004001 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
4002 defining->next = *mmhead;
4003 *mmhead = defining;
4004 defining = NULL;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004005 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004006
H. Peter Anvin89cee572009-07-15 09:16:54 -04004007 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004008 /*
4009 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004010 * macro-end marker for a macro with a name. Then we
4011 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004012 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004013 list_for_each(l, istk->expansion)
4014 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004015 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004016
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004017 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004018 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004019 * Remove all conditional entries relative to this
4020 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004021 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004022 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
4023 cond = istk->conds;
4024 istk->conds = cond->next;
4025 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004026 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004027 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004028 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004029 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004030 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004031 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05004032
H. Peter Anvina26433d2008-07-16 14:40:01 -07004033 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08004034 casesense = false;
4035 /* fall through */
4036 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07004037 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004038 MMacro **mmac_p;
4039 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07004040
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004041 nasm_zero(spec);
H. Peter Anvin8b262472019-02-26 14:00:54 -08004042 spec.casesense = casesense;
4043 if (!parse_mmacro_spec(tline, &spec, dname)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004044 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004045 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004046 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
4047 while (mmac_p && *mmac_p) {
4048 mmac = *mmac_p;
4049 if (mmac->casesense == spec.casesense &&
4050 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
4051 mmac->nparam_min == spec.nparam_min &&
4052 mmac->nparam_max == spec.nparam_max &&
4053 mmac->plus == spec.plus) {
4054 *mmac_p = mmac->next;
4055 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004056 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004057 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004058 }
4059 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004060 free_tlist(spec.dlist);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004061 break;
H. Peter Anvina26433d2008-07-16 14:40:01 -07004062 }
4063
H. Peter Anvine2c80182005-01-15 22:15:51 +00004064 case PP_ROTATE:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004065 while (tok_white(tline->next))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004066 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04004067 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004068 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004069 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00004070 return DIRECTIVE_FOUND;
4071 }
4072 t = expand_smacro(tline->next);
4073 tline->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004074 pps.tptr = tline = t;
4075 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004076 tokval.t_type = TOKEN_INVALID;
4077 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004078 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004079 free_tlist(tline);
4080 if (!evalresult)
4081 return DIRECTIVE_FOUND;
4082 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004083 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00004084 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004085 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00004086 return DIRECTIVE_FOUND;
4087 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004088 mmac = istk->mstk.mmac;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004089 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004090 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004091 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004092 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004093 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004094 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004095
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004096 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004097 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004098 rotate += mmac->nparam;
4099
4100 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004101 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004102 break;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004103
H. Peter Anvine2c80182005-01-15 22:15:51 +00004104 case PP_REP:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004105 {
4106 MMacro *tmp_defining;
4107
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004108 nolist = false;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004109 tline = skip_white(tline->next);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004110 if (tok_type(tline, TOK_ID) && tline->len == 7 &&
4111 !nasm_memicmp(tline->text.a, ".nolist", 7)) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07004112 nolist = !list_option('f') || istk->nolist;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004113 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004114 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004115
H. Peter Anvine2c80182005-01-15 22:15:51 +00004116 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08004117 pps.tptr = expand_smacro(tline);
4118 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004119 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004120 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004121 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004122 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004123 if (!evalresult)
4124 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004125 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004126 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00004127 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004128 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004129 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004130 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04004131 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07004132 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004133 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
4134 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04004135 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07004136 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08004137 /*!
4138 *!negative-rep [on] regative %rep count
4139 *! warns about negative counts given to the \c{%rep}
4140 *! preprocessor directive.
4141 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08004142 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07004143 "negative `%%rep' count: %"PRId64, count);
4144 count = 0;
4145 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04004146 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07004147 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004148 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004149 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07004150 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004151 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004152 tmp_defining = defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07004153 nasm_new(defining);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004154 defining->nolist = nolist;
4155 defining->in_progress = count;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004156 defining->mstk = istk->mstk;
4157 defining->dstk.mstk = tmp_defining;
4158 defining->dstk.mmac = tmp_defining ? tmp_defining->dstk.mmac : NULL;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07004159 src_get(&defining->xline, &defining->fname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004160 break;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004161 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004162
H. Peter Anvine2c80182005-01-15 22:15:51 +00004163 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004164 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004165 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004166 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004167 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004168
H. Peter Anvine2c80182005-01-15 22:15:51 +00004169 /*
4170 * Now we have a "macro" defined - although it has no name
4171 * and we won't be entering it in the hash tables - we must
4172 * push a macro-end marker for it on to istk->expansion.
4173 * After that, it will take care of propagating itself (a
4174 * macro-end marker line for a macro which is really a %rep
4175 * block will cause the macro to be re-expanded, complete
4176 * with another macro-end marker to ensure the process
4177 * continues) until the whole expansion is forcibly removed
4178 * from istk->expansion by a %exitrep.
4179 */
H. Peter Anvin6686de22019-08-10 05:33:14 -07004180 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004181 l->next = istk->expansion;
4182 l->finishes = defining;
4183 l->first = NULL;
4184 istk->expansion = l;
4185
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004186 istk->mstk.mstk = defining;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004187
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07004188 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004189 defining = defining->dstk.mstk;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004190 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004191
H. Peter Anvine2c80182005-01-15 22:15:51 +00004192 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004193 /*
4194 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004195 * macro-end marker for a macro with no name. Then we set
4196 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004197 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004198 list_for_each(l, istk->expansion)
4199 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004200 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004201
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004202 if (l)
H. Peter Anvind983b622019-10-07 21:19:32 -07004203 l->finishes->in_progress = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004204 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004205 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004206 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004207
H. Peter Anvin8b262472019-02-26 14:00:54 -08004208 case PP_DEFINE:
4209 case PP_XDEFINE:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004210 case PP_DEFALIAS:
H. Peter Anvin8b262472019-02-26 14:00:54 -08004211 {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004212 SMacro tmpl;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004213 Token **lastp;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07004214 int nparam;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07004215
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004216 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004217 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004218
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004219 nasm_zero(tmpl);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004220 lastp = &tline->next;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07004221 nparam = parse_smacro_template(&lastp, &tmpl);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004222 tline = *lastp;
4223 *lastp = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004224
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004225 if (unlikely(op == PP_DEFALIAS)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004226 macro_start = tline;
4227 if (!is_macro_id(macro_start)) {
4228 nasm_nonfatal("`%s' expects a macro identifier to alias",
4229 dname);
4230 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004231 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004232 tt = macro_start->next;
4233 macro_start->next = NULL;
4234 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004235 tline = skip_white(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004236 if (tline && tline->type) {
4237 nasm_warn(WARN_OTHER,
4238 "trailing garbage after aliasing identifier ignored");
4239 }
4240 free_tlist(tt);
4241 tmpl.alias = true;
4242 } else {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004243 if (op == PP_XDEFINE) {
4244 /* Protect macro parameter tokens */
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07004245 if (nparam)
4246 mark_smac_params(tline, &tmpl, TOK_XDEF_PARAM);
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004247 tline = expand_smacro(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004248 }
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004249 /* NB: Does this still make sense? */
4250 macro_start = reverse_tokens(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004251 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004252
H. Peter Anvine2c80182005-01-15 22:15:51 +00004253 /*
4254 * Good. We now have a macro name, a parameter count, and a
4255 * token list (in reverse order) for an expansion. We ought
4256 * to be OK just to create an SMacro, store it, and let
4257 * free_tlist have the rest of the line (which we have
4258 * carefully re-terminated after chopping off the expansion
4259 * from the end).
4260 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004261 define_smacro(mname, casesense, macro_start, &tmpl);
4262 break;
4263 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004264
H. Peter Anvine2c80182005-01-15 22:15:51 +00004265 case PP_UNDEF:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004266 case PP_UNDEFALIAS:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004267 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004268 goto done;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004269 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004270 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004271
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004272 undef_smacro(mname, op == PP_UNDEFALIAS);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004273 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004274
H. Peter Anvin8b262472019-02-26 14:00:54 -08004275 case PP_DEFSTR:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004276 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004277 goto done;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004278
H. Peter Anvin9e200162008-06-04 17:23:14 -07004279 last = tline;
4280 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004281 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004282
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004283 tline = zap_white(tline);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004284 q = detoken(tline, false);
4285 macro_start = make_tok_qstr(NULL, q);
4286 nasm_free(q);
H. Peter Anvin9e200162008-06-04 17:23:14 -07004287
4288 /*
4289 * We now have a macro name, an implicit parameter count of
4290 * zero, and a string token to use as an expansion. Create
4291 * and store an SMacro.
4292 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004293 define_smacro(mname, casesense, macro_start, NULL);
4294 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004295
H. Peter Anvin8b262472019-02-26 14:00:54 -08004296 case PP_DEFTOK:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004297 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004298 goto done;
4299
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004300 last = tline;
4301 tline = expand_smacro(tline->next);
4302 last->next = NULL;
4303
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004304 t = skip_white(tline);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004305 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004306 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004307 nasm_nonfatal("`%s' requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004308 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004309 goto done;
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004310 }
4311
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04004312 /*
4313 * Convert the string to a token stream. Note that smacros
4314 * are stored with the token stream reversed, so we have to
4315 * reverse the output of tokenize().
4316 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07004317 macro_start = reverse_tokens(tokenize(unquote_token_cstr(t)));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004318
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004319 /*
4320 * We now have a macro name, an implicit parameter count of
4321 * zero, and a numeric token to use as an expansion. Create
4322 * and store an SMacro.
4323 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004324 define_smacro(mname, casesense, macro_start, NULL);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004325 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004326 break;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004327
H. Peter Anvin418ca702008-05-30 10:42:30 -07004328 case PP_PATHSEARCH:
4329 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07004330 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004331
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004332 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004333 goto done;
4334
H. Peter Anvin418ca702008-05-30 10:42:30 -07004335 last = tline;
4336 tline = expand_smacro(tline->next);
4337 last->next = NULL;
4338
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004339 t = skip_white(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004340 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004341 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004342 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004343 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004344 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004345 }
4346 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004347 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004348
4349 p = unquote_token_cstr(t);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004350
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07004351 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07004352 if (!found_path)
4353 found_path = p;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004354 macro_start = make_tok_qstr(NULL, found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004355
4356 /*
4357 * We now have a macro name, an implicit parameter count of
4358 * zero, and a string token to use as an expansion. Create
4359 * and store an SMacro.
4360 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004361 define_smacro(mname, casesense, macro_start, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004362 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004363 break;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004364 }
4365
H. Peter Anvine2c80182005-01-15 22:15:51 +00004366 case PP_STRLEN:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004367 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004368 goto done;
4369
H. Peter Anvine2c80182005-01-15 22:15:51 +00004370 last = tline;
4371 tline = expand_smacro(tline->next);
4372 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004373
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004374 t = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004375 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004376 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004377 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004378 free_tlist(tline);
4379 free_tlist(origline);
4380 return DIRECTIVE_FOUND;
4381 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004382
H. Peter Anvin8571f062019-09-23 16:40:03 -07004383 unquote_token(t);
4384 macro_start = make_tok_num(NULL, t->len);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004385
H. Peter Anvine2c80182005-01-15 22:15:51 +00004386 /*
4387 * We now have a macro name, an implicit parameter count of
4388 * zero, and a numeric token to use as an expansion. Create
4389 * and store an SMacro.
4390 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004391 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004392 free_tlist(tline);
4393 free_tlist(origline);
4394 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004395
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004396 case PP_STRCAT:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004397 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004398 goto done;
4399
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004400 last = tline;
4401 tline = expand_smacro(tline->next);
4402 last->next = NULL;
4403
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004404 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004405 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004406 switch (t->type) {
4407 case TOK_WHITESPACE:
4408 break;
4409 case TOK_STRING:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004410 unquote_token(t);
4411 len += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004412 break;
4413 case TOK_OTHER:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004414 if (tok_is(t, ',')) /* permit comma separators */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004415 break;
4416 /* else fall through */
4417 default:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004418 nasm_nonfatal("non-string passed to `%s': %s", dname,
4419 tok_text(t));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004420 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004421 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004422 }
4423 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004424
H. Peter Anvin (Intel)f770ce82019-10-17 18:22:43 -07004425 q = qbuf = nasm_malloc(len+1);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004426 list_for_each(t, tline) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004427 if (t->type == TOK_INTERNAL_STRING)
4428 q = mempcpy(q, tok_text(t), t->len);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004429 }
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07004430 *q = '\0';
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004431
4432 /*
4433 * We now have a macro name, an implicit parameter count of
4434 * zero, and a numeric token to use as an expansion. Create
4435 * and store an SMacro.
4436 */
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07004437 macro_start = make_tok_qstr_len(NULL, qbuf, len);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004438 nasm_free(qbuf);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004439 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004440 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004441 break;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004442
H. Peter Anvine2c80182005-01-15 22:15:51 +00004443 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004444 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004445 int64_t start, count;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004446 const char *txt;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004447 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07004448
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004449 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004450 goto done;
4451
H. Peter Anvine2c80182005-01-15 22:15:51 +00004452 last = tline;
4453 tline = expand_smacro(tline->next);
4454 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004455
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04004456 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004457 t = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004458
4459 t = skip_white(t);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004460
H. Peter Anvine2c80182005-01-15 22:15:51 +00004461 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004462 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004463 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004464 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004465 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004466 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004467
H. Peter Anvin8b262472019-02-26 14:00:54 -08004468 pps.tptr = t->next;
4469 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004470 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004471 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004472 if (!evalresult) {
4473 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004474 goto done;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004475 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004476 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004477 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004478 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004479 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004480 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004481
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004482 pps.tptr = skip_white(pps.tptr);
H. Peter Anvin8b262472019-02-26 14:00:54 -08004483 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004484 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004485 } else {
4486 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004487 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004488 if (!evalresult) {
4489 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004490 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004491 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004492 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004493 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004494 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004495 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004496 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004497 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004498
H. Peter Anvin8571f062019-09-23 16:40:03 -07004499 unquote_token(t);
4500 len = t->len;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004501
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04004502 /* make start and count being in range */
4503 if (start < 0)
4504 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004505 if (count < 0)
4506 count = len + count + 1 - start;
4507 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04004508 count = len - start;
4509 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004510 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004511
H. Peter Anvin8571f062019-09-23 16:40:03 -07004512 txt = (start < 0) ? "" : tok_text(t) + start;
4513 len = count;
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07004514 macro_start = make_tok_qstr_len(NULL, txt, len);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004515
H. Peter Anvine2c80182005-01-15 22:15:51 +00004516 /*
4517 * We now have a macro name, an implicit parameter count of
4518 * zero, and a numeric token to use as an expansion. Create
4519 * and store an SMacro.
4520 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004521 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004522 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004523 break;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004524 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004525
H. Peter Anvin8b262472019-02-26 14:00:54 -08004526 case PP_ASSIGN:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004527 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004528 goto done;
4529
H. Peter Anvine2c80182005-01-15 22:15:51 +00004530 last = tline;
4531 tline = expand_smacro(tline->next);
4532 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004533
H. Peter Anvin8b262472019-02-26 14:00:54 -08004534 pps.tptr = tline;
4535 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004536 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004537 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004538 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004539 if (!evalresult)
4540 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004541
H. Peter Anvine2c80182005-01-15 22:15:51 +00004542 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004543 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00004544
H. Peter Anvine2c80182005-01-15 22:15:51 +00004545 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004546 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004547 free_tlist(origline);
4548 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004549 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004550
H. Peter Anvin8571f062019-09-23 16:40:03 -07004551 macro_start = make_tok_num(NULL, reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00004552
H. Peter Anvine2c80182005-01-15 22:15:51 +00004553 /*
4554 * We now have a macro name, an implicit parameter count of
4555 * zero, and a numeric token to use as an expansion. Create
4556 * and store an SMacro.
4557 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004558 define_smacro(mname, casesense, macro_start, NULL);
4559 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004560
H. Peter Anvind2354082019-08-27 16:38:48 -07004561 case PP_ALIASES:
4562 tline = tline->next;
4563 tline = expand_smacro(tline);
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07004564 ppopt.noaliases = !pp_get_boolean_option(tline, !ppopt.noaliases);
H. Peter Anvind2354082019-08-27 16:38:48 -07004565 break;
4566
H. Peter Anvine2c80182005-01-15 22:15:51 +00004567 case PP_LINE:
4568 /*
4569 * Syntax is `%line nnn[+mmm] [filename]'
4570 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004571 if (unlikely(pp_noline))
4572 goto done;
4573
H. Peter Anvine2c80182005-01-15 22:15:51 +00004574 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004575 tline = skip_white(tline);
4576 if (!tok_type(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004577 nasm_nonfatal("`%s' expects line number", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004578 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004579 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07004580 k = readnum(tok_text(tline), &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004581 m = 1;
4582 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004583 if (tok_is(tline, '+')) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004584 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004585 if (!tok_type(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004586 nasm_nonfatal("`%s' expects line increment", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004587 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004588 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07004589 m = readnum(tok_text(tline), &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004590 tline = tline->next;
4591 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004592 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004593 src_set_linnum(k);
4594 istk->lineinc = m;
4595 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07004596 char *fname = detoken(tline, false);
4597 src_set_fname(fname);
4598 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004599 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004600 break;
4601 }
4602
4603done:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004604 free_tlist(origline);
4605 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004606}
4607
4608/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00004609 * Ensure that a macro parameter contains a condition code and
4610 * nothing else. Return the condition code index if so, or -1
4611 * otherwise.
4612 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004613static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004614{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004615 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004616
H. Peter Anvin25a99342007-09-22 17:45:45 -07004617 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004618 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07004619
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004620 t = skip_white(t);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004621 if (!tok_type(t, TOK_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004622 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004623 tt = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004624 tt = skip_white(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004625 if (tok_isnt(tt, ','))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004626 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004627
H. Peter Anvin8571f062019-09-23 16:40:03 -07004628 return bsii(tok_text(t), (const char **)conditions,
4629 ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00004630}
4631
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004632static inline bool pp_concat_match(const Token *t, unsigned int mask)
4633{
4634 return t && (PP_CONCAT_MASK(t->type) & mask);
4635}
4636
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004637/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004638 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004639 * pasting, if @handle_explicit passed then explicit pasting
4640 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004641 * The @m array can contain a series of token types which are
4642 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004643 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004644static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004645 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07004646{
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004647 Token *tok, *t, *next, **prev_next, **prev_nonspace;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004648 bool pasted = false;
4649 char *buf, *p;
4650 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07004651
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004652 /*
4653 * The last token before pasting. We need it
4654 * to be able to connect new handled tokens.
4655 * In other words if there were a tokens stream
4656 *
4657 * A -> B -> C -> D
4658 *
4659 * and we've joined tokens B and C, the resulting
4660 * stream should be
4661 *
4662 * A -> BC -> D
4663 */
4664 tok = *head;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004665 prev_next = prev_nonspace = head;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004666
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004667 if (tok_white(tok) || tok_type(tok, TOK_PASTE))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004668 prev_nonspace = NULL;
4669
4670 while (tok && (next = tok->next)) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004671 bool did_paste = false;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004672
4673 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07004674 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004675 /* Zap redundant whitespaces */
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004676 tok->next = next = zap_white(next);
H. Peter Anvind784a082009-04-20 14:01:18 -07004677 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004678
4679 case TOK_PASTE:
4680 /* Explicit pasting */
4681 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004682 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004683
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004684 /* Left pasting token is start of line */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004685 if (!prev_nonspace) {
4686 nasm_nonfatal("No lvalue found on pasting");
4687 tok = delete_Token(tok);
4688 break;
4689 }
4690
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004691 did_paste = true;
4692
4693 prev_next = prev_nonspace;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004694 t = *prev_nonspace;
4695
4696 /* Delete leading whitespace */
4697 next = zap_white(t->next);
4698
4699 /* Delete the %+ token itself */
4700 nasm_assert(next == tok);
4701 next = delete_Token(next);
4702
4703 /* Delete trailing whitespace */
4704 next = zap_white(next);
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004705
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004706 /*
4707 * No ending token, this might happen in two
4708 * cases
4709 *
4710 * 1) There indeed no right token at all
4711 * 2) There is a bare "%define ID" statement,
4712 * and @ID does expand to whitespace.
4713 *
4714 * So technically we need to do a grammar analysis
4715 * in another stage of parsing, but for now lets don't
4716 * change the behaviour people used to. Simply allow
4717 * whitespace after paste token.
4718 */
4719 if (!next) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004720 *prev_nonspace = tok = NULL; /* End of line */
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004721 break;
4722 }
4723
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004724 p = buf = nasm_malloc(t->len + next->len + 1);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004725 p = mempcpy(p, tok_text(t), t->len);
4726 p = mempcpy(p, tok_text(next), next->len);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004727 *p = '\0';
4728 delete_Token(t);
4729 t = tokenize(buf);
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004730 nasm_free(buf);
4731
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004732 if (unlikely(!t)) {
4733 /*
4734 * No output at all? Replace with a single whitespace.
4735 * This should never happen.
4736 */
4737 t = new_White(NULL);
4738 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004739
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004740 *prev_nonspace = tok = t;
4741 while (t->next)
4742 t = t->next; /* Find the last token produced */
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004743
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004744 /* Delete the second token and attach to the end of the list */
4745 t->next = delete_Token(next);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004746
4747 /* We want to restart from the head of the pasted token */
4748 next = tok;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004749 break;
4750
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004751 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004752 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004753 for (i = 0; i < mnum; i++) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004754 if (pp_concat_match(tok, m[i].mask_head))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004755 break;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004756 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004757
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004758 if (i >= mnum)
4759 break;
4760
4761 len = tok->len;
4762 while (pp_concat_match(next, m[i].mask_tail)) {
4763 len += next->len;
4764 next = next->next;
4765 }
4766
4767 /* No match or no text to process */
4768 if (len == tok->len)
4769 break;
4770
4771 p = buf = nasm_malloc(len + 1);
4772 while (tok != next) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004773 p = mempcpy(p, tok_text(tok), tok->len);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004774 tok = delete_Token(tok);
4775 }
4776 *p = '\0';
4777 *prev_next = tok = t = tokenize(buf);
4778 nasm_free(buf);
4779
4780 /*
4781 * Connect pasted into original stream,
4782 * ie A -> new-tokens -> B
4783 */
4784 while (t->next)
4785 t = t->next;
4786 t->next = next;
4787 prev_next = prev_nonspace = &t->next;
4788 did_paste = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004789 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004790 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004791
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004792 if (did_paste) {
4793 pasted = true;
4794 } else {
4795 prev_next = &tok->next;
4796 if (next && next->type != TOK_WHITESPACE && next->type != TOK_PASTE)
4797 prev_nonspace = prev_next;
4798 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004799
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004800 tok = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004801 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004802
4803 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07004804}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004805
4806/*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004807 * Computes the proper rotation of mmacro parameters
4808 */
4809static int mmac_rotate(const MMacro *mac, unsigned int n)
4810{
4811 if (--n < mac->nparam)
4812 n = (n + mac->rotate) % mac->nparam;
4813
4814 return n+1;
4815}
4816
4817/*
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004818 * expands to a list of tokens from %{x:y}
4819 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004820static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004821{
4822 Token *t = tline, **tt, *tm, *head;
4823 char *pos;
4824 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004825
H. Peter Anvin8571f062019-09-23 16:40:03 -07004826 pos = strchr(tok_text(tline), ':');
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004827 nasm_assert(pos);
4828
4829 lst = atoi(pos + 1);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004830 fst = atoi(tok_text(tline) + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004831
4832 /*
4833 * only macros params are accounted so
4834 * if someone passes %0 -- we reject such
4835 * value(s)
4836 */
4837 if (lst == 0 || fst == 0)
4838 goto err;
4839
4840 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004841 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
4842 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004843 goto err;
4844
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004845 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
4846 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004847
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004848 /* count from zero */
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004849 fst--, lst--;
4850
4851 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004852 * It will be at least one token. Note we
4853 * need to scan params until separator, otherwise
4854 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004855 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004856 j = (fst + mac->rotate) % mac->nparam;
4857 tm = mac->params[j+1];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03004858 if (!tm)
4859 goto err;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004860 head = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004861 tt = &head->next, tm = tm->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004862 while (tok_isnt(tm, ',')) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004863 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004864 *tt = t, tt = &t->next, tm = tm->next;
4865 }
4866
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004867 if (fst < lst) {
4868 for (i = fst + 1; i <= lst; i++) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004869 t = make_tok_char(NULL, ',');
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004870 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004871 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004872 tm = mac->params[j+1];
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004873 while (tok_isnt(tm, ',')) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004874 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004875 *tt = t, tt = &t->next, tm = tm->next;
4876 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004877 }
4878 } else {
4879 for (i = fst - 1; i >= lst; i--) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004880 t = make_tok_char(NULL, ',');
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004881 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004882 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004883 tm = mac->params[j+1];
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004884 while (!tok_isnt(tm, ',')) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004885 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004886 *tt = t, tt = &t->next, tm = tm->next;
4887 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004888 }
4889 }
4890
4891 *last = tt;
4892 return head;
4893
4894err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004895 nasm_nonfatal("`%%{%s}': macro parameters out of range",
H. Peter Anvin8571f062019-09-23 16:40:03 -07004896 tok_text(tline) + 1);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004897 return NULL;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004898}
4899
H. Peter Anvin76690a12002-04-30 20:52:49 +00004900/*
4901 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004902 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004903 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004904 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004905static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004906{
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004907 Token **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004908 bool changed = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004909 MMacro *mac = istk->mstk.mmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004910
4911 tail = &thead;
4912 thead = NULL;
4913
H. Peter Anvine2c80182005-01-15 22:15:51 +00004914 while (tline) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004915 bool change;
H. Peter Anvin (Intel)a762cd42020-06-01 11:49:08 -07004916 bool err_not_mac = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004917 Token *t = tline;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004918 const char *text = tok_text(t);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004919 int type = t->type;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004920
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004921 tline = tline->next;
4922 t->next = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004923
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004924 switch (type) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004925 case TOK_LOCAL_SYMBOL:
H. Peter Anvin (Intel)a762cd42020-06-01 11:49:08 -07004926 change = true;
4927
4928 if (!mac) {
4929 err_not_mac = true;
4930 break;
4931 }
4932
H. Peter Anvin8571f062019-09-23 16:40:03 -07004933 type = TOK_ID;
4934 text = nasm_asprintf("..@%"PRIu64".%s", mac->unique, text+2);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004935 break;
4936 case TOK_MMACRO_PARAM:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004937 {
4938 Token *tt = NULL;
4939
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004940 change = true;
4941
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004942 if (!mac) {
H. Peter Anvin (Intel)a762cd42020-06-01 11:49:08 -07004943 err_not_mac = true;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004944 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004945 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004946
4947 if (strchr(text, ':')) {
4948 /*
4949 * seems we have a parameters range here
4950 */
4951 Token *head, **last;
4952 head = expand_mmac_params_range(mac, t, &last);
4953 if (head) {
4954 *tail = head;
4955 *last = tline;
4956 text = NULL;
4957 }
4958 break;
4959 }
4960
4961 switch (text[1]) {
4962 /*
4963 * We have to make a substitution of one of the
4964 * forms %1, %-1, %+1, %%foo, %0, %00.
4965 */
4966 case '0':
4967 if (!text[2]) {
4968 type = TOK_NUMBER;
4969 text = nasm_asprintf("%d", mac->nparam);
4970 break;
4971 }
4972 if (text[2] != '0' || text[3])
4973 goto invalid;
4974 /* a possible captured label == mac->params[0] */
4975 /* fall through */
4976 default:
4977 {
4978 unsigned long n;
4979 char *ep;
4980
4981 n = strtoul(text + 1, &ep, 10);
4982 if (unlikely(*ep))
4983 goto invalid;
4984
4985 if (n <= mac->nparam) {
4986 n = mmac_rotate(mac, n);
4987 dup_tlistn(mac->params[n], mac->paramlen[n], &tail);
4988 }
4989 text = NULL;
4990 break;
4991 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004992 case '-':
4993 case '+':
4994 {
4995 int cc;
4996 unsigned long n;
4997 char *ep;
4998
H. Peter Anvin8571f062019-09-23 16:40:03 -07004999 n = strtoul(tok_text(t) + 2, &ep, 10);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005000 if (unlikely(*ep))
5001 goto invalid;
5002
Chang S. Bae057b8322020-04-18 23:11:21 +00005003 if (n && n <= mac->nparam) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005004 n = mmac_rotate(mac, n);
5005 tt = mac->params[n];
5006 }
5007 cc = find_cc(tt);
5008 if (cc == -1) {
5009 nasm_nonfatal("macro parameter `%s' is not a condition code",
H. Peter Anvin8571f062019-09-23 16:40:03 -07005010 tok_text(t));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005011 text = NULL;
5012 break;
5013 }
5014
5015 type = TOK_ID;
5016 if (text[1] == '-') {
5017 int ncc = inverse_ccs[cc];
5018 if (unlikely(ncc == -1)) {
5019 nasm_nonfatal("condition code `%s' is not invertible",
5020 conditions[cc]);
5021 break;
5022 }
5023 cc = ncc;
5024 }
5025 text = nasm_strdup(conditions[cc]);
5026 break;
5027 }
5028
5029 invalid:
5030 nasm_nonfatal("invalid macro parameter: `%s'", text);
5031 text = NULL;
5032 break;
5033 }
5034 break;
5035 }
5036
5037 case TOK_PREPROC_Q:
5038 if (mac) {
5039 type = TOK_ID;
5040 text = nasm_strdup(mac->iname);
5041 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07005042 } else {
5043 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005044 }
5045 break;
5046
5047 case TOK_PREPROC_QQ:
5048 if (mac) {
5049 type = TOK_ID;
5050 text = nasm_strdup(mac->name);
5051 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07005052 } else {
5053 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005054 }
5055 break;
5056
5057 case TOK_INDIRECT:
5058 {
5059 Token *tt;
5060
H. Peter Anvin8571f062019-09-23 16:40:03 -07005061 tt = tokenize(tok_text(t));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005062 tt = expand_mmac_params(tt);
5063 tt = expand_smacro(tt);
5064 /* Why dup_tlist() here? We should own tt... */
5065 dup_tlist(tt, &tail);
5066 text = NULL;
5067 change = true;
5068 break;
5069 }
5070
5071 default:
5072 change = false;
5073 break;
5074 }
5075
H. Peter Anvin (Intel)a762cd42020-06-01 11:49:08 -07005076 if (err_not_mac) {
5077 nasm_nonfatal("`%s': not in a macro call", text);
5078 text = NULL;
5079 change = true;
5080 }
5081
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005082 if (change) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005083 if (!text) {
5084 delete_Token(t);
5085 } else {
5086 *tail = t;
5087 tail = &t->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005088 set_text(t, text, tok_strlen(text));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005089 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005090 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005091 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005092 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005093 *tail = t;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005094 tail = &t->next;
5095 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00005096 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005097
H. Peter Anvineba20a72002-04-30 20:53:55 +00005098 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07005099
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005100 if (changed) {
5101 const struct tokseq_match t[] = {
5102 {
5103 PP_CONCAT_MASK(TOK_ID) |
5104 PP_CONCAT_MASK(TOK_FLOAT), /* head */
5105 PP_CONCAT_MASK(TOK_ID) |
5106 PP_CONCAT_MASK(TOK_NUMBER) |
5107 PP_CONCAT_MASK(TOK_FLOAT) |
5108 PP_CONCAT_MASK(TOK_OTHER) /* tail */
5109 },
5110 {
5111 PP_CONCAT_MASK(TOK_NUMBER), /* head */
5112 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
5113 }
5114 };
5115 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
5116 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07005117
H. Peter Anvin76690a12002-04-30 20:52:49 +00005118 return thead;
5119}
5120
H. Peter Anvin322bee02019-08-10 01:38:06 -07005121static Token *expand_smacro_noreset(Token * tline);
H. Peter Anvin322bee02019-08-10 01:38:06 -07005122
H. Peter Anvin76690a12002-04-30 20:52:49 +00005123/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005124 * Expand *one* single-line macro instance. If the first token is not
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005125 * a macro at all, it is simply copied to the output and the pointer
5126 * advanced. tpp should be a pointer to a pointer (usually the next
5127 * pointer of the previous token) to the first token. **tpp is updated
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005128 * to point to the first token of the expansion, and *tpp updated to
5129 * point to the next pointer of the last token of the expansion.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005130 *
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005131 * If the expansion is empty, *tpp will be unchanged but **tpp will
5132 * be advanced past the macro call.
5133 *
H. Peter Anvin322bee02019-08-10 01:38:06 -07005134 * Return the macro expanded, or NULL if no expansion took place.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005135 */
H. Peter Anvin322bee02019-08-10 01:38:06 -07005136static SMacro *expand_one_smacro(Token ***tpp)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005137{
5138 Token **params = NULL;
5139 const char *mname;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005140 Token *mstart = **tpp;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005141 Token *tline = mstart;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005142 SMacro *head, *m;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005143 int i;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005144 Token *t, *tup, *tafter;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005145 int nparam = 0;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005146 bool cond_comma;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005147
5148 if (!tline)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005149 return false; /* Empty line, nothing to do */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005150
H. Peter Anvin8571f062019-09-23 16:40:03 -07005151 mname = tok_text(mstart);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005152
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005153 smacro_deadman.total--;
H. Peter Anvin322bee02019-08-10 01:38:06 -07005154 smacro_deadman.levels--;
5155
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005156 if (unlikely(smacro_deadman.total < 0 || smacro_deadman.levels < 0)) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07005157 if (unlikely(!smacro_deadman.triggered)) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005158 nasm_nonfatal("interminable macro recursion");
H. Peter Anvin322bee02019-08-10 01:38:06 -07005159 smacro_deadman.triggered = true;
5160 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005161 goto not_a_macro;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005162 } else if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005163 head = (SMacro *)hash_findix(&smacros, mname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005164 } else if (tline->type == TOK_LOCAL_MACRO) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005165 Context *ctx = get_ctx(mname, &mname);
5166 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
5167 } else {
5168 goto not_a_macro;
5169 }
5170
5171 /*
5172 * We've hit an identifier of some sort. First check whether the
5173 * identifier is a single-line macro at all, then think about
5174 * checking for parameters if necessary.
5175 */
5176 list_for_each(m, head) {
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005177 if (unlikely(m->alias && ppopt.noaliases))
H. Peter Anvind2354082019-08-27 16:38:48 -07005178 continue;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005179 if (!mstrcmp(m->name, mname, m->casesense))
5180 break;
5181 }
5182
5183 if (!m) {
5184 goto not_a_macro;
5185 }
5186
5187 /* Parse parameters, if applicable */
5188
5189 params = NULL;
5190 nparam = 0;
5191
5192 if (m->nparam == 0) {
5193 /*
5194 * Simple case: the macro is parameterless.
5195 * Nothing to parse; the expansion code will
5196 * drop the macro name token.
5197 */
5198 } else {
5199 /*
5200 * Complicated case: at least one macro with this name
5201 * exists and takes parameters. We must find the
5202 * parameters in the call, count them, find the SMacro
5203 * that corresponds to that form of the macro call, and
5204 * substitute for the parameters when we expand. What a
5205 * pain.
5206 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005207 Token *t;
5208 int paren, brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005209
5210 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005211 tline = skip_white(tline);
5212 if (!tok_is(tline, '(')) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005213 /*
5214 * This macro wasn't called with parameters: ignore
5215 * the call. (Behaviour borrowed from gnu cpp.)
5216 */
5217 goto not_a_macro;
5218 }
5219
5220 paren = 1;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005221 nparam = 1;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005222 brackets = 0;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005223 t = tline; /* tline points to leading ( */
5224
5225 while (paren) {
5226 t = t->next;
5227
5228 if (!t) {
5229 nasm_nonfatal("macro call expects terminating `)'");
5230 goto not_a_macro;
5231 }
5232
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005233 if (t->type != TOK_OTHER || t->len != 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005234 continue;
5235
H. Peter Anvin8571f062019-09-23 16:40:03 -07005236 switch (t->text.a[0]) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005237 case ',':
H. Peter Anvinbd00f252020-06-04 21:05:01 -07005238 if (!brackets && paren == 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005239 nparam++;
5240 break;
5241
5242 case '{':
5243 brackets++;
5244 break;
5245
5246 case '}':
5247 if (brackets > 0)
5248 brackets--;
5249 break;
5250
5251 case '(':
5252 if (!brackets)
5253 paren++;
5254 break;
5255
5256 case ')':
5257 if (!brackets)
5258 paren--;
5259 break;
5260
5261 default:
5262 break; /* Normal token */
5263 }
5264 }
5265
5266 /*
5267 * Look for a macro matching in both name and parameter count.
5268 * We already know any matches cannot be anywhere before the
5269 * current position of "m", so there is no reason to
5270 * backtrack.
5271 */
5272 while (1) {
5273 if (!m) {
5274 /*!
5275 *!macro-params-single [on] single-line macro calls with wrong parameter count
5276 *! warns about \i{single-line macros} being invoked
5277 *! with the wrong number of parameters.
5278 */
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005279 nasm_warn(WARN_MACRO_PARAMS_SINGLE|ERR_HOLD,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005280 "single-line macro `%s' exists, "
5281 "but not taking %d parameter%s",
5282 mname, nparam, (nparam == 1) ? "" : "s");
5283 goto not_a_macro;
5284 }
5285
5286 if (!mstrcmp(m->name, mname, m->casesense)) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005287 if (nparam == m->nparam)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005288 break; /* It's good */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005289 if (m->greedy && nparam >= m->nparam-1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005290 break; /* Also good */
5291 }
5292 m = m->next;
5293 }
5294 }
5295
5296 if (m->in_progress)
5297 goto not_a_macro;
5298
5299 /* Expand the macro */
5300 m->in_progress = true;
5301
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005302 if (nparam) {
5303 /* Extract parameters */
5304 Token **phead, **pep;
5305 int white = 0;
5306 int brackets = 0;
5307 int paren;
5308 bool bracketed = false;
5309 bool bad_bracket = false;
5310 enum sparmflags flags;
5311
5312 nparam = m->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005313 paren = 1;
5314 nasm_newn(params, nparam);
5315 i = 0;
5316 flags = m->params[i].flags;
5317 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005318 *pep = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005319
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005320 while (paren) {
5321 bool skip;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005322 char ch;
5323
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005324 tline = tline->next;
5325
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005326 if (!tline)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005327 nasm_nonfatal("macro call expects terminating `)'");
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005328
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005329 ch = 0;
5330 skip = false;
5331
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005332
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005333 switch (tline->type) {
5334 case TOK_OTHER:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005335 if (tline->len == 1)
H. Peter Anvin8571f062019-09-23 16:40:03 -07005336 ch = tline->text.a[0];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005337 break;
5338
5339 case TOK_WHITESPACE:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005340 if (!(flags & SPARM_NOSTRIP)) {
5341 if (brackets || *phead)
5342 white++; /* Keep interior whitespace */
5343 skip = true;
5344 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005345 break;
5346
5347 default:
5348 break;
5349 }
5350
5351 switch (ch) {
5352 case ',':
H. Peter Anvinbd00f252020-06-04 21:05:01 -07005353 if (!brackets && paren == 1 && !(flags & SPARM_GREEDY)) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005354 i++;
5355 nasm_assert(i < nparam);
5356 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005357 *pep = NULL;
5358 bracketed = false;
5359 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005360 flags = m->params[i].flags;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005361 }
5362 break;
5363
5364 case '{':
5365 if (!bracketed) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005366 bracketed = !*phead && !(flags & SPARM_NOSTRIP);
5367 skip = bracketed;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005368 }
5369 brackets++;
5370 break;
5371
5372 case '}':
5373 if (brackets > 0) {
5374 if (!--brackets)
5375 skip = bracketed;
5376 }
5377 break;
5378
5379 case '(':
5380 if (!brackets)
5381 paren++;
5382 break;
5383
5384 case ')':
5385 if (!brackets) {
5386 paren--;
5387 if (!paren) {
5388 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005389 i++; /* Found last argument */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005390 }
5391 }
5392 break;
5393
5394 default:
5395 break; /* Normal token */
5396 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005397
5398 if (!skip) {
5399 Token *t;
5400
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005401 bad_bracket |= bracketed && !brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005402
5403 if (white) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005404 *pep = t = new_White(NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005405 pep = &t->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005406 white = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005407 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005408 *pep = t = dup_Token(NULL, tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005409 pep = &t->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005410 }
5411 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005412
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005413 /*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005414 * Possible further processing of parameters. Note that the
5415 * ordering matters here.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005416 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005417 for (i = 0; i < nparam; i++) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005418 enum sparmflags flags = m->params[i].flags;
5419
5420 if (flags & SPARM_EVAL) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005421 /* Evaluate this parameter as a number */
5422 struct ppscan pps;
5423 struct tokenval tokval;
5424 expr *evalresult;
5425 Token *eval_param;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005426
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005427 pps.tptr = eval_param = expand_smacro_noreset(params[i]);
5428 pps.ntokens = -1;
5429 tokval.t_type = TOKEN_INVALID;
5430 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005431
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005432 free_tlist(eval_param);
5433 params[i] = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005434
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005435 if (!evalresult) {
5436 /* Nothing meaningful to do */
5437 } else if (tokval.t_type) {
5438 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
5439 } else if (!is_simple(evalresult)) {
5440 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
5441 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005442 params[i] = make_tok_num(NULL, reloc_value(evalresult));
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005443 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005444 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005445
5446 if (flags & SPARM_STR) {
5447 /* Convert expansion to a quoted string */
5448 char *arg;
5449 Token *qs;
5450
5451 qs = expand_smacro_noreset(params[i]);
5452 arg = detoken(qs, false);
5453 free_tlist(qs);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005454 params[i] = make_tok_qstr(NULL, arg);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005455 nasm_free(arg);
5456 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005457 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005458 }
5459
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005460 /* Note: we own the expansion this returns. */
5461 t = m->expand(m, params, nparam);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005462
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005463 tafter = tline->next; /* Skip past the macro call */
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005464 tline->next = NULL; /* Truncate list at the macro call end */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005465 tline = tafter;
5466
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005467 tup = NULL;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005468 cond_comma = false;
5469
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005470 while (t) {
5471 enum pp_token_type type = t->type;
5472 Token *tnext = t->next;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005473
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005474 switch (type) {
5475 case TOK_PREPROC_Q:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005476 delete_Token(t);
5477 t = dup_Token(tline, mstart);
5478 break;
5479
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005480 case TOK_PREPROC_QQ:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005481 {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005482 size_t mlen = strlen(m->name);
5483 size_t len;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005484 char *p;
5485
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005486 t->type = mstart->type;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005487 if (t->type == TOK_LOCAL_MACRO) {
5488 const char *psp; /* prefix start pointer */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005489 const char *pep; /* prefix end pointer */
H. Peter Anvin8571f062019-09-23 16:40:03 -07005490 size_t plen;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005491
H. Peter Anvin8571f062019-09-23 16:40:03 -07005492 psp = tok_text(mstart);
5493 get_ctx(psp, &pep);
5494 plen = pep - psp;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005495
H. Peter Anvin8571f062019-09-23 16:40:03 -07005496 len = mlen + plen;
5497 p = nasm_malloc(len + 1);
5498 p = mempcpy(p, psp, plen);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005499 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005500 len = mlen;
5501 p = nasm_malloc(len + 1);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005502 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07005503 p = mempcpy(p, m->name, mlen);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005504 *p = '\0';
H. Peter Anvin8571f062019-09-23 16:40:03 -07005505 set_text_free(t, p, len);
5506
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005507 t->next = tline;
5508 break;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005509 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005510
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005511 case TOK_COND_COMMA:
5512 delete_Token(t);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005513 t = cond_comma ? make_tok_char(tline, ',') : NULL;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005514 break;
5515
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005516 case TOK_ID:
5517 case TOK_PREPROC_ID:
H. Peter Anvin8571f062019-09-23 16:40:03 -07005518 case TOK_LOCAL_MACRO:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005519 {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005520 /*
5521 * Chain this into the target line *before* expanding,
5522 * that way we pick up any arguments to the new macro call,
5523 * if applicable.
5524 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005525 Token **tp = &t;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005526 t->next = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005527 expand_one_smacro(&tp);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005528 tline = *tp; /* First token left after any macro call */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005529 break;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005530 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005531 default:
5532 if (is_smac_param(t->type)) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005533 int param = smac_nparam(t->type);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005534 nasm_assert(!tup && param < nparam);
5535 delete_Token(t);
5536 t = NULL;
5537 tup = tnext;
5538 tnext = dup_tlist_reverse(params[param], NULL);
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005539 cond_comma = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005540 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005541 t->next = tline;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005542 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005543 }
5544
5545 if (t) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005546 Token *endt = tline;
5547
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005548 tline = t;
Chang S. Bae95e54a92020-02-06 14:39:22 -08005549 while (!cond_comma && t && t != endt) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005550 cond_comma = t->type != TOK_WHITESPACE;
Chang S. Bae95e54a92020-02-06 14:39:22 -08005551 t = t->next;
5552 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005553 }
5554
5555 if (tnext) {
5556 t = tnext;
5557 } else {
5558 t = tup;
5559 tup = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005560 }
5561 }
5562
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005563 **tpp = tline;
H. Peter Anvin (Intel)6e714962020-06-01 12:21:10 -07005564 for (t = tline; t && t != tafter; t = t->next)
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005565 *tpp = &t->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005566
5567 m->in_progress = false;
5568
5569 /* Don't do this until after expansion or we will clobber mname */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005570 free_tlist(mstart);
H. Peter Anvin322bee02019-08-10 01:38:06 -07005571 goto done;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005572
5573 /*
5574 * No macro expansion needed; roll back to mstart (if necessary)
H. Peter Anvin322bee02019-08-10 01:38:06 -07005575 * and then advance to the next input token. Note that this is
5576 * by far the common case!
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005577 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005578not_a_macro:
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005579 *tpp = &mstart->next;
H. Peter Anvin322bee02019-08-10 01:38:06 -07005580 m = NULL;
5581done:
5582 smacro_deadman.levels++;
5583 if (unlikely(params))
5584 free_tlist_array(params, nparam);
5585 return m;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005586}
5587
5588/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005589 * Expand all single-line macro calls made in the given line.
5590 * Return the expanded version of the line. The original is deemed
5591 * to be destroyed in the process. (In reality we'll just move
5592 * Tokens from input to output a lot of the time, rather than
5593 * actually bothering to destroy and replicate.)
5594 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005595static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005596{
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005597 smacro_deadman.total = nasm_limit[LIMIT_MACRO_TOKENS];
H. Peter Anvin322bee02019-08-10 01:38:06 -07005598 smacro_deadman.levels = nasm_limit[LIMIT_MACRO_LEVELS];
5599 smacro_deadman.triggered = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005600 return expand_smacro_noreset(tline);
5601}
5602
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005603static Token *expand_smacro_noreset(Token *org_tline)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005604{
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005605 Token *tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005606 bool expanded;
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005607 errhold errhold; /* Hold warning/errors during expansion */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005608
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005609 if (!org_tline)
5610 return NULL; /* Empty input */
5611
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005612 /*
5613 * Trick: we should avoid changing the start token pointer since it can
5614 * be contained in "next" field of other token. Because of this
5615 * we allocate a copy of first token and work with it; at the end of
5616 * routine we copy it back
5617 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005618 tline = dup_Token(org_tline->next, org_tline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005619
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005620 /*
5621 * Pretend that we always end up doing expansion on the first pass;
5622 * that way %+ get processed. However, if we process %+ before the
5623 * first pass, we end up with things like MACRO %+ TAIL trying to
5624 * look up the macro "MACROTAIL", which we don't want.
5625 */
5626 expanded = true;
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005627
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005628 while (true) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005629 static const struct tokseq_match tmatch[] = {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005630 {
5631 PP_CONCAT_MASK(TOK_ID) |
H. Peter Anvin8571f062019-09-23 16:40:03 -07005632 PP_CONCAT_MASK(TOK_LOCAL_MACRO) |
5633 PP_CONCAT_MASK(TOK_ENVIRON) |
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005634 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
5635 PP_CONCAT_MASK(TOK_ID) |
H. Peter Anvin8571f062019-09-23 16:40:03 -07005636 PP_CONCAT_MASK(TOK_LOCAL_MACRO) |
5637 PP_CONCAT_MASK(TOK_ENVIRON) |
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005638 PP_CONCAT_MASK(TOK_PREPROC_ID) |
5639 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
5640 }
5641 };
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005642 Token **tail = &tline;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005643
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005644 /*
5645 * We hold warnings/errors until we are done this this loop. It is
5646 * possible for nuisance warnings to appear that disappear on later
5647 * passes.
5648 */
5649 errhold = nasm_error_hold_push();
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005650
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005651 while (*tail) /* main token loop */
H. Peter Anvin322bee02019-08-10 01:38:06 -07005652 expanded |= !!expand_one_smacro(&tail);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005653
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005654 if (!expanded)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005655 break; /* Done! */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005656
5657 /*
5658 * Now scan the entire line and look for successive TOK_IDs
5659 * that resulted after expansion (they can't be produced by
5660 * tokenize()). The successive TOK_IDs should be concatenated.
5661 * Also we look for %+ tokens and concatenate the tokens
5662 * before and after them (without white spaces in between).
5663 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005664 if (!paste_tokens(&tline, tmatch, ARRAY_SIZE(tmatch), true))
5665 break; /* Done again! */
5666
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005667 nasm_error_hold_pop(errhold, false);
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005668 expanded = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005669 }
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005670 nasm_error_hold_pop(errhold, true);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005671
H. Peter Anvin8571f062019-09-23 16:40:03 -07005672 if (!tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005673 /*
5674 * The expression expanded to empty line;
5675 * we can't return NULL because of the "trick" above.
5676 * Just set the line to a single WHITESPACE token.
H. Peter Anvin8571f062019-09-23 16:40:03 -07005677 */
5678
5679 tline = new_White(NULL);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005680 }
5681
H. Peter Anvin8571f062019-09-23 16:40:03 -07005682 steal_Token(org_tline, tline);
5683 org_tline->next = tline->next;
5684 delete_Token(tline);
5685
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005686 return org_tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005687}
5688
5689/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005690 * Similar to expand_smacro but used exclusively with macro identifiers
5691 * right before they are fetched in. The reason is that there can be
5692 * identifiers consisting of several subparts. We consider that if there
5693 * are more than one element forming the name, user wants a expansion,
5694 * otherwise it will be left as-is. Example:
5695 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005696 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005697 *
5698 * the identifier %$abc will be left as-is so that the handler for %define
5699 * will suck it and define the corresponding value. Other case:
5700 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005701 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005702 *
5703 * In this case user wants name to be expanded *before* %define starts
5704 * working, so we'll expand %$abc into something (if it has a value;
5705 * otherwise it will be left as-is) then concatenate all successive
5706 * PP_IDs into one.
5707 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005708static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005709{
5710 Token *cur, *oldnext = NULL;
5711
H. Peter Anvin734b1882002-04-30 21:01:08 +00005712 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005713 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005714
5715 cur = tline;
5716 while (cur->next &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07005717 (cur->next->type == TOK_ID || cur->next->type == TOK_PREPROC_ID ||
5718 cur->next->type == TOK_LOCAL_MACRO || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005719 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005720
5721 /* If identifier consists of just one token, don't expand */
5722 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005723 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005724
H. Peter Anvine2c80182005-01-15 22:15:51 +00005725 if (cur) {
5726 oldnext = cur->next; /* Detach the tail past identifier */
5727 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005728 }
5729
H. Peter Anvin734b1882002-04-30 21:01:08 +00005730 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005731
H. Peter Anvine2c80182005-01-15 22:15:51 +00005732 if (cur) {
5733 /* expand_smacro possibly changhed tline; re-scan for EOL */
5734 cur = tline;
5735 while (cur && cur->next)
5736 cur = cur->next;
5737 if (cur)
5738 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005739 }
5740
5741 return tline;
5742}
5743
5744/*
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005745 * This is called from find_mmacro_in_list() after finding a suitable macro.
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005746 */
5747static MMacro *use_mmacro(MMacro *m, int *nparamp, Token ***paramsp)
5748{
5749 int nparam = *nparamp;
5750 Token **params = *paramsp;
5751
5752 /*
5753 * This one is right. Just check if cycle removal
5754 * prohibits us using it before we actually celebrate...
5755 */
5756 if (m->in_progress > m->max_depth) {
5757 if (m->max_depth > 0) {
5758 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
5759 m->max_depth);
5760 }
5761 nasm_free(params);
5762 *nparamp = 0;
5763 *paramsp = NULL;
5764 return NULL;
5765 }
5766
5767 /*
5768 * It's right, and we can use it. Add its default
5769 * parameters to the end of our list if necessary.
5770 */
5771 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
5772 int newnparam = m->nparam_min + m->ndefs;
5773 params = nasm_realloc(params, sizeof(*params) * (newnparam+2));
5774 memcpy(&params[nparam+1], &m->defaults[nparam+1-m->nparam_min],
5775 (newnparam - nparam) * sizeof(*params));
5776 nparam = newnparam;
5777 }
5778 /*
5779 * If we've gone over the maximum parameter count (and
5780 * we're in Plus mode), ignore parameters beyond
5781 * nparam_max.
5782 */
5783 if (m->plus && nparam > m->nparam_max)
5784 nparam = m->nparam_max;
5785
5786 /*
5787 * If nparam was adjusted above, make sure the list is still
5788 * NULL-terminated.
5789 */
5790 params[nparam+1] = NULL;
5791
5792 /* Done! */
5793 *paramsp = params;
5794 *nparamp = nparam;
5795 return m;
5796}
5797
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005798/*
5799 * Search a macro list and try to find a match. If matching, call
5800 * use_mmacro() to set up the macro call. m points to the list of
5801 * search, which is_mmacro() sets to the first *possible* match.
5802 */
5803static MMacro *
5804find_mmacro_in_list(MMacro *m, const char *finding,
5805 int *nparamp, Token ***paramsp)
5806{
5807 int nparam = *nparamp;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005808
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005809 while (m) {
5810 if (m->nparam_min <= nparam
5811 && (m->plus || nparam <= m->nparam_max)) {
5812 /*
5813 * This one matches, use it.
5814 */
5815 return use_mmacro(m, nparamp, paramsp);
5816 }
5817
5818 /*
5819 * Otherwise search for the next one with a name match.
5820 */
5821 list_for_each(m, m->next) {
5822 if (!mstrcmp(m->name, finding, m->casesense))
5823 break;
5824 }
5825 }
5826
5827 return NULL;
5828}
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005829
5830/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005831 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005832 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005833 * to check for an initial label - that's taken care of in
5834 * expand_mmacro - but must check numbers of parameters. Guaranteed
5835 * to be called with tline->type == TOK_ID, so the putative macro
5836 * name is easy to find.
5837 */
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005838static MMacro *is_mmacro(Token * tline, int *nparamp, Token ***paramsp)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005839{
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005840 MMacro *head, *m, *found;
5841 Token **params, **comma;
5842 int raw_nparam, nparam;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005843 const char *finding = tok_text(tline);
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005844 bool empty_args = !tline->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005845
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005846 *nparamp = 0;
5847 *paramsp = NULL;
5848
H. Peter Anvin8571f062019-09-23 16:40:03 -07005849 head = (MMacro *) hash_findix(&mmacros, finding);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005850
5851 /*
5852 * Efficiency: first we see if any macro exists with the given
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005853 * name which isn't already excluded by macro cycle removal.
5854 * (The cycle removal test here helps optimize the case of wrapping
5855 * instructions, and is cheap to do here.)
5856 *
5857 * If not, we can return NULL immediately. _Then_ we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005858 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005859 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005860 */
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005861 list_for_each(m, head) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005862 if (!mstrcmp(m->name, finding, m->casesense) &&
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005863 (m->in_progress != 1 || m->max_depth > 0))
5864 break; /* Found something that needs consideration */
5865 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005866 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005867 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005868
5869 /*
5870 * OK, we have a potential macro. Count and demarcate the
5871 * parameters.
5872 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005873 comma = count_mmac_params(tline->next, nparamp, paramsp);
5874 raw_nparam = *nparamp;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005875
5876 /*
5877 * Search for an exact match. This cannot come *before* the m
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005878 * found in the list search before, so we can start there.
5879 *
5880 * If found is NULL and *paramsp has been cleared, then we
5881 * encountered an error for which we have already issued a
5882 * diagnostic, so we should not proceed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005883 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005884 found = find_mmacro_in_list(m, finding, nparamp, paramsp);
5885 if (!*paramsp)
5886 return NULL;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005887
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005888 nparam = *nparamp;
5889 params = *paramsp;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005890
5891 /*
5892 * Special weirdness: in NASM < 2.15, an expansion of
5893 * *only* whitespace, as can happen during macro expansion under
5894 * certain circumstances, is counted as zero arguments for the
5895 * purpose of %0, but one argument for the purpose of macro
5896 * matching! In particular, this affects:
5897 *
5898 * foobar %1
5899 *
5900 * ... with %1 being empty; this would call the one-argument
5901 * version of "foobar" with an empty argument, equivalent to
5902 *
5903 * foobar {%1}
5904 *
5905 * ... except that %0 would be set to 0 inside foobar, even if
5906 * foobar is declared with "%macro foobar 1" or equivalent!
5907 *
5908 * The proper way to do that is to define "%macro foobar 0-1".
5909 *
5910 * To be compatible without doing something too stupid, try to
5911 * match a zero-argument macro first, but if that fails, try
5912 * for a one-argument macro with the above behavior.
5913 *
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005914 * Furthermore, NASM < 2.15 will match stripping a tailing empty
5915 * argument, but in that case %0 *does* reflect that this argument
5916 * have been stripped; this is handled in count_mmac_params().
5917 *
5918 * To disable these insane legacy behaviors, use:
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005919 *
5920 * %pragma preproc sane_empty_expansion yes
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005921 *
5922 *!macro-params-legacy [on] improperly calling multi-line macro for legacy support
5923 *! warns about \i{multi-line macros} being invoked
5924 *! with the wrong number of parameters, but for bug-compatibility
5925 *! with NASM versions older than 2.15, NASM tried to fix up the
5926 *! parameters to match the legacy behavior and call the macro anyway.
5927 *! This can happen in certain cases where there are empty arguments
5928 *! without braces, sometimes as a result of macro expansion.
5929 *!-
5930 *! The legacy behavior is quite strange and highly context-dependent,
5931 *! and can be disabled with:
5932 *!-
5933 *! \c %pragma preproc sane_empty_expansion true
5934 *!-
5935 *! It is highly recommended to use this option in new code.
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005936 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005937 if (!ppopt.sane_empty_expansion) {
5938 if (!found) {
5939 if (raw_nparam == 0 && !empty_args) {
5940 /*
5941 * A single all-whitespace parameter as the only thing?
5942 * Look for a one-argument macro, but don't adjust
5943 * *nparamp.
5944 */
5945 int bogus_nparam = 1;
5946 params[2] = NULL;
5947 found = find_mmacro_in_list(m, finding, &bogus_nparam, paramsp);
5948 } else if (raw_nparam > 1 && comma) {
5949 Token *comma_tail = *comma;
5950
5951 /*
5952 * Drop the terminal argument and try again.
5953 * If we fail, we need to restore the comma to
5954 * preserve tlist.
5955 */
5956 *comma = NULL;
5957 *nparamp = raw_nparam - 1;
5958 found = find_mmacro_in_list(m, finding, nparamp, paramsp);
5959 if (found)
5960 free_tlist(comma_tail);
5961 else
5962 *comma = comma_tail;
5963 }
5964
5965 if (!*paramsp)
5966 return NULL;
5967 } else if (comma) {
5968 free_tlist(*comma);
5969 *comma = NULL;
5970 if (raw_nparam > found->nparam_min &&
5971 raw_nparam <= found->nparam_min + found->ndefs) {
5972 /* Replace empty argument with default parameter */
5973 params[raw_nparam] =
5974 found->defaults[raw_nparam - found->nparam_min];
5975 } else if (raw_nparam > found->nparam_max && found->plus) {
5976 /* Just drop the comma, don't adjust argument count */
5977 } else {
5978 /* Drop argument. This may cause nparam < nparam_min. */
5979 params[raw_nparam] = NULL;
5980 *nparamp = nparam = raw_nparam - 1;
5981 }
5982 }
5983
5984 if (found) {
5985 if (raw_nparam < found->nparam_min ||
5986 (raw_nparam > found->nparam_max && !found->plus)) {
5987 nasm_warn(WARN_MACRO_PARAMS_LEGACY,
5988 "improperly calling multi-line macro `%s' with %d parameters",
5989 found->name, raw_nparam);
5990 } else if (comma) {
5991 nasm_warn(WARN_MACRO_PARAMS_LEGACY,
5992 "dropping trailing empty parameter in call to multi-line macro `%s'", found->name);
5993 }
5994 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005995 }
5996
5997 /*
5998 * After all that, we didn't find one with the right number of
5999 * parameters. Issue a warning, and fail to expand the macro.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006000 *!
6001 *!macro-params-multi [on] multi-line macro calls with wrong parameter count
6002 *! warns about \i{multi-line macros} being invoked
6003 *! with the wrong number of parameters. See \k{mlmacover} for an
6004 *! example of why you might want to disable this warning.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006005 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07006006 if (found)
6007 return found;
6008
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006009 nasm_warn(WARN_MACRO_PARAMS_MULTI,
6010 "multi-line macro `%s' exists, but not taking %d parameter%s",
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07006011 finding, nparam, (nparam == 1) ? "" : "s");
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07006012 nasm_free(*paramsp);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006013 return NULL;
6014}
6015
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006016
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006017#if 0
6018
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006019/*
6020 * Save MMacro invocation specific fields in
6021 * preparation for a recursive macro expansion
6022 */
6023static void push_mmacro(MMacro *m)
6024{
6025 MMacroInvocation *i;
6026
6027 i = nasm_malloc(sizeof(MMacroInvocation));
6028 i->prev = m->prev;
6029 i->params = m->params;
6030 i->iline = m->iline;
6031 i->nparam = m->nparam;
6032 i->rotate = m->rotate;
6033 i->paramlen = m->paramlen;
6034 i->unique = m->unique;
6035 i->condcnt = m->condcnt;
6036 m->prev = i;
6037}
6038
6039
6040/*
6041 * Restore MMacro invocation specific fields that were
6042 * saved during a previous recursive macro expansion
6043 */
6044static void pop_mmacro(MMacro *m)
6045{
6046 MMacroInvocation *i;
6047
6048 if (m->prev) {
6049 i = m->prev;
6050 m->prev = i->prev;
6051 m->params = i->params;
6052 m->iline = i->iline;
6053 m->nparam = i->nparam;
6054 m->rotate = i->rotate;
6055 m->paramlen = i->paramlen;
6056 m->unique = i->unique;
6057 m->condcnt = i->condcnt;
6058 nasm_free(i);
6059 }
6060}
6061
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006062#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006063
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006064/*
H. Peter Anvin (Intel)ffe89dd2019-08-20 16:06:36 -07006065 * List an mmacro call with arguments (-Lm option)
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07006066 */
6067static void list_mmacro_call(const MMacro *m)
6068{
6069 const char prefix[] = " ;;; [macro] ";
6070 size_t namelen, size;
6071 char *buf, *p;
6072 unsigned int i;
6073 const Token *t;
6074
6075 namelen = strlen(m->iname);
6076 size = namelen + sizeof(prefix); /* Includes final null (from prefix) */
6077
6078 for (i = 1; i <= m->nparam; i++) {
6079 int j = 0;
6080 size += 3; /* Braces and space/comma */
6081 list_for_each(t, m->params[i]) {
6082 if (j++ >= m->paramlen[i])
6083 break;
6084 size += (t->type == TOK_WHITESPACE) ? 1 : t->len;
6085 }
6086 }
6087
6088 buf = p = nasm_malloc(size);
6089 p = mempcpy(p, prefix, sizeof(prefix) - 1);
6090 p = mempcpy(p, m->iname, namelen);
6091 *p++ = ' ';
6092
6093 for (i = 1; i <= m->nparam; i++) {
6094 int j = 0;
6095 *p++ = '{';
6096 list_for_each(t, m->params[i]) {
6097 if (j++ >= m->paramlen[i])
6098 break;
H. Peter Anvin8571f062019-09-23 16:40:03 -07006099 p = mempcpy(p, tok_text(t), t->len);
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07006100 }
6101 *p++ = '}';
6102 *p++ = ',';
6103 }
6104
6105 *--p = '\0'; /* Replace last delimeter with null */
6106 lfmt->line(LIST_MACRO, -1, buf);
6107 nasm_free(buf);
6108}
6109
6110/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006111 * Expand the multi-line macro call made by the given line, if
6112 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006113 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006114 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006115static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006116{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006117 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006118 Token *label = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006119 bool dont_prepend = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006120 Token **params, *t, *tt;
6121 MMacro *m;
6122 Line *l, *ll;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07006123 int i, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07006124 const char *mname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006125 int nparam = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006126
6127 t = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006128 t = skip_white(t);
6129 /* if (!tok_type(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07006130 if (!tok_type(t, TOK_ID) && !tok_type(t, TOK_LOCAL_MACRO))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006131 return 0;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006132 m = is_mmacro(t, &nparam, &params);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006133 if (m) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006134 mname = tok_text(t);
H. Peter Anvinc751e862008-06-09 10:18:45 -07006135 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006136 Token *last;
6137 /*
6138 * We have an id which isn't a macro call. We'll assume
6139 * it might be a label; we'll also check to see if a
6140 * colon follows it. Then, if there's another id after
6141 * that lot, we'll check it again for macro-hood.
6142 */
6143 label = last = t;
6144 t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006145 if (tok_white(t))
H. Peter Anvine2c80182005-01-15 22:15:51 +00006146 last = t, t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006147 if (tok_is(t, ':')) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006148 dont_prepend = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006149 last = t, t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006150 if (tok_white(t))
H. Peter Anvine2c80182005-01-15 22:15:51 +00006151 last = t, t = t->next;
6152 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006153 if (!tok_type(t, TOK_ID) || !(m = is_mmacro(t, &nparam, &params)))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006154 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006155 last->next = NULL;
H. Peter Anvin8571f062019-09-23 16:40:03 -07006156 mname = tok_text(t);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006157 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006158 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006159
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07006160 if (unlikely(mmacro_deadman.total >= nasm_limit[LIMIT_MMACROS] ||
6161 mmacro_deadman.levels >= nasm_limit[LIMIT_MACRO_LEVELS])) {
6162 if (!mmacro_deadman.triggered) {
6163 nasm_nonfatal("interminable multiline macro recursion");
6164 mmacro_deadman.triggered = true;
6165 }
6166 return 0;
6167 }
6168
6169 mmacro_deadman.total++;
6170 mmacro_deadman.levels++;
6171
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006172 /*
6173 * Fix up the parameters: this involves stripping leading and
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07006174 * trailing whitespace and stripping braces if they are present.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006175 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006176 nasm_newn(paramlen, nparam+1);
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07006177
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006178 for (i = 1; (t = params[i]); i++) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006179 bool braced = false;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08006180 int brace = 0;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006181 int white = 0;
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07006182 bool comma = !m->plus || i < nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006183
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006184 t = skip_white(t);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006185 if (tok_is(t, '{')) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006186 t = t->next;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006187 brace = 1;
6188 braced = true;
6189 comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006190 }
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006191
6192 params[i] = t;
6193 for (; t; t = t->next) {
6194 if (tok_white(t)) {
6195 white++;
6196 continue;
6197 }
6198
6199 if (t->type == TOK_OTHER && t->len == 1) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006200 switch (t->text.a[0]) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006201 case ',':
6202 if (comma && !brace)
6203 goto endparam;
6204 break;
6205
6206 case '{':
6207 brace++;
6208 break;
6209
6210 case '}':
6211 brace--;
6212 if (braced && !brace) {
6213 paramlen[i] += white;
6214 goto endparam;
6215 }
6216 break;
6217
6218 default:
6219 break;
6220 }
6221 }
6222
6223 paramlen[i] += white + 1;
6224 white = 0;
6225 }
6226 endparam:
6227 ;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006228 }
6229
6230 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006231 * OK, we have a MMacro structure together with a set of
6232 * parameters. We must now go through the expansion and push
6233 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00006234 * parameter tokens and macro-local tokens doesn't get done
6235 * until the single-line macro substitution process; this is
6236 * because delaying them allows us to change the semantics
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006237 * later through %rotate and give the right semantics for
6238 * nested mmacros.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006239 *
6240 * First, push an end marker on to istk->expansion, mark this
6241 * macro as in progress, and set up its invocation-specific
6242 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006243 */
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07006244 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006245 ll->next = istk->expansion;
6246 ll->finishes = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006247 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006248
6249 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006250 * Save the previous MMacro expansion in the case of
6251 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006252 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006253#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006254 if (m->max_depth && m->in_progress)
6255 push_mmacro(m);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006256#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006257
6258 m->in_progress ++;
6259 m->params = params;
6260 m->iline = tline;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006261 m->iname = nasm_strdup(mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006262 m->nparam = nparam;
6263 m->rotate = 0;
6264 m->paramlen = paramlen;
6265 m->unique = unique++;
6266 m->lineno = 0;
6267 m->condcnt = 0;
6268
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006269 m->mstk = istk->mstk;
6270 istk->mstk.mstk = istk->mstk.mmac = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006271
6272 list_for_each(l, m->expansion) {
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07006273 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006274 ll->next = istk->expansion;
6275 istk->expansion = ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006276 ll->first = dup_tlist(l->first, NULL);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006277 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006278
6279 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006280 * If we had a label, and this macro definition does not include
6281 * a %00, push it on as the first line of, ot
H. Peter Anvineba20a72002-04-30 20:53:55 +00006282 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006283 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006284 if (label) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006285 /*
6286 * We had a label. If this macro contains an %00 parameter,
6287 * save the value as a special parameter (which is what it
6288 * is), otherwise push it as the first line of the macro
6289 * expansion.
6290 */
6291 if (m->capture_label) {
6292 params[0] = dup_Token(NULL, label);
6293 paramlen[0] = 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006294 free_tlist(startline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006295 } else {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006296 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006297 ll->finishes = NULL;
6298 ll->next = istk->expansion;
6299 istk->expansion = ll;
6300 ll->first = startline;
6301 if (!dont_prepend) {
6302 while (label->next)
6303 label = label->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07006304 label->next = tt = make_tok_char(NULL, ':');
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006305 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006306 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00006307 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006308
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07006309 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006310
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07006311 if (list_option('m') && !m->nolist)
6312 list_mmacro_call(m);
6313
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006314 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006315}
6316
H. Peter Anvin130736c2016-02-17 20:27:41 -08006317/*
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006318 * This function decides if an error message should be suppressed.
6319 * It will never be called with a severity level of ERR_FATAL or
6320 * higher.
H. Peter Anvin130736c2016-02-17 20:27:41 -08006321 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006322static bool pp_suppress_error(errflags severity)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02006323{
H. Peter Anvin130736c2016-02-17 20:27:41 -08006324 /*
6325 * If we're in a dead branch of IF or something like it, ignore the error.
6326 * However, because %else etc are evaluated in the state context
6327 * of the previous branch, errors might get lost:
6328 * %if 0 ... %else trailing garbage ... %endif
6329 * So %else etc should set the ERR_PP_PRECOND flag.
6330 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006331 if (istk && istk->conds &&
H. Peter Anvin130736c2016-02-17 20:27:41 -08006332 ((severity & ERR_PP_PRECOND) ?
6333 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07006334 !emitting(istk->conds->state)))
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006335 return true;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02006336
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006337 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00006338}
6339
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006340static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006341stdmac_file(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006342{
6343 (void)s;
6344 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006345 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006346
H. Peter Anvin8571f062019-09-23 16:40:03 -07006347 return make_tok_qstr(NULL, src_get_fname());
H. Peter Anvin8b262472019-02-26 14:00:54 -08006348}
6349
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006350static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006351stdmac_line(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006352{
6353 (void)s;
6354 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006355 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006356
H. Peter Anvin8571f062019-09-23 16:40:03 -07006357 return make_tok_num(NULL, src_get_linnum());
H. Peter Anvin8b262472019-02-26 14:00:54 -08006358}
6359
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006360static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006361stdmac_bits(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006362{
6363 (void)s;
6364 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006365 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006366
H. Peter Anvin8571f062019-09-23 16:40:03 -07006367 return make_tok_num(NULL, globalbits);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006368}
6369
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006370static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006371stdmac_ptr(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006372{
H. Peter Anvin8b262472019-02-26 14:00:54 -08006373 (void)s;
6374 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006375 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006376
6377 switch (globalbits) {
6378 case 16:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006379 return new_Token(NULL, TOK_ID, "word", 4);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006380 case 32:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006381 return new_Token(NULL, TOK_ID, "dword", 5);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006382 case 64:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006383 return new_Token(NULL, TOK_ID, "qword", 5);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006384 default:
6385 panic();
6386 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08006387}
6388
H. Peter Anvin8b262472019-02-26 14:00:54 -08006389/* Add magic standard macros */
6390struct magic_macros {
6391 const char *name;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006392 int nparam;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006393 ExpandSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006394};
6395static const struct magic_macros magic_macros[] =
6396{
H. Peter Anvind2354082019-08-27 16:38:48 -07006397 { "__?FILE?__", 0, stdmac_file },
6398 { "__?LINE?__", 0, stdmac_line },
6399 { "__?BITS?__", 0, stdmac_bits },
6400 { "__?PTR?__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08006401 { NULL, 0, NULL }
6402};
6403
6404static void pp_add_magic_stdmac(void)
6405{
6406 const struct magic_macros *m;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006407 SMacro tmpl;
6408
6409 nasm_zero(tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006410
6411 for (m = magic_macros; m->name; m++) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006412 tmpl.nparam = m->nparam;
6413 tmpl.expand = m->func;
6414 define_smacro(m->name, true, NULL, &tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006415 }
6416}
6417
H. Peter Anvin734b1882002-04-30 21:01:08 +00006418static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006419pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006420{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006421 int apass;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006422 struct Include *inc;
H. Peter Anvin7383b402008-09-24 10:20:40 -07006423
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006424 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006425 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07006426 nested_mac_count = 0;
6427 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07006428 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006429 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07006430 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006431 pp_mode = mode;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07006432
6433 /* Reset options to default */
6434 nasm_zero(ppopt);
H. Peter Anvinf7606612016-07-13 14:23:48 -07006435
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07006436 if (!use_loaded)
6437 use_loaded = nasm_malloc(use_package_count * sizeof(bool));
6438 memset(use_loaded, 0, use_package_count * sizeof(bool));
6439
H. Peter Anvin6686de22019-08-10 05:33:14 -07006440 /* First set up the top level input file */
6441 nasm_new(istk);
6442 istk->fp = nasm_open_read(file, NF_TEXT);
6443 src_set(0, file);
6444 istk->lineinc = 1;
6445 if (!istk->fp)
6446 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
6447
6448 strlist_add(deplist, file);
6449
6450 /*
6451 * Set up the stdmac packages as a virtual include file,
6452 * indicated by a null file pointer.
6453 */
6454 nasm_new(inc);
6455 inc->next = istk;
6456 inc->fname = src_set_fname(NULL);
6457 inc->nolist = !list_option('b');
6458 istk = inc;
6459 lfmt->uplevel(LIST_INCLUDE, 0);
6460
H. Peter Anvin8b262472019-02-26 14:00:54 -08006461 pp_add_magic_stdmac();
6462
H. Peter Anvinf7606612016-07-13 14:23:48 -07006463 if (tasm_compatible_mode)
6464 pp_add_stdmac(nasm_stdmac_tasm);
6465
6466 pp_add_stdmac(nasm_stdmac_nasm);
6467 pp_add_stdmac(nasm_stdmac_version);
6468
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006469 if (extrastdmac)
6470 pp_add_stdmac(extrastdmac);
6471
H. Peter Anvinf7606612016-07-13 14:23:48 -07006472 stdmacpos = stdmacros[0];
6473 stdmacnext = &stdmacros[1];
6474
H. Peter Anvind2456592008-06-19 15:04:18 -07006475 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006476
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006477 /*
H. Peter Anvind2354082019-08-27 16:38:48 -07006478 * Define the __?PASS?__ macro. This is defined here unlike all the
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07006479 * other builtins, because it is special -- it varies between
6480 * passes -- but there is really no particular reason to make it
6481 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006482 *
6483 * 0 = dependencies only
6484 * 1 = preparatory passes
6485 * 2 = final pass
6486 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006487 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006488 switch (mode) {
6489 case PP_NORMAL:
6490 apass = pass_final() ? 2 : 1;
6491 break;
6492 case PP_DEPS:
6493 apass = 0;
6494 break;
6495 case PP_PREPROC:
6496 apass = 3;
6497 break;
6498 default:
6499 panic();
6500 }
6501
H. Peter Anvin8571f062019-09-23 16:40:03 -07006502 define_smacro("__?PASS?__", true, make_tok_num(NULL, apass), NULL);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006503}
6504
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006505static void pp_init(void)
6506{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006507}
6508
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006509/*
6510 * Get a line of tokens. If we popped the macro expansion/include stack,
6511 * we return a pointer to the dummy token tok_pop; at that point if
6512 * istk is NULL then we have reached end of input;
6513 */
6514static Token tok_pop; /* Dummy token placeholder */
6515
6516static Token *pp_tokline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006517{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006518 while (true) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006519 Line *l = istk->expansion;
6520 Token *tline = NULL;
6521 Token *dtline;
6522
H. Peter Anvine2c80182005-01-15 22:15:51 +00006523 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006524 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00006525 * buffer or from the input file.
6526 */
6527 tline = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006528 while (l && l->finishes) {
6529 MMacro *fm = l->finishes;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006530
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006531 if (!fm->name && fm->in_progress > 1) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006532 /*
6533 * This is a macro-end marker for a macro with no
6534 * name, which means it's not really a macro at all
6535 * but a %rep block, and the `in_progress' field is
6536 * more than 1, meaning that we still need to
6537 * repeat. (1 means the natural last repetition; 0
6538 * means termination by %exitrep.) We have
6539 * therefore expanded up to the %endrep, and must
6540 * push the whole block on to the expansion buffer
6541 * again. We don't bother to remove the macro-end
6542 * marker: we'd only have to generate another one
6543 * if we did.
6544 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006545 fm->in_progress--;
6546 list_for_each(l, fm->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006547 Token *t, *tt, **tail;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006548 Line *ll;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006549
Chang S. Baebec812f2020-02-07 15:49:38 -08006550 istk->mstk.mstk->lineno = 0;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006551 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006552 ll->next = istk->expansion;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006553 tail = &ll->first;
6554
6555 list_for_each(t, l->first) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006556 if (t->len) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006557 tt = *tail = dup_Token(NULL, t);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006558 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006559 }
6560 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006561 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006562 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006563 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006564 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006565 MMacro *m = istk->mstk.mstk;
6566
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006567 /*
6568 * Check whether a `%rep' was started and not ended
6569 * within this macro expansion. This can happen and
6570 * should be detected. It's a fatal error because
6571 * I'm too confused to work out how to recover
6572 * sensibly from it.
6573 */
6574 if (defining) {
6575 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07006576 nasm_panic("defining with name in expansion");
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006577 else if (m->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07006578 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006579 " expansion of macro `%s'", m->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006580 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006581
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006582 /*
6583 * FIXME: investigate the relationship at this point between
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006584 * istk->mstk.mstk and fm
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006585 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006586 istk->mstk = m->mstk;
6587 if (m->name) {
6588 /*
6589 * This was a real macro call, not a %rep, and
6590 * therefore the parameter information needs to
6591 * be freed and the iteration count/nesting
6592 * depth adjusted.
6593 */
6594
6595 if (!--mmacro_deadman.levels) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006596 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006597 * If all mmacro processing done,
6598 * clear all counters and the deadman
6599 * message trigger.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006600 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006601 nasm_zero(mmacro_deadman); /* Clear all counters */
Adam Majer91e72402017-07-25 10:42:01 +02006602 }
6603
Adam Majer91e72402017-07-25 10:42:01 +02006604#if 0
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006605 if (m->prev) {
6606 pop_mmacro(m);
6607 fm->in_progress --;
6608 } else
Adam Majer91e72402017-07-25 10:42:01 +02006609#endif
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006610 {
6611 nasm_free(m->params);
6612 free_tlist(m->iline);
6613 nasm_free(m->paramlen);
6614 fm->in_progress = 0;
6615 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006616 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006617
6618 /*
6619 * FIXME It is incorrect to always free_mmacro here.
6620 * It leads to usage-after-free.
6621 *
6622 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
6623 */
6624#if 0
6625 else
6626 free_mmacro(m);
6627#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006628 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006629 istk->expansion = l->next;
6630 nasm_free(l);
6631 lfmt->downlevel(LIST_MACRO);
6632 return &tok_pop;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006633 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006634
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006635 do { /* until we get a line we can use */
6636 char *line;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006637
6638 if (istk->expansion) { /* from a macro expansion */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006639 Line *l = istk->expansion;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006640 int32_t lineno;
6641
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006642 if (istk->mstk.mstk) {
6643 istk->mstk.mstk->lineno++;
6644 if (istk->mstk.mstk->fname)
6645 lineno = istk->mstk.mstk->lineno +
6646 istk->mstk.mstk->xline;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006647 else
6648 lineno = 0; /* Defined at init time or builtin */
6649 } else {
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006650 lineno = src_get_linnum();
H. Peter Anvin6686de22019-08-10 05:33:14 -07006651 }
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006652
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006653 tline = l->first;
6654 istk->expansion = l->next;
6655 nasm_free(l);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006656
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006657 line = detoken(tline, false);
H. Peter Anvin6686de22019-08-10 05:33:14 -07006658 if (!istk->nolist)
6659 lfmt->line(LIST_MACRO, lineno, line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006660 nasm_free(line);
6661 } else if ((line = read_line())) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006662 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006663 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006664 nasm_free(line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006665 } else {
6666 /*
6667 * The current file has ended; work down the istk
6668 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00006669 Include *i = istk;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006670 if (i->fp)
6671 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006672 if (i->conds) {
6673 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07006674 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006675 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00006676 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07006677 if (i->next)
6678 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006679 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08006680 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006681 nasm_free(i);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006682 return &tok_pop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006683 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006684 } while (0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006685
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006686 /*
6687 * We must expand MMacro parameters and MMacro-local labels
6688 * _before_ we plunge into directive processing, to cope
6689 * with things like `%define something %1' such as STRUC
6690 * uses. Unless we're _defining_ a MMacro, in which case
6691 * those tokens should be left alone to go into the
6692 * definition; and unless we're in a non-emitting
6693 * condition, in which case we don't want to meddle with
6694 * anything.
6695 */
H. Peter Anvin (Intel)bacf04a2020-06-08 13:29:06 -07006696 if (!defining &&
6697 !(istk->conds && !emitting(istk->conds->state)) &&
6698 !(istk->mstk.mmac && !istk->mstk.mmac->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006699 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006700 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006701
H. Peter Anvine2c80182005-01-15 22:15:51 +00006702 /*
6703 * Check the line to see if it's a preprocessor directive.
6704 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006705 if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) {
6706 if (dtline)
6707 return dtline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006708 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006709 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006710 * We're defining a multi-line macro. We emit nothing
6711 * at all, and just
6712 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00006713 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006714 MMacro *mmac = defining->dstk.mmac;
6715
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006716 Line *l = nasm_malloc(sizeof(Line));
6717 l->next = defining->expansion;
6718 l->first = tline;
6719 l->finishes = NULL;
6720 defining->expansion = l;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006721
6722 /*
6723 * Remember if this mmacro expansion contains %00:
6724 * if it does, we will have to handle leading labels
6725 * specially.
6726 */
6727 if (mmac) {
6728 const Token *t;
6729 list_for_each(t, tline) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006730 if (!memcmp(t->text.a, "%00", 4))
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006731 mmac->capture_label = true;
6732 }
6733 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006734 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006735 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006736 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00006737 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006738 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00006739 * directive so we keep our place correctly.
6740 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006741 free_tlist(tline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006742 } else if (istk->mstk.mstk && !istk->mstk.mstk->in_progress) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006743 /*
6744 * We're in a %rep block which has been terminated, so
6745 * we're walking through to the %endrep without
6746 * emitting anything. Emit nothing at all, not even a
6747 * blank line: when we emerge from the %rep block we'll
6748 * give a line-number directive so we keep our place
6749 * correctly.
6750 */
6751 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006752 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006753 tline = expand_smacro(tline);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006754 if (!expand_mmacro(tline))
6755 return tline;
6756 }
6757 }
6758}
6759
6760static char *pp_getline(void)
6761{
6762 char *line = NULL;
6763 Token *tline;
6764
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006765 while (true) {
6766 tline = pp_tokline();
6767 if (tline == &tok_pop) {
6768 /*
6769 * We popped the macro/include stack. If istk is empty,
6770 * we are at end of input, otherwise just loop back.
6771 */
6772 if (!istk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006773 break;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006774 } else {
6775 /*
6776 * De-tokenize the line and emit it.
6777 */
6778 line = detoken(tline, true);
6779 free_tlist(tline);
6780 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006781 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006782 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006783
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006784 if (list_option('e') && istk && !istk->nolist && line && line[0]) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006785 char *buf = nasm_strcat(" ;;; ", line);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006786 lfmt->line(LIST_MACRO, -1, buf);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006787 nasm_free(buf);
6788 }
6789
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006790 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006791}
6792
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006793static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006794{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006795 if (defining) {
6796 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006797 nasm_nonfatal("end of file while still defining macro `%s'",
6798 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006799 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006800 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006801 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006802
6803 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006804 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006805 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08006806
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006807 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006808 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07006809 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006810 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006811 Include *i = istk;
6812 istk = istk->next;
6813 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03006814 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006815 }
6816 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006817 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07006818 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006819}
6820
6821static void pp_cleanup_session(void)
6822{
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07006823 nasm_free(use_loaded);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006824 free_llist(predef);
6825 predef = NULL;
6826 delete_Blocks();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006827 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006828}
6829
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006830static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006831{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006832 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006833}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00006834
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006835static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006836{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006837 Token *inc, *space, *name;
6838 Line *l;
6839
H. Peter Anvin734b1882002-04-30 21:01:08 +00006840 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006841 space = new_White(name);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006842 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006843
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006844 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006845 l->next = predef;
6846 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006847 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006848 predef = l;
6849}
6850
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006851static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006852{
6853 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006854 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00006855 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006856
6857 equals = strchr(definition, '=');
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006858 space = new_White(NULL);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006859 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006860 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006861 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00006862 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006863 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006864 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006865
H. Peter Anvin8571f062019-09-23 16:40:03 -07006866 /* We can't predefine a TOK_LOCAL_MACRO for obvious reasons... */
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006867 if (space->next->type != TOK_PREPROC_ID &&
6868 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08006869 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006870
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006871 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006872 l->next = predef;
6873 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006874 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006875 predef = l;
6876}
6877
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006878static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00006879{
6880 Token *def, *space;
6881 Line *l;
6882
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006883 space = new_White(NULL);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006884 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006885 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00006886
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006887 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00006888 l->next = predef;
6889 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006890 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00006891 predef = l;
6892}
6893
H. Peter Anvin05990342018-06-11 13:32:42 -07006894/* Insert an early preprocessor command that doesn't need special handling */
6895static void pp_pre_command(const char *what, char *string)
6896{
6897 char *cmd;
6898 Token *def, *space;
6899 Line *l;
6900
6901 def = tokenize(string);
6902 if (what) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006903 space = new_White(def);
H. Peter Anvin8571f062019-09-23 16:40:03 -07006904 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
6905 def = new_Token(space, TOK_PREPROC_ID, cmd, nasm_last_string_len());
6906 nasm_free(cmd);
H. Peter Anvin05990342018-06-11 13:32:42 -07006907 }
6908
6909 l = nasm_malloc(sizeof(Line));
6910 l->next = predef;
6911 l->first = def;
6912 l->finishes = NULL;
6913 predef = l;
6914}
6915
H. Peter Anvinf7606612016-07-13 14:23:48 -07006916static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006917{
H. Peter Anvinf7606612016-07-13 14:23:48 -07006918 macros_t **mp;
6919
6920 /* Find the end of the list and avoid duplicates */
6921 for (mp = stdmacros; *mp; mp++) {
6922 if (*mp == macros)
6923 return; /* Nothing to do */
6924 }
6925
6926 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
6927
6928 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00006929}
6930
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006931static void pp_extra_stdmac(macros_t *macros)
6932{
6933 extrastdmac = macros;
6934}
6935
H. Peter Anvin8571f062019-09-23 16:40:03 -07006936/* Create a numeric token */
6937static Token *make_tok_num(Token *next, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006938{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04006939 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08006940 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvin8571f062019-09-23 16:40:03 -07006941 return new_Token(next, TOK_NUMBER, numbuf, len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006942}
6943
H. Peter Anvin8571f062019-09-23 16:40:03 -07006944/* Create a quoted string token */
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07006945static Token *make_tok_qstr_len(Token *next, const char *str, size_t len)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006946{
H. Peter Anvin8571f062019-09-23 16:40:03 -07006947 char *p = nasm_quote(str, &len);
6948 return new_Token_free(next, TOK_STRING, p, len);
6949}
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07006950static Token *make_tok_qstr(Token *next, const char *str)
6951{
6952 return make_tok_qstr_len(next, str, strlen(str));
6953}
H. Peter Anvin8571f062019-09-23 16:40:03 -07006954
6955/* Create a single-character operator token */
6956static Token *make_tok_char(Token *next, char op)
6957{
6958 Token *t = new_Token(next, TOK_OTHER, NULL, 1);
6959 t->text.a[0] = op;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006960 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006961}
6962
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006963static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07006964{
6965 if (!m)
6966 return;
6967
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006968 /* We need to print the mstk.mmac list in reverse order */
6969 pp_list_one_macro(m->mstk.mmac, severity);
H. Peter Anvin37368952016-05-09 14:10:32 -07006970
6971 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07006972 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08006973 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07006974 }
6975}
6976
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006977static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006978{
H. Peter Anvinddb29062018-12-11 00:06:29 -08006979 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006980
H. Peter Anvinddb29062018-12-11 00:06:29 -08006981 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
6982 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006983
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03006984 if (istk)
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006985 pp_list_one_macro(istk->mstk.mmac, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006986
H. Peter Anvinddb29062018-12-11 00:06:29 -08006987 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006988}
6989
H. Peter Anvine7469712016-02-18 02:20:59 -08006990const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006991 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006992 pp_reset,
6993 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006994 pp_cleanup_pass,
6995 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006996 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006997 pp_pre_define,
6998 pp_pre_undefine,
6999 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07007000 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07007001 pp_include_path,
7002 pp_error_list_macros,
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07007003 pp_suppress_error
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00007004};