blob: fd06ba892b59a06f91965c1e48fc48133cd2623b [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{
H. Peter Anvin8571f062019-09-23 16:40:03 -0700368 if (t->len > INLINE_TEXT)
369 nasm_free(t->text.p.ptr);
370
H. Peter Anvin (Intel)00335e42020-06-14 19:49:19 -0700371 nasm_zero(t->text);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700372
H. Peter Anvin (Intel)00335e42020-06-14 19:49:19 -0700373 t->len = len = tok_check_len(len);
H. Peter Anvin (Intel)4ed23c82020-06-14 19:55:49 -0700374 if (len > INLINE_TEXT) {
375 char *textp;
376
377 t->text.p.ptr = textp = nasm_malloc(len+1);
378 memcpy(textp, text, len);
379 textp[len] = '\0';
380 } else {
381 /* Null-terminated due to nasm_zero() above */
382 t->len = len;
383 memcpy(t->text.a, text, len);
384 }
385
H. Peter Anvin8571f062019-09-23 16:40:03 -0700386 return t;
387}
388
389/*
390 * Set the text field to the existing pre-allocated string, either
391 * taking over or freeing the allocation in the process.
392 */
H. Peter Anvin (Intel)00335e42020-06-14 19:49:19 -0700393static Token *set_text_free(struct Token *t, char *text, size_t len)
H. Peter Anvin8571f062019-09-23 16:40:03 -0700394{
395 if (t->len > INLINE_TEXT)
396 nasm_free(t->text.p.ptr);
397
H. Peter Anvin (Intel)00335e42020-06-14 19:49:19 -0700398 nasm_zero(t->text);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700399
H. Peter Anvin (Intel)00335e42020-06-14 19:49:19 -0700400 t->len = len = tok_check_len(len);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700401 if (len > INLINE_TEXT) {
402 t->text.p.ptr = text;
H. Peter Anvin (Intel)00335e42020-06-14 19:49:19 -0700403 text[len] = '\0';
H. Peter Anvin8571f062019-09-23 16:40:03 -0700404 } else {
H. Peter Anvin (Intel)4ed23c82020-06-14 19:55:49 -0700405 /* Null-terminated due to nasm_zero() above */
H. Peter Anvin (Intel)00335e42020-06-14 19:49:19 -0700406 memcpy(t->text.a, text, len);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700407 nasm_free(text);
408 }
409
410 return t;
411}
412
413/*
414 * Allocate a new buffer containing a copy of the text field
415 * of the token.
416 */
417static char *dup_text(const struct Token *t)
418{
419 size_t size = t->len + 1;
420 char *p = nasm_malloc(size);
421
422 return memcpy(p, tok_text(t), size);
423}
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000424
425/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800426 * Multi-line macro definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000427 * these, which is essentially a container to allow several linked
428 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700429 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000430 * Note that in this module, linked lists are treated as stacks
431 * wherever possible. For this reason, Lines are _pushed_ on to the
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800432 * `expansion' field in MMacro structures, so that the linked list,
433 * if walked, would give the macro lines in reverse order; this
434 * means that we can walk the list when expanding a macro, and thus
435 * push the lines on to the `expansion' field in _istk_ in reverse
436 * order (so that when popped back off they are in the right
437 * order). It may seem cockeyed, and it relies on my design having
438 * an even number of steps in, but it works...
439 *
440 * Some of these structures, rather than being actual lines, are
441 * markers delimiting the end of the expansion of a given macro.
442 * This is for use in the cycle-tracking and %rep-handling code.
443 * Such structures have `finishes' non-NULL, and `first' NULL. All
444 * others have `finishes' NULL, but `first' may still be NULL if
445 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000446 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000447struct Line {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800448 Line *next;
449 MMacro *finishes;
450 Token *first;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500451};
452
453/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000454 * To handle an arbitrary level of file inclusion, we maintain a
455 * stack (ie linked list) of these things.
456 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000457struct Include {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800458 Include *next;
459 FILE *fp;
460 Cond *conds;
461 Line *expansion;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700462 const char *fname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700463 struct mstk mstk;
H. Peter Anvin6686de22019-08-10 05:33:14 -0700464 int lineno, lineinc;
465 bool nolist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000466};
467
468/*
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700469 * File real name hash, so we don't have to re-search the include
470 * path for every pass (and potentially more than that if a file
471 * is used more than once.)
472 */
473struct hash_table FileHash;
474
475/*
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -0700476 * Counters to trap on insane macro recursion or processing.
477 * Note: for smacros these count *down*, for mmacros they count *up*.
478 */
479struct deadman {
480 int64_t total; /* Total number of macros/tokens */
481 int64_t levels; /* Descent depth across all macros */
482 bool triggered; /* Already triggered, no need for error msg */
483};
484
485static struct deadman smacro_deadman, mmacro_deadman;
486
487/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000488 * Conditional assembly: we maintain a separate stack of these for
489 * each level of file inclusion. (The only reason we keep the
490 * stacks separate is to ensure that a stray `%endif' in a file
491 * included from within the true branch of a `%if' won't terminate
492 * it and cause confusion: instead, rightly, it'll cause an error.)
493 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -0700494enum cond_state {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000495 /*
496 * These states are for use just after %if or %elif: IF_TRUE
497 * means the condition has evaluated to truth so we are
498 * currently emitting, whereas IF_FALSE means we are not
499 * currently emitting but will start doing so if a %else comes
500 * up. In these states, all directives are admissible: %elif,
501 * %else and %endif. (And of course %if.)
502 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800503 COND_IF_TRUE, COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000504 /*
505 * These states come up after a %else: ELSE_TRUE means we're
506 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
507 * any %elif or %else will cause an error.
508 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800509 COND_ELSE_TRUE, COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000510 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200511 * These states mean that we're not emitting now, and also that
512 * nothing until %endif will be emitted at all. COND_DONE is
513 * used when we've had our moment of emission
514 * and have now started seeing %elifs. COND_NEVER is used when
515 * the condition construct in question is contained within a
516 * non-emitting branch of a larger condition construct,
517 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000518 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800519 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000520};
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -0700521struct Cond {
522 Cond *next;
523 enum cond_state state;
524};
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800525#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000526
H. Peter Anvin70653092007-10-19 14:42:29 -0700527/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000528 * These defines are used as the possible return values for do_directive
529 */
530#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300531#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000532
Keith Kanios852f1ee2009-07-12 00:19:55 -0500533/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000534 * Condition codes. Note that we use c_ prefix not C_ because C_ is
535 * used in nasm.h for the "real" condition codes. At _this_ level,
536 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
537 * ones, so we need a different enum...
538 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700539static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000540 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
541 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000542 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000543};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700544enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000545 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
546 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 -0700547 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
548 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000549};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700550static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000551 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
552 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 +0000553 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000554};
555
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800556/*
557 * Directive names.
558 */
559/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
560static int is_condition(enum preproc_token arg)
561{
562 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
563}
564
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000565/* For TASM compatibility we need to be able to recognise TASM compatible
566 * conditional compilation directives. Using the NASM pre-processor does
567 * not work, so we look for them specifically from the following list and
568 * then jam in the equivalent NASM directive into the input stream.
569 */
570
H. Peter Anvine2c80182005-01-15 22:15:51 +0000571enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000572 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
573 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
574};
575
H. Peter Anvin476d2862007-10-02 22:04:15 -0700576static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000577 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
578 "ifndef", "include", "local"
579};
580
581static int StackSize = 4;
H. Peter Anvin6c8b2be2016-05-24 23:46:50 -0700582static const char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000583static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800584static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000585
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000586static Context *cstk;
587static Include *istk;
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300588static const struct strlist *ipath_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000589
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300590static struct strlist *deplist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000591
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300592static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000593
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800594static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700595static bool do_predef;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800596static enum preproc_mode pp_mode;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000597
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000598/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800599 * The current set of multi-line macros we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000600 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800601static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000602
603/*
604 * The current set of single-line macros we have defined.
605 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700606static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000607
608/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800609 * The multi-line macro we are currently defining, or the %rep
610 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000611 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800612static MMacro *defining;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000613
Charles Crayned4200be2008-07-12 16:42:33 -0700614static uint64_t nested_mac_count;
615static uint64_t nested_rep_count;
616
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000617/*
618 * The number of macro parameters to allocate space for at a time.
619 */
620#define PARAM_DELTA 16
621
622/*
H. Peter Anvinf7606612016-07-13 14:23:48 -0700623 * The standard macro set: defined in macros.c in a set of arrays.
624 * This gives our position in any macro set, while we are processing it.
625 * The stdmacset is an array of such macro sets.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000626 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700627static macros_t *stdmacpos;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700628static macros_t **stdmacnext;
629static macros_t *stdmacros[8];
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +0300630static macros_t *extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000631
632/*
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -0700633 * Map of which %use packages have been loaded
634 */
635static bool *use_loaded;
636
637/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000638 * Forward declarations.
639 */
H. Peter Anvinf7606612016-07-13 14:23:48 -0700640static void pp_add_stdmac(macros_t *macros);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000641static Token *expand_mmac_params(Token * tline);
642static Token *expand_smacro(Token * tline);
643static Token *expand_id(Token * tline);
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +0400644static Context *get_ctx(const char *name, const char **namep);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700645static Token *make_tok_num(Token *next, int64_t val);
646static Token *make_tok_qstr(Token *next, const char *str);
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -0700647static Token *make_tok_qstr_len(Token *next, const char *str, size_t len);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700648static Token *make_tok_char(Token *next, char op);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700649static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700650 const char *text, size_t txtlen);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700651static Token *new_Token_free(Token * next, enum pp_token_type type,
652 char *text, size_t txtlen);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700653static Token *dup_Token(Token *next, const Token *src);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700654static Token *new_White(Token *next);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700655static Token *delete_Token(Token *t);
656static Token *steal_Token(Token *dst, Token *src);
H. Peter Anvindd88aa92019-09-12 19:39:48 -0700657static const struct use_package *
H. Peter Anvin8571f062019-09-23 16:40:03 -0700658get_use_pkg(Token *t, const char *dname, const char **name);
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -0700659static void mark_smac_params(Token *tline, const SMacro *tmpl,
660 enum pp_token_type type);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000661
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700662/* Safe test for token type, false on x == NULL */
663static inline bool tok_type(const Token *x, enum pp_token_type t)
664{
665 return x && x->type == t;
666}
667
668/* Whitespace token? */
669static inline bool tok_white(const Token *x)
670{
671 return tok_type(x, TOK_WHITESPACE);
672}
673
674/* Skip past any whitespace */
675static inline Token *skip_white(Token *x)
676{
677 while (tok_white(x))
678 x = x->next;
679
680 return x;
681}
682
683/* Delete any whitespace */
684static Token *zap_white(Token *x)
685{
686 while (tok_white(x))
687 x = delete_Token(x);
688
689 return x;
690}
691
H. Peter Anvin8571f062019-09-23 16:40:03 -0700692/*
693 * Single special character tests. The use of & rather than && is intentional; it
694 * tells the compiler that it is safe to access text.a[1] unconditionally; hopefully
695 * a smart compiler should turn it into a 16-bit memory reference.
696 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700697static inline bool tok_is(const Token *x, char c)
698{
H. Peter Anvin8571f062019-09-23 16:40:03 -0700699 return x && ((x->text.a[0] == c) & !x->text.a[1]);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700700}
701
702/* True if any other kind of token that "c", but not NULL */
703static inline bool tok_isnt(const Token *x, char c)
704{
H. Peter Anvin8571f062019-09-23 16:40:03 -0700705 return x && !((x->text.a[0] == c) & !x->text.a[1]);
706}
707
708/*
709 * Unquote a token if it is a string, and set its type to
710 * TOK_INTERNAL_STRING.
711 */
712static const char *unquote_token(Token *t)
713{
714 if (t->type != TOK_STRING)
715 return tok_text(t);
716
717 t->type = TOK_INTERNAL_STRING;
718
719 if (t->len > INLINE_TEXT) {
720 char *p = t->text.p.ptr;
721
722 t->len = nasm_unquote(p, NULL);
723
724 if (t->len <= INLINE_TEXT) {
725 nasm_zero(t->text.a);
726 memcpy(t->text.a, p, t->len);
727 nasm_free(p);
728 return t->text.a;
729 } else {
730 return p;
731 }
732 } else {
733 t->len = nasm_unquote(t->text.a, NULL);
734 return t->text.a;
735 }
736}
737
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700738/*
739 * Same as unquote_token(), but error out if the resulting string
740 * contains unacceptable control characters.
741 */
H. Peter Anvin8571f062019-09-23 16:40:03 -0700742static const char *unquote_token_cstr(Token *t)
743{
744 if (t->type != TOK_STRING)
745 return tok_text(t);
746
747 t->type = TOK_INTERNAL_STRING;
748
749 if (t->len > INLINE_TEXT) {
750 char *p = t->text.p.ptr;
751
752 t->len = nasm_unquote_cstr(p, NULL);
753
754 if (t->len <= INLINE_TEXT) {
755 nasm_zero(t->text.a);
756 memcpy(t->text.a, p, t->len);
757 nasm_free(p);
758 return t->text.a;
759 } else {
760 return p;
761 }
762 } else {
763 t->len = nasm_unquote_cstr(t->text.a, NULL);
764 return t->text.a;
765 }
766}
767
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700768/*
769 * Convert a TOK_INTERNAL_STRING token to a quoted
770 * TOK_STRING tokens.
771 */
772static Token *quote_any_token(Token *t);
773static inline Token *quote_token(Token *t)
774{
775 if (likely(!tok_is(t, TOK_INTERNAL_STRING)))
776 return t;
777
778 return quote_any_token(t);
779}
780
781/*
782 * Convert *any* kind of token to a quoted
783 * TOK_STRING token.
784 */
785static Token *quote_any_token(Token *t)
H. Peter Anvin8571f062019-09-23 16:40:03 -0700786{
787 size_t len;
788 char *p;
789
790 p = nasm_quote(tok_text(t), &len);
791 t->type = TOK_STRING;
792 return set_text_free(t, p, len);
793}
794
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400795/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700796 * In-place reverse a list of tokens.
797 */
798static Token *reverse_tokens(Token *t)
799{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800800 Token *prev = NULL;
801 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700802
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800803 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400804 next = t->next;
805 t->next = prev;
806 prev = t;
807 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800808 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700809
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800810 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700811}
812
813/*
H. Peter Anvin8571f062019-09-23 16:40:03 -0700814 * getenv() variant operating on an input token
815 */
816static const char *pp_getenv(const Token *t, bool warn)
817{
818 const char *txt = tok_text(t);
819 const char *v;
820 char *buf = NULL;
821 bool is_string = false;
822
823 if (!t)
824 return NULL;
825
826 switch (t->type) {
827 case TOK_ENVIRON:
828 txt += 2; /* Skip leading %! */
829 is_string = nasm_isquote(*txt);
830 break;
831
832 case TOK_STRING:
833 is_string = true;
834 break;
835
836 case TOK_INTERNAL_STRING:
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700837 case TOK_NAKED_STRING:
H. Peter Anvin8571f062019-09-23 16:40:03 -0700838 case TOK_ID:
839 is_string = false;
840 break;
841
842 default:
843 return NULL;
844 }
845
846 if (is_string) {
847 buf = nasm_strdup(txt);
848 nasm_unquote_cstr(buf, NULL);
849 txt = buf;
850 }
851
852 v = getenv(txt);
853 if (warn && !v) {
854 /*!
855 *!environment [on] nonexistent environment variable
856 *! warns if a nonexistent environment variable
857 *! is accessed using the \c{%!} preprocessor
858 *! construct (see \k{getenv}.) Such environment
859 *! variables are treated as empty (with this
860 *! warning issued) starting in NASM 2.15;
861 *! earlier versions of NASM would treat this as
862 *! an error.
863 */
864 nasm_warn(WARN_ENVIRONMENT, "nonexistent environment variable `%s'", txt);
865 v = "";
866 }
867
868 if (buf)
869 nasm_free(buf);
870
871 return v;
872}
873
874/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300875 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000876 * front of them. We do it here because I could not find any other
877 * place to do it for the moment, and it is a hack (ideally it would
878 * be nice to be able to use the NASM pre-processor to do it).
879 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000880static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000881{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000882 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400883 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000884
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400885 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000886
887 /* Binary search for the directive name */
888 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400889 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400890 q = nasm_skip_word(p);
891 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000892 if (len) {
893 oldchar = p[len];
894 p[len] = 0;
895 while (j - i > 1) {
896 k = (j + i) / 2;
897 m = nasm_stricmp(p, tasm_directives[k]);
898 if (m == 0) {
899 /* We have found a directive, so jam a % in front of it
900 * so that NASM will then recognise it as one if it's own.
901 */
902 p[len] = oldchar;
903 len = strlen(p);
904 oldline = line;
905 line = nasm_malloc(len + 2);
906 line[0] = '%';
907 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700908 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300909 * NASM does not recognise IFDIFI, so we convert
910 * it to %if 0. This is not used in NASM
911 * compatible code, but does need to parse for the
912 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000913 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700914 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000915 } else {
916 memcpy(line + 1, p, len + 1);
917 }
918 nasm_free(oldline);
919 return line;
920 } else if (m < 0) {
921 j = k;
922 } else
923 i = k;
924 }
925 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000926 }
927 return line;
928}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000929
H. Peter Anvin76690a12002-04-30 20:52:49 +0000930/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000931 * The pre-preprocessing stage... This function translates line
932 * number indications as they emerge from GNU cpp (`# lineno "file"
933 * flags') into NASM preprocessor line number indications (`%line
934 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000935 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000936static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000937{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000938 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000939 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000940
H. Peter Anvine2c80182005-01-15 22:15:51 +0000941 if (line[0] == '#' && line[1] == ' ') {
942 oldline = line;
943 fname = oldline + 2;
944 lineno = atoi(fname);
945 fname += strspn(fname, "0123456789 ");
946 if (*fname == '"')
947 fname++;
948 fnlen = strcspn(fname, "\"");
949 line = nasm_malloc(20 + fnlen);
950 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
951 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000952 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000953 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000954 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000955 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000956}
957
958/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000959 * Free a linked list of tokens.
960 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000961static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000962{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400963 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000964 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000965}
966
967/*
968 * Free a linked list of lines.
969 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000970static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000971{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400972 Line *l, *tmp;
973 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000974 free_tlist(l->first);
975 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000976 }
977}
978
979/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700980 * Free an array of linked lists of tokens
981 */
982static void free_tlist_array(Token **array, size_t nlists)
983{
984 Token **listp = array;
985
986 while (nlists--)
987 free_tlist(*listp++);
988
989 nasm_free(array);
990}
991
992/*
993 * Duplicate a linked list of tokens.
994 */
995static Token *dup_tlist(const Token *list, Token ***tailp)
996{
997 Token *newlist = NULL;
998 Token **tailpp = &newlist;
999 const Token *t;
1000
1001 list_for_each(t, list) {
1002 Token *nt;
1003 *tailpp = nt = dup_Token(NULL, t);
1004 tailpp = &nt->next;
1005 }
1006
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001007 if (tailp) {
1008 **tailp = newlist;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07001009 *tailp = tailpp;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001010 }
1011
1012 return newlist;
1013}
1014
1015/*
1016 * Duplicate a linked list of tokens with a maximum count
1017 */
1018static Token *dup_tlistn(const Token *list, size_t cnt, Token ***tailp)
1019{
1020 Token *newlist = NULL;
1021 Token **tailpp = &newlist;
1022 const Token *t;
1023
1024 list_for_each(t, list) {
1025 Token *nt;
1026 if (!cnt--)
1027 break;
1028 *tailpp = nt = dup_Token(NULL, t);
1029 tailpp = &nt->next;
1030 }
1031
1032 if (tailp) {
1033 **tailp = newlist;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07001034 if (newlist)
1035 *tailp = tailpp;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001036 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07001037
1038 return newlist;
1039}
1040
1041/*
1042 * Duplicate a linked list of tokens in reverse order
1043 */
1044static Token *dup_tlist_reverse(const Token *list, Token *tail)
1045{
1046 const Token *t;
1047
1048 list_for_each(t, list)
1049 tail = dup_Token(tail, t);
1050
1051 return tail;
1052}
1053
1054/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001055 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +00001056 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001057static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001058{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001059 nasm_free(m->name);
1060 free_tlist(m->dlist);
1061 nasm_free(m->defaults);
1062 free_llist(m->expansion);
1063 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001064}
1065
1066/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001067 * Clear or free an SMacro
H. Peter Anvin8b262472019-02-26 14:00:54 -08001068 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001069static void free_smacro_members(SMacro *s)
H. Peter Anvin8b262472019-02-26 14:00:54 -08001070{
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07001071 if (s->params) {
1072 int i;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001073 for (i = 0; i < s->nparam; i++) {
1074 if (s->params[i].name.len > INLINE_TEXT)
1075 nasm_free(s->params[i].name.text.p.ptr);
1076 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07001077 nasm_free(s->params);
1078 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08001079 nasm_free(s->name);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001080 free_tlist(s->expansion);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001081}
1082
1083static void clear_smacro(SMacro *s)
1084{
1085 free_smacro_members(s);
1086 /* Wipe everything except the next pointer */
1087 memset(&s->next + 1, 0, sizeof *s - sizeof s->next);
1088}
1089
1090/*
1091 * Free an SMacro
1092 */
1093static void free_smacro(SMacro *s)
1094{
1095 free_smacro_members(s);
1096 nasm_free(s);
H. Peter Anvin8b262472019-02-26 14:00:54 -08001097}
1098
1099/*
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07001100 * Free all currently defined macros, and free the hash tables if empty
H. Peter Anvin97a23472007-09-16 17:57:25 -07001101 */
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07001102enum clear_what {
1103 CLEAR_NONE = 0,
1104 CLEAR_DEFINE = 1, /* Clear smacros */
1105 CLEAR_DEFALIAS = 2, /* Clear smacro aliases */
1106 CLEAR_ALLDEFINE = CLEAR_DEFINE|CLEAR_DEFALIAS,
1107 CLEAR_MMACRO = 4,
1108 CLEAR_ALL = CLEAR_ALLDEFINE|CLEAR_MMACRO
1109};
1110
1111static void clear_smacro_table(struct hash_table *smt, enum clear_what what)
H. Peter Anvin97a23472007-09-16 17:57:25 -07001112{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001113 struct hash_iterator it;
1114 const struct hash_node *np;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07001115 bool empty = true;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001116
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07001117 /*
1118 * Walk the hash table and clear out anything we don't want
1119 */
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001120 hash_for_each(smt, it, np) {
1121 SMacro *tmp;
1122 SMacro *s = np->data;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07001123 SMacro **head = (SMacro **)&np->data;
1124
1125 list_for_each_safe(s, tmp, s) {
1126 if (what & ((enum clear_what)s->alias + 1)) {
1127 *head = s->next;
1128 free_smacro(s);
1129 } else {
1130 empty = false;
1131 }
1132 }
H. Peter Anvin97a23472007-09-16 17:57:25 -07001133 }
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07001134
1135 /*
1136 * Free the hash table and keys if and only if it is now empty.
1137 * Note: we cannot free keys even for an empty list above, as that
1138 * mucks up the hash algorithm.
1139 */
1140 if (empty)
1141 hash_free_all(smt, true);
1142}
1143
1144static void free_smacro_table(struct hash_table *smt)
1145{
1146 clear_smacro_table(smt, CLEAR_ALLDEFINE);
H. Peter Anvin072771e2008-05-22 13:17:51 -07001147}
1148
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001149static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -07001150{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001151 struct hash_iterator it;
1152 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001153
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001154 hash_for_each(mmt, it, np) {
1155 MMacro *tmp;
1156 MMacro *m = np->data;
1157 nasm_free((void *)np->key);
1158 list_for_each_safe(m, tmp, m)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001159 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001160 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001161 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -07001162}
1163
1164static void free_macros(void)
1165{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001166 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001167 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001168}
1169
1170/*
1171 * Initialize the hash tables
1172 */
1173static void init_macros(void)
1174{
H. Peter Anvin97a23472007-09-16 17:57:25 -07001175}
1176
1177/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001178 * Pop the context stack.
1179 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001180static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001181{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001182 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001183
1184 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001185 free_smacro_table(&c->localmac);
H. Peter Anvin8571f062019-09-23 16:40:03 -07001186 nasm_free((char *)c->name);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001187 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001188}
1189
H. Peter Anvin072771e2008-05-22 13:17:51 -07001190/*
1191 * Search for a key in the hash index; adding it if necessary
1192 * (in which case we initialize the data pointer to NULL.)
1193 */
1194static void **
1195hash_findi_add(struct hash_table *hash, const char *str)
1196{
1197 struct hash_insert hi;
1198 void **r;
1199 char *strx;
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001200 size_t l = strlen(str) + 1;
H. Peter Anvin072771e2008-05-22 13:17:51 -07001201
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001202 r = hash_findib(hash, str, l, &hi);
H. Peter Anvin072771e2008-05-22 13:17:51 -07001203 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001204 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -07001205
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001206 strx = nasm_malloc(l); /* Use a more efficient allocator here? */
1207 memcpy(strx, str, l);
H. Peter Anvin072771e2008-05-22 13:17:51 -07001208 return hash_add(&hi, strx, NULL);
1209}
1210
1211/*
1212 * Like hash_findi, but returns the data element rather than a pointer
1213 * to it. Used only when not adding a new element, hence no third
1214 * argument.
1215 */
1216static void *
1217hash_findix(struct hash_table *hash, const char *str)
1218{
1219 void **p;
1220
1221 p = hash_findi(hash, str, NULL);
1222 return p ? *p : NULL;
1223}
1224
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001225/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001226 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001227 * if there no more left -- return NULL
1228 */
1229static char *line_from_stdmac(void)
1230{
1231 unsigned char c;
1232 const unsigned char *p = stdmacpos;
1233 char *line, *q;
1234 size_t len = 0;
1235
1236 if (!stdmacpos)
1237 return NULL;
1238
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -07001239 /*
1240 * 32-126 is ASCII, 127 is end of line, 128-31 are directives
1241 * (allowed to wrap around) corresponding to PP_* tokens 0-159.
1242 */
1243 while ((c = *p++) != 127) {
1244 uint8_t ndir = c - 128;
1245 if (ndir < 256-96)
1246 len += pp_directives_len[ndir] + 1;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001247 else
1248 len++;
1249 }
1250
1251 line = nasm_malloc(len + 1);
1252 q = line;
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -07001253
1254 while ((c = *stdmacpos++) != 127) {
1255 uint8_t ndir = c - 128;
1256 if (ndir < 256-96) {
1257 memcpy(q, pp_directives[ndir], pp_directives_len[ndir]);
1258 q += pp_directives_len[ndir];
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001259 *q++ = ' ';
1260 } else {
1261 *q++ = c;
1262 }
1263 }
1264 stdmacpos = p;
1265 *q = '\0';
1266
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -07001267 if (*stdmacpos == 127) {
H. Peter Anvinf7606612016-07-13 14:23:48 -07001268 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001269 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -07001270 if (*stdmacnext) {
1271 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001272 } else if (do_predef) {
1273 Line *pd, *l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001274
1275 /*
1276 * Nasty hack: here we push the contents of
1277 * `predef' on to the top-level expansion stack,
1278 * since this is the most convenient way to
1279 * implement the pre-include and pre-define
1280 * features.
1281 */
1282 list_for_each(pd, predef) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07001283 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001284 l->next = istk->expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07001285 l->first = dup_tlist(pd->first, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001286 l->finishes = NULL;
1287
1288 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001289 }
1290 do_predef = false;
1291 }
1292 }
1293
1294 return line;
1295}
1296
H. Peter Anvin6686de22019-08-10 05:33:14 -07001297/*
1298 * Read a line from a file. Return NULL on end of file.
1299 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001300static char *line_from_file(FILE *f)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001301{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001302 int c;
1303 unsigned int size, next;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001304 const unsigned int delta = 512;
1305 const unsigned int pad = 8;
1306 unsigned int nr_cont = 0;
1307 bool cont = false;
1308 char *buffer, *p;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07001309 int32_t lineno;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001310
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001311 size = delta;
1312 p = buffer = nasm_malloc(size);
1313
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001314 do {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001315 c = fgetc(f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001316
1317 switch (c) {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001318 case EOF:
1319 if (p == buffer) {
1320 nasm_free(buffer);
1321 return NULL;
1322 }
1323 c = 0;
1324 break;
1325
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001326 case '\r':
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001327 next = fgetc(f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001328 if (next != '\n')
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001329 ungetc(next, f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001330 if (cont) {
1331 cont = false;
1332 continue;
1333 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001334 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001335 break;
1336
1337 case '\n':
1338 if (cont) {
1339 cont = false;
1340 continue;
1341 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001342 c = 0;
1343 break;
1344
1345 case 032: /* ^Z = legacy MS-DOS end of file mark */
1346 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001347 break;
1348
1349 case '\\':
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001350 next = fgetc(f);
1351 ungetc(next, f);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +04001352 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001353 cont = true;
1354 nr_cont++;
1355 continue;
1356 }
1357 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001358 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001359
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001360 if (p >= (buffer + size - pad)) {
1361 buffer = nasm_realloc(buffer, size + delta);
1362 p = buffer + size - pad;
1363 size += delta;
1364 }
1365
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001366 *p++ = c;
1367 } while (c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001368
H. Peter Anvinab6f8312019-08-09 22:31:45 -07001369 lineno = src_get_linnum() + istk->lineinc +
1370 (nr_cont * istk->lineinc);
1371 src_set_linnum(lineno);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001372
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001373 return buffer;
1374}
1375
1376/*
H. Peter Anvin6686de22019-08-10 05:33:14 -07001377 * Common read routine regardless of source
1378 */
1379static char *read_line(void)
1380{
1381 char *line;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001382 FILE *f = istk->fp;
H. Peter Anvin6686de22019-08-10 05:33:14 -07001383
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001384 if (f)
1385 line = line_from_file(f);
H. Peter Anvin6686de22019-08-10 05:33:14 -07001386 else
1387 line = line_from_stdmac();
1388
1389 if (!line)
1390 return NULL;
1391
1392 if (!istk->nolist)
1393 lfmt->line(LIST_READ, src_get_linnum(), line);
1394
1395 return line;
1396}
1397
1398/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00001399 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001400 * don't need to parse the value out of e.g. numeric tokens: we
1401 * simply split one string into many.
1402 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07001403static Token *tokenize(const char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001404{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001405 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001406 Token *list = NULL;
1407 Token *t, **tail = &list;
1408
H. Peter Anvine2c80182005-01-15 22:15:51 +00001409 while (*line) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001410 const char *p = line;
1411 const char *ep = NULL; /* End of token, for trimming the end */
1412 size_t toklen;
1413 char firstchar = *p; /* Can be used to override the first char */
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001414
H. Peter Anvine2c80182005-01-15 22:15:51 +00001415 if (*p == '%') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001416 /*
1417 * Preprocessor construct; find the end of the token.
1418 * Classification is handled later, because %{...} can be
1419 * used to create any preprocessor token.
1420 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001421 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001422 if (*p == '+' && !nasm_isdigit(p[1])) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001423 /* Paste token */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001424 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001425 } else if (nasm_isdigit(*p) ||
1426 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001427 do {
1428 p++;
1429 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001430 while (nasm_isdigit(*p));
H. Peter Anvin8571f062019-09-23 16:40:03 -07001431 } else if (*p == '{' || *p == '[') {
1432 /* %{...} or %[...] */
1433 char firstchar = *p;
1434 char endchar = *p + 2; /* } or ] */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001435 int lvl = 1;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001436 line += (*p++ == '{'); /* Skip { but not [ (yet) */
1437 while (lvl) {
1438 if (*p == firstchar) {
1439 lvl++;
1440 } else if (*p == endchar) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001441 lvl--;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001442 } else if (nasm_isquote(*p)) {
1443 p = nasm_skip_string(p);
1444 }
1445
1446 /*
1447 * *p can have been advanced to a null character by
1448 * nasm_skip_string()
1449 */
1450 if (!*p) {
1451 nasm_warn(WARN_OTHER, "unterminated %%%c construct",
1452 firstchar);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001453 break;
1454 }
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001455 p++;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001456 }
1457 ep = lvl ? p : p-1; /* Terminal character not part of token */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001458 } else if (*p == '?') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001459 /* %? or %?? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001460 p++;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001461 if (*p == '?')
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001462 p++;
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001463 } else if (*p == '!') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001464 /* Environment variable reference */
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001465 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001466 if (nasm_isidchar(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001467 do {
1468 p++;
1469 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001470 while (nasm_isidchar(*p));
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001471 } else if (nasm_isquote(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001472 p = nasm_skip_string(p);
1473 if (*p)
1474 p++;
1475 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001476 nasm_nonfatalf(ERR_PASS1, "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001477 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001478 /* %! without anything else... */
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001479 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07001480 } else if (*p == ',') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001481 /* Conditional comma */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07001482 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001483 } else if (nasm_isidchar(*p) ||
H. Peter Anvin8571f062019-09-23 16:40:03 -07001484 ((*p == '%' || *p == '$') && nasm_isidchar(p[1]))) {
1485 /* Identifier or some sort */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001486 do {
1487 p++;
1488 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001489 while (nasm_isidchar(*p));
H. Peter Anvin8571f062019-09-23 16:40:03 -07001490 } else if (*p == '%') {
1491 /* %% operator */
1492 p++;
1493 }
1494
1495 if (!ep)
1496 ep = p;
1497 toklen = ep - line;
1498
1499 /* Classify here, to handle %{...} correctly */
1500 if (toklen < 2) {
1501 type = TOK_OTHER; /* % operator */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001502 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001503 char c0 = line[1];
1504
1505 switch (c0) {
1506 case '+':
1507 type = (toklen == 2) ? TOK_PASTE : TOK_MMACRO_PARAM;
1508 break;
1509
1510 case '-':
1511 type = TOK_MMACRO_PARAM;
1512 break;
1513
1514 case '?':
1515 if (toklen == 2)
1516 type = TOK_PREPROC_Q;
1517 else if (toklen == 3 && line[2] == '?')
1518 type = TOK_PREPROC_QQ;
1519 else
1520 type = TOK_PREPROC_ID;
1521 break;
1522
1523 case '!':
1524 type = (toklen == 2) ? TOK_OTHER : TOK_ENVIRON;
1525 break;
1526
1527 case '%':
1528 type = (toklen == 2) ? TOK_OTHER : TOK_LOCAL_SYMBOL;
1529 break;
1530
1531 case '$':
1532 type = (toklen == 2) ? TOK_OTHER : TOK_LOCAL_MACRO;
1533 break;
1534
1535 case '[':
1536 line += 2; /* Skip %[ */
1537 firstchar = *line; /* Don't clobber */
1538 toklen -= 2;
1539 type = TOK_INDIRECT;
1540 break;
1541
1542 case ',':
1543 type = (toklen == 2) ? TOK_COND_COMMA : TOK_PREPROC_ID;
1544 break;
1545
1546 case '\'':
1547 case '\"':
1548 case '`':
1549 /* %{'string'} */
1550 type = TOK_PREPROC_ID;
1551 break;
1552
1553 case ':':
1554 type = TOK_MMACRO_PARAM; /* %{:..} */
1555 break;
1556
1557 default:
1558 if (nasm_isdigit(c0))
1559 type = TOK_MMACRO_PARAM;
1560 else if (nasm_isidchar(c0) || toklen > 2)
1561 type = TOK_PREPROC_ID;
1562 else
1563 type = TOK_OTHER;
1564 break;
1565 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001566 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001567 } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001568 /*
1569 * An identifier. This includes the ? operator, which is
1570 * treated as a keyword, not as a special character
1571 * operator
1572 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001573 type = TOK_ID;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001574 while (nasm_isidchar(*++p))
1575 ;
1576 } else if (nasm_isquote(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001577 /*
1578 * A string token.
1579 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001580 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001581 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001582
H. Peter Anvine2c80182005-01-15 22:15:51 +00001583 if (*p) {
1584 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001585 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001586 nasm_warn(WARN_OTHER, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001587 /* Handling unterminated strings by UNV */
1588 /* type = -1; */
1589 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001590 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001591 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001592 p += 2;
H. Peter Anvin13506202018-11-28 14:55:58 -08001593 } else if (nasm_isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001594 bool is_hex = false;
1595 bool is_float = false;
1596 bool has_e = false;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001597 char c;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001598
H. Peter Anvine2c80182005-01-15 22:15:51 +00001599 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001600 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001601 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001602
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001603 if (*p == '$') {
1604 p++;
1605 is_hex = true;
1606 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001607
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001608 for (;;) {
1609 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001610
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001611 if (!is_hex && (c == 'e' || c == 'E')) {
1612 has_e = true;
1613 if (*p == '+' || *p == '-') {
1614 /*
1615 * e can only be followed by +/- if it is either a
1616 * prefixed hex number or a floating-point number
1617 */
1618 p++;
1619 is_float = true;
1620 }
1621 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1622 is_hex = true;
1623 } else if (c == 'P' || c == 'p') {
1624 is_float = true;
1625 if (*p == '+' || *p == '-')
1626 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001627 } else if (nasm_isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001628 ; /* just advance */
1629 else if (c == '.') {
1630 /*
1631 * we need to deal with consequences of the legacy
1632 * parser, like "1.nolist" being two tokens
1633 * (TOK_NUMBER, TOK_ID) here; at least give it
1634 * a shot for now. In the future, we probably need
1635 * a flex-based scanner with proper pattern matching
1636 * to do it as well as it can be done. Nothing in
1637 * the world is going to help the person who wants
1638 * 0x123.p16 interpreted as two tokens, though.
1639 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07001640 const char *r = p;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001641 while (*r == '_')
1642 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001643
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001644 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1645 (!is_hex && (*r == 'e' || *r == 'E')) ||
1646 (*r == 'p' || *r == 'P')) {
1647 p = r;
1648 is_float = true;
1649 } else
1650 break; /* Terminate the token */
1651 } else
1652 break;
1653 }
1654 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001655
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001656 if (p == line+1 && *line == '$') {
1657 type = TOK_OTHER; /* TOKEN_HERE */
1658 } else {
1659 if (has_e && !is_hex) {
1660 /* 1e13 is floating-point, but 1e13h is not */
1661 is_float = true;
1662 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001663
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001664 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1665 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001666 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001667 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001668 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001669 /*
1670 * Whitespace just before end-of-line is discarded by
1671 * pretending it's a comment; whitespace just before a
1672 * comment gets lumped into the comment.
1673 */
1674 if (!*p || *p == ';') {
1675 type = TOK_COMMENT;
1676 while (*p)
1677 p++;
1678 }
1679 } else if (*p == ';') {
1680 type = TOK_COMMENT;
1681 while (*p)
1682 p++;
1683 } else {
1684 /*
1685 * Anything else is an operator of some kind. We check
1686 * for all the double-character operators (>>, <<, //,
H. Peter Anvin8571f062019-09-23 16:40:03 -07001687 * %%, <=, >=, ==, !=, <>, &&, ||, ^^) and the triple-
1688 * character operators (<<<, >>>, <=>) but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001689 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001690 */
1691 type = TOK_OTHER;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001692 switch (*p++) {
1693 case '>':
1694 if (*p == '>') {
1695 p++;
1696 if (*p == '>')
1697 p++;
H. Peter Anvind03a6c82019-10-07 21:29:05 -07001698 } else if (*p == '=') {
1699 p++;
1700 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07001701 break;
1702
1703 case '<':
1704 if (*p == '<') {
1705 p++;
1706 if (*p == '<')
1707 p++;
1708 } else if (*p == '=') {
1709 p++;
1710 if (*p == '>')
1711 p++;
1712 } else if (*p == '>') {
1713 p++;
1714 }
1715 break;
1716
1717 case '!':
1718 if (*p == '=')
1719 p++;
1720 break;
1721
1722 case '/':
1723 case '=':
1724 case '&':
1725 case '|':
1726 case '^':
1727 /* These operators can be doubled but nothing else */
1728 if (*p == p[-1])
1729 p++;
1730 break;
1731
1732 default:
1733 break;
1734 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001735 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001736
H. Peter Anvin8571f062019-09-23 16:40:03 -07001737 if (type == TOK_WHITESPACE) {
1738 *tail = t = new_White(NULL);
1739 tail = &t->next;
1740 } else if (type != TOK_COMMENT) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001741 if (!ep)
1742 ep = p;
1743 *tail = t = new_Token(NULL, type, line, ep - line);
H. Peter Anvin8571f062019-09-23 16:40:03 -07001744 *tok_text_buf(t) = firstchar; /* E.g. %{foo} -> {foo -> %foo */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001745 tail = &t->next;
1746 }
1747 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001748 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001749 return list;
1750}
1751
H. Peter Anvin8571f062019-09-23 16:40:03 -07001752/*
1753 * Tokens are allocated in blocks to improve speed. Set the blocksize
1754 * to 0 to use regular nasm_malloc(); this is useful for debugging.
1755 *
1756 * alloc_Token() returns a zero-initialized token structure.
1757 */
1758#define TOKEN_BLOCKSIZE 4096
1759
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001760#if TOKEN_BLOCKSIZE
H. Peter Anvin8571f062019-09-23 16:40:03 -07001761
1762static Token *freeTokens = NULL;
1763static Token *tokenblocks = NULL;
1764
1765static Token *alloc_Token(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001766{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001767 Token *t = freeTokens;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001768
H. Peter Anvin8571f062019-09-23 16:40:03 -07001769 if (unlikely(!t)) {
1770 Token *block;
1771 size_t i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001772
H. Peter Anvin8571f062019-09-23 16:40:03 -07001773 nasm_newn(block, TOKEN_BLOCKSIZE);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001774
H. Peter Anvin8571f062019-09-23 16:40:03 -07001775 /*
1776 * The first entry in each array are a linked list of
1777 * block allocations and is not used for data.
1778 */
1779 block[0].next = tokenblocks;
1780 block[0].type = TOK_BLOCK;
1781 tokenblocks = block;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001782
H. Peter Anvin8571f062019-09-23 16:40:03 -07001783 /*
1784 * Add the rest to the free list
1785 */
1786 for (i = 2; i < TOKEN_BLOCKSIZE - 1; i++)
1787 block[i].next = &block[i+1];
1788
1789 freeTokens = &block[2];
1790
1791 /*
1792 * Return the topmost usable token
1793 */
1794 return &block[1];
1795 }
1796
1797 freeTokens = t->next;
1798 t->next = NULL;
1799 return t;
H. Peter Anvince616072002-04-30 21:02:23 +00001800}
1801
H. Peter Anvin8571f062019-09-23 16:40:03 -07001802static Token *delete_Token(Token *t)
1803{
1804 Token *next = t->next;
1805
1806 nasm_zero(*t);
1807 t->next = freeTokens;
1808 freeTokens = t;
1809
1810 return next;
1811}
1812
H. Peter Anvine2c80182005-01-15 22:15:51 +00001813static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001814{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001815 Token *block, *blocktmp;
H. Peter Anvince616072002-04-30 21:02:23 +00001816
H. Peter Anvin8571f062019-09-23 16:40:03 -07001817 list_for_each_safe(block, blocktmp, tokenblocks)
1818 nasm_free(block);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001819
H. Peter Anvin8571f062019-09-23 16:40:03 -07001820 freeTokens = tokenblocks = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001821}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001822
H. Peter Anvin8571f062019-09-23 16:40:03 -07001823#else
1824
1825static inline Token *alloc_Token(void)
1826{
1827 Token *t;
1828 nasm_new(*t);
1829 return t;
1830}
1831
1832static Token *delete_Token(Token *t)
1833{
1834 Token *next = t->next;
1835 nasm_free(t);
1836 return next;
1837}
1838
1839static inline void delete_Blocks(void)
1840{
1841 /* Nothing to do */
1842}
1843
1844#endif
1845
H. Peter Anvin734b1882002-04-30 21:01:08 +00001846/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001847 * this function creates a new Token and passes a pointer to it
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001848 * back to the caller. It sets the type, text, and next pointer elements.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001849 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001850static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001851 const char *text, size_t txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001852{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001853 Token *t = alloc_Token();
1854 char *textp;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001855
H. Peter Anvin734b1882002-04-30 21:01:08 +00001856 t->next = next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001857 t->type = type;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001858 if (type == TOK_WHITESPACE) {
1859 t->len = 1;
1860 t->text.a[0] = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001861 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001862 if (text && text[0] && !txtlen)
1863 txtlen = tok_strlen(text);
1864
1865 t->len = tok_check_len(txtlen);
1866
1867 if (text) {
1868 textp = (txtlen > INLINE_TEXT)
1869 ? (t->text.p.ptr = nasm_malloc(txtlen+1)) : t->text.a;
1870 memcpy(textp, text, txtlen);
1871 textp[txtlen] = '\0'; /* In case we needed malloc() */
1872 } else {
1873 /*
1874 * Allocate a buffer but do not fill it. The caller
1875 * can fill in text, but must not change the length.
1876 * The filled in text must be exactly txtlen once
1877 * the buffer is filled and before the token is added
1878 * to any line lists.
1879 */
1880 if (txtlen > INLINE_TEXT)
1881 t->text.p.ptr = nasm_zalloc(txtlen+1);
1882 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001883 }
1884 return t;
1885}
1886
H. Peter Anvin8571f062019-09-23 16:40:03 -07001887/*
1888 * Same as new_Token(), but text belongs to the new token and is
1889 * either taken over or freed. This function MUST be called
1890 * with valid txt and txtlen, unlike new_Token().
1891 */
1892static Token *new_Token_free(Token * next, enum pp_token_type type,
1893 char *text, size_t txtlen)
1894{
1895 Token *t = alloc_Token();
1896
1897 t->next = next;
1898 t->type = type;
1899 t->len = tok_check_len(txtlen);
1900
1901 if (txtlen <= INLINE_TEXT) {
1902 memcpy(t->text.a, text, txtlen);
1903 free(text);
1904 } else {
1905 t->text.p.ptr = text;
1906 }
1907
1908 return t;
1909}
1910
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001911static Token *dup_Token(Token *next, const Token *src)
1912{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001913 Token *t = alloc_Token();
1914
1915 memcpy(t, src, sizeof *src);
1916 t->next = next;
1917
1918 if (t->len > INLINE_TEXT) {
1919 t->text.p.ptr = nasm_malloc(t->len + 1);
1920 memcpy(t->text.p.ptr, src->text.p.ptr, t->len+1);
1921 }
1922
1923 return t;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001924}
1925
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001926static Token *new_White(Token *next)
1927{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001928 Token *t = alloc_Token();
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001929
H. Peter Anvin8571f062019-09-23 16:40:03 -07001930 t->next = next;
1931 t->type = TOK_WHITESPACE;
1932 t->len = 1;
1933 t->text.a[0] = ' ';
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001934
H. Peter Anvin8571f062019-09-23 16:40:03 -07001935 return t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001936}
1937
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001938/*
H. Peter Anvin8571f062019-09-23 16:40:03 -07001939 * This *transfers* the content from one token to another, leaving the
1940 * next pointer of the latter intact. Unlike dup_Token(), the old
1941 * token is destroyed, except for its next pointer, and the text
1942 * pointer allocation, if any, is simply transferred.
1943 */
1944static Token *steal_Token(Token *dst, Token *src)
1945{
1946 /* Overwrite everything except the next pointers */
1947 memcpy((char *)dst + sizeof(Token *), (char *)src + sizeof(Token *),
1948 sizeof(Token) - sizeof(Token *));
1949
1950 /* Clear the donor token */
1951 memset((char *)src + sizeof(Token *), 0, sizeof(Token) - sizeof(Token *));
1952
1953 return dst;
1954}
1955
1956/*
1957 * Convert a line of tokens back into text. This modifies the list
1958 * by expanding environment variables.
1959 *
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001960 * If expand_locals is not zero, identifiers of the form "%$*xxx"
H. Peter Anvin8571f062019-09-23 16:40:03 -07001961 * are also transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001962 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001963static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001964{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001965 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001966 char *line, *p;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001967 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001968
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001969 list_for_each(t, tlist) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001970 switch (t->type) {
1971 case TOK_ENVIRON:
1972 {
1973 const char *v = pp_getenv(t, true);
1974 set_text(t, v, tok_strlen(v));
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07001975 t->type = TOK_NAKED_STRING;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001976 break;
1977 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001978
H. Peter Anvin8571f062019-09-23 16:40:03 -07001979 case TOK_LOCAL_MACRO:
1980 case TOK_LOCAL_SYMBOL:
1981 if (expand_locals) {
1982 const char *q;
1983 char *p;
1984 Context *ctx = get_ctx(tok_text(t), &q);
1985 if (ctx) {
1986 p = nasm_asprintf("..@%"PRIu64".%s", ctx->number, q);
1987 set_text_free(t, p, nasm_last_string_len());
1988 t->type = TOK_ID;
1989 }
1990 }
1991 break;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001992
H. Peter Anvin8571f062019-09-23 16:40:03 -07001993 default:
1994 break; /* No modifications */
1995 }
1996
1997 if (debug_level(2)) {
1998 unsigned int t_len = t->len;
1999 unsigned int s_len = tok_strlen(tok_text(t));
2000 if (t_len != s_len) {
2001 nasm_panic("assertion failed: token \"%s\" type %u len %u has t->len %u\n",
2002 tok_text(t), t->type, s_len, t_len);
2003 t->len = s_len;
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002004 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002005 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07002006
H. Peter Anvin8571f062019-09-23 16:40:03 -07002007 len += t->len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002008 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04002009
H. Peter Anvin734b1882002-04-30 21:01:08 +00002010 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04002011
H. Peter Anvin8571f062019-09-23 16:40:03 -07002012 list_for_each(t, tlist)
2013 p = mempcpy(p, tok_text(t), t->len);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002014 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04002015
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002016 return line;
2017}
2018
2019/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002020 * A scanner, suitable for use by the expression evaluator, which
2021 * operates on a line of Tokens. Expects a pointer to a pointer to
2022 * the first token in the line to be passed in as its private_data
2023 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07002024 *
2025 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00002026 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08002027struct ppscan {
2028 Token *tptr;
2029 int ntokens;
2030};
2031
H. Peter Anvine2c80182005-01-15 22:15:51 +00002032static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002033{
H. Peter Anvin8b262472019-02-26 14:00:54 -08002034 struct ppscan *pps = private_data;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002035 Token *tline;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002036 const char *txt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002037
H. Peter Anvine2c80182005-01-15 22:15:51 +00002038 do {
H. Peter Anvin8b262472019-02-26 14:00:54 -08002039 if (pps->ntokens && (tline = pps->tptr)) {
2040 pps->ntokens--;
2041 pps->tptr = tline->next;
2042 } else {
2043 pps->tptr = NULL;
2044 pps->ntokens = 0;
2045 return tokval->t_type = TOKEN_EOS;
2046 }
2047 } while (tline->type == TOK_WHITESPACE || tline->type == TOK_COMMENT);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002048
H. Peter Anvin8571f062019-09-23 16:40:03 -07002049 txt = tok_text(tline);
2050 tokval->t_charptr = (char *)txt; /* Fix this */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07002051
H. Peter Anvin8571f062019-09-23 16:40:03 -07002052 if (txt[0] == '$') {
2053 if (!txt[1]) {
2054 return tokval->t_type = TOKEN_HERE;
2055 } else if (txt[1] == '$' && !txt[2]) {
2056 return tokval->t_type = TOKEN_BASE;
2057 } else if (tline->type == TOK_ID) {
2058 tokval->t_charptr++;
2059 return tokval->t_type = TOKEN_ID;
2060 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002061 }
2062
H. Peter Anvin8571f062019-09-23 16:40:03 -07002063 switch (tline->type) {
2064 default:
2065 if (tline->len == 1)
2066 return tokval->t_type = txt[0];
2067 /* fall through */
2068 case TOK_ID:
2069 return nasm_token_hash(txt, tokval);
2070
2071 case TOK_NUMBER:
2072 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002073 bool rn_error;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002074 tokval->t_integer = readnum(txt, &rn_error);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002075 if (rn_error)
2076 return tokval->t_type = TOKEN_ERRNUM;
2077 else
2078 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07002079 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002080
H. Peter Anvin8571f062019-09-23 16:40:03 -07002081 case TOK_FLOAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002082 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002083
2084 case TOK_STRING:
2085 tokval->t_charptr = (char *)unquote_token(tline);
2086 tokval->t_inttwo = tline->len;
2087 return tokval->t_type = TOKEN_STR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002088 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002089}
2090
2091/*
H. Peter Anvind2354082019-08-27 16:38:48 -07002092 * 1. An expression (true if nonzero 0)
2093 * 2. The keywords true, on, yes for true
2094 * 3. The keywords false, off, no for false
2095 * 4. An empty line, for true
2096 *
2097 * On error, return defval (usually the previous value)
2098 */
2099static bool pp_get_boolean_option(Token *tline, bool defval)
2100{
2101 static const char * const noyes[] = {
2102 "no", "yes",
2103 "false", "true",
2104 "off", "on"
2105 };
2106 struct ppscan pps;
2107 struct tokenval tokval;
2108 expr *evalresult;
2109
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002110 tline = skip_white(tline);
2111 if (!tline)
H. Peter Anvind2354082019-08-27 16:38:48 -07002112 return true;
2113
2114 if (tline->type == TOK_ID) {
2115 size_t i;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002116 const char *txt = tok_text(tline);
2117
H. Peter Anvind2354082019-08-27 16:38:48 -07002118 for (i = 0; i < ARRAY_SIZE(noyes); i++)
H. Peter Anvin8571f062019-09-23 16:40:03 -07002119 if (!nasm_stricmp(txt, noyes[i]))
H. Peter Anvind2354082019-08-27 16:38:48 -07002120 return i & 1;
2121 }
2122
2123 pps.tptr = NULL;
2124 pps.tptr = tline;
2125 pps.ntokens = -1;
2126 tokval.t_type = TOKEN_INVALID;
2127 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
2128
2129 if (!evalresult)
2130 return true;
2131
2132 if (tokval.t_type)
2133 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
2134 if (!is_really_simple(evalresult)) {
2135 nasm_nonfatal("boolean flag expression must be a constant");
2136 return defval;
2137 }
2138
2139 return reloc_value(evalresult) != 0;
2140}
2141
2142/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002143 * Compare a string to the name of an existing macro; this is a
2144 * simple wrapper which calls either strcmp or nasm_stricmp
2145 * depending on the value of the `casesense' parameter.
2146 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002147static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002148{
H. Peter Anvin734b1882002-04-30 21:01:08 +00002149 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002150}
2151
2152/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07002153 * Compare a string to the name of an existing macro; this is a
2154 * simple wrapper which calls either strcmp or nasm_stricmp
2155 * depending on the value of the `casesense' parameter.
2156 */
2157static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
2158{
2159 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
2160}
2161
2162/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002163 * Return the Context structure associated with a %$ token. Return
2164 * NULL, having _already_ reported an error condition, if the
2165 * context stack isn't deep enough for the supplied number of $
2166 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002167 *
2168 * If "namep" is non-NULL, set it to the pointer to the macro name
2169 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002170 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04002171static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002172{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002173 Context *ctx;
2174 int i;
2175
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002176 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002177 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002178
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002179 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00002180 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002181
H. Peter Anvine2c80182005-01-15 22:15:51 +00002182 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002183 nasm_nonfatal("`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002184 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002185 }
2186
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002187 name += 2;
2188 ctx = cstk;
2189 i = 0;
2190 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002191 name++;
2192 i++;
2193 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002194 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002195 if (!ctx) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002196 nasm_nonfatal("`%s': context stack is only"
2197 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002198 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002199 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002200
2201 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002202 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002203
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04002204 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002205}
2206
2207/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002208 * Open an include file. This routine must always return a valid
2209 * file pointer if it returns - it's responsible for throwing an
2210 * ERR_FATAL and bombing out completely if not. It should also try
2211 * the include path one by one until it finds the file or reaches
2212 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07002213 *
2214 * Note: for INC_PROBE the function returns NULL at all times;
2215 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002216 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07002217enum incopen_mode {
2218 INC_NEEDED, /* File must exist */
2219 INC_OPTIONAL, /* Missing is OK */
2220 INC_PROBE /* Only an existence probe */
2221};
2222
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002223/* This is conducts a full pathname search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002224static FILE *inc_fopen_search(const char *file, char **slpath,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002225 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002226{
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -08002227 const struct strlist_entry *ip = strlist_head(ipath_list);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002228 FILE *fp;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002229 const char *prefix = "";
night199ukfdb1a1b2018-10-18 23:19:47 +02002230 char *sp;
H. Peter Anvind81a2352016-09-21 14:03:18 -07002231 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002232
H. Peter Anvine2c80182005-01-15 22:15:51 +00002233 while (1) {
night199ukfdb1a1b2018-10-18 23:19:47 +02002234 sp = nasm_catfile(prefix, file);
H. Peter Anvind81a2352016-09-21 14:03:18 -07002235 if (omode == INC_PROBE) {
2236 fp = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002237 found = nasm_file_exists(sp);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07002238 } else {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002239 fp = nasm_open_read(sp, fmode);
H. Peter Anvind81a2352016-09-21 14:03:18 -07002240 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002241 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07002242 if (found) {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002243 *slpath = sp;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002244 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002245 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002246
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002247 nasm_free(sp);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002248
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002249 if (!ip) {
2250 *slpath = NULL;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002251 return NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002252 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002253
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002254 prefix = ip->str;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002255 ip = ip->next;
2256 }
2257}
2258
2259/*
2260 * Open a file, or test for the presence of one (depending on omode),
2261 * considering the include path.
2262 */
2263static FILE *inc_fopen(const char *file,
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002264 struct strlist *dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002265 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002266 enum incopen_mode omode,
2267 enum file_flags fmode)
2268{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002269 struct hash_insert hi;
2270 void **hp;
2271 char *path;
2272 FILE *fp = NULL;
2273
2274 hp = hash_find(&FileHash, file, &hi);
2275 if (hp) {
2276 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03002277 if (path || omode != INC_NEEDED) {
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002278 strlist_add(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03002279 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002280 } else {
2281 /* Need to do the actual path search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002282 fp = inc_fopen_search(file, &path, omode, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002283
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002284 /* Positive or negative result */
2285 hash_add(&hi, nasm_strdup(file), path);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002286
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002287 /*
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002288 * Add file to dependency path.
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002289 */
2290 if (path || omode != INC_NEEDED)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002291 strlist_add(dhead, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00002292 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002293
H. Peter Anvin (Intel)5d68f982020-06-01 12:32:35 -07002294 if (path && !fp && omode != INC_PROBE)
2295 fp = nasm_open_read(path, fmode);
2296
2297 if (omode == INC_NEEDED && !fp) {
2298 if (!path)
2299 errno = ENOENT;
2300
2301 nasm_nonfatal("unable to open include file `%s': %s",
2302 file, strerror(errno));
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002303 }
2304
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002305 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002306 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002307
2308 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002309}
2310
2311/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07002312 * Opens an include or input file. Public version, for use by modules
2313 * that get a file:lineno pair and need to look at the file again
2314 * (e.g. the CodeView debug backend). Returns NULL on failure.
2315 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07002316FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07002317{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002318 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07002319}
2320
2321/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002322 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002323 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002324 * return true if _any_ single-line macro of that name is defined.
2325 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002326 * `nparam' or no parameters is defined.
2327 *
2328 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002329 * defined, or nparam is -1, the address of the definition structure
2330 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002331 * is NULL, no action will be taken regarding its contents, and no
2332 * error will occur.
2333 *
2334 * Note that this is also called with nparam zero to resolve
2335 * `ifdef'.
2336 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002337static bool
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002338smacro_defined(Context *ctx, const char *name, int nparam, SMacro **defn,
H. Peter Anvind2354082019-08-27 16:38:48 -07002339 bool nocase, bool find_alias)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002340{
H. Peter Anvin166c2472008-05-28 12:28:58 -07002341 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002342 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002343
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002344 smtbl = ctx ? &ctx->localmac : &smacros;
H. Peter Anvind2354082019-08-27 16:38:48 -07002345
2346restart:
H. Peter Anvin166c2472008-05-28 12:28:58 -07002347 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002348
H. Peter Anvine2c80182005-01-15 22:15:51 +00002349 while (m) {
2350 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002351 (nparam <= 0 || m->nparam == 0 || nparam == m->nparam ||
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002352 (m->greedy && nparam >= m->nparam-1))) {
H. Peter Anvind2354082019-08-27 16:38:48 -07002353 if (m->alias && !find_alias) {
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002354 if (!ppopt.noaliases) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002355 name = tok_text(m->expansion);
H. Peter Anvind2354082019-08-27 16:38:48 -07002356 goto restart;
2357 } else {
2358 continue;
2359 }
2360 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002361 if (defn) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002362 *defn = (nparam == m->nparam || nparam == -1) ? m : NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002363 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002364 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002365 }
2366 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002367 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002368
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002369 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002370}
2371
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002372/* param should be a natural number [0; INT_MAX] */
2373static int read_param_count(const char *str)
2374{
2375 int result;
2376 bool err;
2377
2378 result = readnum(str, &err);
2379 if (result < 0 || result > INT_MAX) {
2380 result = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002381 nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]",
2382 str, 0, INT_MAX);
2383 } else if (err)
2384 nasm_nonfatal("unable to parse parameter count `%s'", str);
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002385 return result;
2386}
2387
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002388/*
2389 * Count and mark off the parameters in a multi-line macro call.
2390 * This is called both from within the multi-line macro expansion
2391 * code, and also to mark off the default parameters when provided
2392 * in a %macro definition line.
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002393 *
2394 * Note that we need space in the params array for parameter 0 being
2395 * a possible captured label as well as the final NULL.
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002396 *
2397 * Returns a pointer to the pointer to a terminal comma if present;
2398 * used to drop an empty terminal argument for legacy reasons.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002399 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002400static Token **count_mmac_params(Token *tline, int *nparamp, Token ***paramsp)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002401{
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07002402 int paramsize;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002403 int nparam = 0;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002404 Token *t;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002405 Token **comma = NULL, **maybe_comma = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002406 Token **params;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002407
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002408 paramsize = PARAM_DELTA;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002409 nasm_newn(params, paramsize);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002410
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002411 t = skip_white(tline);
2412 if (t) {
2413 while (true) {
2414 /* Need two slots for captured label and NULL */
2415 if (unlikely(nparam+2 >= paramsize)) {
2416 paramsize += PARAM_DELTA;
2417 params = nasm_realloc(params, sizeof(*params) * paramsize);
2418 }
2419 params[++nparam] = t;
2420 if (tok_is(t, '{')) {
2421 int brace = 1;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002422
2423 comma = NULL; /* Non-empty parameter */
2424
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002425 while (brace && (t = t->next)) {
2426 brace += tok_is(t, '{');
2427 brace -= tok_is(t, '}');
2428 }
H. Peter Anvin (Intel)f8639bd2020-06-04 16:29:53 -07002429
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002430 if (t) {
2431 /*
2432 * Now we've found the closing brace, look further
2433 * for the comma.
2434 */
2435 t = skip_white(t->next);
2436 if (tok_isnt(t, ','))
2437 nasm_nonfatal("braces do not enclose all of macro parameter");
2438 } else {
2439 nasm_nonfatal("expecting closing brace in macro parameter");
2440 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08002441 }
2442
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002443 /* Advance to the next comma */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002444 maybe_comma = &t->next;
2445 while (tok_isnt(t, ',')) {
2446 if (!tok_white(t))
2447 comma = NULL; /* Non-empty parameter */
2448 maybe_comma = &t->next;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002449 t = t->next;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002450 }
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002451
2452 if (!t)
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002453 break; /* End of string, no comma */
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002454
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002455 comma = maybe_comma; /* Point to comma pointer */
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002456 t = skip_white(t->next); /* Eat the comma and whitespace */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08002457 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002458 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002459
2460 params[nparam+1] = NULL;
2461 *paramsp = params;
2462 *nparamp = nparam;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002463
2464 return comma;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002465}
2466
2467/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002468 * Determine whether one of the various `if' conditions is true or
2469 * not.
2470 *
2471 * We must free the tline we get passed.
2472 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002473static enum cond_state if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002474{
H. Peter Anvin70055962007-10-11 00:05:31 -07002475 bool j;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002476 Token *t, *tt, *origline;
2477 struct ppscan pps;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002478 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002479 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002480 enum pp_token_type needtype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002481 const char *dname = pp_directives[ct];
2482 bool casesense = true;
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002483 enum preproc_token cond = PP_COND(ct);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002484
2485 origline = tline;
2486
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002487 switch (cond) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002488 case PP_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002489 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02002490 while (true) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002491 tline = skip_white(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02002492 if (!tline)
2493 break;
2494 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002495 nasm_nonfatal("`%s' expects context identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002496 dname);
2497 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002498 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002499 if (cstk && cstk->name && !nasm_stricmp(tok_text(tline), cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002500 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002501 tline = tline->next;
2502 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002503 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002504
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002505 case PP_IFDEF:
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002506 case PP_IFDEFALIAS:
2507 {
2508 bool alias = cond == PP_IFDEFALIAS;
2509 SMacro *smac;
2510 Context *ctx;
2511 const char *mname;
2512
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002513 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002514 while (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002515 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002516 if (!tline || (tline->type != TOK_ID &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07002517 tline->type != TOK_LOCAL_MACRO)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002518 nasm_nonfatal("`%s' expects macro identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002519 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002520 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002521 }
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002522
2523 mname = tok_text(tline);
2524 ctx = get_ctx(mname, &mname);
H. Peter Anvin (Intel)b91e7732020-06-05 12:22:26 -07002525 if (smacro_defined(ctx, mname, -1, &smac, true, alias) && smac
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002526 && smac->alias == alias) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002527 j = true;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002528 break;
2529 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002530 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002531 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002532 break;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002533 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002534
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002535 case PP_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002536 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002537 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002538 while (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002539 tline = skip_white(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002540 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002541 tline->type != TOK_STRING &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07002542 tline->type != TOK_INTERNAL_STRING &&
2543 tline->type != TOK_ENVIRON)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002544 nasm_nonfatal("`%s' expects environment variable names",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002545 dname);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002546 goto fail;
2547 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002548
2549 j |= !!pp_getenv(tline, false);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002550 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002551 }
2552 break;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002553
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002554 case PP_IFIDNI:
2555 casesense = false;
2556 /* fall through */
2557 case PP_IFIDN:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002558 tline = expand_smacro(tline);
2559 t = tt = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002560 while (tok_isnt(tt, ','))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002561 tt = tt->next;
2562 if (!tt) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002563 nasm_nonfatal("`%s' expects two comma-separated arguments",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002564 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002565 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002566 }
2567 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002568 j = true; /* assume equality unless proved not */
H. Peter Anvin8571f062019-09-23 16:40:03 -07002569 while (tok_isnt(t, ',') && tt) {
2570 unsigned int l1, l2;
2571 const char *t1, *t2;
2572
2573 if (tok_is(tt, ',')) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002574 nasm_nonfatal("`%s': more than one comma on line",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002575 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002576 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002577 }
2578 if (t->type == TOK_WHITESPACE) {
2579 t = t->next;
2580 continue;
2581 }
2582 if (tt->type == TOK_WHITESPACE) {
2583 tt = tt->next;
2584 continue;
2585 }
2586 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002587 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002588 break;
2589 }
H. Peter Anvind2456592008-06-19 15:04:18 -07002590
H. Peter Anvin8571f062019-09-23 16:40:03 -07002591 t1 = unquote_token(t);
2592 t2 = unquote_token(tt);
2593 l1 = t->len;
2594 l2 = tt->len;
2595
2596 if (l1 != l2 || mmemcmp(t1, t2, l1, casesense)) {
2597 j = false;
2598 break;
2599 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00002600
H. Peter Anvine2c80182005-01-15 22:15:51 +00002601 t = t->next;
2602 tt = tt->next;
2603 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002604 if (!tok_is(t, ',') || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002605 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002606 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002607
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002608 case PP_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04002609 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002610 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002611 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00002612
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002613 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002614 tline = expand_id(tline);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002615 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002616 nasm_nonfatal("`%s' expects a macro name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002617 goto fail;
2618 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002619 nasm_zero(searching);
H. Peter Anvin8571f062019-09-23 16:40:03 -07002620 searching.name = dup_text(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002621 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002622 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002623 searching.nparam_max = INT_MAX;
2624 tline = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002625 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002626 if (!tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002627 } else if (!tok_type(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002628 nasm_nonfatal("`%s' expects a parameter count or nothing",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002629 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002630 } else {
2631 searching.nparam_min = searching.nparam_max =
H. Peter Anvin8571f062019-09-23 16:40:03 -07002632 read_param_count(tok_text(tline));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002633 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002634 if (tline && tok_is(tline->next, '-')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002635 tline = tline->next->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002636 if (tok_is(tline, '*'))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002637 searching.nparam_max = INT_MAX;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002638 else if (!tok_type(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002639 nasm_nonfatal("`%s' expects a parameter count after `-'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002640 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002641 else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002642 searching.nparam_max = read_param_count(tok_text(tline));
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002643 if (searching.nparam_min > searching.nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002644 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002645 searching.nparam_max = searching.nparam_min;
2646 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002647 }
2648 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002649 if (tline && tok_is(tline->next, '+')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002650 tline = tline->next;
2651 searching.plus = true;
2652 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002653 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
2654 while (mmac) {
2655 if (!strcmp(mmac->name, searching.name) &&
2656 (mmac->nparam_min <= searching.nparam_max
2657 || searching.plus)
2658 && (searching.nparam_min <= mmac->nparam_max
2659 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002660 found = true;
2661 break;
2662 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002663 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002664 }
2665 if (tline && tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002666 nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002667 nasm_free(searching.name);
2668 j = found;
2669 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002670 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002671
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002672 case PP_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002673 needtype = TOK_ID;
2674 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002675 case PP_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002676 needtype = TOK_NUMBER;
2677 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002678 case PP_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002679 needtype = TOK_STRING;
2680 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002681
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002682iftype:
2683 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002684
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002685 while (tok_white(t) ||
2686 (needtype == TOK_NUMBER && (tok_is(t, '-') | tok_is(t, '+'))))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002687 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002688
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002689 j = tok_type(t, needtype);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002690 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002691
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002692 case PP_IFTOKEN:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002693 tline = expand_smacro(tline);
2694 t = skip_white(tline);
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002695
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002696 j = false;
2697 if (t) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002698 t = skip_white(t->next); /* Skip the actual token + whitespace */
2699 j = !t;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002700 }
2701 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002702
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002703 case PP_IFEMPTY:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002704 tline = expand_smacro(tline);
2705 t = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002706 j = !t; /* Should be empty */
2707 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002708
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002709 case PP_IF:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002710 pps.tptr = tline = expand_smacro(tline);
2711 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002712 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002713 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002714 if (!evalresult)
2715 return -1;
2716 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002717 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002718 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002719 nasm_nonfatal("non-constant value given to `%s'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002720 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002721 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002722 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002723 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002724 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002725
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002726 case PP_IFUSING:
2727 case PP_IFUSABLE:
2728 {
2729 const struct use_package *pkg;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002730 const char *name;
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002731
H. Peter Anvin8571f062019-09-23 16:40:03 -07002732 pkg = get_use_pkg(tline, dname, &name);
2733 if (!name)
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002734 goto fail;
2735
2736 j = pkg && ((cond == PP_IFUSABLE) | use_loaded[pkg->index]);
2737 break;
2738 }
2739
H. Peter Anvine2c80182005-01-15 22:15:51 +00002740 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002741 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002742 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002743 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002744
2745 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002746 return (j ^ PP_COND_NEGATIVE(ct)) ? COND_IF_TRUE : COND_IF_FALSE;
H. Peter Anvin70653092007-10-19 14:42:29 -07002747
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002748fail:
2749 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002750 return COND_NEVER;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002751}
2752
2753/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002754 * Default smacro expansion routine: just returns a copy of the
2755 * expansion list.
2756 */
2757static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002758smacro_expand_default(const SMacro *s, Token **params, int nparams)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002759{
2760 (void)params;
2761 (void)nparams;
2762
2763 return dup_tlist(s->expansion, NULL);
2764}
2765
2766/*
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002767 * Emit a macro defintion or undef to the listing file, if
2768 * desired. This is similar to detoken(), but it handles the reverse
2769 * expansion list, does not expand %! or local variable tokens, and
2770 * does some special handling for macro parameters.
2771 */
2772static void
2773list_smacro_def(enum preproc_token op, const Context *ctx, const SMacro *m)
2774{
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002775 Token *t;
2776 size_t namelen, size;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002777 char *def, *p;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002778 char *context_prefix = NULL;
2779 size_t context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002780
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002781 namelen = strlen(m->name);
2782 size = namelen + 2; /* Include room for space after name + NUL */
2783
2784 if (ctx) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07002785 int context_depth = cstk->depth - ctx->depth + 1;
2786 context_prefix =
2787 nasm_asprintf("[%s::%"PRIu64"] %%%-*s",
2788 ctx->name ? ctx->name : "",
2789 ctx->number, context_depth, "");
2790
2791 context_len = nasm_last_string_len();
2792 memset(context_prefix + context_len - context_depth,
2793 '$', context_depth);
2794 size += context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002795 }
2796
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002797 list_for_each(t, m->expansion)
H. Peter Anvin8571f062019-09-23 16:40:03 -07002798 size += t->len;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002799
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002800 if (m->nparam) {
2801 /*
2802 * Space for ( and either , or ) around each
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002803 * parameter, plus up to 4 flags.
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002804 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002805 int i;
2806
2807 size += 1 + 4 * m->nparam;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002808 for (i = 0; i < m->nparam; i++)
H. Peter Anvin8571f062019-09-23 16:40:03 -07002809 size += m->params[i].name.len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002810 }
2811
2812 def = nasm_malloc(size);
2813 p = def+size;
2814 *--p = '\0';
2815
2816 list_for_each(t, m->expansion) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002817 p -= t->len;
2818 memcpy(p, tok_text(t), t->len);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002819 }
2820
2821 *--p = ' ';
2822
2823 if (m->nparam) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002824 int i;
2825
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002826 *--p = ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002827 for (i = m->nparam-1; i >= 0; i--) {
2828 enum sparmflags flags = m->params[i].flags;
2829 if (flags & SPARM_GREEDY)
2830 *--p = '+';
H. Peter Anvin8571f062019-09-23 16:40:03 -07002831 p -= m->params[i].name.len;
2832 memcpy(p, tok_text(&m->params[i].name), m->params[i].name.len);
2833
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002834 if (flags & SPARM_NOSTRIP)
2835 *--p = '!';
2836 if (flags & SPARM_STR)
2837 *--p = '&';
2838 if (flags & SPARM_EVAL)
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002839 *--p = '=';
2840 *--p = ',';
2841 }
2842 *p = '('; /* First parameter starts with ( not , */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002843 }
2844
2845 p -= namelen;
2846 memcpy(p, m->name, namelen);
2847
H. Peter Anvin6686de22019-08-10 05:33:14 -07002848 if (context_prefix) {
2849 p -= context_len;
2850 memcpy(p, context_prefix, context_len);
2851 nasm_free(context_prefix);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002852 }
2853
2854 nasm_listmsg("%s %s", pp_directives[op], p);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002855 nasm_free(def);
H. Peter Anvin6686de22019-08-10 05:33:14 -07002856}
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002857
2858/*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002859 * Parse smacro arguments, return argument count. If the tmpl argument
2860 * is set, set the nparam, greedy and params field in the template.
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002861 * *tpp is updated to point to the pointer to the first token after the
2862 * prototype.
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002863 *
2864 * The text values from any argument tokens are "stolen" and the
2865 * corresponding text fields set to NULL.
2866 */
2867static int parse_smacro_template(Token ***tpp, SMacro *tmpl)
2868{
2869 int nparam = 0;
2870 enum sparmflags flags;
2871 struct smac_param *params = NULL;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002872 bool err, done;
2873 bool greedy = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002874 Token **tn = *tpp;
2875 Token *t = *tn;
2876 Token *name;
2877
H. Peter Anvin (Intel)d4607842019-08-20 16:19:37 -07002878 /*
2879 * DO NOT skip whitespace here, or we won't be able to distinguish:
2880 *
2881 * %define foo (a,b) ; no arguments, (a,b) is the expansion
2882 * %define bar(a,b) ; two arguments, empty expansion
2883 *
2884 * This ambiguity was inherited from C.
2885 */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002886
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002887 if (!tok_is(t, '('))
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002888 goto finish;
2889
2890 if (tmpl) {
2891 Token *tx = t;
2892 Token **txpp = &tx;
2893 int sparam;
2894
2895 /* Count parameters first */
2896 sparam = parse_smacro_template(&txpp, NULL);
2897 if (!sparam)
2898 goto finish; /* No parameters, we're done */
2899 nasm_newn(params, sparam);
2900 }
2901
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002902 /* Skip leading paren */
2903 tn = &t->next;
2904 t = *tn;
2905
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002906 name = NULL;
2907 flags = 0;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002908 err = done = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002909
2910 while (!done) {
2911 if (!t || !t->type) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002912 if (name || flags)
2913 nasm_nonfatal("`)' expected to terminate macro template");
2914 else
2915 nasm_nonfatal("parameter identifier expected");
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002916 break;
2917 }
2918
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002919 switch (t->type) {
2920 case TOK_ID:
2921 if (name)
2922 goto bad;
2923 name = t;
2924 break;
2925
2926 case TOK_OTHER:
H. Peter Anvin8571f062019-09-23 16:40:03 -07002927 if (t->len != 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002928 goto bad;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002929 switch (t->text.a[0]) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002930 case '=':
2931 flags |= SPARM_EVAL;
2932 break;
2933 case '&':
2934 flags |= SPARM_STR;
2935 break;
2936 case '!':
2937 flags |= SPARM_NOSTRIP;
2938 break;
2939 case '+':
2940 flags |= SPARM_GREEDY;
2941 greedy = true;
2942 break;
2943 case ',':
2944 if (greedy)
2945 nasm_nonfatal("greedy parameter must be last");
2946 /* fall through */
2947 case ')':
2948 if (params) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002949 if (name)
2950 steal_Token(&params[nparam].name, name);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002951 params[nparam].flags = flags;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002952 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002953 nparam++;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002954 name = NULL;
2955 flags = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002956 done = t->text.a[0] == ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002957 break;
2958 default:
2959 goto bad;
2960 }
2961 break;
2962
2963 case TOK_WHITESPACE:
2964 break;
2965
2966 default:
2967 bad:
2968 if (!err) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002969 nasm_nonfatal("garbage `%s' in macro parameter list", tok_text(t));
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002970 err = true;
2971 }
2972 break;
2973 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002974
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002975 tn = &t->next;
2976 t = *tn;
2977 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002978
2979finish:
2980 while (t && t->type == TOK_WHITESPACE) {
2981 tn = &t->next;
2982 t = t->next;
2983 }
2984 *tpp = tn;
2985 if (tmpl) {
2986 tmpl->nparam = nparam;
2987 tmpl->greedy = greedy;
2988 tmpl->params = params;
2989 }
2990 return nparam;
2991}
2992
2993/*
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002994 * Common code for defining an smacro. The tmpl argument, if not NULL,
2995 * contains any macro parameters that aren't explicit arguments;
2996 * those are the more uncommon macro variants.
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002997 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002998static SMacro *define_smacro(const char *mname, bool casesense,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002999 Token *expansion, SMacro *tmpl)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003000{
3001 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003002 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003003 Context *ctx;
3004 bool defining_alias = false;
3005 unsigned int nparam = 0;
H. Peter Anvin70653092007-10-19 14:42:29 -07003006
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003007 if (tmpl) {
3008 defining_alias = tmpl->alias;
3009 nparam = tmpl->nparam;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003010 if (nparam && !defining_alias)
3011 mark_smac_params(expansion, tmpl, 0);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003012 }
3013
3014 while (1) {
3015 ctx = get_ctx(mname, &mname);
3016
H. Peter Anvind2354082019-08-27 16:38:48 -07003017 if (!smacro_defined(ctx, mname, nparam, &smac, casesense, true)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003018 /* Create a new macro */
3019 smtbl = ctx ? &ctx->localmac : &smacros;
3020 smhead = (SMacro **) hash_findi_add(smtbl, mname);
3021 nasm_new(smac);
3022 smac->next = *smhead;
3023 *smhead = smac;
3024 break;
3025 } else if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003026 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003027 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003028 /*
3029 * Some instances of the old code considered this a failure,
3030 * some others didn't. What is the right thing to do here?
3031 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003032 goto fail;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003033 } else if (!smac->alias || ppopt.noaliases || defining_alias) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003034 /*
3035 * We're redefining, so we have to take over an
3036 * existing SMacro structure. This means freeing
H. Peter Anvin8b262472019-02-26 14:00:54 -08003037 * what was already in it, but not the structure itself.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003038 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003039 clear_smacro(smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003040 break;
3041 } else if (smac->in_progress) {
3042 nasm_nonfatal("macro alias loop");
3043 goto fail;
3044 } else {
3045 /* It is an alias macro; follow the alias link */
3046 SMacro *s;
3047
3048 smac->in_progress = true;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003049 s = define_smacro(tok_text(smac->expansion), casesense,
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003050 expansion, tmpl);
3051 smac->in_progress = false;
3052 return s;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003053 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003054 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003055
3056 smac->name = nasm_strdup(mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003057 smac->casesense = casesense;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003058 smac->expansion = expansion;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003059 smac->expand = smacro_expand_default;
3060 if (tmpl) {
3061 smac->nparam = tmpl->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003062 smac->params = tmpl->params;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003063 smac->alias = tmpl->alias;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003064 smac->greedy = tmpl->greedy;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003065 if (tmpl->expand)
3066 smac->expand = tmpl->expand;
3067 }
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07003068 if (list_option('s')) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07003069 list_smacro_def((smac->alias ? PP_DEFALIAS : PP_DEFINE)
3070 + !casesense, ctx, smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003071 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08003072 return smac;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003073
3074fail:
3075 free_tlist(expansion);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003076 if (tmpl)
3077 free_smacro_members(tmpl);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003078 return NULL;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003079}
3080
3081/*
3082 * Undefine an smacro
3083 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003084static void undef_smacro(const char *mname, bool undefalias)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003085{
3086 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003087 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003088 Context *ctx;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003089
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003090 ctx = get_ctx(mname, &mname);
H. Peter Anvin166c2472008-05-28 12:28:58 -07003091 smtbl = ctx ? &ctx->localmac : &smacros;
3092 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07003093
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003094 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003095 /*
3096 * We now have a macro name... go hunt for it.
3097 */
3098 sp = smhead;
3099 while ((s = *sp) != NULL) {
3100 if (!mstrcmp(s->name, mname, s->casesense)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003101 if (s->alias && !undefalias) {
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003102 if (!ppopt.noaliases) {
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003103 if (s->in_progress) {
3104 nasm_nonfatal("macro alias loop");
3105 } else {
3106 s->in_progress = true;
3107 undef_smacro(tok_text(s->expansion), false);
3108 s->in_progress = false;
3109 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003110 }
3111 } else {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07003112 if (list_option('d'))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003113 list_smacro_def(s->alias ? PP_UNDEFALIAS : PP_UNDEF,
3114 ctx, s);
3115 *sp = s->next;
3116 free_smacro(s);
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003117 continue;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003118 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003119 }
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003120 sp = &s->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003121 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003122 }
3123}
3124
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003125/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07003126 * Parse a mmacro specification.
3127 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003128static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07003129{
H. Peter Anvina26433d2008-07-16 14:40:01 -07003130 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003131 tline = skip_white(tline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003132 tline = expand_id(tline);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003133 if (!tok_type(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003134 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003135 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003136 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02003137
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003138#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003139 def->prev = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003140#endif
H. Peter Anvin8571f062019-09-23 16:40:03 -07003141 def->name = dup_text(tline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003142 def->plus = false;
3143 def->nolist = false;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02003144 def->nparam_min = 0;
3145 def->nparam_max = 0;
3146
H. Peter Anvina26433d2008-07-16 14:40:01 -07003147 tline = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003148 tline = skip_white(tline);
3149 if (!tok_type(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003150 nasm_nonfatal("`%s' expects a parameter count", directive);
3151 else
H. Peter Anvin8571f062019-09-23 16:40:03 -07003152 def->nparam_min = def->nparam_max = read_param_count(tok_text(tline));
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003153 if (tline && tok_is(tline->next, '-')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003154 tline = tline->next->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003155 if (tok_is(tline, '*')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003156 def->nparam_max = INT_MAX;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003157 } else if (!tok_type(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003158 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003159 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003160 def->nparam_max = read_param_count(tok_text(tline));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003161 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003162 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03003163 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003164 }
3165 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003166 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003167 if (tline && tok_is(tline->next, '+')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003168 tline = tline->next;
3169 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003170 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003171 if (tline && tok_type(tline->next, TOK_ID) &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07003172 tline->next->len == 7 &&
3173 !nasm_stricmp(tline->next->text.a, ".nolist")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003174 tline = tline->next;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003175 def->nolist = !list_option('f') || istk->nolist;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003176 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003177
H. Peter Anvina26433d2008-07-16 14:40:01 -07003178 /*
3179 * Handle default parameters.
3180 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07003181 def->ndefs = 0;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003182 if (tline && tline->next) {
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07003183 Token **comma;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003184 def->dlist = tline->next;
3185 tline->next = NULL;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07003186 comma = count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
3187 if (!ppopt.sane_empty_expansion && comma) {
3188 *comma = NULL;
3189 def->ndefs--;
3190 nasm_warn(WARN_MACRO_PARAMS_LEGACY,
3191 "dropping trailing empty default parameter in defintion of multi-line macro `%s'",
3192 def->name);
3193 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003194 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003195 def->dlist = NULL;
3196 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003197 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003198 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003199
H. Peter Anvin89cee572009-07-15 09:16:54 -04003200 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003201 !def->plus) {
3202 /*
3203 *!macro-defaults [on] macros with more default than optional parameters
3204 *! warns when a macro has more default parameters than optional parameters.
3205 *! See \k{mlmacdef} for why might want to disable this warning.
3206 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003207 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003208 "too many default macro parameters in macro `%s'", def->name);
3209 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02003210
H. Peter Anvina26433d2008-07-16 14:40:01 -07003211 return true;
3212}
3213
3214
3215/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003216 * Decode a size directive
3217 */
3218static int parse_size(const char *str) {
3219 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003220 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003221 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003222 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03003223 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003224}
3225
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003226/*
3227 * Process a preprocessor %pragma directive. Currently there are none.
3228 * Gets passed the token list starting with the "preproc" token from
3229 * "%pragma preproc".
3230 */
3231static void do_pragma_preproc(Token *tline)
3232{
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003233 const char *txt;
3234
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003235 /* Skip to the real stuff */
3236 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003237 tline = skip_white(tline);
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003238
3239 if (!tok_type(tline, TOK_ID))
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003240 return;
3241
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003242 txt = tok_text(tline);
3243 if (!nasm_stricmp(txt, "sane_empty_expansion")) {
3244 tline = skip_white(tline->next);
3245 ppopt.sane_empty_expansion =
3246 pp_get_boolean_option(tline, ppopt.sane_empty_expansion);
3247 } else {
3248 /* Unknown pragma, ignore for now */
3249 }
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003250}
3251
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003252static bool is_macro_id(const Token *t)
3253{
H. Peter Anvin8571f062019-09-23 16:40:03 -07003254 return tok_type(t, TOK_ID) || tok_type(t, TOK_LOCAL_MACRO);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003255}
3256
H. Peter Anvin8571f062019-09-23 16:40:03 -07003257static const char *get_id(Token **tp, const char *dname)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003258{
H. Peter Anvin8571f062019-09-23 16:40:03 -07003259 const char *id;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003260 Token *t = *tp;
3261
3262 t = t->next; /* Skip directive */
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 t = expand_id(t);
3265
3266 if (!is_macro_id(t)) {
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003267 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003268 return NULL;
3269 }
3270
H. Peter Anvin8571f062019-09-23 16:40:03 -07003271 id = tok_text(t);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003272 t = skip_white(t);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003273 *tp = t;
3274 return id;
3275}
3276
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003277/* Parse a %use package name and find the package. Set *err on syntax error. */
3278static const struct use_package *
H. Peter Anvin8571f062019-09-23 16:40:03 -07003279get_use_pkg(Token *t, const char *dname, const char **name)
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003280{
H. Peter Anvin8571f062019-09-23 16:40:03 -07003281 const char *id;
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003282
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003283 t = skip_white(t);
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003284 t = expand_smacro(t);
3285
H. Peter Anvin8571f062019-09-23 16:40:03 -07003286 *name = NULL;
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003287
H. Peter Anvin8571f062019-09-23 16:40:03 -07003288 if (!t) {
3289 nasm_nonfatal("`%s' expects a package name, got end of line", dname);
3290 return NULL;
3291 } else if (t->type != TOK_ID && t->type != TOK_STRING) {
3292 nasm_nonfatal("`%s' expects a package name, got `%s'",
3293 dname, tok_text(t));
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003294 return NULL;
3295 }
3296
H. Peter Anvin8571f062019-09-23 16:40:03 -07003297 *name = id = unquote_token(t);
3298
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003299 t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003300 t = skip_white(t);
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003301 if (t)
3302 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
3303
3304 return nasm_find_use_package(id);
3305}
3306
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003307/*
3308 * Mark parameter tokens in an smacro definition. If the type argument
3309 * is 0, create smac param tokens, otherwise use the type specified;
3310 * normally this is used for TOK_XDEF_PARAM, which is used to protect
3311 * parameter tokens during expansion during %xdefine.
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003312 *
3313 * tmpl may not be NULL here.
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003314 */
3315static void mark_smac_params(Token *tline, const SMacro *tmpl,
3316 enum pp_token_type type)
3317{
3318 const struct smac_param *params = tmpl->params;
3319 int nparam = tmpl->nparam;
3320 Token *t;
3321 int i;
3322
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003323 list_for_each(t, tline) {
3324 if (t->type != TOK_ID && t->type != TOK_XDEF_PARAM)
3325 continue;
3326
3327 for (i = 0; i < nparam; i++) {
3328 if (tok_text_match(t, &params[i].name))
3329 t->type = type ? type : tok_smac_param(i);
3330 }
3331 }
3332}
3333
Ed Beroset3ab3f412002-06-11 03:31:49 +00003334/**
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003335 * %clear selected macro sets either globally or in contexts
3336 */
3337static void do_clear(enum clear_what what, bool context)
3338{
3339 if (context) {
3340 if (what & CLEAR_ALLDEFINE) {
3341 Context *ctx;
3342 list_for_each(ctx, cstk)
3343 clear_smacro_table(&ctx->localmac, what);
3344 }
3345 /* Nothing else can be context-local */
3346 } else {
3347 if (what & CLEAR_ALLDEFINE)
3348 clear_smacro_table(&smacros, what);
3349 if (what & CLEAR_MMACRO)
3350 free_mmacro_table(&mmacros);
3351 }
3352}
3353
3354/**
Ed Beroset3ab3f412002-06-11 03:31:49 +00003355 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003356 * Find out if a line contains a preprocessor directive, and deal
3357 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07003358 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00003359 * If a directive _is_ found, it is the responsibility of this routine
3360 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003361 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00003362 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003363 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00003364 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07003365 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003366 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07003367static int do_directive(Token *tline, Token **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003368{
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003369 enum preproc_token op;
H. Peter Anvin4169a472007-09-12 01:29:43 +00003370 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07003371 bool err;
H. Peter Anvin70055962007-10-11 00:05:31 -07003372 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003373 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07003374 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003375 int offset;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003376 const char *p;
3377 char *q, *qbuf;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003378 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003379 const char *mname;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003380 struct ppscan pps;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003381 Include *inc;
3382 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003383 Cond *cond;
3384 MMacro *mmac, **mmhead;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003385 Token *t = NULL, *tt, *macro_start, *last, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003386 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003387 struct tokenval tokval;
3388 expr *evalresult;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003389 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003390 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08003391 errflags severity;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003392 const char *dname; /* Name of directive, for messages */
H. Peter Anvin76690a12002-04-30 20:52:49 +00003393
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003394 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00003395 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003396
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003397 tline = skip_white(tline);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003398 if (!tline || !tok_type(tline, TOK_PREPROC_ID))
3399 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003400
H. Peter Anvin8571f062019-09-23 16:40:03 -07003401 dname = tok_text(tline);
3402 if (dname[1] == '%')
3403 return NO_DIRECTIVE_FOUND;
3404
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003405 op = pp_token_hash(dname);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003406
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003407 casesense = true;
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003408 if (PP_HAS_CASE(op) & PP_INSENSITIVE(op)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003409 casesense = false;
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003410 op--;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003411 }
3412
3413 /*
3414 * If we're in a non-emitting branch of a condition construct,
3415 * or walking to the end of an already terminated %rep block,
3416 * we should ignore all directives except for condition
3417 * directives.
3418 */
3419 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003420 (istk->mstk.mstk && !istk->mstk.mstk->in_progress)) &&
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003421 !is_condition(op)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003422 return NO_DIRECTIVE_FOUND;
3423 }
3424
3425 /*
3426 * If we're defining a macro or reading a %rep block, we should
3427 * ignore all directives except for %macro/%imacro (which nest),
3428 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
3429 * If we're in a %rep block, another %rep nests, so should be let through.
3430 */
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003431 if (defining && op != PP_MACRO && op != PP_RMACRO &&
3432 op != PP_ENDMACRO && op != PP_ENDM &&
3433 (defining->name || (op != PP_ENDREP && op != PP_REP))) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003434 return NO_DIRECTIVE_FOUND;
3435 }
3436
3437 if (defining) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003438 if (op == PP_MACRO || op == PP_RMACRO) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003439 nested_mac_count++;
3440 return NO_DIRECTIVE_FOUND;
3441 } else if (nested_mac_count > 0) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003442 if (op == PP_ENDMACRO) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003443 nested_mac_count--;
3444 return NO_DIRECTIVE_FOUND;
3445 }
3446 }
3447 if (!defining->name) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003448 if (op == PP_REP) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003449 nested_rep_count++;
3450 return NO_DIRECTIVE_FOUND;
3451 } else if (nested_rep_count > 0) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003452 if (op == PP_ENDREP) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003453 nested_rep_count--;
3454 return NO_DIRECTIVE_FOUND;
3455 }
3456 }
3457 }
3458 }
3459
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003460 switch (op) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003461 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003462 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003463 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003464
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003465 case PP_PRAGMA:
3466 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003467 * %pragma namespace options...
3468 *
3469 * The namespace "preproc" is reserved for the preprocessor;
3470 * all other namespaces generate a [pragma] assembly directive.
3471 *
3472 * Invalid %pragmas are ignored and may have different
3473 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003474 */
H. Peter Anvinf5d7d902019-08-10 06:21:00 -07003475 t = tline;
3476 tline = tline->next;
3477 t->next = NULL;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003478 tline = zap_white(expand_smacro(tline));
3479 if (tok_type(tline, TOK_ID)) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003480 if (!nasm_stricmp(tok_text(tline), "preproc")) {
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003481 /* Preprocessor pragma */
3482 do_pragma_preproc(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07003483 free_tlist(tline);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003484 } else {
3485 /* Build the assembler directive */
H. Peter Anvin06335872019-08-10 06:42:55 -07003486
3487 /* Append bracket to the end of the output */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003488 for (t = tline; t->next; t = t->next)
3489 ;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003490 t->next = make_tok_char(NULL, ']');
H. Peter Anvin06335872019-08-10 06:42:55 -07003491
3492 /* Prepend "[pragma " */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003493 t = new_White(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07003494 t = new_Token(t, TOK_ID, "pragma", 6);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003495 t = make_tok_char(t, '[');
H. Peter Anvin06335872019-08-10 06:42:55 -07003496 tline = t;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07003497 *output = tline;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003498 }
3499 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003500 break;
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003501
H. Peter Anvine2c80182005-01-15 22:15:51 +00003502 case PP_STACKSIZE:
3503 /* Directive to tell NASM what the default stack size is. The
3504 * default is for a 16-bit stack, and this can be overriden with
3505 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003506 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003507 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003508 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003509 nasm_nonfatal("`%s' missing size parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003510 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07003511 if (nasm_stricmp(tok_text(tline), "flat") == 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003512 /* All subsequent ARG directives are for a 32-bit stack */
3513 StackSize = 4;
3514 StackPointer = "ebp";
3515 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003516 LocalOffset = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003517 } else if (nasm_stricmp(tok_text(tline), "flat64") == 0) {
Charles Crayne7eaf9192007-11-08 22:11:14 -08003518 /* All subsequent ARG directives are for a 64-bit stack */
3519 StackSize = 8;
3520 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03003521 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08003522 LocalOffset = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003523 } else if (nasm_stricmp(tok_text(tline), "large") == 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003524 /* All subsequent ARG directives are for a 16-bit stack,
3525 * far function call.
3526 */
3527 StackSize = 2;
3528 StackPointer = "bp";
3529 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003530 LocalOffset = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003531 } else if (nasm_stricmp(tok_text(tline), "small") == 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003532 /* All subsequent ARG directives are for a 16-bit stack,
3533 * far function call. We don't support near functions.
3534 */
3535 StackSize = 2;
3536 StackPointer = "bp";
3537 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003538 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003539 } else {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003540 nasm_nonfatal("`%s' invalid size type", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003541 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003542 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003543
H. Peter Anvine2c80182005-01-15 22:15:51 +00003544 case PP_ARG:
3545 /* TASM like ARG directive to define arguments to functions, in
3546 * the following form:
3547 *
3548 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
3549 */
3550 offset = ArgOffset;
3551 do {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003552 const char *arg;
3553 char directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003554 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003555
H. Peter Anvine2c80182005-01-15 22:15:51 +00003556 /* Find the argument name */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003557 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003558 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003559 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003560 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003561 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07003562 arg = tok_text(tline);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003563
H. Peter Anvine2c80182005-01-15 22:15:51 +00003564 /* Find the argument size type */
3565 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003566 if (!tok_is(tline, ':')) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003567 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003568 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003569 }
3570 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003571 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003572 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003573 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003574 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003575
H. Peter Anvine2c80182005-01-15 22:15:51 +00003576 /* Allow macro expansion of type parameter */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003577 tt = tokenize(tok_text(tline));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003578 tt = expand_smacro(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003579 size = parse_size(tok_text(tt));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003580 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003581 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003582 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003583 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003584 }
3585 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003586
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003587 /* Round up to even stack slots */
3588 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003589
H. Peter Anvine2c80182005-01-15 22:15:51 +00003590 /* Now define the macro for the argument */
3591 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
3592 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003593 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003594 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003595
H. Peter Anvine2c80182005-01-15 22:15:51 +00003596 /* Move to the next argument in the list */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003597 tline = skip_white(tline->next);
3598 } while (tok_is(tline, ','));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003599 ArgOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003600 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003601
H. Peter Anvine2c80182005-01-15 22:15:51 +00003602 case PP_LOCAL:
3603 /* TASM like LOCAL directive to define local variables for a
3604 * function, in the following form:
3605 *
3606 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
3607 *
3608 * The '= LocalSize' at the end is ignored by NASM, but is
3609 * required by TASM to define the local parameter size (and used
3610 * by the TASM macro package).
3611 */
3612 offset = LocalOffset;
3613 do {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003614 const char *local;
3615 char directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003616 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003617
H. Peter Anvine2c80182005-01-15 22:15:51 +00003618 /* Find the argument name */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003619 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003620 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003621 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003622 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003623 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07003624 local = tok_text(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003625
H. Peter Anvine2c80182005-01-15 22:15:51 +00003626 /* Find the argument size type */
3627 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003628 if (!tok_is(tline, ':')) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003629 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003630 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003631 }
3632 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003633 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003634 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003635 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003636 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003637
H. Peter Anvine2c80182005-01-15 22:15:51 +00003638 /* Allow macro expansion of type parameter */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003639 tt = tokenize(tok_text(tline));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003640 tt = expand_smacro(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003641 size = parse_size(tok_text(tt));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003642 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003643 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003644 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003645 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003646 }
3647 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003648
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003649 /* Round up to even stack slots */
3650 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003651
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003652 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003653
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003654 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003655 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
3656 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003657 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003658
H. Peter Anvine2c80182005-01-15 22:15:51 +00003659 /* Now define the assign to setup the enter_c macro correctly */
3660 snprintf(directive, sizeof(directive),
3661 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003662 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003663
H. Peter Anvine2c80182005-01-15 22:15:51 +00003664 /* Move to the next argument in the list */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003665 tline = skip_white(tline->next);
3666 } while (tok_is(tline, ','));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003667 LocalOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003668 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003669
H. Peter Anvine2c80182005-01-15 22:15:51 +00003670 case PP_CLEAR:
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003671 {
3672 bool context = false;
3673
3674 t = tline->next = expand_smacro(tline->next);
3675 t = skip_white(t);
3676 if (!t) {
3677 /* Emulate legacy behavior */
3678 do_clear(CLEAR_DEFINE|CLEAR_MMACRO, false);
3679 } else {
3680 while ((t = skip_white(t)) && t->type == TOK_ID) {
3681 const char *txt = tok_text(t);
3682 if (!nasm_stricmp(txt, "all")) {
3683 do_clear(CLEAR_ALL, context);
3684 } else if (!nasm_stricmp(txt, "define") ||
3685 !nasm_stricmp(txt, "def") ||
3686 !nasm_stricmp(txt, "smacro")) {
3687 do_clear(CLEAR_DEFINE, context);
3688 } else if (!nasm_stricmp(txt, "defalias") ||
3689 !nasm_stricmp(txt, "alias") ||
3690 !nasm_stricmp(txt, "salias")) {
3691 do_clear(CLEAR_DEFALIAS, context);
3692 } else if (!nasm_stricmp(txt, "alldef") ||
3693 !nasm_stricmp(txt, "alldefine")) {
3694 do_clear(CLEAR_ALLDEFINE, context);
3695 } else if (!nasm_stricmp(txt, "macro") ||
3696 !nasm_stricmp(txt, "mmacro")) {
3697 do_clear(CLEAR_MMACRO, context);
3698 } else if (!nasm_stricmp(txt, "context") ||
3699 !nasm_stricmp(txt, "ctx")) {
3700 context = true;
3701 } else if (!nasm_stricmp(txt, "global")) {
3702 context = false;
3703 } else if (!nasm_stricmp(txt, "nothing") ||
3704 !nasm_stricmp(txt, "none") ||
3705 !nasm_stricmp(txt, "ignore") ||
3706 !nasm_stricmp(txt, "-") ||
3707 !nasm_stricmp(txt, "--")) {
3708 /* Do nothing */
3709 } else {
3710 nasm_nonfatal("invalid option to %s: %s", dname, txt);
3711 t = NULL;
3712 }
3713 }
3714 }
3715
3716 t = skip_white(t);
3717 if (t)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003718 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003719 break;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003720 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003721
H. Peter Anvin418ca702008-05-30 10:42:30 -07003722 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003723 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003724 t = skip_white(t);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003725 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003726 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003727 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003728 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003729 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003730 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003731 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003732
3733 strlist_add(deplist, unquote_token_cstr(t));
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003734 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003735
3736 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003737 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003738 t = skip_white(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07003739
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003740 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003741 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003742 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003743 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003744 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003745 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003746 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003747 p = unquote_token_cstr(t);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003748 nasm_new(inc);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003749 inc->next = istk;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003750 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07003751 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08003752 (pp_mode == PP_DEPS)
3753 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003754 if (!inc->fp) {
3755 /* -MG given but file not found */
3756 nasm_free(inc);
3757 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003758 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003759 inc->lineno = src_set_linnum(0);
3760 inc->lineinc = 1;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003761 inc->nolist = istk->nolist;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003762 istk = inc;
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003763 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003764 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003765 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003766
H. Peter Anvind2456592008-06-19 15:04:18 -07003767 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003768 {
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003769 const struct use_package *pkg;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003770 const char *name;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003771
H. Peter Anvin8571f062019-09-23 16:40:03 -07003772 pkg = get_use_pkg(tline->next, dname, &name);
3773 if (!name)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003774 goto done;
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003775 if (!pkg) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003776 nasm_nonfatal("unknown `%s' package: `%s'", dname, name);
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003777 } else if (!use_loaded[pkg->index]) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003778 /*
3779 * Not already included, go ahead and include it.
3780 * Treat it as an include file for the purpose of
3781 * producing a listing.
3782 */
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003783 use_loaded[pkg->index] = true;
3784 stdmacpos = pkg->macros;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003785 nasm_new(inc);
3786 inc->next = istk;
3787 inc->fname = src_set_fname(NULL);
3788 inc->lineno = src_set_linnum(0);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003789 inc->nolist = !list_option('b') || istk->nolist;
3790 istk = inc;
3791 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003792 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003793 break;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003794 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003795 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003796 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07003797 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003798 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003799 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003800 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003801 if (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003802 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003803 nasm_nonfatal("`%s' expects a context identifier", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003804 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003805 }
3806 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003807 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003808 dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003809 p = tok_text(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003810 } else {
3811 p = NULL; /* Anonymous */
3812 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07003813
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003814 if (op == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08003815 nasm_new(ctx);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003816 ctx->depth = cstk ? cstk->depth + 1 : 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003817 ctx->next = cstk;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003818 ctx->name = p ? nasm_strdup(p) : NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003819 ctx->number = unique++;
3820 cstk = ctx;
3821 } else {
3822 /* %pop or %repl */
3823 if (!cstk) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003824 nasm_nonfatal("`%s': context stack is empty", dname);
3825 } else if (op == PP_POP) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003826 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003827 nasm_nonfatal("`%s' in wrong context: %s, "
H. Peter Anvin8b262472019-02-26 14:00:54 -08003828 "expected %s",
3829 dname, cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003830 else
3831 ctx_pop();
3832 } else {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003833 /* op == PP_REPL */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003834 nasm_free((char *)cstk->name);
3835 cstk->name = p ? nasm_strdup(p) : NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003836 p = NULL;
3837 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003838 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003839 break;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003840 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003841 severity = ERR_FATAL;
3842 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003843 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003844 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003845 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003846 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003847 /*!
3848 *!user [on] %warning directives
3849 *! controls output of \c{%warning} directives (see \k{pperror}).
3850 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003851 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003852 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003853
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003854issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07003855 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003856 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003857 tline->next = expand_smacro(tline->next);
3858 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003859 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003860 t = tline ? tline->next : NULL;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003861 t = skip_white(t);
3862 if (tok_type(tline, TOK_STRING) && !t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003863 /* The line contains only a quoted string */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003864 p = unquote_token(tline); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08003865 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003866 } else {
3867 /* Not a quoted string, or more than a quoted string */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003868 q = detoken(tline, false);
3869 nasm_error(severity, "%s", q);
3870 nasm_free(q);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003871 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003872 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003873 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003874
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003875 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003876 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003877 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003878 else {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003879 j = if_condition(tline->next, op);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003880 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003881 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003882 cond = nasm_malloc(sizeof(Cond));
3883 cond->next = istk->conds;
3884 cond->state = j;
3885 istk->conds = cond;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003886 if(istk->mstk.mstk)
3887 istk->mstk.mstk->condcnt++;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003888 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003889
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003890 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003891 if (!istk->conds)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003892 nasm_fatal("`%s': no matching `%%if'", dname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003893 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003894 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003895 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003896 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003897
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003898 case COND_DONE:
3899 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003900 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003901
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003902 case COND_ELSE_TRUE:
3903 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003904 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003905 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003906 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003907 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003908
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003909 case COND_IF_FALSE:
3910 /*
3911 * IMPORTANT: In the case of %if, we will already have
3912 * called expand_mmac_params(); however, if we're
3913 * processing an %elif we must have been in a
3914 * non-emitting mode, which would have inhibited
3915 * the normal invocation of expand_mmac_params().
3916 * Therefore, we have to do it explicitly here.
3917 */
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003918 j = if_condition(expand_mmac_params(tline->next), op);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003919 tline->next = NULL; /* it got freed */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003920 istk->conds->state = j;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003921 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003922 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003923 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003924
H. Peter Anvine2c80182005-01-15 22:15:51 +00003925 case PP_ELSE:
3926 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003927 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003928 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003929 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07003930 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003931 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003932 case COND_IF_TRUE:
3933 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003934 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003935 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003936
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003937 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003938 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003939
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003940 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003941 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003942 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003943
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003944 case COND_ELSE_TRUE:
3945 case COND_ELSE_FALSE:
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 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003948 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003949 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003950 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003951 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003952
H. Peter Anvine2c80182005-01-15 22:15:51 +00003953 case PP_ENDIF:
3954 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003955 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003956 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003957 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003958 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003959 cond = istk->conds;
3960 istk->conds = cond->next;
3961 nasm_free(cond);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003962 if(istk->mstk.mstk)
3963 istk->mstk.mstk->condcnt--;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003964 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003965
H. Peter Anvin8b262472019-02-26 14:00:54 -08003966 case PP_RMACRO:
3967 case PP_MACRO:
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07003968 {
3969 MMacro *def;
3970
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003971 nasm_assert(!defining);
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07003972 nasm_new(def);
3973 def->casesense = casesense;
3974 def->dstk.mmac = defining;
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003975 if (op == PP_RMACRO)
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07003976 def->max_depth = nasm_limit[LIMIT_MACRO_LEVELS];
3977 if (!parse_mmacro_spec(tline, def, dname)) {
3978 nasm_free(def);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003979 goto done;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003980 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003981
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07003982 defining = def;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07003983 src_get(&defining->xline, &defining->fname);
3984
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003985 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
3986 while (mmac) {
3987 if (!strcmp(mmac->name, defining->name) &&
3988 (mmac->nparam_min <= defining->nparam_max
3989 || defining->plus)
3990 && (defining->nparam_min <= mmac->nparam_max
3991 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003992 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003993 defining->name);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003994 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003995 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003996 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003997 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003998 break;
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07003999 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004000
H. Peter Anvine2c80182005-01-15 22:15:51 +00004001 case PP_ENDM:
4002 case PP_ENDMACRO:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004003 if (!(defining && defining->name)) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004004 nasm_nonfatal("`%s': not defining a macro", tok_text(tline));
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004005 goto done;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004006 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004007 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
4008 defining->next = *mmhead;
4009 *mmhead = defining;
4010 defining = NULL;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004011 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004012
H. Peter Anvin89cee572009-07-15 09:16:54 -04004013 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004014 /*
4015 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004016 * macro-end marker for a macro with a name. Then we
4017 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004018 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004019 list_for_each(l, istk->expansion)
4020 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004021 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004022
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004023 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004024 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004025 * Remove all conditional entries relative to this
4026 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004027 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004028 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
4029 cond = istk->conds;
4030 istk->conds = cond->next;
4031 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004032 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004033 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004034 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004035 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004036 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004037 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05004038
H. Peter Anvina26433d2008-07-16 14:40:01 -07004039 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08004040 casesense = false;
4041 /* fall through */
4042 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07004043 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004044 MMacro **mmac_p;
4045 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07004046
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004047 nasm_zero(spec);
H. Peter Anvin8b262472019-02-26 14:00:54 -08004048 spec.casesense = casesense;
4049 if (!parse_mmacro_spec(tline, &spec, dname)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004050 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004051 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004052 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
4053 while (mmac_p && *mmac_p) {
4054 mmac = *mmac_p;
4055 if (mmac->casesense == spec.casesense &&
4056 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
4057 mmac->nparam_min == spec.nparam_min &&
4058 mmac->nparam_max == spec.nparam_max &&
4059 mmac->plus == spec.plus) {
4060 *mmac_p = mmac->next;
4061 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004062 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004063 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004064 }
4065 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004066 free_tlist(spec.dlist);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004067 break;
H. Peter Anvina26433d2008-07-16 14:40:01 -07004068 }
4069
H. Peter Anvine2c80182005-01-15 22:15:51 +00004070 case PP_ROTATE:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004071 while (tok_white(tline->next))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004072 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04004073 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004074 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004075 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00004076 return DIRECTIVE_FOUND;
4077 }
4078 t = expand_smacro(tline->next);
4079 tline->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004080 pps.tptr = tline = t;
4081 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004082 tokval.t_type = TOKEN_INVALID;
4083 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004084 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004085 free_tlist(tline);
4086 if (!evalresult)
4087 return DIRECTIVE_FOUND;
4088 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004089 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00004090 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004091 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00004092 return DIRECTIVE_FOUND;
4093 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004094 mmac = istk->mstk.mmac;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004095 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004096 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004097 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004098 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004099 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004100 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004101
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004102 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004103 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004104 rotate += mmac->nparam;
4105
4106 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004107 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004108 break;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004109
H. Peter Anvine2c80182005-01-15 22:15:51 +00004110 case PP_REP:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004111 {
4112 MMacro *tmp_defining;
4113
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004114 nolist = false;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004115 tline = skip_white(tline->next);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004116 if (tok_type(tline, TOK_ID) && tline->len == 7 &&
4117 !nasm_memicmp(tline->text.a, ".nolist", 7)) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07004118 nolist = !list_option('f') || istk->nolist;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004119 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004120 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004121
H. Peter Anvine2c80182005-01-15 22:15:51 +00004122 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08004123 pps.tptr = expand_smacro(tline);
4124 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004125 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004126 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004127 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004128 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004129 if (!evalresult)
4130 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004131 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004132 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00004133 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004134 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004135 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004136 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04004137 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07004138 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004139 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
4140 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04004141 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07004142 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08004143 /*!
4144 *!negative-rep [on] regative %rep count
4145 *! warns about negative counts given to the \c{%rep}
4146 *! preprocessor directive.
4147 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08004148 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07004149 "negative `%%rep' count: %"PRId64, count);
4150 count = 0;
4151 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04004152 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07004153 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004154 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004155 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07004156 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004157 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004158 tmp_defining = defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07004159 nasm_new(defining);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004160 defining->nolist = nolist;
4161 defining->in_progress = count;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004162 defining->mstk = istk->mstk;
4163 defining->dstk.mstk = tmp_defining;
4164 defining->dstk.mmac = tmp_defining ? tmp_defining->dstk.mmac : NULL;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07004165 src_get(&defining->xline, &defining->fname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004166 break;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004167 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004168
H. Peter Anvine2c80182005-01-15 22:15:51 +00004169 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004170 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004171 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004172 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004173 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004174
H. Peter Anvine2c80182005-01-15 22:15:51 +00004175 /*
4176 * Now we have a "macro" defined - although it has no name
4177 * and we won't be entering it in the hash tables - we must
4178 * push a macro-end marker for it on to istk->expansion.
4179 * After that, it will take care of propagating itself (a
4180 * macro-end marker line for a macro which is really a %rep
4181 * block will cause the macro to be re-expanded, complete
4182 * with another macro-end marker to ensure the process
4183 * continues) until the whole expansion is forcibly removed
4184 * from istk->expansion by a %exitrep.
4185 */
H. Peter Anvin6686de22019-08-10 05:33:14 -07004186 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004187 l->next = istk->expansion;
4188 l->finishes = defining;
4189 l->first = NULL;
4190 istk->expansion = l;
4191
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004192 istk->mstk.mstk = defining;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004193
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07004194 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004195 defining = defining->dstk.mstk;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004196 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004197
H. Peter Anvine2c80182005-01-15 22:15:51 +00004198 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004199 /*
4200 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004201 * macro-end marker for a macro with no name. Then we set
4202 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004203 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004204 list_for_each(l, istk->expansion)
4205 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004206 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004207
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004208 if (l)
H. Peter Anvind983b622019-10-07 21:19:32 -07004209 l->finishes->in_progress = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004210 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004211 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004212 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004213
H. Peter Anvin8b262472019-02-26 14:00:54 -08004214 case PP_DEFINE:
4215 case PP_XDEFINE:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004216 case PP_DEFALIAS:
H. Peter Anvin8b262472019-02-26 14:00:54 -08004217 {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004218 SMacro tmpl;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004219 Token **lastp;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07004220 int nparam;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07004221
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004222 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004223 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004224
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004225 nasm_zero(tmpl);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004226 lastp = &tline->next;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07004227 nparam = parse_smacro_template(&lastp, &tmpl);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004228 tline = *lastp;
4229 *lastp = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004230
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004231 if (unlikely(op == PP_DEFALIAS)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004232 macro_start = tline;
4233 if (!is_macro_id(macro_start)) {
4234 nasm_nonfatal("`%s' expects a macro identifier to alias",
4235 dname);
4236 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004237 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004238 tt = macro_start->next;
4239 macro_start->next = NULL;
4240 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004241 tline = skip_white(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004242 if (tline && tline->type) {
4243 nasm_warn(WARN_OTHER,
4244 "trailing garbage after aliasing identifier ignored");
4245 }
4246 free_tlist(tt);
4247 tmpl.alias = true;
4248 } else {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004249 if (op == PP_XDEFINE) {
4250 /* Protect macro parameter tokens */
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07004251 if (nparam)
4252 mark_smac_params(tline, &tmpl, TOK_XDEF_PARAM);
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004253 tline = expand_smacro(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004254 }
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004255 /* NB: Does this still make sense? */
4256 macro_start = reverse_tokens(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004257 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004258
H. Peter Anvine2c80182005-01-15 22:15:51 +00004259 /*
4260 * Good. We now have a macro name, a parameter count, and a
4261 * token list (in reverse order) for an expansion. We ought
4262 * to be OK just to create an SMacro, store it, and let
4263 * free_tlist have the rest of the line (which we have
4264 * carefully re-terminated after chopping off the expansion
4265 * from the end).
4266 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004267 define_smacro(mname, casesense, macro_start, &tmpl);
4268 break;
4269 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004270
H. Peter Anvine2c80182005-01-15 22:15:51 +00004271 case PP_UNDEF:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004272 case PP_UNDEFALIAS:
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;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004275 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004276 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004277
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004278 undef_smacro(mname, op == PP_UNDEFALIAS);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004279 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004280
H. Peter Anvin8b262472019-02-26 14:00:54 -08004281 case PP_DEFSTR:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004282 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004283 goto done;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004284
H. Peter Anvin9e200162008-06-04 17:23:14 -07004285 last = tline;
4286 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004287 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004288
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004289 tline = zap_white(tline);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004290 q = detoken(tline, false);
4291 macro_start = make_tok_qstr(NULL, q);
4292 nasm_free(q);
H. Peter Anvin9e200162008-06-04 17:23:14 -07004293
4294 /*
4295 * We now have a macro name, an implicit parameter count of
4296 * zero, and a string token to use as an expansion. Create
4297 * and store an SMacro.
4298 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004299 define_smacro(mname, casesense, macro_start, NULL);
4300 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004301
H. Peter Anvin8b262472019-02-26 14:00:54 -08004302 case PP_DEFTOK:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004303 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004304 goto done;
4305
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004306 last = tline;
4307 tline = expand_smacro(tline->next);
4308 last->next = NULL;
4309
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004310 t = skip_white(tline);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004311 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004312 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004313 nasm_nonfatal("`%s' requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004314 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004315 goto done;
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004316 }
4317
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04004318 /*
4319 * Convert the string to a token stream. Note that smacros
4320 * are stored with the token stream reversed, so we have to
4321 * reverse the output of tokenize().
4322 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07004323 macro_start = reverse_tokens(tokenize(unquote_token_cstr(t)));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004324
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004325 /*
4326 * We now have a macro name, an implicit parameter count of
4327 * zero, and a numeric token to use as an expansion. Create
4328 * and store an SMacro.
4329 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004330 define_smacro(mname, casesense, macro_start, NULL);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004331 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004332 break;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004333
H. Peter Anvin418ca702008-05-30 10:42:30 -07004334 case PP_PATHSEARCH:
4335 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07004336 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004337
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004338 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004339 goto done;
4340
H. Peter Anvin418ca702008-05-30 10:42:30 -07004341 last = tline;
4342 tline = expand_smacro(tline->next);
4343 last->next = NULL;
4344
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004345 t = skip_white(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004346 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004347 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004348 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004349 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004350 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004351 }
4352 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004353 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004354
4355 p = unquote_token_cstr(t);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004356
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07004357 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07004358 if (!found_path)
4359 found_path = p;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004360 macro_start = make_tok_qstr(NULL, found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004361
4362 /*
4363 * We now have a macro name, an implicit parameter count of
4364 * zero, and a string token to use as an expansion. Create
4365 * and store an SMacro.
4366 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004367 define_smacro(mname, casesense, macro_start, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004368 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004369 break;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004370 }
4371
H. Peter Anvine2c80182005-01-15 22:15:51 +00004372 case PP_STRLEN:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004373 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004374 goto done;
4375
H. Peter Anvine2c80182005-01-15 22:15:51 +00004376 last = tline;
4377 tline = expand_smacro(tline->next);
4378 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004379
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004380 t = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004381 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004382 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004383 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004384 free_tlist(tline);
4385 free_tlist(origline);
4386 return DIRECTIVE_FOUND;
4387 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004388
H. Peter Anvin8571f062019-09-23 16:40:03 -07004389 unquote_token(t);
4390 macro_start = make_tok_num(NULL, t->len);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004391
H. Peter Anvine2c80182005-01-15 22:15:51 +00004392 /*
4393 * We now have a macro name, an implicit parameter count of
4394 * zero, and a numeric token to use as an expansion. Create
4395 * and store an SMacro.
4396 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004397 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004398 free_tlist(tline);
4399 free_tlist(origline);
4400 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004401
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004402 case PP_STRCAT:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004403 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004404 goto done;
4405
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004406 last = tline;
4407 tline = expand_smacro(tline->next);
4408 last->next = NULL;
4409
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004410 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004411 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004412 switch (t->type) {
4413 case TOK_WHITESPACE:
4414 break;
4415 case TOK_STRING:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004416 unquote_token(t);
4417 len += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004418 break;
4419 case TOK_OTHER:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004420 if (tok_is(t, ',')) /* permit comma separators */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004421 break;
4422 /* else fall through */
4423 default:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004424 nasm_nonfatal("non-string passed to `%s': %s", dname,
4425 tok_text(t));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004426 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004427 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004428 }
4429 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004430
H. Peter Anvin (Intel)f770ce82019-10-17 18:22:43 -07004431 q = qbuf = nasm_malloc(len+1);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004432 list_for_each(t, tline) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004433 if (t->type == TOK_INTERNAL_STRING)
4434 q = mempcpy(q, tok_text(t), t->len);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004435 }
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07004436 *q = '\0';
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004437
4438 /*
4439 * We now have a macro name, an implicit parameter count of
4440 * zero, and a numeric token to use as an expansion. Create
4441 * and store an SMacro.
4442 */
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07004443 macro_start = make_tok_qstr_len(NULL, qbuf, len);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004444 nasm_free(qbuf);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004445 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004446 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004447 break;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004448
H. Peter Anvine2c80182005-01-15 22:15:51 +00004449 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004450 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004451 int64_t start, count;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004452 const char *txt;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004453 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07004454
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004455 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004456 goto done;
4457
H. Peter Anvine2c80182005-01-15 22:15:51 +00004458 last = tline;
4459 tline = expand_smacro(tline->next);
4460 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004461
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04004462 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004463 t = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004464
4465 t = skip_white(t);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004466
H. Peter Anvine2c80182005-01-15 22:15:51 +00004467 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004468 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004469 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004470 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004471 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004472 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004473
H. Peter Anvin8b262472019-02-26 14:00:54 -08004474 pps.tptr = t->next;
4475 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004476 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004477 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004478 if (!evalresult) {
4479 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004480 goto done;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004481 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004482 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004483 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004484 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004485 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004486 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004487
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004488 pps.tptr = skip_white(pps.tptr);
H. Peter Anvin8b262472019-02-26 14:00:54 -08004489 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004490 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004491 } else {
4492 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004493 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004494 if (!evalresult) {
4495 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004496 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004497 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004498 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004499 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004500 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004501 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004502 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004503 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004504
H. Peter Anvin8571f062019-09-23 16:40:03 -07004505 unquote_token(t);
4506 len = t->len;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004507
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04004508 /* make start and count being in range */
4509 if (start < 0)
4510 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004511 if (count < 0)
4512 count = len + count + 1 - start;
4513 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04004514 count = len - start;
4515 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004516 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004517
H. Peter Anvin8571f062019-09-23 16:40:03 -07004518 txt = (start < 0) ? "" : tok_text(t) + start;
4519 len = count;
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07004520 macro_start = make_tok_qstr_len(NULL, txt, len);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004521
H. Peter Anvine2c80182005-01-15 22:15:51 +00004522 /*
4523 * We now have a macro name, an implicit parameter count of
4524 * zero, and a numeric token to use as an expansion. Create
4525 * and store an SMacro.
4526 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004527 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004528 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004529 break;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004530 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004531
H. Peter Anvin8b262472019-02-26 14:00:54 -08004532 case PP_ASSIGN:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004533 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004534 goto done;
4535
H. Peter Anvine2c80182005-01-15 22:15:51 +00004536 last = tline;
4537 tline = expand_smacro(tline->next);
4538 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004539
H. Peter Anvin8b262472019-02-26 14:00:54 -08004540 pps.tptr = tline;
4541 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004542 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004543 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004544 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004545 if (!evalresult)
4546 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004547
H. Peter Anvine2c80182005-01-15 22:15:51 +00004548 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004549 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00004550
H. Peter Anvine2c80182005-01-15 22:15:51 +00004551 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004552 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004553 free_tlist(origline);
4554 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004555 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004556
H. Peter Anvin8571f062019-09-23 16:40:03 -07004557 macro_start = make_tok_num(NULL, reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00004558
H. Peter Anvine2c80182005-01-15 22:15:51 +00004559 /*
4560 * We now have a macro name, an implicit parameter count of
4561 * zero, and a numeric token to use as an expansion. Create
4562 * and store an SMacro.
4563 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004564 define_smacro(mname, casesense, macro_start, NULL);
4565 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004566
H. Peter Anvind2354082019-08-27 16:38:48 -07004567 case PP_ALIASES:
4568 tline = tline->next;
4569 tline = expand_smacro(tline);
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07004570 ppopt.noaliases = !pp_get_boolean_option(tline, !ppopt.noaliases);
H. Peter Anvind2354082019-08-27 16:38:48 -07004571 break;
4572
H. Peter Anvine2c80182005-01-15 22:15:51 +00004573 case PP_LINE:
4574 /*
4575 * Syntax is `%line nnn[+mmm] [filename]'
4576 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004577 if (unlikely(pp_noline))
4578 goto done;
4579
H. Peter Anvine2c80182005-01-15 22:15:51 +00004580 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004581 tline = skip_white(tline);
4582 if (!tok_type(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004583 nasm_nonfatal("`%s' expects line number", 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 k = readnum(tok_text(tline), &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004587 m = 1;
4588 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004589 if (tok_is(tline, '+')) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004590 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004591 if (!tok_type(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004592 nasm_nonfatal("`%s' expects line increment", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004593 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004594 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07004595 m = readnum(tok_text(tline), &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004596 tline = tline->next;
4597 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004598 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004599 src_set_linnum(k);
4600 istk->lineinc = m;
4601 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07004602 char *fname = detoken(tline, false);
4603 src_set_fname(fname);
4604 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004605 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004606 break;
4607 }
4608
4609done:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004610 free_tlist(origline);
4611 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004612}
4613
4614/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00004615 * Ensure that a macro parameter contains a condition code and
4616 * nothing else. Return the condition code index if so, or -1
4617 * otherwise.
4618 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004619static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004620{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004621 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004622
H. Peter Anvin25a99342007-09-22 17:45:45 -07004623 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004624 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07004625
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004626 t = skip_white(t);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004627 if (!tok_type(t, TOK_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004628 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004629 tt = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004630 tt = skip_white(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004631 if (tok_isnt(tt, ','))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004632 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004633
H. Peter Anvin8571f062019-09-23 16:40:03 -07004634 return bsii(tok_text(t), (const char **)conditions,
4635 ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00004636}
4637
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004638static inline bool pp_concat_match(const Token *t, unsigned int mask)
4639{
4640 return t && (PP_CONCAT_MASK(t->type) & mask);
4641}
4642
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004643/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004644 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004645 * pasting, if @handle_explicit passed then explicit pasting
4646 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004647 * The @m array can contain a series of token types which are
4648 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004649 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004650static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004651 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07004652{
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004653 Token *tok, *t, *next, **prev_next, **prev_nonspace;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004654 bool pasted = false;
4655 char *buf, *p;
4656 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07004657
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004658 /*
4659 * The last token before pasting. We need it
4660 * to be able to connect new handled tokens.
4661 * In other words if there were a tokens stream
4662 *
4663 * A -> B -> C -> D
4664 *
4665 * and we've joined tokens B and C, the resulting
4666 * stream should be
4667 *
4668 * A -> BC -> D
4669 */
4670 tok = *head;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004671 prev_next = prev_nonspace = head;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004672
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004673 if (tok_white(tok) || tok_type(tok, TOK_PASTE))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004674 prev_nonspace = NULL;
4675
4676 while (tok && (next = tok->next)) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004677 bool did_paste = false;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004678
4679 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07004680 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004681 /* Zap redundant whitespaces */
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004682 tok->next = next = zap_white(next);
H. Peter Anvind784a082009-04-20 14:01:18 -07004683 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004684
4685 case TOK_PASTE:
4686 /* Explicit pasting */
4687 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004688 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004689
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004690 /* Left pasting token is start of line */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004691 if (!prev_nonspace) {
4692 nasm_nonfatal("No lvalue found on pasting");
4693 tok = delete_Token(tok);
4694 break;
4695 }
4696
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004697 did_paste = true;
4698
4699 prev_next = prev_nonspace;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004700 t = *prev_nonspace;
4701
4702 /* Delete leading whitespace */
4703 next = zap_white(t->next);
4704
4705 /* Delete the %+ token itself */
4706 nasm_assert(next == tok);
4707 next = delete_Token(next);
4708
4709 /* Delete trailing whitespace */
4710 next = zap_white(next);
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004711
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004712 /*
4713 * No ending token, this might happen in two
4714 * cases
4715 *
4716 * 1) There indeed no right token at all
4717 * 2) There is a bare "%define ID" statement,
4718 * and @ID does expand to whitespace.
4719 *
4720 * So technically we need to do a grammar analysis
4721 * in another stage of parsing, but for now lets don't
4722 * change the behaviour people used to. Simply allow
4723 * whitespace after paste token.
4724 */
4725 if (!next) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004726 *prev_nonspace = tok = NULL; /* End of line */
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004727 break;
4728 }
4729
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004730 p = buf = nasm_malloc(t->len + next->len + 1);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004731 p = mempcpy(p, tok_text(t), t->len);
4732 p = mempcpy(p, tok_text(next), next->len);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004733 *p = '\0';
4734 delete_Token(t);
4735 t = tokenize(buf);
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004736 nasm_free(buf);
4737
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004738 if (unlikely(!t)) {
4739 /*
4740 * No output at all? Replace with a single whitespace.
4741 * This should never happen.
4742 */
4743 t = new_White(NULL);
4744 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004745
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004746 *prev_nonspace = tok = t;
4747 while (t->next)
4748 t = t->next; /* Find the last token produced */
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004749
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004750 /* Delete the second token and attach to the end of the list */
4751 t->next = delete_Token(next);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004752
4753 /* We want to restart from the head of the pasted token */
4754 next = tok;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004755 break;
4756
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004757 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004758 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004759 for (i = 0; i < mnum; i++) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004760 if (pp_concat_match(tok, m[i].mask_head))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004761 break;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004762 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004763
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004764 if (i >= mnum)
4765 break;
4766
4767 len = tok->len;
4768 while (pp_concat_match(next, m[i].mask_tail)) {
4769 len += next->len;
4770 next = next->next;
4771 }
4772
4773 /* No match or no text to process */
4774 if (len == tok->len)
4775 break;
4776
4777 p = buf = nasm_malloc(len + 1);
4778 while (tok != next) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004779 p = mempcpy(p, tok_text(tok), tok->len);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004780 tok = delete_Token(tok);
4781 }
4782 *p = '\0';
4783 *prev_next = tok = t = tokenize(buf);
4784 nasm_free(buf);
4785
4786 /*
4787 * Connect pasted into original stream,
4788 * ie A -> new-tokens -> B
4789 */
4790 while (t->next)
4791 t = t->next;
4792 t->next = next;
4793 prev_next = prev_nonspace = &t->next;
4794 did_paste = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004795 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004796 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004797
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004798 if (did_paste) {
4799 pasted = true;
4800 } else {
4801 prev_next = &tok->next;
4802 if (next && next->type != TOK_WHITESPACE && next->type != TOK_PASTE)
4803 prev_nonspace = prev_next;
4804 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004805
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004806 tok = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004807 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004808
4809 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07004810}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004811
4812/*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004813 * Computes the proper rotation of mmacro parameters
4814 */
4815static int mmac_rotate(const MMacro *mac, unsigned int n)
4816{
4817 if (--n < mac->nparam)
4818 n = (n + mac->rotate) % mac->nparam;
4819
4820 return n+1;
4821}
4822
4823/*
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004824 * expands to a list of tokens from %{x:y}
4825 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004826static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004827{
4828 Token *t = tline, **tt, *tm, *head;
4829 char *pos;
4830 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004831
H. Peter Anvin8571f062019-09-23 16:40:03 -07004832 pos = strchr(tok_text(tline), ':');
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004833 nasm_assert(pos);
4834
4835 lst = atoi(pos + 1);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004836 fst = atoi(tok_text(tline) + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004837
4838 /*
4839 * only macros params are accounted so
4840 * if someone passes %0 -- we reject such
4841 * value(s)
4842 */
4843 if (lst == 0 || fst == 0)
4844 goto err;
4845
4846 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004847 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
4848 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004849 goto err;
4850
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004851 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
4852 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004853
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004854 /* count from zero */
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004855 fst--, lst--;
4856
4857 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004858 * It will be at least one token. Note we
4859 * need to scan params until separator, otherwise
4860 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004861 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004862 j = (fst + mac->rotate) % mac->nparam;
4863 tm = mac->params[j+1];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03004864 if (!tm)
4865 goto err;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004866 head = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004867 tt = &head->next, tm = tm->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004868 while (tok_isnt(tm, ',')) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004869 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004870 *tt = t, tt = &t->next, tm = tm->next;
4871 }
4872
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004873 if (fst < lst) {
4874 for (i = fst + 1; i <= lst; i++) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004875 t = make_tok_char(NULL, ',');
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004876 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004877 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004878 tm = mac->params[j+1];
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004879 while (tok_isnt(tm, ',')) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004880 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004881 *tt = t, tt = &t->next, tm = tm->next;
4882 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004883 }
4884 } else {
4885 for (i = fst - 1; i >= lst; i--) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004886 t = make_tok_char(NULL, ',');
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004887 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004888 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004889 tm = mac->params[j+1];
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004890 while (!tok_isnt(tm, ',')) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004891 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004892 *tt = t, tt = &t->next, tm = tm->next;
4893 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004894 }
4895 }
4896
4897 *last = tt;
4898 return head;
4899
4900err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004901 nasm_nonfatal("`%%{%s}': macro parameters out of range",
H. Peter Anvin8571f062019-09-23 16:40:03 -07004902 tok_text(tline) + 1);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004903 return NULL;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004904}
4905
H. Peter Anvin76690a12002-04-30 20:52:49 +00004906/*
4907 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004908 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004909 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004910 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004911static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004912{
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004913 Token **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004914 bool changed = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004915 MMacro *mac = istk->mstk.mmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004916
4917 tail = &thead;
4918 thead = NULL;
4919
H. Peter Anvine2c80182005-01-15 22:15:51 +00004920 while (tline) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004921 bool change;
H. Peter Anvin (Intel)a762cd42020-06-01 11:49:08 -07004922 bool err_not_mac = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004923 Token *t = tline;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004924 const char *text = tok_text(t);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004925 int type = t->type;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004926
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004927 tline = tline->next;
4928 t->next = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004929
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004930 switch (type) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004931 case TOK_LOCAL_SYMBOL:
H. Peter Anvin (Intel)a762cd42020-06-01 11:49:08 -07004932 change = true;
4933
4934 if (!mac) {
4935 err_not_mac = true;
4936 break;
4937 }
4938
H. Peter Anvin8571f062019-09-23 16:40:03 -07004939 type = TOK_ID;
4940 text = nasm_asprintf("..@%"PRIu64".%s", mac->unique, text+2);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004941 break;
4942 case TOK_MMACRO_PARAM:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004943 {
4944 Token *tt = NULL;
4945
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004946 change = true;
4947
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004948 if (!mac) {
H. Peter Anvin (Intel)a762cd42020-06-01 11:49:08 -07004949 err_not_mac = true;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004950 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004951 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004952
4953 if (strchr(text, ':')) {
4954 /*
4955 * seems we have a parameters range here
4956 */
4957 Token *head, **last;
4958 head = expand_mmac_params_range(mac, t, &last);
4959 if (head) {
4960 *tail = head;
4961 *last = tline;
4962 text = NULL;
4963 }
4964 break;
4965 }
4966
4967 switch (text[1]) {
4968 /*
4969 * We have to make a substitution of one of the
4970 * forms %1, %-1, %+1, %%foo, %0, %00.
4971 */
4972 case '0':
4973 if (!text[2]) {
4974 type = TOK_NUMBER;
4975 text = nasm_asprintf("%d", mac->nparam);
4976 break;
4977 }
4978 if (text[2] != '0' || text[3])
4979 goto invalid;
4980 /* a possible captured label == mac->params[0] */
4981 /* fall through */
4982 default:
4983 {
4984 unsigned long n;
4985 char *ep;
4986
4987 n = strtoul(text + 1, &ep, 10);
4988 if (unlikely(*ep))
4989 goto invalid;
4990
4991 if (n <= mac->nparam) {
4992 n = mmac_rotate(mac, n);
4993 dup_tlistn(mac->params[n], mac->paramlen[n], &tail);
4994 }
4995 text = NULL;
4996 break;
4997 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004998 case '-':
4999 case '+':
5000 {
5001 int cc;
5002 unsigned long n;
5003 char *ep;
5004
H. Peter Anvin8571f062019-09-23 16:40:03 -07005005 n = strtoul(tok_text(t) + 2, &ep, 10);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005006 if (unlikely(*ep))
5007 goto invalid;
5008
Chang S. Bae057b8322020-04-18 23:11:21 +00005009 if (n && n <= mac->nparam) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005010 n = mmac_rotate(mac, n);
5011 tt = mac->params[n];
5012 }
5013 cc = find_cc(tt);
5014 if (cc == -1) {
5015 nasm_nonfatal("macro parameter `%s' is not a condition code",
H. Peter Anvin8571f062019-09-23 16:40:03 -07005016 tok_text(t));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005017 text = NULL;
5018 break;
5019 }
5020
5021 type = TOK_ID;
5022 if (text[1] == '-') {
5023 int ncc = inverse_ccs[cc];
5024 if (unlikely(ncc == -1)) {
5025 nasm_nonfatal("condition code `%s' is not invertible",
5026 conditions[cc]);
5027 break;
5028 }
5029 cc = ncc;
5030 }
5031 text = nasm_strdup(conditions[cc]);
5032 break;
5033 }
5034
5035 invalid:
5036 nasm_nonfatal("invalid macro parameter: `%s'", text);
5037 text = NULL;
5038 break;
5039 }
5040 break;
5041 }
5042
5043 case TOK_PREPROC_Q:
5044 if (mac) {
5045 type = TOK_ID;
5046 text = nasm_strdup(mac->iname);
5047 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07005048 } else {
5049 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005050 }
5051 break;
5052
5053 case TOK_PREPROC_QQ:
5054 if (mac) {
5055 type = TOK_ID;
5056 text = nasm_strdup(mac->name);
5057 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07005058 } else {
5059 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005060 }
5061 break;
5062
5063 case TOK_INDIRECT:
5064 {
5065 Token *tt;
5066
H. Peter Anvin8571f062019-09-23 16:40:03 -07005067 tt = tokenize(tok_text(t));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005068 tt = expand_mmac_params(tt);
5069 tt = expand_smacro(tt);
5070 /* Why dup_tlist() here? We should own tt... */
5071 dup_tlist(tt, &tail);
5072 text = NULL;
5073 change = true;
5074 break;
5075 }
5076
5077 default:
5078 change = false;
5079 break;
5080 }
5081
H. Peter Anvin (Intel)a762cd42020-06-01 11:49:08 -07005082 if (err_not_mac) {
5083 nasm_nonfatal("`%s': not in a macro call", text);
5084 text = NULL;
5085 change = true;
5086 }
5087
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005088 if (change) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005089 if (!text) {
5090 delete_Token(t);
5091 } else {
5092 *tail = t;
5093 tail = &t->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005094 set_text(t, text, tok_strlen(text));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005095 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005096 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005097 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005098 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005099 *tail = t;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005100 tail = &t->next;
5101 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00005102 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005103
H. Peter Anvineba20a72002-04-30 20:53:55 +00005104 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07005105
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005106 if (changed) {
5107 const struct tokseq_match t[] = {
5108 {
5109 PP_CONCAT_MASK(TOK_ID) |
5110 PP_CONCAT_MASK(TOK_FLOAT), /* head */
5111 PP_CONCAT_MASK(TOK_ID) |
5112 PP_CONCAT_MASK(TOK_NUMBER) |
5113 PP_CONCAT_MASK(TOK_FLOAT) |
5114 PP_CONCAT_MASK(TOK_OTHER) /* tail */
5115 },
5116 {
5117 PP_CONCAT_MASK(TOK_NUMBER), /* head */
5118 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
5119 }
5120 };
5121 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
5122 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07005123
H. Peter Anvin76690a12002-04-30 20:52:49 +00005124 return thead;
5125}
5126
H. Peter Anvin322bee02019-08-10 01:38:06 -07005127static Token *expand_smacro_noreset(Token * tline);
H. Peter Anvin322bee02019-08-10 01:38:06 -07005128
H. Peter Anvin76690a12002-04-30 20:52:49 +00005129/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005130 * Expand *one* single-line macro instance. If the first token is not
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005131 * a macro at all, it is simply copied to the output and the pointer
5132 * advanced. tpp should be a pointer to a pointer (usually the next
5133 * pointer of the previous token) to the first token. **tpp is updated
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005134 * to point to the first token of the expansion, and *tpp updated to
5135 * point to the next pointer of the last token of the expansion.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005136 *
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005137 * If the expansion is empty, *tpp will be unchanged but **tpp will
5138 * be advanced past the macro call.
5139 *
H. Peter Anvin322bee02019-08-10 01:38:06 -07005140 * Return the macro expanded, or NULL if no expansion took place.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005141 */
H. Peter Anvin322bee02019-08-10 01:38:06 -07005142static SMacro *expand_one_smacro(Token ***tpp)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005143{
5144 Token **params = NULL;
5145 const char *mname;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005146 Token *mstart = **tpp;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005147 Token *tline = mstart;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005148 SMacro *head, *m;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005149 int i;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005150 Token *t, *tup, *tafter;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005151 int nparam = 0;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005152 bool cond_comma;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005153
5154 if (!tline)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005155 return false; /* Empty line, nothing to do */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005156
H. Peter Anvin8571f062019-09-23 16:40:03 -07005157 mname = tok_text(mstart);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005158
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005159 smacro_deadman.total--;
H. Peter Anvin322bee02019-08-10 01:38:06 -07005160 smacro_deadman.levels--;
5161
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005162 if (unlikely(smacro_deadman.total < 0 || smacro_deadman.levels < 0)) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07005163 if (unlikely(!smacro_deadman.triggered)) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005164 nasm_nonfatal("interminable macro recursion");
H. Peter Anvin322bee02019-08-10 01:38:06 -07005165 smacro_deadman.triggered = true;
5166 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005167 goto not_a_macro;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005168 } else if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005169 head = (SMacro *)hash_findix(&smacros, mname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005170 } else if (tline->type == TOK_LOCAL_MACRO) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005171 Context *ctx = get_ctx(mname, &mname);
5172 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
5173 } else {
5174 goto not_a_macro;
5175 }
5176
5177 /*
5178 * We've hit an identifier of some sort. First check whether the
5179 * identifier is a single-line macro at all, then think about
5180 * checking for parameters if necessary.
5181 */
5182 list_for_each(m, head) {
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005183 if (unlikely(m->alias && ppopt.noaliases))
H. Peter Anvind2354082019-08-27 16:38:48 -07005184 continue;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005185 if (!mstrcmp(m->name, mname, m->casesense))
5186 break;
5187 }
5188
5189 if (!m) {
5190 goto not_a_macro;
5191 }
5192
5193 /* Parse parameters, if applicable */
5194
5195 params = NULL;
5196 nparam = 0;
5197
5198 if (m->nparam == 0) {
5199 /*
5200 * Simple case: the macro is parameterless.
5201 * Nothing to parse; the expansion code will
5202 * drop the macro name token.
5203 */
5204 } else {
5205 /*
5206 * Complicated case: at least one macro with this name
5207 * exists and takes parameters. We must find the
5208 * parameters in the call, count them, find the SMacro
5209 * that corresponds to that form of the macro call, and
5210 * substitute for the parameters when we expand. What a
5211 * pain.
5212 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005213 Token *t;
5214 int paren, brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005215
5216 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005217 tline = skip_white(tline);
5218 if (!tok_is(tline, '(')) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005219 /*
5220 * This macro wasn't called with parameters: ignore
5221 * the call. (Behaviour borrowed from gnu cpp.)
5222 */
5223 goto not_a_macro;
5224 }
5225
5226 paren = 1;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005227 nparam = 1;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005228 brackets = 0;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005229 t = tline; /* tline points to leading ( */
5230
5231 while (paren) {
5232 t = t->next;
5233
5234 if (!t) {
5235 nasm_nonfatal("macro call expects terminating `)'");
5236 goto not_a_macro;
5237 }
5238
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005239 if (t->type != TOK_OTHER || t->len != 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005240 continue;
5241
H. Peter Anvin8571f062019-09-23 16:40:03 -07005242 switch (t->text.a[0]) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005243 case ',':
H. Peter Anvinbd00f252020-06-04 21:05:01 -07005244 if (!brackets && paren == 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005245 nparam++;
5246 break;
5247
5248 case '{':
5249 brackets++;
5250 break;
5251
5252 case '}':
5253 if (brackets > 0)
5254 brackets--;
5255 break;
5256
5257 case '(':
5258 if (!brackets)
5259 paren++;
5260 break;
5261
5262 case ')':
5263 if (!brackets)
5264 paren--;
5265 break;
5266
5267 default:
5268 break; /* Normal token */
5269 }
5270 }
5271
5272 /*
5273 * Look for a macro matching in both name and parameter count.
5274 * We already know any matches cannot be anywhere before the
5275 * current position of "m", so there is no reason to
5276 * backtrack.
5277 */
5278 while (1) {
5279 if (!m) {
5280 /*!
5281 *!macro-params-single [on] single-line macro calls with wrong parameter count
5282 *! warns about \i{single-line macros} being invoked
5283 *! with the wrong number of parameters.
5284 */
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005285 nasm_warn(WARN_MACRO_PARAMS_SINGLE|ERR_HOLD,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005286 "single-line macro `%s' exists, "
5287 "but not taking %d parameter%s",
5288 mname, nparam, (nparam == 1) ? "" : "s");
5289 goto not_a_macro;
5290 }
5291
5292 if (!mstrcmp(m->name, mname, m->casesense)) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005293 if (nparam == m->nparam)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005294 break; /* It's good */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005295 if (m->greedy && nparam >= m->nparam-1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005296 break; /* Also good */
5297 }
5298 m = m->next;
5299 }
5300 }
5301
5302 if (m->in_progress)
5303 goto not_a_macro;
5304
5305 /* Expand the macro */
5306 m->in_progress = true;
5307
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005308 if (nparam) {
5309 /* Extract parameters */
5310 Token **phead, **pep;
5311 int white = 0;
5312 int brackets = 0;
5313 int paren;
5314 bool bracketed = false;
5315 bool bad_bracket = false;
5316 enum sparmflags flags;
5317
5318 nparam = m->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005319 paren = 1;
5320 nasm_newn(params, nparam);
5321 i = 0;
5322 flags = m->params[i].flags;
5323 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005324 *pep = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005325
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005326 while (paren) {
5327 bool skip;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005328 char ch;
5329
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005330 tline = tline->next;
5331
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005332 if (!tline)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005333 nasm_nonfatal("macro call expects terminating `)'");
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005334
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005335 ch = 0;
5336 skip = false;
5337
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005338
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005339 switch (tline->type) {
5340 case TOK_OTHER:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005341 if (tline->len == 1)
H. Peter Anvin8571f062019-09-23 16:40:03 -07005342 ch = tline->text.a[0];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005343 break;
5344
5345 case TOK_WHITESPACE:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005346 if (!(flags & SPARM_NOSTRIP)) {
5347 if (brackets || *phead)
5348 white++; /* Keep interior whitespace */
5349 skip = true;
5350 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005351 break;
5352
5353 default:
5354 break;
5355 }
5356
5357 switch (ch) {
5358 case ',':
H. Peter Anvinbd00f252020-06-04 21:05:01 -07005359 if (!brackets && paren == 1 && !(flags & SPARM_GREEDY)) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005360 i++;
5361 nasm_assert(i < nparam);
5362 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005363 *pep = NULL;
5364 bracketed = false;
5365 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005366 flags = m->params[i].flags;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005367 }
5368 break;
5369
5370 case '{':
5371 if (!bracketed) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005372 bracketed = !*phead && !(flags & SPARM_NOSTRIP);
5373 skip = bracketed;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005374 }
5375 brackets++;
5376 break;
5377
5378 case '}':
5379 if (brackets > 0) {
5380 if (!--brackets)
5381 skip = bracketed;
5382 }
5383 break;
5384
5385 case '(':
5386 if (!brackets)
5387 paren++;
5388 break;
5389
5390 case ')':
5391 if (!brackets) {
5392 paren--;
5393 if (!paren) {
5394 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005395 i++; /* Found last argument */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005396 }
5397 }
5398 break;
5399
5400 default:
5401 break; /* Normal token */
5402 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005403
5404 if (!skip) {
5405 Token *t;
5406
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005407 bad_bracket |= bracketed && !brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005408
5409 if (white) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005410 *pep = t = new_White(NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005411 pep = &t->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005412 white = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005413 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005414 *pep = t = dup_Token(NULL, tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005415 pep = &t->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005416 }
5417 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005418
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005419 /*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005420 * Possible further processing of parameters. Note that the
5421 * ordering matters here.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005422 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005423 for (i = 0; i < nparam; i++) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005424 enum sparmflags flags = m->params[i].flags;
5425
5426 if (flags & SPARM_EVAL) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005427 /* Evaluate this parameter as a number */
5428 struct ppscan pps;
5429 struct tokenval tokval;
5430 expr *evalresult;
5431 Token *eval_param;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005432
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005433 pps.tptr = eval_param = expand_smacro_noreset(params[i]);
5434 pps.ntokens = -1;
5435 tokval.t_type = TOKEN_INVALID;
5436 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005437
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005438 free_tlist(eval_param);
5439 params[i] = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005440
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005441 if (!evalresult) {
5442 /* Nothing meaningful to do */
5443 } else if (tokval.t_type) {
5444 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
5445 } else if (!is_simple(evalresult)) {
5446 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
5447 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005448 params[i] = make_tok_num(NULL, reloc_value(evalresult));
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005449 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005450 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005451
5452 if (flags & SPARM_STR) {
5453 /* Convert expansion to a quoted string */
5454 char *arg;
5455 Token *qs;
5456
5457 qs = expand_smacro_noreset(params[i]);
5458 arg = detoken(qs, false);
5459 free_tlist(qs);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005460 params[i] = make_tok_qstr(NULL, arg);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005461 nasm_free(arg);
5462 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005463 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005464 }
5465
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005466 /* Note: we own the expansion this returns. */
5467 t = m->expand(m, params, nparam);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005468
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005469 tafter = tline->next; /* Skip past the macro call */
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005470 tline->next = NULL; /* Truncate list at the macro call end */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005471 tline = tafter;
5472
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005473 tup = NULL;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005474 cond_comma = false;
5475
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005476 while (t) {
5477 enum pp_token_type type = t->type;
5478 Token *tnext = t->next;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005479
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005480 switch (type) {
5481 case TOK_PREPROC_Q:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005482 delete_Token(t);
5483 t = dup_Token(tline, mstart);
5484 break;
5485
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005486 case TOK_PREPROC_QQ:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005487 {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005488 size_t mlen = strlen(m->name);
5489 size_t len;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005490 char *p;
5491
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005492 t->type = mstart->type;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005493 if (t->type == TOK_LOCAL_MACRO) {
5494 const char *psp; /* prefix start pointer */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005495 const char *pep; /* prefix end pointer */
H. Peter Anvin8571f062019-09-23 16:40:03 -07005496 size_t plen;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005497
H. Peter Anvin8571f062019-09-23 16:40:03 -07005498 psp = tok_text(mstart);
5499 get_ctx(psp, &pep);
5500 plen = pep - psp;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005501
H. Peter Anvin8571f062019-09-23 16:40:03 -07005502 len = mlen + plen;
5503 p = nasm_malloc(len + 1);
5504 p = mempcpy(p, psp, plen);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005505 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005506 len = mlen;
5507 p = nasm_malloc(len + 1);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005508 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07005509 p = mempcpy(p, m->name, mlen);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005510 *p = '\0';
H. Peter Anvin8571f062019-09-23 16:40:03 -07005511 set_text_free(t, p, len);
5512
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005513 t->next = tline;
5514 break;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005515 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005516
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005517 case TOK_COND_COMMA:
5518 delete_Token(t);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005519 t = cond_comma ? make_tok_char(tline, ',') : NULL;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005520 break;
5521
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005522 case TOK_ID:
5523 case TOK_PREPROC_ID:
H. Peter Anvin8571f062019-09-23 16:40:03 -07005524 case TOK_LOCAL_MACRO:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005525 {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005526 /*
5527 * Chain this into the target line *before* expanding,
5528 * that way we pick up any arguments to the new macro call,
5529 * if applicable.
5530 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005531 Token **tp = &t;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005532 t->next = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005533 expand_one_smacro(&tp);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005534 tline = *tp; /* First token left after any macro call */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005535 break;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005536 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005537 default:
5538 if (is_smac_param(t->type)) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005539 int param = smac_nparam(t->type);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005540 nasm_assert(!tup && param < nparam);
5541 delete_Token(t);
5542 t = NULL;
5543 tup = tnext;
5544 tnext = dup_tlist_reverse(params[param], NULL);
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005545 cond_comma = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005546 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005547 t->next = tline;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005548 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005549 }
5550
5551 if (t) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005552 Token *endt = tline;
5553
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005554 tline = t;
Chang S. Bae95e54a92020-02-06 14:39:22 -08005555 while (!cond_comma && t && t != endt) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005556 cond_comma = t->type != TOK_WHITESPACE;
Chang S. Bae95e54a92020-02-06 14:39:22 -08005557 t = t->next;
5558 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005559 }
5560
5561 if (tnext) {
5562 t = tnext;
5563 } else {
5564 t = tup;
5565 tup = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005566 }
5567 }
5568
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005569 **tpp = tline;
H. Peter Anvin (Intel)6e714962020-06-01 12:21:10 -07005570 for (t = tline; t && t != tafter; t = t->next)
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005571 *tpp = &t->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005572
5573 m->in_progress = false;
5574
5575 /* Don't do this until after expansion or we will clobber mname */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005576 free_tlist(mstart);
H. Peter Anvin322bee02019-08-10 01:38:06 -07005577 goto done;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005578
5579 /*
5580 * No macro expansion needed; roll back to mstart (if necessary)
H. Peter Anvin322bee02019-08-10 01:38:06 -07005581 * and then advance to the next input token. Note that this is
5582 * by far the common case!
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005583 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005584not_a_macro:
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005585 *tpp = &mstart->next;
H. Peter Anvin322bee02019-08-10 01:38:06 -07005586 m = NULL;
5587done:
5588 smacro_deadman.levels++;
5589 if (unlikely(params))
5590 free_tlist_array(params, nparam);
5591 return m;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005592}
5593
5594/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005595 * Expand all single-line macro calls made in the given line.
5596 * Return the expanded version of the line. The original is deemed
5597 * to be destroyed in the process. (In reality we'll just move
5598 * Tokens from input to output a lot of the time, rather than
5599 * actually bothering to destroy and replicate.)
5600 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005601static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005602{
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005603 smacro_deadman.total = nasm_limit[LIMIT_MACRO_TOKENS];
H. Peter Anvin322bee02019-08-10 01:38:06 -07005604 smacro_deadman.levels = nasm_limit[LIMIT_MACRO_LEVELS];
5605 smacro_deadman.triggered = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005606 return expand_smacro_noreset(tline);
5607}
5608
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005609static Token *expand_smacro_noreset(Token *org_tline)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005610{
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005611 Token *tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005612 bool expanded;
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005613 errhold errhold; /* Hold warning/errors during expansion */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005614
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005615 if (!org_tline)
5616 return NULL; /* Empty input */
5617
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005618 /*
5619 * Trick: we should avoid changing the start token pointer since it can
5620 * be contained in "next" field of other token. Because of this
5621 * we allocate a copy of first token and work with it; at the end of
5622 * routine we copy it back
5623 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005624 tline = dup_Token(org_tline->next, org_tline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005625
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005626 /*
5627 * Pretend that we always end up doing expansion on the first pass;
5628 * that way %+ get processed. However, if we process %+ before the
5629 * first pass, we end up with things like MACRO %+ TAIL trying to
5630 * look up the macro "MACROTAIL", which we don't want.
5631 */
5632 expanded = true;
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005633
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005634 while (true) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005635 static const struct tokseq_match tmatch[] = {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005636 {
5637 PP_CONCAT_MASK(TOK_ID) |
H. Peter Anvin8571f062019-09-23 16:40:03 -07005638 PP_CONCAT_MASK(TOK_LOCAL_MACRO) |
5639 PP_CONCAT_MASK(TOK_ENVIRON) |
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005640 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
5641 PP_CONCAT_MASK(TOK_ID) |
H. Peter Anvin8571f062019-09-23 16:40:03 -07005642 PP_CONCAT_MASK(TOK_LOCAL_MACRO) |
5643 PP_CONCAT_MASK(TOK_ENVIRON) |
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005644 PP_CONCAT_MASK(TOK_PREPROC_ID) |
5645 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
5646 }
5647 };
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005648 Token **tail = &tline;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005649
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005650 /*
5651 * We hold warnings/errors until we are done this this loop. It is
5652 * possible for nuisance warnings to appear that disappear on later
5653 * passes.
5654 */
5655 errhold = nasm_error_hold_push();
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005656
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005657 while (*tail) /* main token loop */
H. Peter Anvin322bee02019-08-10 01:38:06 -07005658 expanded |= !!expand_one_smacro(&tail);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005659
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005660 if (!expanded)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005661 break; /* Done! */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005662
5663 /*
5664 * Now scan the entire line and look for successive TOK_IDs
5665 * that resulted after expansion (they can't be produced by
5666 * tokenize()). The successive TOK_IDs should be concatenated.
5667 * Also we look for %+ tokens and concatenate the tokens
5668 * before and after them (without white spaces in between).
5669 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005670 if (!paste_tokens(&tline, tmatch, ARRAY_SIZE(tmatch), true))
5671 break; /* Done again! */
5672
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005673 nasm_error_hold_pop(errhold, false);
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005674 expanded = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005675 }
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005676 nasm_error_hold_pop(errhold, true);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005677
H. Peter Anvin8571f062019-09-23 16:40:03 -07005678 if (!tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005679 /*
5680 * The expression expanded to empty line;
5681 * we can't return NULL because of the "trick" above.
5682 * Just set the line to a single WHITESPACE token.
H. Peter Anvin8571f062019-09-23 16:40:03 -07005683 */
5684
5685 tline = new_White(NULL);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005686 }
5687
H. Peter Anvin8571f062019-09-23 16:40:03 -07005688 steal_Token(org_tline, tline);
5689 org_tline->next = tline->next;
5690 delete_Token(tline);
5691
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005692 return org_tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005693}
5694
5695/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005696 * Similar to expand_smacro but used exclusively with macro identifiers
5697 * right before they are fetched in. The reason is that there can be
5698 * identifiers consisting of several subparts. We consider that if there
5699 * are more than one element forming the name, user wants a expansion,
5700 * otherwise it will be left as-is. Example:
5701 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005702 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005703 *
5704 * the identifier %$abc will be left as-is so that the handler for %define
5705 * will suck it and define the corresponding value. Other case:
5706 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005707 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005708 *
5709 * In this case user wants name to be expanded *before* %define starts
5710 * working, so we'll expand %$abc into something (if it has a value;
5711 * otherwise it will be left as-is) then concatenate all successive
5712 * PP_IDs into one.
5713 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005714static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005715{
5716 Token *cur, *oldnext = NULL;
5717
H. Peter Anvin734b1882002-04-30 21:01:08 +00005718 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005719 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005720
5721 cur = tline;
5722 while (cur->next &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07005723 (cur->next->type == TOK_ID || cur->next->type == TOK_PREPROC_ID ||
5724 cur->next->type == TOK_LOCAL_MACRO || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005725 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005726
5727 /* If identifier consists of just one token, don't expand */
5728 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005729 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005730
H. Peter Anvine2c80182005-01-15 22:15:51 +00005731 if (cur) {
5732 oldnext = cur->next; /* Detach the tail past identifier */
5733 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005734 }
5735
H. Peter Anvin734b1882002-04-30 21:01:08 +00005736 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005737
H. Peter Anvine2c80182005-01-15 22:15:51 +00005738 if (cur) {
5739 /* expand_smacro possibly changhed tline; re-scan for EOL */
5740 cur = tline;
5741 while (cur && cur->next)
5742 cur = cur->next;
5743 if (cur)
5744 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005745 }
5746
5747 return tline;
5748}
5749
5750/*
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005751 * This is called from find_mmacro_in_list() after finding a suitable macro.
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005752 */
5753static MMacro *use_mmacro(MMacro *m, int *nparamp, Token ***paramsp)
5754{
5755 int nparam = *nparamp;
5756 Token **params = *paramsp;
5757
5758 /*
5759 * This one is right. Just check if cycle removal
5760 * prohibits us using it before we actually celebrate...
5761 */
5762 if (m->in_progress > m->max_depth) {
5763 if (m->max_depth > 0) {
5764 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
5765 m->max_depth);
5766 }
5767 nasm_free(params);
5768 *nparamp = 0;
5769 *paramsp = NULL;
5770 return NULL;
5771 }
5772
5773 /*
5774 * It's right, and we can use it. Add its default
5775 * parameters to the end of our list if necessary.
5776 */
5777 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
5778 int newnparam = m->nparam_min + m->ndefs;
5779 params = nasm_realloc(params, sizeof(*params) * (newnparam+2));
5780 memcpy(&params[nparam+1], &m->defaults[nparam+1-m->nparam_min],
5781 (newnparam - nparam) * sizeof(*params));
5782 nparam = newnparam;
5783 }
5784 /*
5785 * If we've gone over the maximum parameter count (and
5786 * we're in Plus mode), ignore parameters beyond
5787 * nparam_max.
5788 */
5789 if (m->plus && nparam > m->nparam_max)
5790 nparam = m->nparam_max;
5791
5792 /*
5793 * If nparam was adjusted above, make sure the list is still
5794 * NULL-terminated.
5795 */
5796 params[nparam+1] = NULL;
5797
5798 /* Done! */
5799 *paramsp = params;
5800 *nparamp = nparam;
5801 return m;
5802}
5803
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005804/*
5805 * Search a macro list and try to find a match. If matching, call
5806 * use_mmacro() to set up the macro call. m points to the list of
5807 * search, which is_mmacro() sets to the first *possible* match.
5808 */
5809static MMacro *
5810find_mmacro_in_list(MMacro *m, const char *finding,
5811 int *nparamp, Token ***paramsp)
5812{
5813 int nparam = *nparamp;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005814
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005815 while (m) {
5816 if (m->nparam_min <= nparam
5817 && (m->plus || nparam <= m->nparam_max)) {
5818 /*
5819 * This one matches, use it.
5820 */
5821 return use_mmacro(m, nparamp, paramsp);
5822 }
5823
5824 /*
5825 * Otherwise search for the next one with a name match.
5826 */
5827 list_for_each(m, m->next) {
5828 if (!mstrcmp(m->name, finding, m->casesense))
5829 break;
5830 }
5831 }
5832
5833 return NULL;
5834}
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005835
5836/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005837 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005838 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005839 * to check for an initial label - that's taken care of in
5840 * expand_mmacro - but must check numbers of parameters. Guaranteed
5841 * to be called with tline->type == TOK_ID, so the putative macro
5842 * name is easy to find.
5843 */
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005844static MMacro *is_mmacro(Token * tline, int *nparamp, Token ***paramsp)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005845{
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005846 MMacro *head, *m, *found;
5847 Token **params, **comma;
5848 int raw_nparam, nparam;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005849 const char *finding = tok_text(tline);
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005850 bool empty_args = !tline->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005851
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005852 *nparamp = 0;
5853 *paramsp = NULL;
5854
H. Peter Anvin8571f062019-09-23 16:40:03 -07005855 head = (MMacro *) hash_findix(&mmacros, finding);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005856
5857 /*
5858 * Efficiency: first we see if any macro exists with the given
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005859 * name which isn't already excluded by macro cycle removal.
5860 * (The cycle removal test here helps optimize the case of wrapping
5861 * instructions, and is cheap to do here.)
5862 *
5863 * If not, we can return NULL immediately. _Then_ we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005864 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005865 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005866 */
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005867 list_for_each(m, head) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005868 if (!mstrcmp(m->name, finding, m->casesense) &&
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005869 (m->in_progress != 1 || m->max_depth > 0))
5870 break; /* Found something that needs consideration */
5871 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005872 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005873 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005874
5875 /*
5876 * OK, we have a potential macro. Count and demarcate the
5877 * parameters.
5878 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005879 comma = count_mmac_params(tline->next, nparamp, paramsp);
5880 raw_nparam = *nparamp;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005881
5882 /*
5883 * Search for an exact match. This cannot come *before* the m
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005884 * found in the list search before, so we can start there.
5885 *
5886 * If found is NULL and *paramsp has been cleared, then we
5887 * encountered an error for which we have already issued a
5888 * diagnostic, so we should not proceed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005889 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005890 found = find_mmacro_in_list(m, finding, nparamp, paramsp);
5891 if (!*paramsp)
5892 return NULL;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005893
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005894 nparam = *nparamp;
5895 params = *paramsp;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005896
5897 /*
5898 * Special weirdness: in NASM < 2.15, an expansion of
5899 * *only* whitespace, as can happen during macro expansion under
5900 * certain circumstances, is counted as zero arguments for the
5901 * purpose of %0, but one argument for the purpose of macro
5902 * matching! In particular, this affects:
5903 *
5904 * foobar %1
5905 *
5906 * ... with %1 being empty; this would call the one-argument
5907 * version of "foobar" with an empty argument, equivalent to
5908 *
5909 * foobar {%1}
5910 *
5911 * ... except that %0 would be set to 0 inside foobar, even if
5912 * foobar is declared with "%macro foobar 1" or equivalent!
5913 *
5914 * The proper way to do that is to define "%macro foobar 0-1".
5915 *
5916 * To be compatible without doing something too stupid, try to
5917 * match a zero-argument macro first, but if that fails, try
5918 * for a one-argument macro with the above behavior.
5919 *
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005920 * Furthermore, NASM < 2.15 will match stripping a tailing empty
5921 * argument, but in that case %0 *does* reflect that this argument
5922 * have been stripped; this is handled in count_mmac_params().
5923 *
5924 * To disable these insane legacy behaviors, use:
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005925 *
5926 * %pragma preproc sane_empty_expansion yes
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005927 *
5928 *!macro-params-legacy [on] improperly calling multi-line macro for legacy support
5929 *! warns about \i{multi-line macros} being invoked
5930 *! with the wrong number of parameters, but for bug-compatibility
5931 *! with NASM versions older than 2.15, NASM tried to fix up the
5932 *! parameters to match the legacy behavior and call the macro anyway.
5933 *! This can happen in certain cases where there are empty arguments
5934 *! without braces, sometimes as a result of macro expansion.
5935 *!-
5936 *! The legacy behavior is quite strange and highly context-dependent,
5937 *! and can be disabled with:
5938 *!-
5939 *! \c %pragma preproc sane_empty_expansion true
5940 *!-
5941 *! It is highly recommended to use this option in new code.
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005942 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005943 if (!ppopt.sane_empty_expansion) {
5944 if (!found) {
5945 if (raw_nparam == 0 && !empty_args) {
5946 /*
5947 * A single all-whitespace parameter as the only thing?
5948 * Look for a one-argument macro, but don't adjust
5949 * *nparamp.
5950 */
5951 int bogus_nparam = 1;
5952 params[2] = NULL;
5953 found = find_mmacro_in_list(m, finding, &bogus_nparam, paramsp);
5954 } else if (raw_nparam > 1 && comma) {
5955 Token *comma_tail = *comma;
5956
5957 /*
5958 * Drop the terminal argument and try again.
5959 * If we fail, we need to restore the comma to
5960 * preserve tlist.
5961 */
5962 *comma = NULL;
5963 *nparamp = raw_nparam - 1;
5964 found = find_mmacro_in_list(m, finding, nparamp, paramsp);
5965 if (found)
5966 free_tlist(comma_tail);
5967 else
5968 *comma = comma_tail;
5969 }
5970
5971 if (!*paramsp)
5972 return NULL;
5973 } else if (comma) {
5974 free_tlist(*comma);
5975 *comma = NULL;
5976 if (raw_nparam > found->nparam_min &&
5977 raw_nparam <= found->nparam_min + found->ndefs) {
5978 /* Replace empty argument with default parameter */
5979 params[raw_nparam] =
5980 found->defaults[raw_nparam - found->nparam_min];
5981 } else if (raw_nparam > found->nparam_max && found->plus) {
5982 /* Just drop the comma, don't adjust argument count */
5983 } else {
5984 /* Drop argument. This may cause nparam < nparam_min. */
5985 params[raw_nparam] = NULL;
5986 *nparamp = nparam = raw_nparam - 1;
5987 }
5988 }
5989
5990 if (found) {
5991 if (raw_nparam < found->nparam_min ||
5992 (raw_nparam > found->nparam_max && !found->plus)) {
5993 nasm_warn(WARN_MACRO_PARAMS_LEGACY,
5994 "improperly calling multi-line macro `%s' with %d parameters",
5995 found->name, raw_nparam);
5996 } else if (comma) {
5997 nasm_warn(WARN_MACRO_PARAMS_LEGACY,
5998 "dropping trailing empty parameter in call to multi-line macro `%s'", found->name);
5999 }
6000 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006001 }
6002
6003 /*
6004 * After all that, we didn't find one with the right number of
6005 * parameters. Issue a warning, and fail to expand the macro.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006006 *!
6007 *!macro-params-multi [on] multi-line macro calls with wrong parameter count
6008 *! warns about \i{multi-line macros} being invoked
6009 *! with the wrong number of parameters. See \k{mlmacover} for an
6010 *! example of why you might want to disable this warning.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006011 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07006012 if (found)
6013 return found;
6014
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006015 nasm_warn(WARN_MACRO_PARAMS_MULTI,
6016 "multi-line macro `%s' exists, but not taking %d parameter%s",
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07006017 finding, nparam, (nparam == 1) ? "" : "s");
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07006018 nasm_free(*paramsp);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006019 return NULL;
6020}
6021
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006022
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006023#if 0
6024
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006025/*
6026 * Save MMacro invocation specific fields in
6027 * preparation for a recursive macro expansion
6028 */
6029static void push_mmacro(MMacro *m)
6030{
6031 MMacroInvocation *i;
6032
6033 i = nasm_malloc(sizeof(MMacroInvocation));
6034 i->prev = m->prev;
6035 i->params = m->params;
6036 i->iline = m->iline;
6037 i->nparam = m->nparam;
6038 i->rotate = m->rotate;
6039 i->paramlen = m->paramlen;
6040 i->unique = m->unique;
6041 i->condcnt = m->condcnt;
6042 m->prev = i;
6043}
6044
6045
6046/*
6047 * Restore MMacro invocation specific fields that were
6048 * saved during a previous recursive macro expansion
6049 */
6050static void pop_mmacro(MMacro *m)
6051{
6052 MMacroInvocation *i;
6053
6054 if (m->prev) {
6055 i = m->prev;
6056 m->prev = i->prev;
6057 m->params = i->params;
6058 m->iline = i->iline;
6059 m->nparam = i->nparam;
6060 m->rotate = i->rotate;
6061 m->paramlen = i->paramlen;
6062 m->unique = i->unique;
6063 m->condcnt = i->condcnt;
6064 nasm_free(i);
6065 }
6066}
6067
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006068#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006069
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006070/*
H. Peter Anvin (Intel)ffe89dd2019-08-20 16:06:36 -07006071 * List an mmacro call with arguments (-Lm option)
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07006072 */
6073static void list_mmacro_call(const MMacro *m)
6074{
6075 const char prefix[] = " ;;; [macro] ";
6076 size_t namelen, size;
6077 char *buf, *p;
6078 unsigned int i;
6079 const Token *t;
6080
6081 namelen = strlen(m->iname);
6082 size = namelen + sizeof(prefix); /* Includes final null (from prefix) */
6083
6084 for (i = 1; i <= m->nparam; i++) {
6085 int j = 0;
6086 size += 3; /* Braces and space/comma */
6087 list_for_each(t, m->params[i]) {
6088 if (j++ >= m->paramlen[i])
6089 break;
6090 size += (t->type == TOK_WHITESPACE) ? 1 : t->len;
6091 }
6092 }
6093
6094 buf = p = nasm_malloc(size);
6095 p = mempcpy(p, prefix, sizeof(prefix) - 1);
6096 p = mempcpy(p, m->iname, namelen);
6097 *p++ = ' ';
6098
6099 for (i = 1; i <= m->nparam; i++) {
6100 int j = 0;
6101 *p++ = '{';
6102 list_for_each(t, m->params[i]) {
6103 if (j++ >= m->paramlen[i])
6104 break;
H. Peter Anvin8571f062019-09-23 16:40:03 -07006105 p = mempcpy(p, tok_text(t), t->len);
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07006106 }
6107 *p++ = '}';
6108 *p++ = ',';
6109 }
6110
6111 *--p = '\0'; /* Replace last delimeter with null */
6112 lfmt->line(LIST_MACRO, -1, buf);
6113 nasm_free(buf);
6114}
6115
6116/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006117 * Expand the multi-line macro call made by the given line, if
6118 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006119 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006120 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006121static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006122{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006123 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006124 Token *label = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006125 bool dont_prepend = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006126 Token **params, *t, *tt;
6127 MMacro *m;
6128 Line *l, *ll;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07006129 int i, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07006130 const char *mname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006131 int nparam = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006132
6133 t = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006134 t = skip_white(t);
6135 /* if (!tok_type(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07006136 if (!tok_type(t, TOK_ID) && !tok_type(t, TOK_LOCAL_MACRO))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006137 return 0;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006138 m = is_mmacro(t, &nparam, &params);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006139 if (m) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006140 mname = tok_text(t);
H. Peter Anvinc751e862008-06-09 10:18:45 -07006141 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006142 Token *last;
6143 /*
6144 * We have an id which isn't a macro call. We'll assume
6145 * it might be a label; we'll also check to see if a
6146 * colon follows it. Then, if there's another id after
6147 * that lot, we'll check it again for macro-hood.
6148 */
6149 label = last = t;
6150 t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006151 if (tok_white(t))
H. Peter Anvine2c80182005-01-15 22:15:51 +00006152 last = t, t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006153 if (tok_is(t, ':')) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006154 dont_prepend = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006155 last = t, t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006156 if (tok_white(t))
H. Peter Anvine2c80182005-01-15 22:15:51 +00006157 last = t, t = t->next;
6158 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006159 if (!tok_type(t, TOK_ID) || !(m = is_mmacro(t, &nparam, &params)))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006160 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006161 last->next = NULL;
H. Peter Anvin8571f062019-09-23 16:40:03 -07006162 mname = tok_text(t);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006163 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006164 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006165
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07006166 if (unlikely(mmacro_deadman.total >= nasm_limit[LIMIT_MMACROS] ||
6167 mmacro_deadman.levels >= nasm_limit[LIMIT_MACRO_LEVELS])) {
6168 if (!mmacro_deadman.triggered) {
6169 nasm_nonfatal("interminable multiline macro recursion");
6170 mmacro_deadman.triggered = true;
6171 }
6172 return 0;
6173 }
6174
6175 mmacro_deadman.total++;
6176 mmacro_deadman.levels++;
6177
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006178 /*
6179 * Fix up the parameters: this involves stripping leading and
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07006180 * trailing whitespace and stripping braces if they are present.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006181 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006182 nasm_newn(paramlen, nparam+1);
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07006183
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006184 for (i = 1; (t = params[i]); i++) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006185 bool braced = false;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08006186 int brace = 0;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006187 int white = 0;
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07006188 bool comma = !m->plus || i < nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006189
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006190 t = skip_white(t);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006191 if (tok_is(t, '{')) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006192 t = t->next;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006193 brace = 1;
6194 braced = true;
6195 comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006196 }
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006197
6198 params[i] = t;
6199 for (; t; t = t->next) {
6200 if (tok_white(t)) {
6201 white++;
6202 continue;
6203 }
6204
6205 if (t->type == TOK_OTHER && t->len == 1) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006206 switch (t->text.a[0]) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006207 case ',':
6208 if (comma && !brace)
6209 goto endparam;
6210 break;
6211
6212 case '{':
6213 brace++;
6214 break;
6215
6216 case '}':
6217 brace--;
6218 if (braced && !brace) {
6219 paramlen[i] += white;
6220 goto endparam;
6221 }
6222 break;
6223
6224 default:
6225 break;
6226 }
6227 }
6228
6229 paramlen[i] += white + 1;
6230 white = 0;
6231 }
6232 endparam:
6233 ;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006234 }
6235
6236 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006237 * OK, we have a MMacro structure together with a set of
6238 * parameters. We must now go through the expansion and push
6239 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00006240 * parameter tokens and macro-local tokens doesn't get done
6241 * until the single-line macro substitution process; this is
6242 * because delaying them allows us to change the semantics
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006243 * later through %rotate and give the right semantics for
6244 * nested mmacros.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006245 *
6246 * First, push an end marker on to istk->expansion, mark this
6247 * macro as in progress, and set up its invocation-specific
6248 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006249 */
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07006250 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006251 ll->next = istk->expansion;
6252 ll->finishes = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006253 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006254
6255 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006256 * Save the previous MMacro expansion in the case of
6257 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006258 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006259#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006260 if (m->max_depth && m->in_progress)
6261 push_mmacro(m);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006262#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006263
6264 m->in_progress ++;
6265 m->params = params;
6266 m->iline = tline;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006267 m->iname = nasm_strdup(mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006268 m->nparam = nparam;
6269 m->rotate = 0;
6270 m->paramlen = paramlen;
6271 m->unique = unique++;
6272 m->lineno = 0;
6273 m->condcnt = 0;
6274
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006275 m->mstk = istk->mstk;
6276 istk->mstk.mstk = istk->mstk.mmac = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006277
6278 list_for_each(l, m->expansion) {
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07006279 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006280 ll->next = istk->expansion;
6281 istk->expansion = ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006282 ll->first = dup_tlist(l->first, NULL);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006283 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006284
6285 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006286 * If we had a label, and this macro definition does not include
6287 * a %00, push it on as the first line of, ot
H. Peter Anvineba20a72002-04-30 20:53:55 +00006288 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006289 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006290 if (label) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006291 /*
6292 * We had a label. If this macro contains an %00 parameter,
6293 * save the value as a special parameter (which is what it
6294 * is), otherwise push it as the first line of the macro
6295 * expansion.
6296 */
6297 if (m->capture_label) {
6298 params[0] = dup_Token(NULL, label);
6299 paramlen[0] = 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006300 free_tlist(startline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006301 } else {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006302 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006303 ll->finishes = NULL;
6304 ll->next = istk->expansion;
6305 istk->expansion = ll;
6306 ll->first = startline;
6307 if (!dont_prepend) {
6308 while (label->next)
6309 label = label->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07006310 label->next = tt = make_tok_char(NULL, ':');
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006311 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006312 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00006313 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006314
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07006315 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006316
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07006317 if (list_option('m') && !m->nolist)
6318 list_mmacro_call(m);
6319
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006320 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006321}
6322
H. Peter Anvin130736c2016-02-17 20:27:41 -08006323/*
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006324 * This function decides if an error message should be suppressed.
6325 * It will never be called with a severity level of ERR_FATAL or
6326 * higher.
H. Peter Anvin130736c2016-02-17 20:27:41 -08006327 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006328static bool pp_suppress_error(errflags severity)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02006329{
H. Peter Anvin130736c2016-02-17 20:27:41 -08006330 /*
6331 * If we're in a dead branch of IF or something like it, ignore the error.
6332 * However, because %else etc are evaluated in the state context
6333 * of the previous branch, errors might get lost:
6334 * %if 0 ... %else trailing garbage ... %endif
6335 * So %else etc should set the ERR_PP_PRECOND flag.
6336 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006337 if (istk && istk->conds &&
H. Peter Anvin130736c2016-02-17 20:27:41 -08006338 ((severity & ERR_PP_PRECOND) ?
6339 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07006340 !emitting(istk->conds->state)))
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006341 return true;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02006342
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006343 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00006344}
6345
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006346static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006347stdmac_file(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006348{
6349 (void)s;
6350 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006351 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006352
H. Peter Anvin8571f062019-09-23 16:40:03 -07006353 return make_tok_qstr(NULL, src_get_fname());
H. Peter Anvin8b262472019-02-26 14:00:54 -08006354}
6355
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006356static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006357stdmac_line(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006358{
6359 (void)s;
6360 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006361 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006362
H. Peter Anvin8571f062019-09-23 16:40:03 -07006363 return make_tok_num(NULL, src_get_linnum());
H. Peter Anvin8b262472019-02-26 14:00:54 -08006364}
6365
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006366static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006367stdmac_bits(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006368{
6369 (void)s;
6370 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006371 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006372
H. Peter Anvin8571f062019-09-23 16:40:03 -07006373 return make_tok_num(NULL, globalbits);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006374}
6375
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006376static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006377stdmac_ptr(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006378{
H. Peter Anvin8b262472019-02-26 14:00:54 -08006379 (void)s;
6380 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006381 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006382
6383 switch (globalbits) {
6384 case 16:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006385 return new_Token(NULL, TOK_ID, "word", 4);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006386 case 32:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006387 return new_Token(NULL, TOK_ID, "dword", 5);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006388 case 64:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006389 return new_Token(NULL, TOK_ID, "qword", 5);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006390 default:
6391 panic();
6392 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08006393}
6394
H. Peter Anvin8b262472019-02-26 14:00:54 -08006395/* Add magic standard macros */
6396struct magic_macros {
6397 const char *name;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006398 int nparam;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006399 ExpandSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006400};
6401static const struct magic_macros magic_macros[] =
6402{
H. Peter Anvind2354082019-08-27 16:38:48 -07006403 { "__?FILE?__", 0, stdmac_file },
6404 { "__?LINE?__", 0, stdmac_line },
6405 { "__?BITS?__", 0, stdmac_bits },
6406 { "__?PTR?__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08006407 { NULL, 0, NULL }
6408};
6409
6410static void pp_add_magic_stdmac(void)
6411{
6412 const struct magic_macros *m;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006413 SMacro tmpl;
6414
6415 nasm_zero(tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006416
6417 for (m = magic_macros; m->name; m++) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006418 tmpl.nparam = m->nparam;
6419 tmpl.expand = m->func;
6420 define_smacro(m->name, true, NULL, &tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006421 }
6422}
6423
H. Peter Anvin734b1882002-04-30 21:01:08 +00006424static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006425pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006426{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006427 int apass;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006428 struct Include *inc;
H. Peter Anvin7383b402008-09-24 10:20:40 -07006429
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006430 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006431 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07006432 nested_mac_count = 0;
6433 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07006434 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006435 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07006436 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006437 pp_mode = mode;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07006438
6439 /* Reset options to default */
6440 nasm_zero(ppopt);
H. Peter Anvinf7606612016-07-13 14:23:48 -07006441
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07006442 if (!use_loaded)
6443 use_loaded = nasm_malloc(use_package_count * sizeof(bool));
6444 memset(use_loaded, 0, use_package_count * sizeof(bool));
6445
H. Peter Anvin6686de22019-08-10 05:33:14 -07006446 /* First set up the top level input file */
6447 nasm_new(istk);
6448 istk->fp = nasm_open_read(file, NF_TEXT);
6449 src_set(0, file);
6450 istk->lineinc = 1;
6451 if (!istk->fp)
6452 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
6453
6454 strlist_add(deplist, file);
6455
6456 /*
6457 * Set up the stdmac packages as a virtual include file,
6458 * indicated by a null file pointer.
6459 */
6460 nasm_new(inc);
6461 inc->next = istk;
6462 inc->fname = src_set_fname(NULL);
6463 inc->nolist = !list_option('b');
6464 istk = inc;
6465 lfmt->uplevel(LIST_INCLUDE, 0);
6466
H. Peter Anvin8b262472019-02-26 14:00:54 -08006467 pp_add_magic_stdmac();
6468
H. Peter Anvinf7606612016-07-13 14:23:48 -07006469 if (tasm_compatible_mode)
6470 pp_add_stdmac(nasm_stdmac_tasm);
6471
6472 pp_add_stdmac(nasm_stdmac_nasm);
6473 pp_add_stdmac(nasm_stdmac_version);
6474
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006475 if (extrastdmac)
6476 pp_add_stdmac(extrastdmac);
6477
H. Peter Anvinf7606612016-07-13 14:23:48 -07006478 stdmacpos = stdmacros[0];
6479 stdmacnext = &stdmacros[1];
6480
H. Peter Anvind2456592008-06-19 15:04:18 -07006481 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006482
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006483 /*
H. Peter Anvind2354082019-08-27 16:38:48 -07006484 * Define the __?PASS?__ macro. This is defined here unlike all the
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07006485 * other builtins, because it is special -- it varies between
6486 * passes -- but there is really no particular reason to make it
6487 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006488 *
6489 * 0 = dependencies only
6490 * 1 = preparatory passes
6491 * 2 = final pass
6492 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006493 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006494 switch (mode) {
6495 case PP_NORMAL:
6496 apass = pass_final() ? 2 : 1;
6497 break;
6498 case PP_DEPS:
6499 apass = 0;
6500 break;
6501 case PP_PREPROC:
6502 apass = 3;
6503 break;
6504 default:
6505 panic();
6506 }
6507
H. Peter Anvin8571f062019-09-23 16:40:03 -07006508 define_smacro("__?PASS?__", true, make_tok_num(NULL, apass), NULL);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006509}
6510
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006511static void pp_init(void)
6512{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006513}
6514
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006515/*
6516 * Get a line of tokens. If we popped the macro expansion/include stack,
6517 * we return a pointer to the dummy token tok_pop; at that point if
6518 * istk is NULL then we have reached end of input;
6519 */
6520static Token tok_pop; /* Dummy token placeholder */
6521
6522static Token *pp_tokline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006523{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006524 while (true) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006525 Line *l = istk->expansion;
6526 Token *tline = NULL;
6527 Token *dtline;
6528
H. Peter Anvine2c80182005-01-15 22:15:51 +00006529 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006530 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00006531 * buffer or from the input file.
6532 */
6533 tline = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006534 while (l && l->finishes) {
6535 MMacro *fm = l->finishes;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006536
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006537 if (!fm->name && fm->in_progress > 1) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006538 /*
6539 * This is a macro-end marker for a macro with no
6540 * name, which means it's not really a macro at all
6541 * but a %rep block, and the `in_progress' field is
6542 * more than 1, meaning that we still need to
6543 * repeat. (1 means the natural last repetition; 0
6544 * means termination by %exitrep.) We have
6545 * therefore expanded up to the %endrep, and must
6546 * push the whole block on to the expansion buffer
6547 * again. We don't bother to remove the macro-end
6548 * marker: we'd only have to generate another one
6549 * if we did.
6550 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006551 fm->in_progress--;
6552 list_for_each(l, fm->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006553 Token *t, *tt, **tail;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006554 Line *ll;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006555
Chang S. Baebec812f2020-02-07 15:49:38 -08006556 istk->mstk.mstk->lineno = 0;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006557 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006558 ll->next = istk->expansion;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006559 tail = &ll->first;
6560
6561 list_for_each(t, l->first) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006562 if (t->len) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006563 tt = *tail = dup_Token(NULL, t);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006564 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006565 }
6566 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006567 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006568 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006569 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006570 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006571 MMacro *m = istk->mstk.mstk;
6572
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006573 /*
6574 * Check whether a `%rep' was started and not ended
6575 * within this macro expansion. This can happen and
6576 * should be detected. It's a fatal error because
6577 * I'm too confused to work out how to recover
6578 * sensibly from it.
6579 */
6580 if (defining) {
6581 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07006582 nasm_panic("defining with name in expansion");
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006583 else if (m->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07006584 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006585 " expansion of macro `%s'", m->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006586 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006587
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006588 /*
6589 * FIXME: investigate the relationship at this point between
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006590 * istk->mstk.mstk and fm
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006591 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006592 istk->mstk = m->mstk;
6593 if (m->name) {
6594 /*
6595 * This was a real macro call, not a %rep, and
6596 * therefore the parameter information needs to
6597 * be freed and the iteration count/nesting
6598 * depth adjusted.
6599 */
6600
6601 if (!--mmacro_deadman.levels) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006602 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006603 * If all mmacro processing done,
6604 * clear all counters and the deadman
6605 * message trigger.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006606 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006607 nasm_zero(mmacro_deadman); /* Clear all counters */
Adam Majer91e72402017-07-25 10:42:01 +02006608 }
6609
Adam Majer91e72402017-07-25 10:42:01 +02006610#if 0
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006611 if (m->prev) {
6612 pop_mmacro(m);
6613 fm->in_progress --;
6614 } else
Adam Majer91e72402017-07-25 10:42:01 +02006615#endif
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006616 {
6617 nasm_free(m->params);
6618 free_tlist(m->iline);
6619 nasm_free(m->paramlen);
6620 fm->in_progress = 0;
6621 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006622 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006623
6624 /*
6625 * FIXME It is incorrect to always free_mmacro here.
6626 * It leads to usage-after-free.
6627 *
6628 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
6629 */
6630#if 0
6631 else
6632 free_mmacro(m);
6633#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006634 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006635 istk->expansion = l->next;
6636 nasm_free(l);
6637 lfmt->downlevel(LIST_MACRO);
6638 return &tok_pop;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006639 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006640
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006641 do { /* until we get a line we can use */
6642 char *line;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006643
6644 if (istk->expansion) { /* from a macro expansion */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006645 Line *l = istk->expansion;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006646 int32_t lineno;
6647
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006648 if (istk->mstk.mstk) {
6649 istk->mstk.mstk->lineno++;
6650 if (istk->mstk.mstk->fname)
6651 lineno = istk->mstk.mstk->lineno +
6652 istk->mstk.mstk->xline;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006653 else
6654 lineno = 0; /* Defined at init time or builtin */
6655 } else {
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006656 lineno = src_get_linnum();
H. Peter Anvin6686de22019-08-10 05:33:14 -07006657 }
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006658
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006659 tline = l->first;
6660 istk->expansion = l->next;
6661 nasm_free(l);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006662
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006663 line = detoken(tline, false);
H. Peter Anvin6686de22019-08-10 05:33:14 -07006664 if (!istk->nolist)
6665 lfmt->line(LIST_MACRO, lineno, line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006666 nasm_free(line);
6667 } else if ((line = read_line())) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006668 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006669 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006670 nasm_free(line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006671 } else {
6672 /*
6673 * The current file has ended; work down the istk
6674 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00006675 Include *i = istk;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006676 if (i->fp)
6677 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006678 if (i->conds) {
6679 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07006680 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006681 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00006682 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07006683 if (i->next)
6684 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006685 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08006686 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006687 nasm_free(i);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006688 return &tok_pop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006689 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006690 } while (0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006691
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006692 /*
6693 * We must expand MMacro parameters and MMacro-local labels
6694 * _before_ we plunge into directive processing, to cope
6695 * with things like `%define something %1' such as STRUC
6696 * uses. Unless we're _defining_ a MMacro, in which case
6697 * those tokens should be left alone to go into the
6698 * definition; and unless we're in a non-emitting
6699 * condition, in which case we don't want to meddle with
6700 * anything.
6701 */
H. Peter Anvin (Intel)bacf04a2020-06-08 13:29:06 -07006702 if (!defining &&
6703 !(istk->conds && !emitting(istk->conds->state)) &&
6704 !(istk->mstk.mmac && !istk->mstk.mmac->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006705 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006706 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006707
H. Peter Anvine2c80182005-01-15 22:15:51 +00006708 /*
6709 * Check the line to see if it's a preprocessor directive.
6710 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006711 if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) {
6712 if (dtline)
6713 return dtline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006714 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006715 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006716 * We're defining a multi-line macro. We emit nothing
6717 * at all, and just
6718 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00006719 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006720 MMacro *mmac = defining->dstk.mmac;
6721
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006722 Line *l = nasm_malloc(sizeof(Line));
6723 l->next = defining->expansion;
6724 l->first = tline;
6725 l->finishes = NULL;
6726 defining->expansion = l;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006727
6728 /*
6729 * Remember if this mmacro expansion contains %00:
6730 * if it does, we will have to handle leading labels
6731 * specially.
6732 */
6733 if (mmac) {
6734 const Token *t;
6735 list_for_each(t, tline) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006736 if (!memcmp(t->text.a, "%00", 4))
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006737 mmac->capture_label = true;
6738 }
6739 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006740 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006741 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006742 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00006743 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006744 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00006745 * directive so we keep our place correctly.
6746 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006747 free_tlist(tline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006748 } else if (istk->mstk.mstk && !istk->mstk.mstk->in_progress) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006749 /*
6750 * We're in a %rep block which has been terminated, so
6751 * we're walking through to the %endrep without
6752 * emitting anything. Emit nothing at all, not even a
6753 * blank line: when we emerge from the %rep block we'll
6754 * give a line-number directive so we keep our place
6755 * correctly.
6756 */
6757 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006758 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006759 tline = expand_smacro(tline);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006760 if (!expand_mmacro(tline))
6761 return tline;
6762 }
6763 }
6764}
6765
6766static char *pp_getline(void)
6767{
6768 char *line = NULL;
6769 Token *tline;
6770
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006771 while (true) {
6772 tline = pp_tokline();
6773 if (tline == &tok_pop) {
6774 /*
6775 * We popped the macro/include stack. If istk is empty,
6776 * we are at end of input, otherwise just loop back.
6777 */
6778 if (!istk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006779 break;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006780 } else {
6781 /*
6782 * De-tokenize the line and emit it.
6783 */
6784 line = detoken(tline, true);
6785 free_tlist(tline);
6786 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006787 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006788 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006789
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006790 if (list_option('e') && istk && !istk->nolist && line && line[0]) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006791 char *buf = nasm_strcat(" ;;; ", line);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006792 lfmt->line(LIST_MACRO, -1, buf);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006793 nasm_free(buf);
6794 }
6795
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006796 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006797}
6798
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006799static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006800{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006801 if (defining) {
6802 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006803 nasm_nonfatal("end of file while still defining macro `%s'",
6804 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006805 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006806 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006807 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006808
6809 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006810 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006811 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08006812
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006813 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006814 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07006815 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006816 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006817 Include *i = istk;
6818 istk = istk->next;
6819 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03006820 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006821 }
6822 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006823 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07006824 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006825}
6826
6827static void pp_cleanup_session(void)
6828{
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07006829 nasm_free(use_loaded);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006830 free_llist(predef);
6831 predef = NULL;
6832 delete_Blocks();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006833 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006834}
6835
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006836static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006837{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006838 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006839}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00006840
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006841static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006842{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006843 Token *inc, *space, *name;
6844 Line *l;
6845
H. Peter Anvin734b1882002-04-30 21:01:08 +00006846 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006847 space = new_White(name);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006848 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006849
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006850 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006851 l->next = predef;
6852 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006853 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006854 predef = l;
6855}
6856
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006857static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006858{
6859 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006860 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00006861 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006862
6863 equals = strchr(definition, '=');
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006864 space = new_White(NULL);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006865 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006866 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006867 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00006868 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006869 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006870 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006871
H. Peter Anvin8571f062019-09-23 16:40:03 -07006872 /* We can't predefine a TOK_LOCAL_MACRO for obvious reasons... */
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006873 if (space->next->type != TOK_PREPROC_ID &&
6874 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08006875 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006876
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006877 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006878 l->next = predef;
6879 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006880 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006881 predef = l;
6882}
6883
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006884static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00006885{
6886 Token *def, *space;
6887 Line *l;
6888
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006889 space = new_White(NULL);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006890 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006891 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00006892
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006893 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00006894 l->next = predef;
6895 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006896 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00006897 predef = l;
6898}
6899
H. Peter Anvin05990342018-06-11 13:32:42 -07006900/* Insert an early preprocessor command that doesn't need special handling */
6901static void pp_pre_command(const char *what, char *string)
6902{
6903 char *cmd;
6904 Token *def, *space;
6905 Line *l;
6906
6907 def = tokenize(string);
6908 if (what) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006909 space = new_White(def);
H. Peter Anvin8571f062019-09-23 16:40:03 -07006910 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
6911 def = new_Token(space, TOK_PREPROC_ID, cmd, nasm_last_string_len());
6912 nasm_free(cmd);
H. Peter Anvin05990342018-06-11 13:32:42 -07006913 }
6914
6915 l = nasm_malloc(sizeof(Line));
6916 l->next = predef;
6917 l->first = def;
6918 l->finishes = NULL;
6919 predef = l;
6920}
6921
H. Peter Anvinf7606612016-07-13 14:23:48 -07006922static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006923{
H. Peter Anvinf7606612016-07-13 14:23:48 -07006924 macros_t **mp;
6925
6926 /* Find the end of the list and avoid duplicates */
6927 for (mp = stdmacros; *mp; mp++) {
6928 if (*mp == macros)
6929 return; /* Nothing to do */
6930 }
6931
6932 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
6933
6934 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00006935}
6936
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006937static void pp_extra_stdmac(macros_t *macros)
6938{
6939 extrastdmac = macros;
6940}
6941
H. Peter Anvin8571f062019-09-23 16:40:03 -07006942/* Create a numeric token */
6943static Token *make_tok_num(Token *next, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006944{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04006945 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08006946 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvin8571f062019-09-23 16:40:03 -07006947 return new_Token(next, TOK_NUMBER, numbuf, len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006948}
6949
H. Peter Anvin8571f062019-09-23 16:40:03 -07006950/* Create a quoted string token */
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07006951static Token *make_tok_qstr_len(Token *next, const char *str, size_t len)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006952{
H. Peter Anvin8571f062019-09-23 16:40:03 -07006953 char *p = nasm_quote(str, &len);
6954 return new_Token_free(next, TOK_STRING, p, len);
6955}
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07006956static Token *make_tok_qstr(Token *next, const char *str)
6957{
6958 return make_tok_qstr_len(next, str, strlen(str));
6959}
H. Peter Anvin8571f062019-09-23 16:40:03 -07006960
6961/* Create a single-character operator token */
6962static Token *make_tok_char(Token *next, char op)
6963{
6964 Token *t = new_Token(next, TOK_OTHER, NULL, 1);
6965 t->text.a[0] = op;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006966 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006967}
6968
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006969static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07006970{
6971 if (!m)
6972 return;
6973
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006974 /* We need to print the mstk.mmac list in reverse order */
6975 pp_list_one_macro(m->mstk.mmac, severity);
H. Peter Anvin37368952016-05-09 14:10:32 -07006976
6977 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07006978 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08006979 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07006980 }
6981}
6982
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006983static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006984{
H. Peter Anvinddb29062018-12-11 00:06:29 -08006985 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006986
H. Peter Anvinddb29062018-12-11 00:06:29 -08006987 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
6988 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006989
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03006990 if (istk)
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006991 pp_list_one_macro(istk->mstk.mmac, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006992
H. Peter Anvinddb29062018-12-11 00:06:29 -08006993 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006994}
6995
H. Peter Anvine7469712016-02-18 02:20:59 -08006996const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006997 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006998 pp_reset,
6999 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08007000 pp_cleanup_pass,
7001 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03007002 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04007003 pp_pre_define,
7004 pp_pre_undefine,
7005 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07007006 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07007007 pp_include_path,
7008 pp_error_list_macros,
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07007009 pp_suppress_error
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00007010};