blob: cf77002673e590aab9db77bbdf437b87a8379c99 [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);
2451 if (smacro_defined(ctx, mname, 0, &smac, true, alias)
2452 && 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)de7acc32019-08-19 17:52:55 -07003810 nasm_assert(!defining);
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07003811 nasm_new(defining);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003812 defining->casesense = casesense;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003813 defining->dstk.mmac = defining;
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003814 if (op == PP_RMACRO)
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07003815 defining->max_depth = nasm_limit[LIMIT_MACRO_LEVELS];
H. Peter Anvin8b262472019-02-26 14:00:54 -08003816 if (!parse_mmacro_spec(tline, defining, dname)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003817 nasm_free(defining);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003818 goto done;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003819 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003820
H. Peter Anvin4def1a82016-05-09 13:59:44 -07003821 src_get(&defining->xline, &defining->fname);
3822
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003823 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
3824 while (mmac) {
3825 if (!strcmp(mmac->name, defining->name) &&
3826 (mmac->nparam_min <= defining->nparam_max
3827 || defining->plus)
3828 && (defining->nparam_min <= mmac->nparam_max
3829 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003830 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003831 defining->name);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003832 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003833 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003834 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003835 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003836 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003837
H. Peter Anvine2c80182005-01-15 22:15:51 +00003838 case PP_ENDM:
3839 case PP_ENDMACRO:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003840 if (!(defining && defining->name)) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003841 nasm_nonfatal("`%s': not defining a macro", tok_text(tline));
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003842 goto done;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003843 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003844 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
3845 defining->next = *mmhead;
3846 *mmhead = defining;
3847 defining = NULL;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003848 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003849
H. Peter Anvin89cee572009-07-15 09:16:54 -04003850 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003851 /*
3852 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003853 * macro-end marker for a macro with a name. Then we
3854 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003855 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003856 list_for_each(l, istk->expansion)
3857 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003858 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003859
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003860 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003861 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003862 * Remove all conditional entries relative to this
3863 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003864 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003865 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
3866 cond = istk->conds;
3867 istk->conds = cond->next;
3868 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003869 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003870 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003871 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003872 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003873 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003874 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05003875
H. Peter Anvina26433d2008-07-16 14:40:01 -07003876 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003877 casesense = false;
3878 /* fall through */
3879 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07003880 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003881 MMacro **mmac_p;
3882 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003883
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003884 nasm_zero(spec);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003885 spec.casesense = casesense;
3886 if (!parse_mmacro_spec(tline, &spec, dname)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003887 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003888 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003889 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
3890 while (mmac_p && *mmac_p) {
3891 mmac = *mmac_p;
3892 if (mmac->casesense == spec.casesense &&
3893 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
3894 mmac->nparam_min == spec.nparam_min &&
3895 mmac->nparam_max == spec.nparam_max &&
3896 mmac->plus == spec.plus) {
3897 *mmac_p = mmac->next;
3898 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003899 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003900 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003901 }
3902 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003903 free_tlist(spec.dlist);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003904 break;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003905 }
3906
H. Peter Anvine2c80182005-01-15 22:15:51 +00003907 case PP_ROTATE:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003908 while (tok_white(tline->next))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003909 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003910 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003911 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003912 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003913 return DIRECTIVE_FOUND;
3914 }
3915 t = expand_smacro(tline->next);
3916 tline->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003917 pps.tptr = tline = t;
3918 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003919 tokval.t_type = TOKEN_INVALID;
3920 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003921 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003922 free_tlist(tline);
3923 if (!evalresult)
3924 return DIRECTIVE_FOUND;
3925 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003926 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003927 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003928 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003929 return DIRECTIVE_FOUND;
3930 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003931 mmac = istk->mstk.mmac;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003932 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003933 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003934 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003935 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003936 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003937 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003938
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003939 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003940 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003941 rotate += mmac->nparam;
3942
3943 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003944 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003945 break;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003946
H. Peter Anvine2c80182005-01-15 22:15:51 +00003947 case PP_REP:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003948 {
3949 MMacro *tmp_defining;
3950
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003951 nolist = false;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003952 tline = skip_white(tline->next);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003953 if (tok_type(tline, TOK_ID) && tline->len == 7 &&
3954 !nasm_memicmp(tline->text.a, ".nolist", 7)) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003955 nolist = !list_option('f') || istk->nolist;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003956 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003957 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003958
H. Peter Anvine2c80182005-01-15 22:15:51 +00003959 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003960 pps.tptr = expand_smacro(tline);
3961 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003962 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003963 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003964 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003965 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003966 if (!evalresult)
3967 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003968 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003969 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003970 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003971 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003972 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003973 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003974 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003975 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003976 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
3977 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003978 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003979 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003980 /*!
3981 *!negative-rep [on] regative %rep count
3982 *! warns about negative counts given to the \c{%rep}
3983 *! preprocessor directive.
3984 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08003985 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003986 "negative `%%rep' count: %"PRId64, count);
3987 count = 0;
3988 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003989 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003990 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003991 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003992 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003993 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003994 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003995 tmp_defining = defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003996 nasm_new(defining);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003997 defining->nolist = nolist;
3998 defining->in_progress = count;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003999 defining->mstk = istk->mstk;
4000 defining->dstk.mstk = tmp_defining;
4001 defining->dstk.mmac = tmp_defining ? tmp_defining->dstk.mmac : NULL;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07004002 src_get(&defining->xline, &defining->fname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004003 break;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004004 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004005
H. Peter Anvine2c80182005-01-15 22:15:51 +00004006 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004007 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004008 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004009 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004010 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004011
H. Peter Anvine2c80182005-01-15 22:15:51 +00004012 /*
4013 * Now we have a "macro" defined - although it has no name
4014 * and we won't be entering it in the hash tables - we must
4015 * push a macro-end marker for it on to istk->expansion.
4016 * After that, it will take care of propagating itself (a
4017 * macro-end marker line for a macro which is really a %rep
4018 * block will cause the macro to be re-expanded, complete
4019 * with another macro-end marker to ensure the process
4020 * continues) until the whole expansion is forcibly removed
4021 * from istk->expansion by a %exitrep.
4022 */
H. Peter Anvin6686de22019-08-10 05:33:14 -07004023 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004024 l->next = istk->expansion;
4025 l->finishes = defining;
4026 l->first = NULL;
4027 istk->expansion = l;
4028
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004029 istk->mstk.mstk = defining;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004030
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07004031 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004032 defining = defining->dstk.mstk;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004033 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004034
H. Peter Anvine2c80182005-01-15 22:15:51 +00004035 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004036 /*
4037 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004038 * macro-end marker for a macro with no name. Then we set
4039 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004040 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004041 list_for_each(l, istk->expansion)
4042 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004043 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004044
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004045 if (l)
H. Peter Anvind983b622019-10-07 21:19:32 -07004046 l->finishes->in_progress = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004047 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004048 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004049 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004050
H. Peter Anvin8b262472019-02-26 14:00:54 -08004051 case PP_DEFINE:
4052 case PP_XDEFINE:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004053 case PP_DEFALIAS:
H. Peter Anvin8b262472019-02-26 14:00:54 -08004054 {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004055 SMacro tmpl;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004056 Token **lastp;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07004057 int nparam;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07004058
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004059 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004060 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004061
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004062 nasm_zero(tmpl);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004063 lastp = &tline->next;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07004064 nparam = parse_smacro_template(&lastp, &tmpl);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004065 tline = *lastp;
4066 *lastp = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004067
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004068 if (unlikely(op == PP_DEFALIAS)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004069 macro_start = tline;
4070 if (!is_macro_id(macro_start)) {
4071 nasm_nonfatal("`%s' expects a macro identifier to alias",
4072 dname);
4073 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004074 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004075 tt = macro_start->next;
4076 macro_start->next = NULL;
4077 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004078 tline = skip_white(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004079 if (tline && tline->type) {
4080 nasm_warn(WARN_OTHER,
4081 "trailing garbage after aliasing identifier ignored");
4082 }
4083 free_tlist(tt);
4084 tmpl.alias = true;
4085 } else {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004086 if (op == PP_XDEFINE) {
4087 /* Protect macro parameter tokens */
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07004088 if (nparam)
4089 mark_smac_params(tline, &tmpl, TOK_XDEF_PARAM);
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004090 tline = expand_smacro(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004091 }
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004092 /* NB: Does this still make sense? */
4093 macro_start = reverse_tokens(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004094 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004095
H. Peter Anvine2c80182005-01-15 22:15:51 +00004096 /*
4097 * Good. We now have a macro name, a parameter count, and a
4098 * token list (in reverse order) for an expansion. We ought
4099 * to be OK just to create an SMacro, store it, and let
4100 * free_tlist have the rest of the line (which we have
4101 * carefully re-terminated after chopping off the expansion
4102 * from the end).
4103 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004104 define_smacro(mname, casesense, macro_start, &tmpl);
4105 break;
4106 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004107
H. Peter Anvine2c80182005-01-15 22:15:51 +00004108 case PP_UNDEF:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004109 case PP_UNDEFALIAS:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004110 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004111 goto done;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004112 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004113 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004114
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004115 undef_smacro(mname, op == PP_UNDEFALIAS);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004116 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004117
H. Peter Anvin8b262472019-02-26 14:00:54 -08004118 case PP_DEFSTR:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004119 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004120 goto done;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004121
H. Peter Anvin9e200162008-06-04 17:23:14 -07004122 last = tline;
4123 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004124 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004125
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004126 tline = zap_white(tline);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004127 q = detoken(tline, false);
4128 macro_start = make_tok_qstr(NULL, q);
4129 nasm_free(q);
H. Peter Anvin9e200162008-06-04 17:23:14 -07004130
4131 /*
4132 * We now have a macro name, an implicit parameter count of
4133 * zero, and a string token to use as an expansion. Create
4134 * and store an SMacro.
4135 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004136 define_smacro(mname, casesense, macro_start, NULL);
4137 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004138
H. Peter Anvin8b262472019-02-26 14:00:54 -08004139 case PP_DEFTOK:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004140 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004141 goto done;
4142
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004143 last = tline;
4144 tline = expand_smacro(tline->next);
4145 last->next = NULL;
4146
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004147 t = skip_white(tline);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004148 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004149 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004150 nasm_nonfatal("`%s' requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004151 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004152 goto done;
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004153 }
4154
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04004155 /*
4156 * Convert the string to a token stream. Note that smacros
4157 * are stored with the token stream reversed, so we have to
4158 * reverse the output of tokenize().
4159 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07004160 macro_start = reverse_tokens(tokenize(unquote_token_cstr(t)));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004161
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004162 /*
4163 * We now have a macro name, an implicit parameter count of
4164 * zero, and a numeric token to use as an expansion. Create
4165 * and store an SMacro.
4166 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004167 define_smacro(mname, casesense, macro_start, NULL);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004168 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004169 break;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004170
H. Peter Anvin418ca702008-05-30 10:42:30 -07004171 case PP_PATHSEARCH:
4172 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07004173 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004174
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004175 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004176 goto done;
4177
H. Peter Anvin418ca702008-05-30 10:42:30 -07004178 last = tline;
4179 tline = expand_smacro(tline->next);
4180 last->next = NULL;
4181
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004182 t = skip_white(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004183 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004184 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004185 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004186 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004187 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004188 }
4189 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004190 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004191
4192 p = unquote_token_cstr(t);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004193
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07004194 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07004195 if (!found_path)
4196 found_path = p;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004197 macro_start = make_tok_qstr(NULL, found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004198
4199 /*
4200 * We now have a macro name, an implicit parameter count of
4201 * zero, and a string token to use as an expansion. Create
4202 * and store an SMacro.
4203 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004204 define_smacro(mname, casesense, macro_start, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004205 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004206 break;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004207 }
4208
H. Peter Anvine2c80182005-01-15 22:15:51 +00004209 case PP_STRLEN:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004210 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004211 goto done;
4212
H. Peter Anvine2c80182005-01-15 22:15:51 +00004213 last = tline;
4214 tline = expand_smacro(tline->next);
4215 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004216
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004217 t = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004218 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004219 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004220 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004221 free_tlist(tline);
4222 free_tlist(origline);
4223 return DIRECTIVE_FOUND;
4224 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004225
H. Peter Anvin8571f062019-09-23 16:40:03 -07004226 unquote_token(t);
4227 macro_start = make_tok_num(NULL, t->len);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004228
H. Peter Anvine2c80182005-01-15 22:15:51 +00004229 /*
4230 * We now have a macro name, an implicit parameter count of
4231 * zero, and a numeric token to use as an expansion. Create
4232 * and store an SMacro.
4233 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004234 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004235 free_tlist(tline);
4236 free_tlist(origline);
4237 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004238
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004239 case PP_STRCAT:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004240 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004241 goto done;
4242
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004243 last = tline;
4244 tline = expand_smacro(tline->next);
4245 last->next = NULL;
4246
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004247 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004248 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004249 switch (t->type) {
4250 case TOK_WHITESPACE:
4251 break;
4252 case TOK_STRING:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004253 unquote_token(t);
4254 len += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004255 break;
4256 case TOK_OTHER:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004257 if (tok_is(t, ',')) /* permit comma separators */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004258 break;
4259 /* else fall through */
4260 default:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004261 nasm_nonfatal("non-string passed to `%s': %s", dname,
4262 tok_text(t));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004263 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004264 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004265 }
4266 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004267
H. Peter Anvin (Intel)f770ce82019-10-17 18:22:43 -07004268 q = qbuf = nasm_malloc(len+1);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004269 list_for_each(t, tline) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004270 if (t->type == TOK_INTERNAL_STRING)
4271 q = mempcpy(q, tok_text(t), t->len);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004272 }
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07004273 *q = '\0';
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004274
4275 /*
4276 * We now have a macro name, an implicit parameter count of
4277 * zero, and a numeric token to use as an expansion. Create
4278 * and store an SMacro.
4279 */
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07004280 macro_start = make_tok_qstr_len(NULL, qbuf, len);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004281 nasm_free(qbuf);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004282 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004283 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004284 break;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004285
H. Peter Anvine2c80182005-01-15 22:15:51 +00004286 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004287 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004288 int64_t start, count;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004289 const char *txt;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004290 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07004291
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004292 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004293 goto done;
4294
H. Peter Anvine2c80182005-01-15 22:15:51 +00004295 last = tline;
4296 tline = expand_smacro(tline->next);
4297 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004298
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04004299 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004300 t = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004301
4302 t = skip_white(t);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004303
H. Peter Anvine2c80182005-01-15 22:15:51 +00004304 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004305 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004306 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004307 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004308 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004309 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004310
H. Peter Anvin8b262472019-02-26 14:00:54 -08004311 pps.tptr = t->next;
4312 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004313 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004314 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004315 if (!evalresult) {
4316 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004317 goto done;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004318 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004319 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004320 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004321 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004322 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004323 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004324
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004325 pps.tptr = skip_white(pps.tptr);
H. Peter Anvin8b262472019-02-26 14:00:54 -08004326 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004327 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004328 } else {
4329 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004330 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004331 if (!evalresult) {
4332 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004333 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004334 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004335 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004336 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004337 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004338 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004339 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004340 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004341
H. Peter Anvin8571f062019-09-23 16:40:03 -07004342 unquote_token(t);
4343 len = t->len;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004344
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04004345 /* make start and count being in range */
4346 if (start < 0)
4347 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004348 if (count < 0)
4349 count = len + count + 1 - start;
4350 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04004351 count = len - start;
4352 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004353 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004354
H. Peter Anvin8571f062019-09-23 16:40:03 -07004355 txt = (start < 0) ? "" : tok_text(t) + start;
4356 len = count;
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07004357 macro_start = make_tok_qstr_len(NULL, txt, len);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004358
H. Peter Anvine2c80182005-01-15 22:15:51 +00004359 /*
4360 * We now have a macro name, an implicit parameter count of
4361 * zero, and a numeric token to use as an expansion. Create
4362 * and store an SMacro.
4363 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004364 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004365 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004366 break;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004367 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004368
H. Peter Anvin8b262472019-02-26 14:00:54 -08004369 case PP_ASSIGN:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004370 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004371 goto done;
4372
H. Peter Anvine2c80182005-01-15 22:15:51 +00004373 last = tline;
4374 tline = expand_smacro(tline->next);
4375 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004376
H. Peter Anvin8b262472019-02-26 14:00:54 -08004377 pps.tptr = tline;
4378 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004379 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004380 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004381 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004382 if (!evalresult)
4383 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004384
H. Peter Anvine2c80182005-01-15 22:15:51 +00004385 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004386 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00004387
H. Peter Anvine2c80182005-01-15 22:15:51 +00004388 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004389 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004390 free_tlist(origline);
4391 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004392 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004393
H. Peter Anvin8571f062019-09-23 16:40:03 -07004394 macro_start = make_tok_num(NULL, reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00004395
H. Peter Anvine2c80182005-01-15 22:15:51 +00004396 /*
4397 * We now have a macro name, an implicit parameter count of
4398 * zero, and a numeric token to use as an expansion. Create
4399 * and store an SMacro.
4400 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004401 define_smacro(mname, casesense, macro_start, NULL);
4402 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004403
H. Peter Anvind2354082019-08-27 16:38:48 -07004404 case PP_ALIASES:
4405 tline = tline->next;
4406 tline = expand_smacro(tline);
4407 do_aliases = pp_get_boolean_option(tline, do_aliases);
4408 break;
4409
H. Peter Anvine2c80182005-01-15 22:15:51 +00004410 case PP_LINE:
4411 /*
4412 * Syntax is `%line nnn[+mmm] [filename]'
4413 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004414 if (unlikely(pp_noline))
4415 goto done;
4416
H. Peter Anvine2c80182005-01-15 22:15:51 +00004417 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004418 tline = skip_white(tline);
4419 if (!tok_type(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004420 nasm_nonfatal("`%s' expects line number", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004421 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004422 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07004423 k = readnum(tok_text(tline), &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004424 m = 1;
4425 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004426 if (tok_is(tline, '+')) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004427 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004428 if (!tok_type(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004429 nasm_nonfatal("`%s' expects line increment", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004430 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004431 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07004432 m = readnum(tok_text(tline), &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004433 tline = tline->next;
4434 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004435 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004436 src_set_linnum(k);
4437 istk->lineinc = m;
4438 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07004439 char *fname = detoken(tline, false);
4440 src_set_fname(fname);
4441 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004442 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004443 break;
4444 }
4445
4446done:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004447 free_tlist(origline);
4448 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004449}
4450
4451/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00004452 * Ensure that a macro parameter contains a condition code and
4453 * nothing else. Return the condition code index if so, or -1
4454 * otherwise.
4455 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004456static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004457{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004458 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004459
H. Peter Anvin25a99342007-09-22 17:45:45 -07004460 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004461 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07004462
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004463 t = skip_white(t);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004464 if (!tok_type(t, TOK_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004465 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004466 tt = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004467 tt = skip_white(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004468 if (tok_isnt(tt, ','))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004469 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004470
H. Peter Anvin8571f062019-09-23 16:40:03 -07004471 return bsii(tok_text(t), (const char **)conditions,
4472 ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00004473}
4474
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004475static inline bool pp_concat_match(const Token *t, unsigned int mask)
4476{
4477 return t && (PP_CONCAT_MASK(t->type) & mask);
4478}
4479
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004480/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004481 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004482 * pasting, if @handle_explicit passed then explicit pasting
4483 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004484 * The @m array can contain a series of token types which are
4485 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004486 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004487static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004488 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07004489{
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004490 Token *tok, *t, *next, **prev_next, **prev_nonspace;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004491 bool pasted = false;
4492 char *buf, *p;
4493 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07004494
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004495 /*
4496 * The last token before pasting. We need it
4497 * to be able to connect new handled tokens.
4498 * In other words if there were a tokens stream
4499 *
4500 * A -> B -> C -> D
4501 *
4502 * and we've joined tokens B and C, the resulting
4503 * stream should be
4504 *
4505 * A -> BC -> D
4506 */
4507 tok = *head;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004508 prev_next = prev_nonspace = head;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004509
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004510 if (tok_white(tok) || tok_type(tok, TOK_PASTE))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004511 prev_nonspace = NULL;
4512
4513 while (tok && (next = tok->next)) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004514 bool did_paste = false;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004515
4516 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07004517 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004518 /* Zap redundant whitespaces */
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004519 tok->next = next = zap_white(next);
H. Peter Anvind784a082009-04-20 14:01:18 -07004520 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004521
4522 case TOK_PASTE:
4523 /* Explicit pasting */
4524 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004525 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004526
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004527 /* Left pasting token is start of line */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004528 if (!prev_nonspace) {
4529 nasm_nonfatal("No lvalue found on pasting");
4530 tok = delete_Token(tok);
4531 break;
4532 }
4533
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004534 did_paste = true;
4535
4536 prev_next = prev_nonspace;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004537 t = *prev_nonspace;
4538
4539 /* Delete leading whitespace */
4540 next = zap_white(t->next);
4541
4542 /* Delete the %+ token itself */
4543 nasm_assert(next == tok);
4544 next = delete_Token(next);
4545
4546 /* Delete trailing whitespace */
4547 next = zap_white(next);
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004548
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004549 /*
4550 * No ending token, this might happen in two
4551 * cases
4552 *
4553 * 1) There indeed no right token at all
4554 * 2) There is a bare "%define ID" statement,
4555 * and @ID does expand to whitespace.
4556 *
4557 * So technically we need to do a grammar analysis
4558 * in another stage of parsing, but for now lets don't
4559 * change the behaviour people used to. Simply allow
4560 * whitespace after paste token.
4561 */
4562 if (!next) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004563 *prev_nonspace = tok = NULL; /* End of line */
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004564 break;
4565 }
4566
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004567 p = buf = nasm_malloc(t->len + next->len + 1);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004568 p = mempcpy(p, tok_text(t), t->len);
4569 p = mempcpy(p, tok_text(next), next->len);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004570 *p = '\0';
4571 delete_Token(t);
4572 t = tokenize(buf);
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004573 nasm_free(buf);
4574
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004575 if (unlikely(!t)) {
4576 /*
4577 * No output at all? Replace with a single whitespace.
4578 * This should never happen.
4579 */
4580 t = new_White(NULL);
4581 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004582
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004583 *prev_nonspace = tok = t;
4584 while (t->next)
4585 t = t->next; /* Find the last token produced */
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004586
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004587 /* Delete the second token and attach to the end of the list */
4588 t->next = delete_Token(next);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004589
4590 /* We want to restart from the head of the pasted token */
4591 next = tok;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004592 break;
4593
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004594 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004595 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004596 for (i = 0; i < mnum; i++) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004597 if (pp_concat_match(tok, m[i].mask_head))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004598 break;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004599 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004600
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004601 if (i >= mnum)
4602 break;
4603
4604 len = tok->len;
4605 while (pp_concat_match(next, m[i].mask_tail)) {
4606 len += next->len;
4607 next = next->next;
4608 }
4609
4610 /* No match or no text to process */
4611 if (len == tok->len)
4612 break;
4613
4614 p = buf = nasm_malloc(len + 1);
4615 while (tok != next) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004616 p = mempcpy(p, tok_text(tok), tok->len);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004617 tok = delete_Token(tok);
4618 }
4619 *p = '\0';
4620 *prev_next = tok = t = tokenize(buf);
4621 nasm_free(buf);
4622
4623 /*
4624 * Connect pasted into original stream,
4625 * ie A -> new-tokens -> B
4626 */
4627 while (t->next)
4628 t = t->next;
4629 t->next = next;
4630 prev_next = prev_nonspace = &t->next;
4631 did_paste = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004632 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004633 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004634
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004635 if (did_paste) {
4636 pasted = true;
4637 } else {
4638 prev_next = &tok->next;
4639 if (next && next->type != TOK_WHITESPACE && next->type != TOK_PASTE)
4640 prev_nonspace = prev_next;
4641 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004642
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004643 tok = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004644 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004645
4646 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07004647}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004648
4649/*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004650 * Computes the proper rotation of mmacro parameters
4651 */
4652static int mmac_rotate(const MMacro *mac, unsigned int n)
4653{
4654 if (--n < mac->nparam)
4655 n = (n + mac->rotate) % mac->nparam;
4656
4657 return n+1;
4658}
4659
4660/*
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004661 * expands to a list of tokens from %{x:y}
4662 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004663static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004664{
4665 Token *t = tline, **tt, *tm, *head;
4666 char *pos;
4667 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004668
H. Peter Anvin8571f062019-09-23 16:40:03 -07004669 pos = strchr(tok_text(tline), ':');
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004670 nasm_assert(pos);
4671
4672 lst = atoi(pos + 1);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004673 fst = atoi(tok_text(tline) + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004674
4675 /*
4676 * only macros params are accounted so
4677 * if someone passes %0 -- we reject such
4678 * value(s)
4679 */
4680 if (lst == 0 || fst == 0)
4681 goto err;
4682
4683 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004684 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
4685 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004686 goto err;
4687
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004688 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
4689 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004690
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004691 /* count from zero */
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004692 fst--, lst--;
4693
4694 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004695 * It will be at least one token. Note we
4696 * need to scan params until separator, otherwise
4697 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004698 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004699 j = (fst + mac->rotate) % mac->nparam;
4700 tm = mac->params[j+1];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03004701 if (!tm)
4702 goto err;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004703 head = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004704 tt = &head->next, tm = tm->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004705 while (tok_isnt(tm, ',')) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004706 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004707 *tt = t, tt = &t->next, tm = tm->next;
4708 }
4709
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004710 if (fst < lst) {
4711 for (i = fst + 1; i <= lst; i++) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004712 t = make_tok_char(NULL, ',');
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004713 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004714 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004715 tm = mac->params[j+1];
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004716 while (tok_isnt(tm, ',')) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004717 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004718 *tt = t, tt = &t->next, tm = tm->next;
4719 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004720 }
4721 } else {
4722 for (i = fst - 1; i >= lst; i--) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004723 t = make_tok_char(NULL, ',');
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004724 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004725 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004726 tm = mac->params[j+1];
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004727 while (!tok_isnt(tm, ',')) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004728 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004729 *tt = t, tt = &t->next, tm = tm->next;
4730 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004731 }
4732 }
4733
4734 *last = tt;
4735 return head;
4736
4737err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004738 nasm_nonfatal("`%%{%s}': macro parameters out of range",
H. Peter Anvin8571f062019-09-23 16:40:03 -07004739 tok_text(tline) + 1);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004740 return NULL;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004741}
4742
H. Peter Anvin76690a12002-04-30 20:52:49 +00004743/*
4744 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004745 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004746 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004747 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004748static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004749{
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004750 Token **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004751 bool changed = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004752 MMacro *mac = istk->mstk.mmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004753
4754 tail = &thead;
4755 thead = NULL;
4756
H. Peter Anvine2c80182005-01-15 22:15:51 +00004757 while (tline) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004758 bool change;
4759 Token *t = tline;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004760 const char *text = tok_text(t);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004761 int type = t->type;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004762
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004763 tline = tline->next;
4764 t->next = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004765
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004766 switch (type) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004767 case TOK_LOCAL_SYMBOL:
4768 type = TOK_ID;
4769 text = nasm_asprintf("..@%"PRIu64".%s", mac->unique, text+2);
4770 change = true;
4771 break;
4772 case TOK_MMACRO_PARAM:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004773 {
4774 Token *tt = NULL;
4775
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004776 change = true;
4777
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004778 if (!mac) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004779 nasm_nonfatal("`%s': not in a macro call", text);
4780 text = NULL;
4781 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004782 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004783
4784 if (strchr(text, ':')) {
4785 /*
4786 * seems we have a parameters range here
4787 */
4788 Token *head, **last;
4789 head = expand_mmac_params_range(mac, t, &last);
4790 if (head) {
4791 *tail = head;
4792 *last = tline;
4793 text = NULL;
4794 }
4795 break;
4796 }
4797
4798 switch (text[1]) {
4799 /*
4800 * We have to make a substitution of one of the
4801 * forms %1, %-1, %+1, %%foo, %0, %00.
4802 */
4803 case '0':
4804 if (!text[2]) {
4805 type = TOK_NUMBER;
4806 text = nasm_asprintf("%d", mac->nparam);
4807 break;
4808 }
4809 if (text[2] != '0' || text[3])
4810 goto invalid;
4811 /* a possible captured label == mac->params[0] */
4812 /* fall through */
4813 default:
4814 {
4815 unsigned long n;
4816 char *ep;
4817
4818 n = strtoul(text + 1, &ep, 10);
4819 if (unlikely(*ep))
4820 goto invalid;
4821
4822 if (n <= mac->nparam) {
4823 n = mmac_rotate(mac, n);
4824 dup_tlistn(mac->params[n], mac->paramlen[n], &tail);
4825 }
4826 text = NULL;
4827 break;
4828 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004829 case '-':
4830 case '+':
4831 {
4832 int cc;
4833 unsigned long n;
4834 char *ep;
4835
H. Peter Anvin8571f062019-09-23 16:40:03 -07004836 n = strtoul(tok_text(t) + 2, &ep, 10);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004837 if (unlikely(*ep))
4838 goto invalid;
4839
4840 if (n && n < mac->nparam) {
4841 n = mmac_rotate(mac, n);
4842 tt = mac->params[n];
4843 }
4844 cc = find_cc(tt);
4845 if (cc == -1) {
4846 nasm_nonfatal("macro parameter `%s' is not a condition code",
H. Peter Anvin8571f062019-09-23 16:40:03 -07004847 tok_text(t));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004848 text = NULL;
4849 break;
4850 }
4851
4852 type = TOK_ID;
4853 if (text[1] == '-') {
4854 int ncc = inverse_ccs[cc];
4855 if (unlikely(ncc == -1)) {
4856 nasm_nonfatal("condition code `%s' is not invertible",
4857 conditions[cc]);
4858 break;
4859 }
4860 cc = ncc;
4861 }
4862 text = nasm_strdup(conditions[cc]);
4863 break;
4864 }
4865
4866 invalid:
4867 nasm_nonfatal("invalid macro parameter: `%s'", text);
4868 text = NULL;
4869 break;
4870 }
4871 break;
4872 }
4873
4874 case TOK_PREPROC_Q:
4875 if (mac) {
4876 type = TOK_ID;
4877 text = nasm_strdup(mac->iname);
4878 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07004879 } else {
4880 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004881 }
4882 break;
4883
4884 case TOK_PREPROC_QQ:
4885 if (mac) {
4886 type = TOK_ID;
4887 text = nasm_strdup(mac->name);
4888 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07004889 } else {
4890 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004891 }
4892 break;
4893
4894 case TOK_INDIRECT:
4895 {
4896 Token *tt;
4897
H. Peter Anvin8571f062019-09-23 16:40:03 -07004898 tt = tokenize(tok_text(t));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004899 tt = expand_mmac_params(tt);
4900 tt = expand_smacro(tt);
4901 /* Why dup_tlist() here? We should own tt... */
4902 dup_tlist(tt, &tail);
4903 text = NULL;
4904 change = true;
4905 break;
4906 }
4907
4908 default:
4909 change = false;
4910 break;
4911 }
4912
4913 if (change) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004914 if (!text) {
4915 delete_Token(t);
4916 } else {
4917 *tail = t;
4918 tail = &t->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004919 set_text(t, text, tok_strlen(text));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004920 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004921 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004922 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004923 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004924 *tail = t;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004925 tail = &t->next;
4926 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004927 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004928
H. Peter Anvineba20a72002-04-30 20:53:55 +00004929 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004930
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004931 if (changed) {
4932 const struct tokseq_match t[] = {
4933 {
4934 PP_CONCAT_MASK(TOK_ID) |
4935 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4936 PP_CONCAT_MASK(TOK_ID) |
4937 PP_CONCAT_MASK(TOK_NUMBER) |
4938 PP_CONCAT_MASK(TOK_FLOAT) |
4939 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4940 },
4941 {
4942 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4943 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4944 }
4945 };
4946 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4947 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004948
H. Peter Anvin76690a12002-04-30 20:52:49 +00004949 return thead;
4950}
4951
H. Peter Anvin322bee02019-08-10 01:38:06 -07004952static Token *expand_smacro_noreset(Token * tline);
H. Peter Anvin322bee02019-08-10 01:38:06 -07004953
H. Peter Anvin76690a12002-04-30 20:52:49 +00004954/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004955 * Expand *one* single-line macro instance. If the first token is not
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004956 * a macro at all, it is simply copied to the output and the pointer
4957 * advanced. tpp should be a pointer to a pointer (usually the next
4958 * pointer of the previous token) to the first token. **tpp is updated
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004959 * to point to the first token of the expansion, and *tpp updated to
4960 * point to the next pointer of the last token of the expansion.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004961 *
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004962 * If the expansion is empty, *tpp will be unchanged but **tpp will
4963 * be advanced past the macro call.
4964 *
H. Peter Anvin322bee02019-08-10 01:38:06 -07004965 * Return the macro expanded, or NULL if no expansion took place.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004966 */
H. Peter Anvin322bee02019-08-10 01:38:06 -07004967static SMacro *expand_one_smacro(Token ***tpp)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004968{
4969 Token **params = NULL;
4970 const char *mname;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004971 Token *mstart = **tpp;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004972 Token *tline = mstart;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004973 SMacro *head, *m;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004974 int i;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004975 Token *t, *tup, *tafter;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004976 int nparam = 0;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004977 bool cond_comma;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004978
4979 if (!tline)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004980 return false; /* Empty line, nothing to do */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004981
H. Peter Anvin8571f062019-09-23 16:40:03 -07004982 mname = tok_text(mstart);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004983
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004984 smacro_deadman.total--;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004985 smacro_deadman.levels--;
4986
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004987 if (unlikely(smacro_deadman.total < 0 || smacro_deadman.levels < 0)) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07004988 if (unlikely(!smacro_deadman.triggered)) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004989 nasm_nonfatal("interminable macro recursion");
H. Peter Anvin322bee02019-08-10 01:38:06 -07004990 smacro_deadman.triggered = true;
4991 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004992 goto not_a_macro;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004993 } else if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004994 head = (SMacro *)hash_findix(&smacros, mname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004995 } else if (tline->type == TOK_LOCAL_MACRO) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004996 Context *ctx = get_ctx(mname, &mname);
4997 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4998 } else {
4999 goto not_a_macro;
5000 }
5001
5002 /*
5003 * We've hit an identifier of some sort. First check whether the
5004 * identifier is a single-line macro at all, then think about
5005 * checking for parameters if necessary.
5006 */
5007 list_for_each(m, head) {
H. Peter Anvind2354082019-08-27 16:38:48 -07005008 if (unlikely(m->alias && !do_aliases))
5009 continue;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005010 if (!mstrcmp(m->name, mname, m->casesense))
5011 break;
5012 }
5013
5014 if (!m) {
5015 goto not_a_macro;
5016 }
5017
5018 /* Parse parameters, if applicable */
5019
5020 params = NULL;
5021 nparam = 0;
5022
5023 if (m->nparam == 0) {
5024 /*
5025 * Simple case: the macro is parameterless.
5026 * Nothing to parse; the expansion code will
5027 * drop the macro name token.
5028 */
5029 } else {
5030 /*
5031 * Complicated case: at least one macro with this name
5032 * exists and takes parameters. We must find the
5033 * parameters in the call, count them, find the SMacro
5034 * that corresponds to that form of the macro call, and
5035 * substitute for the parameters when we expand. What a
5036 * pain.
5037 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005038 Token *t;
5039 int paren, brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005040
5041 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005042 tline = skip_white(tline);
5043 if (!tok_is(tline, '(')) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005044 /*
5045 * This macro wasn't called with parameters: ignore
5046 * the call. (Behaviour borrowed from gnu cpp.)
5047 */
5048 goto not_a_macro;
5049 }
5050
5051 paren = 1;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005052 nparam = 1;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005053 brackets = 0;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005054 t = tline; /* tline points to leading ( */
5055
5056 while (paren) {
5057 t = t->next;
5058
5059 if (!t) {
5060 nasm_nonfatal("macro call expects terminating `)'");
5061 goto not_a_macro;
5062 }
5063
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005064 if (t->type != TOK_OTHER || t->len != 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005065 continue;
5066
H. Peter Anvin8571f062019-09-23 16:40:03 -07005067 switch (t->text.a[0]) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005068 case ',':
5069 if (!brackets)
5070 nparam++;
5071 break;
5072
5073 case '{':
5074 brackets++;
5075 break;
5076
5077 case '}':
5078 if (brackets > 0)
5079 brackets--;
5080 break;
5081
5082 case '(':
5083 if (!brackets)
5084 paren++;
5085 break;
5086
5087 case ')':
5088 if (!brackets)
5089 paren--;
5090 break;
5091
5092 default:
5093 break; /* Normal token */
5094 }
5095 }
5096
5097 /*
5098 * Look for a macro matching in both name and parameter count.
5099 * We already know any matches cannot be anywhere before the
5100 * current position of "m", so there is no reason to
5101 * backtrack.
5102 */
5103 while (1) {
5104 if (!m) {
5105 /*!
5106 *!macro-params-single [on] single-line macro calls with wrong parameter count
5107 *! warns about \i{single-line macros} being invoked
5108 *! with the wrong number of parameters.
5109 */
5110 nasm_warn(WARN_MACRO_PARAMS_SINGLE,
5111 "single-line macro `%s' exists, "
5112 "but not taking %d parameter%s",
5113 mname, nparam, (nparam == 1) ? "" : "s");
5114 goto not_a_macro;
5115 }
5116
5117 if (!mstrcmp(m->name, mname, m->casesense)) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005118 if (nparam == m->nparam)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005119 break; /* It's good */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005120 if (m->greedy && nparam >= m->nparam-1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005121 break; /* Also good */
5122 }
5123 m = m->next;
5124 }
5125 }
5126
5127 if (m->in_progress)
5128 goto not_a_macro;
5129
5130 /* Expand the macro */
5131 m->in_progress = true;
5132
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005133 if (nparam) {
5134 /* Extract parameters */
5135 Token **phead, **pep;
5136 int white = 0;
5137 int brackets = 0;
5138 int paren;
5139 bool bracketed = false;
5140 bool bad_bracket = false;
5141 enum sparmflags flags;
5142
5143 nparam = m->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005144 paren = 1;
5145 nasm_newn(params, nparam);
5146 i = 0;
5147 flags = m->params[i].flags;
5148 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005149 *pep = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005150
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005151 while (paren) {
5152 bool skip;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005153 char ch;
5154
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005155 tline = tline->next;
5156
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005157 if (!tline)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005158 nasm_nonfatal("macro call expects terminating `)'");
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005159
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005160 ch = 0;
5161 skip = false;
5162
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005163
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005164 switch (tline->type) {
5165 case TOK_OTHER:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005166 if (tline->len == 1)
H. Peter Anvin8571f062019-09-23 16:40:03 -07005167 ch = tline->text.a[0];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005168 break;
5169
5170 case TOK_WHITESPACE:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005171 if (!(flags & SPARM_NOSTRIP)) {
5172 if (brackets || *phead)
5173 white++; /* Keep interior whitespace */
5174 skip = true;
5175 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005176 break;
5177
5178 default:
5179 break;
5180 }
5181
5182 switch (ch) {
5183 case ',':
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005184 if (!brackets && !(flags & SPARM_GREEDY)) {
5185 i++;
5186 nasm_assert(i < nparam);
5187 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005188 *pep = NULL;
5189 bracketed = false;
5190 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005191 flags = m->params[i].flags;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005192 }
5193 break;
5194
5195 case '{':
5196 if (!bracketed) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005197 bracketed = !*phead && !(flags & SPARM_NOSTRIP);
5198 skip = bracketed;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005199 }
5200 brackets++;
5201 break;
5202
5203 case '}':
5204 if (brackets > 0) {
5205 if (!--brackets)
5206 skip = bracketed;
5207 }
5208 break;
5209
5210 case '(':
5211 if (!brackets)
5212 paren++;
5213 break;
5214
5215 case ')':
5216 if (!brackets) {
5217 paren--;
5218 if (!paren) {
5219 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005220 i++; /* Found last argument */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005221 }
5222 }
5223 break;
5224
5225 default:
5226 break; /* Normal token */
5227 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005228
5229 if (!skip) {
5230 Token *t;
5231
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005232 bad_bracket |= bracketed && !brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005233
5234 if (white) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005235 *pep = t = new_White(NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005236 pep = &t->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005237 white = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005238 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005239 *pep = t = dup_Token(NULL, tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005240 pep = &t->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005241 }
5242 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005243
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005244 /*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005245 * Possible further processing of parameters. Note that the
5246 * ordering matters here.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005247 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005248 for (i = 0; i < nparam; i++) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005249 enum sparmflags flags = m->params[i].flags;
5250
5251 if (flags & SPARM_EVAL) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005252 /* Evaluate this parameter as a number */
5253 struct ppscan pps;
5254 struct tokenval tokval;
5255 expr *evalresult;
5256 Token *eval_param;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005257
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005258 pps.tptr = eval_param = expand_smacro_noreset(params[i]);
5259 pps.ntokens = -1;
5260 tokval.t_type = TOKEN_INVALID;
5261 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005262
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005263 free_tlist(eval_param);
5264 params[i] = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005265
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005266 if (!evalresult) {
5267 /* Nothing meaningful to do */
5268 } else if (tokval.t_type) {
5269 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
5270 } else if (!is_simple(evalresult)) {
5271 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
5272 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005273 params[i] = make_tok_num(NULL, reloc_value(evalresult));
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005274 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005275 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005276
5277 if (flags & SPARM_STR) {
5278 /* Convert expansion to a quoted string */
5279 char *arg;
5280 Token *qs;
5281
5282 qs = expand_smacro_noreset(params[i]);
5283 arg = detoken(qs, false);
5284 free_tlist(qs);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005285 params[i] = make_tok_qstr(NULL, arg);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005286 nasm_free(arg);
5287 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005288 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005289 }
5290
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005291 /* Note: we own the expansion this returns. */
5292 t = m->expand(m, params, nparam);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005293
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005294 tafter = tline->next; /* Skip past the macro call */
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005295 tline->next = NULL; /* Truncate list at the macro call end */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005296 tline = tafter;
5297
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005298 tup = NULL;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005299 cond_comma = false;
5300
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005301 while (t) {
5302 enum pp_token_type type = t->type;
5303 Token *tnext = t->next;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005304
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005305 switch (type) {
5306 case TOK_PREPROC_Q:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005307 delete_Token(t);
5308 t = dup_Token(tline, mstart);
5309 break;
5310
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005311 case TOK_PREPROC_QQ:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005312 {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005313 size_t mlen = strlen(m->name);
5314 size_t len;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005315 char *p;
5316
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005317 t->type = mstart->type;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005318 if (t->type == TOK_LOCAL_MACRO) {
5319 const char *psp; /* prefix start pointer */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005320 const char *pep; /* prefix end pointer */
H. Peter Anvin8571f062019-09-23 16:40:03 -07005321 size_t plen;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005322
H. Peter Anvin8571f062019-09-23 16:40:03 -07005323 psp = tok_text(mstart);
5324 get_ctx(psp, &pep);
5325 plen = pep - psp;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005326
H. Peter Anvin8571f062019-09-23 16:40:03 -07005327 len = mlen + plen;
5328 p = nasm_malloc(len + 1);
5329 p = mempcpy(p, psp, plen);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005330 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005331 len = mlen;
5332 p = nasm_malloc(len + 1);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005333 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07005334 p = mempcpy(p, m->name, mlen);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005335 *p = '\0';
H. Peter Anvin8571f062019-09-23 16:40:03 -07005336 set_text_free(t, p, len);
5337
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005338 t->next = tline;
5339 break;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005340 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005341
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005342 case TOK_COND_COMMA:
5343 delete_Token(t);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005344 t = cond_comma ? make_tok_char(tline, ',') : NULL;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005345 break;
5346
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005347 case TOK_ID:
5348 case TOK_PREPROC_ID:
H. Peter Anvin8571f062019-09-23 16:40:03 -07005349 case TOK_LOCAL_MACRO:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005350 {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005351 /*
5352 * Chain this into the target line *before* expanding,
5353 * that way we pick up any arguments to the new macro call,
5354 * if applicable.
5355 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005356 Token **tp = &t;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005357 t->next = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005358 expand_one_smacro(&tp);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005359 tline = *tp; /* First token left after any macro call */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005360 break;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005361 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005362 default:
5363 if (is_smac_param(t->type)) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005364 int param = smac_nparam(t->type);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005365 nasm_assert(!tup && param < nparam);
5366 delete_Token(t);
5367 t = NULL;
5368 tup = tnext;
5369 tnext = dup_tlist_reverse(params[param], NULL);
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005370 cond_comma = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005371 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005372 t->next = tline;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005373 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005374 }
5375
5376 if (t) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005377 Token *endt = tline;
5378
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005379 tline = t;
Chang S. Bae95e54a92020-02-06 14:39:22 -08005380 while (!cond_comma && t && t != endt) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005381 cond_comma = t->type != TOK_WHITESPACE;
Chang S. Bae95e54a92020-02-06 14:39:22 -08005382 t = t->next;
5383 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005384 }
5385
5386 if (tnext) {
5387 t = tnext;
5388 } else {
5389 t = tup;
5390 tup = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005391 }
5392 }
5393
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005394 **tpp = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005395 for (t = tline; t != tafter; t = t->next)
5396 *tpp = &t->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005397
5398 m->in_progress = false;
5399
5400 /* Don't do this until after expansion or we will clobber mname */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005401 free_tlist(mstart);
H. Peter Anvin322bee02019-08-10 01:38:06 -07005402 goto done;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005403
5404 /*
5405 * No macro expansion needed; roll back to mstart (if necessary)
H. Peter Anvin322bee02019-08-10 01:38:06 -07005406 * and then advance to the next input token. Note that this is
5407 * by far the common case!
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005408 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005409not_a_macro:
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005410 *tpp = &mstart->next;
H. Peter Anvin322bee02019-08-10 01:38:06 -07005411 m = NULL;
5412done:
5413 smacro_deadman.levels++;
5414 if (unlikely(params))
5415 free_tlist_array(params, nparam);
5416 return m;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005417}
5418
5419/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005420 * Expand all single-line macro calls made in the given line.
5421 * Return the expanded version of the line. The original is deemed
5422 * to be destroyed in the process. (In reality we'll just move
5423 * Tokens from input to output a lot of the time, rather than
5424 * actually bothering to destroy and replicate.)
5425 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005426static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005427{
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005428 smacro_deadman.total = nasm_limit[LIMIT_MACRO_TOKENS];
H. Peter Anvin322bee02019-08-10 01:38:06 -07005429 smacro_deadman.levels = nasm_limit[LIMIT_MACRO_LEVELS];
5430 smacro_deadman.triggered = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005431 return expand_smacro_noreset(tline);
5432}
5433
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005434static Token *expand_smacro_noreset(Token *org_tline)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005435{
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005436 Token *tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005437 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005438
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005439 if (!org_tline)
5440 return NULL; /* Empty input */
5441
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005442 /*
5443 * Trick: we should avoid changing the start token pointer since it can
5444 * be contained in "next" field of other token. Because of this
5445 * we allocate a copy of first token and work with it; at the end of
5446 * routine we copy it back
5447 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005448 tline = dup_Token(org_tline->next, org_tline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005449
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005450 /*
5451 * Pretend that we always end up doing expansion on the first pass;
5452 * that way %+ get processed. However, if we process %+ before the
5453 * first pass, we end up with things like MACRO %+ TAIL trying to
5454 * look up the macro "MACROTAIL", which we don't want.
5455 */
5456 expanded = true;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005457 while (true) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005458 static const struct tokseq_match tmatch[] = {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005459 {
5460 PP_CONCAT_MASK(TOK_ID) |
H. Peter Anvin8571f062019-09-23 16:40:03 -07005461 PP_CONCAT_MASK(TOK_LOCAL_MACRO) |
5462 PP_CONCAT_MASK(TOK_ENVIRON) |
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005463 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
5464 PP_CONCAT_MASK(TOK_ID) |
H. Peter Anvin8571f062019-09-23 16:40:03 -07005465 PP_CONCAT_MASK(TOK_LOCAL_MACRO) |
5466 PP_CONCAT_MASK(TOK_ENVIRON) |
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005467 PP_CONCAT_MASK(TOK_PREPROC_ID) |
5468 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
5469 }
5470 };
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005471 Token **tail = &tline;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005472
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005473 while (*tail) /* main token loop */
H. Peter Anvin322bee02019-08-10 01:38:06 -07005474 expanded |= !!expand_one_smacro(&tail);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005475
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005476 if (!expanded)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005477 break; /* Done! */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005478
5479 /*
5480 * Now scan the entire line and look for successive TOK_IDs
5481 * that resulted after expansion (they can't be produced by
5482 * tokenize()). The successive TOK_IDs should be concatenated.
5483 * Also we look for %+ tokens and concatenate the tokens
5484 * before and after them (without white spaces in between).
5485 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005486 if (!paste_tokens(&tline, tmatch, ARRAY_SIZE(tmatch), true))
5487 break; /* Done again! */
5488
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005489 expanded = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005490 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005491
H. Peter Anvin8571f062019-09-23 16:40:03 -07005492 if (!tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005493 /*
5494 * The expression expanded to empty line;
5495 * we can't return NULL because of the "trick" above.
5496 * Just set the line to a single WHITESPACE token.
H. Peter Anvin8571f062019-09-23 16:40:03 -07005497 */
5498
5499 tline = new_White(NULL);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005500 }
5501
H. Peter Anvin8571f062019-09-23 16:40:03 -07005502 steal_Token(org_tline, tline);
5503 org_tline->next = tline->next;
5504 delete_Token(tline);
5505
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005506 return org_tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005507}
5508
5509/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005510 * Similar to expand_smacro but used exclusively with macro identifiers
5511 * right before they are fetched in. The reason is that there can be
5512 * identifiers consisting of several subparts. We consider that if there
5513 * are more than one element forming the name, user wants a expansion,
5514 * otherwise it will be left as-is. Example:
5515 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005516 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005517 *
5518 * the identifier %$abc will be left as-is so that the handler for %define
5519 * will suck it and define the corresponding value. Other case:
5520 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005521 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005522 *
5523 * In this case user wants name to be expanded *before* %define starts
5524 * working, so we'll expand %$abc into something (if it has a value;
5525 * otherwise it will be left as-is) then concatenate all successive
5526 * PP_IDs into one.
5527 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005528static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005529{
5530 Token *cur, *oldnext = NULL;
5531
H. Peter Anvin734b1882002-04-30 21:01:08 +00005532 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005533 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005534
5535 cur = tline;
5536 while (cur->next &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07005537 (cur->next->type == TOK_ID || cur->next->type == TOK_PREPROC_ID ||
5538 cur->next->type == TOK_LOCAL_MACRO || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005539 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005540
5541 /* If identifier consists of just one token, don't expand */
5542 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005543 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005544
H. Peter Anvine2c80182005-01-15 22:15:51 +00005545 if (cur) {
5546 oldnext = cur->next; /* Detach the tail past identifier */
5547 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005548 }
5549
H. Peter Anvin734b1882002-04-30 21:01:08 +00005550 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005551
H. Peter Anvine2c80182005-01-15 22:15:51 +00005552 if (cur) {
5553 /* expand_smacro possibly changhed tline; re-scan for EOL */
5554 cur = tline;
5555 while (cur && cur->next)
5556 cur = cur->next;
5557 if (cur)
5558 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005559 }
5560
5561 return tline;
5562}
5563
5564/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005565 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005566 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005567 * to check for an initial label - that's taken care of in
5568 * expand_mmacro - but must check numbers of parameters. Guaranteed
5569 * to be called with tline->type == TOK_ID, so the putative macro
5570 * name is easy to find.
5571 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005572static MMacro *is_mmacro(Token * tline, int *nparamp, Token ***params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005573{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005574 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005575 Token **params;
5576 int nparam;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005577 const char *finding = tok_text(tline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005578
H. Peter Anvin8571f062019-09-23 16:40:03 -07005579 head = (MMacro *) hash_findix(&mmacros, finding);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005580
5581 /*
5582 * Efficiency: first we see if any macro exists with the given
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005583 * name which isn't already excluded by macro cycle removal.
5584 * (The cycle removal test here helps optimize the case of wrapping
5585 * instructions, and is cheap to do here.)
5586 *
5587 * If not, we can return NULL immediately. _Then_ we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005588 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005589 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005590 */
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005591 list_for_each(m, head) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005592 if (!mstrcmp(m->name, finding, m->casesense) &&
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005593 (m->in_progress != 1 || m->max_depth > 0))
5594 break; /* Found something that needs consideration */
5595 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005596 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005597 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005598
5599 /*
5600 * OK, we have a potential macro. Count and demarcate the
5601 * parameters.
5602 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00005603 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005604
5605 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005606 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005607 * structure that handles this number.
5608 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005609 while (m) {
5610 if (m->nparam_min <= nparam
5611 && (m->plus || nparam <= m->nparam_max)) {
5612 /*
5613 * This one is right. Just check if cycle removal
5614 * prohibits us using it before we actually celebrate...
5615 */
5616 if (m->in_progress > m->max_depth) {
5617 if (m->max_depth > 0) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08005618 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005619 m->max_depth);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005620 }
5621 nasm_free(params);
5622 return NULL;
5623 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005624 /*
5625 * It's right, and we can use it. Add its default
5626 * parameters to the end of our list if necessary.
5627 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005628 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005629 int newnparam = m->nparam_min + m->ndefs;
5630 params = nasm_realloc(params, sizeof(*params) * (newnparam+2));
5631 memcpy(&params[nparam+1], &m->defaults[nparam+1-m->nparam_min],
5632 (newnparam - nparam) * sizeof(*params));
5633 nparam = newnparam;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005634 }
5635 /*
5636 * If we've gone over the maximum parameter count (and
5637 * we're in Plus mode), ignore parameters beyond
5638 * nparam_max.
5639 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005640 if (m->plus && nparam > m->nparam_max)
5641 nparam = m->nparam_max;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005642
H. Peter Anvin (Intel)7eb18212019-08-20 16:24:46 -07005643 /*
5644 * If nparam was adjusted above, make sure the list is still
5645 * NULL-terminated.
5646 */
5647 params[nparam+1] = NULL;
5648
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005649 /* Done! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005650 *params_array = params;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005651 *nparamp = nparam;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005652 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005653 }
5654 /*
5655 * This one wasn't right: look for the next one with the
5656 * same name.
5657 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005658 list_for_each(m, m->next)
H. Peter Anvin8571f062019-09-23 16:40:03 -07005659 if (!mstrcmp(m->name, tok_text(tline), m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005660 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005661 }
5662
5663 /*
5664 * After all that, we didn't find one with the right number of
5665 * parameters. Issue a warning, and fail to expand the macro.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005666 *!
5667 *!macro-params-multi [on] multi-line macro calls with wrong parameter count
5668 *! warns about \i{multi-line macros} being invoked
5669 *! with the wrong number of parameters. See \k{mlmacover} for an
5670 *! example of why you might want to disable this warning.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005671 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005672 nasm_warn(WARN_MACRO_PARAMS_MULTI,
5673 "multi-line macro `%s' exists, but not taking %d parameter%s",
H. Peter Anvin8571f062019-09-23 16:40:03 -07005674 tok_text(tline), nparam, (nparam == 1) ? "" : "s");
H. Peter Anvin734b1882002-04-30 21:01:08 +00005675 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005676 return NULL;
5677}
5678
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005679
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005680#if 0
5681
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005682/*
5683 * Save MMacro invocation specific fields in
5684 * preparation for a recursive macro expansion
5685 */
5686static void push_mmacro(MMacro *m)
5687{
5688 MMacroInvocation *i;
5689
5690 i = nasm_malloc(sizeof(MMacroInvocation));
5691 i->prev = m->prev;
5692 i->params = m->params;
5693 i->iline = m->iline;
5694 i->nparam = m->nparam;
5695 i->rotate = m->rotate;
5696 i->paramlen = m->paramlen;
5697 i->unique = m->unique;
5698 i->condcnt = m->condcnt;
5699 m->prev = i;
5700}
5701
5702
5703/*
5704 * Restore MMacro invocation specific fields that were
5705 * saved during a previous recursive macro expansion
5706 */
5707static void pop_mmacro(MMacro *m)
5708{
5709 MMacroInvocation *i;
5710
5711 if (m->prev) {
5712 i = m->prev;
5713 m->prev = i->prev;
5714 m->params = i->params;
5715 m->iline = i->iline;
5716 m->nparam = i->nparam;
5717 m->rotate = i->rotate;
5718 m->paramlen = i->paramlen;
5719 m->unique = i->unique;
5720 m->condcnt = i->condcnt;
5721 nasm_free(i);
5722 }
5723}
5724
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005725#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005726
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005727/*
H. Peter Anvin (Intel)ffe89dd2019-08-20 16:06:36 -07005728 * List an mmacro call with arguments (-Lm option)
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005729 */
5730static void list_mmacro_call(const MMacro *m)
5731{
5732 const char prefix[] = " ;;; [macro] ";
5733 size_t namelen, size;
5734 char *buf, *p;
5735 unsigned int i;
5736 const Token *t;
5737
5738 namelen = strlen(m->iname);
5739 size = namelen + sizeof(prefix); /* Includes final null (from prefix) */
5740
5741 for (i = 1; i <= m->nparam; i++) {
5742 int j = 0;
5743 size += 3; /* Braces and space/comma */
5744 list_for_each(t, m->params[i]) {
5745 if (j++ >= m->paramlen[i])
5746 break;
5747 size += (t->type == TOK_WHITESPACE) ? 1 : t->len;
5748 }
5749 }
5750
5751 buf = p = nasm_malloc(size);
5752 p = mempcpy(p, prefix, sizeof(prefix) - 1);
5753 p = mempcpy(p, m->iname, namelen);
5754 *p++ = ' ';
5755
5756 for (i = 1; i <= m->nparam; i++) {
5757 int j = 0;
5758 *p++ = '{';
5759 list_for_each(t, m->params[i]) {
5760 if (j++ >= m->paramlen[i])
5761 break;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005762 p = mempcpy(p, tok_text(t), t->len);
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005763 }
5764 *p++ = '}';
5765 *p++ = ',';
5766 }
5767
5768 *--p = '\0'; /* Replace last delimeter with null */
5769 lfmt->line(LIST_MACRO, -1, buf);
5770 nasm_free(buf);
5771}
5772
5773/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005774 * Expand the multi-line macro call made by the given line, if
5775 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005776 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005777 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005778static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005779{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005780 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005781 Token *label = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005782 bool dont_prepend = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005783 Token **params, *t, *tt;
5784 MMacro *m;
5785 Line *l, *ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005786 int i, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07005787 const char *mname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005788 int nparam = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005789
5790 t = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005791 t = skip_white(t);
5792 /* if (!tok_type(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07005793 if (!tok_type(t, TOK_ID) && !tok_type(t, TOK_LOCAL_MACRO))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005794 return 0;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005795 m = is_mmacro(t, &nparam, &params);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005796 if (m) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005797 mname = tok_text(t);
H. Peter Anvinc751e862008-06-09 10:18:45 -07005798 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005799 Token *last;
5800 /*
5801 * We have an id which isn't a macro call. We'll assume
5802 * it might be a label; we'll also check to see if a
5803 * colon follows it. Then, if there's another id after
5804 * that lot, we'll check it again for macro-hood.
5805 */
5806 label = last = t;
5807 t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005808 if (tok_white(t))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005809 last = t, t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005810 if (tok_is(t, ':')) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005811 dont_prepend = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005812 last = t, t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005813 if (tok_white(t))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005814 last = t, t = t->next;
5815 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005816 if (!tok_type(t, TOK_ID) || !(m = is_mmacro(t, &nparam, &params)))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005817 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005818 last->next = NULL;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005819 mname = tok_text(t);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005820 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005821 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005822
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005823 if (unlikely(mmacro_deadman.total >= nasm_limit[LIMIT_MMACROS] ||
5824 mmacro_deadman.levels >= nasm_limit[LIMIT_MACRO_LEVELS])) {
5825 if (!mmacro_deadman.triggered) {
5826 nasm_nonfatal("interminable multiline macro recursion");
5827 mmacro_deadman.triggered = true;
5828 }
5829 return 0;
5830 }
5831
5832 mmacro_deadman.total++;
5833 mmacro_deadman.levels++;
5834
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005835 /*
5836 * Fix up the parameters: this involves stripping leading and
5837 * trailing whitespace, then stripping braces if they are
5838 * present.
5839 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005840 nasm_newn(paramlen, nparam+1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005841
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005842 nasm_assert(params[nparam+1] == NULL);
5843
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005844 for (i = 1; (t = params[i]); i++) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005845 bool braced = false;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005846 int brace = 0;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005847 int white = 0;
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005848 bool comma = !m->plus || i < nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005849
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005850 t = skip_white(t);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005851 if (tok_is(t, '{')) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005852 t = t->next;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005853 brace = 1;
5854 braced = true;
5855 comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005856 }
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005857
5858 params[i] = t;
5859 for (; t; t = t->next) {
5860 if (tok_white(t)) {
5861 white++;
5862 continue;
5863 }
5864
5865 if (t->type == TOK_OTHER && t->len == 1) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005866 switch (t->text.a[0]) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005867 case ',':
5868 if (comma && !brace)
5869 goto endparam;
5870 break;
5871
5872 case '{':
5873 brace++;
5874 break;
5875
5876 case '}':
5877 brace--;
5878 if (braced && !brace) {
5879 paramlen[i] += white;
5880 goto endparam;
5881 }
5882 break;
5883
5884 default:
5885 break;
5886 }
5887 }
5888
5889 paramlen[i] += white + 1;
5890 white = 0;
5891 }
5892 endparam:
5893 ;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005894 }
5895
5896 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005897 * OK, we have a MMacro structure together with a set of
5898 * parameters. We must now go through the expansion and push
5899 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00005900 * parameter tokens and macro-local tokens doesn't get done
5901 * until the single-line macro substitution process; this is
5902 * because delaying them allows us to change the semantics
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005903 * later through %rotate and give the right semantics for
5904 * nested mmacros.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005905 *
5906 * First, push an end marker on to istk->expansion, mark this
5907 * macro as in progress, and set up its invocation-specific
5908 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005909 */
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005910 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005911 ll->next = istk->expansion;
5912 ll->finishes = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005913 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005914
5915 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005916 * Save the previous MMacro expansion in the case of
5917 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005918 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005919#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005920 if (m->max_depth && m->in_progress)
5921 push_mmacro(m);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005922#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005923
5924 m->in_progress ++;
5925 m->params = params;
5926 m->iline = tline;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005927 m->iname = nasm_strdup(mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005928 m->nparam = nparam;
5929 m->rotate = 0;
5930 m->paramlen = paramlen;
5931 m->unique = unique++;
5932 m->lineno = 0;
5933 m->condcnt = 0;
5934
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005935 m->mstk = istk->mstk;
5936 istk->mstk.mstk = istk->mstk.mmac = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005937
5938 list_for_each(l, m->expansion) {
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005939 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005940 ll->next = istk->expansion;
5941 istk->expansion = ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005942 ll->first = dup_tlist(l->first, NULL);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005943 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005944
5945 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005946 * If we had a label, and this macro definition does not include
5947 * a %00, push it on as the first line of, ot
H. Peter Anvineba20a72002-04-30 20:53:55 +00005948 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005949 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005950 if (label) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005951 /*
5952 * We had a label. If this macro contains an %00 parameter,
5953 * save the value as a special parameter (which is what it
5954 * is), otherwise push it as the first line of the macro
5955 * expansion.
5956 */
5957 if (m->capture_label) {
5958 params[0] = dup_Token(NULL, label);
5959 paramlen[0] = 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005960 free_tlist(startline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005961 } else {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005962 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005963 ll->finishes = NULL;
5964 ll->next = istk->expansion;
5965 istk->expansion = ll;
5966 ll->first = startline;
5967 if (!dont_prepend) {
5968 while (label->next)
5969 label = label->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005970 label->next = tt = make_tok_char(NULL, ':');
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005971 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005972 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005973 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005974
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07005975 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005976
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005977 if (list_option('m') && !m->nolist)
5978 list_mmacro_call(m);
5979
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005980 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005981}
5982
H. Peter Anvin130736c2016-02-17 20:27:41 -08005983/*
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005984 * This function decides if an error message should be suppressed.
5985 * It will never be called with a severity level of ERR_FATAL or
5986 * higher.
H. Peter Anvin130736c2016-02-17 20:27:41 -08005987 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005988static bool pp_suppress_error(errflags severity)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005989{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005990 /*
5991 * If we're in a dead branch of IF or something like it, ignore the error.
5992 * However, because %else etc are evaluated in the state context
5993 * of the previous branch, errors might get lost:
5994 * %if 0 ... %else trailing garbage ... %endif
5995 * So %else etc should set the ERR_PP_PRECOND flag.
5996 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005997 if (istk && istk->conds &&
H. Peter Anvin130736c2016-02-17 20:27:41 -08005998 ((severity & ERR_PP_PRECOND) ?
5999 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07006000 !emitting(istk->conds->state)))
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006001 return true;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02006002
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006003 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00006004}
6005
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006006static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006007stdmac_file(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006008{
6009 (void)s;
6010 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006011 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006012
H. Peter Anvin8571f062019-09-23 16:40:03 -07006013 return make_tok_qstr(NULL, src_get_fname());
H. Peter Anvin8b262472019-02-26 14:00:54 -08006014}
6015
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006016static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006017stdmac_line(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006018{
6019 (void)s;
6020 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006021 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006022
H. Peter Anvin8571f062019-09-23 16:40:03 -07006023 return make_tok_num(NULL, src_get_linnum());
H. Peter Anvin8b262472019-02-26 14:00:54 -08006024}
6025
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006026static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006027stdmac_bits(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006028{
6029 (void)s;
6030 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006031 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006032
H. Peter Anvin8571f062019-09-23 16:40:03 -07006033 return make_tok_num(NULL, globalbits);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006034}
6035
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006036static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006037stdmac_ptr(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006038{
H. Peter Anvin8b262472019-02-26 14:00:54 -08006039 (void)s;
6040 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006041 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006042
6043 switch (globalbits) {
6044 case 16:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006045 return new_Token(NULL, TOK_ID, "word", 4);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006046 case 32:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006047 return new_Token(NULL, TOK_ID, "dword", 5);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006048 case 64:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006049 return new_Token(NULL, TOK_ID, "qword", 5);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006050 default:
6051 panic();
6052 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08006053}
6054
H. Peter Anvin8b262472019-02-26 14:00:54 -08006055/* Add magic standard macros */
6056struct magic_macros {
6057 const char *name;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006058 int nparam;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006059 ExpandSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006060};
6061static const struct magic_macros magic_macros[] =
6062{
H. Peter Anvind2354082019-08-27 16:38:48 -07006063 { "__?FILE?__", 0, stdmac_file },
6064 { "__?LINE?__", 0, stdmac_line },
6065 { "__?BITS?__", 0, stdmac_bits },
6066 { "__?PTR?__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08006067 { NULL, 0, NULL }
6068};
6069
6070static void pp_add_magic_stdmac(void)
6071{
6072 const struct magic_macros *m;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006073 SMacro tmpl;
6074
6075 nasm_zero(tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006076
6077 for (m = magic_macros; m->name; m++) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006078 tmpl.nparam = m->nparam;
6079 tmpl.expand = m->func;
6080 define_smacro(m->name, true, NULL, &tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006081 }
6082}
6083
H. Peter Anvin734b1882002-04-30 21:01:08 +00006084static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006085pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006086{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006087 int apass;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006088 struct Include *inc;
H. Peter Anvin7383b402008-09-24 10:20:40 -07006089
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006090 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006091 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07006092 nested_mac_count = 0;
6093 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07006094 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006095 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07006096 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006097 pp_mode = mode;
H. Peter Anvind2354082019-08-27 16:38:48 -07006098 do_aliases = true;
H. Peter Anvinf7606612016-07-13 14:23:48 -07006099
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07006100 if (!use_loaded)
6101 use_loaded = nasm_malloc(use_package_count * sizeof(bool));
6102 memset(use_loaded, 0, use_package_count * sizeof(bool));
6103
H. Peter Anvin6686de22019-08-10 05:33:14 -07006104 /* First set up the top level input file */
6105 nasm_new(istk);
6106 istk->fp = nasm_open_read(file, NF_TEXT);
6107 src_set(0, file);
6108 istk->lineinc = 1;
6109 if (!istk->fp)
6110 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
6111
6112 strlist_add(deplist, file);
6113
6114 /*
6115 * Set up the stdmac packages as a virtual include file,
6116 * indicated by a null file pointer.
6117 */
6118 nasm_new(inc);
6119 inc->next = istk;
6120 inc->fname = src_set_fname(NULL);
6121 inc->nolist = !list_option('b');
6122 istk = inc;
6123 lfmt->uplevel(LIST_INCLUDE, 0);
6124
H. Peter Anvin8b262472019-02-26 14:00:54 -08006125 pp_add_magic_stdmac();
6126
H. Peter Anvinf7606612016-07-13 14:23:48 -07006127 if (tasm_compatible_mode)
6128 pp_add_stdmac(nasm_stdmac_tasm);
6129
6130 pp_add_stdmac(nasm_stdmac_nasm);
6131 pp_add_stdmac(nasm_stdmac_version);
6132
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006133 if (extrastdmac)
6134 pp_add_stdmac(extrastdmac);
6135
H. Peter Anvinf7606612016-07-13 14:23:48 -07006136 stdmacpos = stdmacros[0];
6137 stdmacnext = &stdmacros[1];
6138
H. Peter Anvind2456592008-06-19 15:04:18 -07006139 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006140
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006141 /*
H. Peter Anvind2354082019-08-27 16:38:48 -07006142 * Define the __?PASS?__ macro. This is defined here unlike all the
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07006143 * other builtins, because it is special -- it varies between
6144 * passes -- but there is really no particular reason to make it
6145 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006146 *
6147 * 0 = dependencies only
6148 * 1 = preparatory passes
6149 * 2 = final pass
6150 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006151 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006152 switch (mode) {
6153 case PP_NORMAL:
6154 apass = pass_final() ? 2 : 1;
6155 break;
6156 case PP_DEPS:
6157 apass = 0;
6158 break;
6159 case PP_PREPROC:
6160 apass = 3;
6161 break;
6162 default:
6163 panic();
6164 }
6165
H. Peter Anvin8571f062019-09-23 16:40:03 -07006166 define_smacro("__?PASS?__", true, make_tok_num(NULL, apass), NULL);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006167}
6168
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006169static void pp_init(void)
6170{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006171}
6172
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006173/*
6174 * Get a line of tokens. If we popped the macro expansion/include stack,
6175 * we return a pointer to the dummy token tok_pop; at that point if
6176 * istk is NULL then we have reached end of input;
6177 */
6178static Token tok_pop; /* Dummy token placeholder */
6179
6180static Token *pp_tokline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006181{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006182 while (true) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006183 Line *l = istk->expansion;
6184 Token *tline = NULL;
6185 Token *dtline;
6186
H. Peter Anvine2c80182005-01-15 22:15:51 +00006187 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006188 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00006189 * buffer or from the input file.
6190 */
6191 tline = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006192 while (l && l->finishes) {
6193 MMacro *fm = l->finishes;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006194
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006195 if (!fm->name && fm->in_progress > 1) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006196 /*
6197 * This is a macro-end marker for a macro with no
6198 * name, which means it's not really a macro at all
6199 * but a %rep block, and the `in_progress' field is
6200 * more than 1, meaning that we still need to
6201 * repeat. (1 means the natural last repetition; 0
6202 * means termination by %exitrep.) We have
6203 * therefore expanded up to the %endrep, and must
6204 * push the whole block on to the expansion buffer
6205 * again. We don't bother to remove the macro-end
6206 * marker: we'd only have to generate another one
6207 * if we did.
6208 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006209 fm->in_progress--;
6210 list_for_each(l, fm->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006211 Token *t, *tt, **tail;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006212 Line *ll;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006213
Chang S. Baebec812f2020-02-07 15:49:38 -08006214 istk->mstk.mstk->lineno = 0;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006215 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006216 ll->next = istk->expansion;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006217 tail = &ll->first;
6218
6219 list_for_each(t, l->first) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006220 if (t->len) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006221 tt = *tail = dup_Token(NULL, t);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006222 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006223 }
6224 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006225 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006226 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006227 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006228 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006229 MMacro *m = istk->mstk.mstk;
6230
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006231 /*
6232 * Check whether a `%rep' was started and not ended
6233 * within this macro expansion. This can happen and
6234 * should be detected. It's a fatal error because
6235 * I'm too confused to work out how to recover
6236 * sensibly from it.
6237 */
6238 if (defining) {
6239 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07006240 nasm_panic("defining with name in expansion");
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006241 else if (m->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07006242 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006243 " expansion of macro `%s'", m->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006244 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006245
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006246 /*
6247 * FIXME: investigate the relationship at this point between
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006248 * istk->mstk.mstk and fm
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006249 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006250 istk->mstk = m->mstk;
6251 if (m->name) {
6252 /*
6253 * This was a real macro call, not a %rep, and
6254 * therefore the parameter information needs to
6255 * be freed and the iteration count/nesting
6256 * depth adjusted.
6257 */
6258
6259 if (!--mmacro_deadman.levels) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006260 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006261 * If all mmacro processing done,
6262 * clear all counters and the deadman
6263 * message trigger.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006264 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006265 nasm_zero(mmacro_deadman); /* Clear all counters */
Adam Majer91e72402017-07-25 10:42:01 +02006266 }
6267
Adam Majer91e72402017-07-25 10:42:01 +02006268#if 0
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006269 if (m->prev) {
6270 pop_mmacro(m);
6271 fm->in_progress --;
6272 } else
Adam Majer91e72402017-07-25 10:42:01 +02006273#endif
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006274 {
6275 nasm_free(m->params);
6276 free_tlist(m->iline);
6277 nasm_free(m->paramlen);
6278 fm->in_progress = 0;
6279 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006280 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006281
6282 /*
6283 * FIXME It is incorrect to always free_mmacro here.
6284 * It leads to usage-after-free.
6285 *
6286 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
6287 */
6288#if 0
6289 else
6290 free_mmacro(m);
6291#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006292 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006293 istk->expansion = l->next;
6294 nasm_free(l);
6295 lfmt->downlevel(LIST_MACRO);
6296 return &tok_pop;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006297 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006298
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006299 do { /* until we get a line we can use */
6300 char *line;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006301
6302 if (istk->expansion) { /* from a macro expansion */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006303 Line *l = istk->expansion;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006304 int32_t lineno;
6305
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006306 if (istk->mstk.mstk) {
6307 istk->mstk.mstk->lineno++;
6308 if (istk->mstk.mstk->fname)
6309 lineno = istk->mstk.mstk->lineno +
6310 istk->mstk.mstk->xline;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006311 else
6312 lineno = 0; /* Defined at init time or builtin */
6313 } else {
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006314 lineno = src_get_linnum();
H. Peter Anvin6686de22019-08-10 05:33:14 -07006315 }
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006316
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006317 tline = l->first;
6318 istk->expansion = l->next;
6319 nasm_free(l);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006320
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006321 line = detoken(tline, false);
H. Peter Anvin6686de22019-08-10 05:33:14 -07006322 if (!istk->nolist)
6323 lfmt->line(LIST_MACRO, lineno, line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006324 nasm_free(line);
6325 } else if ((line = read_line())) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006326 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006327 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006328 nasm_free(line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006329 } else {
6330 /*
6331 * The current file has ended; work down the istk
6332 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00006333 Include *i = istk;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006334 if (i->fp)
6335 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006336 if (i->conds) {
6337 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07006338 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006339 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00006340 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07006341 if (i->next)
6342 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006343 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08006344 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006345 nasm_free(i);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006346 return &tok_pop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006347 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006348 } while (0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006349
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006350 /*
6351 * We must expand MMacro parameters and MMacro-local labels
6352 * _before_ we plunge into directive processing, to cope
6353 * with things like `%define something %1' such as STRUC
6354 * uses. Unless we're _defining_ a MMacro, in which case
6355 * those tokens should be left alone to go into the
6356 * definition; and unless we're in a non-emitting
6357 * condition, in which case we don't want to meddle with
6358 * anything.
6359 */
6360 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006361 && !(istk->mstk.mstk && !istk->mstk.mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006362 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006363 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006364
H. Peter Anvine2c80182005-01-15 22:15:51 +00006365 /*
6366 * Check the line to see if it's a preprocessor directive.
6367 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006368 if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) {
6369 if (dtline)
6370 return dtline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006371 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006372 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006373 * We're defining a multi-line macro. We emit nothing
6374 * at all, and just
6375 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00006376 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006377 MMacro *mmac = defining->dstk.mmac;
6378
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006379 Line *l = nasm_malloc(sizeof(Line));
6380 l->next = defining->expansion;
6381 l->first = tline;
6382 l->finishes = NULL;
6383 defining->expansion = l;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006384
6385 /*
6386 * Remember if this mmacro expansion contains %00:
6387 * if it does, we will have to handle leading labels
6388 * specially.
6389 */
6390 if (mmac) {
6391 const Token *t;
6392 list_for_each(t, tline) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006393 if (!memcmp(t->text.a, "%00", 4))
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006394 mmac->capture_label = true;
6395 }
6396 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006397 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006398 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006399 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00006400 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006401 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00006402 * directive so we keep our place correctly.
6403 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006404 free_tlist(tline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006405 } else if (istk->mstk.mstk && !istk->mstk.mstk->in_progress) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006406 /*
6407 * We're in a %rep block which has been terminated, so
6408 * we're walking through to the %endrep without
6409 * emitting anything. Emit nothing at all, not even a
6410 * blank line: when we emerge from the %rep block we'll
6411 * give a line-number directive so we keep our place
6412 * correctly.
6413 */
6414 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006415 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006416 tline = expand_smacro(tline);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006417 if (!expand_mmacro(tline))
6418 return tline;
6419 }
6420 }
6421}
6422
6423static char *pp_getline(void)
6424{
6425 char *line = NULL;
6426 Token *tline;
6427
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006428 while (true) {
6429 tline = pp_tokline();
6430 if (tline == &tok_pop) {
6431 /*
6432 * We popped the macro/include stack. If istk is empty,
6433 * we are at end of input, otherwise just loop back.
6434 */
6435 if (!istk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006436 break;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006437 } else {
6438 /*
6439 * De-tokenize the line and emit it.
6440 */
6441 line = detoken(tline, true);
6442 free_tlist(tline);
6443 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006444 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006445 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006446
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006447 if (list_option('e') && istk && !istk->nolist && line && line[0]) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006448 char *buf = nasm_strcat(" ;;; ", line);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006449 lfmt->line(LIST_MACRO, -1, buf);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006450 nasm_free(buf);
6451 }
6452
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006453 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006454}
6455
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006456static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006457{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006458 if (defining) {
6459 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006460 nasm_nonfatal("end of file while still defining macro `%s'",
6461 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006462 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006463 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006464 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006465
6466 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006467 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006468 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08006469
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006470 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006471 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07006472 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006473 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006474 Include *i = istk;
6475 istk = istk->next;
6476 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03006477 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006478 }
6479 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006480 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07006481 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006482}
6483
6484static void pp_cleanup_session(void)
6485{
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07006486 nasm_free(use_loaded);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006487 free_llist(predef);
6488 predef = NULL;
6489 delete_Blocks();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006490 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006491}
6492
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006493static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006494{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006495 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006496}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00006497
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006498static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006499{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006500 Token *inc, *space, *name;
6501 Line *l;
6502
H. Peter Anvin734b1882002-04-30 21:01:08 +00006503 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006504 space = new_White(name);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006505 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006506
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006507 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006508 l->next = predef;
6509 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006510 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006511 predef = l;
6512}
6513
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006514static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006515{
6516 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006517 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00006518 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006519
6520 equals = strchr(definition, '=');
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006521 space = new_White(NULL);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006522 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006523 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006524 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00006525 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006526 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006527 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006528
H. Peter Anvin8571f062019-09-23 16:40:03 -07006529 /* We can't predefine a TOK_LOCAL_MACRO for obvious reasons... */
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006530 if (space->next->type != TOK_PREPROC_ID &&
6531 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08006532 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006533
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006534 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006535 l->next = predef;
6536 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006537 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006538 predef = l;
6539}
6540
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006541static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00006542{
6543 Token *def, *space;
6544 Line *l;
6545
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006546 space = new_White(NULL);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006547 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006548 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00006549
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006550 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00006551 l->next = predef;
6552 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006553 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00006554 predef = l;
6555}
6556
H. Peter Anvin05990342018-06-11 13:32:42 -07006557/* Insert an early preprocessor command that doesn't need special handling */
6558static void pp_pre_command(const char *what, char *string)
6559{
6560 char *cmd;
6561 Token *def, *space;
6562 Line *l;
6563
6564 def = tokenize(string);
6565 if (what) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006566 space = new_White(def);
H. Peter Anvin8571f062019-09-23 16:40:03 -07006567 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
6568 def = new_Token(space, TOK_PREPROC_ID, cmd, nasm_last_string_len());
6569 nasm_free(cmd);
H. Peter Anvin05990342018-06-11 13:32:42 -07006570 }
6571
6572 l = nasm_malloc(sizeof(Line));
6573 l->next = predef;
6574 l->first = def;
6575 l->finishes = NULL;
6576 predef = l;
6577}
6578
H. Peter Anvinf7606612016-07-13 14:23:48 -07006579static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006580{
H. Peter Anvinf7606612016-07-13 14:23:48 -07006581 macros_t **mp;
6582
6583 /* Find the end of the list and avoid duplicates */
6584 for (mp = stdmacros; *mp; mp++) {
6585 if (*mp == macros)
6586 return; /* Nothing to do */
6587 }
6588
6589 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
6590
6591 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00006592}
6593
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006594static void pp_extra_stdmac(macros_t *macros)
6595{
6596 extrastdmac = macros;
6597}
6598
H. Peter Anvin8571f062019-09-23 16:40:03 -07006599/* Create a numeric token */
6600static Token *make_tok_num(Token *next, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006601{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04006602 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08006603 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvin8571f062019-09-23 16:40:03 -07006604 return new_Token(next, TOK_NUMBER, numbuf, len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006605}
6606
H. Peter Anvin8571f062019-09-23 16:40:03 -07006607/* Create a quoted string token */
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07006608static Token *make_tok_qstr_len(Token *next, const char *str, size_t len)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006609{
H. Peter Anvin8571f062019-09-23 16:40:03 -07006610 char *p = nasm_quote(str, &len);
6611 return new_Token_free(next, TOK_STRING, p, len);
6612}
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07006613static Token *make_tok_qstr(Token *next, const char *str)
6614{
6615 return make_tok_qstr_len(next, str, strlen(str));
6616}
H. Peter Anvin8571f062019-09-23 16:40:03 -07006617
6618/* Create a single-character operator token */
6619static Token *make_tok_char(Token *next, char op)
6620{
6621 Token *t = new_Token(next, TOK_OTHER, NULL, 1);
6622 t->text.a[0] = op;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006623 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006624}
6625
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006626static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07006627{
6628 if (!m)
6629 return;
6630
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006631 /* We need to print the mstk.mmac list in reverse order */
6632 pp_list_one_macro(m->mstk.mmac, severity);
H. Peter Anvin37368952016-05-09 14:10:32 -07006633
6634 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07006635 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08006636 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07006637 }
6638}
6639
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006640static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006641{
H. Peter Anvinddb29062018-12-11 00:06:29 -08006642 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006643
H. Peter Anvinddb29062018-12-11 00:06:29 -08006644 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
6645 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006646
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03006647 if (istk)
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006648 pp_list_one_macro(istk->mstk.mmac, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006649
H. Peter Anvinddb29062018-12-11 00:06:29 -08006650 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006651}
6652
H. Peter Anvine7469712016-02-18 02:20:59 -08006653const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006654 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006655 pp_reset,
6656 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006657 pp_cleanup_pass,
6658 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006659 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006660 pp_pre_define,
6661 pp_pre_undefine,
6662 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07006663 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006664 pp_include_path,
6665 pp_error_list_macros,
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006666 pp_suppress_error
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006667};