blob: 53136abd3175b7003a41fbf1444ca57de50c17a0 [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
373 nasm_zero(t->text.a);
374
375 t->len = tok_check_len(len);
376 textp = (len > INLINE_TEXT)
377 ? (t->text.p.ptr = nasm_malloc(len+1)) : t->text.a;
378 memcpy(textp, text, len+1);
379 return t;
380}
381
382/*
383 * Set the text field to the existing pre-allocated string, either
384 * taking over or freeing the allocation in the process.
385 */
386static Token *set_text_free(struct Token *t, char *text, unsigned int len)
387{
388 if (t->len > INLINE_TEXT)
389 nasm_free(t->text.p.ptr);
390
391 nasm_zero(t->text.a);
392
393 t->len = tok_check_len(len);
394 if (len > INLINE_TEXT) {
395 t->text.p.ptr = text;
396 } else {
397 memcpy(t->text.a, text, len+1);
398 nasm_free(text);
399 }
400
401 return t;
402}
403
404/*
405 * Allocate a new buffer containing a copy of the text field
406 * of the token.
407 */
408static char *dup_text(const struct Token *t)
409{
410 size_t size = t->len + 1;
411 char *p = nasm_malloc(size);
412
413 return memcpy(p, tok_text(t), size);
414}
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000415
416/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800417 * Multi-line macro definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000418 * these, which is essentially a container to allow several linked
419 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700420 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000421 * Note that in this module, linked lists are treated as stacks
422 * wherever possible. For this reason, Lines are _pushed_ on to the
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800423 * `expansion' field in MMacro structures, so that the linked list,
424 * if walked, would give the macro lines in reverse order; this
425 * means that we can walk the list when expanding a macro, and thus
426 * push the lines on to the `expansion' field in _istk_ in reverse
427 * order (so that when popped back off they are in the right
428 * order). It may seem cockeyed, and it relies on my design having
429 * an even number of steps in, but it works...
430 *
431 * Some of these structures, rather than being actual lines, are
432 * markers delimiting the end of the expansion of a given macro.
433 * This is for use in the cycle-tracking and %rep-handling code.
434 * Such structures have `finishes' non-NULL, and `first' NULL. All
435 * others have `finishes' NULL, but `first' may still be NULL if
436 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000437 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000438struct Line {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800439 Line *next;
440 MMacro *finishes;
441 Token *first;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500442};
443
444/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000445 * To handle an arbitrary level of file inclusion, we maintain a
446 * stack (ie linked list) of these things.
447 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000448struct Include {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800449 Include *next;
450 FILE *fp;
451 Cond *conds;
452 Line *expansion;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700453 const char *fname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700454 struct mstk mstk;
H. Peter Anvin6686de22019-08-10 05:33:14 -0700455 int lineno, lineinc;
456 bool nolist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000457};
458
459/*
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700460 * File real name hash, so we don't have to re-search the include
461 * path for every pass (and potentially more than that if a file
462 * is used more than once.)
463 */
464struct hash_table FileHash;
465
466/*
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -0700467 * Counters to trap on insane macro recursion or processing.
468 * Note: for smacros these count *down*, for mmacros they count *up*.
469 */
470struct deadman {
471 int64_t total; /* Total number of macros/tokens */
472 int64_t levels; /* Descent depth across all macros */
473 bool triggered; /* Already triggered, no need for error msg */
474};
475
476static struct deadman smacro_deadman, mmacro_deadman;
477
478/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000479 * Conditional assembly: we maintain a separate stack of these for
480 * each level of file inclusion. (The only reason we keep the
481 * stacks separate is to ensure that a stray `%endif' in a file
482 * included from within the true branch of a `%if' won't terminate
483 * it and cause confusion: instead, rightly, it'll cause an error.)
484 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -0700485enum cond_state {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000486 /*
487 * These states are for use just after %if or %elif: IF_TRUE
488 * means the condition has evaluated to truth so we are
489 * currently emitting, whereas IF_FALSE means we are not
490 * currently emitting but will start doing so if a %else comes
491 * up. In these states, all directives are admissible: %elif,
492 * %else and %endif. (And of course %if.)
493 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800494 COND_IF_TRUE, COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000495 /*
496 * These states come up after a %else: ELSE_TRUE means we're
497 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
498 * any %elif or %else will cause an error.
499 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800500 COND_ELSE_TRUE, COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000501 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200502 * These states mean that we're not emitting now, and also that
503 * nothing until %endif will be emitted at all. COND_DONE is
504 * used when we've had our moment of emission
505 * and have now started seeing %elifs. COND_NEVER is used when
506 * the condition construct in question is contained within a
507 * non-emitting branch of a larger condition construct,
508 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000509 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800510 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000511};
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -0700512struct Cond {
513 Cond *next;
514 enum cond_state state;
515};
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800516#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000517
H. Peter Anvin70653092007-10-19 14:42:29 -0700518/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000519 * These defines are used as the possible return values for do_directive
520 */
521#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300522#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000523
Keith Kanios852f1ee2009-07-12 00:19:55 -0500524/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000525 * Condition codes. Note that we use c_ prefix not C_ because C_ is
526 * used in nasm.h for the "real" condition codes. At _this_ level,
527 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
528 * ones, so we need a different enum...
529 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700530static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000531 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
532 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000533 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000534};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700535enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000536 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
537 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 -0700538 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
539 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000540};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700541static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000542 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
543 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 +0000544 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000545};
546
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800547/*
548 * Directive names.
549 */
550/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
551static int is_condition(enum preproc_token arg)
552{
553 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
554}
555
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000556/* For TASM compatibility we need to be able to recognise TASM compatible
557 * conditional compilation directives. Using the NASM pre-processor does
558 * not work, so we look for them specifically from the following list and
559 * then jam in the equivalent NASM directive into the input stream.
560 */
561
H. Peter Anvine2c80182005-01-15 22:15:51 +0000562enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000563 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
564 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
565};
566
H. Peter Anvin476d2862007-10-02 22:04:15 -0700567static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000568 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
569 "ifndef", "include", "local"
570};
571
572static int StackSize = 4;
H. Peter Anvin6c8b2be2016-05-24 23:46:50 -0700573static const char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000574static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800575static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000576
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000577static Context *cstk;
578static Include *istk;
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300579static const struct strlist *ipath_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000580
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300581static struct strlist *deplist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000582
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300583static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000584
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800585static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700586static bool do_predef;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800587static enum preproc_mode pp_mode;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000588
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000589/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800590 * The current set of multi-line macros we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000591 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800592static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000593
594/*
595 * The current set of single-line macros we have defined.
596 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700597static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000598
599/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800600 * The multi-line macro we are currently defining, or the %rep
601 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000602 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800603static MMacro *defining;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000604
Charles Crayned4200be2008-07-12 16:42:33 -0700605static uint64_t nested_mac_count;
606static uint64_t nested_rep_count;
607
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000608/*
609 * The number of macro parameters to allocate space for at a time.
610 */
611#define PARAM_DELTA 16
612
613/*
H. Peter Anvinf7606612016-07-13 14:23:48 -0700614 * The standard macro set: defined in macros.c in a set of arrays.
615 * This gives our position in any macro set, while we are processing it.
616 * The stdmacset is an array of such macro sets.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000617 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700618static macros_t *stdmacpos;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700619static macros_t **stdmacnext;
620static macros_t *stdmacros[8];
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +0300621static macros_t *extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000622
623/*
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -0700624 * Map of which %use packages have been loaded
625 */
626static bool *use_loaded;
627
628/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000629 * Forward declarations.
630 */
H. Peter Anvinf7606612016-07-13 14:23:48 -0700631static void pp_add_stdmac(macros_t *macros);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000632static Token *expand_mmac_params(Token * tline);
633static Token *expand_smacro(Token * tline);
634static Token *expand_id(Token * tline);
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +0400635static Context *get_ctx(const char *name, const char **namep);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700636static Token *make_tok_num(Token *next, int64_t val);
637static Token *make_tok_qstr(Token *next, const char *str);
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -0700638static Token *make_tok_qstr_len(Token *next, const char *str, size_t len);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700639static Token *make_tok_char(Token *next, char op);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700640static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700641 const char *text, size_t txtlen);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700642static Token *new_Token_free(Token * next, enum pp_token_type type,
643 char *text, size_t txtlen);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700644static Token *dup_Token(Token *next, const Token *src);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700645static Token *new_White(Token *next);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700646static Token *delete_Token(Token *t);
647static Token *steal_Token(Token *dst, Token *src);
H. Peter Anvindd88aa92019-09-12 19:39:48 -0700648static const struct use_package *
H. Peter Anvin8571f062019-09-23 16:40:03 -0700649get_use_pkg(Token *t, const char *dname, const char **name);
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -0700650static void mark_smac_params(Token *tline, const SMacro *tmpl,
651 enum pp_token_type type);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000652
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700653/* Safe test for token type, false on x == NULL */
654static inline bool tok_type(const Token *x, enum pp_token_type t)
655{
656 return x && x->type == t;
657}
658
659/* Whitespace token? */
660static inline bool tok_white(const Token *x)
661{
662 return tok_type(x, TOK_WHITESPACE);
663}
664
665/* Skip past any whitespace */
666static inline Token *skip_white(Token *x)
667{
668 while (tok_white(x))
669 x = x->next;
670
671 return x;
672}
673
674/* Delete any whitespace */
675static Token *zap_white(Token *x)
676{
677 while (tok_white(x))
678 x = delete_Token(x);
679
680 return x;
681}
682
H. Peter Anvin8571f062019-09-23 16:40:03 -0700683/*
684 * Single special character tests. The use of & rather than && is intentional; it
685 * tells the compiler that it is safe to access text.a[1] unconditionally; hopefully
686 * a smart compiler should turn it into a 16-bit memory reference.
687 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700688static inline bool tok_is(const Token *x, char c)
689{
H. Peter Anvin8571f062019-09-23 16:40:03 -0700690 return x && ((x->text.a[0] == c) & !x->text.a[1]);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700691}
692
693/* True if any other kind of token that "c", but not NULL */
694static inline bool tok_isnt(const Token *x, char c)
695{
H. Peter Anvin8571f062019-09-23 16:40:03 -0700696 return x && !((x->text.a[0] == c) & !x->text.a[1]);
697}
698
699/*
700 * Unquote a token if it is a string, and set its type to
701 * TOK_INTERNAL_STRING.
702 */
703static const char *unquote_token(Token *t)
704{
705 if (t->type != TOK_STRING)
706 return tok_text(t);
707
708 t->type = TOK_INTERNAL_STRING;
709
710 if (t->len > INLINE_TEXT) {
711 char *p = t->text.p.ptr;
712
713 t->len = nasm_unquote(p, NULL);
714
715 if (t->len <= INLINE_TEXT) {
716 nasm_zero(t->text.a);
717 memcpy(t->text.a, p, t->len);
718 nasm_free(p);
719 return t->text.a;
720 } else {
721 return p;
722 }
723 } else {
724 t->len = nasm_unquote(t->text.a, NULL);
725 return t->text.a;
726 }
727}
728
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700729/*
730 * Same as unquote_token(), but error out if the resulting string
731 * contains unacceptable control characters.
732 */
H. Peter Anvin8571f062019-09-23 16:40:03 -0700733static const char *unquote_token_cstr(Token *t)
734{
735 if (t->type != TOK_STRING)
736 return tok_text(t);
737
738 t->type = TOK_INTERNAL_STRING;
739
740 if (t->len > INLINE_TEXT) {
741 char *p = t->text.p.ptr;
742
743 t->len = nasm_unquote_cstr(p, NULL);
744
745 if (t->len <= INLINE_TEXT) {
746 nasm_zero(t->text.a);
747 memcpy(t->text.a, p, t->len);
748 nasm_free(p);
749 return t->text.a;
750 } else {
751 return p;
752 }
753 } else {
754 t->len = nasm_unquote_cstr(t->text.a, NULL);
755 return t->text.a;
756 }
757}
758
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700759/*
760 * Convert a TOK_INTERNAL_STRING token to a quoted
761 * TOK_STRING tokens.
762 */
763static Token *quote_any_token(Token *t);
764static inline Token *quote_token(Token *t)
765{
766 if (likely(!tok_is(t, TOK_INTERNAL_STRING)))
767 return t;
768
769 return quote_any_token(t);
770}
771
772/*
773 * Convert *any* kind of token to a quoted
774 * TOK_STRING token.
775 */
776static Token *quote_any_token(Token *t)
H. Peter Anvin8571f062019-09-23 16:40:03 -0700777{
778 size_t len;
779 char *p;
780
781 p = nasm_quote(tok_text(t), &len);
782 t->type = TOK_STRING;
783 return set_text_free(t, p, len);
784}
785
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400786/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700787 * In-place reverse a list of tokens.
788 */
789static Token *reverse_tokens(Token *t)
790{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800791 Token *prev = NULL;
792 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700793
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800794 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400795 next = t->next;
796 t->next = prev;
797 prev = t;
798 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800799 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700800
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800801 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700802}
803
804/*
H. Peter Anvin8571f062019-09-23 16:40:03 -0700805 * getenv() variant operating on an input token
806 */
807static const char *pp_getenv(const Token *t, bool warn)
808{
809 const char *txt = tok_text(t);
810 const char *v;
811 char *buf = NULL;
812 bool is_string = false;
813
814 if (!t)
815 return NULL;
816
817 switch (t->type) {
818 case TOK_ENVIRON:
819 txt += 2; /* Skip leading %! */
820 is_string = nasm_isquote(*txt);
821 break;
822
823 case TOK_STRING:
824 is_string = true;
825 break;
826
827 case TOK_INTERNAL_STRING:
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700828 case TOK_NAKED_STRING:
H. Peter Anvin8571f062019-09-23 16:40:03 -0700829 case TOK_ID:
830 is_string = false;
831 break;
832
833 default:
834 return NULL;
835 }
836
837 if (is_string) {
838 buf = nasm_strdup(txt);
839 nasm_unquote_cstr(buf, NULL);
840 txt = buf;
841 }
842
843 v = getenv(txt);
844 if (warn && !v) {
845 /*!
846 *!environment [on] nonexistent environment variable
847 *! warns if a nonexistent environment variable
848 *! is accessed using the \c{%!} preprocessor
849 *! construct (see \k{getenv}.) Such environment
850 *! variables are treated as empty (with this
851 *! warning issued) starting in NASM 2.15;
852 *! earlier versions of NASM would treat this as
853 *! an error.
854 */
855 nasm_warn(WARN_ENVIRONMENT, "nonexistent environment variable `%s'", txt);
856 v = "";
857 }
858
859 if (buf)
860 nasm_free(buf);
861
862 return v;
863}
864
865/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300866 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000867 * front of them. We do it here because I could not find any other
868 * place to do it for the moment, and it is a hack (ideally it would
869 * be nice to be able to use the NASM pre-processor to do it).
870 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000871static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000872{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000873 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400874 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000875
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400876 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000877
878 /* Binary search for the directive name */
879 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400880 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400881 q = nasm_skip_word(p);
882 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000883 if (len) {
884 oldchar = p[len];
885 p[len] = 0;
886 while (j - i > 1) {
887 k = (j + i) / 2;
888 m = nasm_stricmp(p, tasm_directives[k]);
889 if (m == 0) {
890 /* We have found a directive, so jam a % in front of it
891 * so that NASM will then recognise it as one if it's own.
892 */
893 p[len] = oldchar;
894 len = strlen(p);
895 oldline = line;
896 line = nasm_malloc(len + 2);
897 line[0] = '%';
898 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700899 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300900 * NASM does not recognise IFDIFI, so we convert
901 * it to %if 0. This is not used in NASM
902 * compatible code, but does need to parse for the
903 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000904 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700905 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000906 } else {
907 memcpy(line + 1, p, len + 1);
908 }
909 nasm_free(oldline);
910 return line;
911 } else if (m < 0) {
912 j = k;
913 } else
914 i = k;
915 }
916 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000917 }
918 return line;
919}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000920
H. Peter Anvin76690a12002-04-30 20:52:49 +0000921/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000922 * The pre-preprocessing stage... This function translates line
923 * number indications as they emerge from GNU cpp (`# lineno "file"
924 * flags') into NASM preprocessor line number indications (`%line
925 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000926 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000927static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000928{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000929 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000930 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000931
H. Peter Anvine2c80182005-01-15 22:15:51 +0000932 if (line[0] == '#' && line[1] == ' ') {
933 oldline = line;
934 fname = oldline + 2;
935 lineno = atoi(fname);
936 fname += strspn(fname, "0123456789 ");
937 if (*fname == '"')
938 fname++;
939 fnlen = strcspn(fname, "\"");
940 line = nasm_malloc(20 + fnlen);
941 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
942 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000943 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000944 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000945 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000946 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000947}
948
949/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000950 * Free a linked list of tokens.
951 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000952static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000953{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400954 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000955 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000956}
957
958/*
959 * Free a linked list of lines.
960 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000961static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000962{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400963 Line *l, *tmp;
964 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000965 free_tlist(l->first);
966 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000967 }
968}
969
970/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700971 * Free an array of linked lists of tokens
972 */
973static void free_tlist_array(Token **array, size_t nlists)
974{
975 Token **listp = array;
976
977 while (nlists--)
978 free_tlist(*listp++);
979
980 nasm_free(array);
981}
982
983/*
984 * Duplicate a linked list of tokens.
985 */
986static Token *dup_tlist(const Token *list, Token ***tailp)
987{
988 Token *newlist = NULL;
989 Token **tailpp = &newlist;
990 const Token *t;
991
992 list_for_each(t, list) {
993 Token *nt;
994 *tailpp = nt = dup_Token(NULL, t);
995 tailpp = &nt->next;
996 }
997
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700998 if (tailp) {
999 **tailp = newlist;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07001000 *tailp = tailpp;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001001 }
1002
1003 return newlist;
1004}
1005
1006/*
1007 * Duplicate a linked list of tokens with a maximum count
1008 */
1009static Token *dup_tlistn(const Token *list, size_t cnt, Token ***tailp)
1010{
1011 Token *newlist = NULL;
1012 Token **tailpp = &newlist;
1013 const Token *t;
1014
1015 list_for_each(t, list) {
1016 Token *nt;
1017 if (!cnt--)
1018 break;
1019 *tailpp = nt = dup_Token(NULL, t);
1020 tailpp = &nt->next;
1021 }
1022
1023 if (tailp) {
1024 **tailp = newlist;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07001025 if (newlist)
1026 *tailp = tailpp;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001027 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07001028
1029 return newlist;
1030}
1031
1032/*
1033 * Duplicate a linked list of tokens in reverse order
1034 */
1035static Token *dup_tlist_reverse(const Token *list, Token *tail)
1036{
1037 const Token *t;
1038
1039 list_for_each(t, list)
1040 tail = dup_Token(tail, t);
1041
1042 return tail;
1043}
1044
1045/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001046 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +00001047 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001048static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001049{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001050 nasm_free(m->name);
1051 free_tlist(m->dlist);
1052 nasm_free(m->defaults);
1053 free_llist(m->expansion);
1054 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001055}
1056
1057/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001058 * Clear or free an SMacro
H. Peter Anvin8b262472019-02-26 14:00:54 -08001059 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001060static void free_smacro_members(SMacro *s)
H. Peter Anvin8b262472019-02-26 14:00:54 -08001061{
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07001062 if (s->params) {
1063 int i;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001064 for (i = 0; i < s->nparam; i++) {
1065 if (s->params[i].name.len > INLINE_TEXT)
1066 nasm_free(s->params[i].name.text.p.ptr);
1067 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07001068 nasm_free(s->params);
1069 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08001070 nasm_free(s->name);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001071 free_tlist(s->expansion);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001072}
1073
1074static void clear_smacro(SMacro *s)
1075{
1076 free_smacro_members(s);
1077 /* Wipe everything except the next pointer */
1078 memset(&s->next + 1, 0, sizeof *s - sizeof s->next);
1079}
1080
1081/*
1082 * Free an SMacro
1083 */
1084static void free_smacro(SMacro *s)
1085{
1086 free_smacro_members(s);
1087 nasm_free(s);
H. Peter Anvin8b262472019-02-26 14:00:54 -08001088}
1089
1090/*
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07001091 * Free all currently defined macros, and free the hash tables if empty
H. Peter Anvin97a23472007-09-16 17:57:25 -07001092 */
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07001093enum clear_what {
1094 CLEAR_NONE = 0,
1095 CLEAR_DEFINE = 1, /* Clear smacros */
1096 CLEAR_DEFALIAS = 2, /* Clear smacro aliases */
1097 CLEAR_ALLDEFINE = CLEAR_DEFINE|CLEAR_DEFALIAS,
1098 CLEAR_MMACRO = 4,
1099 CLEAR_ALL = CLEAR_ALLDEFINE|CLEAR_MMACRO
1100};
1101
1102static void clear_smacro_table(struct hash_table *smt, enum clear_what what)
H. Peter Anvin97a23472007-09-16 17:57:25 -07001103{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001104 struct hash_iterator it;
1105 const struct hash_node *np;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07001106 bool empty = true;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001107
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07001108 /*
1109 * Walk the hash table and clear out anything we don't want
1110 */
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001111 hash_for_each(smt, it, np) {
1112 SMacro *tmp;
1113 SMacro *s = np->data;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07001114 SMacro **head = (SMacro **)&np->data;
1115
1116 list_for_each_safe(s, tmp, s) {
1117 if (what & ((enum clear_what)s->alias + 1)) {
1118 *head = s->next;
1119 free_smacro(s);
1120 } else {
1121 empty = false;
1122 }
1123 }
H. Peter Anvin97a23472007-09-16 17:57:25 -07001124 }
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07001125
1126 /*
1127 * Free the hash table and keys if and only if it is now empty.
1128 * Note: we cannot free keys even for an empty list above, as that
1129 * mucks up the hash algorithm.
1130 */
1131 if (empty)
1132 hash_free_all(smt, true);
1133}
1134
1135static void free_smacro_table(struct hash_table *smt)
1136{
1137 clear_smacro_table(smt, CLEAR_ALLDEFINE);
H. Peter Anvin072771e2008-05-22 13:17:51 -07001138}
1139
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001140static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -07001141{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001142 struct hash_iterator it;
1143 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001144
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001145 hash_for_each(mmt, it, np) {
1146 MMacro *tmp;
1147 MMacro *m = np->data;
1148 nasm_free((void *)np->key);
1149 list_for_each_safe(m, tmp, m)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001150 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001151 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001152 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -07001153}
1154
1155static void free_macros(void)
1156{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001157 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001158 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001159}
1160
1161/*
1162 * Initialize the hash tables
1163 */
1164static void init_macros(void)
1165{
H. Peter Anvin97a23472007-09-16 17:57:25 -07001166}
1167
1168/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001169 * Pop the context stack.
1170 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001171static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001172{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001173 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001174
1175 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001176 free_smacro_table(&c->localmac);
H. Peter Anvin8571f062019-09-23 16:40:03 -07001177 nasm_free((char *)c->name);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001178 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001179}
1180
H. Peter Anvin072771e2008-05-22 13:17:51 -07001181/*
1182 * Search for a key in the hash index; adding it if necessary
1183 * (in which case we initialize the data pointer to NULL.)
1184 */
1185static void **
1186hash_findi_add(struct hash_table *hash, const char *str)
1187{
1188 struct hash_insert hi;
1189 void **r;
1190 char *strx;
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001191 size_t l = strlen(str) + 1;
H. Peter Anvin072771e2008-05-22 13:17:51 -07001192
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001193 r = hash_findib(hash, str, l, &hi);
H. Peter Anvin072771e2008-05-22 13:17:51 -07001194 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001195 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -07001196
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001197 strx = nasm_malloc(l); /* Use a more efficient allocator here? */
1198 memcpy(strx, str, l);
H. Peter Anvin072771e2008-05-22 13:17:51 -07001199 return hash_add(&hi, strx, NULL);
1200}
1201
1202/*
1203 * Like hash_findi, but returns the data element rather than a pointer
1204 * to it. Used only when not adding a new element, hence no third
1205 * argument.
1206 */
1207static void *
1208hash_findix(struct hash_table *hash, const char *str)
1209{
1210 void **p;
1211
1212 p = hash_findi(hash, str, NULL);
1213 return p ? *p : NULL;
1214}
1215
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001216/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001217 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001218 * if there no more left -- return NULL
1219 */
1220static char *line_from_stdmac(void)
1221{
1222 unsigned char c;
1223 const unsigned char *p = stdmacpos;
1224 char *line, *q;
1225 size_t len = 0;
1226
1227 if (!stdmacpos)
1228 return NULL;
1229
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -07001230 /*
1231 * 32-126 is ASCII, 127 is end of line, 128-31 are directives
1232 * (allowed to wrap around) corresponding to PP_* tokens 0-159.
1233 */
1234 while ((c = *p++) != 127) {
1235 uint8_t ndir = c - 128;
1236 if (ndir < 256-96)
1237 len += pp_directives_len[ndir] + 1;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001238 else
1239 len++;
1240 }
1241
1242 line = nasm_malloc(len + 1);
1243 q = line;
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -07001244
1245 while ((c = *stdmacpos++) != 127) {
1246 uint8_t ndir = c - 128;
1247 if (ndir < 256-96) {
1248 memcpy(q, pp_directives[ndir], pp_directives_len[ndir]);
1249 q += pp_directives_len[ndir];
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001250 *q++ = ' ';
1251 } else {
1252 *q++ = c;
1253 }
1254 }
1255 stdmacpos = p;
1256 *q = '\0';
1257
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -07001258 if (*stdmacpos == 127) {
H. Peter Anvinf7606612016-07-13 14:23:48 -07001259 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001260 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -07001261 if (*stdmacnext) {
1262 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001263 } else if (do_predef) {
1264 Line *pd, *l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001265
1266 /*
1267 * Nasty hack: here we push the contents of
1268 * `predef' on to the top-level expansion stack,
1269 * since this is the most convenient way to
1270 * implement the pre-include and pre-define
1271 * features.
1272 */
1273 list_for_each(pd, predef) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07001274 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001275 l->next = istk->expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07001276 l->first = dup_tlist(pd->first, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001277 l->finishes = NULL;
1278
1279 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001280 }
1281 do_predef = false;
1282 }
1283 }
1284
1285 return line;
1286}
1287
H. Peter Anvin6686de22019-08-10 05:33:14 -07001288/*
1289 * Read a line from a file. Return NULL on end of file.
1290 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001291static char *line_from_file(FILE *f)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001292{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001293 int c;
1294 unsigned int size, next;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001295 const unsigned int delta = 512;
1296 const unsigned int pad = 8;
1297 unsigned int nr_cont = 0;
1298 bool cont = false;
1299 char *buffer, *p;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07001300 int32_t lineno;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001301
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001302 size = delta;
1303 p = buffer = nasm_malloc(size);
1304
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001305 do {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001306 c = fgetc(f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001307
1308 switch (c) {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001309 case EOF:
1310 if (p == buffer) {
1311 nasm_free(buffer);
1312 return NULL;
1313 }
1314 c = 0;
1315 break;
1316
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001317 case '\r':
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001318 next = fgetc(f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001319 if (next != '\n')
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001320 ungetc(next, f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001321 if (cont) {
1322 cont = false;
1323 continue;
1324 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001325 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001326 break;
1327
1328 case '\n':
1329 if (cont) {
1330 cont = false;
1331 continue;
1332 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001333 c = 0;
1334 break;
1335
1336 case 032: /* ^Z = legacy MS-DOS end of file mark */
1337 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001338 break;
1339
1340 case '\\':
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001341 next = fgetc(f);
1342 ungetc(next, f);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +04001343 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001344 cont = true;
1345 nr_cont++;
1346 continue;
1347 }
1348 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001349 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001350
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001351 if (p >= (buffer + size - pad)) {
1352 buffer = nasm_realloc(buffer, size + delta);
1353 p = buffer + size - pad;
1354 size += delta;
1355 }
1356
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001357 *p++ = c;
1358 } while (c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001359
H. Peter Anvinab6f8312019-08-09 22:31:45 -07001360 lineno = src_get_linnum() + istk->lineinc +
1361 (nr_cont * istk->lineinc);
1362 src_set_linnum(lineno);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001363
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001364 return buffer;
1365}
1366
1367/*
H. Peter Anvin6686de22019-08-10 05:33:14 -07001368 * Common read routine regardless of source
1369 */
1370static char *read_line(void)
1371{
1372 char *line;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001373 FILE *f = istk->fp;
H. Peter Anvin6686de22019-08-10 05:33:14 -07001374
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001375 if (f)
1376 line = line_from_file(f);
H. Peter Anvin6686de22019-08-10 05:33:14 -07001377 else
1378 line = line_from_stdmac();
1379
1380 if (!line)
1381 return NULL;
1382
1383 if (!istk->nolist)
1384 lfmt->line(LIST_READ, src_get_linnum(), line);
1385
1386 return line;
1387}
1388
1389/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00001390 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001391 * don't need to parse the value out of e.g. numeric tokens: we
1392 * simply split one string into many.
1393 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07001394static Token *tokenize(const char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001395{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001396 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001397 Token *list = NULL;
1398 Token *t, **tail = &list;
1399
H. Peter Anvine2c80182005-01-15 22:15:51 +00001400 while (*line) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001401 const char *p = line;
1402 const char *ep = NULL; /* End of token, for trimming the end */
1403 size_t toklen;
1404 char firstchar = *p; /* Can be used to override the first char */
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001405
H. Peter Anvine2c80182005-01-15 22:15:51 +00001406 if (*p == '%') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001407 /*
1408 * Preprocessor construct; find the end of the token.
1409 * Classification is handled later, because %{...} can be
1410 * used to create any preprocessor token.
1411 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001412 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001413 if (*p == '+' && !nasm_isdigit(p[1])) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001414 /* Paste token */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001415 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001416 } else if (nasm_isdigit(*p) ||
1417 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001418 do {
1419 p++;
1420 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001421 while (nasm_isdigit(*p));
H. Peter Anvin8571f062019-09-23 16:40:03 -07001422 } else if (*p == '{' || *p == '[') {
1423 /* %{...} or %[...] */
1424 char firstchar = *p;
1425 char endchar = *p + 2; /* } or ] */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001426 int lvl = 1;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001427 line += (*p++ == '{'); /* Skip { but not [ (yet) */
1428 while (lvl) {
1429 if (*p == firstchar) {
1430 lvl++;
1431 } else if (*p == endchar) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001432 lvl--;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001433 } else if (nasm_isquote(*p)) {
1434 p = nasm_skip_string(p);
1435 }
1436
1437 /*
1438 * *p can have been advanced to a null character by
1439 * nasm_skip_string()
1440 */
1441 if (!*p) {
1442 nasm_warn(WARN_OTHER, "unterminated %%%c construct",
1443 firstchar);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001444 break;
1445 }
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001446 p++;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001447 }
1448 ep = lvl ? p : p-1; /* Terminal character not part of token */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001449 } else if (*p == '?') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001450 /* %? or %?? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001451 p++;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001452 if (*p == '?')
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001453 p++;
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001454 } else if (*p == '!') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001455 /* Environment variable reference */
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001456 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001457 if (nasm_isidchar(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001458 do {
1459 p++;
1460 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001461 while (nasm_isidchar(*p));
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001462 } else if (nasm_isquote(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001463 p = nasm_skip_string(p);
1464 if (*p)
1465 p++;
1466 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001467 nasm_nonfatalf(ERR_PASS1, "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001468 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001469 /* %! without anything else... */
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001470 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07001471 } else if (*p == ',') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001472 /* Conditional comma */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07001473 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001474 } else if (nasm_isidchar(*p) ||
H. Peter Anvin8571f062019-09-23 16:40:03 -07001475 ((*p == '%' || *p == '$') && nasm_isidchar(p[1]))) {
1476 /* Identifier or some sort */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001477 do {
1478 p++;
1479 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001480 while (nasm_isidchar(*p));
H. Peter Anvin8571f062019-09-23 16:40:03 -07001481 } else if (*p == '%') {
1482 /* %% operator */
1483 p++;
1484 }
1485
1486 if (!ep)
1487 ep = p;
1488 toklen = ep - line;
1489
1490 /* Classify here, to handle %{...} correctly */
1491 if (toklen < 2) {
1492 type = TOK_OTHER; /* % operator */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001493 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001494 char c0 = line[1];
1495
1496 switch (c0) {
1497 case '+':
1498 type = (toklen == 2) ? TOK_PASTE : TOK_MMACRO_PARAM;
1499 break;
1500
1501 case '-':
1502 type = TOK_MMACRO_PARAM;
1503 break;
1504
1505 case '?':
1506 if (toklen == 2)
1507 type = TOK_PREPROC_Q;
1508 else if (toklen == 3 && line[2] == '?')
1509 type = TOK_PREPROC_QQ;
1510 else
1511 type = TOK_PREPROC_ID;
1512 break;
1513
1514 case '!':
1515 type = (toklen == 2) ? TOK_OTHER : TOK_ENVIRON;
1516 break;
1517
1518 case '%':
1519 type = (toklen == 2) ? TOK_OTHER : TOK_LOCAL_SYMBOL;
1520 break;
1521
1522 case '$':
1523 type = (toklen == 2) ? TOK_OTHER : TOK_LOCAL_MACRO;
1524 break;
1525
1526 case '[':
1527 line += 2; /* Skip %[ */
1528 firstchar = *line; /* Don't clobber */
1529 toklen -= 2;
1530 type = TOK_INDIRECT;
1531 break;
1532
1533 case ',':
1534 type = (toklen == 2) ? TOK_COND_COMMA : TOK_PREPROC_ID;
1535 break;
1536
1537 case '\'':
1538 case '\"':
1539 case '`':
1540 /* %{'string'} */
1541 type = TOK_PREPROC_ID;
1542 break;
1543
1544 case ':':
1545 type = TOK_MMACRO_PARAM; /* %{:..} */
1546 break;
1547
1548 default:
1549 if (nasm_isdigit(c0))
1550 type = TOK_MMACRO_PARAM;
1551 else if (nasm_isidchar(c0) || toklen > 2)
1552 type = TOK_PREPROC_ID;
1553 else
1554 type = TOK_OTHER;
1555 break;
1556 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001557 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001558 } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001559 /*
1560 * An identifier. This includes the ? operator, which is
1561 * treated as a keyword, not as a special character
1562 * operator
1563 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001564 type = TOK_ID;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001565 while (nasm_isidchar(*++p))
1566 ;
1567 } else if (nasm_isquote(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001568 /*
1569 * A string token.
1570 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001571 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001572 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001573
H. Peter Anvine2c80182005-01-15 22:15:51 +00001574 if (*p) {
1575 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001576 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001577 nasm_warn(WARN_OTHER, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001578 /* Handling unterminated strings by UNV */
1579 /* type = -1; */
1580 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001581 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001582 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001583 p += 2;
H. Peter Anvin13506202018-11-28 14:55:58 -08001584 } else if (nasm_isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001585 bool is_hex = false;
1586 bool is_float = false;
1587 bool has_e = false;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001588 char c;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001589
H. Peter Anvine2c80182005-01-15 22:15:51 +00001590 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001591 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001592 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001593
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001594 if (*p == '$') {
1595 p++;
1596 is_hex = true;
1597 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001598
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001599 for (;;) {
1600 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001601
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001602 if (!is_hex && (c == 'e' || c == 'E')) {
1603 has_e = true;
1604 if (*p == '+' || *p == '-') {
1605 /*
1606 * e can only be followed by +/- if it is either a
1607 * prefixed hex number or a floating-point number
1608 */
1609 p++;
1610 is_float = true;
1611 }
1612 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1613 is_hex = true;
1614 } else if (c == 'P' || c == 'p') {
1615 is_float = true;
1616 if (*p == '+' || *p == '-')
1617 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001618 } else if (nasm_isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001619 ; /* just advance */
1620 else if (c == '.') {
1621 /*
1622 * we need to deal with consequences of the legacy
1623 * parser, like "1.nolist" being two tokens
1624 * (TOK_NUMBER, TOK_ID) here; at least give it
1625 * a shot for now. In the future, we probably need
1626 * a flex-based scanner with proper pattern matching
1627 * to do it as well as it can be done. Nothing in
1628 * the world is going to help the person who wants
1629 * 0x123.p16 interpreted as two tokens, though.
1630 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07001631 const char *r = p;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001632 while (*r == '_')
1633 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001634
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001635 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1636 (!is_hex && (*r == 'e' || *r == 'E')) ||
1637 (*r == 'p' || *r == 'P')) {
1638 p = r;
1639 is_float = true;
1640 } else
1641 break; /* Terminate the token */
1642 } else
1643 break;
1644 }
1645 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001646
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001647 if (p == line+1 && *line == '$') {
1648 type = TOK_OTHER; /* TOKEN_HERE */
1649 } else {
1650 if (has_e && !is_hex) {
1651 /* 1e13 is floating-point, but 1e13h is not */
1652 is_float = true;
1653 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001654
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001655 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1656 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001657 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001658 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001659 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001660 /*
1661 * Whitespace just before end-of-line is discarded by
1662 * pretending it's a comment; whitespace just before a
1663 * comment gets lumped into the comment.
1664 */
1665 if (!*p || *p == ';') {
1666 type = TOK_COMMENT;
1667 while (*p)
1668 p++;
1669 }
1670 } else if (*p == ';') {
1671 type = TOK_COMMENT;
1672 while (*p)
1673 p++;
1674 } else {
1675 /*
1676 * Anything else is an operator of some kind. We check
1677 * for all the double-character operators (>>, <<, //,
H. Peter Anvin8571f062019-09-23 16:40:03 -07001678 * %%, <=, >=, ==, !=, <>, &&, ||, ^^) and the triple-
1679 * character operators (<<<, >>>, <=>) but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001680 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001681 */
1682 type = TOK_OTHER;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001683 switch (*p++) {
1684 case '>':
1685 if (*p == '>') {
1686 p++;
1687 if (*p == '>')
1688 p++;
H. Peter Anvind03a6c82019-10-07 21:29:05 -07001689 } else if (*p == '=') {
1690 p++;
1691 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07001692 break;
1693
1694 case '<':
1695 if (*p == '<') {
1696 p++;
1697 if (*p == '<')
1698 p++;
1699 } else if (*p == '=') {
1700 p++;
1701 if (*p == '>')
1702 p++;
1703 } else if (*p == '>') {
1704 p++;
1705 }
1706 break;
1707
1708 case '!':
1709 if (*p == '=')
1710 p++;
1711 break;
1712
1713 case '/':
1714 case '=':
1715 case '&':
1716 case '|':
1717 case '^':
1718 /* These operators can be doubled but nothing else */
1719 if (*p == p[-1])
1720 p++;
1721 break;
1722
1723 default:
1724 break;
1725 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001726 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001727
H. Peter Anvin8571f062019-09-23 16:40:03 -07001728 if (type == TOK_WHITESPACE) {
1729 *tail = t = new_White(NULL);
1730 tail = &t->next;
1731 } else if (type != TOK_COMMENT) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001732 if (!ep)
1733 ep = p;
1734 *tail = t = new_Token(NULL, type, line, ep - line);
H. Peter Anvin8571f062019-09-23 16:40:03 -07001735 *tok_text_buf(t) = firstchar; /* E.g. %{foo} -> {foo -> %foo */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001736 tail = &t->next;
1737 }
1738 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001739 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001740 return list;
1741}
1742
H. Peter Anvin8571f062019-09-23 16:40:03 -07001743/*
1744 * Tokens are allocated in blocks to improve speed. Set the blocksize
1745 * to 0 to use regular nasm_malloc(); this is useful for debugging.
1746 *
1747 * alloc_Token() returns a zero-initialized token structure.
1748 */
1749#define TOKEN_BLOCKSIZE 4096
1750
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001751#if TOKEN_BLOCKSIZE
H. Peter Anvin8571f062019-09-23 16:40:03 -07001752
1753static Token *freeTokens = NULL;
1754static Token *tokenblocks = NULL;
1755
1756static Token *alloc_Token(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001757{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001758 Token *t = freeTokens;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001759
H. Peter Anvin8571f062019-09-23 16:40:03 -07001760 if (unlikely(!t)) {
1761 Token *block;
1762 size_t i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001763
H. Peter Anvin8571f062019-09-23 16:40:03 -07001764 nasm_newn(block, TOKEN_BLOCKSIZE);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001765
H. Peter Anvin8571f062019-09-23 16:40:03 -07001766 /*
1767 * The first entry in each array are a linked list of
1768 * block allocations and is not used for data.
1769 */
1770 block[0].next = tokenblocks;
1771 block[0].type = TOK_BLOCK;
1772 tokenblocks = block;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001773
H. Peter Anvin8571f062019-09-23 16:40:03 -07001774 /*
1775 * Add the rest to the free list
1776 */
1777 for (i = 2; i < TOKEN_BLOCKSIZE - 1; i++)
1778 block[i].next = &block[i+1];
1779
1780 freeTokens = &block[2];
1781
1782 /*
1783 * Return the topmost usable token
1784 */
1785 return &block[1];
1786 }
1787
1788 freeTokens = t->next;
1789 t->next = NULL;
1790 return t;
H. Peter Anvince616072002-04-30 21:02:23 +00001791}
1792
H. Peter Anvin8571f062019-09-23 16:40:03 -07001793static Token *delete_Token(Token *t)
1794{
1795 Token *next = t->next;
1796
1797 nasm_zero(*t);
1798 t->next = freeTokens;
1799 freeTokens = t;
1800
1801 return next;
1802}
1803
H. Peter Anvine2c80182005-01-15 22:15:51 +00001804static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001805{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001806 Token *block, *blocktmp;
H. Peter Anvince616072002-04-30 21:02:23 +00001807
H. Peter Anvin8571f062019-09-23 16:40:03 -07001808 list_for_each_safe(block, blocktmp, tokenblocks)
1809 nasm_free(block);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001810
H. Peter Anvin8571f062019-09-23 16:40:03 -07001811 freeTokens = tokenblocks = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001812}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001813
H. Peter Anvin8571f062019-09-23 16:40:03 -07001814#else
1815
1816static inline Token *alloc_Token(void)
1817{
1818 Token *t;
1819 nasm_new(*t);
1820 return t;
1821}
1822
1823static Token *delete_Token(Token *t)
1824{
1825 Token *next = t->next;
1826 nasm_free(t);
1827 return next;
1828}
1829
1830static inline void delete_Blocks(void)
1831{
1832 /* Nothing to do */
1833}
1834
1835#endif
1836
H. Peter Anvin734b1882002-04-30 21:01:08 +00001837/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001838 * this function creates a new Token and passes a pointer to it
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001839 * back to the caller. It sets the type, text, and next pointer elements.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001840 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001841static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001842 const char *text, size_t txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001843{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001844 Token *t = alloc_Token();
1845 char *textp;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001846
H. Peter Anvin734b1882002-04-30 21:01:08 +00001847 t->next = next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001848 t->type = type;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001849 if (type == TOK_WHITESPACE) {
1850 t->len = 1;
1851 t->text.a[0] = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001852 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001853 if (text && text[0] && !txtlen)
1854 txtlen = tok_strlen(text);
1855
1856 t->len = tok_check_len(txtlen);
1857
1858 if (text) {
1859 textp = (txtlen > INLINE_TEXT)
1860 ? (t->text.p.ptr = nasm_malloc(txtlen+1)) : t->text.a;
1861 memcpy(textp, text, txtlen);
1862 textp[txtlen] = '\0'; /* In case we needed malloc() */
1863 } else {
1864 /*
1865 * Allocate a buffer but do not fill it. The caller
1866 * can fill in text, but must not change the length.
1867 * The filled in text must be exactly txtlen once
1868 * the buffer is filled and before the token is added
1869 * to any line lists.
1870 */
1871 if (txtlen > INLINE_TEXT)
1872 t->text.p.ptr = nasm_zalloc(txtlen+1);
1873 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001874 }
1875 return t;
1876}
1877
H. Peter Anvin8571f062019-09-23 16:40:03 -07001878/*
1879 * Same as new_Token(), but text belongs to the new token and is
1880 * either taken over or freed. This function MUST be called
1881 * with valid txt and txtlen, unlike new_Token().
1882 */
1883static Token *new_Token_free(Token * next, enum pp_token_type type,
1884 char *text, size_t txtlen)
1885{
1886 Token *t = alloc_Token();
1887
1888 t->next = next;
1889 t->type = type;
1890 t->len = tok_check_len(txtlen);
1891
1892 if (txtlen <= INLINE_TEXT) {
1893 memcpy(t->text.a, text, txtlen);
1894 free(text);
1895 } else {
1896 t->text.p.ptr = text;
1897 }
1898
1899 return t;
1900}
1901
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001902static Token *dup_Token(Token *next, const Token *src)
1903{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001904 Token *t = alloc_Token();
1905
1906 memcpy(t, src, sizeof *src);
1907 t->next = next;
1908
1909 if (t->len > INLINE_TEXT) {
1910 t->text.p.ptr = nasm_malloc(t->len + 1);
1911 memcpy(t->text.p.ptr, src->text.p.ptr, t->len+1);
1912 }
1913
1914 return t;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001915}
1916
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001917static Token *new_White(Token *next)
1918{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001919 Token *t = alloc_Token();
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001920
H. Peter Anvin8571f062019-09-23 16:40:03 -07001921 t->next = next;
1922 t->type = TOK_WHITESPACE;
1923 t->len = 1;
1924 t->text.a[0] = ' ';
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001925
H. Peter Anvin8571f062019-09-23 16:40:03 -07001926 return t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001927}
1928
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001929/*
H. Peter Anvin8571f062019-09-23 16:40:03 -07001930 * This *transfers* the content from one token to another, leaving the
1931 * next pointer of the latter intact. Unlike dup_Token(), the old
1932 * token is destroyed, except for its next pointer, and the text
1933 * pointer allocation, if any, is simply transferred.
1934 */
1935static Token *steal_Token(Token *dst, Token *src)
1936{
1937 /* Overwrite everything except the next pointers */
1938 memcpy((char *)dst + sizeof(Token *), (char *)src + sizeof(Token *),
1939 sizeof(Token) - sizeof(Token *));
1940
1941 /* Clear the donor token */
1942 memset((char *)src + sizeof(Token *), 0, sizeof(Token) - sizeof(Token *));
1943
1944 return dst;
1945}
1946
1947/*
1948 * Convert a line of tokens back into text. This modifies the list
1949 * by expanding environment variables.
1950 *
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001951 * If expand_locals is not zero, identifiers of the form "%$*xxx"
H. Peter Anvin8571f062019-09-23 16:40:03 -07001952 * are also transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001953 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001954static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001955{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001956 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001957 char *line, *p;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001958 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001959
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001960 list_for_each(t, tlist) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001961 switch (t->type) {
1962 case TOK_ENVIRON:
1963 {
1964 const char *v = pp_getenv(t, true);
1965 set_text(t, v, tok_strlen(v));
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07001966 t->type = TOK_NAKED_STRING;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001967 break;
1968 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001969
H. Peter Anvin8571f062019-09-23 16:40:03 -07001970 case TOK_LOCAL_MACRO:
1971 case TOK_LOCAL_SYMBOL:
1972 if (expand_locals) {
1973 const char *q;
1974 char *p;
1975 Context *ctx = get_ctx(tok_text(t), &q);
1976 if (ctx) {
1977 p = nasm_asprintf("..@%"PRIu64".%s", ctx->number, q);
1978 set_text_free(t, p, nasm_last_string_len());
1979 t->type = TOK_ID;
1980 }
1981 }
1982 break;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001983
H. Peter Anvin8571f062019-09-23 16:40:03 -07001984 default:
1985 break; /* No modifications */
1986 }
1987
1988 if (debug_level(2)) {
1989 unsigned int t_len = t->len;
1990 unsigned int s_len = tok_strlen(tok_text(t));
1991 if (t_len != s_len) {
1992 nasm_panic("assertion failed: token \"%s\" type %u len %u has t->len %u\n",
1993 tok_text(t), t->type, s_len, t_len);
1994 t->len = s_len;
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001995 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001996 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001997
H. Peter Anvin8571f062019-09-23 16:40:03 -07001998 len += t->len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001999 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04002000
H. Peter Anvin734b1882002-04-30 21:01:08 +00002001 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04002002
H. Peter Anvin8571f062019-09-23 16:40:03 -07002003 list_for_each(t, tlist)
2004 p = mempcpy(p, tok_text(t), t->len);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002005 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04002006
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002007 return line;
2008}
2009
2010/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002011 * A scanner, suitable for use by the expression evaluator, which
2012 * operates on a line of Tokens. Expects a pointer to a pointer to
2013 * the first token in the line to be passed in as its private_data
2014 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07002015 *
2016 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00002017 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08002018struct ppscan {
2019 Token *tptr;
2020 int ntokens;
2021};
2022
H. Peter Anvine2c80182005-01-15 22:15:51 +00002023static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002024{
H. Peter Anvin8b262472019-02-26 14:00:54 -08002025 struct ppscan *pps = private_data;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002026 Token *tline;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002027 const char *txt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002028
H. Peter Anvine2c80182005-01-15 22:15:51 +00002029 do {
H. Peter Anvin8b262472019-02-26 14:00:54 -08002030 if (pps->ntokens && (tline = pps->tptr)) {
2031 pps->ntokens--;
2032 pps->tptr = tline->next;
2033 } else {
2034 pps->tptr = NULL;
2035 pps->ntokens = 0;
2036 return tokval->t_type = TOKEN_EOS;
2037 }
2038 } while (tline->type == TOK_WHITESPACE || tline->type == TOK_COMMENT);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002039
H. Peter Anvin8571f062019-09-23 16:40:03 -07002040 txt = tok_text(tline);
2041 tokval->t_charptr = (char *)txt; /* Fix this */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07002042
H. Peter Anvin8571f062019-09-23 16:40:03 -07002043 if (txt[0] == '$') {
2044 if (!txt[1]) {
2045 return tokval->t_type = TOKEN_HERE;
2046 } else if (txt[1] == '$' && !txt[2]) {
2047 return tokval->t_type = TOKEN_BASE;
2048 } else if (tline->type == TOK_ID) {
2049 tokval->t_charptr++;
2050 return tokval->t_type = TOKEN_ID;
2051 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002052 }
2053
H. Peter Anvin8571f062019-09-23 16:40:03 -07002054 switch (tline->type) {
2055 default:
2056 if (tline->len == 1)
2057 return tokval->t_type = txt[0];
2058 /* fall through */
2059 case TOK_ID:
2060 return nasm_token_hash(txt, tokval);
2061
2062 case TOK_NUMBER:
2063 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002064 bool rn_error;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002065 tokval->t_integer = readnum(txt, &rn_error);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002066 if (rn_error)
2067 return tokval->t_type = TOKEN_ERRNUM;
2068 else
2069 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07002070 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002071
H. Peter Anvin8571f062019-09-23 16:40:03 -07002072 case TOK_FLOAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002073 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002074
2075 case TOK_STRING:
2076 tokval->t_charptr = (char *)unquote_token(tline);
2077 tokval->t_inttwo = tline->len;
2078 return tokval->t_type = TOKEN_STR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002079 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002080}
2081
2082/*
H. Peter Anvind2354082019-08-27 16:38:48 -07002083 * 1. An expression (true if nonzero 0)
2084 * 2. The keywords true, on, yes for true
2085 * 3. The keywords false, off, no for false
2086 * 4. An empty line, for true
2087 *
2088 * On error, return defval (usually the previous value)
2089 */
2090static bool pp_get_boolean_option(Token *tline, bool defval)
2091{
2092 static const char * const noyes[] = {
2093 "no", "yes",
2094 "false", "true",
2095 "off", "on"
2096 };
2097 struct ppscan pps;
2098 struct tokenval tokval;
2099 expr *evalresult;
2100
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002101 tline = skip_white(tline);
2102 if (!tline)
H. Peter Anvind2354082019-08-27 16:38:48 -07002103 return true;
2104
2105 if (tline->type == TOK_ID) {
2106 size_t i;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002107 const char *txt = tok_text(tline);
2108
H. Peter Anvind2354082019-08-27 16:38:48 -07002109 for (i = 0; i < ARRAY_SIZE(noyes); i++)
H. Peter Anvin8571f062019-09-23 16:40:03 -07002110 if (!nasm_stricmp(txt, noyes[i]))
H. Peter Anvind2354082019-08-27 16:38:48 -07002111 return i & 1;
2112 }
2113
2114 pps.tptr = NULL;
2115 pps.tptr = tline;
2116 pps.ntokens = -1;
2117 tokval.t_type = TOKEN_INVALID;
2118 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
2119
2120 if (!evalresult)
2121 return true;
2122
2123 if (tokval.t_type)
2124 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
2125 if (!is_really_simple(evalresult)) {
2126 nasm_nonfatal("boolean flag expression must be a constant");
2127 return defval;
2128 }
2129
2130 return reloc_value(evalresult) != 0;
2131}
2132
2133/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002134 * Compare a string to the name of an existing macro; this is a
2135 * simple wrapper which calls either strcmp or nasm_stricmp
2136 * depending on the value of the `casesense' parameter.
2137 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002138static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002139{
H. Peter Anvin734b1882002-04-30 21:01:08 +00002140 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002141}
2142
2143/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07002144 * Compare a string to the name of an existing macro; this is a
2145 * simple wrapper which calls either strcmp or nasm_stricmp
2146 * depending on the value of the `casesense' parameter.
2147 */
2148static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
2149{
2150 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
2151}
2152
2153/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002154 * Return the Context structure associated with a %$ token. Return
2155 * NULL, having _already_ reported an error condition, if the
2156 * context stack isn't deep enough for the supplied number of $
2157 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002158 *
2159 * If "namep" is non-NULL, set it to the pointer to the macro name
2160 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002161 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04002162static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002163{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002164 Context *ctx;
2165 int i;
2166
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002167 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002168 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002169
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002170 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00002171 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002172
H. Peter Anvine2c80182005-01-15 22:15:51 +00002173 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002174 nasm_nonfatal("`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002175 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002176 }
2177
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002178 name += 2;
2179 ctx = cstk;
2180 i = 0;
2181 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002182 name++;
2183 i++;
2184 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002185 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002186 if (!ctx) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002187 nasm_nonfatal("`%s': context stack is only"
2188 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002189 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002190 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002191
2192 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002193 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002194
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04002195 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002196}
2197
2198/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002199 * Open an include file. This routine must always return a valid
2200 * file pointer if it returns - it's responsible for throwing an
2201 * ERR_FATAL and bombing out completely if not. It should also try
2202 * the include path one by one until it finds the file or reaches
2203 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07002204 *
2205 * Note: for INC_PROBE the function returns NULL at all times;
2206 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002207 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07002208enum incopen_mode {
2209 INC_NEEDED, /* File must exist */
2210 INC_OPTIONAL, /* Missing is OK */
2211 INC_PROBE /* Only an existence probe */
2212};
2213
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002214/* This is conducts a full pathname search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002215static FILE *inc_fopen_search(const char *file, char **slpath,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002216 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002217{
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -08002218 const struct strlist_entry *ip = strlist_head(ipath_list);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002219 FILE *fp;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002220 const char *prefix = "";
night199ukfdb1a1b2018-10-18 23:19:47 +02002221 char *sp;
H. Peter Anvind81a2352016-09-21 14:03:18 -07002222 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002223
H. Peter Anvine2c80182005-01-15 22:15:51 +00002224 while (1) {
night199ukfdb1a1b2018-10-18 23:19:47 +02002225 sp = nasm_catfile(prefix, file);
H. Peter Anvind81a2352016-09-21 14:03:18 -07002226 if (omode == INC_PROBE) {
2227 fp = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002228 found = nasm_file_exists(sp);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07002229 } else {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002230 fp = nasm_open_read(sp, fmode);
H. Peter Anvind81a2352016-09-21 14:03:18 -07002231 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002232 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07002233 if (found) {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002234 *slpath = sp;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002235 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002236 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002237
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002238 nasm_free(sp);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002239
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002240 if (!ip) {
2241 *slpath = NULL;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002242 return NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002243 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002244
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002245 prefix = ip->str;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002246 ip = ip->next;
2247 }
2248}
2249
2250/*
2251 * Open a file, or test for the presence of one (depending on omode),
2252 * considering the include path.
2253 */
2254static FILE *inc_fopen(const char *file,
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002255 struct strlist *dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002256 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002257 enum incopen_mode omode,
2258 enum file_flags fmode)
2259{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002260 struct hash_insert hi;
2261 void **hp;
2262 char *path;
2263 FILE *fp = NULL;
2264
2265 hp = hash_find(&FileHash, file, &hi);
2266 if (hp) {
2267 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03002268 if (path || omode != INC_NEEDED) {
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002269 strlist_add(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03002270 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002271 } else {
2272 /* Need to do the actual path search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002273 fp = inc_fopen_search(file, &path, omode, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002274
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002275 /* Positive or negative result */
2276 hash_add(&hi, nasm_strdup(file), path);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002277
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002278 /*
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002279 * Add file to dependency path.
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002280 */
2281 if (path || omode != INC_NEEDED)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002282 strlist_add(dhead, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00002283 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002284
H. Peter Anvin (Intel)5d68f982020-06-01 12:32:35 -07002285 if (path && !fp && omode != INC_PROBE)
2286 fp = nasm_open_read(path, fmode);
2287
2288 if (omode == INC_NEEDED && !fp) {
2289 if (!path)
2290 errno = ENOENT;
2291
2292 nasm_nonfatal("unable to open include file `%s': %s",
2293 file, strerror(errno));
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002294 }
2295
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002296 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002297 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002298
2299 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002300}
2301
2302/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07002303 * Opens an include or input file. Public version, for use by modules
2304 * that get a file:lineno pair and need to look at the file again
2305 * (e.g. the CodeView debug backend). Returns NULL on failure.
2306 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07002307FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07002308{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002309 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07002310}
2311
2312/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002313 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002314 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002315 * return true if _any_ single-line macro of that name is defined.
2316 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002317 * `nparam' or no parameters is defined.
2318 *
2319 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002320 * defined, or nparam is -1, the address of the definition structure
2321 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002322 * is NULL, no action will be taken regarding its contents, and no
2323 * error will occur.
2324 *
2325 * Note that this is also called with nparam zero to resolve
2326 * `ifdef'.
2327 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002328static bool
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002329smacro_defined(Context *ctx, const char *name, int nparam, SMacro **defn,
H. Peter Anvind2354082019-08-27 16:38:48 -07002330 bool nocase, bool find_alias)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002331{
H. Peter Anvin166c2472008-05-28 12:28:58 -07002332 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002333 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002334
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002335 smtbl = ctx ? &ctx->localmac : &smacros;
H. Peter Anvind2354082019-08-27 16:38:48 -07002336
2337restart:
H. Peter Anvin166c2472008-05-28 12:28:58 -07002338 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002339
H. Peter Anvine2c80182005-01-15 22:15:51 +00002340 while (m) {
2341 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002342 (nparam <= 0 || m->nparam == 0 || nparam == m->nparam ||
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002343 (m->greedy && nparam >= m->nparam-1))) {
H. Peter Anvind2354082019-08-27 16:38:48 -07002344 if (m->alias && !find_alias) {
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002345 if (!ppopt.noaliases) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002346 name = tok_text(m->expansion);
H. Peter Anvind2354082019-08-27 16:38:48 -07002347 goto restart;
2348 } else {
2349 continue;
2350 }
2351 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002352 if (defn) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002353 *defn = (nparam == m->nparam || nparam == -1) ? m : NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002354 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002355 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002356 }
2357 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002358 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002359
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002360 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002361}
2362
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002363/* param should be a natural number [0; INT_MAX] */
2364static int read_param_count(const char *str)
2365{
2366 int result;
2367 bool err;
2368
2369 result = readnum(str, &err);
2370 if (result < 0 || result > INT_MAX) {
2371 result = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002372 nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]",
2373 str, 0, INT_MAX);
2374 } else if (err)
2375 nasm_nonfatal("unable to parse parameter count `%s'", str);
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002376 return result;
2377}
2378
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002379/*
2380 * Count and mark off the parameters in a multi-line macro call.
2381 * This is called both from within the multi-line macro expansion
2382 * code, and also to mark off the default parameters when provided
2383 * in a %macro definition line.
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002384 *
2385 * Note that we need space in the params array for parameter 0 being
2386 * a possible captured label as well as the final NULL.
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002387 *
2388 * Returns a pointer to the pointer to a terminal comma if present;
2389 * used to drop an empty terminal argument for legacy reasons.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002390 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002391static Token **count_mmac_params(Token *tline, int *nparamp, Token ***paramsp)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002392{
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07002393 int paramsize;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002394 int nparam = 0;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002395 Token *t;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002396 Token **comma = NULL, **maybe_comma = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002397 Token **params;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002398
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002399 paramsize = PARAM_DELTA;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002400 nasm_newn(params, paramsize);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002401
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002402 t = skip_white(tline);
2403 if (t) {
2404 while (true) {
2405 /* Need two slots for captured label and NULL */
2406 if (unlikely(nparam+2 >= paramsize)) {
2407 paramsize += PARAM_DELTA;
2408 params = nasm_realloc(params, sizeof(*params) * paramsize);
2409 }
2410 params[++nparam] = t;
2411 if (tok_is(t, '{')) {
2412 int brace = 1;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002413
2414 comma = NULL; /* Non-empty parameter */
2415
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002416 while (brace && (t = t->next)) {
2417 brace += tok_is(t, '{');
2418 brace -= tok_is(t, '}');
2419 }
H. Peter Anvin (Intel)f8639bd2020-06-04 16:29:53 -07002420
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002421 if (t) {
2422 /*
2423 * Now we've found the closing brace, look further
2424 * for the comma.
2425 */
2426 t = skip_white(t->next);
2427 if (tok_isnt(t, ','))
2428 nasm_nonfatal("braces do not enclose all of macro parameter");
2429 } else {
2430 nasm_nonfatal("expecting closing brace in macro parameter");
2431 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08002432 }
2433
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002434 /* Advance to the next comma */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002435 maybe_comma = &t->next;
2436 while (tok_isnt(t, ',')) {
2437 if (!tok_white(t))
2438 comma = NULL; /* Non-empty parameter */
2439 maybe_comma = &t->next;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002440 t = t->next;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002441 }
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002442
2443 if (!t)
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002444 break; /* End of string, no comma */
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002445
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002446 comma = maybe_comma; /* Point to comma pointer */
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002447 t = skip_white(t->next); /* Eat the comma and whitespace */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08002448 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002449 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002450
2451 params[nparam+1] = NULL;
2452 *paramsp = params;
2453 *nparamp = nparam;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002454
2455 return comma;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002456}
2457
2458/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002459 * Determine whether one of the various `if' conditions is true or
2460 * not.
2461 *
2462 * We must free the tline we get passed.
2463 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002464static enum cond_state if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002465{
H. Peter Anvin70055962007-10-11 00:05:31 -07002466 bool j;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002467 Token *t, *tt, *origline;
2468 struct ppscan pps;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002469 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002470 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002471 enum pp_token_type needtype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002472 const char *dname = pp_directives[ct];
2473 bool casesense = true;
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002474 enum preproc_token cond = PP_COND(ct);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002475
2476 origline = tline;
2477
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002478 switch (cond) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002479 case PP_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002480 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02002481 while (true) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002482 tline = skip_white(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02002483 if (!tline)
2484 break;
2485 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002486 nasm_nonfatal("`%s' expects context identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002487 dname);
2488 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002489 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002490 if (cstk && cstk->name && !nasm_stricmp(tok_text(tline), cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002491 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002492 tline = tline->next;
2493 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002494 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002495
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002496 case PP_IFDEF:
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002497 case PP_IFDEFALIAS:
2498 {
2499 bool alias = cond == PP_IFDEFALIAS;
2500 SMacro *smac;
2501 Context *ctx;
2502 const char *mname;
2503
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002504 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002505 while (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002506 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002507 if (!tline || (tline->type != TOK_ID &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07002508 tline->type != TOK_LOCAL_MACRO)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002509 nasm_nonfatal("`%s' expects macro identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002510 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002511 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002512 }
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002513
2514 mname = tok_text(tline);
2515 ctx = get_ctx(mname, &mname);
H. Peter Anvin (Intel)b91e7732020-06-05 12:22:26 -07002516 if (smacro_defined(ctx, mname, -1, &smac, true, alias) && smac
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002517 && smac->alias == alias) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002518 j = true;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002519 break;
2520 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002521 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002522 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002523 break;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002524 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002525
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002526 case PP_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002527 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002528 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002529 while (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002530 tline = skip_white(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002531 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002532 tline->type != TOK_STRING &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07002533 tline->type != TOK_INTERNAL_STRING &&
2534 tline->type != TOK_ENVIRON)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002535 nasm_nonfatal("`%s' expects environment variable names",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002536 dname);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002537 goto fail;
2538 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002539
2540 j |= !!pp_getenv(tline, false);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002541 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002542 }
2543 break;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002544
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002545 case PP_IFIDNI:
2546 casesense = false;
2547 /* fall through */
2548 case PP_IFIDN:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002549 tline = expand_smacro(tline);
2550 t = tt = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002551 while (tok_isnt(tt, ','))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002552 tt = tt->next;
2553 if (!tt) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002554 nasm_nonfatal("`%s' expects two comma-separated arguments",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002555 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002556 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002557 }
2558 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002559 j = true; /* assume equality unless proved not */
H. Peter Anvin8571f062019-09-23 16:40:03 -07002560 while (tok_isnt(t, ',') && tt) {
2561 unsigned int l1, l2;
2562 const char *t1, *t2;
2563
2564 if (tok_is(tt, ',')) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002565 nasm_nonfatal("`%s': more than one comma on line",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002566 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002567 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002568 }
2569 if (t->type == TOK_WHITESPACE) {
2570 t = t->next;
2571 continue;
2572 }
2573 if (tt->type == TOK_WHITESPACE) {
2574 tt = tt->next;
2575 continue;
2576 }
2577 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002578 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002579 break;
2580 }
H. Peter Anvind2456592008-06-19 15:04:18 -07002581
H. Peter Anvin8571f062019-09-23 16:40:03 -07002582 t1 = unquote_token(t);
2583 t2 = unquote_token(tt);
2584 l1 = t->len;
2585 l2 = tt->len;
2586
2587 if (l1 != l2 || mmemcmp(t1, t2, l1, casesense)) {
2588 j = false;
2589 break;
2590 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00002591
H. Peter Anvine2c80182005-01-15 22:15:51 +00002592 t = t->next;
2593 tt = tt->next;
2594 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002595 if (!tok_is(t, ',') || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002596 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002597 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002598
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002599 case PP_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04002600 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002601 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002602 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00002603
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002604 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002605 tline = expand_id(tline);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002606 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002607 nasm_nonfatal("`%s' expects a macro name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002608 goto fail;
2609 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002610 nasm_zero(searching);
H. Peter Anvin8571f062019-09-23 16:40:03 -07002611 searching.name = dup_text(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002612 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002613 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002614 searching.nparam_max = INT_MAX;
2615 tline = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002616 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002617 if (!tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002618 } else if (!tok_type(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002619 nasm_nonfatal("`%s' expects a parameter count or nothing",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002620 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002621 } else {
2622 searching.nparam_min = searching.nparam_max =
H. Peter Anvin8571f062019-09-23 16:40:03 -07002623 read_param_count(tok_text(tline));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002624 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002625 if (tline && tok_is(tline->next, '-')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002626 tline = tline->next->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002627 if (tok_is(tline, '*'))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002628 searching.nparam_max = INT_MAX;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002629 else if (!tok_type(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002630 nasm_nonfatal("`%s' expects a parameter count after `-'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002631 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002632 else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002633 searching.nparam_max = read_param_count(tok_text(tline));
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002634 if (searching.nparam_min > searching.nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002635 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002636 searching.nparam_max = searching.nparam_min;
2637 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002638 }
2639 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002640 if (tline && tok_is(tline->next, '+')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002641 tline = tline->next;
2642 searching.plus = true;
2643 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002644 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
2645 while (mmac) {
2646 if (!strcmp(mmac->name, searching.name) &&
2647 (mmac->nparam_min <= searching.nparam_max
2648 || searching.plus)
2649 && (searching.nparam_min <= mmac->nparam_max
2650 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002651 found = true;
2652 break;
2653 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002654 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002655 }
2656 if (tline && tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002657 nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002658 nasm_free(searching.name);
2659 j = found;
2660 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002661 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002662
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002663 case PP_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002664 needtype = TOK_ID;
2665 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002666 case PP_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002667 needtype = TOK_NUMBER;
2668 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002669 case PP_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002670 needtype = TOK_STRING;
2671 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002672
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002673iftype:
2674 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002675
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002676 while (tok_white(t) ||
2677 (needtype == TOK_NUMBER && (tok_is(t, '-') | tok_is(t, '+'))))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002678 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002679
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002680 j = tok_type(t, needtype);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002681 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002682
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002683 case PP_IFTOKEN:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002684 tline = expand_smacro(tline);
2685 t = skip_white(tline);
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002686
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002687 j = false;
2688 if (t) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002689 t = skip_white(t->next); /* Skip the actual token + whitespace */
2690 j = !t;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002691 }
2692 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002693
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002694 case PP_IFEMPTY:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002695 tline = expand_smacro(tline);
2696 t = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002697 j = !t; /* Should be empty */
2698 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002699
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002700 case PP_IF:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002701 pps.tptr = tline = expand_smacro(tline);
2702 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002703 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002704 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002705 if (!evalresult)
2706 return -1;
2707 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002708 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002709 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002710 nasm_nonfatal("non-constant value given to `%s'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002711 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002712 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002713 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002714 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002715 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002716
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002717 case PP_IFUSING:
2718 case PP_IFUSABLE:
2719 {
2720 const struct use_package *pkg;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002721 const char *name;
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002722
H. Peter Anvin8571f062019-09-23 16:40:03 -07002723 pkg = get_use_pkg(tline, dname, &name);
2724 if (!name)
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002725 goto fail;
2726
2727 j = pkg && ((cond == PP_IFUSABLE) | use_loaded[pkg->index]);
2728 break;
2729 }
2730
H. Peter Anvine2c80182005-01-15 22:15:51 +00002731 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002732 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002733 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002734 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002735
2736 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002737 return (j ^ PP_COND_NEGATIVE(ct)) ? COND_IF_TRUE : COND_IF_FALSE;
H. Peter Anvin70653092007-10-19 14:42:29 -07002738
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002739fail:
2740 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002741 return COND_NEVER;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002742}
2743
2744/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002745 * Default smacro expansion routine: just returns a copy of the
2746 * expansion list.
2747 */
2748static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002749smacro_expand_default(const SMacro *s, Token **params, int nparams)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002750{
2751 (void)params;
2752 (void)nparams;
2753
2754 return dup_tlist(s->expansion, NULL);
2755}
2756
2757/*
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002758 * Emit a macro defintion or undef to the listing file, if
2759 * desired. This is similar to detoken(), but it handles the reverse
2760 * expansion list, does not expand %! or local variable tokens, and
2761 * does some special handling for macro parameters.
2762 */
2763static void
2764list_smacro_def(enum preproc_token op, const Context *ctx, const SMacro *m)
2765{
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002766 Token *t;
2767 size_t namelen, size;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002768 char *def, *p;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002769 char *context_prefix = NULL;
2770 size_t context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002771
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002772 namelen = strlen(m->name);
2773 size = namelen + 2; /* Include room for space after name + NUL */
2774
2775 if (ctx) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07002776 int context_depth = cstk->depth - ctx->depth + 1;
2777 context_prefix =
2778 nasm_asprintf("[%s::%"PRIu64"] %%%-*s",
2779 ctx->name ? ctx->name : "",
2780 ctx->number, context_depth, "");
2781
2782 context_len = nasm_last_string_len();
2783 memset(context_prefix + context_len - context_depth,
2784 '$', context_depth);
2785 size += context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002786 }
2787
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002788 list_for_each(t, m->expansion)
H. Peter Anvin8571f062019-09-23 16:40:03 -07002789 size += t->len;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002790
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002791 if (m->nparam) {
2792 /*
2793 * Space for ( and either , or ) around each
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002794 * parameter, plus up to 4 flags.
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002795 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002796 int i;
2797
2798 size += 1 + 4 * m->nparam;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002799 for (i = 0; i < m->nparam; i++)
H. Peter Anvin8571f062019-09-23 16:40:03 -07002800 size += m->params[i].name.len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002801 }
2802
2803 def = nasm_malloc(size);
2804 p = def+size;
2805 *--p = '\0';
2806
2807 list_for_each(t, m->expansion) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002808 p -= t->len;
2809 memcpy(p, tok_text(t), t->len);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002810 }
2811
2812 *--p = ' ';
2813
2814 if (m->nparam) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002815 int i;
2816
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002817 *--p = ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002818 for (i = m->nparam-1; i >= 0; i--) {
2819 enum sparmflags flags = m->params[i].flags;
2820 if (flags & SPARM_GREEDY)
2821 *--p = '+';
H. Peter Anvin8571f062019-09-23 16:40:03 -07002822 p -= m->params[i].name.len;
2823 memcpy(p, tok_text(&m->params[i].name), m->params[i].name.len);
2824
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002825 if (flags & SPARM_NOSTRIP)
2826 *--p = '!';
2827 if (flags & SPARM_STR)
2828 *--p = '&';
2829 if (flags & SPARM_EVAL)
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002830 *--p = '=';
2831 *--p = ',';
2832 }
2833 *p = '('; /* First parameter starts with ( not , */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002834 }
2835
2836 p -= namelen;
2837 memcpy(p, m->name, namelen);
2838
H. Peter Anvin6686de22019-08-10 05:33:14 -07002839 if (context_prefix) {
2840 p -= context_len;
2841 memcpy(p, context_prefix, context_len);
2842 nasm_free(context_prefix);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002843 }
2844
2845 nasm_listmsg("%s %s", pp_directives[op], p);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002846 nasm_free(def);
H. Peter Anvin6686de22019-08-10 05:33:14 -07002847}
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002848
2849/*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002850 * Parse smacro arguments, return argument count. If the tmpl argument
2851 * is set, set the nparam, greedy and params field in the template.
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002852 * *tpp is updated to point to the pointer to the first token after the
2853 * prototype.
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002854 *
2855 * The text values from any argument tokens are "stolen" and the
2856 * corresponding text fields set to NULL.
2857 */
2858static int parse_smacro_template(Token ***tpp, SMacro *tmpl)
2859{
2860 int nparam = 0;
2861 enum sparmflags flags;
2862 struct smac_param *params = NULL;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002863 bool err, done;
2864 bool greedy = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002865 Token **tn = *tpp;
2866 Token *t = *tn;
2867 Token *name;
2868
H. Peter Anvin (Intel)d4607842019-08-20 16:19:37 -07002869 /*
2870 * DO NOT skip whitespace here, or we won't be able to distinguish:
2871 *
2872 * %define foo (a,b) ; no arguments, (a,b) is the expansion
2873 * %define bar(a,b) ; two arguments, empty expansion
2874 *
2875 * This ambiguity was inherited from C.
2876 */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002877
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002878 if (!tok_is(t, '('))
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002879 goto finish;
2880
2881 if (tmpl) {
2882 Token *tx = t;
2883 Token **txpp = &tx;
2884 int sparam;
2885
2886 /* Count parameters first */
2887 sparam = parse_smacro_template(&txpp, NULL);
2888 if (!sparam)
2889 goto finish; /* No parameters, we're done */
2890 nasm_newn(params, sparam);
2891 }
2892
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002893 /* Skip leading paren */
2894 tn = &t->next;
2895 t = *tn;
2896
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002897 name = NULL;
2898 flags = 0;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002899 err = done = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002900
2901 while (!done) {
2902 if (!t || !t->type) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002903 if (name || flags)
2904 nasm_nonfatal("`)' expected to terminate macro template");
2905 else
2906 nasm_nonfatal("parameter identifier expected");
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002907 break;
2908 }
2909
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002910 switch (t->type) {
2911 case TOK_ID:
2912 if (name)
2913 goto bad;
2914 name = t;
2915 break;
2916
2917 case TOK_OTHER:
H. Peter Anvin8571f062019-09-23 16:40:03 -07002918 if (t->len != 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002919 goto bad;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002920 switch (t->text.a[0]) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002921 case '=':
2922 flags |= SPARM_EVAL;
2923 break;
2924 case '&':
2925 flags |= SPARM_STR;
2926 break;
2927 case '!':
2928 flags |= SPARM_NOSTRIP;
2929 break;
2930 case '+':
2931 flags |= SPARM_GREEDY;
2932 greedy = true;
2933 break;
2934 case ',':
2935 if (greedy)
2936 nasm_nonfatal("greedy parameter must be last");
2937 /* fall through */
2938 case ')':
2939 if (params) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002940 if (name)
2941 steal_Token(&params[nparam].name, name);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002942 params[nparam].flags = flags;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002943 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002944 nparam++;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002945 name = NULL;
2946 flags = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002947 done = t->text.a[0] == ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002948 break;
2949 default:
2950 goto bad;
2951 }
2952 break;
2953
2954 case TOK_WHITESPACE:
2955 break;
2956
2957 default:
2958 bad:
2959 if (!err) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002960 nasm_nonfatal("garbage `%s' in macro parameter list", tok_text(t));
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002961 err = true;
2962 }
2963 break;
2964 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002965
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002966 tn = &t->next;
2967 t = *tn;
2968 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002969
2970finish:
2971 while (t && t->type == TOK_WHITESPACE) {
2972 tn = &t->next;
2973 t = t->next;
2974 }
2975 *tpp = tn;
2976 if (tmpl) {
2977 tmpl->nparam = nparam;
2978 tmpl->greedy = greedy;
2979 tmpl->params = params;
2980 }
2981 return nparam;
2982}
2983
2984/*
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002985 * Common code for defining an smacro. The tmpl argument, if not NULL,
2986 * contains any macro parameters that aren't explicit arguments;
2987 * those are the more uncommon macro variants.
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002988 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002989static SMacro *define_smacro(const char *mname, bool casesense,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002990 Token *expansion, SMacro *tmpl)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002991{
2992 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002993 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002994 Context *ctx;
2995 bool defining_alias = false;
2996 unsigned int nparam = 0;
H. Peter Anvin70653092007-10-19 14:42:29 -07002997
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002998 if (tmpl) {
2999 defining_alias = tmpl->alias;
3000 nparam = tmpl->nparam;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003001 if (nparam && !defining_alias)
3002 mark_smac_params(expansion, tmpl, 0);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003003 }
3004
3005 while (1) {
3006 ctx = get_ctx(mname, &mname);
3007
H. Peter Anvind2354082019-08-27 16:38:48 -07003008 if (!smacro_defined(ctx, mname, nparam, &smac, casesense, true)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003009 /* Create a new macro */
3010 smtbl = ctx ? &ctx->localmac : &smacros;
3011 smhead = (SMacro **) hash_findi_add(smtbl, mname);
3012 nasm_new(smac);
3013 smac->next = *smhead;
3014 *smhead = smac;
3015 break;
3016 } else if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003017 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003018 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003019 /*
3020 * Some instances of the old code considered this a failure,
3021 * some others didn't. What is the right thing to do here?
3022 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003023 goto fail;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003024 } else if (!smac->alias || ppopt.noaliases || defining_alias) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003025 /*
3026 * We're redefining, so we have to take over an
3027 * existing SMacro structure. This means freeing
H. Peter Anvin8b262472019-02-26 14:00:54 -08003028 * what was already in it, but not the structure itself.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003029 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003030 clear_smacro(smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003031 break;
3032 } else if (smac->in_progress) {
3033 nasm_nonfatal("macro alias loop");
3034 goto fail;
3035 } else {
3036 /* It is an alias macro; follow the alias link */
3037 SMacro *s;
3038
3039 smac->in_progress = true;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003040 s = define_smacro(tok_text(smac->expansion), casesense,
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003041 expansion, tmpl);
3042 smac->in_progress = false;
3043 return s;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003044 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003045 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003046
3047 smac->name = nasm_strdup(mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003048 smac->casesense = casesense;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003049 smac->expansion = expansion;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003050 smac->expand = smacro_expand_default;
3051 if (tmpl) {
3052 smac->nparam = tmpl->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003053 smac->params = tmpl->params;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003054 smac->alias = tmpl->alias;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003055 smac->greedy = tmpl->greedy;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003056 if (tmpl->expand)
3057 smac->expand = tmpl->expand;
3058 }
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07003059 if (list_option('s')) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07003060 list_smacro_def((smac->alias ? PP_DEFALIAS : PP_DEFINE)
3061 + !casesense, ctx, smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003062 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08003063 return smac;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003064
3065fail:
3066 free_tlist(expansion);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003067 if (tmpl)
3068 free_smacro_members(tmpl);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003069 return NULL;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003070}
3071
3072/*
3073 * Undefine an smacro
3074 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003075static void undef_smacro(const char *mname, bool undefalias)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003076{
3077 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003078 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003079 Context *ctx;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003080
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003081 ctx = get_ctx(mname, &mname);
H. Peter Anvin166c2472008-05-28 12:28:58 -07003082 smtbl = ctx ? &ctx->localmac : &smacros;
3083 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07003084
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003085 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003086 /*
3087 * We now have a macro name... go hunt for it.
3088 */
3089 sp = smhead;
3090 while ((s = *sp) != NULL) {
3091 if (!mstrcmp(s->name, mname, s->casesense)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003092 if (s->alias && !undefalias) {
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003093 if (!ppopt.noaliases) {
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003094 if (s->in_progress) {
3095 nasm_nonfatal("macro alias loop");
3096 } else {
3097 s->in_progress = true;
3098 undef_smacro(tok_text(s->expansion), false);
3099 s->in_progress = false;
3100 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003101 }
3102 } else {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07003103 if (list_option('d'))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003104 list_smacro_def(s->alias ? PP_UNDEFALIAS : PP_UNDEF,
3105 ctx, s);
3106 *sp = s->next;
3107 free_smacro(s);
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003108 continue;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003109 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003110 }
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003111 sp = &s->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003112 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003113 }
3114}
3115
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003116/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07003117 * Parse a mmacro specification.
3118 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003119static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07003120{
H. Peter Anvina26433d2008-07-16 14:40:01 -07003121 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003122 tline = skip_white(tline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003123 tline = expand_id(tline);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003124 if (!tok_type(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003125 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003126 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003127 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02003128
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003129#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003130 def->prev = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003131#endif
H. Peter Anvin8571f062019-09-23 16:40:03 -07003132 def->name = dup_text(tline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003133 def->plus = false;
3134 def->nolist = false;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02003135 def->nparam_min = 0;
3136 def->nparam_max = 0;
3137
H. Peter Anvina26433d2008-07-16 14:40:01 -07003138 tline = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003139 tline = skip_white(tline);
3140 if (!tok_type(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003141 nasm_nonfatal("`%s' expects a parameter count", directive);
3142 else
H. Peter Anvin8571f062019-09-23 16:40:03 -07003143 def->nparam_min = def->nparam_max = read_param_count(tok_text(tline));
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003144 if (tline && tok_is(tline->next, '-')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003145 tline = tline->next->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003146 if (tok_is(tline, '*')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003147 def->nparam_max = INT_MAX;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003148 } else if (!tok_type(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003149 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003150 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003151 def->nparam_max = read_param_count(tok_text(tline));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003152 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003153 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03003154 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003155 }
3156 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003157 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003158 if (tline && tok_is(tline->next, '+')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003159 tline = tline->next;
3160 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003161 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003162 if (tline && tok_type(tline->next, TOK_ID) &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07003163 tline->next->len == 7 &&
3164 !nasm_stricmp(tline->next->text.a, ".nolist")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003165 tline = tline->next;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003166 def->nolist = !list_option('f') || istk->nolist;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003167 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003168
H. Peter Anvina26433d2008-07-16 14:40:01 -07003169 /*
3170 * Handle default parameters.
3171 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07003172 def->ndefs = 0;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003173 if (tline && tline->next) {
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07003174 Token **comma;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003175 def->dlist = tline->next;
3176 tline->next = NULL;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07003177 comma = count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
3178 if (!ppopt.sane_empty_expansion && comma) {
3179 *comma = NULL;
3180 def->ndefs--;
3181 nasm_warn(WARN_MACRO_PARAMS_LEGACY,
3182 "dropping trailing empty default parameter in defintion of multi-line macro `%s'",
3183 def->name);
3184 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003185 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003186 def->dlist = NULL;
3187 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003188 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003189 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003190
H. Peter Anvin89cee572009-07-15 09:16:54 -04003191 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003192 !def->plus) {
3193 /*
3194 *!macro-defaults [on] macros with more default than optional parameters
3195 *! warns when a macro has more default parameters than optional parameters.
3196 *! See \k{mlmacdef} for why might want to disable this warning.
3197 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003198 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003199 "too many default macro parameters in macro `%s'", def->name);
3200 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02003201
H. Peter Anvina26433d2008-07-16 14:40:01 -07003202 return true;
3203}
3204
3205
3206/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003207 * Decode a size directive
3208 */
3209static int parse_size(const char *str) {
3210 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003211 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003212 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003213 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03003214 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003215}
3216
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003217/*
3218 * Process a preprocessor %pragma directive. Currently there are none.
3219 * Gets passed the token list starting with the "preproc" token from
3220 * "%pragma preproc".
3221 */
3222static void do_pragma_preproc(Token *tline)
3223{
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003224 const char *txt;
3225
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003226 /* Skip to the real stuff */
3227 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003228 tline = skip_white(tline);
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003229
3230 if (!tok_type(tline, TOK_ID))
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003231 return;
3232
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003233 txt = tok_text(tline);
3234 if (!nasm_stricmp(txt, "sane_empty_expansion")) {
3235 tline = skip_white(tline->next);
3236 ppopt.sane_empty_expansion =
3237 pp_get_boolean_option(tline, ppopt.sane_empty_expansion);
3238 } else {
3239 /* Unknown pragma, ignore for now */
3240 }
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003241}
3242
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003243static bool is_macro_id(const Token *t)
3244{
H. Peter Anvin8571f062019-09-23 16:40:03 -07003245 return tok_type(t, TOK_ID) || tok_type(t, TOK_LOCAL_MACRO);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003246}
3247
H. Peter Anvin8571f062019-09-23 16:40:03 -07003248static const char *get_id(Token **tp, const char *dname)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003249{
H. Peter Anvin8571f062019-09-23 16:40:03 -07003250 const char *id;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003251 Token *t = *tp;
3252
3253 t = t->next; /* Skip directive */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003254 t = skip_white(t);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003255 t = expand_id(t);
3256
3257 if (!is_macro_id(t)) {
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003258 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003259 return NULL;
3260 }
3261
H. Peter Anvin8571f062019-09-23 16:40:03 -07003262 id = tok_text(t);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003263 t = skip_white(t);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003264 *tp = t;
3265 return id;
3266}
3267
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003268/* Parse a %use package name and find the package. Set *err on syntax error. */
3269static const struct use_package *
H. Peter Anvin8571f062019-09-23 16:40:03 -07003270get_use_pkg(Token *t, const char *dname, const char **name)
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003271{
H. Peter Anvin8571f062019-09-23 16:40:03 -07003272 const char *id;
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003273
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003274 t = skip_white(t);
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003275 t = expand_smacro(t);
3276
H. Peter Anvin8571f062019-09-23 16:40:03 -07003277 *name = NULL;
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003278
H. Peter Anvin8571f062019-09-23 16:40:03 -07003279 if (!t) {
3280 nasm_nonfatal("`%s' expects a package name, got end of line", dname);
3281 return NULL;
3282 } else if (t->type != TOK_ID && t->type != TOK_STRING) {
3283 nasm_nonfatal("`%s' expects a package name, got `%s'",
3284 dname, tok_text(t));
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003285 return NULL;
3286 }
3287
H. Peter Anvin8571f062019-09-23 16:40:03 -07003288 *name = id = unquote_token(t);
3289
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003290 t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003291 t = skip_white(t);
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003292 if (t)
3293 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
3294
3295 return nasm_find_use_package(id);
3296}
3297
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003298/*
3299 * Mark parameter tokens in an smacro definition. If the type argument
3300 * is 0, create smac param tokens, otherwise use the type specified;
3301 * normally this is used for TOK_XDEF_PARAM, which is used to protect
3302 * parameter tokens during expansion during %xdefine.
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003303 *
3304 * tmpl may not be NULL here.
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003305 */
3306static void mark_smac_params(Token *tline, const SMacro *tmpl,
3307 enum pp_token_type type)
3308{
3309 const struct smac_param *params = tmpl->params;
3310 int nparam = tmpl->nparam;
3311 Token *t;
3312 int i;
3313
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003314 list_for_each(t, tline) {
3315 if (t->type != TOK_ID && t->type != TOK_XDEF_PARAM)
3316 continue;
3317
3318 for (i = 0; i < nparam; i++) {
3319 if (tok_text_match(t, &params[i].name))
3320 t->type = type ? type : tok_smac_param(i);
3321 }
3322 }
3323}
3324
Ed Beroset3ab3f412002-06-11 03:31:49 +00003325/**
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003326 * %clear selected macro sets either globally or in contexts
3327 */
3328static void do_clear(enum clear_what what, bool context)
3329{
3330 if (context) {
3331 if (what & CLEAR_ALLDEFINE) {
3332 Context *ctx;
3333 list_for_each(ctx, cstk)
3334 clear_smacro_table(&ctx->localmac, what);
3335 }
3336 /* Nothing else can be context-local */
3337 } else {
3338 if (what & CLEAR_ALLDEFINE)
3339 clear_smacro_table(&smacros, what);
3340 if (what & CLEAR_MMACRO)
3341 free_mmacro_table(&mmacros);
3342 }
3343}
3344
3345/**
Ed Beroset3ab3f412002-06-11 03:31:49 +00003346 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003347 * Find out if a line contains a preprocessor directive, and deal
3348 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07003349 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00003350 * If a directive _is_ found, it is the responsibility of this routine
3351 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003352 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00003353 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003354 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00003355 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07003356 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003357 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07003358static int do_directive(Token *tline, Token **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003359{
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003360 enum preproc_token op;
H. Peter Anvin4169a472007-09-12 01:29:43 +00003361 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07003362 bool err;
H. Peter Anvin70055962007-10-11 00:05:31 -07003363 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003364 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07003365 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003366 int offset;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003367 const char *p;
3368 char *q, *qbuf;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003369 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003370 const char *mname;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003371 struct ppscan pps;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003372 Include *inc;
3373 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003374 Cond *cond;
3375 MMacro *mmac, **mmhead;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003376 Token *t = NULL, *tt, *macro_start, *last, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003377 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003378 struct tokenval tokval;
3379 expr *evalresult;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003380 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003381 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08003382 errflags severity;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003383 const char *dname; /* Name of directive, for messages */
H. Peter Anvin76690a12002-04-30 20:52:49 +00003384
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003385 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00003386 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003387
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003388 tline = skip_white(tline);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003389 if (!tline || !tok_type(tline, TOK_PREPROC_ID))
3390 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003391
H. Peter Anvin8571f062019-09-23 16:40:03 -07003392 dname = tok_text(tline);
3393 if (dname[1] == '%')
3394 return NO_DIRECTIVE_FOUND;
3395
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003396 op = pp_token_hash(dname);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003397
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003398 casesense = true;
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003399 if (PP_HAS_CASE(op) & PP_INSENSITIVE(op)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003400 casesense = false;
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003401 op--;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003402 }
3403
3404 /*
3405 * If we're in a non-emitting branch of a condition construct,
3406 * or walking to the end of an already terminated %rep block,
3407 * we should ignore all directives except for condition
3408 * directives.
3409 */
3410 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003411 (istk->mstk.mstk && !istk->mstk.mstk->in_progress)) &&
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003412 !is_condition(op)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003413 return NO_DIRECTIVE_FOUND;
3414 }
3415
3416 /*
3417 * If we're defining a macro or reading a %rep block, we should
3418 * ignore all directives except for %macro/%imacro (which nest),
3419 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
3420 * If we're in a %rep block, another %rep nests, so should be let through.
3421 */
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003422 if (defining && op != PP_MACRO && op != PP_RMACRO &&
3423 op != PP_ENDMACRO && op != PP_ENDM &&
3424 (defining->name || (op != PP_ENDREP && op != PP_REP))) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003425 return NO_DIRECTIVE_FOUND;
3426 }
3427
3428 if (defining) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003429 if (op == PP_MACRO || op == PP_RMACRO) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003430 nested_mac_count++;
3431 return NO_DIRECTIVE_FOUND;
3432 } else if (nested_mac_count > 0) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003433 if (op == PP_ENDMACRO) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003434 nested_mac_count--;
3435 return NO_DIRECTIVE_FOUND;
3436 }
3437 }
3438 if (!defining->name) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003439 if (op == PP_REP) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003440 nested_rep_count++;
3441 return NO_DIRECTIVE_FOUND;
3442 } else if (nested_rep_count > 0) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003443 if (op == PP_ENDREP) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003444 nested_rep_count--;
3445 return NO_DIRECTIVE_FOUND;
3446 }
3447 }
3448 }
3449 }
3450
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003451 switch (op) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003452 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003453 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003454 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003455
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003456 case PP_PRAGMA:
3457 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003458 * %pragma namespace options...
3459 *
3460 * The namespace "preproc" is reserved for the preprocessor;
3461 * all other namespaces generate a [pragma] assembly directive.
3462 *
3463 * Invalid %pragmas are ignored and may have different
3464 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003465 */
H. Peter Anvinf5d7d902019-08-10 06:21:00 -07003466 t = tline;
3467 tline = tline->next;
3468 t->next = NULL;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003469 tline = zap_white(expand_smacro(tline));
3470 if (tok_type(tline, TOK_ID)) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003471 if (!nasm_stricmp(tok_text(tline), "preproc")) {
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003472 /* Preprocessor pragma */
3473 do_pragma_preproc(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07003474 free_tlist(tline);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003475 } else {
3476 /* Build the assembler directive */
H. Peter Anvin06335872019-08-10 06:42:55 -07003477
3478 /* Append bracket to the end of the output */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003479 for (t = tline; t->next; t = t->next)
3480 ;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003481 t->next = make_tok_char(NULL, ']');
H. Peter Anvin06335872019-08-10 06:42:55 -07003482
3483 /* Prepend "[pragma " */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003484 t = new_White(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07003485 t = new_Token(t, TOK_ID, "pragma", 6);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003486 t = make_tok_char(t, '[');
H. Peter Anvin06335872019-08-10 06:42:55 -07003487 tline = t;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07003488 *output = tline;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003489 }
3490 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003491 break;
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003492
H. Peter Anvine2c80182005-01-15 22:15:51 +00003493 case PP_STACKSIZE:
3494 /* Directive to tell NASM what the default stack size is. The
3495 * default is for a 16-bit stack, and this can be overriden with
3496 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003497 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003498 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003499 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003500 nasm_nonfatal("`%s' missing size parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003501 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07003502 if (nasm_stricmp(tok_text(tline), "flat") == 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003503 /* All subsequent ARG directives are for a 32-bit stack */
3504 StackSize = 4;
3505 StackPointer = "ebp";
3506 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003507 LocalOffset = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003508 } else if (nasm_stricmp(tok_text(tline), "flat64") == 0) {
Charles Crayne7eaf9192007-11-08 22:11:14 -08003509 /* All subsequent ARG directives are for a 64-bit stack */
3510 StackSize = 8;
3511 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03003512 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08003513 LocalOffset = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003514 } else if (nasm_stricmp(tok_text(tline), "large") == 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003515 /* All subsequent ARG directives are for a 16-bit stack,
3516 * far function call.
3517 */
3518 StackSize = 2;
3519 StackPointer = "bp";
3520 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003521 LocalOffset = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003522 } else if (nasm_stricmp(tok_text(tline), "small") == 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003523 /* All subsequent ARG directives are for a 16-bit stack,
3524 * far function call. We don't support near functions.
3525 */
3526 StackSize = 2;
3527 StackPointer = "bp";
3528 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003529 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003530 } else {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003531 nasm_nonfatal("`%s' invalid size type", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003532 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003533 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003534
H. Peter Anvine2c80182005-01-15 22:15:51 +00003535 case PP_ARG:
3536 /* TASM like ARG directive to define arguments to functions, in
3537 * the following form:
3538 *
3539 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
3540 */
3541 offset = ArgOffset;
3542 do {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003543 const char *arg;
3544 char directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003545 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003546
H. Peter Anvine2c80182005-01-15 22:15:51 +00003547 /* Find the argument name */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003548 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003549 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003550 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003551 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003552 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07003553 arg = tok_text(tline);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003554
H. Peter Anvine2c80182005-01-15 22:15:51 +00003555 /* Find the argument size type */
3556 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003557 if (!tok_is(tline, ':')) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003558 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003559 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003560 }
3561 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003562 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003563 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003564 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003565 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003566
H. Peter Anvine2c80182005-01-15 22:15:51 +00003567 /* Allow macro expansion of type parameter */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003568 tt = tokenize(tok_text(tline));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003569 tt = expand_smacro(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003570 size = parse_size(tok_text(tt));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003571 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003572 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003573 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003574 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003575 }
3576 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003577
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003578 /* Round up to even stack slots */
3579 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003580
H. Peter Anvine2c80182005-01-15 22:15:51 +00003581 /* Now define the macro for the argument */
3582 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
3583 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003584 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003585 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003586
H. Peter Anvine2c80182005-01-15 22:15:51 +00003587 /* Move to the next argument in the list */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003588 tline = skip_white(tline->next);
3589 } while (tok_is(tline, ','));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003590 ArgOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003591 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003592
H. Peter Anvine2c80182005-01-15 22:15:51 +00003593 case PP_LOCAL:
3594 /* TASM like LOCAL directive to define local variables for a
3595 * function, in the following form:
3596 *
3597 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
3598 *
3599 * The '= LocalSize' at the end is ignored by NASM, but is
3600 * required by TASM to define the local parameter size (and used
3601 * by the TASM macro package).
3602 */
3603 offset = LocalOffset;
3604 do {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003605 const char *local;
3606 char directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003607 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003608
H. Peter Anvine2c80182005-01-15 22:15:51 +00003609 /* Find the argument name */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003610 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003611 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003612 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003613 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003614 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07003615 local = tok_text(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003616
H. Peter Anvine2c80182005-01-15 22:15:51 +00003617 /* Find the argument size type */
3618 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003619 if (!tok_is(tline, ':')) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003620 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003621 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003622 }
3623 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003624 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003625 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003626 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003627 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003628
H. Peter Anvine2c80182005-01-15 22:15:51 +00003629 /* Allow macro expansion of type parameter */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003630 tt = tokenize(tok_text(tline));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003631 tt = expand_smacro(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003632 size = parse_size(tok_text(tt));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003633 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003634 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003635 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003636 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003637 }
3638 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003639
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003640 /* Round up to even stack slots */
3641 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003642
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003643 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003644
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003645 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003646 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
3647 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003648 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003649
H. Peter Anvine2c80182005-01-15 22:15:51 +00003650 /* Now define the assign to setup the enter_c macro correctly */
3651 snprintf(directive, sizeof(directive),
3652 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003653 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003654
H. Peter Anvine2c80182005-01-15 22:15:51 +00003655 /* Move to the next argument in the list */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003656 tline = skip_white(tline->next);
3657 } while (tok_is(tline, ','));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003658 LocalOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003659 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003660
H. Peter Anvine2c80182005-01-15 22:15:51 +00003661 case PP_CLEAR:
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003662 {
3663 bool context = false;
3664
3665 t = tline->next = expand_smacro(tline->next);
3666 t = skip_white(t);
3667 if (!t) {
3668 /* Emulate legacy behavior */
3669 do_clear(CLEAR_DEFINE|CLEAR_MMACRO, false);
3670 } else {
3671 while ((t = skip_white(t)) && t->type == TOK_ID) {
3672 const char *txt = tok_text(t);
3673 if (!nasm_stricmp(txt, "all")) {
3674 do_clear(CLEAR_ALL, context);
3675 } else if (!nasm_stricmp(txt, "define") ||
3676 !nasm_stricmp(txt, "def") ||
3677 !nasm_stricmp(txt, "smacro")) {
3678 do_clear(CLEAR_DEFINE, context);
3679 } else if (!nasm_stricmp(txt, "defalias") ||
3680 !nasm_stricmp(txt, "alias") ||
3681 !nasm_stricmp(txt, "salias")) {
3682 do_clear(CLEAR_DEFALIAS, context);
3683 } else if (!nasm_stricmp(txt, "alldef") ||
3684 !nasm_stricmp(txt, "alldefine")) {
3685 do_clear(CLEAR_ALLDEFINE, context);
3686 } else if (!nasm_stricmp(txt, "macro") ||
3687 !nasm_stricmp(txt, "mmacro")) {
3688 do_clear(CLEAR_MMACRO, context);
3689 } else if (!nasm_stricmp(txt, "context") ||
3690 !nasm_stricmp(txt, "ctx")) {
3691 context = true;
3692 } else if (!nasm_stricmp(txt, "global")) {
3693 context = false;
3694 } else if (!nasm_stricmp(txt, "nothing") ||
3695 !nasm_stricmp(txt, "none") ||
3696 !nasm_stricmp(txt, "ignore") ||
3697 !nasm_stricmp(txt, "-") ||
3698 !nasm_stricmp(txt, "--")) {
3699 /* Do nothing */
3700 } else {
3701 nasm_nonfatal("invalid option to %s: %s", dname, txt);
3702 t = NULL;
3703 }
3704 }
3705 }
3706
3707 t = skip_white(t);
3708 if (t)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003709 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003710 break;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003711 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003712
H. Peter Anvin418ca702008-05-30 10:42:30 -07003713 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003714 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003715 t = skip_white(t);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003716 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003717 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003718 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003719 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003720 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003721 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003722 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003723
3724 strlist_add(deplist, unquote_token_cstr(t));
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003725 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003726
3727 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003728 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003729 t = skip_white(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07003730
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003731 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003732 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003733 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003734 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003735 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003736 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003737 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003738 p = unquote_token_cstr(t);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003739 nasm_new(inc);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003740 inc->next = istk;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003741 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07003742 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08003743 (pp_mode == PP_DEPS)
3744 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003745 if (!inc->fp) {
3746 /* -MG given but file not found */
3747 nasm_free(inc);
3748 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003749 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003750 inc->lineno = src_set_linnum(0);
3751 inc->lineinc = 1;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003752 inc->nolist = istk->nolist;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003753 istk = inc;
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003754 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003755 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003756 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003757
H. Peter Anvind2456592008-06-19 15:04:18 -07003758 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003759 {
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003760 const struct use_package *pkg;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003761 const char *name;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003762
H. Peter Anvin8571f062019-09-23 16:40:03 -07003763 pkg = get_use_pkg(tline->next, dname, &name);
3764 if (!name)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003765 goto done;
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003766 if (!pkg) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003767 nasm_nonfatal("unknown `%s' package: `%s'", dname, name);
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003768 } else if (!use_loaded[pkg->index]) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003769 /*
3770 * Not already included, go ahead and include it.
3771 * Treat it as an include file for the purpose of
3772 * producing a listing.
3773 */
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003774 use_loaded[pkg->index] = true;
3775 stdmacpos = pkg->macros;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003776 nasm_new(inc);
3777 inc->next = istk;
3778 inc->fname = src_set_fname(NULL);
3779 inc->lineno = src_set_linnum(0);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003780 inc->nolist = !list_option('b') || istk->nolist;
3781 istk = inc;
3782 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003783 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003784 break;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003785 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003786 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003787 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07003788 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003789 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003790 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003791 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003792 if (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003793 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003794 nasm_nonfatal("`%s' expects a context identifier", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003795 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003796 }
3797 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003798 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003799 dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003800 p = tok_text(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003801 } else {
3802 p = NULL; /* Anonymous */
3803 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07003804
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003805 if (op == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08003806 nasm_new(ctx);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003807 ctx->depth = cstk ? cstk->depth + 1 : 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003808 ctx->next = cstk;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003809 ctx->name = p ? nasm_strdup(p) : NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003810 ctx->number = unique++;
3811 cstk = ctx;
3812 } else {
3813 /* %pop or %repl */
3814 if (!cstk) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003815 nasm_nonfatal("`%s': context stack is empty", dname);
3816 } else if (op == PP_POP) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003817 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003818 nasm_nonfatal("`%s' in wrong context: %s, "
H. Peter Anvin8b262472019-02-26 14:00:54 -08003819 "expected %s",
3820 dname, cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003821 else
3822 ctx_pop();
3823 } else {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003824 /* op == PP_REPL */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003825 nasm_free((char *)cstk->name);
3826 cstk->name = p ? nasm_strdup(p) : NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003827 p = NULL;
3828 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003829 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003830 break;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003831 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003832 severity = ERR_FATAL;
3833 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003834 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003835 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003836 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003837 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003838 /*!
3839 *!user [on] %warning directives
3840 *! controls output of \c{%warning} directives (see \k{pperror}).
3841 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003842 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003843 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003844
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003845issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07003846 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003847 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003848 tline->next = expand_smacro(tline->next);
3849 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003850 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003851 t = tline ? tline->next : NULL;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003852 t = skip_white(t);
3853 if (tok_type(tline, TOK_STRING) && !t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003854 /* The line contains only a quoted string */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003855 p = unquote_token(tline); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08003856 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003857 } else {
3858 /* Not a quoted string, or more than a quoted string */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003859 q = detoken(tline, false);
3860 nasm_error(severity, "%s", q);
3861 nasm_free(q);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003862 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003863 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003864 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003865
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003866 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003867 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003868 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003869 else {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003870 j = if_condition(tline->next, op);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003871 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003872 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003873 cond = nasm_malloc(sizeof(Cond));
3874 cond->next = istk->conds;
3875 cond->state = j;
3876 istk->conds = cond;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003877 if(istk->mstk.mstk)
3878 istk->mstk.mstk->condcnt++;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003879 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003880
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003881 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003882 if (!istk->conds)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003883 nasm_fatal("`%s': no matching `%%if'", dname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003884 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003885 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003886 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003887 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003888
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003889 case COND_DONE:
3890 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003891 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003892
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003893 case COND_ELSE_TRUE:
3894 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003895 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003896 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003897 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003898 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003899
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003900 case COND_IF_FALSE:
3901 /*
3902 * IMPORTANT: In the case of %if, we will already have
3903 * called expand_mmac_params(); however, if we're
3904 * processing an %elif we must have been in a
3905 * non-emitting mode, which would have inhibited
3906 * the normal invocation of expand_mmac_params().
3907 * Therefore, we have to do it explicitly here.
3908 */
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003909 j = if_condition(expand_mmac_params(tline->next), op);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003910 tline->next = NULL; /* it got freed */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003911 istk->conds->state = j;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003912 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003913 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003914 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003915
H. Peter Anvine2c80182005-01-15 22:15:51 +00003916 case PP_ELSE:
3917 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003918 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003919 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003920 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07003921 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003922 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003923 case COND_IF_TRUE:
3924 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003925 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003926 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003927
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003928 case COND_NEVER:
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_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003932 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003933 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003934
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003935 case COND_ELSE_TRUE:
3936 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003937 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003938 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003939 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003940 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003941 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003942 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003943
H. Peter Anvine2c80182005-01-15 22:15:51 +00003944 case PP_ENDIF:
3945 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003946 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003947 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003948 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003949 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003950 cond = istk->conds;
3951 istk->conds = cond->next;
3952 nasm_free(cond);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003953 if(istk->mstk.mstk)
3954 istk->mstk.mstk->condcnt--;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003955 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003956
H. Peter Anvin8b262472019-02-26 14:00:54 -08003957 case PP_RMACRO:
3958 case PP_MACRO:
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07003959 {
3960 MMacro *def;
3961
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003962 nasm_assert(!defining);
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07003963 nasm_new(def);
3964 def->casesense = casesense;
3965 def->dstk.mmac = defining;
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003966 if (op == PP_RMACRO)
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07003967 def->max_depth = nasm_limit[LIMIT_MACRO_LEVELS];
3968 if (!parse_mmacro_spec(tline, def, dname)) {
3969 nasm_free(def);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003970 goto done;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003971 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003972
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07003973 defining = def;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07003974 src_get(&defining->xline, &defining->fname);
3975
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003976 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
3977 while (mmac) {
3978 if (!strcmp(mmac->name, defining->name) &&
3979 (mmac->nparam_min <= defining->nparam_max
3980 || defining->plus)
3981 && (defining->nparam_min <= mmac->nparam_max
3982 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003983 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003984 defining->name);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003985 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003986 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003987 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003988 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003989 break;
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07003990 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003991
H. Peter Anvine2c80182005-01-15 22:15:51 +00003992 case PP_ENDM:
3993 case PP_ENDMACRO:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003994 if (!(defining && defining->name)) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003995 nasm_nonfatal("`%s': not defining a macro", tok_text(tline));
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003996 goto done;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003997 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003998 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
3999 defining->next = *mmhead;
4000 *mmhead = defining;
4001 defining = NULL;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004002 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004003
H. Peter Anvin89cee572009-07-15 09:16:54 -04004004 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004005 /*
4006 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004007 * macro-end marker for a macro with a name. Then we
4008 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004009 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004010 list_for_each(l, istk->expansion)
4011 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004012 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004013
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004014 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004015 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004016 * Remove all conditional entries relative to this
4017 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004018 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004019 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
4020 cond = istk->conds;
4021 istk->conds = cond->next;
4022 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004023 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004024 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004025 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004026 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004027 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004028 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05004029
H. Peter Anvina26433d2008-07-16 14:40:01 -07004030 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08004031 casesense = false;
4032 /* fall through */
4033 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07004034 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004035 MMacro **mmac_p;
4036 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07004037
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004038 nasm_zero(spec);
H. Peter Anvin8b262472019-02-26 14:00:54 -08004039 spec.casesense = casesense;
4040 if (!parse_mmacro_spec(tline, &spec, dname)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004041 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004042 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004043 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
4044 while (mmac_p && *mmac_p) {
4045 mmac = *mmac_p;
4046 if (mmac->casesense == spec.casesense &&
4047 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
4048 mmac->nparam_min == spec.nparam_min &&
4049 mmac->nparam_max == spec.nparam_max &&
4050 mmac->plus == spec.plus) {
4051 *mmac_p = mmac->next;
4052 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004053 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004054 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004055 }
4056 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004057 free_tlist(spec.dlist);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004058 break;
H. Peter Anvina26433d2008-07-16 14:40:01 -07004059 }
4060
H. Peter Anvine2c80182005-01-15 22:15:51 +00004061 case PP_ROTATE:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004062 while (tok_white(tline->next))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004063 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04004064 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004065 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004066 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00004067 return DIRECTIVE_FOUND;
4068 }
4069 t = expand_smacro(tline->next);
4070 tline->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004071 pps.tptr = tline = t;
4072 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004073 tokval.t_type = TOKEN_INVALID;
4074 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004075 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004076 free_tlist(tline);
4077 if (!evalresult)
4078 return DIRECTIVE_FOUND;
4079 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004080 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00004081 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004082 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00004083 return DIRECTIVE_FOUND;
4084 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004085 mmac = istk->mstk.mmac;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004086 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004087 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004088 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004089 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004090 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004091 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004092
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004093 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004094 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004095 rotate += mmac->nparam;
4096
4097 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004098 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004099 break;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004100
H. Peter Anvine2c80182005-01-15 22:15:51 +00004101 case PP_REP:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004102 {
4103 MMacro *tmp_defining;
4104
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004105 nolist = false;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004106 tline = skip_white(tline->next);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004107 if (tok_type(tline, TOK_ID) && tline->len == 7 &&
4108 !nasm_memicmp(tline->text.a, ".nolist", 7)) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07004109 nolist = !list_option('f') || istk->nolist;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004110 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004111 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004112
H. Peter Anvine2c80182005-01-15 22:15:51 +00004113 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08004114 pps.tptr = expand_smacro(tline);
4115 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004116 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004117 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004118 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004119 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004120 if (!evalresult)
4121 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004122 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004123 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00004124 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004125 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004126 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004127 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04004128 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07004129 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004130 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
4131 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04004132 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07004133 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08004134 /*!
4135 *!negative-rep [on] regative %rep count
4136 *! warns about negative counts given to the \c{%rep}
4137 *! preprocessor directive.
4138 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08004139 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07004140 "negative `%%rep' count: %"PRId64, count);
4141 count = 0;
4142 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04004143 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07004144 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004145 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004146 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07004147 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004148 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004149 tmp_defining = defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07004150 nasm_new(defining);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004151 defining->nolist = nolist;
4152 defining->in_progress = count;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004153 defining->mstk = istk->mstk;
4154 defining->dstk.mstk = tmp_defining;
4155 defining->dstk.mmac = tmp_defining ? tmp_defining->dstk.mmac : NULL;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07004156 src_get(&defining->xline, &defining->fname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004157 break;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004158 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004159
H. Peter Anvine2c80182005-01-15 22:15:51 +00004160 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004161 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004162 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004163 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004164 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004165
H. Peter Anvine2c80182005-01-15 22:15:51 +00004166 /*
4167 * Now we have a "macro" defined - although it has no name
4168 * and we won't be entering it in the hash tables - we must
4169 * push a macro-end marker for it on to istk->expansion.
4170 * After that, it will take care of propagating itself (a
4171 * macro-end marker line for a macro which is really a %rep
4172 * block will cause the macro to be re-expanded, complete
4173 * with another macro-end marker to ensure the process
4174 * continues) until the whole expansion is forcibly removed
4175 * from istk->expansion by a %exitrep.
4176 */
H. Peter Anvin6686de22019-08-10 05:33:14 -07004177 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004178 l->next = istk->expansion;
4179 l->finishes = defining;
4180 l->first = NULL;
4181 istk->expansion = l;
4182
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004183 istk->mstk.mstk = defining;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004184
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07004185 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004186 defining = defining->dstk.mstk;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004187 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004188
H. Peter Anvine2c80182005-01-15 22:15:51 +00004189 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004190 /*
4191 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004192 * macro-end marker for a macro with no name. Then we set
4193 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004194 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004195 list_for_each(l, istk->expansion)
4196 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004197 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004198
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004199 if (l)
H. Peter Anvind983b622019-10-07 21:19:32 -07004200 l->finishes->in_progress = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004201 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004202 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004203 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004204
H. Peter Anvin8b262472019-02-26 14:00:54 -08004205 case PP_DEFINE:
4206 case PP_XDEFINE:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004207 case PP_DEFALIAS:
H. Peter Anvin8b262472019-02-26 14:00:54 -08004208 {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004209 SMacro tmpl;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004210 Token **lastp;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07004211 int nparam;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07004212
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004213 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004214 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004215
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004216 nasm_zero(tmpl);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004217 lastp = &tline->next;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07004218 nparam = parse_smacro_template(&lastp, &tmpl);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004219 tline = *lastp;
4220 *lastp = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004221
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004222 if (unlikely(op == PP_DEFALIAS)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004223 macro_start = tline;
4224 if (!is_macro_id(macro_start)) {
4225 nasm_nonfatal("`%s' expects a macro identifier to alias",
4226 dname);
4227 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004228 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004229 tt = macro_start->next;
4230 macro_start->next = NULL;
4231 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004232 tline = skip_white(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004233 if (tline && tline->type) {
4234 nasm_warn(WARN_OTHER,
4235 "trailing garbage after aliasing identifier ignored");
4236 }
4237 free_tlist(tt);
4238 tmpl.alias = true;
4239 } else {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004240 if (op == PP_XDEFINE) {
4241 /* Protect macro parameter tokens */
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07004242 if (nparam)
4243 mark_smac_params(tline, &tmpl, TOK_XDEF_PARAM);
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004244 tline = expand_smacro(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004245 }
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004246 /* NB: Does this still make sense? */
4247 macro_start = reverse_tokens(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004248 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004249
H. Peter Anvine2c80182005-01-15 22:15:51 +00004250 /*
4251 * Good. We now have a macro name, a parameter count, and a
4252 * token list (in reverse order) for an expansion. We ought
4253 * to be OK just to create an SMacro, store it, and let
4254 * free_tlist have the rest of the line (which we have
4255 * carefully re-terminated after chopping off the expansion
4256 * from the end).
4257 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004258 define_smacro(mname, casesense, macro_start, &tmpl);
4259 break;
4260 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004261
H. Peter Anvine2c80182005-01-15 22:15:51 +00004262 case PP_UNDEF:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004263 case PP_UNDEFALIAS:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004264 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004265 goto done;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004266 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004267 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004268
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004269 undef_smacro(mname, op == PP_UNDEFALIAS);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004270 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004271
H. Peter Anvin8b262472019-02-26 14:00:54 -08004272 case PP_DEFSTR:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004273 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004274 goto done;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004275
H. Peter Anvin9e200162008-06-04 17:23:14 -07004276 last = tline;
4277 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004278 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004279
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004280 tline = zap_white(tline);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004281 q = detoken(tline, false);
4282 macro_start = make_tok_qstr(NULL, q);
4283 nasm_free(q);
H. Peter Anvin9e200162008-06-04 17:23:14 -07004284
4285 /*
4286 * We now have a macro name, an implicit parameter count of
4287 * zero, and a string token to use as an expansion. Create
4288 * and store an SMacro.
4289 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004290 define_smacro(mname, casesense, macro_start, NULL);
4291 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004292
H. Peter Anvin8b262472019-02-26 14:00:54 -08004293 case PP_DEFTOK:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004294 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004295 goto done;
4296
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004297 last = tline;
4298 tline = expand_smacro(tline->next);
4299 last->next = NULL;
4300
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004301 t = skip_white(tline);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004302 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004303 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004304 nasm_nonfatal("`%s' requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004305 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004306 goto done;
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004307 }
4308
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04004309 /*
4310 * Convert the string to a token stream. Note that smacros
4311 * are stored with the token stream reversed, so we have to
4312 * reverse the output of tokenize().
4313 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07004314 macro_start = reverse_tokens(tokenize(unquote_token_cstr(t)));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004315
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004316 /*
4317 * We now have a macro name, an implicit parameter count of
4318 * zero, and a numeric token to use as an expansion. Create
4319 * and store an SMacro.
4320 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004321 define_smacro(mname, casesense, macro_start, NULL);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004322 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004323 break;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004324
H. Peter Anvin418ca702008-05-30 10:42:30 -07004325 case PP_PATHSEARCH:
4326 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07004327 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004328
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004329 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004330 goto done;
4331
H. Peter Anvin418ca702008-05-30 10:42:30 -07004332 last = tline;
4333 tline = expand_smacro(tline->next);
4334 last->next = NULL;
4335
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004336 t = skip_white(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004337 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004338 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004339 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004340 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004341 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004342 }
4343 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004344 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004345
4346 p = unquote_token_cstr(t);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004347
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07004348 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07004349 if (!found_path)
4350 found_path = p;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004351 macro_start = make_tok_qstr(NULL, found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004352
4353 /*
4354 * We now have a macro name, an implicit parameter count of
4355 * zero, and a string token to use as an expansion. Create
4356 * and store an SMacro.
4357 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004358 define_smacro(mname, casesense, macro_start, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004359 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004360 break;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004361 }
4362
H. Peter Anvine2c80182005-01-15 22:15:51 +00004363 case PP_STRLEN:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004364 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004365 goto done;
4366
H. Peter Anvine2c80182005-01-15 22:15:51 +00004367 last = tline;
4368 tline = expand_smacro(tline->next);
4369 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004370
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004371 t = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004372 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004373 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004374 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004375 free_tlist(tline);
4376 free_tlist(origline);
4377 return DIRECTIVE_FOUND;
4378 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004379
H. Peter Anvin8571f062019-09-23 16:40:03 -07004380 unquote_token(t);
4381 macro_start = make_tok_num(NULL, t->len);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004382
H. Peter Anvine2c80182005-01-15 22:15:51 +00004383 /*
4384 * We now have a macro name, an implicit parameter count of
4385 * zero, and a numeric token to use as an expansion. Create
4386 * and store an SMacro.
4387 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004388 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004389 free_tlist(tline);
4390 free_tlist(origline);
4391 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004392
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004393 case PP_STRCAT:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004394 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004395 goto done;
4396
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004397 last = tline;
4398 tline = expand_smacro(tline->next);
4399 last->next = NULL;
4400
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004401 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004402 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004403 switch (t->type) {
4404 case TOK_WHITESPACE:
4405 break;
4406 case TOK_STRING:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004407 unquote_token(t);
4408 len += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004409 break;
4410 case TOK_OTHER:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004411 if (tok_is(t, ',')) /* permit comma separators */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004412 break;
4413 /* else fall through */
4414 default:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004415 nasm_nonfatal("non-string passed to `%s': %s", dname,
4416 tok_text(t));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004417 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004418 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004419 }
4420 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004421
H. Peter Anvin (Intel)f770ce82019-10-17 18:22:43 -07004422 q = qbuf = nasm_malloc(len+1);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004423 list_for_each(t, tline) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004424 if (t->type == TOK_INTERNAL_STRING)
4425 q = mempcpy(q, tok_text(t), t->len);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004426 }
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07004427 *q = '\0';
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004428
4429 /*
4430 * We now have a macro name, an implicit parameter count of
4431 * zero, and a numeric token to use as an expansion. Create
4432 * and store an SMacro.
4433 */
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07004434 macro_start = make_tok_qstr_len(NULL, qbuf, len);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004435 nasm_free(qbuf);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004436 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004437 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004438 break;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004439
H. Peter Anvine2c80182005-01-15 22:15:51 +00004440 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004441 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004442 int64_t start, count;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004443 const char *txt;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004444 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07004445
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004446 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004447 goto done;
4448
H. Peter Anvine2c80182005-01-15 22:15:51 +00004449 last = tline;
4450 tline = expand_smacro(tline->next);
4451 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004452
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04004453 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004454 t = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004455
4456 t = skip_white(t);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004457
H. Peter Anvine2c80182005-01-15 22:15:51 +00004458 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004459 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004460 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004461 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004462 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004463 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004464
H. Peter Anvin8b262472019-02-26 14:00:54 -08004465 pps.tptr = t->next;
4466 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004467 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004468 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004469 if (!evalresult) {
4470 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004471 goto done;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004472 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004473 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004474 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004475 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004476 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004477 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004478
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004479 pps.tptr = skip_white(pps.tptr);
H. Peter Anvin8b262472019-02-26 14:00:54 -08004480 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004481 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004482 } else {
4483 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004484 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004485 if (!evalresult) {
4486 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004487 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004488 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004489 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004490 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004491 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004492 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004493 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004494 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004495
H. Peter Anvin8571f062019-09-23 16:40:03 -07004496 unquote_token(t);
4497 len = t->len;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004498
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04004499 /* make start and count being in range */
4500 if (start < 0)
4501 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004502 if (count < 0)
4503 count = len + count + 1 - start;
4504 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04004505 count = len - start;
4506 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004507 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004508
H. Peter Anvin8571f062019-09-23 16:40:03 -07004509 txt = (start < 0) ? "" : tok_text(t) + start;
4510 len = count;
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07004511 macro_start = make_tok_qstr_len(NULL, txt, len);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004512
H. Peter Anvine2c80182005-01-15 22:15:51 +00004513 /*
4514 * We now have a macro name, an implicit parameter count of
4515 * zero, and a numeric token to use as an expansion. Create
4516 * and store an SMacro.
4517 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004518 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004519 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004520 break;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004521 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004522
H. Peter Anvin8b262472019-02-26 14:00:54 -08004523 case PP_ASSIGN:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004524 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004525 goto done;
4526
H. Peter Anvine2c80182005-01-15 22:15:51 +00004527 last = tline;
4528 tline = expand_smacro(tline->next);
4529 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004530
H. Peter Anvin8b262472019-02-26 14:00:54 -08004531 pps.tptr = tline;
4532 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004533 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004534 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004535 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004536 if (!evalresult)
4537 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004538
H. Peter Anvine2c80182005-01-15 22:15:51 +00004539 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004540 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00004541
H. Peter Anvine2c80182005-01-15 22:15:51 +00004542 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004543 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004544 free_tlist(origline);
4545 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004546 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004547
H. Peter Anvin8571f062019-09-23 16:40:03 -07004548 macro_start = make_tok_num(NULL, reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00004549
H. Peter Anvine2c80182005-01-15 22:15:51 +00004550 /*
4551 * We now have a macro name, an implicit parameter count of
4552 * zero, and a numeric token to use as an expansion. Create
4553 * and store an SMacro.
4554 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004555 define_smacro(mname, casesense, macro_start, NULL);
4556 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004557
H. Peter Anvind2354082019-08-27 16:38:48 -07004558 case PP_ALIASES:
4559 tline = tline->next;
4560 tline = expand_smacro(tline);
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07004561 ppopt.noaliases = !pp_get_boolean_option(tline, !ppopt.noaliases);
H. Peter Anvind2354082019-08-27 16:38:48 -07004562 break;
4563
H. Peter Anvine2c80182005-01-15 22:15:51 +00004564 case PP_LINE:
4565 /*
4566 * Syntax is `%line nnn[+mmm] [filename]'
4567 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004568 if (unlikely(pp_noline))
4569 goto done;
4570
H. Peter Anvine2c80182005-01-15 22:15:51 +00004571 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004572 tline = skip_white(tline);
4573 if (!tok_type(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004574 nasm_nonfatal("`%s' expects line number", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004575 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004576 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07004577 k = readnum(tok_text(tline), &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004578 m = 1;
4579 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004580 if (tok_is(tline, '+')) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004581 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004582 if (!tok_type(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004583 nasm_nonfatal("`%s' expects line increment", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004584 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004585 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07004586 m = readnum(tok_text(tline), &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004587 tline = tline->next;
4588 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004589 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004590 src_set_linnum(k);
4591 istk->lineinc = m;
4592 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07004593 char *fname = detoken(tline, false);
4594 src_set_fname(fname);
4595 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004596 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004597 break;
4598 }
4599
4600done:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004601 free_tlist(origline);
4602 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004603}
4604
4605/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00004606 * Ensure that a macro parameter contains a condition code and
4607 * nothing else. Return the condition code index if so, or -1
4608 * otherwise.
4609 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004610static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004611{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004612 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004613
H. Peter Anvin25a99342007-09-22 17:45:45 -07004614 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004615 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07004616
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004617 t = skip_white(t);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004618 if (!tok_type(t, TOK_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004619 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004620 tt = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004621 tt = skip_white(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004622 if (tok_isnt(tt, ','))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004623 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004624
H. Peter Anvin8571f062019-09-23 16:40:03 -07004625 return bsii(tok_text(t), (const char **)conditions,
4626 ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00004627}
4628
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004629static inline bool pp_concat_match(const Token *t, unsigned int mask)
4630{
4631 return t && (PP_CONCAT_MASK(t->type) & mask);
4632}
4633
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004634/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004635 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004636 * pasting, if @handle_explicit passed then explicit pasting
4637 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004638 * The @m array can contain a series of token types which are
4639 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004640 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004641static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004642 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07004643{
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004644 Token *tok, *t, *next, **prev_next, **prev_nonspace;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004645 bool pasted = false;
4646 char *buf, *p;
4647 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07004648
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004649 /*
4650 * The last token before pasting. We need it
4651 * to be able to connect new handled tokens.
4652 * In other words if there were a tokens stream
4653 *
4654 * A -> B -> C -> D
4655 *
4656 * and we've joined tokens B and C, the resulting
4657 * stream should be
4658 *
4659 * A -> BC -> D
4660 */
4661 tok = *head;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004662 prev_next = prev_nonspace = head;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004663
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004664 if (tok_white(tok) || tok_type(tok, TOK_PASTE))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004665 prev_nonspace = NULL;
4666
4667 while (tok && (next = tok->next)) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004668 bool did_paste = false;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004669
4670 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07004671 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004672 /* Zap redundant whitespaces */
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004673 tok->next = next = zap_white(next);
H. Peter Anvind784a082009-04-20 14:01:18 -07004674 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004675
4676 case TOK_PASTE:
4677 /* Explicit pasting */
4678 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004679 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004680
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004681 /* Left pasting token is start of line */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004682 if (!prev_nonspace) {
4683 nasm_nonfatal("No lvalue found on pasting");
4684 tok = delete_Token(tok);
4685 break;
4686 }
4687
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004688 did_paste = true;
4689
4690 prev_next = prev_nonspace;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004691 t = *prev_nonspace;
4692
4693 /* Delete leading whitespace */
4694 next = zap_white(t->next);
4695
4696 /* Delete the %+ token itself */
4697 nasm_assert(next == tok);
4698 next = delete_Token(next);
4699
4700 /* Delete trailing whitespace */
4701 next = zap_white(next);
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004702
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004703 /*
4704 * No ending token, this might happen in two
4705 * cases
4706 *
4707 * 1) There indeed no right token at all
4708 * 2) There is a bare "%define ID" statement,
4709 * and @ID does expand to whitespace.
4710 *
4711 * So technically we need to do a grammar analysis
4712 * in another stage of parsing, but for now lets don't
4713 * change the behaviour people used to. Simply allow
4714 * whitespace after paste token.
4715 */
4716 if (!next) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004717 *prev_nonspace = tok = NULL; /* End of line */
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004718 break;
4719 }
4720
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004721 p = buf = nasm_malloc(t->len + next->len + 1);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004722 p = mempcpy(p, tok_text(t), t->len);
4723 p = mempcpy(p, tok_text(next), next->len);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004724 *p = '\0';
4725 delete_Token(t);
4726 t = tokenize(buf);
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004727 nasm_free(buf);
4728
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004729 if (unlikely(!t)) {
4730 /*
4731 * No output at all? Replace with a single whitespace.
4732 * This should never happen.
4733 */
4734 t = new_White(NULL);
4735 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004736
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004737 *prev_nonspace = tok = t;
4738 while (t->next)
4739 t = t->next; /* Find the last token produced */
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004740
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004741 /* Delete the second token and attach to the end of the list */
4742 t->next = delete_Token(next);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004743
4744 /* We want to restart from the head of the pasted token */
4745 next = tok;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004746 break;
4747
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004748 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004749 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004750 for (i = 0; i < mnum; i++) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004751 if (pp_concat_match(tok, m[i].mask_head))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004752 break;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004753 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004754
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004755 if (i >= mnum)
4756 break;
4757
4758 len = tok->len;
4759 while (pp_concat_match(next, m[i].mask_tail)) {
4760 len += next->len;
4761 next = next->next;
4762 }
4763
4764 /* No match or no text to process */
4765 if (len == tok->len)
4766 break;
4767
4768 p = buf = nasm_malloc(len + 1);
4769 while (tok != next) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004770 p = mempcpy(p, tok_text(tok), tok->len);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004771 tok = delete_Token(tok);
4772 }
4773 *p = '\0';
4774 *prev_next = tok = t = tokenize(buf);
4775 nasm_free(buf);
4776
4777 /*
4778 * Connect pasted into original stream,
4779 * ie A -> new-tokens -> B
4780 */
4781 while (t->next)
4782 t = t->next;
4783 t->next = next;
4784 prev_next = prev_nonspace = &t->next;
4785 did_paste = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004786 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004787 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004788
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004789 if (did_paste) {
4790 pasted = true;
4791 } else {
4792 prev_next = &tok->next;
4793 if (next && next->type != TOK_WHITESPACE && next->type != TOK_PASTE)
4794 prev_nonspace = prev_next;
4795 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004796
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004797 tok = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004798 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004799
4800 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07004801}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004802
4803/*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004804 * Computes the proper rotation of mmacro parameters
4805 */
4806static int mmac_rotate(const MMacro *mac, unsigned int n)
4807{
4808 if (--n < mac->nparam)
4809 n = (n + mac->rotate) % mac->nparam;
4810
4811 return n+1;
4812}
4813
4814/*
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004815 * expands to a list of tokens from %{x:y}
4816 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004817static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004818{
4819 Token *t = tline, **tt, *tm, *head;
4820 char *pos;
4821 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004822
H. Peter Anvin8571f062019-09-23 16:40:03 -07004823 pos = strchr(tok_text(tline), ':');
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004824 nasm_assert(pos);
4825
4826 lst = atoi(pos + 1);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004827 fst = atoi(tok_text(tline) + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004828
4829 /*
4830 * only macros params are accounted so
4831 * if someone passes %0 -- we reject such
4832 * value(s)
4833 */
4834 if (lst == 0 || fst == 0)
4835 goto err;
4836
4837 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004838 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
4839 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004840 goto err;
4841
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004842 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
4843 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004844
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004845 /* count from zero */
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004846 fst--, lst--;
4847
4848 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004849 * It will be at least one token. Note we
4850 * need to scan params until separator, otherwise
4851 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004852 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004853 j = (fst + mac->rotate) % mac->nparam;
4854 tm = mac->params[j+1];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03004855 if (!tm)
4856 goto err;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004857 head = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004858 tt = &head->next, tm = tm->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004859 while (tok_isnt(tm, ',')) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004860 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004861 *tt = t, tt = &t->next, tm = tm->next;
4862 }
4863
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004864 if (fst < lst) {
4865 for (i = fst + 1; i <= lst; i++) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004866 t = make_tok_char(NULL, ',');
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004867 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004868 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004869 tm = mac->params[j+1];
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004870 while (tok_isnt(tm, ',')) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004871 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004872 *tt = t, tt = &t->next, tm = tm->next;
4873 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004874 }
4875 } else {
4876 for (i = fst - 1; i >= lst; i--) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004877 t = make_tok_char(NULL, ',');
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004878 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004879 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004880 tm = mac->params[j+1];
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004881 while (!tok_isnt(tm, ',')) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004882 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004883 *tt = t, tt = &t->next, tm = tm->next;
4884 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004885 }
4886 }
4887
4888 *last = tt;
4889 return head;
4890
4891err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004892 nasm_nonfatal("`%%{%s}': macro parameters out of range",
H. Peter Anvin8571f062019-09-23 16:40:03 -07004893 tok_text(tline) + 1);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004894 return NULL;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004895}
4896
H. Peter Anvin76690a12002-04-30 20:52:49 +00004897/*
4898 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004899 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004900 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004901 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004902static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004903{
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004904 Token **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004905 bool changed = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004906 MMacro *mac = istk->mstk.mmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004907
4908 tail = &thead;
4909 thead = NULL;
4910
H. Peter Anvine2c80182005-01-15 22:15:51 +00004911 while (tline) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004912 bool change;
H. Peter Anvin (Intel)a762cd42020-06-01 11:49:08 -07004913 bool err_not_mac = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004914 Token *t = tline;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004915 const char *text = tok_text(t);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004916 int type = t->type;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004917
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004918 tline = tline->next;
4919 t->next = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004920
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004921 switch (type) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004922 case TOK_LOCAL_SYMBOL:
H. Peter Anvin (Intel)a762cd42020-06-01 11:49:08 -07004923 change = true;
4924
4925 if (!mac) {
4926 err_not_mac = true;
4927 break;
4928 }
4929
H. Peter Anvin8571f062019-09-23 16:40:03 -07004930 type = TOK_ID;
4931 text = nasm_asprintf("..@%"PRIu64".%s", mac->unique, text+2);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004932 break;
4933 case TOK_MMACRO_PARAM:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004934 {
4935 Token *tt = NULL;
4936
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004937 change = true;
4938
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004939 if (!mac) {
H. Peter Anvin (Intel)a762cd42020-06-01 11:49:08 -07004940 err_not_mac = true;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004941 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004942 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004943
4944 if (strchr(text, ':')) {
4945 /*
4946 * seems we have a parameters range here
4947 */
4948 Token *head, **last;
4949 head = expand_mmac_params_range(mac, t, &last);
4950 if (head) {
4951 *tail = head;
4952 *last = tline;
4953 text = NULL;
4954 }
4955 break;
4956 }
4957
4958 switch (text[1]) {
4959 /*
4960 * We have to make a substitution of one of the
4961 * forms %1, %-1, %+1, %%foo, %0, %00.
4962 */
4963 case '0':
4964 if (!text[2]) {
4965 type = TOK_NUMBER;
4966 text = nasm_asprintf("%d", mac->nparam);
4967 break;
4968 }
4969 if (text[2] != '0' || text[3])
4970 goto invalid;
4971 /* a possible captured label == mac->params[0] */
4972 /* fall through */
4973 default:
4974 {
4975 unsigned long n;
4976 char *ep;
4977
4978 n = strtoul(text + 1, &ep, 10);
4979 if (unlikely(*ep))
4980 goto invalid;
4981
4982 if (n <= mac->nparam) {
4983 n = mmac_rotate(mac, n);
4984 dup_tlistn(mac->params[n], mac->paramlen[n], &tail);
4985 }
4986 text = NULL;
4987 break;
4988 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004989 case '-':
4990 case '+':
4991 {
4992 int cc;
4993 unsigned long n;
4994 char *ep;
4995
H. Peter Anvin8571f062019-09-23 16:40:03 -07004996 n = strtoul(tok_text(t) + 2, &ep, 10);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004997 if (unlikely(*ep))
4998 goto invalid;
4999
Chang S. Bae057b8322020-04-18 23:11:21 +00005000 if (n && n <= mac->nparam) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005001 n = mmac_rotate(mac, n);
5002 tt = mac->params[n];
5003 }
5004 cc = find_cc(tt);
5005 if (cc == -1) {
5006 nasm_nonfatal("macro parameter `%s' is not a condition code",
H. Peter Anvin8571f062019-09-23 16:40:03 -07005007 tok_text(t));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005008 text = NULL;
5009 break;
5010 }
5011
5012 type = TOK_ID;
5013 if (text[1] == '-') {
5014 int ncc = inverse_ccs[cc];
5015 if (unlikely(ncc == -1)) {
5016 nasm_nonfatal("condition code `%s' is not invertible",
5017 conditions[cc]);
5018 break;
5019 }
5020 cc = ncc;
5021 }
5022 text = nasm_strdup(conditions[cc]);
5023 break;
5024 }
5025
5026 invalid:
5027 nasm_nonfatal("invalid macro parameter: `%s'", text);
5028 text = NULL;
5029 break;
5030 }
5031 break;
5032 }
5033
5034 case TOK_PREPROC_Q:
5035 if (mac) {
5036 type = TOK_ID;
5037 text = nasm_strdup(mac->iname);
5038 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07005039 } else {
5040 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005041 }
5042 break;
5043
5044 case TOK_PREPROC_QQ:
5045 if (mac) {
5046 type = TOK_ID;
5047 text = nasm_strdup(mac->name);
5048 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07005049 } else {
5050 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005051 }
5052 break;
5053
5054 case TOK_INDIRECT:
5055 {
5056 Token *tt;
5057
H. Peter Anvin8571f062019-09-23 16:40:03 -07005058 tt = tokenize(tok_text(t));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005059 tt = expand_mmac_params(tt);
5060 tt = expand_smacro(tt);
5061 /* Why dup_tlist() here? We should own tt... */
5062 dup_tlist(tt, &tail);
5063 text = NULL;
5064 change = true;
5065 break;
5066 }
5067
5068 default:
5069 change = false;
5070 break;
5071 }
5072
H. Peter Anvin (Intel)a762cd42020-06-01 11:49:08 -07005073 if (err_not_mac) {
5074 nasm_nonfatal("`%s': not in a macro call", text);
5075 text = NULL;
5076 change = true;
5077 }
5078
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005079 if (change) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005080 if (!text) {
5081 delete_Token(t);
5082 } else {
5083 *tail = t;
5084 tail = &t->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005085 set_text(t, text, tok_strlen(text));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005086 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005087 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005088 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005089 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005090 *tail = t;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005091 tail = &t->next;
5092 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00005093 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005094
H. Peter Anvineba20a72002-04-30 20:53:55 +00005095 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07005096
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005097 if (changed) {
5098 const struct tokseq_match t[] = {
5099 {
5100 PP_CONCAT_MASK(TOK_ID) |
5101 PP_CONCAT_MASK(TOK_FLOAT), /* head */
5102 PP_CONCAT_MASK(TOK_ID) |
5103 PP_CONCAT_MASK(TOK_NUMBER) |
5104 PP_CONCAT_MASK(TOK_FLOAT) |
5105 PP_CONCAT_MASK(TOK_OTHER) /* tail */
5106 },
5107 {
5108 PP_CONCAT_MASK(TOK_NUMBER), /* head */
5109 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
5110 }
5111 };
5112 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
5113 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07005114
H. Peter Anvin76690a12002-04-30 20:52:49 +00005115 return thead;
5116}
5117
H. Peter Anvin322bee02019-08-10 01:38:06 -07005118static Token *expand_smacro_noreset(Token * tline);
H. Peter Anvin322bee02019-08-10 01:38:06 -07005119
H. Peter Anvin76690a12002-04-30 20:52:49 +00005120/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005121 * Expand *one* single-line macro instance. If the first token is not
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005122 * a macro at all, it is simply copied to the output and the pointer
5123 * advanced. tpp should be a pointer to a pointer (usually the next
5124 * pointer of the previous token) to the first token. **tpp is updated
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005125 * to point to the first token of the expansion, and *tpp updated to
5126 * point to the next pointer of the last token of the expansion.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005127 *
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005128 * If the expansion is empty, *tpp will be unchanged but **tpp will
5129 * be advanced past the macro call.
5130 *
H. Peter Anvin322bee02019-08-10 01:38:06 -07005131 * Return the macro expanded, or NULL if no expansion took place.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005132 */
H. Peter Anvin322bee02019-08-10 01:38:06 -07005133static SMacro *expand_one_smacro(Token ***tpp)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005134{
5135 Token **params = NULL;
5136 const char *mname;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005137 Token *mstart = **tpp;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005138 Token *tline = mstart;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005139 SMacro *head, *m;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005140 int i;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005141 Token *t, *tup, *tafter;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005142 int nparam = 0;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005143 bool cond_comma;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005144
5145 if (!tline)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005146 return false; /* Empty line, nothing to do */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005147
H. Peter Anvin8571f062019-09-23 16:40:03 -07005148 mname = tok_text(mstart);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005149
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005150 smacro_deadman.total--;
H. Peter Anvin322bee02019-08-10 01:38:06 -07005151 smacro_deadman.levels--;
5152
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005153 if (unlikely(smacro_deadman.total < 0 || smacro_deadman.levels < 0)) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07005154 if (unlikely(!smacro_deadman.triggered)) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005155 nasm_nonfatal("interminable macro recursion");
H. Peter Anvin322bee02019-08-10 01:38:06 -07005156 smacro_deadman.triggered = true;
5157 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005158 goto not_a_macro;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005159 } else if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005160 head = (SMacro *)hash_findix(&smacros, mname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005161 } else if (tline->type == TOK_LOCAL_MACRO) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005162 Context *ctx = get_ctx(mname, &mname);
5163 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
5164 } else {
5165 goto not_a_macro;
5166 }
5167
5168 /*
5169 * We've hit an identifier of some sort. First check whether the
5170 * identifier is a single-line macro at all, then think about
5171 * checking for parameters if necessary.
5172 */
5173 list_for_each(m, head) {
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005174 if (unlikely(m->alias && ppopt.noaliases))
H. Peter Anvind2354082019-08-27 16:38:48 -07005175 continue;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005176 if (!mstrcmp(m->name, mname, m->casesense))
5177 break;
5178 }
5179
5180 if (!m) {
5181 goto not_a_macro;
5182 }
5183
5184 /* Parse parameters, if applicable */
5185
5186 params = NULL;
5187 nparam = 0;
5188
5189 if (m->nparam == 0) {
5190 /*
5191 * Simple case: the macro is parameterless.
5192 * Nothing to parse; the expansion code will
5193 * drop the macro name token.
5194 */
5195 } else {
5196 /*
5197 * Complicated case: at least one macro with this name
5198 * exists and takes parameters. We must find the
5199 * parameters in the call, count them, find the SMacro
5200 * that corresponds to that form of the macro call, and
5201 * substitute for the parameters when we expand. What a
5202 * pain.
5203 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005204 Token *t;
5205 int paren, brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005206
5207 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005208 tline = skip_white(tline);
5209 if (!tok_is(tline, '(')) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005210 /*
5211 * This macro wasn't called with parameters: ignore
5212 * the call. (Behaviour borrowed from gnu cpp.)
5213 */
5214 goto not_a_macro;
5215 }
5216
5217 paren = 1;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005218 nparam = 1;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005219 brackets = 0;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005220 t = tline; /* tline points to leading ( */
5221
5222 while (paren) {
5223 t = t->next;
5224
5225 if (!t) {
5226 nasm_nonfatal("macro call expects terminating `)'");
5227 goto not_a_macro;
5228 }
5229
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005230 if (t->type != TOK_OTHER || t->len != 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005231 continue;
5232
H. Peter Anvin8571f062019-09-23 16:40:03 -07005233 switch (t->text.a[0]) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005234 case ',':
H. Peter Anvinbd00f252020-06-04 21:05:01 -07005235 if (!brackets && paren == 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005236 nparam++;
5237 break;
5238
5239 case '{':
5240 brackets++;
5241 break;
5242
5243 case '}':
5244 if (brackets > 0)
5245 brackets--;
5246 break;
5247
5248 case '(':
5249 if (!brackets)
5250 paren++;
5251 break;
5252
5253 case ')':
5254 if (!brackets)
5255 paren--;
5256 break;
5257
5258 default:
5259 break; /* Normal token */
5260 }
5261 }
5262
5263 /*
5264 * Look for a macro matching in both name and parameter count.
5265 * We already know any matches cannot be anywhere before the
5266 * current position of "m", so there is no reason to
5267 * backtrack.
5268 */
5269 while (1) {
5270 if (!m) {
5271 /*!
5272 *!macro-params-single [on] single-line macro calls with wrong parameter count
5273 *! warns about \i{single-line macros} being invoked
5274 *! with the wrong number of parameters.
5275 */
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005276 nasm_warn(WARN_MACRO_PARAMS_SINGLE|ERR_HOLD,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005277 "single-line macro `%s' exists, "
5278 "but not taking %d parameter%s",
5279 mname, nparam, (nparam == 1) ? "" : "s");
5280 goto not_a_macro;
5281 }
5282
5283 if (!mstrcmp(m->name, mname, m->casesense)) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005284 if (nparam == m->nparam)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005285 break; /* It's good */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005286 if (m->greedy && nparam >= m->nparam-1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005287 break; /* Also good */
5288 }
5289 m = m->next;
5290 }
5291 }
5292
5293 if (m->in_progress)
5294 goto not_a_macro;
5295
5296 /* Expand the macro */
5297 m->in_progress = true;
5298
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005299 if (nparam) {
5300 /* Extract parameters */
5301 Token **phead, **pep;
5302 int white = 0;
5303 int brackets = 0;
5304 int paren;
5305 bool bracketed = false;
5306 bool bad_bracket = false;
5307 enum sparmflags flags;
5308
5309 nparam = m->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005310 paren = 1;
5311 nasm_newn(params, nparam);
5312 i = 0;
5313 flags = m->params[i].flags;
5314 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005315 *pep = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005316
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005317 while (paren) {
5318 bool skip;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005319 char ch;
5320
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005321 tline = tline->next;
5322
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005323 if (!tline)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005324 nasm_nonfatal("macro call expects terminating `)'");
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005325
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005326 ch = 0;
5327 skip = false;
5328
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005329
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005330 switch (tline->type) {
5331 case TOK_OTHER:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005332 if (tline->len == 1)
H. Peter Anvin8571f062019-09-23 16:40:03 -07005333 ch = tline->text.a[0];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005334 break;
5335
5336 case TOK_WHITESPACE:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005337 if (!(flags & SPARM_NOSTRIP)) {
5338 if (brackets || *phead)
5339 white++; /* Keep interior whitespace */
5340 skip = true;
5341 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005342 break;
5343
5344 default:
5345 break;
5346 }
5347
5348 switch (ch) {
5349 case ',':
H. Peter Anvinbd00f252020-06-04 21:05:01 -07005350 if (!brackets && paren == 1 && !(flags & SPARM_GREEDY)) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005351 i++;
5352 nasm_assert(i < nparam);
5353 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005354 *pep = NULL;
5355 bracketed = false;
5356 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005357 flags = m->params[i].flags;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005358 }
5359 break;
5360
5361 case '{':
5362 if (!bracketed) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005363 bracketed = !*phead && !(flags & SPARM_NOSTRIP);
5364 skip = bracketed;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005365 }
5366 brackets++;
5367 break;
5368
5369 case '}':
5370 if (brackets > 0) {
5371 if (!--brackets)
5372 skip = bracketed;
5373 }
5374 break;
5375
5376 case '(':
5377 if (!brackets)
5378 paren++;
5379 break;
5380
5381 case ')':
5382 if (!brackets) {
5383 paren--;
5384 if (!paren) {
5385 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005386 i++; /* Found last argument */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005387 }
5388 }
5389 break;
5390
5391 default:
5392 break; /* Normal token */
5393 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005394
5395 if (!skip) {
5396 Token *t;
5397
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005398 bad_bracket |= bracketed && !brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005399
5400 if (white) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005401 *pep = t = new_White(NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005402 pep = &t->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005403 white = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005404 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005405 *pep = t = dup_Token(NULL, tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005406 pep = &t->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005407 }
5408 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005409
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005410 /*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005411 * Possible further processing of parameters. Note that the
5412 * ordering matters here.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005413 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005414 for (i = 0; i < nparam; i++) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005415 enum sparmflags flags = m->params[i].flags;
5416
5417 if (flags & SPARM_EVAL) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005418 /* Evaluate this parameter as a number */
5419 struct ppscan pps;
5420 struct tokenval tokval;
5421 expr *evalresult;
5422 Token *eval_param;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005423
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005424 pps.tptr = eval_param = expand_smacro_noreset(params[i]);
5425 pps.ntokens = -1;
5426 tokval.t_type = TOKEN_INVALID;
5427 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005428
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005429 free_tlist(eval_param);
5430 params[i] = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005431
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005432 if (!evalresult) {
5433 /* Nothing meaningful to do */
5434 } else if (tokval.t_type) {
5435 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
5436 } else if (!is_simple(evalresult)) {
5437 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
5438 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005439 params[i] = make_tok_num(NULL, reloc_value(evalresult));
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005440 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005441 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005442
5443 if (flags & SPARM_STR) {
5444 /* Convert expansion to a quoted string */
5445 char *arg;
5446 Token *qs;
5447
5448 qs = expand_smacro_noreset(params[i]);
5449 arg = detoken(qs, false);
5450 free_tlist(qs);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005451 params[i] = make_tok_qstr(NULL, arg);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005452 nasm_free(arg);
5453 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005454 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005455 }
5456
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005457 /* Note: we own the expansion this returns. */
5458 t = m->expand(m, params, nparam);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005459
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005460 tafter = tline->next; /* Skip past the macro call */
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005461 tline->next = NULL; /* Truncate list at the macro call end */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005462 tline = tafter;
5463
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005464 tup = NULL;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005465 cond_comma = false;
5466
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005467 while (t) {
5468 enum pp_token_type type = t->type;
5469 Token *tnext = t->next;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005470
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005471 switch (type) {
5472 case TOK_PREPROC_Q:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005473 delete_Token(t);
5474 t = dup_Token(tline, mstart);
5475 break;
5476
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005477 case TOK_PREPROC_QQ:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005478 {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005479 size_t mlen = strlen(m->name);
5480 size_t len;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005481 char *p;
5482
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005483 t->type = mstart->type;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005484 if (t->type == TOK_LOCAL_MACRO) {
5485 const char *psp; /* prefix start pointer */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005486 const char *pep; /* prefix end pointer */
H. Peter Anvin8571f062019-09-23 16:40:03 -07005487 size_t plen;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005488
H. Peter Anvin8571f062019-09-23 16:40:03 -07005489 psp = tok_text(mstart);
5490 get_ctx(psp, &pep);
5491 plen = pep - psp;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005492
H. Peter Anvin8571f062019-09-23 16:40:03 -07005493 len = mlen + plen;
5494 p = nasm_malloc(len + 1);
5495 p = mempcpy(p, psp, plen);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005496 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005497 len = mlen;
5498 p = nasm_malloc(len + 1);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005499 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07005500 p = mempcpy(p, m->name, mlen);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005501 *p = '\0';
H. Peter Anvin8571f062019-09-23 16:40:03 -07005502 set_text_free(t, p, len);
5503
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005504 t->next = tline;
5505 break;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005506 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005507
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005508 case TOK_COND_COMMA:
5509 delete_Token(t);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005510 t = cond_comma ? make_tok_char(tline, ',') : NULL;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005511 break;
5512
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005513 case TOK_ID:
5514 case TOK_PREPROC_ID:
H. Peter Anvin8571f062019-09-23 16:40:03 -07005515 case TOK_LOCAL_MACRO:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005516 {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005517 /*
5518 * Chain this into the target line *before* expanding,
5519 * that way we pick up any arguments to the new macro call,
5520 * if applicable.
5521 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005522 Token **tp = &t;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005523 t->next = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005524 expand_one_smacro(&tp);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005525 tline = *tp; /* First token left after any macro call */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005526 break;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005527 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005528 default:
5529 if (is_smac_param(t->type)) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005530 int param = smac_nparam(t->type);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005531 nasm_assert(!tup && param < nparam);
5532 delete_Token(t);
5533 t = NULL;
5534 tup = tnext;
5535 tnext = dup_tlist_reverse(params[param], NULL);
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005536 cond_comma = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005537 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005538 t->next = tline;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005539 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005540 }
5541
5542 if (t) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005543 Token *endt = tline;
5544
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005545 tline = t;
Chang S. Bae95e54a92020-02-06 14:39:22 -08005546 while (!cond_comma && t && t != endt) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005547 cond_comma = t->type != TOK_WHITESPACE;
Chang S. Bae95e54a92020-02-06 14:39:22 -08005548 t = t->next;
5549 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005550 }
5551
5552 if (tnext) {
5553 t = tnext;
5554 } else {
5555 t = tup;
5556 tup = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005557 }
5558 }
5559
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005560 **tpp = tline;
H. Peter Anvin (Intel)6e714962020-06-01 12:21:10 -07005561 for (t = tline; t && t != tafter; t = t->next)
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005562 *tpp = &t->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005563
5564 m->in_progress = false;
5565
5566 /* Don't do this until after expansion or we will clobber mname */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005567 free_tlist(mstart);
H. Peter Anvin322bee02019-08-10 01:38:06 -07005568 goto done;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005569
5570 /*
5571 * No macro expansion needed; roll back to mstart (if necessary)
H. Peter Anvin322bee02019-08-10 01:38:06 -07005572 * and then advance to the next input token. Note that this is
5573 * by far the common case!
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005574 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005575not_a_macro:
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005576 *tpp = &mstart->next;
H. Peter Anvin322bee02019-08-10 01:38:06 -07005577 m = NULL;
5578done:
5579 smacro_deadman.levels++;
5580 if (unlikely(params))
5581 free_tlist_array(params, nparam);
5582 return m;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005583}
5584
5585/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005586 * Expand all single-line macro calls made in the given line.
5587 * Return the expanded version of the line. The original is deemed
5588 * to be destroyed in the process. (In reality we'll just move
5589 * Tokens from input to output a lot of the time, rather than
5590 * actually bothering to destroy and replicate.)
5591 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005592static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005593{
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005594 smacro_deadman.total = nasm_limit[LIMIT_MACRO_TOKENS];
H. Peter Anvin322bee02019-08-10 01:38:06 -07005595 smacro_deadman.levels = nasm_limit[LIMIT_MACRO_LEVELS];
5596 smacro_deadman.triggered = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005597 return expand_smacro_noreset(tline);
5598}
5599
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005600static Token *expand_smacro_noreset(Token *org_tline)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005601{
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005602 Token *tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005603 bool expanded;
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005604 errhold errhold; /* Hold warning/errors during expansion */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005605
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005606 if (!org_tline)
5607 return NULL; /* Empty input */
5608
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005609 /*
5610 * Trick: we should avoid changing the start token pointer since it can
5611 * be contained in "next" field of other token. Because of this
5612 * we allocate a copy of first token and work with it; at the end of
5613 * routine we copy it back
5614 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005615 tline = dup_Token(org_tline->next, org_tline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005616
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005617 /*
5618 * Pretend that we always end up doing expansion on the first pass;
5619 * that way %+ get processed. However, if we process %+ before the
5620 * first pass, we end up with things like MACRO %+ TAIL trying to
5621 * look up the macro "MACROTAIL", which we don't want.
5622 */
5623 expanded = true;
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005624
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005625 while (true) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005626 static const struct tokseq_match tmatch[] = {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005627 {
5628 PP_CONCAT_MASK(TOK_ID) |
H. Peter Anvin8571f062019-09-23 16:40:03 -07005629 PP_CONCAT_MASK(TOK_LOCAL_MACRO) |
5630 PP_CONCAT_MASK(TOK_ENVIRON) |
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005631 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
5632 PP_CONCAT_MASK(TOK_ID) |
H. Peter Anvin8571f062019-09-23 16:40:03 -07005633 PP_CONCAT_MASK(TOK_LOCAL_MACRO) |
5634 PP_CONCAT_MASK(TOK_ENVIRON) |
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005635 PP_CONCAT_MASK(TOK_PREPROC_ID) |
5636 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
5637 }
5638 };
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005639 Token **tail = &tline;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005640
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005641 /*
5642 * We hold warnings/errors until we are done this this loop. It is
5643 * possible for nuisance warnings to appear that disappear on later
5644 * passes.
5645 */
5646 errhold = nasm_error_hold_push();
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005647
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005648 while (*tail) /* main token loop */
H. Peter Anvin322bee02019-08-10 01:38:06 -07005649 expanded |= !!expand_one_smacro(&tail);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005650
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005651 if (!expanded)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005652 break; /* Done! */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005653
5654 /*
5655 * Now scan the entire line and look for successive TOK_IDs
5656 * that resulted after expansion (they can't be produced by
5657 * tokenize()). The successive TOK_IDs should be concatenated.
5658 * Also we look for %+ tokens and concatenate the tokens
5659 * before and after them (without white spaces in between).
5660 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005661 if (!paste_tokens(&tline, tmatch, ARRAY_SIZE(tmatch), true))
5662 break; /* Done again! */
5663
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005664 nasm_error_hold_pop(errhold, false);
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005665 expanded = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005666 }
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005667 nasm_error_hold_pop(errhold, true);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005668
H. Peter Anvin8571f062019-09-23 16:40:03 -07005669 if (!tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005670 /*
5671 * The expression expanded to empty line;
5672 * we can't return NULL because of the "trick" above.
5673 * Just set the line to a single WHITESPACE token.
H. Peter Anvin8571f062019-09-23 16:40:03 -07005674 */
5675
5676 tline = new_White(NULL);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005677 }
5678
H. Peter Anvin8571f062019-09-23 16:40:03 -07005679 steal_Token(org_tline, tline);
5680 org_tline->next = tline->next;
5681 delete_Token(tline);
5682
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005683 return org_tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005684}
5685
5686/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005687 * Similar to expand_smacro but used exclusively with macro identifiers
5688 * right before they are fetched in. The reason is that there can be
5689 * identifiers consisting of several subparts. We consider that if there
5690 * are more than one element forming the name, user wants a expansion,
5691 * otherwise it will be left as-is. Example:
5692 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005693 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005694 *
5695 * the identifier %$abc will be left as-is so that the handler for %define
5696 * will suck it and define the corresponding value. Other case:
5697 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005698 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005699 *
5700 * In this case user wants name to be expanded *before* %define starts
5701 * working, so we'll expand %$abc into something (if it has a value;
5702 * otherwise it will be left as-is) then concatenate all successive
5703 * PP_IDs into one.
5704 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005705static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005706{
5707 Token *cur, *oldnext = NULL;
5708
H. Peter Anvin734b1882002-04-30 21:01:08 +00005709 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005710 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005711
5712 cur = tline;
5713 while (cur->next &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07005714 (cur->next->type == TOK_ID || cur->next->type == TOK_PREPROC_ID ||
5715 cur->next->type == TOK_LOCAL_MACRO || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005716 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005717
5718 /* If identifier consists of just one token, don't expand */
5719 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005720 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005721
H. Peter Anvine2c80182005-01-15 22:15:51 +00005722 if (cur) {
5723 oldnext = cur->next; /* Detach the tail past identifier */
5724 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005725 }
5726
H. Peter Anvin734b1882002-04-30 21:01:08 +00005727 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005728
H. Peter Anvine2c80182005-01-15 22:15:51 +00005729 if (cur) {
5730 /* expand_smacro possibly changhed tline; re-scan for EOL */
5731 cur = tline;
5732 while (cur && cur->next)
5733 cur = cur->next;
5734 if (cur)
5735 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005736 }
5737
5738 return tline;
5739}
5740
5741/*
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005742 * This is called from find_mmacro_in_list() after finding a suitable macro.
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005743 */
5744static MMacro *use_mmacro(MMacro *m, int *nparamp, Token ***paramsp)
5745{
5746 int nparam = *nparamp;
5747 Token **params = *paramsp;
5748
5749 /*
5750 * This one is right. Just check if cycle removal
5751 * prohibits us using it before we actually celebrate...
5752 */
5753 if (m->in_progress > m->max_depth) {
5754 if (m->max_depth > 0) {
5755 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
5756 m->max_depth);
5757 }
5758 nasm_free(params);
5759 *nparamp = 0;
5760 *paramsp = NULL;
5761 return NULL;
5762 }
5763
5764 /*
5765 * It's right, and we can use it. Add its default
5766 * parameters to the end of our list if necessary.
5767 */
5768 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
5769 int newnparam = m->nparam_min + m->ndefs;
5770 params = nasm_realloc(params, sizeof(*params) * (newnparam+2));
5771 memcpy(&params[nparam+1], &m->defaults[nparam+1-m->nparam_min],
5772 (newnparam - nparam) * sizeof(*params));
5773 nparam = newnparam;
5774 }
5775 /*
5776 * If we've gone over the maximum parameter count (and
5777 * we're in Plus mode), ignore parameters beyond
5778 * nparam_max.
5779 */
5780 if (m->plus && nparam > m->nparam_max)
5781 nparam = m->nparam_max;
5782
5783 /*
5784 * If nparam was adjusted above, make sure the list is still
5785 * NULL-terminated.
5786 */
5787 params[nparam+1] = NULL;
5788
5789 /* Done! */
5790 *paramsp = params;
5791 *nparamp = nparam;
5792 return m;
5793}
5794
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005795/*
5796 * Search a macro list and try to find a match. If matching, call
5797 * use_mmacro() to set up the macro call. m points to the list of
5798 * search, which is_mmacro() sets to the first *possible* match.
5799 */
5800static MMacro *
5801find_mmacro_in_list(MMacro *m, const char *finding,
5802 int *nparamp, Token ***paramsp)
5803{
5804 int nparam = *nparamp;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005805
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005806 while (m) {
5807 if (m->nparam_min <= nparam
5808 && (m->plus || nparam <= m->nparam_max)) {
5809 /*
5810 * This one matches, use it.
5811 */
5812 return use_mmacro(m, nparamp, paramsp);
5813 }
5814
5815 /*
5816 * Otherwise search for the next one with a name match.
5817 */
5818 list_for_each(m, m->next) {
5819 if (!mstrcmp(m->name, finding, m->casesense))
5820 break;
5821 }
5822 }
5823
5824 return NULL;
5825}
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005826
5827/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005828 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005829 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005830 * to check for an initial label - that's taken care of in
5831 * expand_mmacro - but must check numbers of parameters. Guaranteed
5832 * to be called with tline->type == TOK_ID, so the putative macro
5833 * name is easy to find.
5834 */
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005835static MMacro *is_mmacro(Token * tline, int *nparamp, Token ***paramsp)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005836{
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005837 MMacro *head, *m, *found;
5838 Token **params, **comma;
5839 int raw_nparam, nparam;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005840 const char *finding = tok_text(tline);
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005841 bool empty_args = !tline->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005842
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005843 *nparamp = 0;
5844 *paramsp = NULL;
5845
H. Peter Anvin8571f062019-09-23 16:40:03 -07005846 head = (MMacro *) hash_findix(&mmacros, finding);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005847
5848 /*
5849 * Efficiency: first we see if any macro exists with the given
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005850 * name which isn't already excluded by macro cycle removal.
5851 * (The cycle removal test here helps optimize the case of wrapping
5852 * instructions, and is cheap to do here.)
5853 *
5854 * If not, we can return NULL immediately. _Then_ we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005855 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005856 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005857 */
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005858 list_for_each(m, head) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005859 if (!mstrcmp(m->name, finding, m->casesense) &&
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005860 (m->in_progress != 1 || m->max_depth > 0))
5861 break; /* Found something that needs consideration */
5862 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005863 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005864 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005865
5866 /*
5867 * OK, we have a potential macro. Count and demarcate the
5868 * parameters.
5869 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005870 comma = count_mmac_params(tline->next, nparamp, paramsp);
5871 raw_nparam = *nparamp;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005872
5873 /*
5874 * Search for an exact match. This cannot come *before* the m
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005875 * found in the list search before, so we can start there.
5876 *
5877 * If found is NULL and *paramsp has been cleared, then we
5878 * encountered an error for which we have already issued a
5879 * diagnostic, so we should not proceed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005880 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005881 found = find_mmacro_in_list(m, finding, nparamp, paramsp);
5882 if (!*paramsp)
5883 return NULL;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005884
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005885 nparam = *nparamp;
5886 params = *paramsp;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005887
5888 /*
5889 * Special weirdness: in NASM < 2.15, an expansion of
5890 * *only* whitespace, as can happen during macro expansion under
5891 * certain circumstances, is counted as zero arguments for the
5892 * purpose of %0, but one argument for the purpose of macro
5893 * matching! In particular, this affects:
5894 *
5895 * foobar %1
5896 *
5897 * ... with %1 being empty; this would call the one-argument
5898 * version of "foobar" with an empty argument, equivalent to
5899 *
5900 * foobar {%1}
5901 *
5902 * ... except that %0 would be set to 0 inside foobar, even if
5903 * foobar is declared with "%macro foobar 1" or equivalent!
5904 *
5905 * The proper way to do that is to define "%macro foobar 0-1".
5906 *
5907 * To be compatible without doing something too stupid, try to
5908 * match a zero-argument macro first, but if that fails, try
5909 * for a one-argument macro with the above behavior.
5910 *
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005911 * Furthermore, NASM < 2.15 will match stripping a tailing empty
5912 * argument, but in that case %0 *does* reflect that this argument
5913 * have been stripped; this is handled in count_mmac_params().
5914 *
5915 * To disable these insane legacy behaviors, use:
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005916 *
5917 * %pragma preproc sane_empty_expansion yes
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005918 *
5919 *!macro-params-legacy [on] improperly calling multi-line macro for legacy support
5920 *! warns about \i{multi-line macros} being invoked
5921 *! with the wrong number of parameters, but for bug-compatibility
5922 *! with NASM versions older than 2.15, NASM tried to fix up the
5923 *! parameters to match the legacy behavior and call the macro anyway.
5924 *! This can happen in certain cases where there are empty arguments
5925 *! without braces, sometimes as a result of macro expansion.
5926 *!-
5927 *! The legacy behavior is quite strange and highly context-dependent,
5928 *! and can be disabled with:
5929 *!-
5930 *! \c %pragma preproc sane_empty_expansion true
5931 *!-
5932 *! It is highly recommended to use this option in new code.
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005933 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005934 if (!ppopt.sane_empty_expansion) {
5935 if (!found) {
5936 if (raw_nparam == 0 && !empty_args) {
5937 /*
5938 * A single all-whitespace parameter as the only thing?
5939 * Look for a one-argument macro, but don't adjust
5940 * *nparamp.
5941 */
5942 int bogus_nparam = 1;
5943 params[2] = NULL;
5944 found = find_mmacro_in_list(m, finding, &bogus_nparam, paramsp);
5945 } else if (raw_nparam > 1 && comma) {
5946 Token *comma_tail = *comma;
5947
5948 /*
5949 * Drop the terminal argument and try again.
5950 * If we fail, we need to restore the comma to
5951 * preserve tlist.
5952 */
5953 *comma = NULL;
5954 *nparamp = raw_nparam - 1;
5955 found = find_mmacro_in_list(m, finding, nparamp, paramsp);
5956 if (found)
5957 free_tlist(comma_tail);
5958 else
5959 *comma = comma_tail;
5960 }
5961
5962 if (!*paramsp)
5963 return NULL;
5964 } else if (comma) {
5965 free_tlist(*comma);
5966 *comma = NULL;
5967 if (raw_nparam > found->nparam_min &&
5968 raw_nparam <= found->nparam_min + found->ndefs) {
5969 /* Replace empty argument with default parameter */
5970 params[raw_nparam] =
5971 found->defaults[raw_nparam - found->nparam_min];
5972 } else if (raw_nparam > found->nparam_max && found->plus) {
5973 /* Just drop the comma, don't adjust argument count */
5974 } else {
5975 /* Drop argument. This may cause nparam < nparam_min. */
5976 params[raw_nparam] = NULL;
5977 *nparamp = nparam = raw_nparam - 1;
5978 }
5979 }
5980
5981 if (found) {
5982 if (raw_nparam < found->nparam_min ||
5983 (raw_nparam > found->nparam_max && !found->plus)) {
5984 nasm_warn(WARN_MACRO_PARAMS_LEGACY,
5985 "improperly calling multi-line macro `%s' with %d parameters",
5986 found->name, raw_nparam);
5987 } else if (comma) {
5988 nasm_warn(WARN_MACRO_PARAMS_LEGACY,
5989 "dropping trailing empty parameter in call to multi-line macro `%s'", found->name);
5990 }
5991 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005992 }
5993
5994 /*
5995 * After all that, we didn't find one with the right number of
5996 * parameters. Issue a warning, and fail to expand the macro.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005997 *!
5998 *!macro-params-multi [on] multi-line macro calls with wrong parameter count
5999 *! warns about \i{multi-line macros} being invoked
6000 *! with the wrong number of parameters. See \k{mlmacover} for an
6001 *! example of why you might want to disable this warning.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006002 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07006003 if (found)
6004 return found;
6005
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006006 nasm_warn(WARN_MACRO_PARAMS_MULTI,
6007 "multi-line macro `%s' exists, but not taking %d parameter%s",
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07006008 finding, nparam, (nparam == 1) ? "" : "s");
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07006009 nasm_free(*paramsp);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006010 return NULL;
6011}
6012
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006013
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006014#if 0
6015
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006016/*
6017 * Save MMacro invocation specific fields in
6018 * preparation for a recursive macro expansion
6019 */
6020static void push_mmacro(MMacro *m)
6021{
6022 MMacroInvocation *i;
6023
6024 i = nasm_malloc(sizeof(MMacroInvocation));
6025 i->prev = m->prev;
6026 i->params = m->params;
6027 i->iline = m->iline;
6028 i->nparam = m->nparam;
6029 i->rotate = m->rotate;
6030 i->paramlen = m->paramlen;
6031 i->unique = m->unique;
6032 i->condcnt = m->condcnt;
6033 m->prev = i;
6034}
6035
6036
6037/*
6038 * Restore MMacro invocation specific fields that were
6039 * saved during a previous recursive macro expansion
6040 */
6041static void pop_mmacro(MMacro *m)
6042{
6043 MMacroInvocation *i;
6044
6045 if (m->prev) {
6046 i = m->prev;
6047 m->prev = i->prev;
6048 m->params = i->params;
6049 m->iline = i->iline;
6050 m->nparam = i->nparam;
6051 m->rotate = i->rotate;
6052 m->paramlen = i->paramlen;
6053 m->unique = i->unique;
6054 m->condcnt = i->condcnt;
6055 nasm_free(i);
6056 }
6057}
6058
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006059#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006060
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006061/*
H. Peter Anvin (Intel)ffe89dd2019-08-20 16:06:36 -07006062 * List an mmacro call with arguments (-Lm option)
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07006063 */
6064static void list_mmacro_call(const MMacro *m)
6065{
6066 const char prefix[] = " ;;; [macro] ";
6067 size_t namelen, size;
6068 char *buf, *p;
6069 unsigned int i;
6070 const Token *t;
6071
6072 namelen = strlen(m->iname);
6073 size = namelen + sizeof(prefix); /* Includes final null (from prefix) */
6074
6075 for (i = 1; i <= m->nparam; i++) {
6076 int j = 0;
6077 size += 3; /* Braces and space/comma */
6078 list_for_each(t, m->params[i]) {
6079 if (j++ >= m->paramlen[i])
6080 break;
6081 size += (t->type == TOK_WHITESPACE) ? 1 : t->len;
6082 }
6083 }
6084
6085 buf = p = nasm_malloc(size);
6086 p = mempcpy(p, prefix, sizeof(prefix) - 1);
6087 p = mempcpy(p, m->iname, namelen);
6088 *p++ = ' ';
6089
6090 for (i = 1; i <= m->nparam; i++) {
6091 int j = 0;
6092 *p++ = '{';
6093 list_for_each(t, m->params[i]) {
6094 if (j++ >= m->paramlen[i])
6095 break;
H. Peter Anvin8571f062019-09-23 16:40:03 -07006096 p = mempcpy(p, tok_text(t), t->len);
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07006097 }
6098 *p++ = '}';
6099 *p++ = ',';
6100 }
6101
6102 *--p = '\0'; /* Replace last delimeter with null */
6103 lfmt->line(LIST_MACRO, -1, buf);
6104 nasm_free(buf);
6105}
6106
6107/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006108 * Expand the multi-line macro call made by the given line, if
6109 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006110 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006111 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006112static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006113{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006114 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006115 Token *label = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006116 bool dont_prepend = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006117 Token **params, *t, *tt;
6118 MMacro *m;
6119 Line *l, *ll;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07006120 int i, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07006121 const char *mname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006122 int nparam = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006123
6124 t = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006125 t = skip_white(t);
6126 /* if (!tok_type(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07006127 if (!tok_type(t, TOK_ID) && !tok_type(t, TOK_LOCAL_MACRO))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006128 return 0;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006129 m = is_mmacro(t, &nparam, &params);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006130 if (m) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006131 mname = tok_text(t);
H. Peter Anvinc751e862008-06-09 10:18:45 -07006132 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006133 Token *last;
6134 /*
6135 * We have an id which isn't a macro call. We'll assume
6136 * it might be a label; we'll also check to see if a
6137 * colon follows it. Then, if there's another id after
6138 * that lot, we'll check it again for macro-hood.
6139 */
6140 label = last = t;
6141 t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006142 if (tok_white(t))
H. Peter Anvine2c80182005-01-15 22:15:51 +00006143 last = t, t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006144 if (tok_is(t, ':')) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006145 dont_prepend = true;
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_white(t))
H. Peter Anvine2c80182005-01-15 22:15:51 +00006148 last = t, t = t->next;
6149 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006150 if (!tok_type(t, TOK_ID) || !(m = is_mmacro(t, &nparam, &params)))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006151 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006152 last->next = NULL;
H. Peter Anvin8571f062019-09-23 16:40:03 -07006153 mname = tok_text(t);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006154 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006155 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006156
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07006157 if (unlikely(mmacro_deadman.total >= nasm_limit[LIMIT_MMACROS] ||
6158 mmacro_deadman.levels >= nasm_limit[LIMIT_MACRO_LEVELS])) {
6159 if (!mmacro_deadman.triggered) {
6160 nasm_nonfatal("interminable multiline macro recursion");
6161 mmacro_deadman.triggered = true;
6162 }
6163 return 0;
6164 }
6165
6166 mmacro_deadman.total++;
6167 mmacro_deadman.levels++;
6168
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006169 /*
6170 * Fix up the parameters: this involves stripping leading and
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07006171 * trailing whitespace and stripping braces if they are present.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006172 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006173 nasm_newn(paramlen, nparam+1);
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07006174
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006175 for (i = 1; (t = params[i]); i++) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006176 bool braced = false;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08006177 int brace = 0;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006178 int white = 0;
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07006179 bool comma = !m->plus || i < nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006180
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006181 t = skip_white(t);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006182 if (tok_is(t, '{')) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006183 t = t->next;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006184 brace = 1;
6185 braced = true;
6186 comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006187 }
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006188
6189 params[i] = t;
6190 for (; t; t = t->next) {
6191 if (tok_white(t)) {
6192 white++;
6193 continue;
6194 }
6195
6196 if (t->type == TOK_OTHER && t->len == 1) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006197 switch (t->text.a[0]) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006198 case ',':
6199 if (comma && !brace)
6200 goto endparam;
6201 break;
6202
6203 case '{':
6204 brace++;
6205 break;
6206
6207 case '}':
6208 brace--;
6209 if (braced && !brace) {
6210 paramlen[i] += white;
6211 goto endparam;
6212 }
6213 break;
6214
6215 default:
6216 break;
6217 }
6218 }
6219
6220 paramlen[i] += white + 1;
6221 white = 0;
6222 }
6223 endparam:
6224 ;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006225 }
6226
6227 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006228 * OK, we have a MMacro structure together with a set of
6229 * parameters. We must now go through the expansion and push
6230 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00006231 * parameter tokens and macro-local tokens doesn't get done
6232 * until the single-line macro substitution process; this is
6233 * because delaying them allows us to change the semantics
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006234 * later through %rotate and give the right semantics for
6235 * nested mmacros.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006236 *
6237 * First, push an end marker on to istk->expansion, mark this
6238 * macro as in progress, and set up its invocation-specific
6239 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006240 */
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07006241 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006242 ll->next = istk->expansion;
6243 ll->finishes = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006244 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006245
6246 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006247 * Save the previous MMacro expansion in the case of
6248 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006249 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006250#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006251 if (m->max_depth && m->in_progress)
6252 push_mmacro(m);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006253#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006254
6255 m->in_progress ++;
6256 m->params = params;
6257 m->iline = tline;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006258 m->iname = nasm_strdup(mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006259 m->nparam = nparam;
6260 m->rotate = 0;
6261 m->paramlen = paramlen;
6262 m->unique = unique++;
6263 m->lineno = 0;
6264 m->condcnt = 0;
6265
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006266 m->mstk = istk->mstk;
6267 istk->mstk.mstk = istk->mstk.mmac = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006268
6269 list_for_each(l, m->expansion) {
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07006270 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006271 ll->next = istk->expansion;
6272 istk->expansion = ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006273 ll->first = dup_tlist(l->first, NULL);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006274 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006275
6276 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006277 * If we had a label, and this macro definition does not include
6278 * a %00, push it on as the first line of, ot
H. Peter Anvineba20a72002-04-30 20:53:55 +00006279 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006280 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006281 if (label) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006282 /*
6283 * We had a label. If this macro contains an %00 parameter,
6284 * save the value as a special parameter (which is what it
6285 * is), otherwise push it as the first line of the macro
6286 * expansion.
6287 */
6288 if (m->capture_label) {
6289 params[0] = dup_Token(NULL, label);
6290 paramlen[0] = 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006291 free_tlist(startline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006292 } else {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006293 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006294 ll->finishes = NULL;
6295 ll->next = istk->expansion;
6296 istk->expansion = ll;
6297 ll->first = startline;
6298 if (!dont_prepend) {
6299 while (label->next)
6300 label = label->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07006301 label->next = tt = make_tok_char(NULL, ':');
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006302 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006303 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00006304 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006305
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07006306 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006307
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07006308 if (list_option('m') && !m->nolist)
6309 list_mmacro_call(m);
6310
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006311 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006312}
6313
H. Peter Anvin130736c2016-02-17 20:27:41 -08006314/*
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006315 * This function decides if an error message should be suppressed.
6316 * It will never be called with a severity level of ERR_FATAL or
6317 * higher.
H. Peter Anvin130736c2016-02-17 20:27:41 -08006318 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006319static bool pp_suppress_error(errflags severity)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02006320{
H. Peter Anvin130736c2016-02-17 20:27:41 -08006321 /*
6322 * If we're in a dead branch of IF or something like it, ignore the error.
6323 * However, because %else etc are evaluated in the state context
6324 * of the previous branch, errors might get lost:
6325 * %if 0 ... %else trailing garbage ... %endif
6326 * So %else etc should set the ERR_PP_PRECOND flag.
6327 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006328 if (istk && istk->conds &&
H. Peter Anvin130736c2016-02-17 20:27:41 -08006329 ((severity & ERR_PP_PRECOND) ?
6330 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07006331 !emitting(istk->conds->state)))
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006332 return true;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02006333
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006334 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00006335}
6336
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006337static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006338stdmac_file(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006339{
6340 (void)s;
6341 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006342 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006343
H. Peter Anvin8571f062019-09-23 16:40:03 -07006344 return make_tok_qstr(NULL, src_get_fname());
H. Peter Anvin8b262472019-02-26 14:00:54 -08006345}
6346
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006347static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006348stdmac_line(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006349{
6350 (void)s;
6351 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006352 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006353
H. Peter Anvin8571f062019-09-23 16:40:03 -07006354 return make_tok_num(NULL, src_get_linnum());
H. Peter Anvin8b262472019-02-26 14:00:54 -08006355}
6356
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006357static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006358stdmac_bits(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006359{
6360 (void)s;
6361 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006362 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006363
H. Peter Anvin8571f062019-09-23 16:40:03 -07006364 return make_tok_num(NULL, globalbits);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006365}
6366
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006367static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006368stdmac_ptr(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006369{
H. Peter Anvin8b262472019-02-26 14:00:54 -08006370 (void)s;
6371 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006372 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006373
6374 switch (globalbits) {
6375 case 16:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006376 return new_Token(NULL, TOK_ID, "word", 4);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006377 case 32:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006378 return new_Token(NULL, TOK_ID, "dword", 5);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006379 case 64:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006380 return new_Token(NULL, TOK_ID, "qword", 5);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006381 default:
6382 panic();
6383 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08006384}
6385
H. Peter Anvin8b262472019-02-26 14:00:54 -08006386/* Add magic standard macros */
6387struct magic_macros {
6388 const char *name;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006389 int nparam;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006390 ExpandSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006391};
6392static const struct magic_macros magic_macros[] =
6393{
H. Peter Anvind2354082019-08-27 16:38:48 -07006394 { "__?FILE?__", 0, stdmac_file },
6395 { "__?LINE?__", 0, stdmac_line },
6396 { "__?BITS?__", 0, stdmac_bits },
6397 { "__?PTR?__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08006398 { NULL, 0, NULL }
6399};
6400
6401static void pp_add_magic_stdmac(void)
6402{
6403 const struct magic_macros *m;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006404 SMacro tmpl;
6405
6406 nasm_zero(tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006407
6408 for (m = magic_macros; m->name; m++) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006409 tmpl.nparam = m->nparam;
6410 tmpl.expand = m->func;
6411 define_smacro(m->name, true, NULL, &tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006412 }
6413}
6414
H. Peter Anvin734b1882002-04-30 21:01:08 +00006415static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006416pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006417{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006418 int apass;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006419 struct Include *inc;
H. Peter Anvin7383b402008-09-24 10:20:40 -07006420
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006421 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006422 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07006423 nested_mac_count = 0;
6424 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07006425 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006426 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07006427 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006428 pp_mode = mode;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07006429
6430 /* Reset options to default */
6431 nasm_zero(ppopt);
H. Peter Anvinf7606612016-07-13 14:23:48 -07006432
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07006433 if (!use_loaded)
6434 use_loaded = nasm_malloc(use_package_count * sizeof(bool));
6435 memset(use_loaded, 0, use_package_count * sizeof(bool));
6436
H. Peter Anvin6686de22019-08-10 05:33:14 -07006437 /* First set up the top level input file */
6438 nasm_new(istk);
6439 istk->fp = nasm_open_read(file, NF_TEXT);
6440 src_set(0, file);
6441 istk->lineinc = 1;
6442 if (!istk->fp)
6443 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
6444
6445 strlist_add(deplist, file);
6446
6447 /*
6448 * Set up the stdmac packages as a virtual include file,
6449 * indicated by a null file pointer.
6450 */
6451 nasm_new(inc);
6452 inc->next = istk;
6453 inc->fname = src_set_fname(NULL);
6454 inc->nolist = !list_option('b');
6455 istk = inc;
6456 lfmt->uplevel(LIST_INCLUDE, 0);
6457
H. Peter Anvin8b262472019-02-26 14:00:54 -08006458 pp_add_magic_stdmac();
6459
H. Peter Anvinf7606612016-07-13 14:23:48 -07006460 if (tasm_compatible_mode)
6461 pp_add_stdmac(nasm_stdmac_tasm);
6462
6463 pp_add_stdmac(nasm_stdmac_nasm);
6464 pp_add_stdmac(nasm_stdmac_version);
6465
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006466 if (extrastdmac)
6467 pp_add_stdmac(extrastdmac);
6468
H. Peter Anvinf7606612016-07-13 14:23:48 -07006469 stdmacpos = stdmacros[0];
6470 stdmacnext = &stdmacros[1];
6471
H. Peter Anvind2456592008-06-19 15:04:18 -07006472 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006473
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006474 /*
H. Peter Anvind2354082019-08-27 16:38:48 -07006475 * Define the __?PASS?__ macro. This is defined here unlike all the
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07006476 * other builtins, because it is special -- it varies between
6477 * passes -- but there is really no particular reason to make it
6478 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006479 *
6480 * 0 = dependencies only
6481 * 1 = preparatory passes
6482 * 2 = final pass
6483 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006484 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006485 switch (mode) {
6486 case PP_NORMAL:
6487 apass = pass_final() ? 2 : 1;
6488 break;
6489 case PP_DEPS:
6490 apass = 0;
6491 break;
6492 case PP_PREPROC:
6493 apass = 3;
6494 break;
6495 default:
6496 panic();
6497 }
6498
H. Peter Anvin8571f062019-09-23 16:40:03 -07006499 define_smacro("__?PASS?__", true, make_tok_num(NULL, apass), NULL);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006500}
6501
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006502static void pp_init(void)
6503{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006504}
6505
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006506/*
6507 * Get a line of tokens. If we popped the macro expansion/include stack,
6508 * we return a pointer to the dummy token tok_pop; at that point if
6509 * istk is NULL then we have reached end of input;
6510 */
6511static Token tok_pop; /* Dummy token placeholder */
6512
6513static Token *pp_tokline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006514{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006515 while (true) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006516 Line *l = istk->expansion;
6517 Token *tline = NULL;
6518 Token *dtline;
6519
H. Peter Anvine2c80182005-01-15 22:15:51 +00006520 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006521 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00006522 * buffer or from the input file.
6523 */
6524 tline = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006525 while (l && l->finishes) {
6526 MMacro *fm = l->finishes;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006527
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006528 if (!fm->name && fm->in_progress > 1) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006529 /*
6530 * This is a macro-end marker for a macro with no
6531 * name, which means it's not really a macro at all
6532 * but a %rep block, and the `in_progress' field is
6533 * more than 1, meaning that we still need to
6534 * repeat. (1 means the natural last repetition; 0
6535 * means termination by %exitrep.) We have
6536 * therefore expanded up to the %endrep, and must
6537 * push the whole block on to the expansion buffer
6538 * again. We don't bother to remove the macro-end
6539 * marker: we'd only have to generate another one
6540 * if we did.
6541 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006542 fm->in_progress--;
6543 list_for_each(l, fm->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006544 Token *t, *tt, **tail;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006545 Line *ll;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006546
Chang S. Baebec812f2020-02-07 15:49:38 -08006547 istk->mstk.mstk->lineno = 0;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006548 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006549 ll->next = istk->expansion;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006550 tail = &ll->first;
6551
6552 list_for_each(t, l->first) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006553 if (t->len) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006554 tt = *tail = dup_Token(NULL, t);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006555 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006556 }
6557 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006558 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006559 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006560 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006561 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006562 MMacro *m = istk->mstk.mstk;
6563
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006564 /*
6565 * Check whether a `%rep' was started and not ended
6566 * within this macro expansion. This can happen and
6567 * should be detected. It's a fatal error because
6568 * I'm too confused to work out how to recover
6569 * sensibly from it.
6570 */
6571 if (defining) {
6572 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07006573 nasm_panic("defining with name in expansion");
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006574 else if (m->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07006575 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006576 " expansion of macro `%s'", m->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006577 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006578
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006579 /*
6580 * FIXME: investigate the relationship at this point between
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006581 * istk->mstk.mstk and fm
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006582 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006583 istk->mstk = m->mstk;
6584 if (m->name) {
6585 /*
6586 * This was a real macro call, not a %rep, and
6587 * therefore the parameter information needs to
6588 * be freed and the iteration count/nesting
6589 * depth adjusted.
6590 */
6591
6592 if (!--mmacro_deadman.levels) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006593 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006594 * If all mmacro processing done,
6595 * clear all counters and the deadman
6596 * message trigger.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006597 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006598 nasm_zero(mmacro_deadman); /* Clear all counters */
Adam Majer91e72402017-07-25 10:42:01 +02006599 }
6600
Adam Majer91e72402017-07-25 10:42:01 +02006601#if 0
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006602 if (m->prev) {
6603 pop_mmacro(m);
6604 fm->in_progress --;
6605 } else
Adam Majer91e72402017-07-25 10:42:01 +02006606#endif
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006607 {
6608 nasm_free(m->params);
6609 free_tlist(m->iline);
6610 nasm_free(m->paramlen);
6611 fm->in_progress = 0;
6612 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006613 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006614
6615 /*
6616 * FIXME It is incorrect to always free_mmacro here.
6617 * It leads to usage-after-free.
6618 *
6619 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
6620 */
6621#if 0
6622 else
6623 free_mmacro(m);
6624#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006625 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006626 istk->expansion = l->next;
6627 nasm_free(l);
6628 lfmt->downlevel(LIST_MACRO);
6629 return &tok_pop;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006630 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006631
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006632 do { /* until we get a line we can use */
6633 char *line;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006634
6635 if (istk->expansion) { /* from a macro expansion */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006636 Line *l = istk->expansion;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006637 int32_t lineno;
6638
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006639 if (istk->mstk.mstk) {
6640 istk->mstk.mstk->lineno++;
6641 if (istk->mstk.mstk->fname)
6642 lineno = istk->mstk.mstk->lineno +
6643 istk->mstk.mstk->xline;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006644 else
6645 lineno = 0; /* Defined at init time or builtin */
6646 } else {
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006647 lineno = src_get_linnum();
H. Peter Anvin6686de22019-08-10 05:33:14 -07006648 }
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006649
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006650 tline = l->first;
6651 istk->expansion = l->next;
6652 nasm_free(l);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006653
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006654 line = detoken(tline, false);
H. Peter Anvin6686de22019-08-10 05:33:14 -07006655 if (!istk->nolist)
6656 lfmt->line(LIST_MACRO, lineno, line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006657 nasm_free(line);
6658 } else if ((line = read_line())) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006659 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006660 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006661 nasm_free(line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006662 } else {
6663 /*
6664 * The current file has ended; work down the istk
6665 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00006666 Include *i = istk;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006667 if (i->fp)
6668 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006669 if (i->conds) {
6670 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07006671 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006672 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00006673 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07006674 if (i->next)
6675 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006676 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08006677 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006678 nasm_free(i);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006679 return &tok_pop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006680 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006681 } while (0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006682
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006683 /*
6684 * We must expand MMacro parameters and MMacro-local labels
6685 * _before_ we plunge into directive processing, to cope
6686 * with things like `%define something %1' such as STRUC
6687 * uses. Unless we're _defining_ a MMacro, in which case
6688 * those tokens should be left alone to go into the
6689 * definition; and unless we're in a non-emitting
6690 * condition, in which case we don't want to meddle with
6691 * anything.
6692 */
H. Peter Anvin (Intel)bacf04a2020-06-08 13:29:06 -07006693 if (!defining &&
6694 !(istk->conds && !emitting(istk->conds->state)) &&
6695 !(istk->mstk.mmac && !istk->mstk.mmac->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006696 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006697 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006698
H. Peter Anvine2c80182005-01-15 22:15:51 +00006699 /*
6700 * Check the line to see if it's a preprocessor directive.
6701 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006702 if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) {
6703 if (dtline)
6704 return dtline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006705 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006706 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006707 * We're defining a multi-line macro. We emit nothing
6708 * at all, and just
6709 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00006710 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006711 MMacro *mmac = defining->dstk.mmac;
6712
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006713 Line *l = nasm_malloc(sizeof(Line));
6714 l->next = defining->expansion;
6715 l->first = tline;
6716 l->finishes = NULL;
6717 defining->expansion = l;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006718
6719 /*
6720 * Remember if this mmacro expansion contains %00:
6721 * if it does, we will have to handle leading labels
6722 * specially.
6723 */
6724 if (mmac) {
6725 const Token *t;
6726 list_for_each(t, tline) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006727 if (!memcmp(t->text.a, "%00", 4))
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006728 mmac->capture_label = true;
6729 }
6730 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006731 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006732 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006733 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00006734 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006735 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00006736 * directive so we keep our place correctly.
6737 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006738 free_tlist(tline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006739 } else if (istk->mstk.mstk && !istk->mstk.mstk->in_progress) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006740 /*
6741 * We're in a %rep block which has been terminated, so
6742 * we're walking through to the %endrep without
6743 * emitting anything. Emit nothing at all, not even a
6744 * blank line: when we emerge from the %rep block we'll
6745 * give a line-number directive so we keep our place
6746 * correctly.
6747 */
6748 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006749 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006750 tline = expand_smacro(tline);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006751 if (!expand_mmacro(tline))
6752 return tline;
6753 }
6754 }
6755}
6756
6757static char *pp_getline(void)
6758{
6759 char *line = NULL;
6760 Token *tline;
6761
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006762 while (true) {
6763 tline = pp_tokline();
6764 if (tline == &tok_pop) {
6765 /*
6766 * We popped the macro/include stack. If istk is empty,
6767 * we are at end of input, otherwise just loop back.
6768 */
6769 if (!istk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006770 break;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006771 } else {
6772 /*
6773 * De-tokenize the line and emit it.
6774 */
6775 line = detoken(tline, true);
6776 free_tlist(tline);
6777 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006778 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006779 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006780
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006781 if (list_option('e') && istk && !istk->nolist && line && line[0]) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006782 char *buf = nasm_strcat(" ;;; ", line);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006783 lfmt->line(LIST_MACRO, -1, buf);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006784 nasm_free(buf);
6785 }
6786
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006787 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006788}
6789
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006790static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006791{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006792 if (defining) {
6793 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006794 nasm_nonfatal("end of file while still defining macro `%s'",
6795 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006796 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006797 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006798 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006799
6800 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006801 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006802 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08006803
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006804 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006805 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07006806 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006807 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006808 Include *i = istk;
6809 istk = istk->next;
6810 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03006811 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006812 }
6813 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006814 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07006815 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006816}
6817
6818static void pp_cleanup_session(void)
6819{
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07006820 nasm_free(use_loaded);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006821 free_llist(predef);
6822 predef = NULL;
6823 delete_Blocks();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006824 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006825}
6826
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006827static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006828{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006829 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006830}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00006831
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006832static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006833{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006834 Token *inc, *space, *name;
6835 Line *l;
6836
H. Peter Anvin734b1882002-04-30 21:01:08 +00006837 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006838 space = new_White(name);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006839 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006840
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006841 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006842 l->next = predef;
6843 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006844 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006845 predef = l;
6846}
6847
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006848static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006849{
6850 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006851 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00006852 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006853
6854 equals = strchr(definition, '=');
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006855 space = new_White(NULL);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006856 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006857 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006858 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00006859 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006860 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006861 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006862
H. Peter Anvin8571f062019-09-23 16:40:03 -07006863 /* We can't predefine a TOK_LOCAL_MACRO for obvious reasons... */
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006864 if (space->next->type != TOK_PREPROC_ID &&
6865 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08006866 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006867
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006868 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006869 l->next = predef;
6870 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006871 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006872 predef = l;
6873}
6874
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006875static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00006876{
6877 Token *def, *space;
6878 Line *l;
6879
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006880 space = new_White(NULL);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006881 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006882 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00006883
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006884 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00006885 l->next = predef;
6886 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006887 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00006888 predef = l;
6889}
6890
H. Peter Anvin05990342018-06-11 13:32:42 -07006891/* Insert an early preprocessor command that doesn't need special handling */
6892static void pp_pre_command(const char *what, char *string)
6893{
6894 char *cmd;
6895 Token *def, *space;
6896 Line *l;
6897
6898 def = tokenize(string);
6899 if (what) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006900 space = new_White(def);
H. Peter Anvin8571f062019-09-23 16:40:03 -07006901 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
6902 def = new_Token(space, TOK_PREPROC_ID, cmd, nasm_last_string_len());
6903 nasm_free(cmd);
H. Peter Anvin05990342018-06-11 13:32:42 -07006904 }
6905
6906 l = nasm_malloc(sizeof(Line));
6907 l->next = predef;
6908 l->first = def;
6909 l->finishes = NULL;
6910 predef = l;
6911}
6912
H. Peter Anvinf7606612016-07-13 14:23:48 -07006913static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006914{
H. Peter Anvinf7606612016-07-13 14:23:48 -07006915 macros_t **mp;
6916
6917 /* Find the end of the list and avoid duplicates */
6918 for (mp = stdmacros; *mp; mp++) {
6919 if (*mp == macros)
6920 return; /* Nothing to do */
6921 }
6922
6923 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
6924
6925 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00006926}
6927
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006928static void pp_extra_stdmac(macros_t *macros)
6929{
6930 extrastdmac = macros;
6931}
6932
H. Peter Anvin8571f062019-09-23 16:40:03 -07006933/* Create a numeric token */
6934static Token *make_tok_num(Token *next, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006935{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04006936 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08006937 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvin8571f062019-09-23 16:40:03 -07006938 return new_Token(next, TOK_NUMBER, numbuf, len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006939}
6940
H. Peter Anvin8571f062019-09-23 16:40:03 -07006941/* Create a quoted string token */
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07006942static Token *make_tok_qstr_len(Token *next, const char *str, size_t len)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006943{
H. Peter Anvin8571f062019-09-23 16:40:03 -07006944 char *p = nasm_quote(str, &len);
6945 return new_Token_free(next, TOK_STRING, p, len);
6946}
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07006947static Token *make_tok_qstr(Token *next, const char *str)
6948{
6949 return make_tok_qstr_len(next, str, strlen(str));
6950}
H. Peter Anvin8571f062019-09-23 16:40:03 -07006951
6952/* Create a single-character operator token */
6953static Token *make_tok_char(Token *next, char op)
6954{
6955 Token *t = new_Token(next, TOK_OTHER, NULL, 1);
6956 t->text.a[0] = op;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006957 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006958}
6959
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006960static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07006961{
6962 if (!m)
6963 return;
6964
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006965 /* We need to print the mstk.mmac list in reverse order */
6966 pp_list_one_macro(m->mstk.mmac, severity);
H. Peter Anvin37368952016-05-09 14:10:32 -07006967
6968 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07006969 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08006970 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07006971 }
6972}
6973
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006974static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006975{
H. Peter Anvinddb29062018-12-11 00:06:29 -08006976 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006977
H. Peter Anvinddb29062018-12-11 00:06:29 -08006978 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
6979 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006980
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03006981 if (istk)
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006982 pp_list_one_macro(istk->mstk.mmac, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006983
H. Peter Anvinddb29062018-12-11 00:06:29 -08006984 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006985}
6986
H. Peter Anvine7469712016-02-18 02:20:59 -08006987const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006988 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006989 pp_reset,
6990 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006991 pp_cleanup_pass,
6992 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006993 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006994 pp_pre_define,
6995 pp_pre_undefine,
6996 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07006997 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006998 pp_include_path,
6999 pp_error_list_macros,
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07007000 pp_suppress_error
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00007001};