blob: e3014f3ea1fbd79b4996c99d597befccefee2f7a [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002 *
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07003 * Copyright 1996-2019 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000010 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070011 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * preproc.c macro preprocessor for the Netwide Assembler
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000036 */
37
H. Peter Anvin4836e332002-04-30 20:56:43 +000038/* Typical flow of text through preproc
39 *
Keith Kaniosb7a89542007-04-12 02:40:54 +000040 * pp_getline gets tokenized lines, either
H. Peter Anvin4836e332002-04-30 20:56:43 +000041 *
42 * from a macro expansion
43 *
44 * or
45 * {
46 * read_line gets raw text from stdmacpos, or predef, or current input file
Keith Kaniosb7a89542007-04-12 02:40:54 +000047 * tokenize converts to tokens
H. Peter Anvin4836e332002-04-30 20:56:43 +000048 * }
49 *
50 * expand_mmac_params is used to expand %1 etc., unless a macro is being
51 * defined or a false conditional is being processed
52 * (%0, %1, %+1, %-1, %%foo
53 *
54 * do_directive checks for directives
55 *
56 * expand_smacro is used to expand single line macros
57 *
58 * expand_mmacro is used to expand multi-line macros
59 *
60 * detoken is used to convert the line back to text
61 */
H. Peter Anvineba20a72002-04-30 20:53:55 +000062
H. Peter Anvinfe501952007-10-02 21:53:51 -070063#include "compiler.h"
64
H. Peter Anvinc2f3f262018-12-27 12:37:25 -080065#include "nctype.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000066
67#include "nasm.h"
68#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080069#include "error.h"
H. Peter Anvin4169a472007-09-12 01:29:43 +000070#include "preproc.h"
H. Peter Anvin97a23472007-09-16 17:57:25 -070071#include "hashtbl.h"
H. Peter Anvin8cad14b2008-06-01 17:23:51 -070072#include "quote.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070073#include "stdscan.h"
H. Peter Anvindbb640b2009-07-18 18:57:16 -070074#include "eval.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070075#include "tokens.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070076#include "tables.h"
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -080077#include "listing.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000078
79typedef struct SMacro SMacro;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080080typedef struct MMacro MMacro;
81typedef struct MMacroInvocation MMacroInvocation;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000082typedef struct Context Context;
83typedef struct Token Token;
84typedef struct Line Line;
85typedef struct Include Include;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080086typedef struct Cond Cond;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000087
88/*
H. Peter 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 Anvin169ac7c2016-09-25 17:08:05 -07002244 if (!path) {
2245 if (omode == INC_NEEDED)
H. Peter Anvinc5136902018-06-15 18:20:17 -07002246 nasm_fatal("unable to open include file `%s'", file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002247 } else {
2248 if (!fp && omode != INC_PROBE)
2249 fp = nasm_open_read(path, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002250 }
2251
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002252 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002253 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002254
2255 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002256}
2257
2258/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07002259 * Opens an include or input file. Public version, for use by modules
2260 * that get a file:lineno pair and need to look at the file again
2261 * (e.g. the CodeView debug backend). Returns NULL on failure.
2262 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07002263FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07002264{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002265 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07002266}
2267
2268/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002269 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002270 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002271 * return true if _any_ single-line macro of that name is defined.
2272 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002273 * `nparam' or no parameters is defined.
2274 *
2275 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002276 * defined, or nparam is -1, the address of the definition structure
2277 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002278 * is NULL, no action will be taken regarding its contents, and no
2279 * error will occur.
2280 *
2281 * Note that this is also called with nparam zero to resolve
2282 * `ifdef'.
2283 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002284static bool
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002285smacro_defined(Context *ctx, const char *name, int nparam, SMacro **defn,
H. Peter Anvind2354082019-08-27 16:38:48 -07002286 bool nocase, bool find_alias)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002287{
H. Peter Anvin166c2472008-05-28 12:28:58 -07002288 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002289 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002290
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002291 smtbl = ctx ? &ctx->localmac : &smacros;
H. Peter Anvind2354082019-08-27 16:38:48 -07002292
2293restart:
H. Peter Anvin166c2472008-05-28 12:28:58 -07002294 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002295
H. Peter Anvine2c80182005-01-15 22:15:51 +00002296 while (m) {
2297 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002298 (nparam <= 0 || m->nparam == 0 || nparam == m->nparam ||
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002299 (m->greedy && nparam >= m->nparam-1))) {
H. Peter Anvind2354082019-08-27 16:38:48 -07002300 if (m->alias && !find_alias) {
2301 if (do_aliases) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002302 name = tok_text(m->expansion);
H. Peter Anvind2354082019-08-27 16:38:48 -07002303 goto restart;
2304 } else {
2305 continue;
2306 }
2307 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002308 if (defn) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002309 *defn = (nparam == m->nparam || nparam == -1) ? m : NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002310 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002311 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002312 }
2313 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002314 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002315
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002316 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002317}
2318
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002319/* param should be a natural number [0; INT_MAX] */
2320static int read_param_count(const char *str)
2321{
2322 int result;
2323 bool err;
2324
2325 result = readnum(str, &err);
2326 if (result < 0 || result > INT_MAX) {
2327 result = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002328 nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]",
2329 str, 0, INT_MAX);
2330 } else if (err)
2331 nasm_nonfatal("unable to parse parameter count `%s'", str);
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002332 return result;
2333}
2334
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002335/*
2336 * Count and mark off the parameters in a multi-line macro call.
2337 * This is called both from within the multi-line macro expansion
2338 * code, and also to mark off the default parameters when provided
2339 * in a %macro definition line.
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002340 *
2341 * Note that we need space in the params array for parameter 0 being
2342 * a possible captured label as well as the final NULL.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002343 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002344static void count_mmac_params(Token * t, int *nparamp, Token ***paramsp)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002345{
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07002346 int paramsize;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002347 int nparam = 0;
2348 Token **params;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002349
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002350 paramsize = PARAM_DELTA;
2351 params = nasm_malloc(paramsize * sizeof(*params));
2352 params[0] = NULL;
2353
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002354 while ((t = skip_white(t))) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002355 /* 2 slots for captured label and NULL */
2356 if (nparam+2 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002357 paramsize += PARAM_DELTA;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002358 params = nasm_realloc(params, sizeof(*params) * paramsize);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002359 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002360 params[++nparam] = t;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07002361 if (tok_is(t, '{')) {
2362 int brace = 1;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002363 while (brace && (t = t->next)) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07002364 brace += tok_is(t, '{');
2365 brace -= tok_is(t, '}');
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08002366 }
2367
2368 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002369 /*
2370 * Now we've found the closing brace, look further
2371 * for the comma.
2372 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002373 t = skip_white(t->next);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07002374 if (tok_isnt(t, ','))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002375 nasm_nonfatal("braces do not enclose all of macro parameter");
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07002376 } else {
2377 nasm_nonfatal("expecting closing brace in macro parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002378 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08002379 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002380
2381 while (tok_isnt(t, ','))
2382 t = t->next;
2383
2384 if (t) /* got a comma */
2385 t = t->next; /* eat the comma */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002386 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002387
2388 params[nparam+1] = NULL;
2389 *paramsp = params;
2390 *nparamp = nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002391}
2392
2393/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002394 * Determine whether one of the various `if' conditions is true or
2395 * not.
2396 *
2397 * We must free the tline we get passed.
2398 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002399static enum cond_state if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002400{
H. Peter Anvin70055962007-10-11 00:05:31 -07002401 bool j;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002402 Token *t, *tt, *origline;
2403 struct ppscan pps;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002404 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002405 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002406 enum pp_token_type needtype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002407 const char *dname = pp_directives[ct];
2408 bool casesense = true;
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002409 enum preproc_token cond = PP_COND(ct);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002410
2411 origline = tline;
2412
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002413 switch (cond) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002414 case PP_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002415 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02002416 while (true) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002417 tline = skip_white(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02002418 if (!tline)
2419 break;
2420 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002421 nasm_nonfatal("`%s' expects context identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002422 dname);
2423 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002424 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002425 if (cstk && cstk->name && !nasm_stricmp(tok_text(tline), cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002426 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002427 tline = tline->next;
2428 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002429 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002430
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002431 case PP_IFDEF:
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002432 case PP_IFDEFALIAS:
2433 {
2434 bool alias = cond == PP_IFDEFALIAS;
2435 SMacro *smac;
2436 Context *ctx;
2437 const char *mname;
2438
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002439 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002440 while (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002441 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002442 if (!tline || (tline->type != TOK_ID &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07002443 tline->type != TOK_LOCAL_MACRO)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002444 nasm_nonfatal("`%s' expects macro identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002445 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002446 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002447 }
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002448
2449 mname = tok_text(tline);
2450 ctx = get_ctx(mname, &mname);
Chang S. Baec52aff42020-04-20 21:43:44 +00002451 if (smacro_defined(ctx, mname, 0, &smac, true, alias) && smac
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002452 && smac->alias == alias) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002453 j = true;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002454 break;
2455 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002456 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002457 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002458 break;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002459 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002460
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002461 case PP_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002462 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002463 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002464 while (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002465 tline = skip_white(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002466 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002467 tline->type != TOK_STRING &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07002468 tline->type != TOK_INTERNAL_STRING &&
2469 tline->type != TOK_ENVIRON)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002470 nasm_nonfatal("`%s' expects environment variable names",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002471 dname);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002472 goto fail;
2473 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002474
2475 j |= !!pp_getenv(tline, false);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002476 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002477 }
2478 break;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002479
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002480 case PP_IFIDNI:
2481 casesense = false;
2482 /* fall through */
2483 case PP_IFIDN:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002484 tline = expand_smacro(tline);
2485 t = tt = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002486 while (tok_isnt(tt, ','))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002487 tt = tt->next;
2488 if (!tt) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002489 nasm_nonfatal("`%s' expects two comma-separated arguments",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002490 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002491 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002492 }
2493 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002494 j = true; /* assume equality unless proved not */
H. Peter Anvin8571f062019-09-23 16:40:03 -07002495 while (tok_isnt(t, ',') && tt) {
2496 unsigned int l1, l2;
2497 const char *t1, *t2;
2498
2499 if (tok_is(tt, ',')) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002500 nasm_nonfatal("`%s': more than one comma on line",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002501 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002502 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002503 }
2504 if (t->type == TOK_WHITESPACE) {
2505 t = t->next;
2506 continue;
2507 }
2508 if (tt->type == TOK_WHITESPACE) {
2509 tt = tt->next;
2510 continue;
2511 }
2512 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002513 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002514 break;
2515 }
H. Peter Anvind2456592008-06-19 15:04:18 -07002516
H. Peter Anvin8571f062019-09-23 16:40:03 -07002517 t1 = unquote_token(t);
2518 t2 = unquote_token(tt);
2519 l1 = t->len;
2520 l2 = tt->len;
2521
2522 if (l1 != l2 || mmemcmp(t1, t2, l1, casesense)) {
2523 j = false;
2524 break;
2525 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00002526
H. Peter Anvine2c80182005-01-15 22:15:51 +00002527 t = t->next;
2528 tt = tt->next;
2529 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002530 if (!tok_is(t, ',') || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002531 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002532 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002533
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002534 case PP_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04002535 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002536 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002537 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00002538
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002539 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002540 tline = expand_id(tline);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002541 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002542 nasm_nonfatal("`%s' expects a macro name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002543 goto fail;
2544 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002545 nasm_zero(searching);
H. Peter Anvin8571f062019-09-23 16:40:03 -07002546 searching.name = dup_text(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002547 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002548 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002549 searching.nparam_max = INT_MAX;
2550 tline = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002551 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002552 if (!tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002553 } else if (!tok_type(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002554 nasm_nonfatal("`%s' expects a parameter count or nothing",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002555 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002556 } else {
2557 searching.nparam_min = searching.nparam_max =
H. Peter Anvin8571f062019-09-23 16:40:03 -07002558 read_param_count(tok_text(tline));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002559 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002560 if (tline && tok_is(tline->next, '-')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002561 tline = tline->next->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002562 if (tok_is(tline, '*'))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002563 searching.nparam_max = INT_MAX;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002564 else if (!tok_type(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002565 nasm_nonfatal("`%s' expects a parameter count after `-'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002566 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002567 else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002568 searching.nparam_max = read_param_count(tok_text(tline));
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002569 if (searching.nparam_min > searching.nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002570 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002571 searching.nparam_max = searching.nparam_min;
2572 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002573 }
2574 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002575 if (tline && tok_is(tline->next, '+')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002576 tline = tline->next;
2577 searching.plus = true;
2578 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002579 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
2580 while (mmac) {
2581 if (!strcmp(mmac->name, searching.name) &&
2582 (mmac->nparam_min <= searching.nparam_max
2583 || searching.plus)
2584 && (searching.nparam_min <= mmac->nparam_max
2585 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002586 found = true;
2587 break;
2588 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002589 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002590 }
2591 if (tline && tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002592 nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002593 nasm_free(searching.name);
2594 j = found;
2595 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002596 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002597
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002598 case PP_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002599 needtype = TOK_ID;
2600 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002601 case PP_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002602 needtype = TOK_NUMBER;
2603 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002604 case PP_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002605 needtype = TOK_STRING;
2606 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002607
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002608iftype:
2609 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002610
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002611 while (tok_white(t) ||
2612 (needtype == TOK_NUMBER && (tok_is(t, '-') | tok_is(t, '+'))))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002613 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002614
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002615 j = tok_type(t, needtype);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002616 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002617
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002618 case PP_IFTOKEN:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002619 tline = expand_smacro(tline);
2620 t = skip_white(tline);
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002621
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002622 j = false;
2623 if (t) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002624 t = skip_white(t->next); /* Skip the actual token + whitespace */
2625 j = !t;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002626 }
2627 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002628
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002629 case PP_IFEMPTY:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002630 tline = expand_smacro(tline);
2631 t = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002632 j = !t; /* Should be empty */
2633 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002634
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002635 case PP_IF:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002636 pps.tptr = tline = expand_smacro(tline);
2637 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002638 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002639 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002640 if (!evalresult)
2641 return -1;
2642 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002643 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002644 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002645 nasm_nonfatal("non-constant value given to `%s'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002646 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002647 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002648 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002649 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002650 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002651
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002652 case PP_IFUSING:
2653 case PP_IFUSABLE:
2654 {
2655 const struct use_package *pkg;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002656 const char *name;
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002657
H. Peter Anvin8571f062019-09-23 16:40:03 -07002658 pkg = get_use_pkg(tline, dname, &name);
2659 if (!name)
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002660 goto fail;
2661
2662 j = pkg && ((cond == PP_IFUSABLE) | use_loaded[pkg->index]);
2663 break;
2664 }
2665
H. Peter Anvine2c80182005-01-15 22:15:51 +00002666 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002667 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002668 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002669 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002670
2671 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002672 return (j ^ PP_COND_NEGATIVE(ct)) ? COND_IF_TRUE : COND_IF_FALSE;
H. Peter Anvin70653092007-10-19 14:42:29 -07002673
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002674fail:
2675 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002676 return COND_NEVER;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002677}
2678
2679/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002680 * Default smacro expansion routine: just returns a copy of the
2681 * expansion list.
2682 */
2683static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002684smacro_expand_default(const SMacro *s, Token **params, int nparams)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002685{
2686 (void)params;
2687 (void)nparams;
2688
2689 return dup_tlist(s->expansion, NULL);
2690}
2691
2692/*
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002693 * Emit a macro defintion or undef to the listing file, if
2694 * desired. This is similar to detoken(), but it handles the reverse
2695 * expansion list, does not expand %! or local variable tokens, and
2696 * does some special handling for macro parameters.
2697 */
2698static void
2699list_smacro_def(enum preproc_token op, const Context *ctx, const SMacro *m)
2700{
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002701 Token *t;
2702 size_t namelen, size;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002703 char *def, *p;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002704 char *context_prefix = NULL;
2705 size_t context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002706
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002707 namelen = strlen(m->name);
2708 size = namelen + 2; /* Include room for space after name + NUL */
2709
2710 if (ctx) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07002711 int context_depth = cstk->depth - ctx->depth + 1;
2712 context_prefix =
2713 nasm_asprintf("[%s::%"PRIu64"] %%%-*s",
2714 ctx->name ? ctx->name : "",
2715 ctx->number, context_depth, "");
2716
2717 context_len = nasm_last_string_len();
2718 memset(context_prefix + context_len - context_depth,
2719 '$', context_depth);
2720 size += context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002721 }
2722
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002723 list_for_each(t, m->expansion)
H. Peter Anvin8571f062019-09-23 16:40:03 -07002724 size += t->len;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002725
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002726 if (m->nparam) {
2727 /*
2728 * Space for ( and either , or ) around each
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002729 * parameter, plus up to 4 flags.
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002730 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002731 int i;
2732
2733 size += 1 + 4 * m->nparam;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002734 for (i = 0; i < m->nparam; i++)
H. Peter Anvin8571f062019-09-23 16:40:03 -07002735 size += m->params[i].name.len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002736 }
2737
2738 def = nasm_malloc(size);
2739 p = def+size;
2740 *--p = '\0';
2741
2742 list_for_each(t, m->expansion) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002743 p -= t->len;
2744 memcpy(p, tok_text(t), t->len);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002745 }
2746
2747 *--p = ' ';
2748
2749 if (m->nparam) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002750 int i;
2751
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002752 *--p = ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002753 for (i = m->nparam-1; i >= 0; i--) {
2754 enum sparmflags flags = m->params[i].flags;
2755 if (flags & SPARM_GREEDY)
2756 *--p = '+';
H. Peter Anvin8571f062019-09-23 16:40:03 -07002757 p -= m->params[i].name.len;
2758 memcpy(p, tok_text(&m->params[i].name), m->params[i].name.len);
2759
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002760 if (flags & SPARM_NOSTRIP)
2761 *--p = '!';
2762 if (flags & SPARM_STR)
2763 *--p = '&';
2764 if (flags & SPARM_EVAL)
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002765 *--p = '=';
2766 *--p = ',';
2767 }
2768 *p = '('; /* First parameter starts with ( not , */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002769 }
2770
2771 p -= namelen;
2772 memcpy(p, m->name, namelen);
2773
H. Peter Anvin6686de22019-08-10 05:33:14 -07002774 if (context_prefix) {
2775 p -= context_len;
2776 memcpy(p, context_prefix, context_len);
2777 nasm_free(context_prefix);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002778 }
2779
2780 nasm_listmsg("%s %s", pp_directives[op], p);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002781 nasm_free(def);
H. Peter Anvin6686de22019-08-10 05:33:14 -07002782}
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002783
2784/*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002785 * Parse smacro arguments, return argument count. If the tmpl argument
2786 * is set, set the nparam, greedy and params field in the template.
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002787 * *tpp is updated to point to the pointer to the first token after the
2788 * prototype.
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002789 *
2790 * The text values from any argument tokens are "stolen" and the
2791 * corresponding text fields set to NULL.
2792 */
2793static int parse_smacro_template(Token ***tpp, SMacro *tmpl)
2794{
2795 int nparam = 0;
2796 enum sparmflags flags;
2797 struct smac_param *params = NULL;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002798 bool err, done;
2799 bool greedy = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002800 Token **tn = *tpp;
2801 Token *t = *tn;
2802 Token *name;
2803
H. Peter Anvin (Intel)d4607842019-08-20 16:19:37 -07002804 /*
2805 * DO NOT skip whitespace here, or we won't be able to distinguish:
2806 *
2807 * %define foo (a,b) ; no arguments, (a,b) is the expansion
2808 * %define bar(a,b) ; two arguments, empty expansion
2809 *
2810 * This ambiguity was inherited from C.
2811 */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002812
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002813 if (!tok_is(t, '('))
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002814 goto finish;
2815
2816 if (tmpl) {
2817 Token *tx = t;
2818 Token **txpp = &tx;
2819 int sparam;
2820
2821 /* Count parameters first */
2822 sparam = parse_smacro_template(&txpp, NULL);
2823 if (!sparam)
2824 goto finish; /* No parameters, we're done */
2825 nasm_newn(params, sparam);
2826 }
2827
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002828 /* Skip leading paren */
2829 tn = &t->next;
2830 t = *tn;
2831
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002832 name = NULL;
2833 flags = 0;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002834 err = done = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002835
2836 while (!done) {
2837 if (!t || !t->type) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002838 if (name || flags)
2839 nasm_nonfatal("`)' expected to terminate macro template");
2840 else
2841 nasm_nonfatal("parameter identifier expected");
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002842 break;
2843 }
2844
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002845 switch (t->type) {
2846 case TOK_ID:
2847 if (name)
2848 goto bad;
2849 name = t;
2850 break;
2851
2852 case TOK_OTHER:
H. Peter Anvin8571f062019-09-23 16:40:03 -07002853 if (t->len != 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002854 goto bad;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002855 switch (t->text.a[0]) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002856 case '=':
2857 flags |= SPARM_EVAL;
2858 break;
2859 case '&':
2860 flags |= SPARM_STR;
2861 break;
2862 case '!':
2863 flags |= SPARM_NOSTRIP;
2864 break;
2865 case '+':
2866 flags |= SPARM_GREEDY;
2867 greedy = true;
2868 break;
2869 case ',':
2870 if (greedy)
2871 nasm_nonfatal("greedy parameter must be last");
2872 /* fall through */
2873 case ')':
2874 if (params) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002875 if (name)
2876 steal_Token(&params[nparam].name, name);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002877 params[nparam].flags = flags;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002878 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002879 nparam++;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002880 name = NULL;
2881 flags = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002882 done = t->text.a[0] == ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002883 break;
2884 default:
2885 goto bad;
2886 }
2887 break;
2888
2889 case TOK_WHITESPACE:
2890 break;
2891
2892 default:
2893 bad:
2894 if (!err) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002895 nasm_nonfatal("garbage `%s' in macro parameter list", tok_text(t));
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002896 err = true;
2897 }
2898 break;
2899 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002900
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002901 tn = &t->next;
2902 t = *tn;
2903 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002904
2905finish:
2906 while (t && t->type == TOK_WHITESPACE) {
2907 tn = &t->next;
2908 t = t->next;
2909 }
2910 *tpp = tn;
2911 if (tmpl) {
2912 tmpl->nparam = nparam;
2913 tmpl->greedy = greedy;
2914 tmpl->params = params;
2915 }
2916 return nparam;
2917}
2918
2919/*
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002920 * Common code for defining an smacro. The tmpl argument, if not NULL,
2921 * contains any macro parameters that aren't explicit arguments;
2922 * those are the more uncommon macro variants.
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002923 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002924static SMacro *define_smacro(const char *mname, bool casesense,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002925 Token *expansion, SMacro *tmpl)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002926{
2927 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002928 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002929 Context *ctx;
2930 bool defining_alias = false;
2931 unsigned int nparam = 0;
H. Peter Anvin70653092007-10-19 14:42:29 -07002932
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002933 if (tmpl) {
2934 defining_alias = tmpl->alias;
2935 nparam = tmpl->nparam;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002936 if (nparam && !defining_alias)
2937 mark_smac_params(expansion, tmpl, 0);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002938 }
2939
2940 while (1) {
2941 ctx = get_ctx(mname, &mname);
2942
H. Peter Anvind2354082019-08-27 16:38:48 -07002943 if (!smacro_defined(ctx, mname, nparam, &smac, casesense, true)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002944 /* Create a new macro */
2945 smtbl = ctx ? &ctx->localmac : &smacros;
2946 smhead = (SMacro **) hash_findi_add(smtbl, mname);
2947 nasm_new(smac);
2948 smac->next = *smhead;
2949 *smhead = smac;
2950 break;
2951 } else if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002952 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002953 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002954 /*
2955 * Some instances of the old code considered this a failure,
2956 * some others didn't. What is the right thing to do here?
2957 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002958 goto fail;
H. Peter Anvind2354082019-08-27 16:38:48 -07002959 } else if (!smac->alias || !do_aliases || defining_alias) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002960 /*
2961 * We're redefining, so we have to take over an
2962 * existing SMacro structure. This means freeing
H. Peter Anvin8b262472019-02-26 14:00:54 -08002963 * what was already in it, but not the structure itself.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002964 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002965 clear_smacro(smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002966 break;
2967 } else if (smac->in_progress) {
2968 nasm_nonfatal("macro alias loop");
2969 goto fail;
2970 } else {
2971 /* It is an alias macro; follow the alias link */
2972 SMacro *s;
2973
2974 smac->in_progress = true;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002975 s = define_smacro(tok_text(smac->expansion), casesense,
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002976 expansion, tmpl);
2977 smac->in_progress = false;
2978 return s;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002979 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002980 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002981
2982 smac->name = nasm_strdup(mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002983 smac->casesense = casesense;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002984 smac->expansion = expansion;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002985 smac->expand = smacro_expand_default;
2986 if (tmpl) {
2987 smac->nparam = tmpl->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002988 smac->params = tmpl->params;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002989 smac->alias = tmpl->alias;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002990 smac->greedy = tmpl->greedy;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002991 if (tmpl->expand)
2992 smac->expand = tmpl->expand;
2993 }
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07002994 if (list_option('s')) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002995 list_smacro_def((smac->alias ? PP_DEFALIAS : PP_DEFINE)
2996 + !casesense, ctx, smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002997 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08002998 return smac;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002999
3000fail:
3001 free_tlist(expansion);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003002 if (tmpl)
3003 free_smacro_members(tmpl);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003004 return NULL;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003005}
3006
3007/*
3008 * Undefine an smacro
3009 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003010static void undef_smacro(const char *mname, bool undefalias)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003011{
3012 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003013 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003014 Context *ctx;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003015
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003016 ctx = get_ctx(mname, &mname);
H. Peter Anvin166c2472008-05-28 12:28:58 -07003017 smtbl = ctx ? &ctx->localmac : &smacros;
3018 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07003019
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003020 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003021 /*
3022 * We now have a macro name... go hunt for it.
3023 */
3024 sp = smhead;
3025 while ((s = *sp) != NULL) {
3026 if (!mstrcmp(s->name, mname, s->casesense)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003027 if (s->alias && !undefalias) {
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003028 if (do_aliases) {
3029 if (s->in_progress) {
3030 nasm_nonfatal("macro alias loop");
3031 } else {
3032 s->in_progress = true;
3033 undef_smacro(tok_text(s->expansion), false);
3034 s->in_progress = false;
3035 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003036 }
3037 } else {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07003038 if (list_option('d'))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003039 list_smacro_def(s->alias ? PP_UNDEFALIAS : PP_UNDEF,
3040 ctx, s);
3041 *sp = s->next;
3042 free_smacro(s);
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003043 continue;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003044 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003045 }
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003046 sp = &s->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003047 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003048 }
3049}
3050
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003051/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07003052 * Parse a mmacro specification.
3053 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003054static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07003055{
H. Peter Anvina26433d2008-07-16 14:40:01 -07003056 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003057 tline = skip_white(tline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003058 tline = expand_id(tline);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003059 if (!tok_type(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003060 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003061 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003062 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02003063
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003064#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003065 def->prev = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003066#endif
H. Peter Anvin8571f062019-09-23 16:40:03 -07003067 def->name = dup_text(tline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003068 def->plus = false;
3069 def->nolist = false;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02003070 def->nparam_min = 0;
3071 def->nparam_max = 0;
3072
H. Peter Anvina26433d2008-07-16 14:40:01 -07003073 tline = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003074 tline = skip_white(tline);
3075 if (!tok_type(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003076 nasm_nonfatal("`%s' expects a parameter count", directive);
3077 else
H. Peter Anvin8571f062019-09-23 16:40:03 -07003078 def->nparam_min = def->nparam_max = read_param_count(tok_text(tline));
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003079 if (tline && tok_is(tline->next, '-')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003080 tline = tline->next->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003081 if (tok_is(tline, '*')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003082 def->nparam_max = INT_MAX;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003083 } else if (!tok_type(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003084 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003085 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003086 def->nparam_max = read_param_count(tok_text(tline));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003087 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003088 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03003089 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003090 }
3091 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003092 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003093 if (tline && tok_is(tline->next, '+')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003094 tline = tline->next;
3095 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003096 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003097 if (tline && tok_type(tline->next, TOK_ID) &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07003098 tline->next->len == 7 &&
3099 !nasm_stricmp(tline->next->text.a, ".nolist")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003100 tline = tline->next;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003101 def->nolist = !list_option('f') || istk->nolist;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003102 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003103
H. Peter Anvina26433d2008-07-16 14:40:01 -07003104 /*
3105 * Handle default parameters.
3106 */
3107 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003108 def->dlist = tline->next;
3109 tline->next = NULL;
3110 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003111 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003112 def->dlist = NULL;
3113 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003114 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003115 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003116
H. Peter Anvin89cee572009-07-15 09:16:54 -04003117 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003118 !def->plus) {
3119 /*
3120 *!macro-defaults [on] macros with more default than optional parameters
3121 *! warns when a macro has more default parameters than optional parameters.
3122 *! See \k{mlmacdef} for why might want to disable this warning.
3123 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003124 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003125 "too many default macro parameters in macro `%s'", def->name);
3126 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02003127
H. Peter Anvina26433d2008-07-16 14:40:01 -07003128 return true;
3129}
3130
3131
3132/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003133 * Decode a size directive
3134 */
3135static int parse_size(const char *str) {
3136 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003137 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003138 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003139 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03003140 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003141}
3142
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003143/*
3144 * Process a preprocessor %pragma directive. Currently there are none.
3145 * Gets passed the token list starting with the "preproc" token from
3146 * "%pragma preproc".
3147 */
3148static void do_pragma_preproc(Token *tline)
3149{
3150 /* Skip to the real stuff */
3151 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003152 tline = skip_white(tline);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003153 if (!tline)
3154 return;
3155
3156 (void)tline; /* Nothing else to do at present */
3157}
3158
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003159static bool is_macro_id(const Token *t)
3160{
H. Peter Anvin8571f062019-09-23 16:40:03 -07003161 return tok_type(t, TOK_ID) || tok_type(t, TOK_LOCAL_MACRO);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003162}
3163
H. Peter Anvin8571f062019-09-23 16:40:03 -07003164static const char *get_id(Token **tp, const char *dname)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003165{
H. Peter Anvin8571f062019-09-23 16:40:03 -07003166 const char *id;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003167 Token *t = *tp;
3168
3169 t = t->next; /* Skip directive */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003170 t = skip_white(t);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003171 t = expand_id(t);
3172
3173 if (!is_macro_id(t)) {
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003174 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003175 return NULL;
3176 }
3177
H. Peter Anvin8571f062019-09-23 16:40:03 -07003178 id = tok_text(t);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003179 t = skip_white(t);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003180 *tp = t;
3181 return id;
3182}
3183
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003184/* Parse a %use package name and find the package. Set *err on syntax error. */
3185static const struct use_package *
H. Peter Anvin8571f062019-09-23 16:40:03 -07003186get_use_pkg(Token *t, const char *dname, const char **name)
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003187{
H. Peter Anvin8571f062019-09-23 16:40:03 -07003188 const char *id;
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003189
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003190 t = skip_white(t);
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003191 t = expand_smacro(t);
3192
H. Peter Anvin8571f062019-09-23 16:40:03 -07003193 *name = NULL;
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003194
H. Peter Anvin8571f062019-09-23 16:40:03 -07003195 if (!t) {
3196 nasm_nonfatal("`%s' expects a package name, got end of line", dname);
3197 return NULL;
3198 } else if (t->type != TOK_ID && t->type != TOK_STRING) {
3199 nasm_nonfatal("`%s' expects a package name, got `%s'",
3200 dname, tok_text(t));
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003201 return NULL;
3202 }
3203
H. Peter Anvin8571f062019-09-23 16:40:03 -07003204 *name = id = unquote_token(t);
3205
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003206 t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003207 t = skip_white(t);
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003208 if (t)
3209 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
3210
3211 return nasm_find_use_package(id);
3212}
3213
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003214/*
3215 * Mark parameter tokens in an smacro definition. If the type argument
3216 * is 0, create smac param tokens, otherwise use the type specified;
3217 * normally this is used for TOK_XDEF_PARAM, which is used to protect
3218 * parameter tokens during expansion during %xdefine.
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003219 *
3220 * tmpl may not be NULL here.
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003221 */
3222static void mark_smac_params(Token *tline, const SMacro *tmpl,
3223 enum pp_token_type type)
3224{
3225 const struct smac_param *params = tmpl->params;
3226 int nparam = tmpl->nparam;
3227 Token *t;
3228 int i;
3229
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003230 list_for_each(t, tline) {
3231 if (t->type != TOK_ID && t->type != TOK_XDEF_PARAM)
3232 continue;
3233
3234 for (i = 0; i < nparam; i++) {
3235 if (tok_text_match(t, &params[i].name))
3236 t->type = type ? type : tok_smac_param(i);
3237 }
3238 }
3239}
3240
Ed Beroset3ab3f412002-06-11 03:31:49 +00003241/**
3242 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003243 * Find out if a line contains a preprocessor directive, and deal
3244 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07003245 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00003246 * If a directive _is_ found, it is the responsibility of this routine
3247 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003248 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00003249 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003250 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00003251 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07003252 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003253 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07003254static int do_directive(Token *tline, Token **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003255{
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003256 enum preproc_token op;
H. Peter Anvin4169a472007-09-12 01:29:43 +00003257 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07003258 bool err;
H. Peter Anvin70055962007-10-11 00:05:31 -07003259 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003260 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07003261 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003262 int offset;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003263 const char *p;
3264 char *q, *qbuf;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003265 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003266 const char *mname;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003267 struct ppscan pps;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003268 Include *inc;
3269 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003270 Cond *cond;
3271 MMacro *mmac, **mmhead;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003272 Token *t = NULL, *tt, *macro_start, *last, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003273 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003274 struct tokenval tokval;
3275 expr *evalresult;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003276 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003277 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08003278 errflags severity;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003279 const char *dname; /* Name of directive, for messages */
H. Peter Anvin76690a12002-04-30 20:52:49 +00003280
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003281 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00003282 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003283
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003284 tline = skip_white(tline);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003285 if (!tline || !tok_type(tline, TOK_PREPROC_ID))
3286 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003287
H. Peter Anvin8571f062019-09-23 16:40:03 -07003288 dname = tok_text(tline);
3289 if (dname[1] == '%')
3290 return NO_DIRECTIVE_FOUND;
3291
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003292 op = pp_token_hash(dname);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003293
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003294 casesense = true;
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003295 if (PP_HAS_CASE(op) & PP_INSENSITIVE(op)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003296 casesense = false;
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003297 op--;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003298 }
3299
3300 /*
3301 * If we're in a non-emitting branch of a condition construct,
3302 * or walking to the end of an already terminated %rep block,
3303 * we should ignore all directives except for condition
3304 * directives.
3305 */
3306 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003307 (istk->mstk.mstk && !istk->mstk.mstk->in_progress)) &&
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003308 !is_condition(op)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003309 return NO_DIRECTIVE_FOUND;
3310 }
3311
3312 /*
3313 * If we're defining a macro or reading a %rep block, we should
3314 * ignore all directives except for %macro/%imacro (which nest),
3315 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
3316 * If we're in a %rep block, another %rep nests, so should be let through.
3317 */
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003318 if (defining && op != PP_MACRO && op != PP_RMACRO &&
3319 op != PP_ENDMACRO && op != PP_ENDM &&
3320 (defining->name || (op != PP_ENDREP && op != PP_REP))) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003321 return NO_DIRECTIVE_FOUND;
3322 }
3323
3324 if (defining) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003325 if (op == PP_MACRO || op == PP_RMACRO) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003326 nested_mac_count++;
3327 return NO_DIRECTIVE_FOUND;
3328 } else if (nested_mac_count > 0) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003329 if (op == PP_ENDMACRO) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003330 nested_mac_count--;
3331 return NO_DIRECTIVE_FOUND;
3332 }
3333 }
3334 if (!defining->name) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003335 if (op == PP_REP) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003336 nested_rep_count++;
3337 return NO_DIRECTIVE_FOUND;
3338 } else if (nested_rep_count > 0) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003339 if (op == PP_ENDREP) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003340 nested_rep_count--;
3341 return NO_DIRECTIVE_FOUND;
3342 }
3343 }
3344 }
3345 }
3346
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003347 switch (op) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003348 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003349 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003350 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003351
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003352 case PP_PRAGMA:
3353 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003354 * %pragma namespace options...
3355 *
3356 * The namespace "preproc" is reserved for the preprocessor;
3357 * all other namespaces generate a [pragma] assembly directive.
3358 *
3359 * Invalid %pragmas are ignored and may have different
3360 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003361 */
H. Peter Anvinf5d7d902019-08-10 06:21:00 -07003362 t = tline;
3363 tline = tline->next;
3364 t->next = NULL;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003365 tline = zap_white(expand_smacro(tline));
3366 if (tok_type(tline, TOK_ID)) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003367 if (!nasm_stricmp(tok_text(tline), "preproc")) {
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003368 /* Preprocessor pragma */
3369 do_pragma_preproc(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07003370 free_tlist(tline);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003371 } else {
3372 /* Build the assembler directive */
H. Peter Anvin06335872019-08-10 06:42:55 -07003373
3374 /* Append bracket to the end of the output */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003375 for (t = tline; t->next; t = t->next)
3376 ;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003377 t->next = make_tok_char(NULL, ']');
H. Peter Anvin06335872019-08-10 06:42:55 -07003378
3379 /* Prepend "[pragma " */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003380 t = new_White(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07003381 t = new_Token(t, TOK_ID, "pragma", 6);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003382 t = make_tok_char(t, '[');
H. Peter Anvin06335872019-08-10 06:42:55 -07003383 tline = t;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07003384 *output = tline;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003385 }
3386 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003387 break;
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003388
H. Peter Anvine2c80182005-01-15 22:15:51 +00003389 case PP_STACKSIZE:
3390 /* Directive to tell NASM what the default stack size is. The
3391 * default is for a 16-bit stack, and this can be overriden with
3392 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003393 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003394 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003395 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003396 nasm_nonfatal("`%s' missing size parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003397 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07003398 if (nasm_stricmp(tok_text(tline), "flat") == 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003399 /* All subsequent ARG directives are for a 32-bit stack */
3400 StackSize = 4;
3401 StackPointer = "ebp";
3402 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003403 LocalOffset = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003404 } else if (nasm_stricmp(tok_text(tline), "flat64") == 0) {
Charles Crayne7eaf9192007-11-08 22:11:14 -08003405 /* All subsequent ARG directives are for a 64-bit stack */
3406 StackSize = 8;
3407 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03003408 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08003409 LocalOffset = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003410 } else if (nasm_stricmp(tok_text(tline), "large") == 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003411 /* All subsequent ARG directives are for a 16-bit stack,
3412 * far function call.
3413 */
3414 StackSize = 2;
3415 StackPointer = "bp";
3416 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003417 LocalOffset = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003418 } else if (nasm_stricmp(tok_text(tline), "small") == 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003419 /* All subsequent ARG directives are for a 16-bit stack,
3420 * far function call. We don't support near functions.
3421 */
3422 StackSize = 2;
3423 StackPointer = "bp";
3424 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003425 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003426 } else {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003427 nasm_nonfatal("`%s' invalid size type", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003428 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003429 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003430
H. Peter Anvine2c80182005-01-15 22:15:51 +00003431 case PP_ARG:
3432 /* TASM like ARG directive to define arguments to functions, in
3433 * the following form:
3434 *
3435 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
3436 */
3437 offset = ArgOffset;
3438 do {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003439 const char *arg;
3440 char directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003441 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003442
H. Peter Anvine2c80182005-01-15 22:15:51 +00003443 /* Find the argument name */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003444 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003445 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003446 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003447 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003448 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07003449 arg = tok_text(tline);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003450
H. Peter Anvine2c80182005-01-15 22:15:51 +00003451 /* Find the argument size type */
3452 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003453 if (!tok_is(tline, ':')) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003454 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003455 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003456 }
3457 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003458 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003459 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003460 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003461 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003462
H. Peter Anvine2c80182005-01-15 22:15:51 +00003463 /* Allow macro expansion of type parameter */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003464 tt = tokenize(tok_text(tline));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003465 tt = expand_smacro(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003466 size = parse_size(tok_text(tt));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003467 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003468 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003469 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003470 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003471 }
3472 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003473
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003474 /* Round up to even stack slots */
3475 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003476
H. Peter Anvine2c80182005-01-15 22:15:51 +00003477 /* Now define the macro for the argument */
3478 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
3479 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003480 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003481 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003482
H. Peter Anvine2c80182005-01-15 22:15:51 +00003483 /* Move to the next argument in the list */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003484 tline = skip_white(tline->next);
3485 } while (tok_is(tline, ','));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003486 ArgOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003487 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003488
H. Peter Anvine2c80182005-01-15 22:15:51 +00003489 case PP_LOCAL:
3490 /* TASM like LOCAL directive to define local variables for a
3491 * function, in the following form:
3492 *
3493 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
3494 *
3495 * The '= LocalSize' at the end is ignored by NASM, but is
3496 * required by TASM to define the local parameter size (and used
3497 * by the TASM macro package).
3498 */
3499 offset = LocalOffset;
3500 do {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003501 const char *local;
3502 char directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003503 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003504
H. Peter Anvine2c80182005-01-15 22:15:51 +00003505 /* Find the argument name */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003506 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003507 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003508 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003509 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003510 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07003511 local = tok_text(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003512
H. Peter Anvine2c80182005-01-15 22:15:51 +00003513 /* Find the argument size type */
3514 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003515 if (!tok_is(tline, ':')) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003516 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003517 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003518 }
3519 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003520 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003521 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003522 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003523 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003524
H. Peter Anvine2c80182005-01-15 22:15:51 +00003525 /* Allow macro expansion of type parameter */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003526 tt = tokenize(tok_text(tline));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003527 tt = expand_smacro(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003528 size = parse_size(tok_text(tt));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003529 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003530 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003531 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003532 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003533 }
3534 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003535
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003536 /* Round up to even stack slots */
3537 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003538
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003539 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003540
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003541 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003542 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
3543 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003544 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003545
H. Peter Anvine2c80182005-01-15 22:15:51 +00003546 /* Now define the assign to setup the enter_c macro correctly */
3547 snprintf(directive, sizeof(directive),
3548 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003549 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003550
H. Peter Anvine2c80182005-01-15 22:15:51 +00003551 /* Move to the next argument in the list */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003552 tline = skip_white(tline->next);
3553 } while (tok_is(tline, ','));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003554 LocalOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003555 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003556
H. Peter Anvine2c80182005-01-15 22:15:51 +00003557 case PP_CLEAR:
3558 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003559 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003560 free_macros();
3561 init_macros();
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003562 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003563
H. Peter Anvin418ca702008-05-30 10:42:30 -07003564 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003565 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003566 t = skip_white(t);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003567 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003568 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003569 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003570 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003571 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003572 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003573 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003574
3575 strlist_add(deplist, unquote_token_cstr(t));
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003576 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003577
3578 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003579 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003580 t = skip_white(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07003581
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003582 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003583 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003584 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003585 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003586 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003587 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003588 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003589 p = unquote_token_cstr(t);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003590 nasm_new(inc);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003591 inc->next = istk;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003592 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07003593 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08003594 (pp_mode == PP_DEPS)
3595 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003596 if (!inc->fp) {
3597 /* -MG given but file not found */
3598 nasm_free(inc);
3599 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003600 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003601 inc->lineno = src_set_linnum(0);
3602 inc->lineinc = 1;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003603 inc->nolist = istk->nolist;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003604 istk = inc;
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003605 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003606 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003607 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003608
H. Peter Anvind2456592008-06-19 15:04:18 -07003609 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003610 {
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003611 const struct use_package *pkg;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003612 const char *name;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003613
H. Peter Anvin8571f062019-09-23 16:40:03 -07003614 pkg = get_use_pkg(tline->next, dname, &name);
3615 if (!name)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003616 goto done;
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003617 if (!pkg) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003618 nasm_nonfatal("unknown `%s' package: `%s'", dname, name);
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003619 } else if (!use_loaded[pkg->index]) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003620 /*
3621 * Not already included, go ahead and include it.
3622 * Treat it as an include file for the purpose of
3623 * producing a listing.
3624 */
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003625 use_loaded[pkg->index] = true;
3626 stdmacpos = pkg->macros;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003627 nasm_new(inc);
3628 inc->next = istk;
3629 inc->fname = src_set_fname(NULL);
3630 inc->lineno = src_set_linnum(0);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003631 inc->nolist = !list_option('b') || istk->nolist;
3632 istk = inc;
3633 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003634 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003635 break;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003636 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003637 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003638 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07003639 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003640 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003641 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003642 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003643 if (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003644 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003645 nasm_nonfatal("`%s' expects a context identifier", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003646 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003647 }
3648 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003649 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003650 dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003651 p = tok_text(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003652 } else {
3653 p = NULL; /* Anonymous */
3654 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07003655
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003656 if (op == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08003657 nasm_new(ctx);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003658 ctx->depth = cstk ? cstk->depth + 1 : 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003659 ctx->next = cstk;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003660 ctx->name = p ? nasm_strdup(p) : NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003661 ctx->number = unique++;
3662 cstk = ctx;
3663 } else {
3664 /* %pop or %repl */
3665 if (!cstk) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003666 nasm_nonfatal("`%s': context stack is empty", dname);
3667 } else if (op == PP_POP) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003668 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003669 nasm_nonfatal("`%s' in wrong context: %s, "
H. Peter Anvin8b262472019-02-26 14:00:54 -08003670 "expected %s",
3671 dname, cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003672 else
3673 ctx_pop();
3674 } else {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003675 /* op == PP_REPL */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003676 nasm_free((char *)cstk->name);
3677 cstk->name = p ? nasm_strdup(p) : NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003678 p = NULL;
3679 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003680 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003681 break;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003682 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003683 severity = ERR_FATAL;
3684 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003685 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003686 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003687 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003688 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003689 /*!
3690 *!user [on] %warning directives
3691 *! controls output of \c{%warning} directives (see \k{pperror}).
3692 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003693 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003694 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003695
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003696issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07003697 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003698 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003699 tline->next = expand_smacro(tline->next);
3700 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003701 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003702 t = tline ? tline->next : NULL;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003703 t = skip_white(t);
3704 if (tok_type(tline, TOK_STRING) && !t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003705 /* The line contains only a quoted string */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003706 p = unquote_token(tline); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08003707 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003708 } else {
3709 /* Not a quoted string, or more than a quoted string */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003710 q = detoken(tline, false);
3711 nasm_error(severity, "%s", q);
3712 nasm_free(q);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003713 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003714 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003715 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003716
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003717 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003718 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003719 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003720 else {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003721 j = if_condition(tline->next, op);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003722 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003723 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003724 cond = nasm_malloc(sizeof(Cond));
3725 cond->next = istk->conds;
3726 cond->state = j;
3727 istk->conds = cond;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003728 if(istk->mstk.mstk)
3729 istk->mstk.mstk->condcnt++;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003730 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003731
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003732 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003733 if (!istk->conds)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003734 nasm_fatal("`%s': no matching `%%if'", dname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003735 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003736 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003737 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003738 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003739
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003740 case COND_DONE:
3741 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003742 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003743
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003744 case COND_ELSE_TRUE:
3745 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003746 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003747 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003748 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003749 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003750
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003751 case COND_IF_FALSE:
3752 /*
3753 * IMPORTANT: In the case of %if, we will already have
3754 * called expand_mmac_params(); however, if we're
3755 * processing an %elif we must have been in a
3756 * non-emitting mode, which would have inhibited
3757 * the normal invocation of expand_mmac_params().
3758 * Therefore, we have to do it explicitly here.
3759 */
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003760 j = if_condition(expand_mmac_params(tline->next), op);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003761 tline->next = NULL; /* it got freed */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003762 istk->conds->state = j;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003763 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003764 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003765 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003766
H. Peter Anvine2c80182005-01-15 22:15:51 +00003767 case PP_ELSE:
3768 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003769 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003770 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003771 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07003772 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003773 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003774 case COND_IF_TRUE:
3775 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003776 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003777 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003778
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003779 case COND_NEVER:
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_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003783 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003784 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003785
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003786 case COND_ELSE_TRUE:
3787 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003788 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003789 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003790 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003791 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003792 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003793 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003794
H. Peter Anvine2c80182005-01-15 22:15:51 +00003795 case PP_ENDIF:
3796 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003797 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003798 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003799 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003800 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003801 cond = istk->conds;
3802 istk->conds = cond->next;
3803 nasm_free(cond);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003804 if(istk->mstk.mstk)
3805 istk->mstk.mstk->condcnt--;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003806 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003807
H. Peter Anvin8b262472019-02-26 14:00:54 -08003808 case PP_RMACRO:
3809 case PP_MACRO:
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07003810 {
3811 MMacro *def;
3812
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003813 nasm_assert(!defining);
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07003814 nasm_new(def);
3815 def->casesense = casesense;
3816 def->dstk.mmac = defining;
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003817 if (op == PP_RMACRO)
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07003818 def->max_depth = nasm_limit[LIMIT_MACRO_LEVELS];
3819 if (!parse_mmacro_spec(tline, def, dname)) {
3820 nasm_free(def);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003821 goto done;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003822 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003823
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07003824 defining = def;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07003825 src_get(&defining->xline, &defining->fname);
3826
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003827 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
3828 while (mmac) {
3829 if (!strcmp(mmac->name, defining->name) &&
3830 (mmac->nparam_min <= defining->nparam_max
3831 || defining->plus)
3832 && (defining->nparam_min <= mmac->nparam_max
3833 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003834 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003835 defining->name);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003836 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003837 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003838 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003839 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003840 break;
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07003841 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003842
H. Peter Anvine2c80182005-01-15 22:15:51 +00003843 case PP_ENDM:
3844 case PP_ENDMACRO:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003845 if (!(defining && defining->name)) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003846 nasm_nonfatal("`%s': not defining a macro", tok_text(tline));
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003847 goto done;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003848 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003849 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
3850 defining->next = *mmhead;
3851 *mmhead = defining;
3852 defining = NULL;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003853 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003854
H. Peter Anvin89cee572009-07-15 09:16:54 -04003855 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003856 /*
3857 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003858 * macro-end marker for a macro with a name. Then we
3859 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003860 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003861 list_for_each(l, istk->expansion)
3862 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003863 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003864
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003865 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003866 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003867 * Remove all conditional entries relative to this
3868 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003869 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003870 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
3871 cond = istk->conds;
3872 istk->conds = cond->next;
3873 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003874 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003875 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003876 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003877 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003878 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003879 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05003880
H. Peter Anvina26433d2008-07-16 14:40:01 -07003881 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003882 casesense = false;
3883 /* fall through */
3884 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07003885 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003886 MMacro **mmac_p;
3887 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003888
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003889 nasm_zero(spec);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003890 spec.casesense = casesense;
3891 if (!parse_mmacro_spec(tline, &spec, dname)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003892 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003893 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003894 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
3895 while (mmac_p && *mmac_p) {
3896 mmac = *mmac_p;
3897 if (mmac->casesense == spec.casesense &&
3898 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
3899 mmac->nparam_min == spec.nparam_min &&
3900 mmac->nparam_max == spec.nparam_max &&
3901 mmac->plus == spec.plus) {
3902 *mmac_p = mmac->next;
3903 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003904 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003905 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003906 }
3907 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003908 free_tlist(spec.dlist);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003909 break;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003910 }
3911
H. Peter Anvine2c80182005-01-15 22:15:51 +00003912 case PP_ROTATE:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003913 while (tok_white(tline->next))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003914 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003915 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003916 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003917 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003918 return DIRECTIVE_FOUND;
3919 }
3920 t = expand_smacro(tline->next);
3921 tline->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003922 pps.tptr = tline = t;
3923 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003924 tokval.t_type = TOKEN_INVALID;
3925 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003926 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003927 free_tlist(tline);
3928 if (!evalresult)
3929 return DIRECTIVE_FOUND;
3930 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003931 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003932 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003933 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003934 return DIRECTIVE_FOUND;
3935 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003936 mmac = istk->mstk.mmac;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003937 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003938 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003939 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003940 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003941 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003942 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003943
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003944 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003945 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003946 rotate += mmac->nparam;
3947
3948 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003949 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003950 break;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003951
H. Peter Anvine2c80182005-01-15 22:15:51 +00003952 case PP_REP:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003953 {
3954 MMacro *tmp_defining;
3955
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003956 nolist = false;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003957 tline = skip_white(tline->next);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003958 if (tok_type(tline, TOK_ID) && tline->len == 7 &&
3959 !nasm_memicmp(tline->text.a, ".nolist", 7)) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003960 nolist = !list_option('f') || istk->nolist;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003961 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003962 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003963
H. Peter Anvine2c80182005-01-15 22:15:51 +00003964 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003965 pps.tptr = expand_smacro(tline);
3966 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003967 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003968 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003969 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003970 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003971 if (!evalresult)
3972 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003973 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003974 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003975 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003976 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003977 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003978 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003979 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003980 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003981 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
3982 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003983 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003984 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003985 /*!
3986 *!negative-rep [on] regative %rep count
3987 *! warns about negative counts given to the \c{%rep}
3988 *! preprocessor directive.
3989 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08003990 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003991 "negative `%%rep' count: %"PRId64, count);
3992 count = 0;
3993 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003994 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003995 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003996 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003997 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003998 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003999 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004000 tmp_defining = defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07004001 nasm_new(defining);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004002 defining->nolist = nolist;
4003 defining->in_progress = count;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004004 defining->mstk = istk->mstk;
4005 defining->dstk.mstk = tmp_defining;
4006 defining->dstk.mmac = tmp_defining ? tmp_defining->dstk.mmac : NULL;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07004007 src_get(&defining->xline, &defining->fname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004008 break;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004009 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004010
H. Peter Anvine2c80182005-01-15 22:15:51 +00004011 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004012 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004013 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004014 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004015 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004016
H. Peter Anvine2c80182005-01-15 22:15:51 +00004017 /*
4018 * Now we have a "macro" defined - although it has no name
4019 * and we won't be entering it in the hash tables - we must
4020 * push a macro-end marker for it on to istk->expansion.
4021 * After that, it will take care of propagating itself (a
4022 * macro-end marker line for a macro which is really a %rep
4023 * block will cause the macro to be re-expanded, complete
4024 * with another macro-end marker to ensure the process
4025 * continues) until the whole expansion is forcibly removed
4026 * from istk->expansion by a %exitrep.
4027 */
H. Peter Anvin6686de22019-08-10 05:33:14 -07004028 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004029 l->next = istk->expansion;
4030 l->finishes = defining;
4031 l->first = NULL;
4032 istk->expansion = l;
4033
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004034 istk->mstk.mstk = defining;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004035
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07004036 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004037 defining = defining->dstk.mstk;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004038 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004039
H. Peter Anvine2c80182005-01-15 22:15:51 +00004040 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004041 /*
4042 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004043 * macro-end marker for a macro with no name. Then we set
4044 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004045 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004046 list_for_each(l, istk->expansion)
4047 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004048 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004049
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004050 if (l)
H. Peter Anvind983b622019-10-07 21:19:32 -07004051 l->finishes->in_progress = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004052 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004053 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004054 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004055
H. Peter Anvin8b262472019-02-26 14:00:54 -08004056 case PP_DEFINE:
4057 case PP_XDEFINE:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004058 case PP_DEFALIAS:
H. Peter Anvin8b262472019-02-26 14:00:54 -08004059 {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004060 SMacro tmpl;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004061 Token **lastp;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07004062 int nparam;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07004063
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004064 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004065 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004066
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004067 nasm_zero(tmpl);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004068 lastp = &tline->next;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07004069 nparam = parse_smacro_template(&lastp, &tmpl);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004070 tline = *lastp;
4071 *lastp = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004072
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004073 if (unlikely(op == PP_DEFALIAS)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004074 macro_start = tline;
4075 if (!is_macro_id(macro_start)) {
4076 nasm_nonfatal("`%s' expects a macro identifier to alias",
4077 dname);
4078 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004079 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004080 tt = macro_start->next;
4081 macro_start->next = NULL;
4082 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004083 tline = skip_white(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004084 if (tline && tline->type) {
4085 nasm_warn(WARN_OTHER,
4086 "trailing garbage after aliasing identifier ignored");
4087 }
4088 free_tlist(tt);
4089 tmpl.alias = true;
4090 } else {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004091 if (op == PP_XDEFINE) {
4092 /* Protect macro parameter tokens */
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07004093 if (nparam)
4094 mark_smac_params(tline, &tmpl, TOK_XDEF_PARAM);
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004095 tline = expand_smacro(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004096 }
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004097 /* NB: Does this still make sense? */
4098 macro_start = reverse_tokens(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004099 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004100
H. Peter Anvine2c80182005-01-15 22:15:51 +00004101 /*
4102 * Good. We now have a macro name, a parameter count, and a
4103 * token list (in reverse order) for an expansion. We ought
4104 * to be OK just to create an SMacro, store it, and let
4105 * free_tlist have the rest of the line (which we have
4106 * carefully re-terminated after chopping off the expansion
4107 * from the end).
4108 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004109 define_smacro(mname, casesense, macro_start, &tmpl);
4110 break;
4111 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004112
H. Peter Anvine2c80182005-01-15 22:15:51 +00004113 case PP_UNDEF:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004114 case PP_UNDEFALIAS:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004115 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004116 goto done;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004117 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004118 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004119
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004120 undef_smacro(mname, op == PP_UNDEFALIAS);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004121 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004122
H. Peter Anvin8b262472019-02-26 14:00:54 -08004123 case PP_DEFSTR:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004124 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004125 goto done;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004126
H. Peter Anvin9e200162008-06-04 17:23:14 -07004127 last = tline;
4128 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004129 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004130
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004131 tline = zap_white(tline);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004132 q = detoken(tline, false);
4133 macro_start = make_tok_qstr(NULL, q);
4134 nasm_free(q);
H. Peter Anvin9e200162008-06-04 17:23:14 -07004135
4136 /*
4137 * We now have a macro name, an implicit parameter count of
4138 * zero, and a string token to use as an expansion. Create
4139 * and store an SMacro.
4140 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004141 define_smacro(mname, casesense, macro_start, NULL);
4142 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004143
H. Peter Anvin8b262472019-02-26 14:00:54 -08004144 case PP_DEFTOK:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004145 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004146 goto done;
4147
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004148 last = tline;
4149 tline = expand_smacro(tline->next);
4150 last->next = NULL;
4151
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004152 t = skip_white(tline);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004153 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004154 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004155 nasm_nonfatal("`%s' requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004156 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004157 goto done;
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004158 }
4159
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04004160 /*
4161 * Convert the string to a token stream. Note that smacros
4162 * are stored with the token stream reversed, so we have to
4163 * reverse the output of tokenize().
4164 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07004165 macro_start = reverse_tokens(tokenize(unquote_token_cstr(t)));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004166
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004167 /*
4168 * We now have a macro name, an implicit parameter count of
4169 * zero, and a numeric token to use as an expansion. Create
4170 * and store an SMacro.
4171 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004172 define_smacro(mname, casesense, macro_start, NULL);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004173 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004174 break;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004175
H. Peter Anvin418ca702008-05-30 10:42:30 -07004176 case PP_PATHSEARCH:
4177 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07004178 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004179
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004180 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004181 goto done;
4182
H. Peter Anvin418ca702008-05-30 10:42:30 -07004183 last = tline;
4184 tline = expand_smacro(tline->next);
4185 last->next = NULL;
4186
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004187 t = skip_white(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004188 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004189 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004190 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004191 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004192 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004193 }
4194 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004195 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004196
4197 p = unquote_token_cstr(t);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004198
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07004199 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07004200 if (!found_path)
4201 found_path = p;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004202 macro_start = make_tok_qstr(NULL, found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004203
4204 /*
4205 * We now have a macro name, an implicit parameter count of
4206 * zero, and a string token to use as an expansion. Create
4207 * and store an SMacro.
4208 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004209 define_smacro(mname, casesense, macro_start, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004210 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004211 break;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004212 }
4213
H. Peter Anvine2c80182005-01-15 22:15:51 +00004214 case PP_STRLEN:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004215 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004216 goto done;
4217
H. Peter Anvine2c80182005-01-15 22:15:51 +00004218 last = tline;
4219 tline = expand_smacro(tline->next);
4220 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004221
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004222 t = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004223 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004224 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004225 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004226 free_tlist(tline);
4227 free_tlist(origline);
4228 return DIRECTIVE_FOUND;
4229 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004230
H. Peter Anvin8571f062019-09-23 16:40:03 -07004231 unquote_token(t);
4232 macro_start = make_tok_num(NULL, t->len);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004233
H. Peter Anvine2c80182005-01-15 22:15:51 +00004234 /*
4235 * We now have a macro name, an implicit parameter count of
4236 * zero, and a numeric token to use as an expansion. Create
4237 * and store an SMacro.
4238 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004239 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004240 free_tlist(tline);
4241 free_tlist(origline);
4242 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004243
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004244 case PP_STRCAT:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004245 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004246 goto done;
4247
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004248 last = tline;
4249 tline = expand_smacro(tline->next);
4250 last->next = NULL;
4251
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004252 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004253 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004254 switch (t->type) {
4255 case TOK_WHITESPACE:
4256 break;
4257 case TOK_STRING:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004258 unquote_token(t);
4259 len += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004260 break;
4261 case TOK_OTHER:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004262 if (tok_is(t, ',')) /* permit comma separators */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004263 break;
4264 /* else fall through */
4265 default:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004266 nasm_nonfatal("non-string passed to `%s': %s", dname,
4267 tok_text(t));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004268 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004269 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004270 }
4271 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004272
H. Peter Anvin (Intel)f770ce82019-10-17 18:22:43 -07004273 q = qbuf = nasm_malloc(len+1);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004274 list_for_each(t, tline) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004275 if (t->type == TOK_INTERNAL_STRING)
4276 q = mempcpy(q, tok_text(t), t->len);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004277 }
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07004278 *q = '\0';
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004279
4280 /*
4281 * We now have a macro name, an implicit parameter count of
4282 * zero, and a numeric token to use as an expansion. Create
4283 * and store an SMacro.
4284 */
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07004285 macro_start = make_tok_qstr_len(NULL, qbuf, len);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004286 nasm_free(qbuf);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004287 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004288 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004289 break;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004290
H. Peter Anvine2c80182005-01-15 22:15:51 +00004291 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004292 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004293 int64_t start, count;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004294 const char *txt;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004295 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07004296
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004297 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004298 goto done;
4299
H. Peter Anvine2c80182005-01-15 22:15:51 +00004300 last = tline;
4301 tline = expand_smacro(tline->next);
4302 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004303
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04004304 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004305 t = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004306
4307 t = skip_white(t);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004308
H. Peter Anvine2c80182005-01-15 22:15:51 +00004309 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004310 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004311 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004312 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004313 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004314 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004315
H. Peter Anvin8b262472019-02-26 14:00:54 -08004316 pps.tptr = t->next;
4317 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004318 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004319 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004320 if (!evalresult) {
4321 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004322 goto done;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004323 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004324 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004325 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004326 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004327 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004328 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004329
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004330 pps.tptr = skip_white(pps.tptr);
H. Peter Anvin8b262472019-02-26 14:00:54 -08004331 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004332 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004333 } else {
4334 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004335 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004336 if (!evalresult) {
4337 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004338 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004339 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004340 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004341 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004342 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004343 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004344 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004345 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004346
H. Peter Anvin8571f062019-09-23 16:40:03 -07004347 unquote_token(t);
4348 len = t->len;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004349
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04004350 /* make start and count being in range */
4351 if (start < 0)
4352 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004353 if (count < 0)
4354 count = len + count + 1 - start;
4355 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04004356 count = len - start;
4357 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004358 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004359
H. Peter Anvin8571f062019-09-23 16:40:03 -07004360 txt = (start < 0) ? "" : tok_text(t) + start;
4361 len = count;
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07004362 macro_start = make_tok_qstr_len(NULL, txt, len);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004363
H. Peter Anvine2c80182005-01-15 22:15:51 +00004364 /*
4365 * We now have a macro name, an implicit parameter count of
4366 * zero, and a numeric token to use as an expansion. Create
4367 * and store an SMacro.
4368 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004369 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004370 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004371 break;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004372 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004373
H. Peter Anvin8b262472019-02-26 14:00:54 -08004374 case PP_ASSIGN:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004375 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004376 goto done;
4377
H. Peter Anvine2c80182005-01-15 22:15:51 +00004378 last = tline;
4379 tline = expand_smacro(tline->next);
4380 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004381
H. Peter Anvin8b262472019-02-26 14:00:54 -08004382 pps.tptr = tline;
4383 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004384 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004385 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004386 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004387 if (!evalresult)
4388 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004389
H. Peter Anvine2c80182005-01-15 22:15:51 +00004390 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004391 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00004392
H. Peter Anvine2c80182005-01-15 22:15:51 +00004393 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004394 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004395 free_tlist(origline);
4396 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004397 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004398
H. Peter Anvin8571f062019-09-23 16:40:03 -07004399 macro_start = make_tok_num(NULL, reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00004400
H. Peter Anvine2c80182005-01-15 22:15:51 +00004401 /*
4402 * We now have a macro name, an implicit parameter count of
4403 * zero, and a numeric token to use as an expansion. Create
4404 * and store an SMacro.
4405 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004406 define_smacro(mname, casesense, macro_start, NULL);
4407 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004408
H. Peter Anvind2354082019-08-27 16:38:48 -07004409 case PP_ALIASES:
4410 tline = tline->next;
4411 tline = expand_smacro(tline);
4412 do_aliases = pp_get_boolean_option(tline, do_aliases);
4413 break;
4414
H. Peter Anvine2c80182005-01-15 22:15:51 +00004415 case PP_LINE:
4416 /*
4417 * Syntax is `%line nnn[+mmm] [filename]'
4418 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004419 if (unlikely(pp_noline))
4420 goto done;
4421
H. Peter Anvine2c80182005-01-15 22:15:51 +00004422 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004423 tline = skip_white(tline);
4424 if (!tok_type(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004425 nasm_nonfatal("`%s' expects line number", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004426 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004427 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07004428 k = readnum(tok_text(tline), &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004429 m = 1;
4430 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004431 if (tok_is(tline, '+')) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004432 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004433 if (!tok_type(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004434 nasm_nonfatal("`%s' expects line increment", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004435 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004436 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07004437 m = readnum(tok_text(tline), &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004438 tline = tline->next;
4439 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004440 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004441 src_set_linnum(k);
4442 istk->lineinc = m;
4443 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07004444 char *fname = detoken(tline, false);
4445 src_set_fname(fname);
4446 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004447 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004448 break;
4449 }
4450
4451done:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004452 free_tlist(origline);
4453 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004454}
4455
4456/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00004457 * Ensure that a macro parameter contains a condition code and
4458 * nothing else. Return the condition code index if so, or -1
4459 * otherwise.
4460 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004461static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004462{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004463 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004464
H. Peter Anvin25a99342007-09-22 17:45:45 -07004465 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004466 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07004467
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004468 t = skip_white(t);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004469 if (!tok_type(t, TOK_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004470 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004471 tt = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004472 tt = skip_white(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004473 if (tok_isnt(tt, ','))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004474 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004475
H. Peter Anvin8571f062019-09-23 16:40:03 -07004476 return bsii(tok_text(t), (const char **)conditions,
4477 ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00004478}
4479
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004480static inline bool pp_concat_match(const Token *t, unsigned int mask)
4481{
4482 return t && (PP_CONCAT_MASK(t->type) & mask);
4483}
4484
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004485/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004486 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004487 * pasting, if @handle_explicit passed then explicit pasting
4488 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004489 * The @m array can contain a series of token types which are
4490 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004491 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004492static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004493 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07004494{
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004495 Token *tok, *t, *next, **prev_next, **prev_nonspace;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004496 bool pasted = false;
4497 char *buf, *p;
4498 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07004499
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004500 /*
4501 * The last token before pasting. We need it
4502 * to be able to connect new handled tokens.
4503 * In other words if there were a tokens stream
4504 *
4505 * A -> B -> C -> D
4506 *
4507 * and we've joined tokens B and C, the resulting
4508 * stream should be
4509 *
4510 * A -> BC -> D
4511 */
4512 tok = *head;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004513 prev_next = prev_nonspace = head;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004514
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004515 if (tok_white(tok) || tok_type(tok, TOK_PASTE))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004516 prev_nonspace = NULL;
4517
4518 while (tok && (next = tok->next)) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004519 bool did_paste = false;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004520
4521 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07004522 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004523 /* Zap redundant whitespaces */
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004524 tok->next = next = zap_white(next);
H. Peter Anvind784a082009-04-20 14:01:18 -07004525 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004526
4527 case TOK_PASTE:
4528 /* Explicit pasting */
4529 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004530 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004531
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004532 /* Left pasting token is start of line */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004533 if (!prev_nonspace) {
4534 nasm_nonfatal("No lvalue found on pasting");
4535 tok = delete_Token(tok);
4536 break;
4537 }
4538
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004539 did_paste = true;
4540
4541 prev_next = prev_nonspace;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004542 t = *prev_nonspace;
4543
4544 /* Delete leading whitespace */
4545 next = zap_white(t->next);
4546
4547 /* Delete the %+ token itself */
4548 nasm_assert(next == tok);
4549 next = delete_Token(next);
4550
4551 /* Delete trailing whitespace */
4552 next = zap_white(next);
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004553
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004554 /*
4555 * No ending token, this might happen in two
4556 * cases
4557 *
4558 * 1) There indeed no right token at all
4559 * 2) There is a bare "%define ID" statement,
4560 * and @ID does expand to whitespace.
4561 *
4562 * So technically we need to do a grammar analysis
4563 * in another stage of parsing, but for now lets don't
4564 * change the behaviour people used to. Simply allow
4565 * whitespace after paste token.
4566 */
4567 if (!next) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004568 *prev_nonspace = tok = NULL; /* End of line */
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004569 break;
4570 }
4571
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004572 p = buf = nasm_malloc(t->len + next->len + 1);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004573 p = mempcpy(p, tok_text(t), t->len);
4574 p = mempcpy(p, tok_text(next), next->len);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004575 *p = '\0';
4576 delete_Token(t);
4577 t = tokenize(buf);
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004578 nasm_free(buf);
4579
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004580 if (unlikely(!t)) {
4581 /*
4582 * No output at all? Replace with a single whitespace.
4583 * This should never happen.
4584 */
4585 t = new_White(NULL);
4586 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004587
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004588 *prev_nonspace = tok = t;
4589 while (t->next)
4590 t = t->next; /* Find the last token produced */
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004591
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004592 /* Delete the second token and attach to the end of the list */
4593 t->next = delete_Token(next);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004594
4595 /* We want to restart from the head of the pasted token */
4596 next = tok;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004597 break;
4598
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004599 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004600 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004601 for (i = 0; i < mnum; i++) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004602 if (pp_concat_match(tok, m[i].mask_head))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004603 break;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004604 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004605
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004606 if (i >= mnum)
4607 break;
4608
4609 len = tok->len;
4610 while (pp_concat_match(next, m[i].mask_tail)) {
4611 len += next->len;
4612 next = next->next;
4613 }
4614
4615 /* No match or no text to process */
4616 if (len == tok->len)
4617 break;
4618
4619 p = buf = nasm_malloc(len + 1);
4620 while (tok != next) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004621 p = mempcpy(p, tok_text(tok), tok->len);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004622 tok = delete_Token(tok);
4623 }
4624 *p = '\0';
4625 *prev_next = tok = t = tokenize(buf);
4626 nasm_free(buf);
4627
4628 /*
4629 * Connect pasted into original stream,
4630 * ie A -> new-tokens -> B
4631 */
4632 while (t->next)
4633 t = t->next;
4634 t->next = next;
4635 prev_next = prev_nonspace = &t->next;
4636 did_paste = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004637 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004638 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004639
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004640 if (did_paste) {
4641 pasted = true;
4642 } else {
4643 prev_next = &tok->next;
4644 if (next && next->type != TOK_WHITESPACE && next->type != TOK_PASTE)
4645 prev_nonspace = prev_next;
4646 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004647
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004648 tok = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004649 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004650
4651 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07004652}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004653
4654/*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004655 * Computes the proper rotation of mmacro parameters
4656 */
4657static int mmac_rotate(const MMacro *mac, unsigned int n)
4658{
4659 if (--n < mac->nparam)
4660 n = (n + mac->rotate) % mac->nparam;
4661
4662 return n+1;
4663}
4664
4665/*
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004666 * expands to a list of tokens from %{x:y}
4667 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004668static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004669{
4670 Token *t = tline, **tt, *tm, *head;
4671 char *pos;
4672 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004673
H. Peter Anvin8571f062019-09-23 16:40:03 -07004674 pos = strchr(tok_text(tline), ':');
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004675 nasm_assert(pos);
4676
4677 lst = atoi(pos + 1);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004678 fst = atoi(tok_text(tline) + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004679
4680 /*
4681 * only macros params are accounted so
4682 * if someone passes %0 -- we reject such
4683 * value(s)
4684 */
4685 if (lst == 0 || fst == 0)
4686 goto err;
4687
4688 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004689 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
4690 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004691 goto err;
4692
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004693 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
4694 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004695
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004696 /* count from zero */
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004697 fst--, lst--;
4698
4699 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004700 * It will be at least one token. Note we
4701 * need to scan params until separator, otherwise
4702 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004703 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004704 j = (fst + mac->rotate) % mac->nparam;
4705 tm = mac->params[j+1];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03004706 if (!tm)
4707 goto err;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004708 head = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004709 tt = &head->next, tm = tm->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004710 while (tok_isnt(tm, ',')) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004711 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004712 *tt = t, tt = &t->next, tm = tm->next;
4713 }
4714
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004715 if (fst < lst) {
4716 for (i = fst + 1; i <= lst; i++) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004717 t = make_tok_char(NULL, ',');
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004718 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004719 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004720 tm = mac->params[j+1];
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004721 while (tok_isnt(tm, ',')) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004722 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004723 *tt = t, tt = &t->next, tm = tm->next;
4724 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004725 }
4726 } else {
4727 for (i = fst - 1; i >= lst; i--) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004728 t = make_tok_char(NULL, ',');
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004729 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004730 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004731 tm = mac->params[j+1];
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004732 while (!tok_isnt(tm, ',')) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004733 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004734 *tt = t, tt = &t->next, tm = tm->next;
4735 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004736 }
4737 }
4738
4739 *last = tt;
4740 return head;
4741
4742err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004743 nasm_nonfatal("`%%{%s}': macro parameters out of range",
H. Peter Anvin8571f062019-09-23 16:40:03 -07004744 tok_text(tline) + 1);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004745 return NULL;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004746}
4747
H. Peter Anvin76690a12002-04-30 20:52:49 +00004748/*
4749 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004750 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004751 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004752 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004753static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004754{
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004755 Token **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004756 bool changed = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004757 MMacro *mac = istk->mstk.mmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004758
4759 tail = &thead;
4760 thead = NULL;
4761
H. Peter Anvine2c80182005-01-15 22:15:51 +00004762 while (tline) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004763 bool change;
H. Peter Anvin (Intel)a762cd42020-06-01 11:49:08 -07004764 bool err_not_mac = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004765 Token *t = tline;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004766 const char *text = tok_text(t);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004767 int type = t->type;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004768
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004769 tline = tline->next;
4770 t->next = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004771
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004772 switch (type) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004773 case TOK_LOCAL_SYMBOL:
H. Peter Anvin (Intel)a762cd42020-06-01 11:49:08 -07004774 change = true;
4775
4776 if (!mac) {
4777 err_not_mac = true;
4778 break;
4779 }
4780
H. Peter Anvin8571f062019-09-23 16:40:03 -07004781 type = TOK_ID;
4782 text = nasm_asprintf("..@%"PRIu64".%s", mac->unique, text+2);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004783 break;
4784 case TOK_MMACRO_PARAM:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004785 {
4786 Token *tt = NULL;
4787
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004788 change = true;
4789
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004790 if (!mac) {
H. Peter Anvin (Intel)a762cd42020-06-01 11:49:08 -07004791 err_not_mac = true;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004792 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004793 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004794
4795 if (strchr(text, ':')) {
4796 /*
4797 * seems we have a parameters range here
4798 */
4799 Token *head, **last;
4800 head = expand_mmac_params_range(mac, t, &last);
4801 if (head) {
4802 *tail = head;
4803 *last = tline;
4804 text = NULL;
4805 }
4806 break;
4807 }
4808
4809 switch (text[1]) {
4810 /*
4811 * We have to make a substitution of one of the
4812 * forms %1, %-1, %+1, %%foo, %0, %00.
4813 */
4814 case '0':
4815 if (!text[2]) {
4816 type = TOK_NUMBER;
4817 text = nasm_asprintf("%d", mac->nparam);
4818 break;
4819 }
4820 if (text[2] != '0' || text[3])
4821 goto invalid;
4822 /* a possible captured label == mac->params[0] */
4823 /* fall through */
4824 default:
4825 {
4826 unsigned long n;
4827 char *ep;
4828
4829 n = strtoul(text + 1, &ep, 10);
4830 if (unlikely(*ep))
4831 goto invalid;
4832
4833 if (n <= mac->nparam) {
4834 n = mmac_rotate(mac, n);
4835 dup_tlistn(mac->params[n], mac->paramlen[n], &tail);
4836 }
4837 text = NULL;
4838 break;
4839 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004840 case '-':
4841 case '+':
4842 {
4843 int cc;
4844 unsigned long n;
4845 char *ep;
4846
H. Peter Anvin8571f062019-09-23 16:40:03 -07004847 n = strtoul(tok_text(t) + 2, &ep, 10);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004848 if (unlikely(*ep))
4849 goto invalid;
4850
Chang S. Bae057b8322020-04-18 23:11:21 +00004851 if (n && n <= mac->nparam) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004852 n = mmac_rotate(mac, n);
4853 tt = mac->params[n];
4854 }
4855 cc = find_cc(tt);
4856 if (cc == -1) {
4857 nasm_nonfatal("macro parameter `%s' is not a condition code",
H. Peter Anvin8571f062019-09-23 16:40:03 -07004858 tok_text(t));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004859 text = NULL;
4860 break;
4861 }
4862
4863 type = TOK_ID;
4864 if (text[1] == '-') {
4865 int ncc = inverse_ccs[cc];
4866 if (unlikely(ncc == -1)) {
4867 nasm_nonfatal("condition code `%s' is not invertible",
4868 conditions[cc]);
4869 break;
4870 }
4871 cc = ncc;
4872 }
4873 text = nasm_strdup(conditions[cc]);
4874 break;
4875 }
4876
4877 invalid:
4878 nasm_nonfatal("invalid macro parameter: `%s'", text);
4879 text = NULL;
4880 break;
4881 }
4882 break;
4883 }
4884
4885 case TOK_PREPROC_Q:
4886 if (mac) {
4887 type = TOK_ID;
4888 text = nasm_strdup(mac->iname);
4889 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07004890 } else {
4891 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004892 }
4893 break;
4894
4895 case TOK_PREPROC_QQ:
4896 if (mac) {
4897 type = TOK_ID;
4898 text = nasm_strdup(mac->name);
4899 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07004900 } else {
4901 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004902 }
4903 break;
4904
4905 case TOK_INDIRECT:
4906 {
4907 Token *tt;
4908
H. Peter Anvin8571f062019-09-23 16:40:03 -07004909 tt = tokenize(tok_text(t));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004910 tt = expand_mmac_params(tt);
4911 tt = expand_smacro(tt);
4912 /* Why dup_tlist() here? We should own tt... */
4913 dup_tlist(tt, &tail);
4914 text = NULL;
4915 change = true;
4916 break;
4917 }
4918
4919 default:
4920 change = false;
4921 break;
4922 }
4923
H. Peter Anvin (Intel)a762cd42020-06-01 11:49:08 -07004924 if (err_not_mac) {
4925 nasm_nonfatal("`%s': not in a macro call", text);
4926 text = NULL;
4927 change = true;
4928 }
4929
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004930 if (change) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004931 if (!text) {
4932 delete_Token(t);
4933 } else {
4934 *tail = t;
4935 tail = &t->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004936 set_text(t, text, tok_strlen(text));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004937 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004938 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004939 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004940 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004941 *tail = t;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004942 tail = &t->next;
4943 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004944 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004945
H. Peter Anvineba20a72002-04-30 20:53:55 +00004946 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004947
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004948 if (changed) {
4949 const struct tokseq_match t[] = {
4950 {
4951 PP_CONCAT_MASK(TOK_ID) |
4952 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4953 PP_CONCAT_MASK(TOK_ID) |
4954 PP_CONCAT_MASK(TOK_NUMBER) |
4955 PP_CONCAT_MASK(TOK_FLOAT) |
4956 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4957 },
4958 {
4959 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4960 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4961 }
4962 };
4963 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4964 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004965
H. Peter Anvin76690a12002-04-30 20:52:49 +00004966 return thead;
4967}
4968
H. Peter Anvin322bee02019-08-10 01:38:06 -07004969static Token *expand_smacro_noreset(Token * tline);
H. Peter Anvin322bee02019-08-10 01:38:06 -07004970
H. Peter Anvin76690a12002-04-30 20:52:49 +00004971/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004972 * Expand *one* single-line macro instance. If the first token is not
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004973 * a macro at all, it is simply copied to the output and the pointer
4974 * advanced. tpp should be a pointer to a pointer (usually the next
4975 * pointer of the previous token) to the first token. **tpp is updated
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004976 * to point to the first token of the expansion, and *tpp updated to
4977 * point to the next pointer of the last token of the expansion.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004978 *
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004979 * If the expansion is empty, *tpp will be unchanged but **tpp will
4980 * be advanced past the macro call.
4981 *
H. Peter Anvin322bee02019-08-10 01:38:06 -07004982 * Return the macro expanded, or NULL if no expansion took place.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004983 */
H. Peter Anvin322bee02019-08-10 01:38:06 -07004984static SMacro *expand_one_smacro(Token ***tpp)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004985{
4986 Token **params = NULL;
4987 const char *mname;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004988 Token *mstart = **tpp;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004989 Token *tline = mstart;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004990 SMacro *head, *m;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004991 int i;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004992 Token *t, *tup, *tafter;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004993 int nparam = 0;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004994 bool cond_comma;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004995
4996 if (!tline)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004997 return false; /* Empty line, nothing to do */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004998
H. Peter Anvin8571f062019-09-23 16:40:03 -07004999 mname = tok_text(mstart);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005000
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005001 smacro_deadman.total--;
H. Peter Anvin322bee02019-08-10 01:38:06 -07005002 smacro_deadman.levels--;
5003
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005004 if (unlikely(smacro_deadman.total < 0 || smacro_deadman.levels < 0)) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07005005 if (unlikely(!smacro_deadman.triggered)) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005006 nasm_nonfatal("interminable macro recursion");
H. Peter Anvin322bee02019-08-10 01:38:06 -07005007 smacro_deadman.triggered = true;
5008 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005009 goto not_a_macro;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005010 } else if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005011 head = (SMacro *)hash_findix(&smacros, mname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005012 } else if (tline->type == TOK_LOCAL_MACRO) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005013 Context *ctx = get_ctx(mname, &mname);
5014 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
5015 } else {
5016 goto not_a_macro;
5017 }
5018
5019 /*
5020 * We've hit an identifier of some sort. First check whether the
5021 * identifier is a single-line macro at all, then think about
5022 * checking for parameters if necessary.
5023 */
5024 list_for_each(m, head) {
H. Peter Anvind2354082019-08-27 16:38:48 -07005025 if (unlikely(m->alias && !do_aliases))
5026 continue;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005027 if (!mstrcmp(m->name, mname, m->casesense))
5028 break;
5029 }
5030
5031 if (!m) {
5032 goto not_a_macro;
5033 }
5034
5035 /* Parse parameters, if applicable */
5036
5037 params = NULL;
5038 nparam = 0;
5039
5040 if (m->nparam == 0) {
5041 /*
5042 * Simple case: the macro is parameterless.
5043 * Nothing to parse; the expansion code will
5044 * drop the macro name token.
5045 */
5046 } else {
5047 /*
5048 * Complicated case: at least one macro with this name
5049 * exists and takes parameters. We must find the
5050 * parameters in the call, count them, find the SMacro
5051 * that corresponds to that form of the macro call, and
5052 * substitute for the parameters when we expand. What a
5053 * pain.
5054 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005055 Token *t;
5056 int paren, brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005057
5058 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005059 tline = skip_white(tline);
5060 if (!tok_is(tline, '(')) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005061 /*
5062 * This macro wasn't called with parameters: ignore
5063 * the call. (Behaviour borrowed from gnu cpp.)
5064 */
5065 goto not_a_macro;
5066 }
5067
5068 paren = 1;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005069 nparam = 1;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005070 brackets = 0;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005071 t = tline; /* tline points to leading ( */
5072
5073 while (paren) {
5074 t = t->next;
5075
5076 if (!t) {
5077 nasm_nonfatal("macro call expects terminating `)'");
5078 goto not_a_macro;
5079 }
5080
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005081 if (t->type != TOK_OTHER || t->len != 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005082 continue;
5083
H. Peter Anvin8571f062019-09-23 16:40:03 -07005084 switch (t->text.a[0]) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005085 case ',':
5086 if (!brackets)
5087 nparam++;
5088 break;
5089
5090 case '{':
5091 brackets++;
5092 break;
5093
5094 case '}':
5095 if (brackets > 0)
5096 brackets--;
5097 break;
5098
5099 case '(':
5100 if (!brackets)
5101 paren++;
5102 break;
5103
5104 case ')':
5105 if (!brackets)
5106 paren--;
5107 break;
5108
5109 default:
5110 break; /* Normal token */
5111 }
5112 }
5113
5114 /*
5115 * Look for a macro matching in both name and parameter count.
5116 * We already know any matches cannot be anywhere before the
5117 * current position of "m", so there is no reason to
5118 * backtrack.
5119 */
5120 while (1) {
5121 if (!m) {
5122 /*!
5123 *!macro-params-single [on] single-line macro calls with wrong parameter count
5124 *! warns about \i{single-line macros} being invoked
5125 *! with the wrong number of parameters.
5126 */
5127 nasm_warn(WARN_MACRO_PARAMS_SINGLE,
5128 "single-line macro `%s' exists, "
5129 "but not taking %d parameter%s",
5130 mname, nparam, (nparam == 1) ? "" : "s");
5131 goto not_a_macro;
5132 }
5133
5134 if (!mstrcmp(m->name, mname, m->casesense)) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005135 if (nparam == m->nparam)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005136 break; /* It's good */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005137 if (m->greedy && nparam >= m->nparam-1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005138 break; /* Also good */
5139 }
5140 m = m->next;
5141 }
5142 }
5143
5144 if (m->in_progress)
5145 goto not_a_macro;
5146
5147 /* Expand the macro */
5148 m->in_progress = true;
5149
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005150 if (nparam) {
5151 /* Extract parameters */
5152 Token **phead, **pep;
5153 int white = 0;
5154 int brackets = 0;
5155 int paren;
5156 bool bracketed = false;
5157 bool bad_bracket = false;
5158 enum sparmflags flags;
5159
5160 nparam = m->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005161 paren = 1;
5162 nasm_newn(params, nparam);
5163 i = 0;
5164 flags = m->params[i].flags;
5165 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005166 *pep = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005167
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005168 while (paren) {
5169 bool skip;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005170 char ch;
5171
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005172 tline = tline->next;
5173
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005174 if (!tline)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005175 nasm_nonfatal("macro call expects terminating `)'");
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005176
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005177 ch = 0;
5178 skip = false;
5179
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005180
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005181 switch (tline->type) {
5182 case TOK_OTHER:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005183 if (tline->len == 1)
H. Peter Anvin8571f062019-09-23 16:40:03 -07005184 ch = tline->text.a[0];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005185 break;
5186
5187 case TOK_WHITESPACE:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005188 if (!(flags & SPARM_NOSTRIP)) {
5189 if (brackets || *phead)
5190 white++; /* Keep interior whitespace */
5191 skip = true;
5192 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005193 break;
5194
5195 default:
5196 break;
5197 }
5198
5199 switch (ch) {
5200 case ',':
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005201 if (!brackets && !(flags & SPARM_GREEDY)) {
5202 i++;
5203 nasm_assert(i < nparam);
5204 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005205 *pep = NULL;
5206 bracketed = false;
5207 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005208 flags = m->params[i].flags;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005209 }
5210 break;
5211
5212 case '{':
5213 if (!bracketed) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005214 bracketed = !*phead && !(flags & SPARM_NOSTRIP);
5215 skip = bracketed;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005216 }
5217 brackets++;
5218 break;
5219
5220 case '}':
5221 if (brackets > 0) {
5222 if (!--brackets)
5223 skip = bracketed;
5224 }
5225 break;
5226
5227 case '(':
5228 if (!brackets)
5229 paren++;
5230 break;
5231
5232 case ')':
5233 if (!brackets) {
5234 paren--;
5235 if (!paren) {
5236 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005237 i++; /* Found last argument */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005238 }
5239 }
5240 break;
5241
5242 default:
5243 break; /* Normal token */
5244 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005245
5246 if (!skip) {
5247 Token *t;
5248
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005249 bad_bracket |= bracketed && !brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005250
5251 if (white) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005252 *pep = t = new_White(NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005253 pep = &t->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005254 white = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005255 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005256 *pep = t = dup_Token(NULL, tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005257 pep = &t->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005258 }
5259 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005260
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005261 /*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005262 * Possible further processing of parameters. Note that the
5263 * ordering matters here.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005264 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005265 for (i = 0; i < nparam; i++) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005266 enum sparmflags flags = m->params[i].flags;
5267
5268 if (flags & SPARM_EVAL) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005269 /* Evaluate this parameter as a number */
5270 struct ppscan pps;
5271 struct tokenval tokval;
5272 expr *evalresult;
5273 Token *eval_param;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005274
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005275 pps.tptr = eval_param = expand_smacro_noreset(params[i]);
5276 pps.ntokens = -1;
5277 tokval.t_type = TOKEN_INVALID;
5278 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005279
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005280 free_tlist(eval_param);
5281 params[i] = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005282
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005283 if (!evalresult) {
5284 /* Nothing meaningful to do */
5285 } else if (tokval.t_type) {
5286 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
5287 } else if (!is_simple(evalresult)) {
5288 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
5289 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005290 params[i] = make_tok_num(NULL, reloc_value(evalresult));
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005291 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005292 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005293
5294 if (flags & SPARM_STR) {
5295 /* Convert expansion to a quoted string */
5296 char *arg;
5297 Token *qs;
5298
5299 qs = expand_smacro_noreset(params[i]);
5300 arg = detoken(qs, false);
5301 free_tlist(qs);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005302 params[i] = make_tok_qstr(NULL, arg);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005303 nasm_free(arg);
5304 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005305 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005306 }
5307
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005308 /* Note: we own the expansion this returns. */
5309 t = m->expand(m, params, nparam);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005310
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005311 tafter = tline->next; /* Skip past the macro call */
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005312 tline->next = NULL; /* Truncate list at the macro call end */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005313 tline = tafter;
5314
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005315 tup = NULL;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005316 cond_comma = false;
5317
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005318 while (t) {
5319 enum pp_token_type type = t->type;
5320 Token *tnext = t->next;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005321
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005322 switch (type) {
5323 case TOK_PREPROC_Q:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005324 delete_Token(t);
5325 t = dup_Token(tline, mstart);
5326 break;
5327
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005328 case TOK_PREPROC_QQ:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005329 {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005330 size_t mlen = strlen(m->name);
5331 size_t len;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005332 char *p;
5333
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005334 t->type = mstart->type;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005335 if (t->type == TOK_LOCAL_MACRO) {
5336 const char *psp; /* prefix start pointer */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005337 const char *pep; /* prefix end pointer */
H. Peter Anvin8571f062019-09-23 16:40:03 -07005338 size_t plen;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005339
H. Peter Anvin8571f062019-09-23 16:40:03 -07005340 psp = tok_text(mstart);
5341 get_ctx(psp, &pep);
5342 plen = pep - psp;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005343
H. Peter Anvin8571f062019-09-23 16:40:03 -07005344 len = mlen + plen;
5345 p = nasm_malloc(len + 1);
5346 p = mempcpy(p, psp, plen);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005347 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005348 len = mlen;
5349 p = nasm_malloc(len + 1);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005350 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07005351 p = mempcpy(p, m->name, mlen);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005352 *p = '\0';
H. Peter Anvin8571f062019-09-23 16:40:03 -07005353 set_text_free(t, p, len);
5354
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005355 t->next = tline;
5356 break;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005357 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005358
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005359 case TOK_COND_COMMA:
5360 delete_Token(t);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005361 t = cond_comma ? make_tok_char(tline, ',') : NULL;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005362 break;
5363
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005364 case TOK_ID:
5365 case TOK_PREPROC_ID:
H. Peter Anvin8571f062019-09-23 16:40:03 -07005366 case TOK_LOCAL_MACRO:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005367 {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005368 /*
5369 * Chain this into the target line *before* expanding,
5370 * that way we pick up any arguments to the new macro call,
5371 * if applicable.
5372 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005373 Token **tp = &t;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005374 t->next = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005375 expand_one_smacro(&tp);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005376 tline = *tp; /* First token left after any macro call */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005377 break;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005378 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005379 default:
5380 if (is_smac_param(t->type)) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005381 int param = smac_nparam(t->type);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005382 nasm_assert(!tup && param < nparam);
5383 delete_Token(t);
5384 t = NULL;
5385 tup = tnext;
5386 tnext = dup_tlist_reverse(params[param], NULL);
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005387 cond_comma = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005388 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005389 t->next = tline;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005390 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005391 }
5392
5393 if (t) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005394 Token *endt = tline;
5395
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005396 tline = t;
Chang S. Bae95e54a92020-02-06 14:39:22 -08005397 while (!cond_comma && t && t != endt) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005398 cond_comma = t->type != TOK_WHITESPACE;
Chang S. Bae95e54a92020-02-06 14:39:22 -08005399 t = t->next;
5400 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005401 }
5402
5403 if (tnext) {
5404 t = tnext;
5405 } else {
5406 t = tup;
5407 tup = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005408 }
5409 }
5410
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005411 **tpp = tline;
H. Peter Anvin (Intel)6e714962020-06-01 12:21:10 -07005412 for (t = tline; t && t != tafter; t = t->next)
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005413 *tpp = &t->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005414
5415 m->in_progress = false;
5416
5417 /* Don't do this until after expansion or we will clobber mname */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005418 free_tlist(mstart);
H. Peter Anvin322bee02019-08-10 01:38:06 -07005419 goto done;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005420
5421 /*
5422 * No macro expansion needed; roll back to mstart (if necessary)
H. Peter Anvin322bee02019-08-10 01:38:06 -07005423 * and then advance to the next input token. Note that this is
5424 * by far the common case!
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005425 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005426not_a_macro:
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005427 *tpp = &mstart->next;
H. Peter Anvin322bee02019-08-10 01:38:06 -07005428 m = NULL;
5429done:
5430 smacro_deadman.levels++;
5431 if (unlikely(params))
5432 free_tlist_array(params, nparam);
5433 return m;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005434}
5435
5436/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005437 * Expand all single-line macro calls made in the given line.
5438 * Return the expanded version of the line. The original is deemed
5439 * to be destroyed in the process. (In reality we'll just move
5440 * Tokens from input to output a lot of the time, rather than
5441 * actually bothering to destroy and replicate.)
5442 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005443static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005444{
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005445 smacro_deadman.total = nasm_limit[LIMIT_MACRO_TOKENS];
H. Peter Anvin322bee02019-08-10 01:38:06 -07005446 smacro_deadman.levels = nasm_limit[LIMIT_MACRO_LEVELS];
5447 smacro_deadman.triggered = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005448 return expand_smacro_noreset(tline);
5449}
5450
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005451static Token *expand_smacro_noreset(Token *org_tline)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005452{
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005453 Token *tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005454 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005455
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005456 if (!org_tline)
5457 return NULL; /* Empty input */
5458
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005459 /*
5460 * Trick: we should avoid changing the start token pointer since it can
5461 * be contained in "next" field of other token. Because of this
5462 * we allocate a copy of first token and work with it; at the end of
5463 * routine we copy it back
5464 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005465 tline = dup_Token(org_tline->next, org_tline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005466
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005467 /*
5468 * Pretend that we always end up doing expansion on the first pass;
5469 * that way %+ get processed. However, if we process %+ before the
5470 * first pass, we end up with things like MACRO %+ TAIL trying to
5471 * look up the macro "MACROTAIL", which we don't want.
5472 */
5473 expanded = true;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005474 while (true) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005475 static const struct tokseq_match tmatch[] = {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005476 {
5477 PP_CONCAT_MASK(TOK_ID) |
H. Peter Anvin8571f062019-09-23 16:40:03 -07005478 PP_CONCAT_MASK(TOK_LOCAL_MACRO) |
5479 PP_CONCAT_MASK(TOK_ENVIRON) |
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005480 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
5481 PP_CONCAT_MASK(TOK_ID) |
H. Peter Anvin8571f062019-09-23 16:40:03 -07005482 PP_CONCAT_MASK(TOK_LOCAL_MACRO) |
5483 PP_CONCAT_MASK(TOK_ENVIRON) |
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005484 PP_CONCAT_MASK(TOK_PREPROC_ID) |
5485 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
5486 }
5487 };
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005488 Token **tail = &tline;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005489
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005490 while (*tail) /* main token loop */
H. Peter Anvin322bee02019-08-10 01:38:06 -07005491 expanded |= !!expand_one_smacro(&tail);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005492
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005493 if (!expanded)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005494 break; /* Done! */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005495
5496 /*
5497 * Now scan the entire line and look for successive TOK_IDs
5498 * that resulted after expansion (they can't be produced by
5499 * tokenize()). The successive TOK_IDs should be concatenated.
5500 * Also we look for %+ tokens and concatenate the tokens
5501 * before and after them (without white spaces in between).
5502 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005503 if (!paste_tokens(&tline, tmatch, ARRAY_SIZE(tmatch), true))
5504 break; /* Done again! */
5505
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005506 expanded = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005507 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005508
H. Peter Anvin8571f062019-09-23 16:40:03 -07005509 if (!tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005510 /*
5511 * The expression expanded to empty line;
5512 * we can't return NULL because of the "trick" above.
5513 * Just set the line to a single WHITESPACE token.
H. Peter Anvin8571f062019-09-23 16:40:03 -07005514 */
5515
5516 tline = new_White(NULL);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005517 }
5518
H. Peter Anvin8571f062019-09-23 16:40:03 -07005519 steal_Token(org_tline, tline);
5520 org_tline->next = tline->next;
5521 delete_Token(tline);
5522
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005523 return org_tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005524}
5525
5526/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005527 * Similar to expand_smacro but used exclusively with macro identifiers
5528 * right before they are fetched in. The reason is that there can be
5529 * identifiers consisting of several subparts. We consider that if there
5530 * are more than one element forming the name, user wants a expansion,
5531 * otherwise it will be left as-is. Example:
5532 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005533 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005534 *
5535 * the identifier %$abc will be left as-is so that the handler for %define
5536 * will suck it and define the corresponding value. Other case:
5537 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005538 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005539 *
5540 * In this case user wants name to be expanded *before* %define starts
5541 * working, so we'll expand %$abc into something (if it has a value;
5542 * otherwise it will be left as-is) then concatenate all successive
5543 * PP_IDs into one.
5544 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005545static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005546{
5547 Token *cur, *oldnext = NULL;
5548
H. Peter Anvin734b1882002-04-30 21:01:08 +00005549 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005550 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005551
5552 cur = tline;
5553 while (cur->next &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07005554 (cur->next->type == TOK_ID || cur->next->type == TOK_PREPROC_ID ||
5555 cur->next->type == TOK_LOCAL_MACRO || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005556 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005557
5558 /* If identifier consists of just one token, don't expand */
5559 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005560 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005561
H. Peter Anvine2c80182005-01-15 22:15:51 +00005562 if (cur) {
5563 oldnext = cur->next; /* Detach the tail past identifier */
5564 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005565 }
5566
H. Peter Anvin734b1882002-04-30 21:01:08 +00005567 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005568
H. Peter Anvine2c80182005-01-15 22:15:51 +00005569 if (cur) {
5570 /* expand_smacro possibly changhed tline; re-scan for EOL */
5571 cur = tline;
5572 while (cur && cur->next)
5573 cur = cur->next;
5574 if (cur)
5575 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005576 }
5577
5578 return tline;
5579}
5580
5581/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005582 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005583 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005584 * to check for an initial label - that's taken care of in
5585 * expand_mmacro - but must check numbers of parameters. Guaranteed
5586 * to be called with tline->type == TOK_ID, so the putative macro
5587 * name is easy to find.
5588 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005589static MMacro *is_mmacro(Token * tline, int *nparamp, Token ***params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005590{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005591 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005592 Token **params;
5593 int nparam;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005594 const char *finding = tok_text(tline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005595
H. Peter Anvin8571f062019-09-23 16:40:03 -07005596 head = (MMacro *) hash_findix(&mmacros, finding);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005597
5598 /*
5599 * Efficiency: first we see if any macro exists with the given
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005600 * name which isn't already excluded by macro cycle removal.
5601 * (The cycle removal test here helps optimize the case of wrapping
5602 * instructions, and is cheap to do here.)
5603 *
5604 * If not, we can return NULL immediately. _Then_ we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005605 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005606 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005607 */
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005608 list_for_each(m, head) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005609 if (!mstrcmp(m->name, finding, m->casesense) &&
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005610 (m->in_progress != 1 || m->max_depth > 0))
5611 break; /* Found something that needs consideration */
5612 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005613 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005614 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005615
5616 /*
5617 * OK, we have a potential macro. Count and demarcate the
5618 * parameters.
5619 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00005620 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005621
5622 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005623 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005624 * structure that handles this number.
5625 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005626 while (m) {
5627 if (m->nparam_min <= nparam
5628 && (m->plus || nparam <= m->nparam_max)) {
5629 /*
5630 * This one is right. Just check if cycle removal
5631 * prohibits us using it before we actually celebrate...
5632 */
5633 if (m->in_progress > m->max_depth) {
5634 if (m->max_depth > 0) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08005635 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005636 m->max_depth);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005637 }
5638 nasm_free(params);
5639 return NULL;
5640 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005641 /*
5642 * It's right, and we can use it. Add its default
5643 * parameters to the end of our list if necessary.
5644 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005645 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005646 int newnparam = m->nparam_min + m->ndefs;
5647 params = nasm_realloc(params, sizeof(*params) * (newnparam+2));
5648 memcpy(&params[nparam+1], &m->defaults[nparam+1-m->nparam_min],
5649 (newnparam - nparam) * sizeof(*params));
5650 nparam = newnparam;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005651 }
5652 /*
5653 * If we've gone over the maximum parameter count (and
5654 * we're in Plus mode), ignore parameters beyond
5655 * nparam_max.
5656 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005657 if (m->plus && nparam > m->nparam_max)
5658 nparam = m->nparam_max;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005659
H. Peter Anvin (Intel)7eb18212019-08-20 16:24:46 -07005660 /*
5661 * If nparam was adjusted above, make sure the list is still
5662 * NULL-terminated.
5663 */
5664 params[nparam+1] = NULL;
5665
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005666 /* Done! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005667 *params_array = params;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005668 *nparamp = nparam;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005669 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005670 }
5671 /*
5672 * This one wasn't right: look for the next one with the
5673 * same name.
5674 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005675 list_for_each(m, m->next)
H. Peter Anvin8571f062019-09-23 16:40:03 -07005676 if (!mstrcmp(m->name, tok_text(tline), m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005677 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005678 }
5679
5680 /*
5681 * After all that, we didn't find one with the right number of
5682 * parameters. Issue a warning, and fail to expand the macro.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005683 *!
5684 *!macro-params-multi [on] multi-line macro calls with wrong parameter count
5685 *! warns about \i{multi-line macros} being invoked
5686 *! with the wrong number of parameters. See \k{mlmacover} for an
5687 *! example of why you might want to disable this warning.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005688 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005689 nasm_warn(WARN_MACRO_PARAMS_MULTI,
5690 "multi-line macro `%s' exists, but not taking %d parameter%s",
H. Peter Anvin8571f062019-09-23 16:40:03 -07005691 tok_text(tline), nparam, (nparam == 1) ? "" : "s");
H. Peter Anvin734b1882002-04-30 21:01:08 +00005692 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005693 return NULL;
5694}
5695
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005696
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005697#if 0
5698
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005699/*
5700 * Save MMacro invocation specific fields in
5701 * preparation for a recursive macro expansion
5702 */
5703static void push_mmacro(MMacro *m)
5704{
5705 MMacroInvocation *i;
5706
5707 i = nasm_malloc(sizeof(MMacroInvocation));
5708 i->prev = m->prev;
5709 i->params = m->params;
5710 i->iline = m->iline;
5711 i->nparam = m->nparam;
5712 i->rotate = m->rotate;
5713 i->paramlen = m->paramlen;
5714 i->unique = m->unique;
5715 i->condcnt = m->condcnt;
5716 m->prev = i;
5717}
5718
5719
5720/*
5721 * Restore MMacro invocation specific fields that were
5722 * saved during a previous recursive macro expansion
5723 */
5724static void pop_mmacro(MMacro *m)
5725{
5726 MMacroInvocation *i;
5727
5728 if (m->prev) {
5729 i = m->prev;
5730 m->prev = i->prev;
5731 m->params = i->params;
5732 m->iline = i->iline;
5733 m->nparam = i->nparam;
5734 m->rotate = i->rotate;
5735 m->paramlen = i->paramlen;
5736 m->unique = i->unique;
5737 m->condcnt = i->condcnt;
5738 nasm_free(i);
5739 }
5740}
5741
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005742#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005743
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005744/*
H. Peter Anvin (Intel)ffe89dd2019-08-20 16:06:36 -07005745 * List an mmacro call with arguments (-Lm option)
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005746 */
5747static void list_mmacro_call(const MMacro *m)
5748{
5749 const char prefix[] = " ;;; [macro] ";
5750 size_t namelen, size;
5751 char *buf, *p;
5752 unsigned int i;
5753 const Token *t;
5754
5755 namelen = strlen(m->iname);
5756 size = namelen + sizeof(prefix); /* Includes final null (from prefix) */
5757
5758 for (i = 1; i <= m->nparam; i++) {
5759 int j = 0;
5760 size += 3; /* Braces and space/comma */
5761 list_for_each(t, m->params[i]) {
5762 if (j++ >= m->paramlen[i])
5763 break;
5764 size += (t->type == TOK_WHITESPACE) ? 1 : t->len;
5765 }
5766 }
5767
5768 buf = p = nasm_malloc(size);
5769 p = mempcpy(p, prefix, sizeof(prefix) - 1);
5770 p = mempcpy(p, m->iname, namelen);
5771 *p++ = ' ';
5772
5773 for (i = 1; i <= m->nparam; i++) {
5774 int j = 0;
5775 *p++ = '{';
5776 list_for_each(t, m->params[i]) {
5777 if (j++ >= m->paramlen[i])
5778 break;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005779 p = mempcpy(p, tok_text(t), t->len);
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005780 }
5781 *p++ = '}';
5782 *p++ = ',';
5783 }
5784
5785 *--p = '\0'; /* Replace last delimeter with null */
5786 lfmt->line(LIST_MACRO, -1, buf);
5787 nasm_free(buf);
5788}
5789
5790/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005791 * Expand the multi-line macro call made by the given line, if
5792 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005793 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005794 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005795static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005796{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005797 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005798 Token *label = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005799 bool dont_prepend = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005800 Token **params, *t, *tt;
5801 MMacro *m;
5802 Line *l, *ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005803 int i, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07005804 const char *mname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005805 int nparam = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005806
5807 t = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005808 t = skip_white(t);
5809 /* if (!tok_type(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07005810 if (!tok_type(t, TOK_ID) && !tok_type(t, TOK_LOCAL_MACRO))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005811 return 0;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005812 m = is_mmacro(t, &nparam, &params);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005813 if (m) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005814 mname = tok_text(t);
H. Peter Anvinc751e862008-06-09 10:18:45 -07005815 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005816 Token *last;
5817 /*
5818 * We have an id which isn't a macro call. We'll assume
5819 * it might be a label; we'll also check to see if a
5820 * colon follows it. Then, if there's another id after
5821 * that lot, we'll check it again for macro-hood.
5822 */
5823 label = last = t;
5824 t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005825 if (tok_white(t))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005826 last = t, t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005827 if (tok_is(t, ':')) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005828 dont_prepend = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005829 last = t, t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005830 if (tok_white(t))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005831 last = t, t = t->next;
5832 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005833 if (!tok_type(t, TOK_ID) || !(m = is_mmacro(t, &nparam, &params)))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005834 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005835 last->next = NULL;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005836 mname = tok_text(t);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005837 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005838 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005839
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005840 if (unlikely(mmacro_deadman.total >= nasm_limit[LIMIT_MMACROS] ||
5841 mmacro_deadman.levels >= nasm_limit[LIMIT_MACRO_LEVELS])) {
5842 if (!mmacro_deadman.triggered) {
5843 nasm_nonfatal("interminable multiline macro recursion");
5844 mmacro_deadman.triggered = true;
5845 }
5846 return 0;
5847 }
5848
5849 mmacro_deadman.total++;
5850 mmacro_deadman.levels++;
5851
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005852 /*
5853 * Fix up the parameters: this involves stripping leading and
5854 * trailing whitespace, then stripping braces if they are
5855 * present.
5856 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005857 nasm_newn(paramlen, nparam+1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005858
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005859 nasm_assert(params[nparam+1] == NULL);
5860
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005861 for (i = 1; (t = params[i]); i++) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005862 bool braced = false;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005863 int brace = 0;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005864 int white = 0;
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005865 bool comma = !m->plus || i < nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005866
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005867 t = skip_white(t);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005868 if (tok_is(t, '{')) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005869 t = t->next;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005870 brace = 1;
5871 braced = true;
5872 comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005873 }
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005874
5875 params[i] = t;
5876 for (; t; t = t->next) {
5877 if (tok_white(t)) {
5878 white++;
5879 continue;
5880 }
5881
5882 if (t->type == TOK_OTHER && t->len == 1) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005883 switch (t->text.a[0]) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005884 case ',':
5885 if (comma && !brace)
5886 goto endparam;
5887 break;
5888
5889 case '{':
5890 brace++;
5891 break;
5892
5893 case '}':
5894 brace--;
5895 if (braced && !brace) {
5896 paramlen[i] += white;
5897 goto endparam;
5898 }
5899 break;
5900
5901 default:
5902 break;
5903 }
5904 }
5905
5906 paramlen[i] += white + 1;
5907 white = 0;
5908 }
5909 endparam:
5910 ;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005911 }
5912
5913 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005914 * OK, we have a MMacro structure together with a set of
5915 * parameters. We must now go through the expansion and push
5916 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00005917 * parameter tokens and macro-local tokens doesn't get done
5918 * until the single-line macro substitution process; this is
5919 * because delaying them allows us to change the semantics
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005920 * later through %rotate and give the right semantics for
5921 * nested mmacros.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005922 *
5923 * First, push an end marker on to istk->expansion, mark this
5924 * macro as in progress, and set up its invocation-specific
5925 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005926 */
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005927 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005928 ll->next = istk->expansion;
5929 ll->finishes = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005930 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005931
5932 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005933 * Save the previous MMacro expansion in the case of
5934 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005935 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005936#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005937 if (m->max_depth && m->in_progress)
5938 push_mmacro(m);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005939#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005940
5941 m->in_progress ++;
5942 m->params = params;
5943 m->iline = tline;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005944 m->iname = nasm_strdup(mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005945 m->nparam = nparam;
5946 m->rotate = 0;
5947 m->paramlen = paramlen;
5948 m->unique = unique++;
5949 m->lineno = 0;
5950 m->condcnt = 0;
5951
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005952 m->mstk = istk->mstk;
5953 istk->mstk.mstk = istk->mstk.mmac = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005954
5955 list_for_each(l, m->expansion) {
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005956 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005957 ll->next = istk->expansion;
5958 istk->expansion = ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005959 ll->first = dup_tlist(l->first, NULL);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005960 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005961
5962 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005963 * If we had a label, and this macro definition does not include
5964 * a %00, push it on as the first line of, ot
H. Peter Anvineba20a72002-04-30 20:53:55 +00005965 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005966 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005967 if (label) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005968 /*
5969 * We had a label. If this macro contains an %00 parameter,
5970 * save the value as a special parameter (which is what it
5971 * is), otherwise push it as the first line of the macro
5972 * expansion.
5973 */
5974 if (m->capture_label) {
5975 params[0] = dup_Token(NULL, label);
5976 paramlen[0] = 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005977 free_tlist(startline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005978 } else {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005979 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005980 ll->finishes = NULL;
5981 ll->next = istk->expansion;
5982 istk->expansion = ll;
5983 ll->first = startline;
5984 if (!dont_prepend) {
5985 while (label->next)
5986 label = label->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005987 label->next = tt = make_tok_char(NULL, ':');
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005988 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005989 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005990 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005991
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07005992 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005993
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005994 if (list_option('m') && !m->nolist)
5995 list_mmacro_call(m);
5996
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005997 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005998}
5999
H. Peter Anvin130736c2016-02-17 20:27:41 -08006000/*
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006001 * This function decides if an error message should be suppressed.
6002 * It will never be called with a severity level of ERR_FATAL or
6003 * higher.
H. Peter Anvin130736c2016-02-17 20:27:41 -08006004 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006005static bool pp_suppress_error(errflags severity)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02006006{
H. Peter Anvin130736c2016-02-17 20:27:41 -08006007 /*
6008 * If we're in a dead branch of IF or something like it, ignore the error.
6009 * However, because %else etc are evaluated in the state context
6010 * of the previous branch, errors might get lost:
6011 * %if 0 ... %else trailing garbage ... %endif
6012 * So %else etc should set the ERR_PP_PRECOND flag.
6013 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006014 if (istk && istk->conds &&
H. Peter Anvin130736c2016-02-17 20:27:41 -08006015 ((severity & ERR_PP_PRECOND) ?
6016 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07006017 !emitting(istk->conds->state)))
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006018 return true;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02006019
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006020 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00006021}
6022
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006023static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006024stdmac_file(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006025{
6026 (void)s;
6027 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006028 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006029
H. Peter Anvin8571f062019-09-23 16:40:03 -07006030 return make_tok_qstr(NULL, src_get_fname());
H. Peter Anvin8b262472019-02-26 14:00:54 -08006031}
6032
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006033static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006034stdmac_line(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006035{
6036 (void)s;
6037 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006038 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006039
H. Peter Anvin8571f062019-09-23 16:40:03 -07006040 return make_tok_num(NULL, src_get_linnum());
H. Peter Anvin8b262472019-02-26 14:00:54 -08006041}
6042
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006043static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006044stdmac_bits(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006045{
6046 (void)s;
6047 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006048 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006049
H. Peter Anvin8571f062019-09-23 16:40:03 -07006050 return make_tok_num(NULL, globalbits);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006051}
6052
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006053static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006054stdmac_ptr(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006055{
H. Peter Anvin8b262472019-02-26 14:00:54 -08006056 (void)s;
6057 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006058 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006059
6060 switch (globalbits) {
6061 case 16:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006062 return new_Token(NULL, TOK_ID, "word", 4);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006063 case 32:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006064 return new_Token(NULL, TOK_ID, "dword", 5);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006065 case 64:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006066 return new_Token(NULL, TOK_ID, "qword", 5);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006067 default:
6068 panic();
6069 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08006070}
6071
H. Peter Anvin8b262472019-02-26 14:00:54 -08006072/* Add magic standard macros */
6073struct magic_macros {
6074 const char *name;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006075 int nparam;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006076 ExpandSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006077};
6078static const struct magic_macros magic_macros[] =
6079{
H. Peter Anvind2354082019-08-27 16:38:48 -07006080 { "__?FILE?__", 0, stdmac_file },
6081 { "__?LINE?__", 0, stdmac_line },
6082 { "__?BITS?__", 0, stdmac_bits },
6083 { "__?PTR?__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08006084 { NULL, 0, NULL }
6085};
6086
6087static void pp_add_magic_stdmac(void)
6088{
6089 const struct magic_macros *m;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006090 SMacro tmpl;
6091
6092 nasm_zero(tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006093
6094 for (m = magic_macros; m->name; m++) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006095 tmpl.nparam = m->nparam;
6096 tmpl.expand = m->func;
6097 define_smacro(m->name, true, NULL, &tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006098 }
6099}
6100
H. Peter Anvin734b1882002-04-30 21:01:08 +00006101static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006102pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006103{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006104 int apass;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006105 struct Include *inc;
H. Peter Anvin7383b402008-09-24 10:20:40 -07006106
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006107 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006108 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07006109 nested_mac_count = 0;
6110 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07006111 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006112 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07006113 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006114 pp_mode = mode;
H. Peter Anvind2354082019-08-27 16:38:48 -07006115 do_aliases = true;
H. Peter Anvinf7606612016-07-13 14:23:48 -07006116
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07006117 if (!use_loaded)
6118 use_loaded = nasm_malloc(use_package_count * sizeof(bool));
6119 memset(use_loaded, 0, use_package_count * sizeof(bool));
6120
H. Peter Anvin6686de22019-08-10 05:33:14 -07006121 /* First set up the top level input file */
6122 nasm_new(istk);
6123 istk->fp = nasm_open_read(file, NF_TEXT);
6124 src_set(0, file);
6125 istk->lineinc = 1;
6126 if (!istk->fp)
6127 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
6128
6129 strlist_add(deplist, file);
6130
6131 /*
6132 * Set up the stdmac packages as a virtual include file,
6133 * indicated by a null file pointer.
6134 */
6135 nasm_new(inc);
6136 inc->next = istk;
6137 inc->fname = src_set_fname(NULL);
6138 inc->nolist = !list_option('b');
6139 istk = inc;
6140 lfmt->uplevel(LIST_INCLUDE, 0);
6141
H. Peter Anvin8b262472019-02-26 14:00:54 -08006142 pp_add_magic_stdmac();
6143
H. Peter Anvinf7606612016-07-13 14:23:48 -07006144 if (tasm_compatible_mode)
6145 pp_add_stdmac(nasm_stdmac_tasm);
6146
6147 pp_add_stdmac(nasm_stdmac_nasm);
6148 pp_add_stdmac(nasm_stdmac_version);
6149
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006150 if (extrastdmac)
6151 pp_add_stdmac(extrastdmac);
6152
H. Peter Anvinf7606612016-07-13 14:23:48 -07006153 stdmacpos = stdmacros[0];
6154 stdmacnext = &stdmacros[1];
6155
H. Peter Anvind2456592008-06-19 15:04:18 -07006156 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006157
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006158 /*
H. Peter Anvind2354082019-08-27 16:38:48 -07006159 * Define the __?PASS?__ macro. This is defined here unlike all the
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07006160 * other builtins, because it is special -- it varies between
6161 * passes -- but there is really no particular reason to make it
6162 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006163 *
6164 * 0 = dependencies only
6165 * 1 = preparatory passes
6166 * 2 = final pass
6167 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006168 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006169 switch (mode) {
6170 case PP_NORMAL:
6171 apass = pass_final() ? 2 : 1;
6172 break;
6173 case PP_DEPS:
6174 apass = 0;
6175 break;
6176 case PP_PREPROC:
6177 apass = 3;
6178 break;
6179 default:
6180 panic();
6181 }
6182
H. Peter Anvin8571f062019-09-23 16:40:03 -07006183 define_smacro("__?PASS?__", true, make_tok_num(NULL, apass), NULL);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006184}
6185
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006186static void pp_init(void)
6187{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006188}
6189
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006190/*
6191 * Get a line of tokens. If we popped the macro expansion/include stack,
6192 * we return a pointer to the dummy token tok_pop; at that point if
6193 * istk is NULL then we have reached end of input;
6194 */
6195static Token tok_pop; /* Dummy token placeholder */
6196
6197static Token *pp_tokline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006198{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006199 while (true) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006200 Line *l = istk->expansion;
6201 Token *tline = NULL;
6202 Token *dtline;
6203
H. Peter Anvine2c80182005-01-15 22:15:51 +00006204 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006205 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00006206 * buffer or from the input file.
6207 */
6208 tline = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006209 while (l && l->finishes) {
6210 MMacro *fm = l->finishes;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006211
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006212 if (!fm->name && fm->in_progress > 1) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006213 /*
6214 * This is a macro-end marker for a macro with no
6215 * name, which means it's not really a macro at all
6216 * but a %rep block, and the `in_progress' field is
6217 * more than 1, meaning that we still need to
6218 * repeat. (1 means the natural last repetition; 0
6219 * means termination by %exitrep.) We have
6220 * therefore expanded up to the %endrep, and must
6221 * push the whole block on to the expansion buffer
6222 * again. We don't bother to remove the macro-end
6223 * marker: we'd only have to generate another one
6224 * if we did.
6225 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006226 fm->in_progress--;
6227 list_for_each(l, fm->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006228 Token *t, *tt, **tail;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006229 Line *ll;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006230
Chang S. Baebec812f2020-02-07 15:49:38 -08006231 istk->mstk.mstk->lineno = 0;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006232 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006233 ll->next = istk->expansion;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006234 tail = &ll->first;
6235
6236 list_for_each(t, l->first) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006237 if (t->len) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006238 tt = *tail = dup_Token(NULL, t);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006239 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006240 }
6241 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006242 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006243 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006244 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006245 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006246 MMacro *m = istk->mstk.mstk;
6247
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006248 /*
6249 * Check whether a `%rep' was started and not ended
6250 * within this macro expansion. This can happen and
6251 * should be detected. It's a fatal error because
6252 * I'm too confused to work out how to recover
6253 * sensibly from it.
6254 */
6255 if (defining) {
6256 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07006257 nasm_panic("defining with name in expansion");
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006258 else if (m->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07006259 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006260 " expansion of macro `%s'", m->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006261 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006262
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006263 /*
6264 * FIXME: investigate the relationship at this point between
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006265 * istk->mstk.mstk and fm
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006266 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006267 istk->mstk = m->mstk;
6268 if (m->name) {
6269 /*
6270 * This was a real macro call, not a %rep, and
6271 * therefore the parameter information needs to
6272 * be freed and the iteration count/nesting
6273 * depth adjusted.
6274 */
6275
6276 if (!--mmacro_deadman.levels) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006277 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006278 * If all mmacro processing done,
6279 * clear all counters and the deadman
6280 * message trigger.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006281 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006282 nasm_zero(mmacro_deadman); /* Clear all counters */
Adam Majer91e72402017-07-25 10:42:01 +02006283 }
6284
Adam Majer91e72402017-07-25 10:42:01 +02006285#if 0
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006286 if (m->prev) {
6287 pop_mmacro(m);
6288 fm->in_progress --;
6289 } else
Adam Majer91e72402017-07-25 10:42:01 +02006290#endif
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006291 {
6292 nasm_free(m->params);
6293 free_tlist(m->iline);
6294 nasm_free(m->paramlen);
6295 fm->in_progress = 0;
6296 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006297 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006298
6299 /*
6300 * FIXME It is incorrect to always free_mmacro here.
6301 * It leads to usage-after-free.
6302 *
6303 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
6304 */
6305#if 0
6306 else
6307 free_mmacro(m);
6308#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006309 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006310 istk->expansion = l->next;
6311 nasm_free(l);
6312 lfmt->downlevel(LIST_MACRO);
6313 return &tok_pop;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006314 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006315
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006316 do { /* until we get a line we can use */
6317 char *line;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006318
6319 if (istk->expansion) { /* from a macro expansion */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006320 Line *l = istk->expansion;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006321 int32_t lineno;
6322
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006323 if (istk->mstk.mstk) {
6324 istk->mstk.mstk->lineno++;
6325 if (istk->mstk.mstk->fname)
6326 lineno = istk->mstk.mstk->lineno +
6327 istk->mstk.mstk->xline;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006328 else
6329 lineno = 0; /* Defined at init time or builtin */
6330 } else {
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006331 lineno = src_get_linnum();
H. Peter Anvin6686de22019-08-10 05:33:14 -07006332 }
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006333
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006334 tline = l->first;
6335 istk->expansion = l->next;
6336 nasm_free(l);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006337
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006338 line = detoken(tline, false);
H. Peter Anvin6686de22019-08-10 05:33:14 -07006339 if (!istk->nolist)
6340 lfmt->line(LIST_MACRO, lineno, line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006341 nasm_free(line);
6342 } else if ((line = read_line())) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006343 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006344 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006345 nasm_free(line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006346 } else {
6347 /*
6348 * The current file has ended; work down the istk
6349 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00006350 Include *i = istk;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006351 if (i->fp)
6352 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006353 if (i->conds) {
6354 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07006355 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006356 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00006357 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07006358 if (i->next)
6359 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006360 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08006361 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006362 nasm_free(i);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006363 return &tok_pop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006364 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006365 } while (0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006366
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006367 /*
6368 * We must expand MMacro parameters and MMacro-local labels
6369 * _before_ we plunge into directive processing, to cope
6370 * with things like `%define something %1' such as STRUC
6371 * uses. Unless we're _defining_ a MMacro, in which case
6372 * those tokens should be left alone to go into the
6373 * definition; and unless we're in a non-emitting
6374 * condition, in which case we don't want to meddle with
6375 * anything.
6376 */
6377 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006378 && !(istk->mstk.mstk && !istk->mstk.mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006379 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006380 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006381
H. Peter Anvine2c80182005-01-15 22:15:51 +00006382 /*
6383 * Check the line to see if it's a preprocessor directive.
6384 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006385 if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) {
6386 if (dtline)
6387 return dtline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006388 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006389 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006390 * We're defining a multi-line macro. We emit nothing
6391 * at all, and just
6392 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00006393 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006394 MMacro *mmac = defining->dstk.mmac;
6395
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006396 Line *l = nasm_malloc(sizeof(Line));
6397 l->next = defining->expansion;
6398 l->first = tline;
6399 l->finishes = NULL;
6400 defining->expansion = l;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006401
6402 /*
6403 * Remember if this mmacro expansion contains %00:
6404 * if it does, we will have to handle leading labels
6405 * specially.
6406 */
6407 if (mmac) {
6408 const Token *t;
6409 list_for_each(t, tline) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006410 if (!memcmp(t->text.a, "%00", 4))
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006411 mmac->capture_label = true;
6412 }
6413 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006414 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006415 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006416 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00006417 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006418 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00006419 * directive so we keep our place correctly.
6420 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006421 free_tlist(tline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006422 } else if (istk->mstk.mstk && !istk->mstk.mstk->in_progress) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006423 /*
6424 * We're in a %rep block which has been terminated, so
6425 * we're walking through to the %endrep without
6426 * emitting anything. Emit nothing at all, not even a
6427 * blank line: when we emerge from the %rep block we'll
6428 * give a line-number directive so we keep our place
6429 * correctly.
6430 */
6431 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006432 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006433 tline = expand_smacro(tline);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006434 if (!expand_mmacro(tline))
6435 return tline;
6436 }
6437 }
6438}
6439
6440static char *pp_getline(void)
6441{
6442 char *line = NULL;
6443 Token *tline;
6444
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006445 while (true) {
6446 tline = pp_tokline();
6447 if (tline == &tok_pop) {
6448 /*
6449 * We popped the macro/include stack. If istk is empty,
6450 * we are at end of input, otherwise just loop back.
6451 */
6452 if (!istk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006453 break;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006454 } else {
6455 /*
6456 * De-tokenize the line and emit it.
6457 */
6458 line = detoken(tline, true);
6459 free_tlist(tline);
6460 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006461 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006462 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006463
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006464 if (list_option('e') && istk && !istk->nolist && line && line[0]) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006465 char *buf = nasm_strcat(" ;;; ", line);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006466 lfmt->line(LIST_MACRO, -1, buf);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006467 nasm_free(buf);
6468 }
6469
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006470 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006471}
6472
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006473static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006474{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006475 if (defining) {
6476 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006477 nasm_nonfatal("end of file while still defining macro `%s'",
6478 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006479 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006480 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006481 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006482
6483 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006484 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006485 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08006486
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006487 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006488 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07006489 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006490 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006491 Include *i = istk;
6492 istk = istk->next;
6493 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03006494 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006495 }
6496 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006497 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07006498 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006499}
6500
6501static void pp_cleanup_session(void)
6502{
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07006503 nasm_free(use_loaded);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006504 free_llist(predef);
6505 predef = NULL;
6506 delete_Blocks();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006507 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006508}
6509
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006510static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006511{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006512 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006513}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00006514
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006515static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006516{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006517 Token *inc, *space, *name;
6518 Line *l;
6519
H. Peter Anvin734b1882002-04-30 21:01:08 +00006520 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006521 space = new_White(name);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006522 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006523
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006524 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006525 l->next = predef;
6526 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006527 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006528 predef = l;
6529}
6530
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006531static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006532{
6533 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006534 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00006535 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006536
6537 equals = strchr(definition, '=');
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006538 space = new_White(NULL);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006539 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006540 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006541 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00006542 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006543 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006544 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006545
H. Peter Anvin8571f062019-09-23 16:40:03 -07006546 /* We can't predefine a TOK_LOCAL_MACRO for obvious reasons... */
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006547 if (space->next->type != TOK_PREPROC_ID &&
6548 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08006549 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006550
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006551 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006552 l->next = predef;
6553 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006554 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006555 predef = l;
6556}
6557
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006558static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00006559{
6560 Token *def, *space;
6561 Line *l;
6562
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006563 space = new_White(NULL);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006564 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006565 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00006566
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006567 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00006568 l->next = predef;
6569 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006570 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00006571 predef = l;
6572}
6573
H. Peter Anvin05990342018-06-11 13:32:42 -07006574/* Insert an early preprocessor command that doesn't need special handling */
6575static void pp_pre_command(const char *what, char *string)
6576{
6577 char *cmd;
6578 Token *def, *space;
6579 Line *l;
6580
6581 def = tokenize(string);
6582 if (what) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006583 space = new_White(def);
H. Peter Anvin8571f062019-09-23 16:40:03 -07006584 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
6585 def = new_Token(space, TOK_PREPROC_ID, cmd, nasm_last_string_len());
6586 nasm_free(cmd);
H. Peter Anvin05990342018-06-11 13:32:42 -07006587 }
6588
6589 l = nasm_malloc(sizeof(Line));
6590 l->next = predef;
6591 l->first = def;
6592 l->finishes = NULL;
6593 predef = l;
6594}
6595
H. Peter Anvinf7606612016-07-13 14:23:48 -07006596static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006597{
H. Peter Anvinf7606612016-07-13 14:23:48 -07006598 macros_t **mp;
6599
6600 /* Find the end of the list and avoid duplicates */
6601 for (mp = stdmacros; *mp; mp++) {
6602 if (*mp == macros)
6603 return; /* Nothing to do */
6604 }
6605
6606 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
6607
6608 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00006609}
6610
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006611static void pp_extra_stdmac(macros_t *macros)
6612{
6613 extrastdmac = macros;
6614}
6615
H. Peter Anvin8571f062019-09-23 16:40:03 -07006616/* Create a numeric token */
6617static Token *make_tok_num(Token *next, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006618{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04006619 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08006620 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvin8571f062019-09-23 16:40:03 -07006621 return new_Token(next, TOK_NUMBER, numbuf, len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006622}
6623
H. Peter Anvin8571f062019-09-23 16:40:03 -07006624/* Create a quoted string token */
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07006625static Token *make_tok_qstr_len(Token *next, const char *str, size_t len)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006626{
H. Peter Anvin8571f062019-09-23 16:40:03 -07006627 char *p = nasm_quote(str, &len);
6628 return new_Token_free(next, TOK_STRING, p, len);
6629}
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07006630static Token *make_tok_qstr(Token *next, const char *str)
6631{
6632 return make_tok_qstr_len(next, str, strlen(str));
6633}
H. Peter Anvin8571f062019-09-23 16:40:03 -07006634
6635/* Create a single-character operator token */
6636static Token *make_tok_char(Token *next, char op)
6637{
6638 Token *t = new_Token(next, TOK_OTHER, NULL, 1);
6639 t->text.a[0] = op;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006640 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006641}
6642
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006643static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07006644{
6645 if (!m)
6646 return;
6647
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006648 /* We need to print the mstk.mmac list in reverse order */
6649 pp_list_one_macro(m->mstk.mmac, severity);
H. Peter Anvin37368952016-05-09 14:10:32 -07006650
6651 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07006652 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08006653 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07006654 }
6655}
6656
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006657static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006658{
H. Peter Anvinddb29062018-12-11 00:06:29 -08006659 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006660
H. Peter Anvinddb29062018-12-11 00:06:29 -08006661 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
6662 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006663
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03006664 if (istk)
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006665 pp_list_one_macro(istk->mstk.mmac, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006666
H. Peter Anvinddb29062018-12-11 00:06:29 -08006667 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006668}
6669
H. Peter Anvine7469712016-02-18 02:20:59 -08006670const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006671 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006672 pp_reset,
6673 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006674 pp_cleanup_pass,
6675 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006676 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006677 pp_pre_define,
6678 pp_pre_undefine,
6679 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07006680 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006681 pp_include_path,
6682 pp_error_list_macros,
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006683 pp_suppress_error
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006684};