blob: 83a61b427ad39f444142d1b9ec824e22972269aa [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
79typedef struct SMacro SMacro;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080080typedef struct MMacro MMacro;
81typedef struct MMacroInvocation MMacroInvocation;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000082typedef struct Context Context;
83typedef struct Token Token;
84typedef struct Line Line;
85typedef struct Include Include;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080086typedef struct Cond Cond;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000087
88/*
H. Peter Anvin8571f062019-09-23 16:40:03 -070089 * This is the internal form which we break input lines up into.
90 * Typically stored in linked lists.
91 *
92 * Note that `type' serves a double meaning: TOK_SMAC_START_PARAMS is
93 * not necessarily used as-is, but is also used to encode the number
94 * and expansion type of substituted parameter. So in the definition
95 *
96 * %define a(x,=y) ( (x) & ~(y) )
97 *
98 * the token representing `x' will have its type changed to
99 * tok_smac_param(0) but the one representing `y' will be
100 * tok_smac_param(1); see the accessor functions below.
101 *
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700102 * TOK_INTERNAL_STRING is a string which has been unquoted, but should
103 * be treated as if it was a quoted string. The code is free to change
104 * one into the other at will. TOK_NAKED_STRING is a text token which
105 * should be treated as a string, but which MUST NOT be turned into a
106 * quoted string. TOK_INTERNAL_STRINGs can contain any character,
107 * including NUL, but TOK_NAKED_STRING must be a valid C string.
H. Peter Anvin8571f062019-09-23 16:40:03 -0700108 */
109enum pp_token_type {
110 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT,
111 TOK_CORRUPT, /* Token text modified in an unsafe manner, now bogus */
112 TOK_BLOCK, /* Storage block pointer, not a real token */
113 TOK_ID,
114 TOK_PREPROC_ID, TOK_MMACRO_PARAM, TOK_LOCAL_SYMBOL,
115 TOK_LOCAL_MACRO, TOK_ENVIRON, TOK_STRING,
116 TOK_NUMBER, TOK_FLOAT, TOK_OTHER,
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700117 TOK_INTERNAL_STRING, TOK_NAKED_STRING,
H. Peter Anvin8571f062019-09-23 16:40:03 -0700118 TOK_PREPROC_Q, TOK_PREPROC_QQ,
119 TOK_PASTE, /* %+ */
120 TOK_COND_COMMA, /* %, */
121 TOK_INDIRECT, /* %[...] */
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700122 TOK_XDEF_PARAM, /* Used during %xdefine processing */
H. Peter Anvin8571f062019-09-23 16:40:03 -0700123 TOK_SMAC_START_PARAMS, /* MUST BE LAST IN THE LIST!!! */
124 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
125};
126
127static inline enum pp_token_type tok_smac_param(int param)
128{
129 return TOK_SMAC_START_PARAMS + param;
130}
131static int smac_nparam(enum pp_token_type toktype)
132{
133 return toktype - TOK_SMAC_START_PARAMS;
134}
135static bool is_smac_param(enum pp_token_type toktype)
136{
137 return toktype >= TOK_SMAC_START_PARAMS;
138}
139
140#define PP_CONCAT_MASK(x) (1U << (x))
141
142struct tokseq_match {
143 int mask_head;
144 int mask_tail;
145};
146
147/*
148 * This is tuned so struct Token should be 64 bytes on 64-bit
149 * systems and 32 bytes on 32-bit systems. It enables them
150 * to be nicely cache aligned, and the text to still be kept
151 * inline for nearly all tokens.
152 *
153 * We prohibit tokens of length > MAX_TEXT even though
154 * length here is an unsigned int; this avoids problems
155 * if the length is passed through an interface with type "int",
156 * and is absurdly large anyway.
157 *
158 * For the text mode, in pointer mode the pointer is stored at the end
159 * of the union and the pad field is cleared. This allows short tokens
160 * to be unconditionally tested for by only looking at the first text
161 * bytes and not examining the type or len fields.
162 */
163#define INLINE_TEXT (7*sizeof(char *)-sizeof(enum pp_token_type)-sizeof(unsigned int)-1)
164#define MAX_TEXT (INT_MAX-2)
165
166struct Token {
167 Token *next;
168 enum pp_token_type type;
169 unsigned int len;
170 union {
171 char a[INLINE_TEXT+1];
172 struct {
173 char pad[INLINE_TEXT+1 - sizeof(char *)];
174 char *ptr;
175 } p;
176 } text;
177};
178
179/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700180 * Note on the storage of both SMacro and MMacros: the hash table
181 * indexes them case-insensitively, and we then have to go through a
182 * linked list of potential case aliases (and, for MMacros, parameter
183 * ranges); this is to preserve the matching semantics of the earlier
184 * code. If the number of case aliases for a specific macro is a
185 * performance issue, you may want to reconsider your coding style.
186 */
187
188/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700189 * Function call tp obtain the expansion of an smacro
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700190 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700191typedef Token *(*ExpandSMacro)(const SMacro *s, Token **params, int nparams);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700192
193/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000194 * Store the definition of a single-line macro.
195 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700196enum sparmflags {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -0700197 SPARM_PLAIN = 0,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700198 SPARM_EVAL = 1, /* Evaluate as a numeric expression (=) */
199 SPARM_STR = 2, /* Convert to quoted string ($) */
200 SPARM_NOSTRIP = 4, /* Don't strip braces (!) */
201 SPARM_GREEDY = 8 /* Greedy final parameter (+) */
202};
203
204struct smac_param {
H. Peter Anvin8571f062019-09-23 16:40:03 -0700205 Token name;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700206 enum sparmflags flags;
207};
208
H. Peter Anvine2c80182005-01-15 22:15:51 +0000209struct SMacro {
H. Peter Anvin8b262472019-02-26 14:00:54 -0800210 SMacro *next; /* MUST BE FIRST - see free_smacro() */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800211 char *name;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700212 Token *expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700213 ExpandSMacro expand;
214 intorptr expandpvt;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700215 struct smac_param *params;
216 int nparam;
217 bool greedy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800218 bool casesense;
219 bool in_progress;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -0700220 bool alias; /* This is an alias macro */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000221};
222
223/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800224 * Store the definition of a multi-line macro. This is also used to
225 * store the interiors of `%rep...%endrep' blocks, which are
226 * effectively self-re-invoking multi-line macros which simply
227 * don't have a name or bother to appear in the hash tables. %rep
228 * blocks are signified by having a NULL `name' field.
229 *
230 * In a MMacro describing a `%rep' block, the `in_progress' field
231 * isn't merely boolean, but gives the number of repeats left to
232 * run.
233 *
234 * The `next' field is used for storing MMacros in hash tables; the
235 * `next_active' field is for stacking them on istk entries.
236 *
237 * When a MMacro is being expanded, `params', `iline', `nparam',
238 * `paramlen', `rotate' and `unique' are local to the invocation.
239 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700240
241/*
242 * Expansion stack. Note that .mmac can point back to the macro itself,
243 * whereas .mstk cannot.
244 */
245struct mstk {
246 MMacro *mstk; /* Any expansion, real macro or not */
247 MMacro *mmac; /* Highest level actual mmacro */
248};
249
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800250struct MMacro {
251 MMacro *next;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700252#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800253 MMacroInvocation *prev; /* previous invocation */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700254#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800255 char *name;
256 int nparam_min, nparam_max;
257 bool casesense;
258 bool plus; /* is the last parameter greedy? */
259 bool nolist; /* is this macro listing-inhibited? */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700260 bool capture_label; /* macro definition has %00; capture label */
261 int32_t in_progress; /* is this macro currently being expanded? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800262 int32_t max_depth; /* maximum number of recursive expansions allowed */
263 Token *dlist; /* All defaults as one list */
264 Token **defaults; /* Parameter default pointers */
265 int ndefs; /* number of default parameters */
266 Line *expansion;
267
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700268 struct mstk mstk; /* Macro expansion stack */
269 struct mstk dstk; /* Macro definitions stack */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800270 Token **params; /* actual parameters */
271 Token *iline; /* invocation line */
272 unsigned int nparam, rotate;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700273 char *iname; /* name invoked as */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800274 int *paramlen;
275 uint64_t unique;
276 int lineno; /* Current line number on expansion */
277 uint64_t condcnt; /* number of if blocks... */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700278
H. Peter Anvin274cda82016-05-10 02:56:29 -0700279 const char *fname; /* File where defined */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700280 int32_t xline; /* First line in macro */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800281};
282
283
284/* Store the definition of a multi-line macro, as defined in a
285 * previous recursive macro expansion.
286 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700287#if 0
288
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800289struct MMacroInvocation {
290 MMacroInvocation *prev; /* previous invocation */
291 Token **params; /* actual parameters */
292 Token *iline; /* invocation line */
293 unsigned int nparam, rotate;
294 int *paramlen;
295 uint64_t unique;
296 uint64_t condcnt;
297};
298
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700299#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800300
301/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000302 * The context stack is composed of a linked list of these.
303 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000304struct Context {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800305 Context *next;
H. Peter Anvin8571f062019-09-23 16:40:03 -0700306 const char *name;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800307 struct hash_table localmac;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -0700308 uint64_t number;
309 unsigned int depth;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000310};
311
H. Peter Anvin8571f062019-09-23 16:40:03 -0700312
313static inline const char *tok_text(const struct Token *t)
314{
315 return (t->len <= INLINE_TEXT) ? t->text.a : t->text.p.ptr;
316}
317
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000318/*
H. Peter Anvin8571f062019-09-23 16:40:03 -0700319 * Returns a mutable pointer to the text buffer. The text can be changed,
320 * but the length MUST NOT CHANGE, in either direction; nor is it permitted
321 * to pad with null characters to create an artificially shorter string.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000322 */
H. Peter Anvin8571f062019-09-23 16:40:03 -0700323static inline char *tok_text_buf(struct Token *t)
H. Peter Anvin8b262472019-02-26 14:00:54 -0800324{
H. Peter Anvin8571f062019-09-23 16:40:03 -0700325 return (t->len <= INLINE_TEXT) ? t->text.a : t->text.p.ptr;
H. Peter Anvin8b262472019-02-26 14:00:54 -0800326}
327
H. Peter Anvin8571f062019-09-23 16:40:03 -0700328static inline unsigned int tok_check_len(size_t len)
329{
330 if (unlikely(len > MAX_TEXT))
331 nasm_fatal("impossibly large token");
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400332
H. Peter Anvin8571f062019-09-23 16:40:03 -0700333 return len;
334}
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400335
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700336static inline bool tok_text_match(const struct Token *a, const struct Token *b)
337{
338 return a->len == b->len && !memcmp(tok_text(a), tok_text(b), a->len);
339}
340
341static inline bool tok_match(const struct Token *a, const struct Token *b)
342{
343 return a->type == b->type && tok_text_match(a, b);
344}
345
H. Peter Anvin8571f062019-09-23 16:40:03 -0700346/* strlen() variant useful for set_text() and its variants */
347static size_t tok_strlen(const char *str)
348{
349 return strnlen(str, MAX_TEXT+1);
350}
351
352/*
353 * Set the text field to a copy of the given string; the length if
354 * not given should be obtained with tok_strlen().
355 */
356static Token *set_text(struct Token *t, const char *text, size_t len)
357{
358 char *textp;
359
360 if (t->len > INLINE_TEXT)
361 nasm_free(t->text.p.ptr);
362
363 nasm_zero(t->text.a);
364
365 t->len = tok_check_len(len);
366 textp = (len > INLINE_TEXT)
367 ? (t->text.p.ptr = nasm_malloc(len+1)) : t->text.a;
368 memcpy(textp, text, len+1);
369 return t;
370}
371
372/*
373 * Set the text field to the existing pre-allocated string, either
374 * taking over or freeing the allocation in the process.
375 */
376static Token *set_text_free(struct Token *t, char *text, unsigned int len)
377{
378 if (t->len > INLINE_TEXT)
379 nasm_free(t->text.p.ptr);
380
381 nasm_zero(t->text.a);
382
383 t->len = tok_check_len(len);
384 if (len > INLINE_TEXT) {
385 t->text.p.ptr = text;
386 } else {
387 memcpy(t->text.a, text, len+1);
388 nasm_free(text);
389 }
390
391 return t;
392}
393
394/*
395 * Allocate a new buffer containing a copy of the text field
396 * of the token.
397 */
398static char *dup_text(const struct Token *t)
399{
400 size_t size = t->len + 1;
401 char *p = nasm_malloc(size);
402
403 return memcpy(p, tok_text(t), size);
404}
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000405
406/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800407 * Multi-line macro definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000408 * these, which is essentially a container to allow several linked
409 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700410 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000411 * Note that in this module, linked lists are treated as stacks
412 * wherever possible. For this reason, Lines are _pushed_ on to the
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800413 * `expansion' field in MMacro structures, so that the linked list,
414 * if walked, would give the macro lines in reverse order; this
415 * means that we can walk the list when expanding a macro, and thus
416 * push the lines on to the `expansion' field in _istk_ in reverse
417 * order (so that when popped back off they are in the right
418 * order). It may seem cockeyed, and it relies on my design having
419 * an even number of steps in, but it works...
420 *
421 * Some of these structures, rather than being actual lines, are
422 * markers delimiting the end of the expansion of a given macro.
423 * This is for use in the cycle-tracking and %rep-handling code.
424 * Such structures have `finishes' non-NULL, and `first' NULL. All
425 * others have `finishes' NULL, but `first' may still be NULL if
426 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000427 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000428struct Line {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800429 Line *next;
430 MMacro *finishes;
431 Token *first;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500432};
433
434/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000435 * To handle an arbitrary level of file inclusion, we maintain a
436 * stack (ie linked list) of these things.
437 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000438struct Include {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800439 Include *next;
440 FILE *fp;
441 Cond *conds;
442 Line *expansion;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700443 const char *fname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700444 struct mstk mstk;
H. Peter Anvin6686de22019-08-10 05:33:14 -0700445 int lineno, lineinc;
446 bool nolist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000447};
448
449/*
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700450 * File real name hash, so we don't have to re-search the include
451 * path for every pass (and potentially more than that if a file
452 * is used more than once.)
453 */
454struct hash_table FileHash;
455
456/*
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -0700457 * Counters to trap on insane macro recursion or processing.
458 * Note: for smacros these count *down*, for mmacros they count *up*.
459 */
460struct deadman {
461 int64_t total; /* Total number of macros/tokens */
462 int64_t levels; /* Descent depth across all macros */
463 bool triggered; /* Already triggered, no need for error msg */
464};
465
466static struct deadman smacro_deadman, mmacro_deadman;
467
468/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000469 * Conditional assembly: we maintain a separate stack of these for
470 * each level of file inclusion. (The only reason we keep the
471 * stacks separate is to ensure that a stray `%endif' in a file
472 * included from within the true branch of a `%if' won't terminate
473 * it and cause confusion: instead, rightly, it'll cause an error.)
474 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -0700475enum cond_state {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000476 /*
477 * These states are for use just after %if or %elif: IF_TRUE
478 * means the condition has evaluated to truth so we are
479 * currently emitting, whereas IF_FALSE means we are not
480 * currently emitting but will start doing so if a %else comes
481 * up. In these states, all directives are admissible: %elif,
482 * %else and %endif. (And of course %if.)
483 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800484 COND_IF_TRUE, COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000485 /*
486 * These states come up after a %else: ELSE_TRUE means we're
487 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
488 * any %elif or %else will cause an error.
489 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800490 COND_ELSE_TRUE, COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000491 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200492 * These states mean that we're not emitting now, and also that
493 * nothing until %endif will be emitted at all. COND_DONE is
494 * used when we've had our moment of emission
495 * and have now started seeing %elifs. COND_NEVER is used when
496 * the condition construct in question is contained within a
497 * non-emitting branch of a larger condition construct,
498 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000499 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800500 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000501};
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -0700502struct Cond {
503 Cond *next;
504 enum cond_state state;
505};
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800506#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000507
H. Peter Anvin70653092007-10-19 14:42:29 -0700508/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000509 * These defines are used as the possible return values for do_directive
510 */
511#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300512#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000513
Keith Kanios852f1ee2009-07-12 00:19:55 -0500514/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000515 * Condition codes. Note that we use c_ prefix not C_ because C_ is
516 * used in nasm.h for the "real" condition codes. At _this_ level,
517 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
518 * ones, so we need a different enum...
519 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700520static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000521 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
522 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000523 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000524};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700525enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000526 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
527 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 -0700528 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
529 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000530};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700531static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000532 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
533 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 +0000534 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000535};
536
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800537/*
538 * Directive names.
539 */
540/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
541static int is_condition(enum preproc_token arg)
542{
543 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
544}
545
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000546/* For TASM compatibility we need to be able to recognise TASM compatible
547 * conditional compilation directives. Using the NASM pre-processor does
548 * not work, so we look for them specifically from the following list and
549 * then jam in the equivalent NASM directive into the input stream.
550 */
551
H. Peter Anvine2c80182005-01-15 22:15:51 +0000552enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000553 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
554 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
555};
556
H. Peter Anvin476d2862007-10-02 22:04:15 -0700557static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000558 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
559 "ifndef", "include", "local"
560};
561
562static int StackSize = 4;
H. Peter Anvin6c8b2be2016-05-24 23:46:50 -0700563static const char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000564static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800565static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000566
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000567static Context *cstk;
568static Include *istk;
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300569static const struct strlist *ipath_list;
H. Peter Anvind2354082019-08-27 16:38:48 -0700570static bool do_aliases;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000571
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300572static struct strlist *deplist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000573
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300574static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000575
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800576static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700577static bool do_predef;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800578static enum preproc_mode pp_mode;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000579
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000580/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800581 * The current set of multi-line macros we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000582 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800583static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000584
585/*
586 * The current set of single-line macros we have defined.
587 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700588static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000589
590/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800591 * The multi-line macro we are currently defining, or the %rep
592 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000593 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800594static MMacro *defining;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000595
Charles Crayned4200be2008-07-12 16:42:33 -0700596static uint64_t nested_mac_count;
597static uint64_t nested_rep_count;
598
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000599/*
600 * The number of macro parameters to allocate space for at a time.
601 */
602#define PARAM_DELTA 16
603
604/*
H. Peter Anvinf7606612016-07-13 14:23:48 -0700605 * The standard macro set: defined in macros.c in a set of arrays.
606 * This gives our position in any macro set, while we are processing it.
607 * The stdmacset is an array of such macro sets.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000608 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700609static macros_t *stdmacpos;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700610static macros_t **stdmacnext;
611static macros_t *stdmacros[8];
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +0300612static macros_t *extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000613
614/*
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -0700615 * Map of which %use packages have been loaded
616 */
617static bool *use_loaded;
618
619/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000620 * Forward declarations.
621 */
H. Peter Anvinf7606612016-07-13 14:23:48 -0700622static void pp_add_stdmac(macros_t *macros);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000623static Token *expand_mmac_params(Token * tline);
624static Token *expand_smacro(Token * tline);
625static Token *expand_id(Token * tline);
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +0400626static Context *get_ctx(const char *name, const char **namep);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700627static Token *make_tok_num(Token *next, int64_t val);
628static Token *make_tok_qstr(Token *next, const char *str);
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -0700629static Token *make_tok_qstr_len(Token *next, const char *str, size_t len);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700630static Token *make_tok_char(Token *next, char op);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700631static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700632 const char *text, size_t txtlen);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700633static Token *new_Token_free(Token * next, enum pp_token_type type,
634 char *text, size_t txtlen);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700635static Token *dup_Token(Token *next, const Token *src);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700636static Token *new_White(Token *next);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700637static Token *delete_Token(Token *t);
638static Token *steal_Token(Token *dst, Token *src);
H. Peter Anvindd88aa92019-09-12 19:39:48 -0700639static const struct use_package *
H. Peter Anvin8571f062019-09-23 16:40:03 -0700640get_use_pkg(Token *t, const char *dname, const char **name);
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -0700641static void mark_smac_params(Token *tline, const SMacro *tmpl,
642 enum pp_token_type type);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000643
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700644/* Safe test for token type, false on x == NULL */
645static inline bool tok_type(const Token *x, enum pp_token_type t)
646{
647 return x && x->type == t;
648}
649
650/* Whitespace token? */
651static inline bool tok_white(const Token *x)
652{
653 return tok_type(x, TOK_WHITESPACE);
654}
655
656/* Skip past any whitespace */
657static inline Token *skip_white(Token *x)
658{
659 while (tok_white(x))
660 x = x->next;
661
662 return x;
663}
664
665/* Delete any whitespace */
666static Token *zap_white(Token *x)
667{
668 while (tok_white(x))
669 x = delete_Token(x);
670
671 return x;
672}
673
H. Peter Anvin8571f062019-09-23 16:40:03 -0700674/*
675 * Single special character tests. The use of & rather than && is intentional; it
676 * tells the compiler that it is safe to access text.a[1] unconditionally; hopefully
677 * a smart compiler should turn it into a 16-bit memory reference.
678 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700679static inline bool tok_is(const Token *x, char c)
680{
H. Peter Anvin8571f062019-09-23 16:40:03 -0700681 return x && ((x->text.a[0] == c) & !x->text.a[1]);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700682}
683
684/* True if any other kind of token that "c", but not NULL */
685static inline bool tok_isnt(const Token *x, char c)
686{
H. Peter Anvin8571f062019-09-23 16:40:03 -0700687 return x && !((x->text.a[0] == c) & !x->text.a[1]);
688}
689
690/*
691 * Unquote a token if it is a string, and set its type to
692 * TOK_INTERNAL_STRING.
693 */
694static const char *unquote_token(Token *t)
695{
696 if (t->type != TOK_STRING)
697 return tok_text(t);
698
699 t->type = TOK_INTERNAL_STRING;
700
701 if (t->len > INLINE_TEXT) {
702 char *p = t->text.p.ptr;
703
704 t->len = nasm_unquote(p, NULL);
705
706 if (t->len <= INLINE_TEXT) {
707 nasm_zero(t->text.a);
708 memcpy(t->text.a, p, t->len);
709 nasm_free(p);
710 return t->text.a;
711 } else {
712 return p;
713 }
714 } else {
715 t->len = nasm_unquote(t->text.a, NULL);
716 return t->text.a;
717 }
718}
719
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700720/*
721 * Same as unquote_token(), but error out if the resulting string
722 * contains unacceptable control characters.
723 */
H. Peter Anvin8571f062019-09-23 16:40:03 -0700724static const char *unquote_token_cstr(Token *t)
725{
726 if (t->type != TOK_STRING)
727 return tok_text(t);
728
729 t->type = TOK_INTERNAL_STRING;
730
731 if (t->len > INLINE_TEXT) {
732 char *p = t->text.p.ptr;
733
734 t->len = nasm_unquote_cstr(p, NULL);
735
736 if (t->len <= INLINE_TEXT) {
737 nasm_zero(t->text.a);
738 memcpy(t->text.a, p, t->len);
739 nasm_free(p);
740 return t->text.a;
741 } else {
742 return p;
743 }
744 } else {
745 t->len = nasm_unquote_cstr(t->text.a, NULL);
746 return t->text.a;
747 }
748}
749
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700750/*
751 * Convert a TOK_INTERNAL_STRING token to a quoted
752 * TOK_STRING tokens.
753 */
754static Token *quote_any_token(Token *t);
755static inline Token *quote_token(Token *t)
756{
757 if (likely(!tok_is(t, TOK_INTERNAL_STRING)))
758 return t;
759
760 return quote_any_token(t);
761}
762
763/*
764 * Convert *any* kind of token to a quoted
765 * TOK_STRING token.
766 */
767static Token *quote_any_token(Token *t)
H. Peter Anvin8571f062019-09-23 16:40:03 -0700768{
769 size_t len;
770 char *p;
771
772 p = nasm_quote(tok_text(t), &len);
773 t->type = TOK_STRING;
774 return set_text_free(t, p, len);
775}
776
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400777/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700778 * In-place reverse a list of tokens.
779 */
780static Token *reverse_tokens(Token *t)
781{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800782 Token *prev = NULL;
783 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700784
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800785 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400786 next = t->next;
787 t->next = prev;
788 prev = t;
789 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800790 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700791
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800792 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700793}
794
795/*
H. Peter Anvin8571f062019-09-23 16:40:03 -0700796 * getenv() variant operating on an input token
797 */
798static const char *pp_getenv(const Token *t, bool warn)
799{
800 const char *txt = tok_text(t);
801 const char *v;
802 char *buf = NULL;
803 bool is_string = false;
804
805 if (!t)
806 return NULL;
807
808 switch (t->type) {
809 case TOK_ENVIRON:
810 txt += 2; /* Skip leading %! */
811 is_string = nasm_isquote(*txt);
812 break;
813
814 case TOK_STRING:
815 is_string = true;
816 break;
817
818 case TOK_INTERNAL_STRING:
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700819 case TOK_NAKED_STRING:
H. Peter Anvin8571f062019-09-23 16:40:03 -0700820 case TOK_ID:
821 is_string = false;
822 break;
823
824 default:
825 return NULL;
826 }
827
828 if (is_string) {
829 buf = nasm_strdup(txt);
830 nasm_unquote_cstr(buf, NULL);
831 txt = buf;
832 }
833
834 v = getenv(txt);
835 if (warn && !v) {
836 /*!
837 *!environment [on] nonexistent environment variable
838 *! warns if a nonexistent environment variable
839 *! is accessed using the \c{%!} preprocessor
840 *! construct (see \k{getenv}.) Such environment
841 *! variables are treated as empty (with this
842 *! warning issued) starting in NASM 2.15;
843 *! earlier versions of NASM would treat this as
844 *! an error.
845 */
846 nasm_warn(WARN_ENVIRONMENT, "nonexistent environment variable `%s'", txt);
847 v = "";
848 }
849
850 if (buf)
851 nasm_free(buf);
852
853 return v;
854}
855
856/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300857 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000858 * front of them. We do it here because I could not find any other
859 * place to do it for the moment, and it is a hack (ideally it would
860 * be nice to be able to use the NASM pre-processor to do it).
861 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000862static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000863{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000864 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400865 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000866
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400867 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000868
869 /* Binary search for the directive name */
870 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400871 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400872 q = nasm_skip_word(p);
873 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000874 if (len) {
875 oldchar = p[len];
876 p[len] = 0;
877 while (j - i > 1) {
878 k = (j + i) / 2;
879 m = nasm_stricmp(p, tasm_directives[k]);
880 if (m == 0) {
881 /* We have found a directive, so jam a % in front of it
882 * so that NASM will then recognise it as one if it's own.
883 */
884 p[len] = oldchar;
885 len = strlen(p);
886 oldline = line;
887 line = nasm_malloc(len + 2);
888 line[0] = '%';
889 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700890 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300891 * NASM does not recognise IFDIFI, so we convert
892 * it to %if 0. This is not used in NASM
893 * compatible code, but does need to parse for the
894 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000895 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700896 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000897 } else {
898 memcpy(line + 1, p, len + 1);
899 }
900 nasm_free(oldline);
901 return line;
902 } else if (m < 0) {
903 j = k;
904 } else
905 i = k;
906 }
907 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000908 }
909 return line;
910}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000911
H. Peter Anvin76690a12002-04-30 20:52:49 +0000912/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000913 * The pre-preprocessing stage... This function translates line
914 * number indications as they emerge from GNU cpp (`# lineno "file"
915 * flags') into NASM preprocessor line number indications (`%line
916 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000917 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000918static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000919{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000920 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000921 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000922
H. Peter Anvine2c80182005-01-15 22:15:51 +0000923 if (line[0] == '#' && line[1] == ' ') {
924 oldline = line;
925 fname = oldline + 2;
926 lineno = atoi(fname);
927 fname += strspn(fname, "0123456789 ");
928 if (*fname == '"')
929 fname++;
930 fnlen = strcspn(fname, "\"");
931 line = nasm_malloc(20 + fnlen);
932 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
933 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000934 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000935 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000936 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000937 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000938}
939
940/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000941 * Free a linked list of tokens.
942 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000943static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000944{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400945 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000946 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000947}
948
949/*
950 * Free a linked list of lines.
951 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000952static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000953{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400954 Line *l, *tmp;
955 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000956 free_tlist(l->first);
957 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000958 }
959}
960
961/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700962 * Free an array of linked lists of tokens
963 */
964static void free_tlist_array(Token **array, size_t nlists)
965{
966 Token **listp = array;
967
968 while (nlists--)
969 free_tlist(*listp++);
970
971 nasm_free(array);
972}
973
974/*
975 * Duplicate a linked list of tokens.
976 */
977static Token *dup_tlist(const Token *list, Token ***tailp)
978{
979 Token *newlist = NULL;
980 Token **tailpp = &newlist;
981 const Token *t;
982
983 list_for_each(t, list) {
984 Token *nt;
985 *tailpp = nt = dup_Token(NULL, t);
986 tailpp = &nt->next;
987 }
988
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700989 if (tailp) {
990 **tailp = newlist;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700991 *tailp = tailpp;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700992 }
993
994 return newlist;
995}
996
997/*
998 * Duplicate a linked list of tokens with a maximum count
999 */
1000static Token *dup_tlistn(const Token *list, size_t cnt, Token ***tailp)
1001{
1002 Token *newlist = NULL;
1003 Token **tailpp = &newlist;
1004 const Token *t;
1005
1006 list_for_each(t, list) {
1007 Token *nt;
1008 if (!cnt--)
1009 break;
1010 *tailpp = nt = dup_Token(NULL, t);
1011 tailpp = &nt->next;
1012 }
1013
1014 if (tailp) {
1015 **tailp = newlist;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07001016 if (newlist)
1017 *tailp = tailpp;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001018 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07001019
1020 return newlist;
1021}
1022
1023/*
1024 * Duplicate a linked list of tokens in reverse order
1025 */
1026static Token *dup_tlist_reverse(const Token *list, Token *tail)
1027{
1028 const Token *t;
1029
1030 list_for_each(t, list)
1031 tail = dup_Token(tail, t);
1032
1033 return tail;
1034}
1035
1036/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001037 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +00001038 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001039static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001040{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001041 nasm_free(m->name);
1042 free_tlist(m->dlist);
1043 nasm_free(m->defaults);
1044 free_llist(m->expansion);
1045 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001046}
1047
1048/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001049 * Clear or free an SMacro
H. Peter Anvin8b262472019-02-26 14:00:54 -08001050 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001051static void free_smacro_members(SMacro *s)
H. Peter Anvin8b262472019-02-26 14:00:54 -08001052{
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07001053 if (s->params) {
1054 int i;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001055 for (i = 0; i < s->nparam; i++) {
1056 if (s->params[i].name.len > INLINE_TEXT)
1057 nasm_free(s->params[i].name.text.p.ptr);
1058 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07001059 nasm_free(s->params);
1060 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08001061 nasm_free(s->name);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001062 free_tlist(s->expansion);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001063}
1064
1065static void clear_smacro(SMacro *s)
1066{
1067 free_smacro_members(s);
1068 /* Wipe everything except the next pointer */
1069 memset(&s->next + 1, 0, sizeof *s - sizeof s->next);
1070}
1071
1072/*
1073 * Free an SMacro
1074 */
1075static void free_smacro(SMacro *s)
1076{
1077 free_smacro_members(s);
1078 nasm_free(s);
H. Peter Anvin8b262472019-02-26 14:00:54 -08001079}
1080
1081/*
H. Peter Anvin97a23472007-09-16 17:57:25 -07001082 * Free all currently defined macros, and free the hash tables
1083 */
H. Peter Anvin072771e2008-05-22 13:17:51 -07001084static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -07001085{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001086 struct hash_iterator it;
1087 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001088
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001089 hash_for_each(smt, it, np) {
1090 SMacro *tmp;
1091 SMacro *s = np->data;
1092 nasm_free((void *)np->key);
H. Peter Anvin8b262472019-02-26 14:00:54 -08001093 list_for_each_safe(s, tmp, s)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001094 free_smacro(s);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001095 }
H. Peter Anvin072771e2008-05-22 13:17:51 -07001096 hash_free(smt);
1097}
1098
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001099static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -07001100{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001101 struct hash_iterator it;
1102 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001103
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001104 hash_for_each(mmt, it, np) {
1105 MMacro *tmp;
1106 MMacro *m = np->data;
1107 nasm_free((void *)np->key);
1108 list_for_each_safe(m, tmp, m)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001109 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001110 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001111 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -07001112}
1113
1114static void free_macros(void)
1115{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001116 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001117 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001118}
1119
1120/*
1121 * Initialize the hash tables
1122 */
1123static void init_macros(void)
1124{
H. Peter Anvin97a23472007-09-16 17:57:25 -07001125}
1126
1127/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001128 * Pop the context stack.
1129 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001130static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001131{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001132 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001133
1134 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001135 free_smacro_table(&c->localmac);
H. Peter Anvin8571f062019-09-23 16:40:03 -07001136 nasm_free((char *)c->name);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001137 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001138}
1139
H. Peter Anvin072771e2008-05-22 13:17:51 -07001140/*
1141 * Search for a key in the hash index; adding it if necessary
1142 * (in which case we initialize the data pointer to NULL.)
1143 */
1144static void **
1145hash_findi_add(struct hash_table *hash, const char *str)
1146{
1147 struct hash_insert hi;
1148 void **r;
1149 char *strx;
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001150 size_t l = strlen(str) + 1;
H. Peter Anvin072771e2008-05-22 13:17:51 -07001151
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001152 r = hash_findib(hash, str, l, &hi);
H. Peter Anvin072771e2008-05-22 13:17:51 -07001153 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001154 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -07001155
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001156 strx = nasm_malloc(l); /* Use a more efficient allocator here? */
1157 memcpy(strx, str, l);
H. Peter Anvin072771e2008-05-22 13:17:51 -07001158 return hash_add(&hi, strx, NULL);
1159}
1160
1161/*
1162 * Like hash_findi, but returns the data element rather than a pointer
1163 * to it. Used only when not adding a new element, hence no third
1164 * argument.
1165 */
1166static void *
1167hash_findix(struct hash_table *hash, const char *str)
1168{
1169 void **p;
1170
1171 p = hash_findi(hash, str, NULL);
1172 return p ? *p : NULL;
1173}
1174
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001175/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001176 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001177 * if there no more left -- return NULL
1178 */
1179static char *line_from_stdmac(void)
1180{
1181 unsigned char c;
1182 const unsigned char *p = stdmacpos;
1183 char *line, *q;
1184 size_t len = 0;
1185
1186 if (!stdmacpos)
1187 return NULL;
1188
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -07001189 /*
1190 * 32-126 is ASCII, 127 is end of line, 128-31 are directives
1191 * (allowed to wrap around) corresponding to PP_* tokens 0-159.
1192 */
1193 while ((c = *p++) != 127) {
1194 uint8_t ndir = c - 128;
1195 if (ndir < 256-96)
1196 len += pp_directives_len[ndir] + 1;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001197 else
1198 len++;
1199 }
1200
1201 line = nasm_malloc(len + 1);
1202 q = line;
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -07001203
1204 while ((c = *stdmacpos++) != 127) {
1205 uint8_t ndir = c - 128;
1206 if (ndir < 256-96) {
1207 memcpy(q, pp_directives[ndir], pp_directives_len[ndir]);
1208 q += pp_directives_len[ndir];
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001209 *q++ = ' ';
1210 } else {
1211 *q++ = c;
1212 }
1213 }
1214 stdmacpos = p;
1215 *q = '\0';
1216
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -07001217 if (*stdmacpos == 127) {
H. Peter Anvinf7606612016-07-13 14:23:48 -07001218 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001219 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -07001220 if (*stdmacnext) {
1221 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001222 } else if (do_predef) {
1223 Line *pd, *l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001224
1225 /*
1226 * Nasty hack: here we push the contents of
1227 * `predef' on to the top-level expansion stack,
1228 * since this is the most convenient way to
1229 * implement the pre-include and pre-define
1230 * features.
1231 */
1232 list_for_each(pd, predef) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07001233 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001234 l->next = istk->expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07001235 l->first = dup_tlist(pd->first, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001236 l->finishes = NULL;
1237
1238 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001239 }
1240 do_predef = false;
1241 }
1242 }
1243
1244 return line;
1245}
1246
H. Peter Anvin6686de22019-08-10 05:33:14 -07001247/*
1248 * Read a line from a file. Return NULL on end of file.
1249 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001250static char *line_from_file(FILE *f)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001251{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001252 int c;
1253 unsigned int size, next;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001254 const unsigned int delta = 512;
1255 const unsigned int pad = 8;
1256 unsigned int nr_cont = 0;
1257 bool cont = false;
1258 char *buffer, *p;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07001259 int32_t lineno;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001260
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001261 size = delta;
1262 p = buffer = nasm_malloc(size);
1263
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001264 do {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001265 c = fgetc(f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001266
1267 switch (c) {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001268 case EOF:
1269 if (p == buffer) {
1270 nasm_free(buffer);
1271 return NULL;
1272 }
1273 c = 0;
1274 break;
1275
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001276 case '\r':
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001277 next = fgetc(f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001278 if (next != '\n')
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001279 ungetc(next, f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001280 if (cont) {
1281 cont = false;
1282 continue;
1283 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001284 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001285 break;
1286
1287 case '\n':
1288 if (cont) {
1289 cont = false;
1290 continue;
1291 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001292 c = 0;
1293 break;
1294
1295 case 032: /* ^Z = legacy MS-DOS end of file mark */
1296 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001297 break;
1298
1299 case '\\':
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001300 next = fgetc(f);
1301 ungetc(next, f);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +04001302 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001303 cont = true;
1304 nr_cont++;
1305 continue;
1306 }
1307 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001308 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001309
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001310 if (p >= (buffer + size - pad)) {
1311 buffer = nasm_realloc(buffer, size + delta);
1312 p = buffer + size - pad;
1313 size += delta;
1314 }
1315
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001316 *p++ = c;
1317 } while (c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001318
H. Peter Anvinab6f8312019-08-09 22:31:45 -07001319 lineno = src_get_linnum() + istk->lineinc +
1320 (nr_cont * istk->lineinc);
1321 src_set_linnum(lineno);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001322
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001323 return buffer;
1324}
1325
1326/*
H. Peter Anvin6686de22019-08-10 05:33:14 -07001327 * Common read routine regardless of source
1328 */
1329static char *read_line(void)
1330{
1331 char *line;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001332 FILE *f = istk->fp;
H. Peter Anvin6686de22019-08-10 05:33:14 -07001333
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001334 if (f)
1335 line = line_from_file(f);
H. Peter Anvin6686de22019-08-10 05:33:14 -07001336 else
1337 line = line_from_stdmac();
1338
1339 if (!line)
1340 return NULL;
1341
1342 if (!istk->nolist)
1343 lfmt->line(LIST_READ, src_get_linnum(), line);
1344
1345 return line;
1346}
1347
1348/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00001349 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001350 * don't need to parse the value out of e.g. numeric tokens: we
1351 * simply split one string into many.
1352 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07001353static Token *tokenize(const char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001354{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001355 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001356 Token *list = NULL;
1357 Token *t, **tail = &list;
1358
H. Peter Anvine2c80182005-01-15 22:15:51 +00001359 while (*line) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001360 const char *p = line;
1361 const char *ep = NULL; /* End of token, for trimming the end */
1362 size_t toklen;
1363 char firstchar = *p; /* Can be used to override the first char */
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001364
H. Peter Anvine2c80182005-01-15 22:15:51 +00001365 if (*p == '%') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001366 /*
1367 * Preprocessor construct; find the end of the token.
1368 * Classification is handled later, because %{...} can be
1369 * used to create any preprocessor token.
1370 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001371 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001372 if (*p == '+' && !nasm_isdigit(p[1])) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001373 /* Paste token */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001374 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001375 } else if (nasm_isdigit(*p) ||
1376 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001377 do {
1378 p++;
1379 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001380 while (nasm_isdigit(*p));
H. Peter Anvin8571f062019-09-23 16:40:03 -07001381 } else if (*p == '{' || *p == '[') {
1382 /* %{...} or %[...] */
1383 char firstchar = *p;
1384 char endchar = *p + 2; /* } or ] */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001385 int lvl = 1;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001386 line += (*p++ == '{'); /* Skip { but not [ (yet) */
1387 while (lvl) {
1388 if (*p == firstchar) {
1389 lvl++;
1390 } else if (*p == endchar) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001391 lvl--;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001392 } else if (nasm_isquote(*p)) {
1393 p = nasm_skip_string(p);
1394 }
1395
1396 /*
1397 * *p can have been advanced to a null character by
1398 * nasm_skip_string()
1399 */
1400 if (!*p) {
1401 nasm_warn(WARN_OTHER, "unterminated %%%c construct",
1402 firstchar);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001403 break;
1404 }
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001405 p++;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001406 }
1407 ep = lvl ? p : p-1; /* Terminal character not part of token */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001408 } else if (*p == '?') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001409 /* %? or %?? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001410 p++;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001411 if (*p == '?')
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001412 p++;
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001413 } else if (*p == '!') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001414 /* Environment variable reference */
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001415 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001416 if (nasm_isidchar(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001417 do {
1418 p++;
1419 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001420 while (nasm_isidchar(*p));
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001421 } else if (nasm_isquote(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001422 p = nasm_skip_string(p);
1423 if (*p)
1424 p++;
1425 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001426 nasm_nonfatalf(ERR_PASS1, "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001427 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001428 /* %! without anything else... */
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001429 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07001430 } else if (*p == ',') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001431 /* Conditional comma */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07001432 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001433 } else if (nasm_isidchar(*p) ||
H. Peter Anvin8571f062019-09-23 16:40:03 -07001434 ((*p == '%' || *p == '$') && nasm_isidchar(p[1]))) {
1435 /* Identifier or some sort */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001436 do {
1437 p++;
1438 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001439 while (nasm_isidchar(*p));
H. Peter Anvin8571f062019-09-23 16:40:03 -07001440 } else if (*p == '%') {
1441 /* %% operator */
1442 p++;
1443 }
1444
1445 if (!ep)
1446 ep = p;
1447 toklen = ep - line;
1448
1449 /* Classify here, to handle %{...} correctly */
1450 if (toklen < 2) {
1451 type = TOK_OTHER; /* % operator */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001452 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001453 char c0 = line[1];
1454
1455 switch (c0) {
1456 case '+':
1457 type = (toklen == 2) ? TOK_PASTE : TOK_MMACRO_PARAM;
1458 break;
1459
1460 case '-':
1461 type = TOK_MMACRO_PARAM;
1462 break;
1463
1464 case '?':
1465 if (toklen == 2)
1466 type = TOK_PREPROC_Q;
1467 else if (toklen == 3 && line[2] == '?')
1468 type = TOK_PREPROC_QQ;
1469 else
1470 type = TOK_PREPROC_ID;
1471 break;
1472
1473 case '!':
1474 type = (toklen == 2) ? TOK_OTHER : TOK_ENVIRON;
1475 break;
1476
1477 case '%':
1478 type = (toklen == 2) ? TOK_OTHER : TOK_LOCAL_SYMBOL;
1479 break;
1480
1481 case '$':
1482 type = (toklen == 2) ? TOK_OTHER : TOK_LOCAL_MACRO;
1483 break;
1484
1485 case '[':
1486 line += 2; /* Skip %[ */
1487 firstchar = *line; /* Don't clobber */
1488 toklen -= 2;
1489 type = TOK_INDIRECT;
1490 break;
1491
1492 case ',':
1493 type = (toklen == 2) ? TOK_COND_COMMA : TOK_PREPROC_ID;
1494 break;
1495
1496 case '\'':
1497 case '\"':
1498 case '`':
1499 /* %{'string'} */
1500 type = TOK_PREPROC_ID;
1501 break;
1502
1503 case ':':
1504 type = TOK_MMACRO_PARAM; /* %{:..} */
1505 break;
1506
1507 default:
1508 if (nasm_isdigit(c0))
1509 type = TOK_MMACRO_PARAM;
1510 else if (nasm_isidchar(c0) || toklen > 2)
1511 type = TOK_PREPROC_ID;
1512 else
1513 type = TOK_OTHER;
1514 break;
1515 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001516 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001517 } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001518 /*
1519 * An identifier. This includes the ? operator, which is
1520 * treated as a keyword, not as a special character
1521 * operator
1522 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001523 type = TOK_ID;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001524 while (nasm_isidchar(*++p))
1525 ;
1526 } else if (nasm_isquote(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001527 /*
1528 * A string token.
1529 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001530 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001531 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001532
H. Peter Anvine2c80182005-01-15 22:15:51 +00001533 if (*p) {
1534 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001535 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001536 nasm_warn(WARN_OTHER, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001537 /* Handling unterminated strings by UNV */
1538 /* type = -1; */
1539 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001540 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001541 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001542 p += 2;
H. Peter Anvin13506202018-11-28 14:55:58 -08001543 } else if (nasm_isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001544 bool is_hex = false;
1545 bool is_float = false;
1546 bool has_e = false;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001547 char c;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001548
H. Peter Anvine2c80182005-01-15 22:15:51 +00001549 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001550 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001551 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001552
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001553 if (*p == '$') {
1554 p++;
1555 is_hex = true;
1556 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001557
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001558 for (;;) {
1559 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001560
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001561 if (!is_hex && (c == 'e' || c == 'E')) {
1562 has_e = true;
1563 if (*p == '+' || *p == '-') {
1564 /*
1565 * e can only be followed by +/- if it is either a
1566 * prefixed hex number or a floating-point number
1567 */
1568 p++;
1569 is_float = true;
1570 }
1571 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1572 is_hex = true;
1573 } else if (c == 'P' || c == 'p') {
1574 is_float = true;
1575 if (*p == '+' || *p == '-')
1576 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001577 } else if (nasm_isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001578 ; /* just advance */
1579 else if (c == '.') {
1580 /*
1581 * we need to deal with consequences of the legacy
1582 * parser, like "1.nolist" being two tokens
1583 * (TOK_NUMBER, TOK_ID) here; at least give it
1584 * a shot for now. In the future, we probably need
1585 * a flex-based scanner with proper pattern matching
1586 * to do it as well as it can be done. Nothing in
1587 * the world is going to help the person who wants
1588 * 0x123.p16 interpreted as two tokens, though.
1589 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07001590 const char *r = p;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001591 while (*r == '_')
1592 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001593
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001594 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1595 (!is_hex && (*r == 'e' || *r == 'E')) ||
1596 (*r == 'p' || *r == 'P')) {
1597 p = r;
1598 is_float = true;
1599 } else
1600 break; /* Terminate the token */
1601 } else
1602 break;
1603 }
1604 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001605
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001606 if (p == line+1 && *line == '$') {
1607 type = TOK_OTHER; /* TOKEN_HERE */
1608 } else {
1609 if (has_e && !is_hex) {
1610 /* 1e13 is floating-point, but 1e13h is not */
1611 is_float = true;
1612 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001613
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001614 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1615 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001616 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001617 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001618 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001619 /*
1620 * Whitespace just before end-of-line is discarded by
1621 * pretending it's a comment; whitespace just before a
1622 * comment gets lumped into the comment.
1623 */
1624 if (!*p || *p == ';') {
1625 type = TOK_COMMENT;
1626 while (*p)
1627 p++;
1628 }
1629 } else if (*p == ';') {
1630 type = TOK_COMMENT;
1631 while (*p)
1632 p++;
1633 } else {
1634 /*
1635 * Anything else is an operator of some kind. We check
1636 * for all the double-character operators (>>, <<, //,
H. Peter Anvin8571f062019-09-23 16:40:03 -07001637 * %%, <=, >=, ==, !=, <>, &&, ||, ^^) and the triple-
1638 * character operators (<<<, >>>, <=>) but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001639 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001640 */
1641 type = TOK_OTHER;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001642 switch (*p++) {
1643 case '>':
1644 if (*p == '>') {
1645 p++;
1646 if (*p == '>')
1647 p++;
H. Peter Anvind03a6c82019-10-07 21:29:05 -07001648 } else if (*p == '=') {
1649 p++;
1650 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07001651 break;
1652
1653 case '<':
1654 if (*p == '<') {
1655 p++;
1656 if (*p == '<')
1657 p++;
1658 } else if (*p == '=') {
1659 p++;
1660 if (*p == '>')
1661 p++;
1662 } else if (*p == '>') {
1663 p++;
1664 }
1665 break;
1666
1667 case '!':
1668 if (*p == '=')
1669 p++;
1670 break;
1671
1672 case '/':
1673 case '=':
1674 case '&':
1675 case '|':
1676 case '^':
1677 /* These operators can be doubled but nothing else */
1678 if (*p == p[-1])
1679 p++;
1680 break;
1681
1682 default:
1683 break;
1684 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001685 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001686
H. Peter Anvin8571f062019-09-23 16:40:03 -07001687 if (type == TOK_WHITESPACE) {
1688 *tail = t = new_White(NULL);
1689 tail = &t->next;
1690 } else if (type != TOK_COMMENT) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001691 if (!ep)
1692 ep = p;
1693 *tail = t = new_Token(NULL, type, line, ep - line);
H. Peter Anvin8571f062019-09-23 16:40:03 -07001694 *tok_text_buf(t) = firstchar; /* E.g. %{foo} -> {foo -> %foo */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001695 tail = &t->next;
1696 }
1697 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001698 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001699 return list;
1700}
1701
H. Peter Anvin8571f062019-09-23 16:40:03 -07001702/*
1703 * Tokens are allocated in blocks to improve speed. Set the blocksize
1704 * to 0 to use regular nasm_malloc(); this is useful for debugging.
1705 *
1706 * alloc_Token() returns a zero-initialized token structure.
1707 */
1708#define TOKEN_BLOCKSIZE 4096
1709
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001710#if TOKEN_BLOCKSIZE
H. Peter Anvin8571f062019-09-23 16:40:03 -07001711
1712static Token *freeTokens = NULL;
1713static Token *tokenblocks = NULL;
1714
1715static Token *alloc_Token(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001716{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001717 Token *t = freeTokens;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001718
H. Peter Anvin8571f062019-09-23 16:40:03 -07001719 if (unlikely(!t)) {
1720 Token *block;
1721 size_t i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001722
H. Peter Anvin8571f062019-09-23 16:40:03 -07001723 nasm_newn(block, TOKEN_BLOCKSIZE);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001724
H. Peter Anvin8571f062019-09-23 16:40:03 -07001725 /*
1726 * The first entry in each array are a linked list of
1727 * block allocations and is not used for data.
1728 */
1729 block[0].next = tokenblocks;
1730 block[0].type = TOK_BLOCK;
1731 tokenblocks = block;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001732
H. Peter Anvin8571f062019-09-23 16:40:03 -07001733 /*
1734 * Add the rest to the free list
1735 */
1736 for (i = 2; i < TOKEN_BLOCKSIZE - 1; i++)
1737 block[i].next = &block[i+1];
1738
1739 freeTokens = &block[2];
1740
1741 /*
1742 * Return the topmost usable token
1743 */
1744 return &block[1];
1745 }
1746
1747 freeTokens = t->next;
1748 t->next = NULL;
1749 return t;
H. Peter Anvince616072002-04-30 21:02:23 +00001750}
1751
H. Peter Anvin8571f062019-09-23 16:40:03 -07001752static Token *delete_Token(Token *t)
1753{
1754 Token *next = t->next;
1755
1756 nasm_zero(*t);
1757 t->next = freeTokens;
1758 freeTokens = t;
1759
1760 return next;
1761}
1762
H. Peter Anvine2c80182005-01-15 22:15:51 +00001763static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001764{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001765 Token *block, *blocktmp;
H. Peter Anvince616072002-04-30 21:02:23 +00001766
H. Peter Anvin8571f062019-09-23 16:40:03 -07001767 list_for_each_safe(block, blocktmp, tokenblocks)
1768 nasm_free(block);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001769
H. Peter Anvin8571f062019-09-23 16:40:03 -07001770 freeTokens = tokenblocks = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001771}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001772
H. Peter Anvin8571f062019-09-23 16:40:03 -07001773#else
1774
1775static inline Token *alloc_Token(void)
1776{
1777 Token *t;
1778 nasm_new(*t);
1779 return t;
1780}
1781
1782static Token *delete_Token(Token *t)
1783{
1784 Token *next = t->next;
1785 nasm_free(t);
1786 return next;
1787}
1788
1789static inline void delete_Blocks(void)
1790{
1791 /* Nothing to do */
1792}
1793
1794#endif
1795
H. Peter Anvin734b1882002-04-30 21:01:08 +00001796/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001797 * this function creates a new Token and passes a pointer to it
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001798 * back to the caller. It sets the type, text, and next pointer elements.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001799 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001800static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001801 const char *text, size_t txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001802{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001803 Token *t = alloc_Token();
1804 char *textp;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001805
H. Peter Anvin734b1882002-04-30 21:01:08 +00001806 t->next = next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001807 t->type = type;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001808 if (type == TOK_WHITESPACE) {
1809 t->len = 1;
1810 t->text.a[0] = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001811 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001812 if (text && text[0] && !txtlen)
1813 txtlen = tok_strlen(text);
1814
1815 t->len = tok_check_len(txtlen);
1816
1817 if (text) {
1818 textp = (txtlen > INLINE_TEXT)
1819 ? (t->text.p.ptr = nasm_malloc(txtlen+1)) : t->text.a;
1820 memcpy(textp, text, txtlen);
1821 textp[txtlen] = '\0'; /* In case we needed malloc() */
1822 } else {
1823 /*
1824 * Allocate a buffer but do not fill it. The caller
1825 * can fill in text, but must not change the length.
1826 * The filled in text must be exactly txtlen once
1827 * the buffer is filled and before the token is added
1828 * to any line lists.
1829 */
1830 if (txtlen > INLINE_TEXT)
1831 t->text.p.ptr = nasm_zalloc(txtlen+1);
1832 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001833 }
1834 return t;
1835}
1836
H. Peter Anvin8571f062019-09-23 16:40:03 -07001837/*
1838 * Same as new_Token(), but text belongs to the new token and is
1839 * either taken over or freed. This function MUST be called
1840 * with valid txt and txtlen, unlike new_Token().
1841 */
1842static Token *new_Token_free(Token * next, enum pp_token_type type,
1843 char *text, size_t txtlen)
1844{
1845 Token *t = alloc_Token();
1846
1847 t->next = next;
1848 t->type = type;
1849 t->len = tok_check_len(txtlen);
1850
1851 if (txtlen <= INLINE_TEXT) {
1852 memcpy(t->text.a, text, txtlen);
1853 free(text);
1854 } else {
1855 t->text.p.ptr = text;
1856 }
1857
1858 return t;
1859}
1860
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001861static Token *dup_Token(Token *next, const Token *src)
1862{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001863 Token *t = alloc_Token();
1864
1865 memcpy(t, src, sizeof *src);
1866 t->next = next;
1867
1868 if (t->len > INLINE_TEXT) {
1869 t->text.p.ptr = nasm_malloc(t->len + 1);
1870 memcpy(t->text.p.ptr, src->text.p.ptr, t->len+1);
1871 }
1872
1873 return t;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001874}
1875
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001876static Token *new_White(Token *next)
1877{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001878 Token *t = alloc_Token();
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001879
H. Peter Anvin8571f062019-09-23 16:40:03 -07001880 t->next = next;
1881 t->type = TOK_WHITESPACE;
1882 t->len = 1;
1883 t->text.a[0] = ' ';
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001884
H. Peter Anvin8571f062019-09-23 16:40:03 -07001885 return t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001886}
1887
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001888/*
H. Peter Anvin8571f062019-09-23 16:40:03 -07001889 * This *transfers* the content from one token to another, leaving the
1890 * next pointer of the latter intact. Unlike dup_Token(), the old
1891 * token is destroyed, except for its next pointer, and the text
1892 * pointer allocation, if any, is simply transferred.
1893 */
1894static Token *steal_Token(Token *dst, Token *src)
1895{
1896 /* Overwrite everything except the next pointers */
1897 memcpy((char *)dst + sizeof(Token *), (char *)src + sizeof(Token *),
1898 sizeof(Token) - sizeof(Token *));
1899
1900 /* Clear the donor token */
1901 memset((char *)src + sizeof(Token *), 0, sizeof(Token) - sizeof(Token *));
1902
1903 return dst;
1904}
1905
1906/*
1907 * Convert a line of tokens back into text. This modifies the list
1908 * by expanding environment variables.
1909 *
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001910 * If expand_locals is not zero, identifiers of the form "%$*xxx"
H. Peter Anvin8571f062019-09-23 16:40:03 -07001911 * are also transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001912 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001913static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001914{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001915 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001916 char *line, *p;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001917 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001918
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001919 list_for_each(t, tlist) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001920 switch (t->type) {
1921 case TOK_ENVIRON:
1922 {
1923 const char *v = pp_getenv(t, true);
1924 set_text(t, v, tok_strlen(v));
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07001925 t->type = TOK_NAKED_STRING;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001926 break;
1927 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001928
H. Peter Anvin8571f062019-09-23 16:40:03 -07001929 case TOK_LOCAL_MACRO:
1930 case TOK_LOCAL_SYMBOL:
1931 if (expand_locals) {
1932 const char *q;
1933 char *p;
1934 Context *ctx = get_ctx(tok_text(t), &q);
1935 if (ctx) {
1936 p = nasm_asprintf("..@%"PRIu64".%s", ctx->number, q);
1937 set_text_free(t, p, nasm_last_string_len());
1938 t->type = TOK_ID;
1939 }
1940 }
1941 break;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001942
H. Peter Anvin8571f062019-09-23 16:40:03 -07001943 default:
1944 break; /* No modifications */
1945 }
1946
1947 if (debug_level(2)) {
1948 unsigned int t_len = t->len;
1949 unsigned int s_len = tok_strlen(tok_text(t));
1950 if (t_len != s_len) {
1951 nasm_panic("assertion failed: token \"%s\" type %u len %u has t->len %u\n",
1952 tok_text(t), t->type, s_len, t_len);
1953 t->len = s_len;
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001954 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001955 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001956
H. Peter Anvin8571f062019-09-23 16:40:03 -07001957 len += t->len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001958 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001959
H. Peter Anvin734b1882002-04-30 21:01:08 +00001960 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001961
H. Peter Anvin8571f062019-09-23 16:40:03 -07001962 list_for_each(t, tlist)
1963 p = mempcpy(p, tok_text(t), t->len);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001964 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001965
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001966 return line;
1967}
1968
1969/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001970 * A scanner, suitable for use by the expression evaluator, which
1971 * operates on a line of Tokens. Expects a pointer to a pointer to
1972 * the first token in the line to be passed in as its private_data
1973 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001974 *
1975 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001976 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08001977struct ppscan {
1978 Token *tptr;
1979 int ntokens;
1980};
1981
H. Peter Anvine2c80182005-01-15 22:15:51 +00001982static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001983{
H. Peter Anvin8b262472019-02-26 14:00:54 -08001984 struct ppscan *pps = private_data;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001985 Token *tline;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001986 const char *txt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001987
H. Peter Anvine2c80182005-01-15 22:15:51 +00001988 do {
H. Peter Anvin8b262472019-02-26 14:00:54 -08001989 if (pps->ntokens && (tline = pps->tptr)) {
1990 pps->ntokens--;
1991 pps->tptr = tline->next;
1992 } else {
1993 pps->tptr = NULL;
1994 pps->ntokens = 0;
1995 return tokval->t_type = TOKEN_EOS;
1996 }
1997 } while (tline->type == TOK_WHITESPACE || tline->type == TOK_COMMENT);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001998
H. Peter Anvin8571f062019-09-23 16:40:03 -07001999 txt = tok_text(tline);
2000 tokval->t_charptr = (char *)txt; /* Fix this */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07002001
H. Peter Anvin8571f062019-09-23 16:40:03 -07002002 if (txt[0] == '$') {
2003 if (!txt[1]) {
2004 return tokval->t_type = TOKEN_HERE;
2005 } else if (txt[1] == '$' && !txt[2]) {
2006 return tokval->t_type = TOKEN_BASE;
2007 } else if (tline->type == TOK_ID) {
2008 tokval->t_charptr++;
2009 return tokval->t_type = TOKEN_ID;
2010 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002011 }
2012
H. Peter Anvin8571f062019-09-23 16:40:03 -07002013 switch (tline->type) {
2014 default:
2015 if (tline->len == 1)
2016 return tokval->t_type = txt[0];
2017 /* fall through */
2018 case TOK_ID:
2019 return nasm_token_hash(txt, tokval);
2020
2021 case TOK_NUMBER:
2022 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002023 bool rn_error;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002024 tokval->t_integer = readnum(txt, &rn_error);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002025 if (rn_error)
2026 return tokval->t_type = TOKEN_ERRNUM;
2027 else
2028 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07002029 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002030
H. Peter Anvin8571f062019-09-23 16:40:03 -07002031 case TOK_FLOAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002032 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002033
2034 case TOK_STRING:
2035 tokval->t_charptr = (char *)unquote_token(tline);
2036 tokval->t_inttwo = tline->len;
2037 return tokval->t_type = TOKEN_STR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002038 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002039}
2040
2041/*
H. Peter Anvind2354082019-08-27 16:38:48 -07002042 * 1. An expression (true if nonzero 0)
2043 * 2. The keywords true, on, yes for true
2044 * 3. The keywords false, off, no for false
2045 * 4. An empty line, for true
2046 *
2047 * On error, return defval (usually the previous value)
2048 */
2049static bool pp_get_boolean_option(Token *tline, bool defval)
2050{
2051 static const char * const noyes[] = {
2052 "no", "yes",
2053 "false", "true",
2054 "off", "on"
2055 };
2056 struct ppscan pps;
2057 struct tokenval tokval;
2058 expr *evalresult;
2059
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002060 tline = skip_white(tline);
2061 if (!tline)
H. Peter Anvind2354082019-08-27 16:38:48 -07002062 return true;
2063
2064 if (tline->type == TOK_ID) {
2065 size_t i;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002066 const char *txt = tok_text(tline);
2067
H. Peter Anvind2354082019-08-27 16:38:48 -07002068 for (i = 0; i < ARRAY_SIZE(noyes); i++)
H. Peter Anvin8571f062019-09-23 16:40:03 -07002069 if (!nasm_stricmp(txt, noyes[i]))
H. Peter Anvind2354082019-08-27 16:38:48 -07002070 return i & 1;
2071 }
2072
2073 pps.tptr = NULL;
2074 pps.tptr = tline;
2075 pps.ntokens = -1;
2076 tokval.t_type = TOKEN_INVALID;
2077 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
2078
2079 if (!evalresult)
2080 return true;
2081
2082 if (tokval.t_type)
2083 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
2084 if (!is_really_simple(evalresult)) {
2085 nasm_nonfatal("boolean flag expression must be a constant");
2086 return defval;
2087 }
2088
2089 return reloc_value(evalresult) != 0;
2090}
2091
2092/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002093 * Compare a string to the name of an existing macro; this is a
2094 * simple wrapper which calls either strcmp or nasm_stricmp
2095 * depending on the value of the `casesense' parameter.
2096 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002097static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002098{
H. Peter Anvin734b1882002-04-30 21:01:08 +00002099 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002100}
2101
2102/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07002103 * Compare a string to the name of an existing macro; this is a
2104 * simple wrapper which calls either strcmp or nasm_stricmp
2105 * depending on the value of the `casesense' parameter.
2106 */
2107static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
2108{
2109 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
2110}
2111
2112/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002113 * Return the Context structure associated with a %$ token. Return
2114 * NULL, having _already_ reported an error condition, if the
2115 * context stack isn't deep enough for the supplied number of $
2116 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002117 *
2118 * If "namep" is non-NULL, set it to the pointer to the macro name
2119 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002120 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04002121static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002122{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002123 Context *ctx;
2124 int i;
2125
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002126 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002127 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002128
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002129 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00002130 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002131
H. Peter Anvine2c80182005-01-15 22:15:51 +00002132 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002133 nasm_nonfatal("`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002134 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002135 }
2136
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002137 name += 2;
2138 ctx = cstk;
2139 i = 0;
2140 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002141 name++;
2142 i++;
2143 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002144 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002145 if (!ctx) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002146 nasm_nonfatal("`%s': context stack is only"
2147 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002148 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002149 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002150
2151 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002152 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002153
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04002154 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002155}
2156
2157/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002158 * Open an include file. This routine must always return a valid
2159 * file pointer if it returns - it's responsible for throwing an
2160 * ERR_FATAL and bombing out completely if not. It should also try
2161 * the include path one by one until it finds the file or reaches
2162 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07002163 *
2164 * Note: for INC_PROBE the function returns NULL at all times;
2165 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002166 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07002167enum incopen_mode {
2168 INC_NEEDED, /* File must exist */
2169 INC_OPTIONAL, /* Missing is OK */
2170 INC_PROBE /* Only an existence probe */
2171};
2172
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002173/* This is conducts a full pathname search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002174static FILE *inc_fopen_search(const char *file, char **slpath,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002175 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002176{
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -08002177 const struct strlist_entry *ip = strlist_head(ipath_list);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002178 FILE *fp;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002179 const char *prefix = "";
night199ukfdb1a1b2018-10-18 23:19:47 +02002180 char *sp;
H. Peter Anvind81a2352016-09-21 14:03:18 -07002181 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002182
H. Peter Anvine2c80182005-01-15 22:15:51 +00002183 while (1) {
night199ukfdb1a1b2018-10-18 23:19:47 +02002184 sp = nasm_catfile(prefix, file);
H. Peter Anvind81a2352016-09-21 14:03:18 -07002185 if (omode == INC_PROBE) {
2186 fp = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002187 found = nasm_file_exists(sp);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07002188 } else {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002189 fp = nasm_open_read(sp, fmode);
H. Peter Anvind81a2352016-09-21 14:03:18 -07002190 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002191 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07002192 if (found) {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002193 *slpath = sp;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002194 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002195 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002196
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002197 nasm_free(sp);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002198
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002199 if (!ip) {
2200 *slpath = NULL;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002201 return NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002202 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002203
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002204 prefix = ip->str;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002205 ip = ip->next;
2206 }
2207}
2208
2209/*
2210 * Open a file, or test for the presence of one (depending on omode),
2211 * considering the include path.
2212 */
2213static FILE *inc_fopen(const char *file,
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002214 struct strlist *dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002215 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002216 enum incopen_mode omode,
2217 enum file_flags fmode)
2218{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002219 struct hash_insert hi;
2220 void **hp;
2221 char *path;
2222 FILE *fp = NULL;
2223
2224 hp = hash_find(&FileHash, file, &hi);
2225 if (hp) {
2226 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03002227 if (path || omode != INC_NEEDED) {
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002228 strlist_add(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03002229 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002230 } else {
2231 /* Need to do the actual path search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002232 fp = inc_fopen_search(file, &path, omode, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002233
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002234 /* Positive or negative result */
2235 hash_add(&hi, nasm_strdup(file), path);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002236
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002237 /*
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002238 * Add file to dependency path.
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002239 */
2240 if (path || omode != INC_NEEDED)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002241 strlist_add(dhead, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00002242 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002243
H. Peter Anvin (Intel)5d68f982020-06-01 12:32:35 -07002244 if (path && !fp && omode != INC_PROBE)
2245 fp = nasm_open_read(path, fmode);
2246
2247 if (omode == INC_NEEDED && !fp) {
2248 if (!path)
2249 errno = ENOENT;
2250
2251 nasm_nonfatal("unable to open include file `%s': %s",
2252 file, strerror(errno));
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002253 }
2254
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002255 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002256 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002257
2258 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002259}
2260
2261/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07002262 * Opens an include or input file. Public version, for use by modules
2263 * that get a file:lineno pair and need to look at the file again
2264 * (e.g. the CodeView debug backend). Returns NULL on failure.
2265 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07002266FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07002267{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002268 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07002269}
2270
2271/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002272 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002273 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002274 * return true if _any_ single-line macro of that name is defined.
2275 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002276 * `nparam' or no parameters is defined.
2277 *
2278 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002279 * defined, or nparam is -1, the address of the definition structure
2280 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002281 * is NULL, no action will be taken regarding its contents, and no
2282 * error will occur.
2283 *
2284 * Note that this is also called with nparam zero to resolve
2285 * `ifdef'.
2286 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002287static bool
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002288smacro_defined(Context *ctx, const char *name, int nparam, SMacro **defn,
H. Peter Anvind2354082019-08-27 16:38:48 -07002289 bool nocase, bool find_alias)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002290{
H. Peter Anvin166c2472008-05-28 12:28:58 -07002291 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002292 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002293
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002294 smtbl = ctx ? &ctx->localmac : &smacros;
H. Peter Anvind2354082019-08-27 16:38:48 -07002295
2296restart:
H. Peter Anvin166c2472008-05-28 12:28:58 -07002297 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002298
H. Peter Anvine2c80182005-01-15 22:15:51 +00002299 while (m) {
2300 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002301 (nparam <= 0 || m->nparam == 0 || nparam == m->nparam ||
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002302 (m->greedy && nparam >= m->nparam-1))) {
H. Peter Anvind2354082019-08-27 16:38:48 -07002303 if (m->alias && !find_alias) {
2304 if (do_aliases) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002305 name = tok_text(m->expansion);
H. Peter Anvind2354082019-08-27 16:38:48 -07002306 goto restart;
2307 } else {
2308 continue;
2309 }
2310 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002311 if (defn) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002312 *defn = (nparam == m->nparam || nparam == -1) ? m : NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002313 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002314 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002315 }
2316 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002317 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002318
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002319 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002320}
2321
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002322/* param should be a natural number [0; INT_MAX] */
2323static int read_param_count(const char *str)
2324{
2325 int result;
2326 bool err;
2327
2328 result = readnum(str, &err);
2329 if (result < 0 || result > INT_MAX) {
2330 result = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002331 nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]",
2332 str, 0, INT_MAX);
2333 } else if (err)
2334 nasm_nonfatal("unable to parse parameter count `%s'", str);
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002335 return result;
2336}
2337
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002338/*
2339 * Count and mark off the parameters in a multi-line macro call.
2340 * This is called both from within the multi-line macro expansion
2341 * code, and also to mark off the default parameters when provided
2342 * in a %macro definition line.
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002343 *
2344 * Note that we need space in the params array for parameter 0 being
2345 * a possible captured label as well as the final NULL.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002346 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002347static void count_mmac_params(Token * t, int *nparamp, Token ***paramsp)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002348{
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07002349 int paramsize;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002350 int nparam = 0;
2351 Token **params;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002352
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002353 paramsize = PARAM_DELTA;
2354 params = nasm_malloc(paramsize * sizeof(*params));
2355 params[0] = NULL;
2356
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002357 while ((t = skip_white(t))) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002358 /* 2 slots for captured label and NULL */
2359 if (nparam+2 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002360 paramsize += PARAM_DELTA;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002361 params = nasm_realloc(params, sizeof(*params) * paramsize);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002362 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002363 params[++nparam] = t;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07002364 if (tok_is(t, '{')) {
2365 int brace = 1;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002366 while (brace && (t = t->next)) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07002367 brace += tok_is(t, '{');
2368 brace -= tok_is(t, '}');
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08002369 }
2370
2371 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002372 /*
2373 * Now we've found the closing brace, look further
2374 * for the comma.
2375 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002376 t = skip_white(t->next);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07002377 if (tok_isnt(t, ','))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002378 nasm_nonfatal("braces do not enclose all of macro parameter");
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07002379 } else {
2380 nasm_nonfatal("expecting closing brace in macro parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002381 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08002382 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002383
2384 while (tok_isnt(t, ','))
2385 t = t->next;
2386
2387 if (t) /* got a comma */
2388 t = t->next; /* eat the comma */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002389 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002390
2391 params[nparam+1] = NULL;
2392 *paramsp = params;
2393 *nparamp = nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002394}
2395
2396/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002397 * Determine whether one of the various `if' conditions is true or
2398 * not.
2399 *
2400 * We must free the tline we get passed.
2401 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002402static enum cond_state if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002403{
H. Peter Anvin70055962007-10-11 00:05:31 -07002404 bool j;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002405 Token *t, *tt, *origline;
2406 struct ppscan pps;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002407 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002408 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002409 enum pp_token_type needtype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002410 const char *dname = pp_directives[ct];
2411 bool casesense = true;
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002412 enum preproc_token cond = PP_COND(ct);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002413
2414 origline = tline;
2415
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002416 switch (cond) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002417 case PP_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002418 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02002419 while (true) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002420 tline = skip_white(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02002421 if (!tline)
2422 break;
2423 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002424 nasm_nonfatal("`%s' expects context identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002425 dname);
2426 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002427 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002428 if (cstk && cstk->name && !nasm_stricmp(tok_text(tline), cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002429 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002430 tline = tline->next;
2431 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002432 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002433
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002434 case PP_IFDEF:
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002435 case PP_IFDEFALIAS:
2436 {
2437 bool alias = cond == PP_IFDEFALIAS;
2438 SMacro *smac;
2439 Context *ctx;
2440 const char *mname;
2441
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002442 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002443 while (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002444 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002445 if (!tline || (tline->type != TOK_ID &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07002446 tline->type != TOK_LOCAL_MACRO)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002447 nasm_nonfatal("`%s' expects macro identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002448 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002449 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002450 }
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002451
2452 mname = tok_text(tline);
2453 ctx = get_ctx(mname, &mname);
Chang S. Baec52aff42020-04-20 21:43:44 +00002454 if (smacro_defined(ctx, mname, 0, &smac, true, alias) && smac
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002455 && smac->alias == alias) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002456 j = true;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002457 break;
2458 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002459 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002460 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002461 break;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002462 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002463
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002464 case PP_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002465 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002466 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002467 while (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002468 tline = skip_white(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002469 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002470 tline->type != TOK_STRING &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07002471 tline->type != TOK_INTERNAL_STRING &&
2472 tline->type != TOK_ENVIRON)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002473 nasm_nonfatal("`%s' expects environment variable names",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002474 dname);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002475 goto fail;
2476 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002477
2478 j |= !!pp_getenv(tline, false);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002479 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002480 }
2481 break;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002482
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002483 case PP_IFIDNI:
2484 casesense = false;
2485 /* fall through */
2486 case PP_IFIDN:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002487 tline = expand_smacro(tline);
2488 t = tt = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002489 while (tok_isnt(tt, ','))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002490 tt = tt->next;
2491 if (!tt) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002492 nasm_nonfatal("`%s' expects two comma-separated arguments",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002493 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002494 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002495 }
2496 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002497 j = true; /* assume equality unless proved not */
H. Peter Anvin8571f062019-09-23 16:40:03 -07002498 while (tok_isnt(t, ',') && tt) {
2499 unsigned int l1, l2;
2500 const char *t1, *t2;
2501
2502 if (tok_is(tt, ',')) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002503 nasm_nonfatal("`%s': more than one comma on line",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002504 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002505 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002506 }
2507 if (t->type == TOK_WHITESPACE) {
2508 t = t->next;
2509 continue;
2510 }
2511 if (tt->type == TOK_WHITESPACE) {
2512 tt = tt->next;
2513 continue;
2514 }
2515 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002516 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002517 break;
2518 }
H. Peter Anvind2456592008-06-19 15:04:18 -07002519
H. Peter Anvin8571f062019-09-23 16:40:03 -07002520 t1 = unquote_token(t);
2521 t2 = unquote_token(tt);
2522 l1 = t->len;
2523 l2 = tt->len;
2524
2525 if (l1 != l2 || mmemcmp(t1, t2, l1, casesense)) {
2526 j = false;
2527 break;
2528 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00002529
H. Peter Anvine2c80182005-01-15 22:15:51 +00002530 t = t->next;
2531 tt = tt->next;
2532 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002533 if (!tok_is(t, ',') || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002534 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002535 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002536
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002537 case PP_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04002538 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002539 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002540 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00002541
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002542 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002543 tline = expand_id(tline);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002544 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002545 nasm_nonfatal("`%s' expects a macro name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002546 goto fail;
2547 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002548 nasm_zero(searching);
H. Peter Anvin8571f062019-09-23 16:40:03 -07002549 searching.name = dup_text(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002550 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002551 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002552 searching.nparam_max = INT_MAX;
2553 tline = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002554 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002555 if (!tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002556 } else if (!tok_type(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002557 nasm_nonfatal("`%s' expects a parameter count or nothing",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002558 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002559 } else {
2560 searching.nparam_min = searching.nparam_max =
H. Peter Anvin8571f062019-09-23 16:40:03 -07002561 read_param_count(tok_text(tline));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002562 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002563 if (tline && tok_is(tline->next, '-')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002564 tline = tline->next->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002565 if (tok_is(tline, '*'))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002566 searching.nparam_max = INT_MAX;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002567 else if (!tok_type(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002568 nasm_nonfatal("`%s' expects a parameter count after `-'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002569 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002570 else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002571 searching.nparam_max = read_param_count(tok_text(tline));
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002572 if (searching.nparam_min > searching.nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002573 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002574 searching.nparam_max = searching.nparam_min;
2575 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002576 }
2577 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002578 if (tline && tok_is(tline->next, '+')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002579 tline = tline->next;
2580 searching.plus = true;
2581 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002582 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
2583 while (mmac) {
2584 if (!strcmp(mmac->name, searching.name) &&
2585 (mmac->nparam_min <= searching.nparam_max
2586 || searching.plus)
2587 && (searching.nparam_min <= mmac->nparam_max
2588 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002589 found = true;
2590 break;
2591 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002592 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002593 }
2594 if (tline && tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002595 nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002596 nasm_free(searching.name);
2597 j = found;
2598 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002599 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002600
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002601 case PP_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002602 needtype = TOK_ID;
2603 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002604 case PP_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002605 needtype = TOK_NUMBER;
2606 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002607 case PP_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002608 needtype = TOK_STRING;
2609 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002610
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002611iftype:
2612 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002613
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002614 while (tok_white(t) ||
2615 (needtype == TOK_NUMBER && (tok_is(t, '-') | tok_is(t, '+'))))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002616 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002617
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002618 j = tok_type(t, needtype);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002619 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002620
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002621 case PP_IFTOKEN:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002622 tline = expand_smacro(tline);
2623 t = skip_white(tline);
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002624
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002625 j = false;
2626 if (t) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002627 t = skip_white(t->next); /* Skip the actual token + whitespace */
2628 j = !t;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002629 }
2630 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002631
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002632 case PP_IFEMPTY:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002633 tline = expand_smacro(tline);
2634 t = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002635 j = !t; /* Should be empty */
2636 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002637
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002638 case PP_IF:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002639 pps.tptr = tline = expand_smacro(tline);
2640 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002641 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002642 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002643 if (!evalresult)
2644 return -1;
2645 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002646 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002647 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002648 nasm_nonfatal("non-constant value given to `%s'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002649 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002650 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002651 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002652 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002653 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002654
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002655 case PP_IFUSING:
2656 case PP_IFUSABLE:
2657 {
2658 const struct use_package *pkg;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002659 const char *name;
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002660
H. Peter Anvin8571f062019-09-23 16:40:03 -07002661 pkg = get_use_pkg(tline, dname, &name);
2662 if (!name)
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002663 goto fail;
2664
2665 j = pkg && ((cond == PP_IFUSABLE) | use_loaded[pkg->index]);
2666 break;
2667 }
2668
H. Peter Anvine2c80182005-01-15 22:15:51 +00002669 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002670 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002671 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002672 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002673
2674 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002675 return (j ^ PP_COND_NEGATIVE(ct)) ? COND_IF_TRUE : COND_IF_FALSE;
H. Peter Anvin70653092007-10-19 14:42:29 -07002676
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002677fail:
2678 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002679 return COND_NEVER;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002680}
2681
2682/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002683 * Default smacro expansion routine: just returns a copy of the
2684 * expansion list.
2685 */
2686static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002687smacro_expand_default(const SMacro *s, Token **params, int nparams)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002688{
2689 (void)params;
2690 (void)nparams;
2691
2692 return dup_tlist(s->expansion, NULL);
2693}
2694
2695/*
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002696 * Emit a macro defintion or undef to the listing file, if
2697 * desired. This is similar to detoken(), but it handles the reverse
2698 * expansion list, does not expand %! or local variable tokens, and
2699 * does some special handling for macro parameters.
2700 */
2701static void
2702list_smacro_def(enum preproc_token op, const Context *ctx, const SMacro *m)
2703{
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002704 Token *t;
2705 size_t namelen, size;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002706 char *def, *p;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002707 char *context_prefix = NULL;
2708 size_t context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002709
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002710 namelen = strlen(m->name);
2711 size = namelen + 2; /* Include room for space after name + NUL */
2712
2713 if (ctx) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07002714 int context_depth = cstk->depth - ctx->depth + 1;
2715 context_prefix =
2716 nasm_asprintf("[%s::%"PRIu64"] %%%-*s",
2717 ctx->name ? ctx->name : "",
2718 ctx->number, context_depth, "");
2719
2720 context_len = nasm_last_string_len();
2721 memset(context_prefix + context_len - context_depth,
2722 '$', context_depth);
2723 size += context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002724 }
2725
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002726 list_for_each(t, m->expansion)
H. Peter Anvin8571f062019-09-23 16:40:03 -07002727 size += t->len;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002728
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002729 if (m->nparam) {
2730 /*
2731 * Space for ( and either , or ) around each
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002732 * parameter, plus up to 4 flags.
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002733 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002734 int i;
2735
2736 size += 1 + 4 * m->nparam;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002737 for (i = 0; i < m->nparam; i++)
H. Peter Anvin8571f062019-09-23 16:40:03 -07002738 size += m->params[i].name.len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002739 }
2740
2741 def = nasm_malloc(size);
2742 p = def+size;
2743 *--p = '\0';
2744
2745 list_for_each(t, m->expansion) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002746 p -= t->len;
2747 memcpy(p, tok_text(t), t->len);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002748 }
2749
2750 *--p = ' ';
2751
2752 if (m->nparam) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002753 int i;
2754
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002755 *--p = ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002756 for (i = m->nparam-1; i >= 0; i--) {
2757 enum sparmflags flags = m->params[i].flags;
2758 if (flags & SPARM_GREEDY)
2759 *--p = '+';
H. Peter Anvin8571f062019-09-23 16:40:03 -07002760 p -= m->params[i].name.len;
2761 memcpy(p, tok_text(&m->params[i].name), m->params[i].name.len);
2762
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002763 if (flags & SPARM_NOSTRIP)
2764 *--p = '!';
2765 if (flags & SPARM_STR)
2766 *--p = '&';
2767 if (flags & SPARM_EVAL)
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002768 *--p = '=';
2769 *--p = ',';
2770 }
2771 *p = '('; /* First parameter starts with ( not , */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002772 }
2773
2774 p -= namelen;
2775 memcpy(p, m->name, namelen);
2776
H. Peter Anvin6686de22019-08-10 05:33:14 -07002777 if (context_prefix) {
2778 p -= context_len;
2779 memcpy(p, context_prefix, context_len);
2780 nasm_free(context_prefix);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002781 }
2782
2783 nasm_listmsg("%s %s", pp_directives[op], p);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002784 nasm_free(def);
H. Peter Anvin6686de22019-08-10 05:33:14 -07002785}
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002786
2787/*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002788 * Parse smacro arguments, return argument count. If the tmpl argument
2789 * is set, set the nparam, greedy and params field in the template.
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002790 * *tpp is updated to point to the pointer to the first token after the
2791 * prototype.
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002792 *
2793 * The text values from any argument tokens are "stolen" and the
2794 * corresponding text fields set to NULL.
2795 */
2796static int parse_smacro_template(Token ***tpp, SMacro *tmpl)
2797{
2798 int nparam = 0;
2799 enum sparmflags flags;
2800 struct smac_param *params = NULL;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002801 bool err, done;
2802 bool greedy = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002803 Token **tn = *tpp;
2804 Token *t = *tn;
2805 Token *name;
2806
H. Peter Anvin (Intel)d4607842019-08-20 16:19:37 -07002807 /*
2808 * DO NOT skip whitespace here, or we won't be able to distinguish:
2809 *
2810 * %define foo (a,b) ; no arguments, (a,b) is the expansion
2811 * %define bar(a,b) ; two arguments, empty expansion
2812 *
2813 * This ambiguity was inherited from C.
2814 */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002815
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002816 if (!tok_is(t, '('))
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002817 goto finish;
2818
2819 if (tmpl) {
2820 Token *tx = t;
2821 Token **txpp = &tx;
2822 int sparam;
2823
2824 /* Count parameters first */
2825 sparam = parse_smacro_template(&txpp, NULL);
2826 if (!sparam)
2827 goto finish; /* No parameters, we're done */
2828 nasm_newn(params, sparam);
2829 }
2830
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002831 /* Skip leading paren */
2832 tn = &t->next;
2833 t = *tn;
2834
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002835 name = NULL;
2836 flags = 0;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002837 err = done = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002838
2839 while (!done) {
2840 if (!t || !t->type) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002841 if (name || flags)
2842 nasm_nonfatal("`)' expected to terminate macro template");
2843 else
2844 nasm_nonfatal("parameter identifier expected");
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002845 break;
2846 }
2847
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002848 switch (t->type) {
2849 case TOK_ID:
2850 if (name)
2851 goto bad;
2852 name = t;
2853 break;
2854
2855 case TOK_OTHER:
H. Peter Anvin8571f062019-09-23 16:40:03 -07002856 if (t->len != 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002857 goto bad;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002858 switch (t->text.a[0]) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002859 case '=':
2860 flags |= SPARM_EVAL;
2861 break;
2862 case '&':
2863 flags |= SPARM_STR;
2864 break;
2865 case '!':
2866 flags |= SPARM_NOSTRIP;
2867 break;
2868 case '+':
2869 flags |= SPARM_GREEDY;
2870 greedy = true;
2871 break;
2872 case ',':
2873 if (greedy)
2874 nasm_nonfatal("greedy parameter must be last");
2875 /* fall through */
2876 case ')':
2877 if (params) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002878 if (name)
2879 steal_Token(&params[nparam].name, name);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002880 params[nparam].flags = flags;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002881 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002882 nparam++;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002883 name = NULL;
2884 flags = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002885 done = t->text.a[0] == ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002886 break;
2887 default:
2888 goto bad;
2889 }
2890 break;
2891
2892 case TOK_WHITESPACE:
2893 break;
2894
2895 default:
2896 bad:
2897 if (!err) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002898 nasm_nonfatal("garbage `%s' in macro parameter list", tok_text(t));
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002899 err = true;
2900 }
2901 break;
2902 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002903
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002904 tn = &t->next;
2905 t = *tn;
2906 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002907
2908finish:
2909 while (t && t->type == TOK_WHITESPACE) {
2910 tn = &t->next;
2911 t = t->next;
2912 }
2913 *tpp = tn;
2914 if (tmpl) {
2915 tmpl->nparam = nparam;
2916 tmpl->greedy = greedy;
2917 tmpl->params = params;
2918 }
2919 return nparam;
2920}
2921
2922/*
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002923 * Common code for defining an smacro. The tmpl argument, if not NULL,
2924 * contains any macro parameters that aren't explicit arguments;
2925 * those are the more uncommon macro variants.
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002926 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002927static SMacro *define_smacro(const char *mname, bool casesense,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002928 Token *expansion, SMacro *tmpl)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002929{
2930 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002931 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002932 Context *ctx;
2933 bool defining_alias = false;
2934 unsigned int nparam = 0;
H. Peter Anvin70653092007-10-19 14:42:29 -07002935
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002936 if (tmpl) {
2937 defining_alias = tmpl->alias;
2938 nparam = tmpl->nparam;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002939 if (nparam && !defining_alias)
2940 mark_smac_params(expansion, tmpl, 0);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002941 }
2942
2943 while (1) {
2944 ctx = get_ctx(mname, &mname);
2945
H. Peter Anvind2354082019-08-27 16:38:48 -07002946 if (!smacro_defined(ctx, mname, nparam, &smac, casesense, true)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002947 /* Create a new macro */
2948 smtbl = ctx ? &ctx->localmac : &smacros;
2949 smhead = (SMacro **) hash_findi_add(smtbl, mname);
2950 nasm_new(smac);
2951 smac->next = *smhead;
2952 *smhead = smac;
2953 break;
2954 } else if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002955 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002956 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002957 /*
2958 * Some instances of the old code considered this a failure,
2959 * some others didn't. What is the right thing to do here?
2960 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002961 goto fail;
H. Peter Anvind2354082019-08-27 16:38:48 -07002962 } else if (!smac->alias || !do_aliases || defining_alias) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002963 /*
2964 * We're redefining, so we have to take over an
2965 * existing SMacro structure. This means freeing
H. Peter Anvin8b262472019-02-26 14:00:54 -08002966 * what was already in it, but not the structure itself.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002967 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002968 clear_smacro(smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002969 break;
2970 } else if (smac->in_progress) {
2971 nasm_nonfatal("macro alias loop");
2972 goto fail;
2973 } else {
2974 /* It is an alias macro; follow the alias link */
2975 SMacro *s;
2976
2977 smac->in_progress = true;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002978 s = define_smacro(tok_text(smac->expansion), casesense,
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002979 expansion, tmpl);
2980 smac->in_progress = false;
2981 return s;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002982 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002983 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002984
2985 smac->name = nasm_strdup(mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002986 smac->casesense = casesense;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002987 smac->expansion = expansion;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002988 smac->expand = smacro_expand_default;
2989 if (tmpl) {
2990 smac->nparam = tmpl->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002991 smac->params = tmpl->params;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002992 smac->alias = tmpl->alias;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002993 smac->greedy = tmpl->greedy;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002994 if (tmpl->expand)
2995 smac->expand = tmpl->expand;
2996 }
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07002997 if (list_option('s')) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002998 list_smacro_def((smac->alias ? PP_DEFALIAS : PP_DEFINE)
2999 + !casesense, ctx, smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003000 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08003001 return smac;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003002
3003fail:
3004 free_tlist(expansion);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003005 if (tmpl)
3006 free_smacro_members(tmpl);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003007 return NULL;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003008}
3009
3010/*
3011 * Undefine an smacro
3012 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003013static void undef_smacro(const char *mname, bool undefalias)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003014{
3015 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003016 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003017 Context *ctx;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003018
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003019 ctx = get_ctx(mname, &mname);
H. Peter Anvin166c2472008-05-28 12:28:58 -07003020 smtbl = ctx ? &ctx->localmac : &smacros;
3021 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07003022
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003023 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003024 /*
3025 * We now have a macro name... go hunt for it.
3026 */
3027 sp = smhead;
3028 while ((s = *sp) != NULL) {
3029 if (!mstrcmp(s->name, mname, s->casesense)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003030 if (s->alias && !undefalias) {
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003031 if (do_aliases) {
3032 if (s->in_progress) {
3033 nasm_nonfatal("macro alias loop");
3034 } else {
3035 s->in_progress = true;
3036 undef_smacro(tok_text(s->expansion), false);
3037 s->in_progress = false;
3038 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003039 }
3040 } else {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07003041 if (list_option('d'))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003042 list_smacro_def(s->alias ? PP_UNDEFALIAS : PP_UNDEF,
3043 ctx, s);
3044 *sp = s->next;
3045 free_smacro(s);
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003046 continue;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003047 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003048 }
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003049 sp = &s->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003050 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003051 }
3052}
3053
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003054/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07003055 * Parse a mmacro specification.
3056 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003057static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07003058{
H. Peter Anvina26433d2008-07-16 14:40:01 -07003059 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003060 tline = skip_white(tline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003061 tline = expand_id(tline);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003062 if (!tok_type(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003063 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003064 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003065 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02003066
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003067#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003068 def->prev = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003069#endif
H. Peter Anvin8571f062019-09-23 16:40:03 -07003070 def->name = dup_text(tline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003071 def->plus = false;
3072 def->nolist = false;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02003073 def->nparam_min = 0;
3074 def->nparam_max = 0;
3075
H. Peter Anvina26433d2008-07-16 14:40:01 -07003076 tline = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003077 tline = skip_white(tline);
3078 if (!tok_type(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003079 nasm_nonfatal("`%s' expects a parameter count", directive);
3080 else
H. Peter Anvin8571f062019-09-23 16:40:03 -07003081 def->nparam_min = def->nparam_max = read_param_count(tok_text(tline));
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003082 if (tline && tok_is(tline->next, '-')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003083 tline = tline->next->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003084 if (tok_is(tline, '*')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003085 def->nparam_max = INT_MAX;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003086 } else if (!tok_type(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003087 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003088 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003089 def->nparam_max = read_param_count(tok_text(tline));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003090 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003091 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03003092 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003093 }
3094 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003095 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003096 if (tline && tok_is(tline->next, '+')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003097 tline = tline->next;
3098 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003099 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003100 if (tline && tok_type(tline->next, TOK_ID) &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07003101 tline->next->len == 7 &&
3102 !nasm_stricmp(tline->next->text.a, ".nolist")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003103 tline = tline->next;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003104 def->nolist = !list_option('f') || istk->nolist;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003105 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003106
H. Peter Anvina26433d2008-07-16 14:40:01 -07003107 /*
3108 * Handle default parameters.
3109 */
3110 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003111 def->dlist = tline->next;
3112 tline->next = NULL;
3113 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003114 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003115 def->dlist = NULL;
3116 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003117 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003118 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003119
H. Peter Anvin89cee572009-07-15 09:16:54 -04003120 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003121 !def->plus) {
3122 /*
3123 *!macro-defaults [on] macros with more default than optional parameters
3124 *! warns when a macro has more default parameters than optional parameters.
3125 *! See \k{mlmacdef} for why might want to disable this warning.
3126 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003127 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003128 "too many default macro parameters in macro `%s'", def->name);
3129 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02003130
H. Peter Anvina26433d2008-07-16 14:40:01 -07003131 return true;
3132}
3133
3134
3135/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003136 * Decode a size directive
3137 */
3138static int parse_size(const char *str) {
3139 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003140 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003141 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003142 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03003143 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003144}
3145
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003146/*
3147 * Process a preprocessor %pragma directive. Currently there are none.
3148 * Gets passed the token list starting with the "preproc" token from
3149 * "%pragma preproc".
3150 */
3151static void do_pragma_preproc(Token *tline)
3152{
3153 /* Skip to the real stuff */
3154 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003155 tline = skip_white(tline);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003156 if (!tline)
3157 return;
3158
3159 (void)tline; /* Nothing else to do at present */
3160}
3161
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003162static bool is_macro_id(const Token *t)
3163{
H. Peter Anvin8571f062019-09-23 16:40:03 -07003164 return tok_type(t, TOK_ID) || tok_type(t, TOK_LOCAL_MACRO);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003165}
3166
H. Peter Anvin8571f062019-09-23 16:40:03 -07003167static const char *get_id(Token **tp, const char *dname)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003168{
H. Peter Anvin8571f062019-09-23 16:40:03 -07003169 const char *id;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003170 Token *t = *tp;
3171
3172 t = t->next; /* Skip directive */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003173 t = skip_white(t);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003174 t = expand_id(t);
3175
3176 if (!is_macro_id(t)) {
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003177 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003178 return NULL;
3179 }
3180
H. Peter Anvin8571f062019-09-23 16:40:03 -07003181 id = tok_text(t);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003182 t = skip_white(t);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003183 *tp = t;
3184 return id;
3185}
3186
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003187/* Parse a %use package name and find the package. Set *err on syntax error. */
3188static const struct use_package *
H. Peter Anvin8571f062019-09-23 16:40:03 -07003189get_use_pkg(Token *t, const char *dname, const char **name)
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003190{
H. Peter Anvin8571f062019-09-23 16:40:03 -07003191 const char *id;
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003192
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003193 t = skip_white(t);
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003194 t = expand_smacro(t);
3195
H. Peter Anvin8571f062019-09-23 16:40:03 -07003196 *name = NULL;
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003197
H. Peter Anvin8571f062019-09-23 16:40:03 -07003198 if (!t) {
3199 nasm_nonfatal("`%s' expects a package name, got end of line", dname);
3200 return NULL;
3201 } else if (t->type != TOK_ID && t->type != TOK_STRING) {
3202 nasm_nonfatal("`%s' expects a package name, got `%s'",
3203 dname, tok_text(t));
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003204 return NULL;
3205 }
3206
H. Peter Anvin8571f062019-09-23 16:40:03 -07003207 *name = id = unquote_token(t);
3208
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003209 t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003210 t = skip_white(t);
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003211 if (t)
3212 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
3213
3214 return nasm_find_use_package(id);
3215}
3216
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003217/*
3218 * Mark parameter tokens in an smacro definition. If the type argument
3219 * is 0, create smac param tokens, otherwise use the type specified;
3220 * normally this is used for TOK_XDEF_PARAM, which is used to protect
3221 * parameter tokens during expansion during %xdefine.
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003222 *
3223 * tmpl may not be NULL here.
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003224 */
3225static void mark_smac_params(Token *tline, const SMacro *tmpl,
3226 enum pp_token_type type)
3227{
3228 const struct smac_param *params = tmpl->params;
3229 int nparam = tmpl->nparam;
3230 Token *t;
3231 int i;
3232
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003233 list_for_each(t, tline) {
3234 if (t->type != TOK_ID && t->type != TOK_XDEF_PARAM)
3235 continue;
3236
3237 for (i = 0; i < nparam; i++) {
3238 if (tok_text_match(t, &params[i].name))
3239 t->type = type ? type : tok_smac_param(i);
3240 }
3241 }
3242}
3243
Ed Beroset3ab3f412002-06-11 03:31:49 +00003244/**
3245 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003246 * Find out if a line contains a preprocessor directive, and deal
3247 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07003248 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00003249 * If a directive _is_ found, it is the responsibility of this routine
3250 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003251 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00003252 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003253 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00003254 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07003255 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003256 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07003257static int do_directive(Token *tline, Token **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003258{
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003259 enum preproc_token op;
H. Peter Anvin4169a472007-09-12 01:29:43 +00003260 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07003261 bool err;
H. Peter Anvin70055962007-10-11 00:05:31 -07003262 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003263 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07003264 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003265 int offset;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003266 const char *p;
3267 char *q, *qbuf;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003268 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003269 const char *mname;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003270 struct ppscan pps;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003271 Include *inc;
3272 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003273 Cond *cond;
3274 MMacro *mmac, **mmhead;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003275 Token *t = NULL, *tt, *macro_start, *last, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003276 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003277 struct tokenval tokval;
3278 expr *evalresult;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003279 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003280 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08003281 errflags severity;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003282 const char *dname; /* Name of directive, for messages */
H. Peter Anvin76690a12002-04-30 20:52:49 +00003283
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003284 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00003285 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003286
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003287 tline = skip_white(tline);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003288 if (!tline || !tok_type(tline, TOK_PREPROC_ID))
3289 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003290
H. Peter Anvin8571f062019-09-23 16:40:03 -07003291 dname = tok_text(tline);
3292 if (dname[1] == '%')
3293 return NO_DIRECTIVE_FOUND;
3294
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003295 op = pp_token_hash(dname);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003296
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003297 casesense = true;
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003298 if (PP_HAS_CASE(op) & PP_INSENSITIVE(op)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003299 casesense = false;
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003300 op--;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003301 }
3302
3303 /*
3304 * If we're in a non-emitting branch of a condition construct,
3305 * or walking to the end of an already terminated %rep block,
3306 * we should ignore all directives except for condition
3307 * directives.
3308 */
3309 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003310 (istk->mstk.mstk && !istk->mstk.mstk->in_progress)) &&
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003311 !is_condition(op)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003312 return NO_DIRECTIVE_FOUND;
3313 }
3314
3315 /*
3316 * If we're defining a macro or reading a %rep block, we should
3317 * ignore all directives except for %macro/%imacro (which nest),
3318 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
3319 * If we're in a %rep block, another %rep nests, so should be let through.
3320 */
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003321 if (defining && op != PP_MACRO && op != PP_RMACRO &&
3322 op != PP_ENDMACRO && op != PP_ENDM &&
3323 (defining->name || (op != PP_ENDREP && op != PP_REP))) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003324 return NO_DIRECTIVE_FOUND;
3325 }
3326
3327 if (defining) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003328 if (op == PP_MACRO || op == PP_RMACRO) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003329 nested_mac_count++;
3330 return NO_DIRECTIVE_FOUND;
3331 } else if (nested_mac_count > 0) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003332 if (op == PP_ENDMACRO) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003333 nested_mac_count--;
3334 return NO_DIRECTIVE_FOUND;
3335 }
3336 }
3337 if (!defining->name) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003338 if (op == PP_REP) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003339 nested_rep_count++;
3340 return NO_DIRECTIVE_FOUND;
3341 } else if (nested_rep_count > 0) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003342 if (op == PP_ENDREP) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003343 nested_rep_count--;
3344 return NO_DIRECTIVE_FOUND;
3345 }
3346 }
3347 }
3348 }
3349
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003350 switch (op) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003351 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003352 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003353 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003354
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003355 case PP_PRAGMA:
3356 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003357 * %pragma namespace options...
3358 *
3359 * The namespace "preproc" is reserved for the preprocessor;
3360 * all other namespaces generate a [pragma] assembly directive.
3361 *
3362 * Invalid %pragmas are ignored and may have different
3363 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003364 */
H. Peter Anvinf5d7d902019-08-10 06:21:00 -07003365 t = tline;
3366 tline = tline->next;
3367 t->next = NULL;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003368 tline = zap_white(expand_smacro(tline));
3369 if (tok_type(tline, TOK_ID)) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003370 if (!nasm_stricmp(tok_text(tline), "preproc")) {
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003371 /* Preprocessor pragma */
3372 do_pragma_preproc(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07003373 free_tlist(tline);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003374 } else {
3375 /* Build the assembler directive */
H. Peter Anvin06335872019-08-10 06:42:55 -07003376
3377 /* Append bracket to the end of the output */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003378 for (t = tline; t->next; t = t->next)
3379 ;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003380 t->next = make_tok_char(NULL, ']');
H. Peter Anvin06335872019-08-10 06:42:55 -07003381
3382 /* Prepend "[pragma " */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003383 t = new_White(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07003384 t = new_Token(t, TOK_ID, "pragma", 6);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003385 t = make_tok_char(t, '[');
H. Peter Anvin06335872019-08-10 06:42:55 -07003386 tline = t;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07003387 *output = tline;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003388 }
3389 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003390 break;
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003391
H. Peter Anvine2c80182005-01-15 22:15:51 +00003392 case PP_STACKSIZE:
3393 /* Directive to tell NASM what the default stack size is. The
3394 * default is for a 16-bit stack, and this can be overriden with
3395 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003396 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003397 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003398 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003399 nasm_nonfatal("`%s' missing size parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003400 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07003401 if (nasm_stricmp(tok_text(tline), "flat") == 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003402 /* All subsequent ARG directives are for a 32-bit stack */
3403 StackSize = 4;
3404 StackPointer = "ebp";
3405 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003406 LocalOffset = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003407 } else if (nasm_stricmp(tok_text(tline), "flat64") == 0) {
Charles Crayne7eaf9192007-11-08 22:11:14 -08003408 /* All subsequent ARG directives are for a 64-bit stack */
3409 StackSize = 8;
3410 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03003411 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08003412 LocalOffset = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003413 } else if (nasm_stricmp(tok_text(tline), "large") == 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003414 /* All subsequent ARG directives are for a 16-bit stack,
3415 * far function call.
3416 */
3417 StackSize = 2;
3418 StackPointer = "bp";
3419 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003420 LocalOffset = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003421 } else if (nasm_stricmp(tok_text(tline), "small") == 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003422 /* All subsequent ARG directives are for a 16-bit stack,
3423 * far function call. We don't support near functions.
3424 */
3425 StackSize = 2;
3426 StackPointer = "bp";
3427 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003428 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003429 } else {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003430 nasm_nonfatal("`%s' invalid size type", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003431 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003432 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003433
H. Peter Anvine2c80182005-01-15 22:15:51 +00003434 case PP_ARG:
3435 /* TASM like ARG directive to define arguments to functions, in
3436 * the following form:
3437 *
3438 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
3439 */
3440 offset = ArgOffset;
3441 do {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003442 const char *arg;
3443 char directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003444 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003445
H. Peter Anvine2c80182005-01-15 22:15:51 +00003446 /* Find the argument name */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003447 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003448 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003449 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003450 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003451 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07003452 arg = tok_text(tline);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003453
H. Peter Anvine2c80182005-01-15 22:15:51 +00003454 /* Find the argument size type */
3455 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003456 if (!tok_is(tline, ':')) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003457 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003458 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003459 }
3460 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003461 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003462 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003463 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003464 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003465
H. Peter Anvine2c80182005-01-15 22:15:51 +00003466 /* Allow macro expansion of type parameter */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003467 tt = tokenize(tok_text(tline));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003468 tt = expand_smacro(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003469 size = parse_size(tok_text(tt));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003470 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003471 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003472 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003473 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003474 }
3475 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003476
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003477 /* Round up to even stack slots */
3478 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003479
H. Peter Anvine2c80182005-01-15 22:15:51 +00003480 /* Now define the macro for the argument */
3481 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
3482 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003483 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003484 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003485
H. Peter Anvine2c80182005-01-15 22:15:51 +00003486 /* Move to the next argument in the list */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003487 tline = skip_white(tline->next);
3488 } while (tok_is(tline, ','));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003489 ArgOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003490 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003491
H. Peter Anvine2c80182005-01-15 22:15:51 +00003492 case PP_LOCAL:
3493 /* TASM like LOCAL directive to define local variables for a
3494 * function, in the following form:
3495 *
3496 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
3497 *
3498 * The '= LocalSize' at the end is ignored by NASM, but is
3499 * required by TASM to define the local parameter size (and used
3500 * by the TASM macro package).
3501 */
3502 offset = LocalOffset;
3503 do {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003504 const char *local;
3505 char directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003506 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003507
H. Peter Anvine2c80182005-01-15 22:15:51 +00003508 /* Find the argument name */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003509 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003510 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003511 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003512 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003513 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07003514 local = tok_text(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003515
H. Peter Anvine2c80182005-01-15 22:15:51 +00003516 /* Find the argument size type */
3517 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003518 if (!tok_is(tline, ':')) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003519 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003520 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003521 }
3522 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003523 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003524 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003525 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003526 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003527
H. Peter Anvine2c80182005-01-15 22:15:51 +00003528 /* Allow macro expansion of type parameter */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003529 tt = tokenize(tok_text(tline));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003530 tt = expand_smacro(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003531 size = parse_size(tok_text(tt));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003532 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003533 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003534 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003535 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003536 }
3537 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003538
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003539 /* Round up to even stack slots */
3540 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003541
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003542 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003543
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003544 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003545 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
3546 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003547 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003548
H. Peter Anvine2c80182005-01-15 22:15:51 +00003549 /* Now define the assign to setup the enter_c macro correctly */
3550 snprintf(directive, sizeof(directive),
3551 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003552 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003553
H. Peter Anvine2c80182005-01-15 22:15:51 +00003554 /* Move to the next argument in the list */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003555 tline = skip_white(tline->next);
3556 } while (tok_is(tline, ','));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003557 LocalOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003558 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003559
H. Peter Anvine2c80182005-01-15 22:15:51 +00003560 case PP_CLEAR:
3561 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003562 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003563 free_macros();
3564 init_macros();
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003565 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003566
H. Peter Anvin418ca702008-05-30 10:42:30 -07003567 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003568 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003569 t = skip_white(t);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003570 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003571 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003572 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003573 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003574 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003575 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003576 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003577
3578 strlist_add(deplist, unquote_token_cstr(t));
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003579 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003580
3581 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003582 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003583 t = skip_white(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07003584
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003585 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003586 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003587 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003588 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003589 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003590 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003591 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003592 p = unquote_token_cstr(t);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003593 nasm_new(inc);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003594 inc->next = istk;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003595 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07003596 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08003597 (pp_mode == PP_DEPS)
3598 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003599 if (!inc->fp) {
3600 /* -MG given but file not found */
3601 nasm_free(inc);
3602 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003603 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003604 inc->lineno = src_set_linnum(0);
3605 inc->lineinc = 1;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003606 inc->nolist = istk->nolist;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003607 istk = inc;
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003608 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003609 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003610 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003611
H. Peter Anvind2456592008-06-19 15:04:18 -07003612 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003613 {
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003614 const struct use_package *pkg;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003615 const char *name;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003616
H. Peter Anvin8571f062019-09-23 16:40:03 -07003617 pkg = get_use_pkg(tline->next, dname, &name);
3618 if (!name)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003619 goto done;
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003620 if (!pkg) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003621 nasm_nonfatal("unknown `%s' package: `%s'", dname, name);
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003622 } else if (!use_loaded[pkg->index]) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003623 /*
3624 * Not already included, go ahead and include it.
3625 * Treat it as an include file for the purpose of
3626 * producing a listing.
3627 */
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003628 use_loaded[pkg->index] = true;
3629 stdmacpos = pkg->macros;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003630 nasm_new(inc);
3631 inc->next = istk;
3632 inc->fname = src_set_fname(NULL);
3633 inc->lineno = src_set_linnum(0);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003634 inc->nolist = !list_option('b') || istk->nolist;
3635 istk = inc;
3636 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003637 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003638 break;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003639 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003640 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003641 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07003642 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003643 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003644 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003645 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003646 if (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003647 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003648 nasm_nonfatal("`%s' expects a context identifier", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003649 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003650 }
3651 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003652 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003653 dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003654 p = tok_text(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003655 } else {
3656 p = NULL; /* Anonymous */
3657 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07003658
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003659 if (op == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08003660 nasm_new(ctx);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003661 ctx->depth = cstk ? cstk->depth + 1 : 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003662 ctx->next = cstk;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003663 ctx->name = p ? nasm_strdup(p) : NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003664 ctx->number = unique++;
3665 cstk = ctx;
3666 } else {
3667 /* %pop or %repl */
3668 if (!cstk) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003669 nasm_nonfatal("`%s': context stack is empty", dname);
3670 } else if (op == PP_POP) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003671 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003672 nasm_nonfatal("`%s' in wrong context: %s, "
H. Peter Anvin8b262472019-02-26 14:00:54 -08003673 "expected %s",
3674 dname, cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003675 else
3676 ctx_pop();
3677 } else {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003678 /* op == PP_REPL */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003679 nasm_free((char *)cstk->name);
3680 cstk->name = p ? nasm_strdup(p) : NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003681 p = NULL;
3682 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003683 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003684 break;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003685 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003686 severity = ERR_FATAL;
3687 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003688 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003689 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003690 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003691 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003692 /*!
3693 *!user [on] %warning directives
3694 *! controls output of \c{%warning} directives (see \k{pperror}).
3695 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003696 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003697 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003698
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003699issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07003700 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003701 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003702 tline->next = expand_smacro(tline->next);
3703 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003704 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003705 t = tline ? tline->next : NULL;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003706 t = skip_white(t);
3707 if (tok_type(tline, TOK_STRING) && !t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003708 /* The line contains only a quoted string */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003709 p = unquote_token(tline); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08003710 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003711 } else {
3712 /* Not a quoted string, or more than a quoted string */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003713 q = detoken(tline, false);
3714 nasm_error(severity, "%s", q);
3715 nasm_free(q);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003716 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003717 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003718 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003719
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003720 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003721 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003722 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003723 else {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003724 j = if_condition(tline->next, op);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003725 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003726 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003727 cond = nasm_malloc(sizeof(Cond));
3728 cond->next = istk->conds;
3729 cond->state = j;
3730 istk->conds = cond;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003731 if(istk->mstk.mstk)
3732 istk->mstk.mstk->condcnt++;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003733 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003734
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003735 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003736 if (!istk->conds)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003737 nasm_fatal("`%s': no matching `%%if'", dname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003738 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003739 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003740 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003741 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003742
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003743 case COND_DONE:
3744 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003745 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003746
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003747 case COND_ELSE_TRUE:
3748 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003749 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003750 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003751 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003752 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003753
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003754 case COND_IF_FALSE:
3755 /*
3756 * IMPORTANT: In the case of %if, we will already have
3757 * called expand_mmac_params(); however, if we're
3758 * processing an %elif we must have been in a
3759 * non-emitting mode, which would have inhibited
3760 * the normal invocation of expand_mmac_params().
3761 * Therefore, we have to do it explicitly here.
3762 */
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003763 j = if_condition(expand_mmac_params(tline->next), op);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003764 tline->next = NULL; /* it got freed */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003765 istk->conds->state = j;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003766 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003767 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003768 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003769
H. Peter Anvine2c80182005-01-15 22:15:51 +00003770 case PP_ELSE:
3771 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003772 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003773 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003774 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07003775 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003776 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003777 case COND_IF_TRUE:
3778 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003779 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003780 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003781
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003782 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003783 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003784
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003785 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003786 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003787 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003788
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003789 case COND_ELSE_TRUE:
3790 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003791 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003792 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003793 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003794 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003795 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003796 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003797
H. Peter Anvine2c80182005-01-15 22:15:51 +00003798 case PP_ENDIF:
3799 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003800 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003801 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003802 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003803 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003804 cond = istk->conds;
3805 istk->conds = cond->next;
3806 nasm_free(cond);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003807 if(istk->mstk.mstk)
3808 istk->mstk.mstk->condcnt--;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003809 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003810
H. Peter Anvin8b262472019-02-26 14:00:54 -08003811 case PP_RMACRO:
3812 case PP_MACRO:
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07003813 {
3814 MMacro *def;
3815
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003816 nasm_assert(!defining);
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07003817 nasm_new(def);
3818 def->casesense = casesense;
3819 def->dstk.mmac = defining;
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003820 if (op == PP_RMACRO)
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07003821 def->max_depth = nasm_limit[LIMIT_MACRO_LEVELS];
3822 if (!parse_mmacro_spec(tline, def, dname)) {
3823 nasm_free(def);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003824 goto done;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003825 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003826
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07003827 defining = def;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07003828 src_get(&defining->xline, &defining->fname);
3829
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003830 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
3831 while (mmac) {
3832 if (!strcmp(mmac->name, defining->name) &&
3833 (mmac->nparam_min <= defining->nparam_max
3834 || defining->plus)
3835 && (defining->nparam_min <= mmac->nparam_max
3836 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003837 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003838 defining->name);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003839 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003840 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003841 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003842 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003843 break;
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07003844 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003845
H. Peter Anvine2c80182005-01-15 22:15:51 +00003846 case PP_ENDM:
3847 case PP_ENDMACRO:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003848 if (!(defining && defining->name)) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003849 nasm_nonfatal("`%s': not defining a macro", tok_text(tline));
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003850 goto done;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003851 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003852 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
3853 defining->next = *mmhead;
3854 *mmhead = defining;
3855 defining = NULL;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003856 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003857
H. Peter Anvin89cee572009-07-15 09:16:54 -04003858 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003859 /*
3860 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003861 * macro-end marker for a macro with a name. Then we
3862 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003863 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003864 list_for_each(l, istk->expansion)
3865 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003866 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003867
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003868 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003869 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003870 * Remove all conditional entries relative to this
3871 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003872 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003873 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
3874 cond = istk->conds;
3875 istk->conds = cond->next;
3876 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003877 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003878 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003879 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003880 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003881 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003882 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05003883
H. Peter Anvina26433d2008-07-16 14:40:01 -07003884 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003885 casesense = false;
3886 /* fall through */
3887 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07003888 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003889 MMacro **mmac_p;
3890 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003891
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003892 nasm_zero(spec);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003893 spec.casesense = casesense;
3894 if (!parse_mmacro_spec(tline, &spec, dname)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003895 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003896 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003897 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
3898 while (mmac_p && *mmac_p) {
3899 mmac = *mmac_p;
3900 if (mmac->casesense == spec.casesense &&
3901 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
3902 mmac->nparam_min == spec.nparam_min &&
3903 mmac->nparam_max == spec.nparam_max &&
3904 mmac->plus == spec.plus) {
3905 *mmac_p = mmac->next;
3906 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003907 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003908 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003909 }
3910 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003911 free_tlist(spec.dlist);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003912 break;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003913 }
3914
H. Peter Anvine2c80182005-01-15 22:15:51 +00003915 case PP_ROTATE:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003916 while (tok_white(tline->next))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003917 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003918 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003919 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003920 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003921 return DIRECTIVE_FOUND;
3922 }
3923 t = expand_smacro(tline->next);
3924 tline->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003925 pps.tptr = tline = t;
3926 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003927 tokval.t_type = TOKEN_INVALID;
3928 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003929 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003930 free_tlist(tline);
3931 if (!evalresult)
3932 return DIRECTIVE_FOUND;
3933 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003934 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003935 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003936 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003937 return DIRECTIVE_FOUND;
3938 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003939 mmac = istk->mstk.mmac;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003940 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003941 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003942 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003943 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003944 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003945 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003946
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003947 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003948 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003949 rotate += mmac->nparam;
3950
3951 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003952 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003953 break;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003954
H. Peter Anvine2c80182005-01-15 22:15:51 +00003955 case PP_REP:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003956 {
3957 MMacro *tmp_defining;
3958
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003959 nolist = false;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003960 tline = skip_white(tline->next);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003961 if (tok_type(tline, TOK_ID) && tline->len == 7 &&
3962 !nasm_memicmp(tline->text.a, ".nolist", 7)) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003963 nolist = !list_option('f') || istk->nolist;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003964 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003965 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003966
H. Peter Anvine2c80182005-01-15 22:15:51 +00003967 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003968 pps.tptr = expand_smacro(tline);
3969 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003970 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003971 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003972 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003973 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003974 if (!evalresult)
3975 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003976 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003977 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003978 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003979 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003980 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003981 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003982 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003983 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003984 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
3985 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003986 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003987 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003988 /*!
3989 *!negative-rep [on] regative %rep count
3990 *! warns about negative counts given to the \c{%rep}
3991 *! preprocessor directive.
3992 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08003993 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003994 "negative `%%rep' count: %"PRId64, count);
3995 count = 0;
3996 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003997 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003998 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003999 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004000 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07004001 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004002 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004003 tmp_defining = defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07004004 nasm_new(defining);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004005 defining->nolist = nolist;
4006 defining->in_progress = count;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004007 defining->mstk = istk->mstk;
4008 defining->dstk.mstk = tmp_defining;
4009 defining->dstk.mmac = tmp_defining ? tmp_defining->dstk.mmac : NULL;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07004010 src_get(&defining->xline, &defining->fname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004011 break;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004012 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004013
H. Peter Anvine2c80182005-01-15 22:15:51 +00004014 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004015 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004016 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004017 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004018 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004019
H. Peter Anvine2c80182005-01-15 22:15:51 +00004020 /*
4021 * Now we have a "macro" defined - although it has no name
4022 * and we won't be entering it in the hash tables - we must
4023 * push a macro-end marker for it on to istk->expansion.
4024 * After that, it will take care of propagating itself (a
4025 * macro-end marker line for a macro which is really a %rep
4026 * block will cause the macro to be re-expanded, complete
4027 * with another macro-end marker to ensure the process
4028 * continues) until the whole expansion is forcibly removed
4029 * from istk->expansion by a %exitrep.
4030 */
H. Peter Anvin6686de22019-08-10 05:33:14 -07004031 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004032 l->next = istk->expansion;
4033 l->finishes = defining;
4034 l->first = NULL;
4035 istk->expansion = l;
4036
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004037 istk->mstk.mstk = defining;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004038
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07004039 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004040 defining = defining->dstk.mstk;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004041 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004042
H. Peter Anvine2c80182005-01-15 22:15:51 +00004043 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004044 /*
4045 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004046 * macro-end marker for a macro with no name. Then we set
4047 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004048 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004049 list_for_each(l, istk->expansion)
4050 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004051 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004052
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004053 if (l)
H. Peter Anvind983b622019-10-07 21:19:32 -07004054 l->finishes->in_progress = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004055 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004056 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004057 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004058
H. Peter Anvin8b262472019-02-26 14:00:54 -08004059 case PP_DEFINE:
4060 case PP_XDEFINE:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004061 case PP_DEFALIAS:
H. Peter Anvin8b262472019-02-26 14:00:54 -08004062 {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004063 SMacro tmpl;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004064 Token **lastp;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07004065 int nparam;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07004066
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004067 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004068 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004069
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004070 nasm_zero(tmpl);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004071 lastp = &tline->next;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07004072 nparam = parse_smacro_template(&lastp, &tmpl);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004073 tline = *lastp;
4074 *lastp = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004075
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004076 if (unlikely(op == PP_DEFALIAS)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004077 macro_start = tline;
4078 if (!is_macro_id(macro_start)) {
4079 nasm_nonfatal("`%s' expects a macro identifier to alias",
4080 dname);
4081 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004082 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004083 tt = macro_start->next;
4084 macro_start->next = NULL;
4085 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004086 tline = skip_white(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004087 if (tline && tline->type) {
4088 nasm_warn(WARN_OTHER,
4089 "trailing garbage after aliasing identifier ignored");
4090 }
4091 free_tlist(tt);
4092 tmpl.alias = true;
4093 } else {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004094 if (op == PP_XDEFINE) {
4095 /* Protect macro parameter tokens */
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07004096 if (nparam)
4097 mark_smac_params(tline, &tmpl, TOK_XDEF_PARAM);
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004098 tline = expand_smacro(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004099 }
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004100 /* NB: Does this still make sense? */
4101 macro_start = reverse_tokens(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004102 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004103
H. Peter Anvine2c80182005-01-15 22:15:51 +00004104 /*
4105 * Good. We now have a macro name, a parameter count, and a
4106 * token list (in reverse order) for an expansion. We ought
4107 * to be OK just to create an SMacro, store it, and let
4108 * free_tlist have the rest of the line (which we have
4109 * carefully re-terminated after chopping off the expansion
4110 * from the end).
4111 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004112 define_smacro(mname, casesense, macro_start, &tmpl);
4113 break;
4114 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004115
H. Peter Anvine2c80182005-01-15 22:15:51 +00004116 case PP_UNDEF:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004117 case PP_UNDEFALIAS:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004118 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004119 goto done;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004120 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004121 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004122
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004123 undef_smacro(mname, op == PP_UNDEFALIAS);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004124 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004125
H. Peter Anvin8b262472019-02-26 14:00:54 -08004126 case PP_DEFSTR:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004127 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004128 goto done;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004129
H. Peter Anvin9e200162008-06-04 17:23:14 -07004130 last = tline;
4131 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004132 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004133
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004134 tline = zap_white(tline);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004135 q = detoken(tline, false);
4136 macro_start = make_tok_qstr(NULL, q);
4137 nasm_free(q);
H. Peter Anvin9e200162008-06-04 17:23:14 -07004138
4139 /*
4140 * We now have a macro name, an implicit parameter count of
4141 * zero, and a string token to use as an expansion. Create
4142 * and store an SMacro.
4143 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004144 define_smacro(mname, casesense, macro_start, NULL);
4145 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004146
H. Peter Anvin8b262472019-02-26 14:00:54 -08004147 case PP_DEFTOK:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004148 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004149 goto done;
4150
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004151 last = tline;
4152 tline = expand_smacro(tline->next);
4153 last->next = NULL;
4154
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004155 t = skip_white(tline);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004156 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004157 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004158 nasm_nonfatal("`%s' requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004159 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004160 goto done;
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004161 }
4162
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04004163 /*
4164 * Convert the string to a token stream. Note that smacros
4165 * are stored with the token stream reversed, so we have to
4166 * reverse the output of tokenize().
4167 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07004168 macro_start = reverse_tokens(tokenize(unquote_token_cstr(t)));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004169
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004170 /*
4171 * We now have a macro name, an implicit parameter count of
4172 * zero, and a numeric token to use as an expansion. Create
4173 * and store an SMacro.
4174 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004175 define_smacro(mname, casesense, macro_start, NULL);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004176 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004177 break;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004178
H. Peter Anvin418ca702008-05-30 10:42:30 -07004179 case PP_PATHSEARCH:
4180 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07004181 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004182
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004183 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004184 goto done;
4185
H. Peter Anvin418ca702008-05-30 10:42:30 -07004186 last = tline;
4187 tline = expand_smacro(tline->next);
4188 last->next = NULL;
4189
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004190 t = skip_white(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004191 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004192 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004193 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004194 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004195 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004196 }
4197 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004198 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004199
4200 p = unquote_token_cstr(t);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004201
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07004202 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07004203 if (!found_path)
4204 found_path = p;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004205 macro_start = make_tok_qstr(NULL, found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004206
4207 /*
4208 * We now have a macro name, an implicit parameter count of
4209 * zero, and a string token to use as an expansion. Create
4210 * and store an SMacro.
4211 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004212 define_smacro(mname, casesense, macro_start, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004213 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004214 break;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004215 }
4216
H. Peter Anvine2c80182005-01-15 22:15:51 +00004217 case PP_STRLEN:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004218 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004219 goto done;
4220
H. Peter Anvine2c80182005-01-15 22:15:51 +00004221 last = tline;
4222 tline = expand_smacro(tline->next);
4223 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004224
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004225 t = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004226 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004227 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004228 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004229 free_tlist(tline);
4230 free_tlist(origline);
4231 return DIRECTIVE_FOUND;
4232 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004233
H. Peter Anvin8571f062019-09-23 16:40:03 -07004234 unquote_token(t);
4235 macro_start = make_tok_num(NULL, t->len);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004236
H. Peter Anvine2c80182005-01-15 22:15:51 +00004237 /*
4238 * We now have a macro name, an implicit parameter count of
4239 * zero, and a numeric token to use as an expansion. Create
4240 * and store an SMacro.
4241 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004242 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004243 free_tlist(tline);
4244 free_tlist(origline);
4245 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004246
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004247 case PP_STRCAT:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004248 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004249 goto done;
4250
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004251 last = tline;
4252 tline = expand_smacro(tline->next);
4253 last->next = NULL;
4254
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004255 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004256 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004257 switch (t->type) {
4258 case TOK_WHITESPACE:
4259 break;
4260 case TOK_STRING:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004261 unquote_token(t);
4262 len += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004263 break;
4264 case TOK_OTHER:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004265 if (tok_is(t, ',')) /* permit comma separators */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004266 break;
4267 /* else fall through */
4268 default:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004269 nasm_nonfatal("non-string passed to `%s': %s", dname,
4270 tok_text(t));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004271 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004272 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004273 }
4274 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004275
H. Peter Anvin (Intel)f770ce82019-10-17 18:22:43 -07004276 q = qbuf = nasm_malloc(len+1);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004277 list_for_each(t, tline) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004278 if (t->type == TOK_INTERNAL_STRING)
4279 q = mempcpy(q, tok_text(t), t->len);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004280 }
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07004281 *q = '\0';
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004282
4283 /*
4284 * We now have a macro name, an implicit parameter count of
4285 * zero, and a numeric token to use as an expansion. Create
4286 * and store an SMacro.
4287 */
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07004288 macro_start = make_tok_qstr_len(NULL, qbuf, len);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004289 nasm_free(qbuf);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004290 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004291 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004292 break;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004293
H. Peter Anvine2c80182005-01-15 22:15:51 +00004294 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004295 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004296 int64_t start, count;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004297 const char *txt;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004298 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07004299
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004300 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004301 goto done;
4302
H. Peter Anvine2c80182005-01-15 22:15:51 +00004303 last = tline;
4304 tline = expand_smacro(tline->next);
4305 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004306
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04004307 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004308 t = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004309
4310 t = skip_white(t);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004311
H. Peter Anvine2c80182005-01-15 22:15:51 +00004312 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004313 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004314 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004315 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004316 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004317 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004318
H. Peter Anvin8b262472019-02-26 14:00:54 -08004319 pps.tptr = t->next;
4320 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004321 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004322 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004323 if (!evalresult) {
4324 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004325 goto done;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004326 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004327 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004328 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004329 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004330 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004331 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004332
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004333 pps.tptr = skip_white(pps.tptr);
H. Peter Anvin8b262472019-02-26 14:00:54 -08004334 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004335 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004336 } else {
4337 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004338 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004339 if (!evalresult) {
4340 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004341 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004342 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004343 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004344 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004345 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004346 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004347 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004348 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004349
H. Peter Anvin8571f062019-09-23 16:40:03 -07004350 unquote_token(t);
4351 len = t->len;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004352
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04004353 /* make start and count being in range */
4354 if (start < 0)
4355 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004356 if (count < 0)
4357 count = len + count + 1 - start;
4358 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04004359 count = len - start;
4360 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004361 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004362
H. Peter Anvin8571f062019-09-23 16:40:03 -07004363 txt = (start < 0) ? "" : tok_text(t) + start;
4364 len = count;
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07004365 macro_start = make_tok_qstr_len(NULL, txt, len);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004366
H. Peter Anvine2c80182005-01-15 22:15:51 +00004367 /*
4368 * We now have a macro name, an implicit parameter count of
4369 * zero, and a numeric token to use as an expansion. Create
4370 * and store an SMacro.
4371 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004372 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004373 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004374 break;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004375 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004376
H. Peter Anvin8b262472019-02-26 14:00:54 -08004377 case PP_ASSIGN:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004378 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004379 goto done;
4380
H. Peter Anvine2c80182005-01-15 22:15:51 +00004381 last = tline;
4382 tline = expand_smacro(tline->next);
4383 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004384
H. Peter Anvin8b262472019-02-26 14:00:54 -08004385 pps.tptr = tline;
4386 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004387 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004388 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004389 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004390 if (!evalresult)
4391 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004392
H. Peter Anvine2c80182005-01-15 22:15:51 +00004393 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004394 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00004395
H. Peter Anvine2c80182005-01-15 22:15:51 +00004396 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004397 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004398 free_tlist(origline);
4399 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004400 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004401
H. Peter Anvin8571f062019-09-23 16:40:03 -07004402 macro_start = make_tok_num(NULL, reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00004403
H. Peter Anvine2c80182005-01-15 22:15:51 +00004404 /*
4405 * We now have a macro name, an implicit parameter count of
4406 * zero, and a numeric token to use as an expansion. Create
4407 * and store an SMacro.
4408 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004409 define_smacro(mname, casesense, macro_start, NULL);
4410 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004411
H. Peter Anvind2354082019-08-27 16:38:48 -07004412 case PP_ALIASES:
4413 tline = tline->next;
4414 tline = expand_smacro(tline);
4415 do_aliases = pp_get_boolean_option(tline, do_aliases);
4416 break;
4417
H. Peter Anvine2c80182005-01-15 22:15:51 +00004418 case PP_LINE:
4419 /*
4420 * Syntax is `%line nnn[+mmm] [filename]'
4421 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004422 if (unlikely(pp_noline))
4423 goto done;
4424
H. Peter Anvine2c80182005-01-15 22:15:51 +00004425 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004426 tline = skip_white(tline);
4427 if (!tok_type(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004428 nasm_nonfatal("`%s' expects line number", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004429 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004430 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07004431 k = readnum(tok_text(tline), &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004432 m = 1;
4433 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004434 if (tok_is(tline, '+')) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004435 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004436 if (!tok_type(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004437 nasm_nonfatal("`%s' expects line increment", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004438 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004439 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07004440 m = readnum(tok_text(tline), &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004441 tline = tline->next;
4442 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004443 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004444 src_set_linnum(k);
4445 istk->lineinc = m;
4446 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07004447 char *fname = detoken(tline, false);
4448 src_set_fname(fname);
4449 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004450 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004451 break;
4452 }
4453
4454done:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004455 free_tlist(origline);
4456 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004457}
4458
4459/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00004460 * Ensure that a macro parameter contains a condition code and
4461 * nothing else. Return the condition code index if so, or -1
4462 * otherwise.
4463 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004464static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004465{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004466 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004467
H. Peter Anvin25a99342007-09-22 17:45:45 -07004468 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004469 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07004470
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004471 t = skip_white(t);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004472 if (!tok_type(t, TOK_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004473 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004474 tt = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004475 tt = skip_white(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004476 if (tok_isnt(tt, ','))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004477 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004478
H. Peter Anvin8571f062019-09-23 16:40:03 -07004479 return bsii(tok_text(t), (const char **)conditions,
4480 ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00004481}
4482
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004483static inline bool pp_concat_match(const Token *t, unsigned int mask)
4484{
4485 return t && (PP_CONCAT_MASK(t->type) & mask);
4486}
4487
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004488/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004489 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004490 * pasting, if @handle_explicit passed then explicit pasting
4491 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004492 * The @m array can contain a series of token types which are
4493 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004494 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004495static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004496 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07004497{
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004498 Token *tok, *t, *next, **prev_next, **prev_nonspace;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004499 bool pasted = false;
4500 char *buf, *p;
4501 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07004502
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004503 /*
4504 * The last token before pasting. We need it
4505 * to be able to connect new handled tokens.
4506 * In other words if there were a tokens stream
4507 *
4508 * A -> B -> C -> D
4509 *
4510 * and we've joined tokens B and C, the resulting
4511 * stream should be
4512 *
4513 * A -> BC -> D
4514 */
4515 tok = *head;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004516 prev_next = prev_nonspace = head;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004517
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004518 if (tok_white(tok) || tok_type(tok, TOK_PASTE))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004519 prev_nonspace = NULL;
4520
4521 while (tok && (next = tok->next)) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004522 bool did_paste = false;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004523
4524 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07004525 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004526 /* Zap redundant whitespaces */
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004527 tok->next = next = zap_white(next);
H. Peter Anvind784a082009-04-20 14:01:18 -07004528 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004529
4530 case TOK_PASTE:
4531 /* Explicit pasting */
4532 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004533 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004534
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004535 /* Left pasting token is start of line */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004536 if (!prev_nonspace) {
4537 nasm_nonfatal("No lvalue found on pasting");
4538 tok = delete_Token(tok);
4539 break;
4540 }
4541
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004542 did_paste = true;
4543
4544 prev_next = prev_nonspace;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004545 t = *prev_nonspace;
4546
4547 /* Delete leading whitespace */
4548 next = zap_white(t->next);
4549
4550 /* Delete the %+ token itself */
4551 nasm_assert(next == tok);
4552 next = delete_Token(next);
4553
4554 /* Delete trailing whitespace */
4555 next = zap_white(next);
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004556
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004557 /*
4558 * No ending token, this might happen in two
4559 * cases
4560 *
4561 * 1) There indeed no right token at all
4562 * 2) There is a bare "%define ID" statement,
4563 * and @ID does expand to whitespace.
4564 *
4565 * So technically we need to do a grammar analysis
4566 * in another stage of parsing, but for now lets don't
4567 * change the behaviour people used to. Simply allow
4568 * whitespace after paste token.
4569 */
4570 if (!next) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004571 *prev_nonspace = tok = NULL; /* End of line */
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004572 break;
4573 }
4574
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004575 p = buf = nasm_malloc(t->len + next->len + 1);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004576 p = mempcpy(p, tok_text(t), t->len);
4577 p = mempcpy(p, tok_text(next), next->len);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004578 *p = '\0';
4579 delete_Token(t);
4580 t = tokenize(buf);
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004581 nasm_free(buf);
4582
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004583 if (unlikely(!t)) {
4584 /*
4585 * No output at all? Replace with a single whitespace.
4586 * This should never happen.
4587 */
4588 t = new_White(NULL);
4589 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004590
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004591 *prev_nonspace = tok = t;
4592 while (t->next)
4593 t = t->next; /* Find the last token produced */
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004594
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004595 /* Delete the second token and attach to the end of the list */
4596 t->next = delete_Token(next);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004597
4598 /* We want to restart from the head of the pasted token */
4599 next = tok;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004600 break;
4601
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004602 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004603 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004604 for (i = 0; i < mnum; i++) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004605 if (pp_concat_match(tok, m[i].mask_head))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004606 break;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004607 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004608
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004609 if (i >= mnum)
4610 break;
4611
4612 len = tok->len;
4613 while (pp_concat_match(next, m[i].mask_tail)) {
4614 len += next->len;
4615 next = next->next;
4616 }
4617
4618 /* No match or no text to process */
4619 if (len == tok->len)
4620 break;
4621
4622 p = buf = nasm_malloc(len + 1);
4623 while (tok != next) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004624 p = mempcpy(p, tok_text(tok), tok->len);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004625 tok = delete_Token(tok);
4626 }
4627 *p = '\0';
4628 *prev_next = tok = t = tokenize(buf);
4629 nasm_free(buf);
4630
4631 /*
4632 * Connect pasted into original stream,
4633 * ie A -> new-tokens -> B
4634 */
4635 while (t->next)
4636 t = t->next;
4637 t->next = next;
4638 prev_next = prev_nonspace = &t->next;
4639 did_paste = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004640 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004641 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004642
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004643 if (did_paste) {
4644 pasted = true;
4645 } else {
4646 prev_next = &tok->next;
4647 if (next && next->type != TOK_WHITESPACE && next->type != TOK_PASTE)
4648 prev_nonspace = prev_next;
4649 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004650
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004651 tok = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004652 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004653
4654 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07004655}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004656
4657/*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004658 * Computes the proper rotation of mmacro parameters
4659 */
4660static int mmac_rotate(const MMacro *mac, unsigned int n)
4661{
4662 if (--n < mac->nparam)
4663 n = (n + mac->rotate) % mac->nparam;
4664
4665 return n+1;
4666}
4667
4668/*
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004669 * expands to a list of tokens from %{x:y}
4670 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004671static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004672{
4673 Token *t = tline, **tt, *tm, *head;
4674 char *pos;
4675 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004676
H. Peter Anvin8571f062019-09-23 16:40:03 -07004677 pos = strchr(tok_text(tline), ':');
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004678 nasm_assert(pos);
4679
4680 lst = atoi(pos + 1);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004681 fst = atoi(tok_text(tline) + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004682
4683 /*
4684 * only macros params are accounted so
4685 * if someone passes %0 -- we reject such
4686 * value(s)
4687 */
4688 if (lst == 0 || fst == 0)
4689 goto err;
4690
4691 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004692 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
4693 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004694 goto err;
4695
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004696 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
4697 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004698
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004699 /* count from zero */
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004700 fst--, lst--;
4701
4702 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004703 * It will be at least one token. Note we
4704 * need to scan params until separator, otherwise
4705 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004706 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004707 j = (fst + mac->rotate) % mac->nparam;
4708 tm = mac->params[j+1];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03004709 if (!tm)
4710 goto err;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004711 head = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004712 tt = &head->next, tm = tm->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004713 while (tok_isnt(tm, ',')) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004714 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004715 *tt = t, tt = &t->next, tm = tm->next;
4716 }
4717
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004718 if (fst < lst) {
4719 for (i = fst + 1; i <= lst; i++) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004720 t = make_tok_char(NULL, ',');
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004721 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004722 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004723 tm = mac->params[j+1];
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004724 while (tok_isnt(tm, ',')) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004725 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004726 *tt = t, tt = &t->next, tm = tm->next;
4727 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004728 }
4729 } else {
4730 for (i = fst - 1; i >= lst; i--) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004731 t = make_tok_char(NULL, ',');
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004732 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004733 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004734 tm = mac->params[j+1];
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004735 while (!tok_isnt(tm, ',')) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004736 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004737 *tt = t, tt = &t->next, tm = tm->next;
4738 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004739 }
4740 }
4741
4742 *last = tt;
4743 return head;
4744
4745err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004746 nasm_nonfatal("`%%{%s}': macro parameters out of range",
H. Peter Anvin8571f062019-09-23 16:40:03 -07004747 tok_text(tline) + 1);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004748 return NULL;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004749}
4750
H. Peter Anvin76690a12002-04-30 20:52:49 +00004751/*
4752 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004753 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004754 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004755 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004756static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004757{
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004758 Token **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004759 bool changed = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004760 MMacro *mac = istk->mstk.mmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004761
4762 tail = &thead;
4763 thead = NULL;
4764
H. Peter Anvine2c80182005-01-15 22:15:51 +00004765 while (tline) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004766 bool change;
H. Peter Anvin (Intel)a762cd42020-06-01 11:49:08 -07004767 bool err_not_mac = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004768 Token *t = tline;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004769 const char *text = tok_text(t);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004770 int type = t->type;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004771
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004772 tline = tline->next;
4773 t->next = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004774
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004775 switch (type) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004776 case TOK_LOCAL_SYMBOL:
H. Peter Anvin (Intel)a762cd42020-06-01 11:49:08 -07004777 change = true;
4778
4779 if (!mac) {
4780 err_not_mac = true;
4781 break;
4782 }
4783
H. Peter Anvin8571f062019-09-23 16:40:03 -07004784 type = TOK_ID;
4785 text = nasm_asprintf("..@%"PRIu64".%s", mac->unique, text+2);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004786 break;
4787 case TOK_MMACRO_PARAM:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004788 {
4789 Token *tt = NULL;
4790
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004791 change = true;
4792
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004793 if (!mac) {
H. Peter Anvin (Intel)a762cd42020-06-01 11:49:08 -07004794 err_not_mac = true;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004795 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004796 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004797
4798 if (strchr(text, ':')) {
4799 /*
4800 * seems we have a parameters range here
4801 */
4802 Token *head, **last;
4803 head = expand_mmac_params_range(mac, t, &last);
4804 if (head) {
4805 *tail = head;
4806 *last = tline;
4807 text = NULL;
4808 }
4809 break;
4810 }
4811
4812 switch (text[1]) {
4813 /*
4814 * We have to make a substitution of one of the
4815 * forms %1, %-1, %+1, %%foo, %0, %00.
4816 */
4817 case '0':
4818 if (!text[2]) {
4819 type = TOK_NUMBER;
4820 text = nasm_asprintf("%d", mac->nparam);
4821 break;
4822 }
4823 if (text[2] != '0' || text[3])
4824 goto invalid;
4825 /* a possible captured label == mac->params[0] */
4826 /* fall through */
4827 default:
4828 {
4829 unsigned long n;
4830 char *ep;
4831
4832 n = strtoul(text + 1, &ep, 10);
4833 if (unlikely(*ep))
4834 goto invalid;
4835
4836 if (n <= mac->nparam) {
4837 n = mmac_rotate(mac, n);
4838 dup_tlistn(mac->params[n], mac->paramlen[n], &tail);
4839 }
4840 text = NULL;
4841 break;
4842 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004843 case '-':
4844 case '+':
4845 {
4846 int cc;
4847 unsigned long n;
4848 char *ep;
4849
H. Peter Anvin8571f062019-09-23 16:40:03 -07004850 n = strtoul(tok_text(t) + 2, &ep, 10);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004851 if (unlikely(*ep))
4852 goto invalid;
4853
Chang S. Bae057b8322020-04-18 23:11:21 +00004854 if (n && n <= mac->nparam) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004855 n = mmac_rotate(mac, n);
4856 tt = mac->params[n];
4857 }
4858 cc = find_cc(tt);
4859 if (cc == -1) {
4860 nasm_nonfatal("macro parameter `%s' is not a condition code",
H. Peter Anvin8571f062019-09-23 16:40:03 -07004861 tok_text(t));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004862 text = NULL;
4863 break;
4864 }
4865
4866 type = TOK_ID;
4867 if (text[1] == '-') {
4868 int ncc = inverse_ccs[cc];
4869 if (unlikely(ncc == -1)) {
4870 nasm_nonfatal("condition code `%s' is not invertible",
4871 conditions[cc]);
4872 break;
4873 }
4874 cc = ncc;
4875 }
4876 text = nasm_strdup(conditions[cc]);
4877 break;
4878 }
4879
4880 invalid:
4881 nasm_nonfatal("invalid macro parameter: `%s'", text);
4882 text = NULL;
4883 break;
4884 }
4885 break;
4886 }
4887
4888 case TOK_PREPROC_Q:
4889 if (mac) {
4890 type = TOK_ID;
4891 text = nasm_strdup(mac->iname);
4892 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07004893 } else {
4894 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004895 }
4896 break;
4897
4898 case TOK_PREPROC_QQ:
4899 if (mac) {
4900 type = TOK_ID;
4901 text = nasm_strdup(mac->name);
4902 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07004903 } else {
4904 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004905 }
4906 break;
4907
4908 case TOK_INDIRECT:
4909 {
4910 Token *tt;
4911
H. Peter Anvin8571f062019-09-23 16:40:03 -07004912 tt = tokenize(tok_text(t));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004913 tt = expand_mmac_params(tt);
4914 tt = expand_smacro(tt);
4915 /* Why dup_tlist() here? We should own tt... */
4916 dup_tlist(tt, &tail);
4917 text = NULL;
4918 change = true;
4919 break;
4920 }
4921
4922 default:
4923 change = false;
4924 break;
4925 }
4926
H. Peter Anvin (Intel)a762cd42020-06-01 11:49:08 -07004927 if (err_not_mac) {
4928 nasm_nonfatal("`%s': not in a macro call", text);
4929 text = NULL;
4930 change = true;
4931 }
4932
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004933 if (change) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004934 if (!text) {
4935 delete_Token(t);
4936 } else {
4937 *tail = t;
4938 tail = &t->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004939 set_text(t, text, tok_strlen(text));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004940 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004941 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004942 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004943 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004944 *tail = t;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004945 tail = &t->next;
4946 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004947 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004948
H. Peter Anvineba20a72002-04-30 20:53:55 +00004949 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004950
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004951 if (changed) {
4952 const struct tokseq_match t[] = {
4953 {
4954 PP_CONCAT_MASK(TOK_ID) |
4955 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4956 PP_CONCAT_MASK(TOK_ID) |
4957 PP_CONCAT_MASK(TOK_NUMBER) |
4958 PP_CONCAT_MASK(TOK_FLOAT) |
4959 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4960 },
4961 {
4962 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4963 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4964 }
4965 };
4966 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4967 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004968
H. Peter Anvin76690a12002-04-30 20:52:49 +00004969 return thead;
4970}
4971
H. Peter Anvin322bee02019-08-10 01:38:06 -07004972static Token *expand_smacro_noreset(Token * tline);
H. Peter Anvin322bee02019-08-10 01:38:06 -07004973
H. Peter Anvin76690a12002-04-30 20:52:49 +00004974/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004975 * Expand *one* single-line macro instance. If the first token is not
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004976 * a macro at all, it is simply copied to the output and the pointer
4977 * advanced. tpp should be a pointer to a pointer (usually the next
4978 * pointer of the previous token) to the first token. **tpp is updated
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004979 * to point to the first token of the expansion, and *tpp updated to
4980 * point to the next pointer of the last token of the expansion.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004981 *
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004982 * If the expansion is empty, *tpp will be unchanged but **tpp will
4983 * be advanced past the macro call.
4984 *
H. Peter Anvin322bee02019-08-10 01:38:06 -07004985 * Return the macro expanded, or NULL if no expansion took place.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004986 */
H. Peter Anvin322bee02019-08-10 01:38:06 -07004987static SMacro *expand_one_smacro(Token ***tpp)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004988{
4989 Token **params = NULL;
4990 const char *mname;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004991 Token *mstart = **tpp;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004992 Token *tline = mstart;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004993 SMacro *head, *m;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004994 int i;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004995 Token *t, *tup, *tafter;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004996 int nparam = 0;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004997 bool cond_comma;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004998
4999 if (!tline)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005000 return false; /* Empty line, nothing to do */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005001
H. Peter Anvin8571f062019-09-23 16:40:03 -07005002 mname = tok_text(mstart);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005003
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005004 smacro_deadman.total--;
H. Peter Anvin322bee02019-08-10 01:38:06 -07005005 smacro_deadman.levels--;
5006
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005007 if (unlikely(smacro_deadman.total < 0 || smacro_deadman.levels < 0)) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07005008 if (unlikely(!smacro_deadman.triggered)) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005009 nasm_nonfatal("interminable macro recursion");
H. Peter Anvin322bee02019-08-10 01:38:06 -07005010 smacro_deadman.triggered = true;
5011 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005012 goto not_a_macro;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005013 } else if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005014 head = (SMacro *)hash_findix(&smacros, mname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005015 } else if (tline->type == TOK_LOCAL_MACRO) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005016 Context *ctx = get_ctx(mname, &mname);
5017 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
5018 } else {
5019 goto not_a_macro;
5020 }
5021
5022 /*
5023 * We've hit an identifier of some sort. First check whether the
5024 * identifier is a single-line macro at all, then think about
5025 * checking for parameters if necessary.
5026 */
5027 list_for_each(m, head) {
H. Peter Anvind2354082019-08-27 16:38:48 -07005028 if (unlikely(m->alias && !do_aliases))
5029 continue;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005030 if (!mstrcmp(m->name, mname, m->casesense))
5031 break;
5032 }
5033
5034 if (!m) {
5035 goto not_a_macro;
5036 }
5037
5038 /* Parse parameters, if applicable */
5039
5040 params = NULL;
5041 nparam = 0;
5042
5043 if (m->nparam == 0) {
5044 /*
5045 * Simple case: the macro is parameterless.
5046 * Nothing to parse; the expansion code will
5047 * drop the macro name token.
5048 */
5049 } else {
5050 /*
5051 * Complicated case: at least one macro with this name
5052 * exists and takes parameters. We must find the
5053 * parameters in the call, count them, find the SMacro
5054 * that corresponds to that form of the macro call, and
5055 * substitute for the parameters when we expand. What a
5056 * pain.
5057 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005058 Token *t;
5059 int paren, brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005060
5061 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005062 tline = skip_white(tline);
5063 if (!tok_is(tline, '(')) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005064 /*
5065 * This macro wasn't called with parameters: ignore
5066 * the call. (Behaviour borrowed from gnu cpp.)
5067 */
5068 goto not_a_macro;
5069 }
5070
5071 paren = 1;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005072 nparam = 1;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005073 brackets = 0;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005074 t = tline; /* tline points to leading ( */
5075
5076 while (paren) {
5077 t = t->next;
5078
5079 if (!t) {
5080 nasm_nonfatal("macro call expects terminating `)'");
5081 goto not_a_macro;
5082 }
5083
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005084 if (t->type != TOK_OTHER || t->len != 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005085 continue;
5086
H. Peter Anvin8571f062019-09-23 16:40:03 -07005087 switch (t->text.a[0]) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005088 case ',':
5089 if (!brackets)
5090 nparam++;
5091 break;
5092
5093 case '{':
5094 brackets++;
5095 break;
5096
5097 case '}':
5098 if (brackets > 0)
5099 brackets--;
5100 break;
5101
5102 case '(':
5103 if (!brackets)
5104 paren++;
5105 break;
5106
5107 case ')':
5108 if (!brackets)
5109 paren--;
5110 break;
5111
5112 default:
5113 break; /* Normal token */
5114 }
5115 }
5116
5117 /*
5118 * Look for a macro matching in both name and parameter count.
5119 * We already know any matches cannot be anywhere before the
5120 * current position of "m", so there is no reason to
5121 * backtrack.
5122 */
5123 while (1) {
5124 if (!m) {
5125 /*!
5126 *!macro-params-single [on] single-line macro calls with wrong parameter count
5127 *! warns about \i{single-line macros} being invoked
5128 *! with the wrong number of parameters.
5129 */
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005130 nasm_warn(WARN_MACRO_PARAMS_SINGLE|ERR_HOLD,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005131 "single-line macro `%s' exists, "
5132 "but not taking %d parameter%s",
5133 mname, nparam, (nparam == 1) ? "" : "s");
5134 goto not_a_macro;
5135 }
5136
5137 if (!mstrcmp(m->name, mname, m->casesense)) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005138 if (nparam == m->nparam)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005139 break; /* It's good */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005140 if (m->greedy && nparam >= m->nparam-1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005141 break; /* Also good */
5142 }
5143 m = m->next;
5144 }
5145 }
5146
5147 if (m->in_progress)
5148 goto not_a_macro;
5149
5150 /* Expand the macro */
5151 m->in_progress = true;
5152
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005153 if (nparam) {
5154 /* Extract parameters */
5155 Token **phead, **pep;
5156 int white = 0;
5157 int brackets = 0;
5158 int paren;
5159 bool bracketed = false;
5160 bool bad_bracket = false;
5161 enum sparmflags flags;
5162
5163 nparam = m->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005164 paren = 1;
5165 nasm_newn(params, nparam);
5166 i = 0;
5167 flags = m->params[i].flags;
5168 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005169 *pep = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005170
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005171 while (paren) {
5172 bool skip;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005173 char ch;
5174
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005175 tline = tline->next;
5176
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005177 if (!tline)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005178 nasm_nonfatal("macro call expects terminating `)'");
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005179
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005180 ch = 0;
5181 skip = false;
5182
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005183
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005184 switch (tline->type) {
5185 case TOK_OTHER:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005186 if (tline->len == 1)
H. Peter Anvin8571f062019-09-23 16:40:03 -07005187 ch = tline->text.a[0];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005188 break;
5189
5190 case TOK_WHITESPACE:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005191 if (!(flags & SPARM_NOSTRIP)) {
5192 if (brackets || *phead)
5193 white++; /* Keep interior whitespace */
5194 skip = true;
5195 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005196 break;
5197
5198 default:
5199 break;
5200 }
5201
5202 switch (ch) {
5203 case ',':
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005204 if (!brackets && !(flags & SPARM_GREEDY)) {
5205 i++;
5206 nasm_assert(i < nparam);
5207 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005208 *pep = NULL;
5209 bracketed = false;
5210 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005211 flags = m->params[i].flags;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005212 }
5213 break;
5214
5215 case '{':
5216 if (!bracketed) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005217 bracketed = !*phead && !(flags & SPARM_NOSTRIP);
5218 skip = bracketed;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005219 }
5220 brackets++;
5221 break;
5222
5223 case '}':
5224 if (brackets > 0) {
5225 if (!--brackets)
5226 skip = bracketed;
5227 }
5228 break;
5229
5230 case '(':
5231 if (!brackets)
5232 paren++;
5233 break;
5234
5235 case ')':
5236 if (!brackets) {
5237 paren--;
5238 if (!paren) {
5239 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005240 i++; /* Found last argument */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005241 }
5242 }
5243 break;
5244
5245 default:
5246 break; /* Normal token */
5247 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005248
5249 if (!skip) {
5250 Token *t;
5251
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005252 bad_bracket |= bracketed && !brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005253
5254 if (white) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005255 *pep = t = new_White(NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005256 pep = &t->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005257 white = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005258 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005259 *pep = t = dup_Token(NULL, tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005260 pep = &t->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005261 }
5262 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005263
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005264 /*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005265 * Possible further processing of parameters. Note that the
5266 * ordering matters here.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005267 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005268 for (i = 0; i < nparam; i++) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005269 enum sparmflags flags = m->params[i].flags;
5270
5271 if (flags & SPARM_EVAL) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005272 /* Evaluate this parameter as a number */
5273 struct ppscan pps;
5274 struct tokenval tokval;
5275 expr *evalresult;
5276 Token *eval_param;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005277
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005278 pps.tptr = eval_param = expand_smacro_noreset(params[i]);
5279 pps.ntokens = -1;
5280 tokval.t_type = TOKEN_INVALID;
5281 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005282
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005283 free_tlist(eval_param);
5284 params[i] = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005285
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005286 if (!evalresult) {
5287 /* Nothing meaningful to do */
5288 } else if (tokval.t_type) {
5289 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
5290 } else if (!is_simple(evalresult)) {
5291 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
5292 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005293 params[i] = make_tok_num(NULL, reloc_value(evalresult));
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005294 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005295 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005296
5297 if (flags & SPARM_STR) {
5298 /* Convert expansion to a quoted string */
5299 char *arg;
5300 Token *qs;
5301
5302 qs = expand_smacro_noreset(params[i]);
5303 arg = detoken(qs, false);
5304 free_tlist(qs);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005305 params[i] = make_tok_qstr(NULL, arg);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005306 nasm_free(arg);
5307 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005308 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005309 }
5310
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005311 /* Note: we own the expansion this returns. */
5312 t = m->expand(m, params, nparam);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005313
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005314 tafter = tline->next; /* Skip past the macro call */
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005315 tline->next = NULL; /* Truncate list at the macro call end */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005316 tline = tafter;
5317
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005318 tup = NULL;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005319 cond_comma = false;
5320
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005321 while (t) {
5322 enum pp_token_type type = t->type;
5323 Token *tnext = t->next;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005324
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005325 switch (type) {
5326 case TOK_PREPROC_Q:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005327 delete_Token(t);
5328 t = dup_Token(tline, mstart);
5329 break;
5330
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005331 case TOK_PREPROC_QQ:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005332 {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005333 size_t mlen = strlen(m->name);
5334 size_t len;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005335 char *p;
5336
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005337 t->type = mstart->type;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005338 if (t->type == TOK_LOCAL_MACRO) {
5339 const char *psp; /* prefix start pointer */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005340 const char *pep; /* prefix end pointer */
H. Peter Anvin8571f062019-09-23 16:40:03 -07005341 size_t plen;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005342
H. Peter Anvin8571f062019-09-23 16:40:03 -07005343 psp = tok_text(mstart);
5344 get_ctx(psp, &pep);
5345 plen = pep - psp;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005346
H. Peter Anvin8571f062019-09-23 16:40:03 -07005347 len = mlen + plen;
5348 p = nasm_malloc(len + 1);
5349 p = mempcpy(p, psp, plen);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005350 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005351 len = mlen;
5352 p = nasm_malloc(len + 1);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005353 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07005354 p = mempcpy(p, m->name, mlen);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005355 *p = '\0';
H. Peter Anvin8571f062019-09-23 16:40:03 -07005356 set_text_free(t, p, len);
5357
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005358 t->next = tline;
5359 break;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005360 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005361
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005362 case TOK_COND_COMMA:
5363 delete_Token(t);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005364 t = cond_comma ? make_tok_char(tline, ',') : NULL;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005365 break;
5366
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005367 case TOK_ID:
5368 case TOK_PREPROC_ID:
H. Peter Anvin8571f062019-09-23 16:40:03 -07005369 case TOK_LOCAL_MACRO:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005370 {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005371 /*
5372 * Chain this into the target line *before* expanding,
5373 * that way we pick up any arguments to the new macro call,
5374 * if applicable.
5375 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005376 Token **tp = &t;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005377 t->next = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005378 expand_one_smacro(&tp);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005379 tline = *tp; /* First token left after any macro call */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005380 break;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005381 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005382 default:
5383 if (is_smac_param(t->type)) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005384 int param = smac_nparam(t->type);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005385 nasm_assert(!tup && param < nparam);
5386 delete_Token(t);
5387 t = NULL;
5388 tup = tnext;
5389 tnext = dup_tlist_reverse(params[param], NULL);
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005390 cond_comma = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005391 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005392 t->next = tline;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005393 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005394 }
5395
5396 if (t) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005397 Token *endt = tline;
5398
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005399 tline = t;
Chang S. Bae95e54a92020-02-06 14:39:22 -08005400 while (!cond_comma && t && t != endt) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005401 cond_comma = t->type != TOK_WHITESPACE;
Chang S. Bae95e54a92020-02-06 14:39:22 -08005402 t = t->next;
5403 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005404 }
5405
5406 if (tnext) {
5407 t = tnext;
5408 } else {
5409 t = tup;
5410 tup = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005411 }
5412 }
5413
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005414 **tpp = tline;
H. Peter Anvin (Intel)6e714962020-06-01 12:21:10 -07005415 for (t = tline; t && t != tafter; t = t->next)
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005416 *tpp = &t->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005417
5418 m->in_progress = false;
5419
5420 /* Don't do this until after expansion or we will clobber mname */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005421 free_tlist(mstart);
H. Peter Anvin322bee02019-08-10 01:38:06 -07005422 goto done;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005423
5424 /*
5425 * No macro expansion needed; roll back to mstart (if necessary)
H. Peter Anvin322bee02019-08-10 01:38:06 -07005426 * and then advance to the next input token. Note that this is
5427 * by far the common case!
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005428 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005429not_a_macro:
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005430 *tpp = &mstart->next;
H. Peter Anvin322bee02019-08-10 01:38:06 -07005431 m = NULL;
5432done:
5433 smacro_deadman.levels++;
5434 if (unlikely(params))
5435 free_tlist_array(params, nparam);
5436 return m;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005437}
5438
5439/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005440 * Expand all single-line macro calls made in the given line.
5441 * Return the expanded version of the line. The original is deemed
5442 * to be destroyed in the process. (In reality we'll just move
5443 * Tokens from input to output a lot of the time, rather than
5444 * actually bothering to destroy and replicate.)
5445 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005446static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005447{
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005448 smacro_deadman.total = nasm_limit[LIMIT_MACRO_TOKENS];
H. Peter Anvin322bee02019-08-10 01:38:06 -07005449 smacro_deadman.levels = nasm_limit[LIMIT_MACRO_LEVELS];
5450 smacro_deadman.triggered = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005451 return expand_smacro_noreset(tline);
5452}
5453
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005454static Token *expand_smacro_noreset(Token *org_tline)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005455{
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005456 Token *tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005457 bool expanded;
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005458 errhold errhold; /* Hold warning/errors during expansion */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005459
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005460 if (!org_tline)
5461 return NULL; /* Empty input */
5462
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005463 /*
5464 * Trick: we should avoid changing the start token pointer since it can
5465 * be contained in "next" field of other token. Because of this
5466 * we allocate a copy of first token and work with it; at the end of
5467 * routine we copy it back
5468 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005469 tline = dup_Token(org_tline->next, org_tline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005470
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005471 /*
5472 * Pretend that we always end up doing expansion on the first pass;
5473 * that way %+ get processed. However, if we process %+ before the
5474 * first pass, we end up with things like MACRO %+ TAIL trying to
5475 * look up the macro "MACROTAIL", which we don't want.
5476 */
5477 expanded = true;
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005478
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005479 while (true) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005480 static const struct tokseq_match tmatch[] = {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005481 {
5482 PP_CONCAT_MASK(TOK_ID) |
H. Peter Anvin8571f062019-09-23 16:40:03 -07005483 PP_CONCAT_MASK(TOK_LOCAL_MACRO) |
5484 PP_CONCAT_MASK(TOK_ENVIRON) |
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005485 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
5486 PP_CONCAT_MASK(TOK_ID) |
H. Peter Anvin8571f062019-09-23 16:40:03 -07005487 PP_CONCAT_MASK(TOK_LOCAL_MACRO) |
5488 PP_CONCAT_MASK(TOK_ENVIRON) |
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005489 PP_CONCAT_MASK(TOK_PREPROC_ID) |
5490 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
5491 }
5492 };
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005493 Token **tail = &tline;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005494
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005495 /*
5496 * We hold warnings/errors until we are done this this loop. It is
5497 * possible for nuisance warnings to appear that disappear on later
5498 * passes.
5499 */
5500 errhold = nasm_error_hold_push();
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005501 while (*tail) /* main token loop */
H. Peter Anvin322bee02019-08-10 01:38:06 -07005502 expanded |= !!expand_one_smacro(&tail);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005503
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005504 if (!expanded)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005505 break; /* Done! */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005506
5507 /*
5508 * Now scan the entire line and look for successive TOK_IDs
5509 * that resulted after expansion (they can't be produced by
5510 * tokenize()). The successive TOK_IDs should be concatenated.
5511 * Also we look for %+ tokens and concatenate the tokens
5512 * before and after them (without white spaces in between).
5513 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005514 if (!paste_tokens(&tline, tmatch, ARRAY_SIZE(tmatch), true))
5515 break; /* Done again! */
5516
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005517 expanded = false;
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005518 nasm_error_hold_pop(errhold, false);
H. Peter Anvin734b1882002-04-30 21:01:08 +00005519 }
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005520 nasm_error_hold_pop(errhold, true);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005521
H. Peter Anvin8571f062019-09-23 16:40:03 -07005522 if (!tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005523 /*
5524 * The expression expanded to empty line;
5525 * we can't return NULL because of the "trick" above.
5526 * Just set the line to a single WHITESPACE token.
H. Peter Anvin8571f062019-09-23 16:40:03 -07005527 */
5528
5529 tline = new_White(NULL);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005530 }
5531
H. Peter Anvin8571f062019-09-23 16:40:03 -07005532 steal_Token(org_tline, tline);
5533 org_tline->next = tline->next;
5534 delete_Token(tline);
5535
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005536 return org_tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005537}
5538
5539/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005540 * Similar to expand_smacro but used exclusively with macro identifiers
5541 * right before they are fetched in. The reason is that there can be
5542 * identifiers consisting of several subparts. We consider that if there
5543 * are more than one element forming the name, user wants a expansion,
5544 * otherwise it will be left as-is. Example:
5545 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005546 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005547 *
5548 * the identifier %$abc will be left as-is so that the handler for %define
5549 * will suck it and define the corresponding value. Other case:
5550 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005551 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005552 *
5553 * In this case user wants name to be expanded *before* %define starts
5554 * working, so we'll expand %$abc into something (if it has a value;
5555 * otherwise it will be left as-is) then concatenate all successive
5556 * PP_IDs into one.
5557 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005558static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005559{
5560 Token *cur, *oldnext = NULL;
5561
H. Peter Anvin734b1882002-04-30 21:01:08 +00005562 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005563 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005564
5565 cur = tline;
5566 while (cur->next &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07005567 (cur->next->type == TOK_ID || cur->next->type == TOK_PREPROC_ID ||
5568 cur->next->type == TOK_LOCAL_MACRO || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005569 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005570
5571 /* If identifier consists of just one token, don't expand */
5572 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005573 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005574
H. Peter Anvine2c80182005-01-15 22:15:51 +00005575 if (cur) {
5576 oldnext = cur->next; /* Detach the tail past identifier */
5577 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005578 }
5579
H. Peter Anvin734b1882002-04-30 21:01:08 +00005580 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005581
H. Peter Anvine2c80182005-01-15 22:15:51 +00005582 if (cur) {
5583 /* expand_smacro possibly changhed tline; re-scan for EOL */
5584 cur = tline;
5585 while (cur && cur->next)
5586 cur = cur->next;
5587 if (cur)
5588 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005589 }
5590
5591 return tline;
5592}
5593
5594/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005595 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005596 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005597 * to check for an initial label - that's taken care of in
5598 * expand_mmacro - but must check numbers of parameters. Guaranteed
5599 * to be called with tline->type == TOK_ID, so the putative macro
5600 * name is easy to find.
5601 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005602static MMacro *is_mmacro(Token * tline, int *nparamp, Token ***params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005603{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005604 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005605 Token **params;
5606 int nparam;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005607 const char *finding = tok_text(tline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005608
H. Peter Anvin8571f062019-09-23 16:40:03 -07005609 head = (MMacro *) hash_findix(&mmacros, finding);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005610
5611 /*
5612 * Efficiency: first we see if any macro exists with the given
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005613 * name which isn't already excluded by macro cycle removal.
5614 * (The cycle removal test here helps optimize the case of wrapping
5615 * instructions, and is cheap to do here.)
5616 *
5617 * If not, we can return NULL immediately. _Then_ we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005618 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005619 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005620 */
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005621 list_for_each(m, head) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005622 if (!mstrcmp(m->name, finding, m->casesense) &&
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005623 (m->in_progress != 1 || m->max_depth > 0))
5624 break; /* Found something that needs consideration */
5625 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005626 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005627 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005628
5629 /*
5630 * OK, we have a potential macro. Count and demarcate the
5631 * parameters.
5632 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00005633 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005634
5635 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005636 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005637 * structure that handles this number.
5638 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005639 while (m) {
5640 if (m->nparam_min <= nparam
5641 && (m->plus || nparam <= m->nparam_max)) {
5642 /*
5643 * This one is right. Just check if cycle removal
5644 * prohibits us using it before we actually celebrate...
5645 */
5646 if (m->in_progress > m->max_depth) {
5647 if (m->max_depth > 0) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08005648 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005649 m->max_depth);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005650 }
5651 nasm_free(params);
5652 return NULL;
5653 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005654 /*
5655 * It's right, and we can use it. Add its default
5656 * parameters to the end of our list if necessary.
5657 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005658 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005659 int newnparam = m->nparam_min + m->ndefs;
5660 params = nasm_realloc(params, sizeof(*params) * (newnparam+2));
5661 memcpy(&params[nparam+1], &m->defaults[nparam+1-m->nparam_min],
5662 (newnparam - nparam) * sizeof(*params));
5663 nparam = newnparam;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005664 }
5665 /*
5666 * If we've gone over the maximum parameter count (and
5667 * we're in Plus mode), ignore parameters beyond
5668 * nparam_max.
5669 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005670 if (m->plus && nparam > m->nparam_max)
5671 nparam = m->nparam_max;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005672
H. Peter Anvin (Intel)7eb18212019-08-20 16:24:46 -07005673 /*
5674 * If nparam was adjusted above, make sure the list is still
5675 * NULL-terminated.
5676 */
5677 params[nparam+1] = NULL;
5678
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005679 /* Done! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005680 *params_array = params;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005681 *nparamp = nparam;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005682 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005683 }
5684 /*
5685 * This one wasn't right: look for the next one with the
5686 * same name.
5687 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005688 list_for_each(m, m->next)
H. Peter Anvin8571f062019-09-23 16:40:03 -07005689 if (!mstrcmp(m->name, tok_text(tline), m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005690 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005691 }
5692
5693 /*
5694 * After all that, we didn't find one with the right number of
5695 * parameters. Issue a warning, and fail to expand the macro.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005696 *!
5697 *!macro-params-multi [on] multi-line macro calls with wrong parameter count
5698 *! warns about \i{multi-line macros} being invoked
5699 *! with the wrong number of parameters. See \k{mlmacover} for an
5700 *! example of why you might want to disable this warning.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005701 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005702 nasm_warn(WARN_MACRO_PARAMS_MULTI,
5703 "multi-line macro `%s' exists, but not taking %d parameter%s",
H. Peter Anvin8571f062019-09-23 16:40:03 -07005704 tok_text(tline), nparam, (nparam == 1) ? "" : "s");
H. Peter Anvin734b1882002-04-30 21:01:08 +00005705 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005706 return NULL;
5707}
5708
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005709
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005710#if 0
5711
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005712/*
5713 * Save MMacro invocation specific fields in
5714 * preparation for a recursive macro expansion
5715 */
5716static void push_mmacro(MMacro *m)
5717{
5718 MMacroInvocation *i;
5719
5720 i = nasm_malloc(sizeof(MMacroInvocation));
5721 i->prev = m->prev;
5722 i->params = m->params;
5723 i->iline = m->iline;
5724 i->nparam = m->nparam;
5725 i->rotate = m->rotate;
5726 i->paramlen = m->paramlen;
5727 i->unique = m->unique;
5728 i->condcnt = m->condcnt;
5729 m->prev = i;
5730}
5731
5732
5733/*
5734 * Restore MMacro invocation specific fields that were
5735 * saved during a previous recursive macro expansion
5736 */
5737static void pop_mmacro(MMacro *m)
5738{
5739 MMacroInvocation *i;
5740
5741 if (m->prev) {
5742 i = m->prev;
5743 m->prev = i->prev;
5744 m->params = i->params;
5745 m->iline = i->iline;
5746 m->nparam = i->nparam;
5747 m->rotate = i->rotate;
5748 m->paramlen = i->paramlen;
5749 m->unique = i->unique;
5750 m->condcnt = i->condcnt;
5751 nasm_free(i);
5752 }
5753}
5754
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005755#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005756
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005757/*
H. Peter Anvin (Intel)ffe89dd2019-08-20 16:06:36 -07005758 * List an mmacro call with arguments (-Lm option)
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005759 */
5760static void list_mmacro_call(const MMacro *m)
5761{
5762 const char prefix[] = " ;;; [macro] ";
5763 size_t namelen, size;
5764 char *buf, *p;
5765 unsigned int i;
5766 const Token *t;
5767
5768 namelen = strlen(m->iname);
5769 size = namelen + sizeof(prefix); /* Includes final null (from prefix) */
5770
5771 for (i = 1; i <= m->nparam; i++) {
5772 int j = 0;
5773 size += 3; /* Braces and space/comma */
5774 list_for_each(t, m->params[i]) {
5775 if (j++ >= m->paramlen[i])
5776 break;
5777 size += (t->type == TOK_WHITESPACE) ? 1 : t->len;
5778 }
5779 }
5780
5781 buf = p = nasm_malloc(size);
5782 p = mempcpy(p, prefix, sizeof(prefix) - 1);
5783 p = mempcpy(p, m->iname, namelen);
5784 *p++ = ' ';
5785
5786 for (i = 1; i <= m->nparam; i++) {
5787 int j = 0;
5788 *p++ = '{';
5789 list_for_each(t, m->params[i]) {
5790 if (j++ >= m->paramlen[i])
5791 break;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005792 p = mempcpy(p, tok_text(t), t->len);
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005793 }
5794 *p++ = '}';
5795 *p++ = ',';
5796 }
5797
5798 *--p = '\0'; /* Replace last delimeter with null */
5799 lfmt->line(LIST_MACRO, -1, buf);
5800 nasm_free(buf);
5801}
5802
5803/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005804 * Expand the multi-line macro call made by the given line, if
5805 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005806 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005807 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005808static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005809{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005810 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005811 Token *label = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005812 bool dont_prepend = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005813 Token **params, *t, *tt;
5814 MMacro *m;
5815 Line *l, *ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005816 int i, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07005817 const char *mname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005818 int nparam = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005819
5820 t = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005821 t = skip_white(t);
5822 /* if (!tok_type(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07005823 if (!tok_type(t, TOK_ID) && !tok_type(t, TOK_LOCAL_MACRO))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005824 return 0;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005825 m = is_mmacro(t, &nparam, &params);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005826 if (m) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005827 mname = tok_text(t);
H. Peter Anvinc751e862008-06-09 10:18:45 -07005828 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005829 Token *last;
5830 /*
5831 * We have an id which isn't a macro call. We'll assume
5832 * it might be a label; we'll also check to see if a
5833 * colon follows it. Then, if there's another id after
5834 * that lot, we'll check it again for macro-hood.
5835 */
5836 label = last = t;
5837 t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005838 if (tok_white(t))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005839 last = t, t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005840 if (tok_is(t, ':')) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005841 dont_prepend = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005842 last = t, t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005843 if (tok_white(t))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005844 last = t, t = t->next;
5845 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005846 if (!tok_type(t, TOK_ID) || !(m = is_mmacro(t, &nparam, &params)))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005847 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005848 last->next = NULL;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005849 mname = tok_text(t);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005850 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005851 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005852
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005853 if (unlikely(mmacro_deadman.total >= nasm_limit[LIMIT_MMACROS] ||
5854 mmacro_deadman.levels >= nasm_limit[LIMIT_MACRO_LEVELS])) {
5855 if (!mmacro_deadman.triggered) {
5856 nasm_nonfatal("interminable multiline macro recursion");
5857 mmacro_deadman.triggered = true;
5858 }
5859 return 0;
5860 }
5861
5862 mmacro_deadman.total++;
5863 mmacro_deadman.levels++;
5864
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005865 /*
5866 * Fix up the parameters: this involves stripping leading and
5867 * trailing whitespace, then stripping braces if they are
5868 * present.
5869 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005870 nasm_newn(paramlen, nparam+1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005871
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005872 nasm_assert(params[nparam+1] == NULL);
5873
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005874 for (i = 1; (t = params[i]); i++) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005875 bool braced = false;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005876 int brace = 0;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005877 int white = 0;
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005878 bool comma = !m->plus || i < nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005879
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005880 t = skip_white(t);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005881 if (tok_is(t, '{')) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005882 t = t->next;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005883 brace = 1;
5884 braced = true;
5885 comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005886 }
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005887
5888 params[i] = t;
5889 for (; t; t = t->next) {
5890 if (tok_white(t)) {
5891 white++;
5892 continue;
5893 }
5894
5895 if (t->type == TOK_OTHER && t->len == 1) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005896 switch (t->text.a[0]) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005897 case ',':
5898 if (comma && !brace)
5899 goto endparam;
5900 break;
5901
5902 case '{':
5903 brace++;
5904 break;
5905
5906 case '}':
5907 brace--;
5908 if (braced && !brace) {
5909 paramlen[i] += white;
5910 goto endparam;
5911 }
5912 break;
5913
5914 default:
5915 break;
5916 }
5917 }
5918
5919 paramlen[i] += white + 1;
5920 white = 0;
5921 }
5922 endparam:
5923 ;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005924 }
5925
5926 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005927 * OK, we have a MMacro structure together with a set of
5928 * parameters. We must now go through the expansion and push
5929 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00005930 * parameter tokens and macro-local tokens doesn't get done
5931 * until the single-line macro substitution process; this is
5932 * because delaying them allows us to change the semantics
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005933 * later through %rotate and give the right semantics for
5934 * nested mmacros.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005935 *
5936 * First, push an end marker on to istk->expansion, mark this
5937 * macro as in progress, and set up its invocation-specific
5938 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005939 */
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005940 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005941 ll->next = istk->expansion;
5942 ll->finishes = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005943 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005944
5945 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005946 * Save the previous MMacro expansion in the case of
5947 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005948 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005949#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005950 if (m->max_depth && m->in_progress)
5951 push_mmacro(m);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005952#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005953
5954 m->in_progress ++;
5955 m->params = params;
5956 m->iline = tline;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005957 m->iname = nasm_strdup(mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005958 m->nparam = nparam;
5959 m->rotate = 0;
5960 m->paramlen = paramlen;
5961 m->unique = unique++;
5962 m->lineno = 0;
5963 m->condcnt = 0;
5964
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005965 m->mstk = istk->mstk;
5966 istk->mstk.mstk = istk->mstk.mmac = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005967
5968 list_for_each(l, m->expansion) {
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005969 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005970 ll->next = istk->expansion;
5971 istk->expansion = ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005972 ll->first = dup_tlist(l->first, NULL);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005973 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005974
5975 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005976 * If we had a label, and this macro definition does not include
5977 * a %00, push it on as the first line of, ot
H. Peter Anvineba20a72002-04-30 20:53:55 +00005978 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005979 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005980 if (label) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005981 /*
5982 * We had a label. If this macro contains an %00 parameter,
5983 * save the value as a special parameter (which is what it
5984 * is), otherwise push it as the first line of the macro
5985 * expansion.
5986 */
5987 if (m->capture_label) {
5988 params[0] = dup_Token(NULL, label);
5989 paramlen[0] = 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005990 free_tlist(startline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005991 } else {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005992 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005993 ll->finishes = NULL;
5994 ll->next = istk->expansion;
5995 istk->expansion = ll;
5996 ll->first = startline;
5997 if (!dont_prepend) {
5998 while (label->next)
5999 label = label->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07006000 label->next = tt = make_tok_char(NULL, ':');
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006001 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006002 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00006003 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006004
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07006005 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006006
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07006007 if (list_option('m') && !m->nolist)
6008 list_mmacro_call(m);
6009
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006010 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006011}
6012
H. Peter Anvin130736c2016-02-17 20:27:41 -08006013/*
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006014 * This function decides if an error message should be suppressed.
6015 * It will never be called with a severity level of ERR_FATAL or
6016 * higher.
H. Peter Anvin130736c2016-02-17 20:27:41 -08006017 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006018static bool pp_suppress_error(errflags severity)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02006019{
H. Peter Anvin130736c2016-02-17 20:27:41 -08006020 /*
6021 * If we're in a dead branch of IF or something like it, ignore the error.
6022 * However, because %else etc are evaluated in the state context
6023 * of the previous branch, errors might get lost:
6024 * %if 0 ... %else trailing garbage ... %endif
6025 * So %else etc should set the ERR_PP_PRECOND flag.
6026 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006027 if (istk && istk->conds &&
H. Peter Anvin130736c2016-02-17 20:27:41 -08006028 ((severity & ERR_PP_PRECOND) ?
6029 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07006030 !emitting(istk->conds->state)))
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006031 return true;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02006032
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006033 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00006034}
6035
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006036static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006037stdmac_file(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006038{
6039 (void)s;
6040 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006041 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006042
H. Peter Anvin8571f062019-09-23 16:40:03 -07006043 return make_tok_qstr(NULL, src_get_fname());
H. Peter Anvin8b262472019-02-26 14:00:54 -08006044}
6045
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006046static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006047stdmac_line(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006048{
6049 (void)s;
6050 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006051 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006052
H. Peter Anvin8571f062019-09-23 16:40:03 -07006053 return make_tok_num(NULL, src_get_linnum());
H. Peter Anvin8b262472019-02-26 14:00:54 -08006054}
6055
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006056static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006057stdmac_bits(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006058{
6059 (void)s;
6060 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006061 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006062
H. Peter Anvin8571f062019-09-23 16:40:03 -07006063 return make_tok_num(NULL, globalbits);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006064}
6065
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006066static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006067stdmac_ptr(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006068{
H. Peter Anvin8b262472019-02-26 14:00:54 -08006069 (void)s;
6070 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006071 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006072
6073 switch (globalbits) {
6074 case 16:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006075 return new_Token(NULL, TOK_ID, "word", 4);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006076 case 32:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006077 return new_Token(NULL, TOK_ID, "dword", 5);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006078 case 64:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006079 return new_Token(NULL, TOK_ID, "qword", 5);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006080 default:
6081 panic();
6082 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08006083}
6084
H. Peter Anvin8b262472019-02-26 14:00:54 -08006085/* Add magic standard macros */
6086struct magic_macros {
6087 const char *name;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006088 int nparam;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006089 ExpandSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006090};
6091static const struct magic_macros magic_macros[] =
6092{
H. Peter Anvind2354082019-08-27 16:38:48 -07006093 { "__?FILE?__", 0, stdmac_file },
6094 { "__?LINE?__", 0, stdmac_line },
6095 { "__?BITS?__", 0, stdmac_bits },
6096 { "__?PTR?__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08006097 { NULL, 0, NULL }
6098};
6099
6100static void pp_add_magic_stdmac(void)
6101{
6102 const struct magic_macros *m;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006103 SMacro tmpl;
6104
6105 nasm_zero(tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006106
6107 for (m = magic_macros; m->name; m++) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006108 tmpl.nparam = m->nparam;
6109 tmpl.expand = m->func;
6110 define_smacro(m->name, true, NULL, &tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006111 }
6112}
6113
H. Peter Anvin734b1882002-04-30 21:01:08 +00006114static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006115pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006116{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006117 int apass;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006118 struct Include *inc;
H. Peter Anvin7383b402008-09-24 10:20:40 -07006119
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006120 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006121 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07006122 nested_mac_count = 0;
6123 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07006124 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006125 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07006126 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006127 pp_mode = mode;
H. Peter Anvind2354082019-08-27 16:38:48 -07006128 do_aliases = true;
H. Peter Anvinf7606612016-07-13 14:23:48 -07006129
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07006130 if (!use_loaded)
6131 use_loaded = nasm_malloc(use_package_count * sizeof(bool));
6132 memset(use_loaded, 0, use_package_count * sizeof(bool));
6133
H. Peter Anvin6686de22019-08-10 05:33:14 -07006134 /* First set up the top level input file */
6135 nasm_new(istk);
6136 istk->fp = nasm_open_read(file, NF_TEXT);
6137 src_set(0, file);
6138 istk->lineinc = 1;
6139 if (!istk->fp)
6140 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
6141
6142 strlist_add(deplist, file);
6143
6144 /*
6145 * Set up the stdmac packages as a virtual include file,
6146 * indicated by a null file pointer.
6147 */
6148 nasm_new(inc);
6149 inc->next = istk;
6150 inc->fname = src_set_fname(NULL);
6151 inc->nolist = !list_option('b');
6152 istk = inc;
6153 lfmt->uplevel(LIST_INCLUDE, 0);
6154
H. Peter Anvin8b262472019-02-26 14:00:54 -08006155 pp_add_magic_stdmac();
6156
H. Peter Anvinf7606612016-07-13 14:23:48 -07006157 if (tasm_compatible_mode)
6158 pp_add_stdmac(nasm_stdmac_tasm);
6159
6160 pp_add_stdmac(nasm_stdmac_nasm);
6161 pp_add_stdmac(nasm_stdmac_version);
6162
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006163 if (extrastdmac)
6164 pp_add_stdmac(extrastdmac);
6165
H. Peter Anvinf7606612016-07-13 14:23:48 -07006166 stdmacpos = stdmacros[0];
6167 stdmacnext = &stdmacros[1];
6168
H. Peter Anvind2456592008-06-19 15:04:18 -07006169 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006170
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006171 /*
H. Peter Anvind2354082019-08-27 16:38:48 -07006172 * Define the __?PASS?__ macro. This is defined here unlike all the
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07006173 * other builtins, because it is special -- it varies between
6174 * passes -- but there is really no particular reason to make it
6175 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006176 *
6177 * 0 = dependencies only
6178 * 1 = preparatory passes
6179 * 2 = final pass
6180 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006181 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006182 switch (mode) {
6183 case PP_NORMAL:
6184 apass = pass_final() ? 2 : 1;
6185 break;
6186 case PP_DEPS:
6187 apass = 0;
6188 break;
6189 case PP_PREPROC:
6190 apass = 3;
6191 break;
6192 default:
6193 panic();
6194 }
6195
H. Peter Anvin8571f062019-09-23 16:40:03 -07006196 define_smacro("__?PASS?__", true, make_tok_num(NULL, apass), NULL);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006197}
6198
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006199static void pp_init(void)
6200{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006201}
6202
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006203/*
6204 * Get a line of tokens. If we popped the macro expansion/include stack,
6205 * we return a pointer to the dummy token tok_pop; at that point if
6206 * istk is NULL then we have reached end of input;
6207 */
6208static Token tok_pop; /* Dummy token placeholder */
6209
6210static Token *pp_tokline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006211{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006212 while (true) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006213 Line *l = istk->expansion;
6214 Token *tline = NULL;
6215 Token *dtline;
6216
H. Peter Anvine2c80182005-01-15 22:15:51 +00006217 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006218 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00006219 * buffer or from the input file.
6220 */
6221 tline = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006222 while (l && l->finishes) {
6223 MMacro *fm = l->finishes;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006224
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006225 if (!fm->name && fm->in_progress > 1) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006226 /*
6227 * This is a macro-end marker for a macro with no
6228 * name, which means it's not really a macro at all
6229 * but a %rep block, and the `in_progress' field is
6230 * more than 1, meaning that we still need to
6231 * repeat. (1 means the natural last repetition; 0
6232 * means termination by %exitrep.) We have
6233 * therefore expanded up to the %endrep, and must
6234 * push the whole block on to the expansion buffer
6235 * again. We don't bother to remove the macro-end
6236 * marker: we'd only have to generate another one
6237 * if we did.
6238 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006239 fm->in_progress--;
6240 list_for_each(l, fm->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006241 Token *t, *tt, **tail;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006242 Line *ll;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006243
Chang S. Baebec812f2020-02-07 15:49:38 -08006244 istk->mstk.mstk->lineno = 0;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006245 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006246 ll->next = istk->expansion;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006247 tail = &ll->first;
6248
6249 list_for_each(t, l->first) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006250 if (t->len) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006251 tt = *tail = dup_Token(NULL, t);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006252 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006253 }
6254 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006255 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006256 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006257 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006258 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006259 MMacro *m = istk->mstk.mstk;
6260
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006261 /*
6262 * Check whether a `%rep' was started and not ended
6263 * within this macro expansion. This can happen and
6264 * should be detected. It's a fatal error because
6265 * I'm too confused to work out how to recover
6266 * sensibly from it.
6267 */
6268 if (defining) {
6269 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07006270 nasm_panic("defining with name in expansion");
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006271 else if (m->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07006272 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006273 " expansion of macro `%s'", m->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006274 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006275
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006276 /*
6277 * FIXME: investigate the relationship at this point between
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006278 * istk->mstk.mstk and fm
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006279 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006280 istk->mstk = m->mstk;
6281 if (m->name) {
6282 /*
6283 * This was a real macro call, not a %rep, and
6284 * therefore the parameter information needs to
6285 * be freed and the iteration count/nesting
6286 * depth adjusted.
6287 */
6288
6289 if (!--mmacro_deadman.levels) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006290 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006291 * If all mmacro processing done,
6292 * clear all counters and the deadman
6293 * message trigger.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006294 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006295 nasm_zero(mmacro_deadman); /* Clear all counters */
Adam Majer91e72402017-07-25 10:42:01 +02006296 }
6297
Adam Majer91e72402017-07-25 10:42:01 +02006298#if 0
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006299 if (m->prev) {
6300 pop_mmacro(m);
6301 fm->in_progress --;
6302 } else
Adam Majer91e72402017-07-25 10:42:01 +02006303#endif
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006304 {
6305 nasm_free(m->params);
6306 free_tlist(m->iline);
6307 nasm_free(m->paramlen);
6308 fm->in_progress = 0;
6309 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006310 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006311
6312 /*
6313 * FIXME It is incorrect to always free_mmacro here.
6314 * It leads to usage-after-free.
6315 *
6316 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
6317 */
6318#if 0
6319 else
6320 free_mmacro(m);
6321#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006322 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006323 istk->expansion = l->next;
6324 nasm_free(l);
6325 lfmt->downlevel(LIST_MACRO);
6326 return &tok_pop;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006327 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006328
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006329 do { /* until we get a line we can use */
6330 char *line;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006331
6332 if (istk->expansion) { /* from a macro expansion */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006333 Line *l = istk->expansion;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006334 int32_t lineno;
6335
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006336 if (istk->mstk.mstk) {
6337 istk->mstk.mstk->lineno++;
6338 if (istk->mstk.mstk->fname)
6339 lineno = istk->mstk.mstk->lineno +
6340 istk->mstk.mstk->xline;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006341 else
6342 lineno = 0; /* Defined at init time or builtin */
6343 } else {
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006344 lineno = src_get_linnum();
H. Peter Anvin6686de22019-08-10 05:33:14 -07006345 }
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006346
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006347 tline = l->first;
6348 istk->expansion = l->next;
6349 nasm_free(l);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006350
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006351 line = detoken(tline, false);
H. Peter Anvin6686de22019-08-10 05:33:14 -07006352 if (!istk->nolist)
6353 lfmt->line(LIST_MACRO, lineno, line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006354 nasm_free(line);
6355 } else if ((line = read_line())) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006356 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006357 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006358 nasm_free(line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006359 } else {
6360 /*
6361 * The current file has ended; work down the istk
6362 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00006363 Include *i = istk;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006364 if (i->fp)
6365 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006366 if (i->conds) {
6367 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07006368 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006369 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00006370 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07006371 if (i->next)
6372 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006373 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08006374 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006375 nasm_free(i);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006376 return &tok_pop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006377 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006378 } while (0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006379
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006380 /*
6381 * We must expand MMacro parameters and MMacro-local labels
6382 * _before_ we plunge into directive processing, to cope
6383 * with things like `%define something %1' such as STRUC
6384 * uses. Unless we're _defining_ a MMacro, in which case
6385 * those tokens should be left alone to go into the
6386 * definition; and unless we're in a non-emitting
6387 * condition, in which case we don't want to meddle with
6388 * anything.
6389 */
6390 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006391 && !(istk->mstk.mstk && !istk->mstk.mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006392 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006393 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006394
H. Peter Anvine2c80182005-01-15 22:15:51 +00006395 /*
6396 * Check the line to see if it's a preprocessor directive.
6397 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006398 if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) {
6399 if (dtline)
6400 return dtline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006401 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006402 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006403 * We're defining a multi-line macro. We emit nothing
6404 * at all, and just
6405 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00006406 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006407 MMacro *mmac = defining->dstk.mmac;
6408
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006409 Line *l = nasm_malloc(sizeof(Line));
6410 l->next = defining->expansion;
6411 l->first = tline;
6412 l->finishes = NULL;
6413 defining->expansion = l;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006414
6415 /*
6416 * Remember if this mmacro expansion contains %00:
6417 * if it does, we will have to handle leading labels
6418 * specially.
6419 */
6420 if (mmac) {
6421 const Token *t;
6422 list_for_each(t, tline) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006423 if (!memcmp(t->text.a, "%00", 4))
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006424 mmac->capture_label = true;
6425 }
6426 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006427 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006428 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006429 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00006430 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006431 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00006432 * directive so we keep our place correctly.
6433 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006434 free_tlist(tline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006435 } else if (istk->mstk.mstk && !istk->mstk.mstk->in_progress) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006436 /*
6437 * We're in a %rep block which has been terminated, so
6438 * we're walking through to the %endrep without
6439 * emitting anything. Emit nothing at all, not even a
6440 * blank line: when we emerge from the %rep block we'll
6441 * give a line-number directive so we keep our place
6442 * correctly.
6443 */
6444 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006445 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006446 tline = expand_smacro(tline);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006447 if (!expand_mmacro(tline))
6448 return tline;
6449 }
6450 }
6451}
6452
6453static char *pp_getline(void)
6454{
6455 char *line = NULL;
6456 Token *tline;
6457
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006458 while (true) {
6459 tline = pp_tokline();
6460 if (tline == &tok_pop) {
6461 /*
6462 * We popped the macro/include stack. If istk is empty,
6463 * we are at end of input, otherwise just loop back.
6464 */
6465 if (!istk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006466 break;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006467 } else {
6468 /*
6469 * De-tokenize the line and emit it.
6470 */
6471 line = detoken(tline, true);
6472 free_tlist(tline);
6473 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006474 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006475 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006476
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006477 if (list_option('e') && istk && !istk->nolist && line && line[0]) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006478 char *buf = nasm_strcat(" ;;; ", line);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006479 lfmt->line(LIST_MACRO, -1, buf);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006480 nasm_free(buf);
6481 }
6482
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006483 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006484}
6485
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006486static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006487{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006488 if (defining) {
6489 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006490 nasm_nonfatal("end of file while still defining macro `%s'",
6491 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006492 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006493 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006494 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006495
6496 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006497 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006498 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08006499
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006500 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006501 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07006502 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006503 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006504 Include *i = istk;
6505 istk = istk->next;
6506 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03006507 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006508 }
6509 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006510 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07006511 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006512}
6513
6514static void pp_cleanup_session(void)
6515{
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07006516 nasm_free(use_loaded);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006517 free_llist(predef);
6518 predef = NULL;
6519 delete_Blocks();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006520 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006521}
6522
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006523static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006524{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006525 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006526}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00006527
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006528static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006529{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006530 Token *inc, *space, *name;
6531 Line *l;
6532
H. Peter Anvin734b1882002-04-30 21:01:08 +00006533 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006534 space = new_White(name);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006535 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006536
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006537 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006538 l->next = predef;
6539 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006540 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006541 predef = l;
6542}
6543
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006544static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006545{
6546 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006547 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00006548 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006549
6550 equals = strchr(definition, '=');
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006551 space = new_White(NULL);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006552 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006553 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006554 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00006555 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006556 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006557 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006558
H. Peter Anvin8571f062019-09-23 16:40:03 -07006559 /* We can't predefine a TOK_LOCAL_MACRO for obvious reasons... */
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006560 if (space->next->type != TOK_PREPROC_ID &&
6561 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08006562 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006563
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006564 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006565 l->next = predef;
6566 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006567 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006568 predef = l;
6569}
6570
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006571static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00006572{
6573 Token *def, *space;
6574 Line *l;
6575
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006576 space = new_White(NULL);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006577 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006578 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00006579
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006580 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00006581 l->next = predef;
6582 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006583 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00006584 predef = l;
6585}
6586
H. Peter Anvin05990342018-06-11 13:32:42 -07006587/* Insert an early preprocessor command that doesn't need special handling */
6588static void pp_pre_command(const char *what, char *string)
6589{
6590 char *cmd;
6591 Token *def, *space;
6592 Line *l;
6593
6594 def = tokenize(string);
6595 if (what) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006596 space = new_White(def);
H. Peter Anvin8571f062019-09-23 16:40:03 -07006597 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
6598 def = new_Token(space, TOK_PREPROC_ID, cmd, nasm_last_string_len());
6599 nasm_free(cmd);
H. Peter Anvin05990342018-06-11 13:32:42 -07006600 }
6601
6602 l = nasm_malloc(sizeof(Line));
6603 l->next = predef;
6604 l->first = def;
6605 l->finishes = NULL;
6606 predef = l;
6607}
6608
H. Peter Anvinf7606612016-07-13 14:23:48 -07006609static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006610{
H. Peter Anvinf7606612016-07-13 14:23:48 -07006611 macros_t **mp;
6612
6613 /* Find the end of the list and avoid duplicates */
6614 for (mp = stdmacros; *mp; mp++) {
6615 if (*mp == macros)
6616 return; /* Nothing to do */
6617 }
6618
6619 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
6620
6621 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00006622}
6623
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006624static void pp_extra_stdmac(macros_t *macros)
6625{
6626 extrastdmac = macros;
6627}
6628
H. Peter Anvin8571f062019-09-23 16:40:03 -07006629/* Create a numeric token */
6630static Token *make_tok_num(Token *next, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006631{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04006632 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08006633 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvin8571f062019-09-23 16:40:03 -07006634 return new_Token(next, TOK_NUMBER, numbuf, len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006635}
6636
H. Peter Anvin8571f062019-09-23 16:40:03 -07006637/* Create a quoted string token */
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07006638static Token *make_tok_qstr_len(Token *next, const char *str, size_t len)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006639{
H. Peter Anvin8571f062019-09-23 16:40:03 -07006640 char *p = nasm_quote(str, &len);
6641 return new_Token_free(next, TOK_STRING, p, len);
6642}
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07006643static Token *make_tok_qstr(Token *next, const char *str)
6644{
6645 return make_tok_qstr_len(next, str, strlen(str));
6646}
H. Peter Anvin8571f062019-09-23 16:40:03 -07006647
6648/* Create a single-character operator token */
6649static Token *make_tok_char(Token *next, char op)
6650{
6651 Token *t = new_Token(next, TOK_OTHER, NULL, 1);
6652 t->text.a[0] = op;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006653 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006654}
6655
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006656static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07006657{
6658 if (!m)
6659 return;
6660
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006661 /* We need to print the mstk.mmac list in reverse order */
6662 pp_list_one_macro(m->mstk.mmac, severity);
H. Peter Anvin37368952016-05-09 14:10:32 -07006663
6664 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07006665 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08006666 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07006667 }
6668}
6669
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006670static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006671{
H. Peter Anvinddb29062018-12-11 00:06:29 -08006672 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006673
H. Peter Anvinddb29062018-12-11 00:06:29 -08006674 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
6675 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006676
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03006677 if (istk)
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006678 pp_list_one_macro(istk->mstk.mmac, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006679
H. Peter Anvinddb29062018-12-11 00:06:29 -08006680 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006681}
6682
H. Peter Anvine7469712016-02-18 02:20:59 -08006683const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006684 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006685 pp_reset,
6686 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006687 pp_cleanup_pass,
6688 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006689 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006690 pp_pre_define,
6691 pp_pre_undefine,
6692 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07006693 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006694 pp_include_path,
6695 pp_error_list_macros,
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006696 pp_suppress_error
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006697};