blob: 5cb928793154d1eb34f2d735de44c34e75f11143 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002 *
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07003 * Copyright 1996-2020 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000010 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070011 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * preproc.c macro preprocessor for the Netwide Assembler
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000036 */
37
H. Peter Anvin4836e332002-04-30 20:56:43 +000038/* Typical flow of text through preproc
39 *
Keith Kaniosb7a89542007-04-12 02:40:54 +000040 * pp_getline gets tokenized lines, either
H. Peter Anvin4836e332002-04-30 20:56:43 +000041 *
42 * from a macro expansion
43 *
44 * or
45 * {
46 * read_line gets raw text from stdmacpos, or predef, or current input file
Keith Kaniosb7a89542007-04-12 02:40:54 +000047 * tokenize converts to tokens
H. Peter Anvin4836e332002-04-30 20:56:43 +000048 * }
49 *
50 * expand_mmac_params is used to expand %1 etc., unless a macro is being
51 * defined or a false conditional is being processed
52 * (%0, %1, %+1, %-1, %%foo
53 *
54 * do_directive checks for directives
55 *
56 * expand_smacro is used to expand single line macros
57 *
58 * expand_mmacro is used to expand multi-line macros
59 *
60 * detoken is used to convert the line back to text
61 */
H. Peter Anvineba20a72002-04-30 20:53:55 +000062
H. Peter Anvinfe501952007-10-02 21:53:51 -070063#include "compiler.h"
64
H. Peter Anvinc2f3f262018-12-27 12:37:25 -080065#include "nctype.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000066
67#include "nasm.h"
68#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080069#include "error.h"
H. Peter Anvin4169a472007-09-12 01:29:43 +000070#include "preproc.h"
H. Peter Anvin97a23472007-09-16 17:57:25 -070071#include "hashtbl.h"
H. Peter Anvin8cad14b2008-06-01 17:23:51 -070072#include "quote.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070073#include "stdscan.h"
H. Peter Anvindbb640b2009-07-18 18:57:16 -070074#include "eval.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070075#include "tokens.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070076#include "tables.h"
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -080077#include "listing.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000078
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -070079/*
80 * Preprocessor execution options that can be controlled by %pragma or
81 * other directives. This structure is initialized to zero on each
82 * pass; this *must* reflect the default initial state.
83 */
84static struct pp_opts {
85 bool noaliases;
86 bool sane_empty_expansion;
87} ppopt;
88
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000089typedef struct SMacro SMacro;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080090typedef struct MMacro MMacro;
91typedef struct MMacroInvocation MMacroInvocation;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000092typedef struct Context Context;
93typedef struct Token Token;
94typedef struct Line Line;
95typedef struct Include Include;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080096typedef struct Cond Cond;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000097
98/*
H. Peter Anvin8571f062019-09-23 16:40:03 -070099 * This is the internal form which we break input lines up into.
100 * Typically stored in linked lists.
101 *
102 * Note that `type' serves a double meaning: TOK_SMAC_START_PARAMS is
103 * not necessarily used as-is, but is also used to encode the number
104 * and expansion type of substituted parameter. So in the definition
105 *
106 * %define a(x,=y) ( (x) & ~(y) )
107 *
108 * the token representing `x' will have its type changed to
109 * tok_smac_param(0) but the one representing `y' will be
110 * tok_smac_param(1); see the accessor functions below.
111 *
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700112 * TOK_INTERNAL_STRING is a string which has been unquoted, but should
113 * be treated as if it was a quoted string. The code is free to change
114 * one into the other at will. TOK_NAKED_STRING is a text token which
115 * should be treated as a string, but which MUST NOT be turned into a
116 * quoted string. TOK_INTERNAL_STRINGs can contain any character,
117 * including NUL, but TOK_NAKED_STRING must be a valid C string.
H. Peter Anvin8571f062019-09-23 16:40:03 -0700118 */
119enum pp_token_type {
120 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT,
121 TOK_CORRUPT, /* Token text modified in an unsafe manner, now bogus */
122 TOK_BLOCK, /* Storage block pointer, not a real token */
123 TOK_ID,
124 TOK_PREPROC_ID, TOK_MMACRO_PARAM, TOK_LOCAL_SYMBOL,
125 TOK_LOCAL_MACRO, TOK_ENVIRON, TOK_STRING,
126 TOK_NUMBER, TOK_FLOAT, TOK_OTHER,
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700127 TOK_INTERNAL_STRING, TOK_NAKED_STRING,
H. Peter Anvin8571f062019-09-23 16:40:03 -0700128 TOK_PREPROC_Q, TOK_PREPROC_QQ,
129 TOK_PASTE, /* %+ */
130 TOK_COND_COMMA, /* %, */
131 TOK_INDIRECT, /* %[...] */
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700132 TOK_XDEF_PARAM, /* Used during %xdefine processing */
H. Peter Anvin8571f062019-09-23 16:40:03 -0700133 TOK_SMAC_START_PARAMS, /* MUST BE LAST IN THE LIST!!! */
134 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
135};
136
137static inline enum pp_token_type tok_smac_param(int param)
138{
139 return TOK_SMAC_START_PARAMS + param;
140}
141static int smac_nparam(enum pp_token_type toktype)
142{
143 return toktype - TOK_SMAC_START_PARAMS;
144}
145static bool is_smac_param(enum pp_token_type toktype)
146{
147 return toktype >= TOK_SMAC_START_PARAMS;
148}
149
150#define PP_CONCAT_MASK(x) (1U << (x))
151
152struct tokseq_match {
153 int mask_head;
154 int mask_tail;
155};
156
157/*
158 * This is tuned so struct Token should be 64 bytes on 64-bit
159 * systems and 32 bytes on 32-bit systems. It enables them
160 * to be nicely cache aligned, and the text to still be kept
161 * inline for nearly all tokens.
162 *
163 * We prohibit tokens of length > MAX_TEXT even though
164 * length here is an unsigned int; this avoids problems
165 * if the length is passed through an interface with type "int",
166 * and is absurdly large anyway.
167 *
168 * For the text mode, in pointer mode the pointer is stored at the end
169 * of the union and the pad field is cleared. This allows short tokens
170 * to be unconditionally tested for by only looking at the first text
171 * bytes and not examining the type or len fields.
172 */
173#define INLINE_TEXT (7*sizeof(char *)-sizeof(enum pp_token_type)-sizeof(unsigned int)-1)
174#define MAX_TEXT (INT_MAX-2)
175
176struct Token {
177 Token *next;
178 enum pp_token_type type;
179 unsigned int len;
180 union {
181 char a[INLINE_TEXT+1];
182 struct {
183 char pad[INLINE_TEXT+1 - sizeof(char *)];
184 char *ptr;
185 } p;
186 } text;
187};
188
189/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700190 * Note on the storage of both SMacro and MMacros: the hash table
191 * indexes them case-insensitively, and we then have to go through a
192 * linked list of potential case aliases (and, for MMacros, parameter
193 * ranges); this is to preserve the matching semantics of the earlier
194 * code. If the number of case aliases for a specific macro is a
195 * performance issue, you may want to reconsider your coding style.
196 */
197
198/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700199 * Function call tp obtain the expansion of an smacro
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700200 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700201typedef Token *(*ExpandSMacro)(const SMacro *s, Token **params, int nparams);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700202
203/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000204 * Store the definition of a single-line macro.
205 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700206enum sparmflags {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -0700207 SPARM_PLAIN = 0,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700208 SPARM_EVAL = 1, /* Evaluate as a numeric expression (=) */
209 SPARM_STR = 2, /* Convert to quoted string ($) */
210 SPARM_NOSTRIP = 4, /* Don't strip braces (!) */
211 SPARM_GREEDY = 8 /* Greedy final parameter (+) */
212};
213
214struct smac_param {
H. Peter Anvin8571f062019-09-23 16:40:03 -0700215 Token name;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700216 enum sparmflags flags;
217};
218
H. Peter Anvine2c80182005-01-15 22:15:51 +0000219struct SMacro {
H. Peter Anvin8b262472019-02-26 14:00:54 -0800220 SMacro *next; /* MUST BE FIRST - see free_smacro() */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800221 char *name;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700222 Token *expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700223 ExpandSMacro expand;
224 intorptr expandpvt;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700225 struct smac_param *params;
226 int nparam;
227 bool greedy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800228 bool casesense;
229 bool in_progress;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -0700230 bool alias; /* This is an alias macro */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000231};
232
233/*
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -0700234 * "No listing" flags. Inside a loop (%rep..%endrep) we may have
235 * macro listing suppressed with .nolist, but we still need to
236 * update line numbers for error messages and debug information...
237 * unless we are nested inside an actual .nolist macro.
238 */
239enum nolist_flags {
240 NL_LIST = 1, /* Suppress list output */
241 NL_LINE = 2 /* Don't update line information */
242};
243
244/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800245 * Store the definition of a multi-line macro. This is also used to
246 * store the interiors of `%rep...%endrep' blocks, which are
247 * effectively self-re-invoking multi-line macros which simply
248 * don't have a name or bother to appear in the hash tables. %rep
249 * blocks are signified by having a NULL `name' field.
250 *
251 * In a MMacro describing a `%rep' block, the `in_progress' field
252 * isn't merely boolean, but gives the number of repeats left to
253 * run.
254 *
255 * The `next' field is used for storing MMacros in hash tables; the
256 * `next_active' field is for stacking them on istk entries.
257 *
258 * When a MMacro is being expanded, `params', `iline', `nparam',
259 * `paramlen', `rotate' and `unique' are local to the invocation.
260 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700261
262/*
263 * Expansion stack. Note that .mmac can point back to the macro itself,
264 * whereas .mstk cannot.
265 */
266struct mstk {
267 MMacro *mstk; /* Any expansion, real macro or not */
268 MMacro *mmac; /* Highest level actual mmacro */
269};
270
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800271struct MMacro {
272 MMacro *next;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700273#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800274 MMacroInvocation *prev; /* previous invocation */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700275#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800276 char *name;
277 int nparam_min, nparam_max;
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -0700278 enum nolist_flags nolist; /* is this macro listing-inhibited? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800279 bool casesense;
280 bool plus; /* is the last parameter greedy? */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700281 bool capture_label; /* macro definition has %00; capture label */
282 int32_t in_progress; /* is this macro currently being expanded? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800283 int32_t max_depth; /* maximum number of recursive expansions allowed */
284 Token *dlist; /* All defaults as one list */
285 Token **defaults; /* Parameter default pointers */
286 int ndefs; /* number of default parameters */
287 Line *expansion;
288
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700289 struct mstk mstk; /* Macro expansion stack */
290 struct mstk dstk; /* Macro definitions stack */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800291 Token **params; /* actual parameters */
292 Token *iline; /* invocation line */
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700293 struct src_location where; /* location of definition */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800294 unsigned int nparam, rotate;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700295 char *iname; /* name invoked as */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800296 int *paramlen;
297 uint64_t unique;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800298 uint64_t condcnt; /* number of if blocks... */
299};
300
301
302/* Store the definition of a multi-line macro, as defined in a
303 * previous recursive macro expansion.
304 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700305#if 0
306
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800307struct MMacroInvocation {
308 MMacroInvocation *prev; /* previous invocation */
309 Token **params; /* actual parameters */
310 Token *iline; /* invocation line */
311 unsigned int nparam, rotate;
312 int *paramlen;
313 uint64_t unique;
314 uint64_t condcnt;
315};
316
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700317#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800318
319/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000320 * The context stack is composed of a linked list of these.
321 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000322struct Context {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800323 Context *next;
H. Peter Anvin8571f062019-09-23 16:40:03 -0700324 const char *name;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800325 struct hash_table localmac;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -0700326 uint64_t number;
327 unsigned int depth;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000328};
329
H. Peter Anvin8571f062019-09-23 16:40:03 -0700330
331static inline const char *tok_text(const struct Token *t)
332{
333 return (t->len <= INLINE_TEXT) ? t->text.a : t->text.p.ptr;
334}
335
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000336/*
H. Peter Anvin8571f062019-09-23 16:40:03 -0700337 * Returns a mutable pointer to the text buffer. The text can be changed,
338 * but the length MUST NOT CHANGE, in either direction; nor is it permitted
339 * to pad with null characters to create an artificially shorter string.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000340 */
H. Peter Anvin8571f062019-09-23 16:40:03 -0700341static inline char *tok_text_buf(struct Token *t)
H. Peter Anvin8b262472019-02-26 14:00:54 -0800342{
H. Peter Anvin8571f062019-09-23 16:40:03 -0700343 return (t->len <= INLINE_TEXT) ? t->text.a : t->text.p.ptr;
H. Peter Anvin8b262472019-02-26 14:00:54 -0800344}
345
H. Peter Anvin8571f062019-09-23 16:40:03 -0700346static inline unsigned int tok_check_len(size_t len)
347{
348 if (unlikely(len > MAX_TEXT))
349 nasm_fatal("impossibly large token");
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400350
H. Peter Anvin8571f062019-09-23 16:40:03 -0700351 return len;
352}
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400353
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700354static inline bool tok_text_match(const struct Token *a, const struct Token *b)
355{
356 return a->len == b->len && !memcmp(tok_text(a), tok_text(b), a->len);
357}
358
H. Peter Anvin (Intel)b8777082020-07-01 20:49:04 -0700359static inline unused_func bool
H. Peter Anvin (Intel)65ab3ab2020-06-30 10:14:21 -0700360tok_match(const struct Token *a, const struct Token *b)
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700361{
362 return a->type == b->type && tok_text_match(a, b);
363}
364
H. Peter Anvin8571f062019-09-23 16:40:03 -0700365/* strlen() variant useful for set_text() and its variants */
366static size_t tok_strlen(const char *str)
367{
368 return strnlen(str, MAX_TEXT+1);
369}
370
371/*
372 * Set the text field to a copy of the given string; the length if
373 * not given should be obtained with tok_strlen().
374 */
375static Token *set_text(struct Token *t, const char *text, size_t len)
376{
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700377 char *textp;
378
H. Peter Anvin8571f062019-09-23 16:40:03 -0700379 if (t->len > INLINE_TEXT)
380 nasm_free(t->text.p.ptr);
381
H. Peter Anvin (Intel)00335e42020-06-14 19:49:19 -0700382 nasm_zero(t->text);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700383
H. Peter Anvin (Intel)00335e42020-06-14 19:49:19 -0700384 t->len = len = tok_check_len(len);
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700385 textp = (len > INLINE_TEXT)
386 ? (t->text.p.ptr = nasm_malloc(len+1)) : t->text.a;
387 memcpy(textp, text, len);
388 textp[len] = '\0';
H. Peter Anvin8571f062019-09-23 16:40:03 -0700389 return t;
390}
391
392/*
393 * Set the text field to the existing pre-allocated string, either
394 * taking over or freeing the allocation in the process.
395 */
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700396static Token *set_text_free(struct Token *t, char *text, unsigned int len)
H. Peter Anvin8571f062019-09-23 16:40:03 -0700397{
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700398 char *textp;
399
H. Peter Anvin8571f062019-09-23 16:40:03 -0700400 if (t->len > INLINE_TEXT)
401 nasm_free(t->text.p.ptr);
402
H. Peter Anvin (Intel)00335e42020-06-14 19:49:19 -0700403 nasm_zero(t->text);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700404
H. Peter Anvin (Intel)00335e42020-06-14 19:49:19 -0700405 t->len = len = tok_check_len(len);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700406 if (len > INLINE_TEXT) {
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700407 textp = t->text.p.ptr = text;
H. Peter Anvin8571f062019-09-23 16:40:03 -0700408 } else {
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700409 textp = memcpy(t->text.a, text, len);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700410 nasm_free(text);
411 }
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700412 textp[len] = '\0';
H. Peter Anvin8571f062019-09-23 16:40:03 -0700413
414 return t;
415}
416
417/*
418 * Allocate a new buffer containing a copy of the text field
419 * of the token.
420 */
421static char *dup_text(const struct Token *t)
422{
423 size_t size = t->len + 1;
424 char *p = nasm_malloc(size);
425
426 return memcpy(p, tok_text(t), size);
427}
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000428
429/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800430 * Multi-line macro definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000431 * these, which is essentially a container to allow several linked
432 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700433 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000434 * Note that in this module, linked lists are treated as stacks
435 * wherever possible. For this reason, Lines are _pushed_ on to the
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800436 * `expansion' field in MMacro structures, so that the linked list,
437 * if walked, would give the macro lines in reverse order; this
438 * means that we can walk the list when expanding a macro, and thus
439 * push the lines on to the `expansion' field in _istk_ in reverse
440 * order (so that when popped back off they are in the right
441 * order). It may seem cockeyed, and it relies on my design having
442 * an even number of steps in, but it works...
443 *
444 * Some of these structures, rather than being actual lines, are
445 * markers delimiting the end of the expansion of a given macro.
446 * This is for use in the cycle-tracking and %rep-handling code.
447 * Such structures have `finishes' non-NULL, and `first' NULL. All
448 * others have `finishes' NULL, but `first' may still be NULL if
449 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000450 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000451struct Line {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800452 Line *next;
453 MMacro *finishes;
454 Token *first;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700455 struct src_location where; /* Where defined */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500456};
457
458/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000459 * To handle an arbitrary level of file inclusion, we maintain a
460 * stack (ie linked list) of these things.
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -0700461 *
462 * Note: when we issue a message for a continuation line, we want to
463 * issue it for the actual *start* of the continuation line. This means
464 * we need to remember how many lines to skip over for the next one.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000465 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000466struct Include {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800467 Include *next;
468 FILE *fp;
469 Cond *conds;
470 Line *expansion;
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -0700471 uint64_t nolist; /* Listing inhibit counter */
472 uint64_t noline; /* Line number update inhibit counter */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700473 struct mstk mstk;
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -0700474 struct src_location where; /* Filename and current line number */
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700475 int32_t lineinc; /* Increment given by %line */
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -0700476 int32_t lineskip; /* Accounting for passed continuation lines */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000477};
478
479/*
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700480 * File real name hash, so we don't have to re-search the include
481 * path for every pass (and potentially more than that if a file
482 * is used more than once.)
483 */
484struct hash_table FileHash;
485
486/*
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -0700487 * Counters to trap on insane macro recursion or processing.
488 * Note: for smacros these count *down*, for mmacros they count *up*.
489 */
490struct deadman {
491 int64_t total; /* Total number of macros/tokens */
492 int64_t levels; /* Descent depth across all macros */
493 bool triggered; /* Already triggered, no need for error msg */
494};
495
496static struct deadman smacro_deadman, mmacro_deadman;
497
498/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000499 * Conditional assembly: we maintain a separate stack of these for
500 * each level of file inclusion. (The only reason we keep the
501 * stacks separate is to ensure that a stray `%endif' in a file
502 * included from within the true branch of a `%if' won't terminate
503 * it and cause confusion: instead, rightly, it'll cause an error.)
504 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -0700505enum cond_state {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000506 /*
507 * These states are for use just after %if or %elif: IF_TRUE
508 * means the condition has evaluated to truth so we are
509 * currently emitting, whereas IF_FALSE means we are not
510 * currently emitting but will start doing so if a %else comes
511 * up. In these states, all directives are admissible: %elif,
512 * %else and %endif. (And of course %if.)
513 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800514 COND_IF_TRUE, COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000515 /*
516 * These states come up after a %else: ELSE_TRUE means we're
517 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
518 * any %elif or %else will cause an error.
519 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800520 COND_ELSE_TRUE, COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000521 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200522 * These states mean that we're not emitting now, and also that
523 * nothing until %endif will be emitted at all. COND_DONE is
524 * used when we've had our moment of emission
525 * and have now started seeing %elifs. COND_NEVER is used when
526 * the condition construct in question is contained within a
527 * non-emitting branch of a larger condition construct,
528 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000529 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800530 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000531};
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -0700532struct Cond {
533 Cond *next;
534 enum cond_state state;
535};
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800536#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000537
H. Peter Anvin70653092007-10-19 14:42:29 -0700538/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000539 * These defines are used as the possible return values for do_directive
540 */
541#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300542#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000543
Keith Kanios852f1ee2009-07-12 00:19:55 -0500544/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000545 * Condition codes. Note that we use c_ prefix not C_ because C_ is
546 * used in nasm.h for the "real" condition codes. At _this_ level,
547 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
548 * ones, so we need a different enum...
549 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700550static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000551 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
552 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000553 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000554};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700555enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000556 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
557 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 -0700558 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
559 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000560};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700561static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000562 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
563 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 +0000564 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000565};
566
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800567/*
568 * Directive names.
569 */
570/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
571static int is_condition(enum preproc_token arg)
572{
573 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
574}
575
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000576/* For TASM compatibility we need to be able to recognise TASM compatible
577 * conditional compilation directives. Using the NASM pre-processor does
578 * not work, so we look for them specifically from the following list and
579 * then jam in the equivalent NASM directive into the input stream.
580 */
581
H. Peter Anvine2c80182005-01-15 22:15:51 +0000582enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000583 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
584 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
585};
586
H. Peter Anvin476d2862007-10-02 22:04:15 -0700587static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000588 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
589 "ifndef", "include", "local"
590};
591
592static int StackSize = 4;
H. Peter Anvin6c8b2be2016-05-24 23:46:50 -0700593static const char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000594static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800595static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000596
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000597static Context *cstk;
598static Include *istk;
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300599static const struct strlist *ipath_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000600
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300601static struct strlist *deplist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000602
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300603static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000604
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800605static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700606static bool do_predef;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800607static enum preproc_mode pp_mode;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000608
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000609/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800610 * The current set of multi-line macros we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000611 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800612static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000613
614/*
615 * The current set of single-line macros we have defined.
616 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700617static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000618
619/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800620 * The multi-line macro we are currently defining, or the %rep
621 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000622 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800623static MMacro *defining;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000624
Charles Crayned4200be2008-07-12 16:42:33 -0700625static uint64_t nested_mac_count;
626static uint64_t nested_rep_count;
627
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000628/*
629 * The number of macro parameters to allocate space for at a time.
630 */
631#define PARAM_DELTA 16
632
633/*
H. Peter Anvinf7606612016-07-13 14:23:48 -0700634 * The standard macro set: defined in macros.c in a set of arrays.
635 * This gives our position in any macro set, while we are processing it.
636 * The stdmacset is an array of such macro sets.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000637 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700638static macros_t *stdmacpos;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700639static macros_t **stdmacnext;
640static macros_t *stdmacros[8];
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +0300641static macros_t *extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000642
643/*
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -0700644 * Map of which %use packages have been loaded
645 */
646static bool *use_loaded;
647
648/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000649 * Forward declarations.
650 */
H. Peter Anvinf7606612016-07-13 14:23:48 -0700651static void pp_add_stdmac(macros_t *macros);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000652static Token *expand_mmac_params(Token * tline);
653static Token *expand_smacro(Token * tline);
654static Token *expand_id(Token * tline);
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +0400655static Context *get_ctx(const char *name, const char **namep);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700656static Token *make_tok_num(Token *next, int64_t val);
657static Token *make_tok_qstr(Token *next, const char *str);
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -0700658static Token *make_tok_qstr_len(Token *next, const char *str, size_t len);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700659static Token *make_tok_char(Token *next, char op);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700660static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700661 const char *text, size_t txtlen);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700662static Token *new_Token_free(Token * next, enum pp_token_type type,
663 char *text, size_t txtlen);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700664static Token *dup_Token(Token *next, const Token *src);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700665static Token *new_White(Token *next);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700666static Token *delete_Token(Token *t);
667static Token *steal_Token(Token *dst, Token *src);
H. Peter Anvindd88aa92019-09-12 19:39:48 -0700668static const struct use_package *
H. Peter Anvin8571f062019-09-23 16:40:03 -0700669get_use_pkg(Token *t, const char *dname, const char **name);
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -0700670static void mark_smac_params(Token *tline, const SMacro *tmpl,
671 enum pp_token_type type);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000672
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700673/* Safe test for token type, false on x == NULL */
674static inline bool tok_type(const Token *x, enum pp_token_type t)
675{
676 return x && x->type == t;
677}
678
679/* Whitespace token? */
680static inline bool tok_white(const Token *x)
681{
682 return tok_type(x, TOK_WHITESPACE);
683}
684
685/* Skip past any whitespace */
686static inline Token *skip_white(Token *x)
687{
688 while (tok_white(x))
689 x = x->next;
690
691 return x;
692}
693
694/* Delete any whitespace */
695static Token *zap_white(Token *x)
696{
697 while (tok_white(x))
698 x = delete_Token(x);
699
700 return x;
701}
702
H. Peter Anvin8571f062019-09-23 16:40:03 -0700703/*
704 * Single special character tests. The use of & rather than && is intentional; it
705 * tells the compiler that it is safe to access text.a[1] unconditionally; hopefully
706 * a smart compiler should turn it into a 16-bit memory reference.
707 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700708static inline bool tok_is(const Token *x, char c)
709{
H. Peter Anvin8571f062019-09-23 16:40:03 -0700710 return x && ((x->text.a[0] == c) & !x->text.a[1]);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700711}
712
713/* True if any other kind of token that "c", but not NULL */
714static inline bool tok_isnt(const Token *x, char c)
715{
H. Peter Anvin8571f062019-09-23 16:40:03 -0700716 return x && !((x->text.a[0] == c) & !x->text.a[1]);
717}
718
719/*
720 * Unquote a token if it is a string, and set its type to
721 * TOK_INTERNAL_STRING.
722 */
723static const char *unquote_token(Token *t)
724{
725 if (t->type != TOK_STRING)
726 return tok_text(t);
727
728 t->type = TOK_INTERNAL_STRING;
729
730 if (t->len > INLINE_TEXT) {
731 char *p = t->text.p.ptr;
732
733 t->len = nasm_unquote(p, NULL);
734
735 if (t->len <= INLINE_TEXT) {
736 nasm_zero(t->text.a);
737 memcpy(t->text.a, p, t->len);
738 nasm_free(p);
739 return t->text.a;
740 } else {
741 return p;
742 }
743 } else {
744 t->len = nasm_unquote(t->text.a, NULL);
745 return t->text.a;
746 }
747}
748
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700749/*
750 * Same as unquote_token(), but error out if the resulting string
751 * contains unacceptable control characters.
752 */
H. Peter Anvin8571f062019-09-23 16:40:03 -0700753static const char *unquote_token_cstr(Token *t)
754{
755 if (t->type != TOK_STRING)
756 return tok_text(t);
757
758 t->type = TOK_INTERNAL_STRING;
759
760 if (t->len > INLINE_TEXT) {
761 char *p = t->text.p.ptr;
762
763 t->len = nasm_unquote_cstr(p, NULL);
764
765 if (t->len <= INLINE_TEXT) {
766 nasm_zero(t->text.a);
767 memcpy(t->text.a, p, t->len);
768 nasm_free(p);
769 return t->text.a;
770 } else {
771 return p;
772 }
773 } else {
774 t->len = nasm_unquote_cstr(t->text.a, NULL);
775 return t->text.a;
776 }
777}
778
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700779/*
780 * Convert a TOK_INTERNAL_STRING token to a quoted
781 * TOK_STRING tokens.
782 */
783static Token *quote_any_token(Token *t);
H. Peter Anvin (Intel)b8777082020-07-01 20:49:04 -0700784static inline unused_func
H. Peter Anvin (Intel)65ab3ab2020-06-30 10:14:21 -0700785Token *quote_token(Token *t)
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700786{
787 if (likely(!tok_is(t, TOK_INTERNAL_STRING)))
788 return t;
789
790 return quote_any_token(t);
791}
792
793/*
794 * Convert *any* kind of token to a quoted
795 * TOK_STRING token.
796 */
797static Token *quote_any_token(Token *t)
H. Peter Anvin8571f062019-09-23 16:40:03 -0700798{
799 size_t len;
800 char *p;
801
802 p = nasm_quote(tok_text(t), &len);
803 t->type = TOK_STRING;
804 return set_text_free(t, p, len);
805}
806
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400807/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700808 * In-place reverse a list of tokens.
809 */
810static Token *reverse_tokens(Token *t)
811{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800812 Token *prev = NULL;
813 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700814
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800815 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400816 next = t->next;
817 t->next = prev;
818 prev = t;
819 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800820 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700821
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800822 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700823}
824
825/*
H. Peter Anvin8571f062019-09-23 16:40:03 -0700826 * getenv() variant operating on an input token
827 */
828static const char *pp_getenv(const Token *t, bool warn)
829{
830 const char *txt = tok_text(t);
831 const char *v;
832 char *buf = NULL;
833 bool is_string = false;
834
835 if (!t)
836 return NULL;
837
838 switch (t->type) {
839 case TOK_ENVIRON:
840 txt += 2; /* Skip leading %! */
841 is_string = nasm_isquote(*txt);
842 break;
843
844 case TOK_STRING:
845 is_string = true;
846 break;
847
848 case TOK_INTERNAL_STRING:
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700849 case TOK_NAKED_STRING:
H. Peter Anvin8571f062019-09-23 16:40:03 -0700850 case TOK_ID:
851 is_string = false;
852 break;
853
854 default:
855 return NULL;
856 }
857
858 if (is_string) {
859 buf = nasm_strdup(txt);
860 nasm_unquote_cstr(buf, NULL);
861 txt = buf;
862 }
863
864 v = getenv(txt);
865 if (warn && !v) {
866 /*!
867 *!environment [on] nonexistent environment variable
868 *! warns if a nonexistent environment variable
869 *! is accessed using the \c{%!} preprocessor
870 *! construct (see \k{getenv}.) Such environment
871 *! variables are treated as empty (with this
872 *! warning issued) starting in NASM 2.15;
873 *! earlier versions of NASM would treat this as
874 *! an error.
875 */
876 nasm_warn(WARN_ENVIRONMENT, "nonexistent environment variable `%s'", txt);
877 v = "";
878 }
879
880 if (buf)
881 nasm_free(buf);
882
883 return v;
884}
885
886/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300887 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000888 * front of them. We do it here because I could not find any other
889 * place to do it for the moment, and it is a hack (ideally it would
890 * be nice to be able to use the NASM pre-processor to do it).
891 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000892static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000893{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000894 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400895 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000896
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400897 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000898
899 /* Binary search for the directive name */
900 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400901 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400902 q = nasm_skip_word(p);
903 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000904 if (len) {
905 oldchar = p[len];
906 p[len] = 0;
907 while (j - i > 1) {
908 k = (j + i) / 2;
909 m = nasm_stricmp(p, tasm_directives[k]);
910 if (m == 0) {
911 /* We have found a directive, so jam a % in front of it
912 * so that NASM will then recognise it as one if it's own.
913 */
914 p[len] = oldchar;
915 len = strlen(p);
916 oldline = line;
917 line = nasm_malloc(len + 2);
918 line[0] = '%';
919 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700920 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300921 * NASM does not recognise IFDIFI, so we convert
922 * it to %if 0. This is not used in NASM
923 * compatible code, but does need to parse for the
924 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000925 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700926 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000927 } else {
928 memcpy(line + 1, p, len + 1);
929 }
930 nasm_free(oldline);
931 return line;
932 } else if (m < 0) {
933 j = k;
934 } else
935 i = k;
936 }
937 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000938 }
939 return line;
940}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000941
H. Peter Anvin76690a12002-04-30 20:52:49 +0000942/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000943 * The pre-preprocessing stage... This function translates line
944 * number indications as they emerge from GNU cpp (`# lineno "file"
945 * flags') into NASM preprocessor line number indications (`%line
946 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000947 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000948static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000949{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000950 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000951 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000952
H. Peter Anvine2c80182005-01-15 22:15:51 +0000953 if (line[0] == '#' && line[1] == ' ') {
954 oldline = line;
955 fname = oldline + 2;
956 lineno = atoi(fname);
957 fname += strspn(fname, "0123456789 ");
958 if (*fname == '"')
959 fname++;
960 fnlen = strcspn(fname, "\"");
961 line = nasm_malloc(20 + fnlen);
962 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
963 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000964 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000965 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000966 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000967 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000968}
969
970/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000971 * Free a linked list of tokens.
972 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000973static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000974{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400975 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000976 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000977}
978
979/*
980 * Free a linked list of lines.
981 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000982static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000983{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400984 Line *l, *tmp;
985 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000986 free_tlist(l->first);
987 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000988 }
989}
990
991/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700992 * Free an array of linked lists of tokens
993 */
994static void free_tlist_array(Token **array, size_t nlists)
995{
996 Token **listp = array;
997
998 while (nlists--)
999 free_tlist(*listp++);
1000
1001 nasm_free(array);
1002}
1003
1004/*
1005 * Duplicate a linked list of tokens.
1006 */
1007static Token *dup_tlist(const Token *list, Token ***tailp)
1008{
1009 Token *newlist = NULL;
1010 Token **tailpp = &newlist;
1011 const Token *t;
1012
1013 list_for_each(t, list) {
1014 Token *nt;
1015 *tailpp = nt = dup_Token(NULL, t);
1016 tailpp = &nt->next;
1017 }
1018
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001019 if (tailp) {
1020 **tailp = newlist;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07001021 *tailp = tailpp;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001022 }
1023
1024 return newlist;
1025}
1026
1027/*
1028 * Duplicate a linked list of tokens with a maximum count
1029 */
1030static Token *dup_tlistn(const Token *list, size_t cnt, Token ***tailp)
1031{
1032 Token *newlist = NULL;
1033 Token **tailpp = &newlist;
1034 const Token *t;
1035
1036 list_for_each(t, list) {
1037 Token *nt;
1038 if (!cnt--)
1039 break;
1040 *tailpp = nt = dup_Token(NULL, t);
1041 tailpp = &nt->next;
1042 }
1043
1044 if (tailp) {
1045 **tailp = newlist;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07001046 if (newlist)
1047 *tailp = tailpp;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001048 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07001049
1050 return newlist;
1051}
1052
1053/*
1054 * Duplicate a linked list of tokens in reverse order
1055 */
1056static Token *dup_tlist_reverse(const Token *list, Token *tail)
1057{
1058 const Token *t;
1059
1060 list_for_each(t, list)
1061 tail = dup_Token(tail, t);
1062
1063 return tail;
1064}
1065
1066/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001067 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +00001068 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001069static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001070{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001071 nasm_free(m->name);
1072 free_tlist(m->dlist);
1073 nasm_free(m->defaults);
1074 free_llist(m->expansion);
1075 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001076}
1077
1078/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001079 * Clear or free an SMacro
H. Peter Anvin8b262472019-02-26 14:00:54 -08001080 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001081static void free_smacro_members(SMacro *s)
H. Peter Anvin8b262472019-02-26 14:00:54 -08001082{
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07001083 if (s->params) {
1084 int i;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001085 for (i = 0; i < s->nparam; i++) {
1086 if (s->params[i].name.len > INLINE_TEXT)
1087 nasm_free(s->params[i].name.text.p.ptr);
1088 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07001089 nasm_free(s->params);
1090 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08001091 nasm_free(s->name);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001092 free_tlist(s->expansion);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001093}
1094
1095static void clear_smacro(SMacro *s)
1096{
1097 free_smacro_members(s);
1098 /* Wipe everything except the next pointer */
1099 memset(&s->next + 1, 0, sizeof *s - sizeof s->next);
1100}
1101
1102/*
1103 * Free an SMacro
1104 */
1105static void free_smacro(SMacro *s)
1106{
1107 free_smacro_members(s);
1108 nasm_free(s);
H. Peter Anvin8b262472019-02-26 14:00:54 -08001109}
1110
1111/*
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07001112 * Free all currently defined macros, and free the hash tables if empty
H. Peter Anvin97a23472007-09-16 17:57:25 -07001113 */
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07001114enum clear_what {
1115 CLEAR_NONE = 0,
1116 CLEAR_DEFINE = 1, /* Clear smacros */
1117 CLEAR_DEFALIAS = 2, /* Clear smacro aliases */
1118 CLEAR_ALLDEFINE = CLEAR_DEFINE|CLEAR_DEFALIAS,
1119 CLEAR_MMACRO = 4,
1120 CLEAR_ALL = CLEAR_ALLDEFINE|CLEAR_MMACRO
1121};
1122
1123static void clear_smacro_table(struct hash_table *smt, enum clear_what what)
H. Peter Anvin97a23472007-09-16 17:57:25 -07001124{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001125 struct hash_iterator it;
1126 const struct hash_node *np;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07001127 bool empty = true;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001128
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07001129 /*
1130 * Walk the hash table and clear out anything we don't want
1131 */
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001132 hash_for_each(smt, it, np) {
1133 SMacro *tmp;
1134 SMacro *s = np->data;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07001135 SMacro **head = (SMacro **)&np->data;
1136
1137 list_for_each_safe(s, tmp, s) {
1138 if (what & ((enum clear_what)s->alias + 1)) {
1139 *head = s->next;
1140 free_smacro(s);
1141 } else {
1142 empty = false;
1143 }
1144 }
H. Peter Anvin97a23472007-09-16 17:57:25 -07001145 }
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07001146
1147 /*
1148 * Free the hash table and keys if and only if it is now empty.
1149 * Note: we cannot free keys even for an empty list above, as that
1150 * mucks up the hash algorithm.
1151 */
1152 if (empty)
1153 hash_free_all(smt, true);
1154}
1155
1156static void free_smacro_table(struct hash_table *smt)
1157{
1158 clear_smacro_table(smt, CLEAR_ALLDEFINE);
H. Peter Anvin072771e2008-05-22 13:17:51 -07001159}
1160
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001161static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -07001162{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001163 struct hash_iterator it;
1164 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001165
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001166 hash_for_each(mmt, it, np) {
1167 MMacro *tmp;
1168 MMacro *m = np->data;
1169 nasm_free((void *)np->key);
1170 list_for_each_safe(m, tmp, m)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001171 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001172 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001173 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -07001174}
1175
1176static void free_macros(void)
1177{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001178 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001179 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001180}
1181
1182/*
1183 * Initialize the hash tables
1184 */
1185static void init_macros(void)
1186{
H. Peter Anvin97a23472007-09-16 17:57:25 -07001187}
1188
1189/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001190 * Pop the context stack.
1191 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001192static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001193{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001194 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001195
1196 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001197 free_smacro_table(&c->localmac);
H. Peter Anvin8571f062019-09-23 16:40:03 -07001198 nasm_free((char *)c->name);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001199 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001200}
1201
H. Peter Anvin072771e2008-05-22 13:17:51 -07001202/*
1203 * Search for a key in the hash index; adding it if necessary
1204 * (in which case we initialize the data pointer to NULL.)
1205 */
1206static void **
1207hash_findi_add(struct hash_table *hash, const char *str)
1208{
1209 struct hash_insert hi;
1210 void **r;
1211 char *strx;
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001212 size_t l = strlen(str) + 1;
H. Peter Anvin072771e2008-05-22 13:17:51 -07001213
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001214 r = hash_findib(hash, str, l, &hi);
H. Peter Anvin072771e2008-05-22 13:17:51 -07001215 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001216 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -07001217
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001218 strx = nasm_malloc(l); /* Use a more efficient allocator here? */
1219 memcpy(strx, str, l);
H. Peter Anvin072771e2008-05-22 13:17:51 -07001220 return hash_add(&hi, strx, NULL);
1221}
1222
1223/*
1224 * Like hash_findi, but returns the data element rather than a pointer
1225 * to it. Used only when not adding a new element, hence no third
1226 * argument.
1227 */
1228static void *
1229hash_findix(struct hash_table *hash, const char *str)
1230{
1231 void **p;
1232
1233 p = hash_findi(hash, str, NULL);
1234 return p ? *p : NULL;
1235}
1236
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001237/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001238 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001239 * if there no more left -- return NULL
1240 */
1241static char *line_from_stdmac(void)
1242{
1243 unsigned char c;
1244 const unsigned char *p = stdmacpos;
1245 char *line, *q;
1246 size_t len = 0;
1247
1248 if (!stdmacpos)
1249 return NULL;
1250
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -07001251 /*
1252 * 32-126 is ASCII, 127 is end of line, 128-31 are directives
1253 * (allowed to wrap around) corresponding to PP_* tokens 0-159.
1254 */
1255 while ((c = *p++) != 127) {
1256 uint8_t ndir = c - 128;
1257 if (ndir < 256-96)
1258 len += pp_directives_len[ndir] + 1;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001259 else
1260 len++;
1261 }
1262
1263 line = nasm_malloc(len + 1);
1264 q = line;
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -07001265
1266 while ((c = *stdmacpos++) != 127) {
1267 uint8_t ndir = c - 128;
1268 if (ndir < 256-96) {
1269 memcpy(q, pp_directives[ndir], pp_directives_len[ndir]);
1270 q += pp_directives_len[ndir];
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001271 *q++ = ' ';
1272 } else {
1273 *q++ = c;
1274 }
1275 }
1276 stdmacpos = p;
1277 *q = '\0';
1278
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -07001279 if (*stdmacpos == 127) {
H. Peter Anvinf7606612016-07-13 14:23:48 -07001280 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001281 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -07001282 if (*stdmacnext) {
1283 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001284 } else if (do_predef) {
1285 Line *pd, *l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001286
1287 /*
1288 * Nasty hack: here we push the contents of
1289 * `predef' on to the top-level expansion stack,
1290 * since this is the most convenient way to
1291 * implement the pre-include and pre-define
1292 * features.
1293 */
1294 list_for_each(pd, predef) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07001295 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001296 l->next = istk->expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07001297 l->first = dup_tlist(pd->first, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001298 l->finishes = NULL;
1299
1300 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001301 }
1302 do_predef = false;
1303 }
1304 }
1305
1306 return line;
1307}
1308
H. Peter Anvin6686de22019-08-10 05:33:14 -07001309/*
1310 * Read a line from a file. Return NULL on end of file.
1311 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001312static char *line_from_file(FILE *f)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001313{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001314 int c;
1315 unsigned int size, next;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001316 const unsigned int delta = 512;
1317 const unsigned int pad = 8;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001318 bool cont = false;
1319 char *buffer, *p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001320
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07001321 istk->where.lineno += istk->lineskip + istk->lineinc;
1322 src_set_linnum(istk->where.lineno);
1323 istk->lineskip = 0;
1324
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001325 size = delta;
1326 p = buffer = nasm_malloc(size);
1327
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001328 do {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001329 c = fgetc(f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001330
1331 switch (c) {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001332 case EOF:
1333 if (p == buffer) {
1334 nasm_free(buffer);
1335 return NULL;
1336 }
1337 c = 0;
1338 break;
1339
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001340 case '\r':
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001341 next = fgetc(f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001342 if (next != '\n')
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001343 ungetc(next, f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001344 if (cont) {
1345 cont = false;
1346 continue;
1347 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001348 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001349 break;
1350
1351 case '\n':
1352 if (cont) {
1353 cont = false;
1354 continue;
1355 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001356 c = 0;
1357 break;
1358
1359 case 032: /* ^Z = legacy MS-DOS end of file mark */
1360 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001361 break;
1362
1363 case '\\':
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001364 next = fgetc(f);
1365 ungetc(next, f);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +04001366 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001367 cont = true;
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07001368 istk->lineskip += istk->lineinc;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001369 continue;
1370 }
1371 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001372 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001373
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001374 if (p >= (buffer + size - pad)) {
1375 buffer = nasm_realloc(buffer, size + delta);
1376 p = buffer + size - pad;
1377 size += delta;
1378 }
1379
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001380 *p++ = c;
1381 } while (c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001382
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001383 return buffer;
1384}
1385
1386/*
H. Peter Anvin6686de22019-08-10 05:33:14 -07001387 * Common read routine regardless of source
1388 */
1389static char *read_line(void)
1390{
1391 char *line;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001392 FILE *f = istk->fp;
H. Peter Anvin6686de22019-08-10 05:33:14 -07001393
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001394 if (f)
1395 line = line_from_file(f);
H. Peter Anvin6686de22019-08-10 05:33:14 -07001396 else
1397 line = line_from_stdmac();
1398
1399 if (!line)
1400 return NULL;
1401
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07001402 if (!istk->nolist)
1403 lfmt->line(LIST_READ, istk->where.lineno, line);
H. Peter Anvin6686de22019-08-10 05:33:14 -07001404
1405 return line;
1406}
1407
1408/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00001409 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001410 * don't need to parse the value out of e.g. numeric tokens: we
1411 * simply split one string into many.
1412 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07001413static Token *tokenize(const char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001414{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001415 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001416 Token *list = NULL;
1417 Token *t, **tail = &list;
1418
H. Peter Anvine2c80182005-01-15 22:15:51 +00001419 while (*line) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001420 const char *p = line;
1421 const char *ep = NULL; /* End of token, for trimming the end */
1422 size_t toklen;
1423 char firstchar = *p; /* Can be used to override the first char */
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001424
H. Peter Anvine2c80182005-01-15 22:15:51 +00001425 if (*p == '%') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001426 /*
1427 * Preprocessor construct; find the end of the token.
1428 * Classification is handled later, because %{...} can be
1429 * used to create any preprocessor token.
1430 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001431 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001432 if (*p == '+' && !nasm_isdigit(p[1])) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001433 /* Paste token */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001434 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001435 } else if (nasm_isdigit(*p) ||
1436 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001437 do {
1438 p++;
1439 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001440 while (nasm_isdigit(*p));
H. Peter Anvin8571f062019-09-23 16:40:03 -07001441 } else if (*p == '{' || *p == '[') {
1442 /* %{...} or %[...] */
1443 char firstchar = *p;
1444 char endchar = *p + 2; /* } or ] */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001445 int lvl = 1;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001446 line += (*p++ == '{'); /* Skip { but not [ (yet) */
1447 while (lvl) {
1448 if (*p == firstchar) {
1449 lvl++;
1450 } else if (*p == endchar) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001451 lvl--;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001452 } else if (nasm_isquote(*p)) {
1453 p = nasm_skip_string(p);
1454 }
1455
1456 /*
1457 * *p can have been advanced to a null character by
1458 * nasm_skip_string()
1459 */
1460 if (!*p) {
1461 nasm_warn(WARN_OTHER, "unterminated %%%c construct",
1462 firstchar);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001463 break;
1464 }
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001465 p++;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001466 }
1467 ep = lvl ? p : p-1; /* Terminal character not part of token */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001468 } else if (*p == '?') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001469 /* %? or %?? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001470 p++;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001471 if (*p == '?')
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001472 p++;
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001473 } else if (*p == '!') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001474 /* Environment variable reference */
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001475 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001476 if (nasm_isidchar(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001477 do {
1478 p++;
1479 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001480 while (nasm_isidchar(*p));
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001481 } else if (nasm_isquote(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001482 p = nasm_skip_string(p);
1483 if (*p)
1484 p++;
1485 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001486 nasm_nonfatalf(ERR_PASS1, "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001487 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001488 /* %! without anything else... */
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001489 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07001490 } else if (*p == ',') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001491 /* Conditional comma */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07001492 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001493 } else if (nasm_isidchar(*p) ||
H. Peter Anvin8571f062019-09-23 16:40:03 -07001494 ((*p == '%' || *p == '$') && nasm_isidchar(p[1]))) {
1495 /* Identifier or some sort */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001496 do {
1497 p++;
1498 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001499 while (nasm_isidchar(*p));
H. Peter Anvin8571f062019-09-23 16:40:03 -07001500 } else if (*p == '%') {
1501 /* %% operator */
1502 p++;
1503 }
1504
1505 if (!ep)
1506 ep = p;
1507 toklen = ep - line;
1508
1509 /* Classify here, to handle %{...} correctly */
1510 if (toklen < 2) {
1511 type = TOK_OTHER; /* % operator */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001512 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001513 char c0 = line[1];
1514
1515 switch (c0) {
1516 case '+':
1517 type = (toklen == 2) ? TOK_PASTE : TOK_MMACRO_PARAM;
1518 break;
1519
1520 case '-':
1521 type = TOK_MMACRO_PARAM;
1522 break;
1523
1524 case '?':
1525 if (toklen == 2)
1526 type = TOK_PREPROC_Q;
1527 else if (toklen == 3 && line[2] == '?')
1528 type = TOK_PREPROC_QQ;
1529 else
1530 type = TOK_PREPROC_ID;
1531 break;
1532
1533 case '!':
1534 type = (toklen == 2) ? TOK_OTHER : TOK_ENVIRON;
1535 break;
1536
1537 case '%':
1538 type = (toklen == 2) ? TOK_OTHER : TOK_LOCAL_SYMBOL;
1539 break;
1540
1541 case '$':
1542 type = (toklen == 2) ? TOK_OTHER : TOK_LOCAL_MACRO;
1543 break;
1544
1545 case '[':
1546 line += 2; /* Skip %[ */
1547 firstchar = *line; /* Don't clobber */
1548 toklen -= 2;
1549 type = TOK_INDIRECT;
1550 break;
1551
1552 case ',':
1553 type = (toklen == 2) ? TOK_COND_COMMA : TOK_PREPROC_ID;
1554 break;
1555
1556 case '\'':
1557 case '\"':
1558 case '`':
1559 /* %{'string'} */
1560 type = TOK_PREPROC_ID;
1561 break;
1562
1563 case ':':
1564 type = TOK_MMACRO_PARAM; /* %{:..} */
1565 break;
1566
1567 default:
1568 if (nasm_isdigit(c0))
1569 type = TOK_MMACRO_PARAM;
1570 else if (nasm_isidchar(c0) || toklen > 2)
1571 type = TOK_PREPROC_ID;
1572 else
1573 type = TOK_OTHER;
1574 break;
1575 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001576 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001577 } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001578 /*
1579 * An identifier. This includes the ? operator, which is
1580 * treated as a keyword, not as a special character
1581 * operator
1582 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001583 type = TOK_ID;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001584 while (nasm_isidchar(*++p))
1585 ;
1586 } else if (nasm_isquote(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001587 /*
1588 * A string token.
1589 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001590 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001591 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001592
H. Peter Anvine2c80182005-01-15 22:15:51 +00001593 if (*p) {
1594 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001595 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001596 nasm_warn(WARN_OTHER, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001597 /* Handling unterminated strings by UNV */
1598 /* type = -1; */
1599 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001600 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001601 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001602 p += 2;
H. Peter Anvin13506202018-11-28 14:55:58 -08001603 } else if (nasm_isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001604 bool is_hex = false;
1605 bool is_float = false;
1606 bool has_e = false;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001607 char c;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001608
H. Peter Anvine2c80182005-01-15 22:15:51 +00001609 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001610 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001611 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001612
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001613 if (*p == '$') {
1614 p++;
1615 is_hex = true;
1616 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001617
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001618 for (;;) {
1619 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001620
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001621 if (!is_hex && (c == 'e' || c == 'E')) {
1622 has_e = true;
1623 if (*p == '+' || *p == '-') {
1624 /*
1625 * e can only be followed by +/- if it is either a
1626 * prefixed hex number or a floating-point number
1627 */
1628 p++;
1629 is_float = true;
1630 }
1631 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1632 is_hex = true;
1633 } else if (c == 'P' || c == 'p') {
1634 is_float = true;
1635 if (*p == '+' || *p == '-')
1636 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001637 } else if (nasm_isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001638 ; /* just advance */
1639 else if (c == '.') {
1640 /*
1641 * we need to deal with consequences of the legacy
1642 * parser, like "1.nolist" being two tokens
1643 * (TOK_NUMBER, TOK_ID) here; at least give it
1644 * a shot for now. In the future, we probably need
1645 * a flex-based scanner with proper pattern matching
1646 * to do it as well as it can be done. Nothing in
1647 * the world is going to help the person who wants
1648 * 0x123.p16 interpreted as two tokens, though.
1649 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07001650 const char *r = p;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001651 while (*r == '_')
1652 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001653
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001654 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1655 (!is_hex && (*r == 'e' || *r == 'E')) ||
1656 (*r == 'p' || *r == 'P')) {
1657 p = r;
1658 is_float = true;
1659 } else
1660 break; /* Terminate the token */
1661 } else
1662 break;
1663 }
1664 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001665
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001666 if (p == line+1 && *line == '$') {
1667 type = TOK_OTHER; /* TOKEN_HERE */
1668 } else {
1669 if (has_e && !is_hex) {
1670 /* 1e13 is floating-point, but 1e13h is not */
1671 is_float = true;
1672 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001673
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001674 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1675 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001676 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001677 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001678 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001679 /*
1680 * Whitespace just before end-of-line is discarded by
1681 * pretending it's a comment; whitespace just before a
1682 * comment gets lumped into the comment.
1683 */
1684 if (!*p || *p == ';') {
1685 type = TOK_COMMENT;
1686 while (*p)
1687 p++;
1688 }
1689 } else if (*p == ';') {
1690 type = TOK_COMMENT;
1691 while (*p)
1692 p++;
1693 } else {
1694 /*
1695 * Anything else is an operator of some kind. We check
1696 * for all the double-character operators (>>, <<, //,
H. Peter Anvin8571f062019-09-23 16:40:03 -07001697 * %%, <=, >=, ==, !=, <>, &&, ||, ^^) and the triple-
1698 * character operators (<<<, >>>, <=>) but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001699 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001700 */
1701 type = TOK_OTHER;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001702 switch (*p++) {
1703 case '>':
1704 if (*p == '>') {
1705 p++;
1706 if (*p == '>')
1707 p++;
H. Peter Anvind03a6c82019-10-07 21:29:05 -07001708 } else if (*p == '=') {
1709 p++;
1710 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07001711 break;
1712
1713 case '<':
1714 if (*p == '<') {
1715 p++;
1716 if (*p == '<')
1717 p++;
1718 } else if (*p == '=') {
1719 p++;
1720 if (*p == '>')
1721 p++;
1722 } else if (*p == '>') {
1723 p++;
1724 }
1725 break;
1726
1727 case '!':
1728 if (*p == '=')
1729 p++;
1730 break;
1731
1732 case '/':
1733 case '=':
1734 case '&':
1735 case '|':
1736 case '^':
1737 /* These operators can be doubled but nothing else */
1738 if (*p == p[-1])
1739 p++;
1740 break;
1741
1742 default:
1743 break;
1744 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001745 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001746
H. Peter Anvin8571f062019-09-23 16:40:03 -07001747 if (type == TOK_WHITESPACE) {
1748 *tail = t = new_White(NULL);
1749 tail = &t->next;
1750 } else if (type != TOK_COMMENT) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001751 if (!ep)
1752 ep = p;
1753 *tail = t = new_Token(NULL, type, line, ep - line);
H. Peter Anvin8571f062019-09-23 16:40:03 -07001754 *tok_text_buf(t) = firstchar; /* E.g. %{foo} -> {foo -> %foo */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001755 tail = &t->next;
1756 }
1757 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001758 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001759 return list;
1760}
1761
H. Peter Anvin8571f062019-09-23 16:40:03 -07001762/*
1763 * Tokens are allocated in blocks to improve speed. Set the blocksize
1764 * to 0 to use regular nasm_malloc(); this is useful for debugging.
1765 *
1766 * alloc_Token() returns a zero-initialized token structure.
1767 */
1768#define TOKEN_BLOCKSIZE 4096
1769
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001770#if TOKEN_BLOCKSIZE
H. Peter Anvin8571f062019-09-23 16:40:03 -07001771
1772static Token *freeTokens = NULL;
1773static Token *tokenblocks = NULL;
1774
1775static Token *alloc_Token(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001776{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001777 Token *t = freeTokens;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001778
H. Peter Anvin8571f062019-09-23 16:40:03 -07001779 if (unlikely(!t)) {
1780 Token *block;
1781 size_t i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001782
H. Peter Anvin8571f062019-09-23 16:40:03 -07001783 nasm_newn(block, TOKEN_BLOCKSIZE);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001784
H. Peter Anvin8571f062019-09-23 16:40:03 -07001785 /*
1786 * The first entry in each array are a linked list of
1787 * block allocations and is not used for data.
1788 */
1789 block[0].next = tokenblocks;
1790 block[0].type = TOK_BLOCK;
1791 tokenblocks = block;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001792
H. Peter Anvin8571f062019-09-23 16:40:03 -07001793 /*
1794 * Add the rest to the free list
1795 */
1796 for (i = 2; i < TOKEN_BLOCKSIZE - 1; i++)
1797 block[i].next = &block[i+1];
1798
1799 freeTokens = &block[2];
1800
1801 /*
1802 * Return the topmost usable token
1803 */
1804 return &block[1];
1805 }
1806
1807 freeTokens = t->next;
1808 t->next = NULL;
1809 return t;
H. Peter Anvince616072002-04-30 21:02:23 +00001810}
1811
H. Peter Anvin8571f062019-09-23 16:40:03 -07001812static Token *delete_Token(Token *t)
1813{
1814 Token *next = t->next;
1815
1816 nasm_zero(*t);
1817 t->next = freeTokens;
1818 freeTokens = t;
1819
1820 return next;
1821}
1822
H. Peter Anvine2c80182005-01-15 22:15:51 +00001823static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001824{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001825 Token *block, *blocktmp;
H. Peter Anvince616072002-04-30 21:02:23 +00001826
H. Peter Anvin8571f062019-09-23 16:40:03 -07001827 list_for_each_safe(block, blocktmp, tokenblocks)
1828 nasm_free(block);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001829
H. Peter Anvin8571f062019-09-23 16:40:03 -07001830 freeTokens = tokenblocks = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001831}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001832
H. Peter Anvin8571f062019-09-23 16:40:03 -07001833#else
1834
1835static inline Token *alloc_Token(void)
1836{
1837 Token *t;
1838 nasm_new(*t);
1839 return t;
1840}
1841
1842static Token *delete_Token(Token *t)
1843{
1844 Token *next = t->next;
1845 nasm_free(t);
1846 return next;
1847}
1848
1849static inline void delete_Blocks(void)
1850{
1851 /* Nothing to do */
1852}
1853
1854#endif
1855
H. Peter Anvin734b1882002-04-30 21:01:08 +00001856/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001857 * this function creates a new Token and passes a pointer to it
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001858 * back to the caller. It sets the type, text, and next pointer elements.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001859 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001860static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001861 const char *text, size_t txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001862{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001863 Token *t = alloc_Token();
1864 char *textp;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001865
H. Peter Anvin734b1882002-04-30 21:01:08 +00001866 t->next = next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001867 t->type = type;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001868 if (type == TOK_WHITESPACE) {
1869 t->len = 1;
1870 t->text.a[0] = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001871 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001872 if (text && text[0] && !txtlen)
1873 txtlen = tok_strlen(text);
1874
1875 t->len = tok_check_len(txtlen);
1876
1877 if (text) {
1878 textp = (txtlen > INLINE_TEXT)
1879 ? (t->text.p.ptr = nasm_malloc(txtlen+1)) : t->text.a;
1880 memcpy(textp, text, txtlen);
1881 textp[txtlen] = '\0'; /* In case we needed malloc() */
1882 } else {
1883 /*
1884 * Allocate a buffer but do not fill it. The caller
1885 * can fill in text, but must not change the length.
1886 * The filled in text must be exactly txtlen once
1887 * the buffer is filled and before the token is added
1888 * to any line lists.
1889 */
1890 if (txtlen > INLINE_TEXT)
1891 t->text.p.ptr = nasm_zalloc(txtlen+1);
1892 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001893 }
1894 return t;
1895}
1896
H. Peter Anvin8571f062019-09-23 16:40:03 -07001897/*
1898 * Same as new_Token(), but text belongs to the new token and is
1899 * either taken over or freed. This function MUST be called
1900 * with valid txt and txtlen, unlike new_Token().
1901 */
1902static Token *new_Token_free(Token * next, enum pp_token_type type,
1903 char *text, size_t txtlen)
1904{
1905 Token *t = alloc_Token();
1906
1907 t->next = next;
1908 t->type = type;
1909 t->len = tok_check_len(txtlen);
1910
1911 if (txtlen <= INLINE_TEXT) {
1912 memcpy(t->text.a, text, txtlen);
1913 free(text);
1914 } else {
1915 t->text.p.ptr = text;
1916 }
1917
1918 return t;
1919}
1920
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001921static Token *dup_Token(Token *next, const Token *src)
1922{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001923 Token *t = alloc_Token();
1924
1925 memcpy(t, src, sizeof *src);
1926 t->next = next;
1927
1928 if (t->len > INLINE_TEXT) {
1929 t->text.p.ptr = nasm_malloc(t->len + 1);
1930 memcpy(t->text.p.ptr, src->text.p.ptr, t->len+1);
1931 }
1932
1933 return t;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001934}
1935
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001936static Token *new_White(Token *next)
1937{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001938 Token *t = alloc_Token();
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001939
H. Peter Anvin8571f062019-09-23 16:40:03 -07001940 t->next = next;
1941 t->type = TOK_WHITESPACE;
1942 t->len = 1;
1943 t->text.a[0] = ' ';
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001944
H. Peter Anvin8571f062019-09-23 16:40:03 -07001945 return t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001946}
1947
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001948/*
H. Peter Anvin8571f062019-09-23 16:40:03 -07001949 * This *transfers* the content from one token to another, leaving the
1950 * next pointer of the latter intact. Unlike dup_Token(), the old
1951 * token is destroyed, except for its next pointer, and the text
1952 * pointer allocation, if any, is simply transferred.
1953 */
1954static Token *steal_Token(Token *dst, Token *src)
1955{
1956 /* Overwrite everything except the next pointers */
1957 memcpy((char *)dst + sizeof(Token *), (char *)src + sizeof(Token *),
1958 sizeof(Token) - sizeof(Token *));
1959
1960 /* Clear the donor token */
1961 memset((char *)src + sizeof(Token *), 0, sizeof(Token) - sizeof(Token *));
1962
1963 return dst;
1964}
1965
1966/*
1967 * Convert a line of tokens back into text. This modifies the list
1968 * by expanding environment variables.
1969 *
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001970 * If expand_locals is not zero, identifiers of the form "%$*xxx"
H. Peter Anvin8571f062019-09-23 16:40:03 -07001971 * are also transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001972 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001973static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001974{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001975 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001976 char *line, *p;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001977 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001978
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001979 list_for_each(t, tlist) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001980 switch (t->type) {
1981 case TOK_ENVIRON:
1982 {
1983 const char *v = pp_getenv(t, true);
1984 set_text(t, v, tok_strlen(v));
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07001985 t->type = TOK_NAKED_STRING;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001986 break;
1987 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001988
H. Peter Anvin8571f062019-09-23 16:40:03 -07001989 case TOK_LOCAL_MACRO:
1990 case TOK_LOCAL_SYMBOL:
1991 if (expand_locals) {
1992 const char *q;
1993 char *p;
1994 Context *ctx = get_ctx(tok_text(t), &q);
1995 if (ctx) {
1996 p = nasm_asprintf("..@%"PRIu64".%s", ctx->number, q);
1997 set_text_free(t, p, nasm_last_string_len());
1998 t->type = TOK_ID;
1999 }
2000 }
2001 break;
H. Peter Anvin077fb932010-07-20 14:56:30 -07002002
H. Peter Anvin8571f062019-09-23 16:40:03 -07002003 default:
2004 break; /* No modifications */
2005 }
2006
2007 if (debug_level(2)) {
2008 unsigned int t_len = t->len;
2009 unsigned int s_len = tok_strlen(tok_text(t));
2010 if (t_len != s_len) {
2011 nasm_panic("assertion failed: token \"%s\" type %u len %u has t->len %u\n",
2012 tok_text(t), t->type, s_len, t_len);
2013 t->len = s_len;
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002014 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002015 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07002016
H. Peter Anvin8571f062019-09-23 16:40:03 -07002017 len += t->len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002018 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04002019
H. Peter Anvin734b1882002-04-30 21:01:08 +00002020 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04002021
H. Peter Anvin8571f062019-09-23 16:40:03 -07002022 list_for_each(t, tlist)
2023 p = mempcpy(p, tok_text(t), t->len);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002024 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04002025
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002026 return line;
2027}
2028
2029/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002030 * A scanner, suitable for use by the expression evaluator, which
2031 * operates on a line of Tokens. Expects a pointer to a pointer to
2032 * the first token in the line to be passed in as its private_data
2033 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07002034 *
2035 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00002036 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08002037struct ppscan {
2038 Token *tptr;
2039 int ntokens;
2040};
2041
H. Peter Anvine2c80182005-01-15 22:15:51 +00002042static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002043{
H. Peter Anvin8b262472019-02-26 14:00:54 -08002044 struct ppscan *pps = private_data;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002045 Token *tline;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002046 const char *txt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002047
H. Peter Anvine2c80182005-01-15 22:15:51 +00002048 do {
H. Peter Anvin8b262472019-02-26 14:00:54 -08002049 if (pps->ntokens && (tline = pps->tptr)) {
2050 pps->ntokens--;
2051 pps->tptr = tline->next;
2052 } else {
2053 pps->tptr = NULL;
2054 pps->ntokens = 0;
2055 return tokval->t_type = TOKEN_EOS;
2056 }
2057 } while (tline->type == TOK_WHITESPACE || tline->type == TOK_COMMENT);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002058
H. Peter Anvin8571f062019-09-23 16:40:03 -07002059 txt = tok_text(tline);
2060 tokval->t_charptr = (char *)txt; /* Fix this */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07002061
H. Peter Anvin8571f062019-09-23 16:40:03 -07002062 if (txt[0] == '$') {
2063 if (!txt[1]) {
2064 return tokval->t_type = TOKEN_HERE;
2065 } else if (txt[1] == '$' && !txt[2]) {
2066 return tokval->t_type = TOKEN_BASE;
2067 } else if (tline->type == TOK_ID) {
2068 tokval->t_charptr++;
2069 return tokval->t_type = TOKEN_ID;
2070 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002071 }
2072
H. Peter Anvin8571f062019-09-23 16:40:03 -07002073 switch (tline->type) {
2074 default:
2075 if (tline->len == 1)
2076 return tokval->t_type = txt[0];
2077 /* fall through */
2078 case TOK_ID:
2079 return nasm_token_hash(txt, tokval);
2080
2081 case TOK_NUMBER:
2082 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002083 bool rn_error;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002084 tokval->t_integer = readnum(txt, &rn_error);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002085 if (rn_error)
2086 return tokval->t_type = TOKEN_ERRNUM;
2087 else
2088 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07002089 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002090
H. Peter Anvin8571f062019-09-23 16:40:03 -07002091 case TOK_FLOAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002092 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002093
2094 case TOK_STRING:
2095 tokval->t_charptr = (char *)unquote_token(tline);
2096 tokval->t_inttwo = tline->len;
2097 return tokval->t_type = TOKEN_STR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002098 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002099}
2100
2101/*
H. Peter Anvind2354082019-08-27 16:38:48 -07002102 * 1. An expression (true if nonzero 0)
2103 * 2. The keywords true, on, yes for true
2104 * 3. The keywords false, off, no for false
2105 * 4. An empty line, for true
2106 *
2107 * On error, return defval (usually the previous value)
2108 */
2109static bool pp_get_boolean_option(Token *tline, bool defval)
2110{
2111 static const char * const noyes[] = {
2112 "no", "yes",
2113 "false", "true",
2114 "off", "on"
2115 };
2116 struct ppscan pps;
2117 struct tokenval tokval;
2118 expr *evalresult;
2119
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002120 tline = skip_white(tline);
2121 if (!tline)
H. Peter Anvind2354082019-08-27 16:38:48 -07002122 return true;
2123
2124 if (tline->type == TOK_ID) {
2125 size_t i;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002126 const char *txt = tok_text(tline);
2127
H. Peter Anvind2354082019-08-27 16:38:48 -07002128 for (i = 0; i < ARRAY_SIZE(noyes); i++)
H. Peter Anvin8571f062019-09-23 16:40:03 -07002129 if (!nasm_stricmp(txt, noyes[i]))
H. Peter Anvind2354082019-08-27 16:38:48 -07002130 return i & 1;
2131 }
2132
2133 pps.tptr = NULL;
2134 pps.tptr = tline;
2135 pps.ntokens = -1;
2136 tokval.t_type = TOKEN_INVALID;
2137 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
2138
2139 if (!evalresult)
2140 return true;
2141
2142 if (tokval.t_type)
2143 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
2144 if (!is_really_simple(evalresult)) {
2145 nasm_nonfatal("boolean flag expression must be a constant");
2146 return defval;
2147 }
2148
2149 return reloc_value(evalresult) != 0;
2150}
2151
2152/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002153 * Compare a string to the name of an existing macro; this is a
2154 * simple wrapper which calls either strcmp or nasm_stricmp
2155 * depending on the value of the `casesense' parameter.
2156 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002157static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002158{
H. Peter Anvin734b1882002-04-30 21:01:08 +00002159 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002160}
2161
2162/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07002163 * Compare a string to the name of an existing macro; this is a
2164 * simple wrapper which calls either strcmp or nasm_stricmp
2165 * depending on the value of the `casesense' parameter.
2166 */
2167static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
2168{
2169 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
2170}
2171
2172/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002173 * Return the Context structure associated with a %$ token. Return
2174 * NULL, having _already_ reported an error condition, if the
2175 * context stack isn't deep enough for the supplied number of $
2176 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002177 *
2178 * If "namep" is non-NULL, set it to the pointer to the macro name
2179 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002180 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04002181static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002182{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002183 Context *ctx;
2184 int i;
2185
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002186 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002187 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002188
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002189 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00002190 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002191
H. Peter Anvine2c80182005-01-15 22:15:51 +00002192 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002193 nasm_nonfatal("`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002194 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002195 }
2196
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002197 name += 2;
2198 ctx = cstk;
2199 i = 0;
2200 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002201 name++;
2202 i++;
2203 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002204 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002205 if (!ctx) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002206 nasm_nonfatal("`%s': context stack is only"
2207 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002208 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002209 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002210
2211 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002212 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002213
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04002214 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002215}
2216
2217/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002218 * Open an include file. This routine must always return a valid
2219 * file pointer if it returns - it's responsible for throwing an
2220 * ERR_FATAL and bombing out completely if not. It should also try
2221 * the include path one by one until it finds the file or reaches
2222 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07002223 *
2224 * Note: for INC_PROBE the function returns NULL at all times;
2225 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002226 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07002227enum incopen_mode {
2228 INC_NEEDED, /* File must exist */
2229 INC_OPTIONAL, /* Missing is OK */
2230 INC_PROBE /* Only an existence probe */
2231};
2232
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002233/* This is conducts a full pathname search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002234static FILE *inc_fopen_search(const char *file, char **slpath,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002235 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002236{
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -08002237 const struct strlist_entry *ip = strlist_head(ipath_list);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002238 FILE *fp;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002239 const char *prefix = "";
night199ukfdb1a1b2018-10-18 23:19:47 +02002240 char *sp;
H. Peter Anvind81a2352016-09-21 14:03:18 -07002241 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002242
H. Peter Anvine2c80182005-01-15 22:15:51 +00002243 while (1) {
night199ukfdb1a1b2018-10-18 23:19:47 +02002244 sp = nasm_catfile(prefix, file);
H. Peter Anvind81a2352016-09-21 14:03:18 -07002245 if (omode == INC_PROBE) {
2246 fp = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002247 found = nasm_file_exists(sp);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07002248 } else {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002249 fp = nasm_open_read(sp, fmode);
H. Peter Anvind81a2352016-09-21 14:03:18 -07002250 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002251 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07002252 if (found) {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002253 *slpath = sp;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002254 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002255 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002256
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002257 nasm_free(sp);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002258
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002259 if (!ip) {
2260 *slpath = NULL;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002261 return NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002262 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002263
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002264 prefix = ip->str;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002265 ip = ip->next;
2266 }
2267}
2268
2269/*
2270 * Open a file, or test for the presence of one (depending on omode),
2271 * considering the include path.
2272 */
2273static FILE *inc_fopen(const char *file,
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002274 struct strlist *dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002275 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002276 enum incopen_mode omode,
2277 enum file_flags fmode)
2278{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002279 struct hash_insert hi;
2280 void **hp;
2281 char *path;
2282 FILE *fp = NULL;
2283
2284 hp = hash_find(&FileHash, file, &hi);
2285 if (hp) {
2286 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03002287 if (path || omode != INC_NEEDED) {
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002288 strlist_add(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03002289 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002290 } else {
2291 /* Need to do the actual path search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002292 fp = inc_fopen_search(file, &path, omode, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002293
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002294 /* Positive or negative result */
2295 hash_add(&hi, nasm_strdup(file), path);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002296
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002297 /*
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002298 * Add file to dependency path.
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002299 */
2300 if (path || omode != INC_NEEDED)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002301 strlist_add(dhead, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00002302 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002303
H. Peter Anvin (Intel)5d68f982020-06-01 12:32:35 -07002304 if (path && !fp && omode != INC_PROBE)
2305 fp = nasm_open_read(path, fmode);
2306
2307 if (omode == INC_NEEDED && !fp) {
2308 if (!path)
2309 errno = ENOENT;
2310
2311 nasm_nonfatal("unable to open include file `%s': %s",
2312 file, strerror(errno));
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002313 }
2314
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002315 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002316 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002317
2318 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002319}
2320
2321/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07002322 * Opens an include or input file. Public version, for use by modules
2323 * that get a file:lineno pair and need to look at the file again
2324 * (e.g. the CodeView debug backend). Returns NULL on failure.
2325 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07002326FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07002327{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002328 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07002329}
2330
2331/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002332 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002333 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002334 * return true if _any_ single-line macro of that name is defined.
2335 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002336 * `nparam' or no parameters is defined.
2337 *
2338 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002339 * defined, or nparam is -1, the address of the definition structure
2340 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002341 * is NULL, no action will be taken regarding its contents, and no
2342 * error will occur.
2343 *
2344 * Note that this is also called with nparam zero to resolve
2345 * `ifdef'.
2346 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002347static bool
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002348smacro_defined(Context *ctx, const char *name, int nparam, SMacro **defn,
H. Peter Anvind2354082019-08-27 16:38:48 -07002349 bool nocase, bool find_alias)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002350{
H. Peter Anvin166c2472008-05-28 12:28:58 -07002351 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002352 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002353
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002354 smtbl = ctx ? &ctx->localmac : &smacros;
H. Peter Anvind2354082019-08-27 16:38:48 -07002355
2356restart:
H. Peter Anvin166c2472008-05-28 12:28:58 -07002357 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002358
H. Peter Anvine2c80182005-01-15 22:15:51 +00002359 while (m) {
2360 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002361 (nparam <= 0 || m->nparam == 0 || nparam == m->nparam ||
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002362 (m->greedy && nparam >= m->nparam-1))) {
H. Peter Anvind2354082019-08-27 16:38:48 -07002363 if (m->alias && !find_alias) {
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002364 if (!ppopt.noaliases) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002365 name = tok_text(m->expansion);
H. Peter Anvind2354082019-08-27 16:38:48 -07002366 goto restart;
2367 } else {
2368 continue;
2369 }
2370 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002371 if (defn) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002372 *defn = (nparam == m->nparam || nparam == -1) ? m : NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002373 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002374 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002375 }
2376 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002377 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002378
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002379 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002380}
2381
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002382/* param should be a natural number [0; INT_MAX] */
2383static int read_param_count(const char *str)
2384{
2385 int result;
2386 bool err;
2387
2388 result = readnum(str, &err);
2389 if (result < 0 || result > INT_MAX) {
2390 result = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002391 nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]",
2392 str, 0, INT_MAX);
2393 } else if (err)
2394 nasm_nonfatal("unable to parse parameter count `%s'", str);
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002395 return result;
2396}
2397
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002398/*
2399 * Count and mark off the parameters in a multi-line macro call.
2400 * This is called both from within the multi-line macro expansion
2401 * code, and also to mark off the default parameters when provided
2402 * in a %macro definition line.
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002403 *
2404 * Note that we need space in the params array for parameter 0 being
2405 * a possible captured label as well as the final NULL.
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002406 *
2407 * Returns a pointer to the pointer to a terminal comma if present;
2408 * used to drop an empty terminal argument for legacy reasons.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002409 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002410static Token **count_mmac_params(Token *tline, int *nparamp, Token ***paramsp)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002411{
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07002412 int paramsize;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002413 int nparam = 0;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002414 Token *t;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002415 Token **comma = NULL, **maybe_comma = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002416 Token **params;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002417
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002418 paramsize = PARAM_DELTA;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002419 nasm_newn(params, paramsize);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002420
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002421 t = skip_white(tline);
2422 if (t) {
2423 while (true) {
2424 /* Need two slots for captured label and NULL */
2425 if (unlikely(nparam+2 >= paramsize)) {
2426 paramsize += PARAM_DELTA;
2427 params = nasm_realloc(params, sizeof(*params) * paramsize);
2428 }
2429 params[++nparam] = t;
2430 if (tok_is(t, '{')) {
2431 int brace = 1;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002432
2433 comma = NULL; /* Non-empty parameter */
2434
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002435 while (brace && (t = t->next)) {
2436 brace += tok_is(t, '{');
2437 brace -= tok_is(t, '}');
2438 }
H. Peter Anvin (Intel)f8639bd2020-06-04 16:29:53 -07002439
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002440 if (t) {
2441 /*
2442 * Now we've found the closing brace, look further
2443 * for the comma.
2444 */
2445 t = skip_white(t->next);
2446 if (tok_isnt(t, ','))
2447 nasm_nonfatal("braces do not enclose all of macro parameter");
2448 } else {
2449 nasm_nonfatal("expecting closing brace in macro parameter");
2450 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08002451 }
2452
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002453 /* Advance to the next comma */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002454 maybe_comma = &t->next;
2455 while (tok_isnt(t, ',')) {
2456 if (!tok_white(t))
2457 comma = NULL; /* Non-empty parameter */
2458 maybe_comma = &t->next;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002459 t = t->next;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002460 }
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002461
2462 if (!t)
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002463 break; /* End of string, no comma */
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002464
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002465 comma = maybe_comma; /* Point to comma pointer */
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002466 t = skip_white(t->next); /* Eat the comma and whitespace */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08002467 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002468 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002469
2470 params[nparam+1] = NULL;
2471 *paramsp = params;
2472 *nparamp = nparam;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002473
2474 return comma;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002475}
2476
2477/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002478 * Determine whether one of the various `if' conditions is true or
2479 * not.
2480 *
2481 * We must free the tline we get passed.
2482 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002483static enum cond_state if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002484{
H. Peter Anvin70055962007-10-11 00:05:31 -07002485 bool j;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002486 Token *t, *tt, *origline;
2487 struct ppscan pps;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002488 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002489 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002490 enum pp_token_type needtype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002491 const char *dname = pp_directives[ct];
2492 bool casesense = true;
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002493 enum preproc_token cond = PP_COND(ct);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002494
2495 origline = tline;
2496
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002497 switch (cond) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002498 case PP_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002499 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02002500 while (true) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002501 tline = skip_white(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02002502 if (!tline)
2503 break;
2504 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002505 nasm_nonfatal("`%s' expects context identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002506 dname);
2507 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002508 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002509 if (cstk && cstk->name && !nasm_stricmp(tok_text(tline), cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002510 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002511 tline = tline->next;
2512 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002513 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002514
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002515 case PP_IFDEF:
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002516 case PP_IFDEFALIAS:
2517 {
2518 bool alias = cond == PP_IFDEFALIAS;
2519 SMacro *smac;
2520 Context *ctx;
2521 const char *mname;
2522
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002523 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002524 while (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002525 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002526 if (!tline || (tline->type != TOK_ID &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07002527 tline->type != TOK_LOCAL_MACRO)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002528 nasm_nonfatal("`%s' expects macro identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002529 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002530 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002531 }
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002532
2533 mname = tok_text(tline);
2534 ctx = get_ctx(mname, &mname);
H. Peter Anvin (Intel)b91e7732020-06-05 12:22:26 -07002535 if (smacro_defined(ctx, mname, -1, &smac, true, alias) && smac
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002536 && smac->alias == alias) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002537 j = true;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002538 break;
2539 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002540 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002541 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002542 break;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002543 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002544
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002545 case PP_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002546 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002547 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002548 while (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002549 tline = skip_white(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002550 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002551 tline->type != TOK_STRING &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07002552 tline->type != TOK_INTERNAL_STRING &&
2553 tline->type != TOK_ENVIRON)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002554 nasm_nonfatal("`%s' expects environment variable names",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002555 dname);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002556 goto fail;
2557 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002558
2559 j |= !!pp_getenv(tline, false);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002560 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002561 }
2562 break;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002563
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002564 case PP_IFIDNI:
2565 casesense = false;
2566 /* fall through */
2567 case PP_IFIDN:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002568 tline = expand_smacro(tline);
2569 t = tt = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002570 while (tok_isnt(tt, ','))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002571 tt = tt->next;
2572 if (!tt) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002573 nasm_nonfatal("`%s' expects two comma-separated arguments",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002574 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002575 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002576 }
2577 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002578 j = true; /* assume equality unless proved not */
H. Peter Anvin8571f062019-09-23 16:40:03 -07002579 while (tok_isnt(t, ',') && tt) {
2580 unsigned int l1, l2;
2581 const char *t1, *t2;
2582
2583 if (tok_is(tt, ',')) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002584 nasm_nonfatal("`%s': more than one comma on line",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002585 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002586 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002587 }
2588 if (t->type == TOK_WHITESPACE) {
2589 t = t->next;
2590 continue;
2591 }
2592 if (tt->type == TOK_WHITESPACE) {
2593 tt = tt->next;
2594 continue;
2595 }
2596 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002597 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002598 break;
2599 }
H. Peter Anvind2456592008-06-19 15:04:18 -07002600
H. Peter Anvin8571f062019-09-23 16:40:03 -07002601 t1 = unquote_token(t);
2602 t2 = unquote_token(tt);
2603 l1 = t->len;
2604 l2 = tt->len;
2605
2606 if (l1 != l2 || mmemcmp(t1, t2, l1, casesense)) {
2607 j = false;
2608 break;
2609 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00002610
H. Peter Anvine2c80182005-01-15 22:15:51 +00002611 t = t->next;
2612 tt = tt->next;
2613 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002614 if (!tok_is(t, ',') || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002615 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002616 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002617
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002618 case PP_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04002619 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002620 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002621 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00002622
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002623 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002624 tline = expand_id(tline);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002625 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002626 nasm_nonfatal("`%s' expects a macro name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002627 goto fail;
2628 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002629 nasm_zero(searching);
H. Peter Anvin8571f062019-09-23 16:40:03 -07002630 searching.name = dup_text(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002631 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002632 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002633 searching.nparam_max = INT_MAX;
2634 tline = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002635 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002636 if (!tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002637 } else if (!tok_type(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002638 nasm_nonfatal("`%s' expects a parameter count or nothing",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002639 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002640 } else {
2641 searching.nparam_min = searching.nparam_max =
H. Peter Anvin8571f062019-09-23 16:40:03 -07002642 read_param_count(tok_text(tline));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002643 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002644 if (tline && tok_is(tline->next, '-')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002645 tline = tline->next->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002646 if (tok_is(tline, '*'))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002647 searching.nparam_max = INT_MAX;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002648 else if (!tok_type(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002649 nasm_nonfatal("`%s' expects a parameter count after `-'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002650 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002651 else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002652 searching.nparam_max = read_param_count(tok_text(tline));
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002653 if (searching.nparam_min > searching.nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002654 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002655 searching.nparam_max = searching.nparam_min;
2656 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002657 }
2658 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002659 if (tline && tok_is(tline->next, '+')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002660 tline = tline->next;
2661 searching.plus = true;
2662 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002663 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
2664 while (mmac) {
2665 if (!strcmp(mmac->name, searching.name) &&
2666 (mmac->nparam_min <= searching.nparam_max
2667 || searching.plus)
2668 && (searching.nparam_min <= mmac->nparam_max
2669 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002670 found = true;
2671 break;
2672 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002673 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002674 }
2675 if (tline && tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002676 nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002677 nasm_free(searching.name);
2678 j = found;
2679 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002680 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002681
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002682 case PP_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002683 needtype = TOK_ID;
2684 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002685 case PP_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002686 needtype = TOK_NUMBER;
2687 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002688 case PP_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002689 needtype = TOK_STRING;
2690 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002691
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002692iftype:
2693 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002694
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002695 while (tok_white(t) ||
2696 (needtype == TOK_NUMBER && (tok_is(t, '-') | tok_is(t, '+'))))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002697 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002698
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002699 j = tok_type(t, needtype);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002700 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002701
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002702 case PP_IFTOKEN:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002703 tline = expand_smacro(tline);
2704 t = skip_white(tline);
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002705
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002706 j = false;
2707 if (t) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002708 t = skip_white(t->next); /* Skip the actual token + whitespace */
2709 j = !t;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002710 }
2711 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002712
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002713 case PP_IFEMPTY:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002714 tline = expand_smacro(tline);
2715 t = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002716 j = !t; /* Should be empty */
2717 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002718
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002719 case PP_IF:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002720 pps.tptr = tline = expand_smacro(tline);
2721 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002722 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002723 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002724 if (!evalresult)
2725 return -1;
2726 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002727 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002728 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002729 nasm_nonfatal("non-constant value given to `%s'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002730 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002731 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002732 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002733 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002734 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002735
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002736 case PP_IFUSING:
2737 case PP_IFUSABLE:
2738 {
2739 const struct use_package *pkg;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002740 const char *name;
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002741
H. Peter Anvin8571f062019-09-23 16:40:03 -07002742 pkg = get_use_pkg(tline, dname, &name);
2743 if (!name)
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002744 goto fail;
2745
2746 j = pkg && ((cond == PP_IFUSABLE) | use_loaded[pkg->index]);
2747 break;
2748 }
2749
H. Peter Anvine2c80182005-01-15 22:15:51 +00002750 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002751 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002752 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002753 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002754
2755 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002756 return (j ^ PP_COND_NEGATIVE(ct)) ? COND_IF_TRUE : COND_IF_FALSE;
H. Peter Anvin70653092007-10-19 14:42:29 -07002757
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002758fail:
2759 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002760 return COND_NEVER;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002761}
2762
2763/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002764 * Default smacro expansion routine: just returns a copy of the
2765 * expansion list.
2766 */
2767static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002768smacro_expand_default(const SMacro *s, Token **params, int nparams)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002769{
2770 (void)params;
2771 (void)nparams;
2772
2773 return dup_tlist(s->expansion, NULL);
2774}
2775
2776/*
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002777 * Emit a macro defintion or undef to the listing file, if
2778 * desired. This is similar to detoken(), but it handles the reverse
2779 * expansion list, does not expand %! or local variable tokens, and
2780 * does some special handling for macro parameters.
2781 */
2782static void
2783list_smacro_def(enum preproc_token op, const Context *ctx, const SMacro *m)
2784{
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002785 Token *t;
2786 size_t namelen, size;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002787 char *def, *p;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002788 char *context_prefix = NULL;
2789 size_t context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002790
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002791 namelen = strlen(m->name);
2792 size = namelen + 2; /* Include room for space after name + NUL */
2793
2794 if (ctx) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07002795 int context_depth = cstk->depth - ctx->depth + 1;
2796 context_prefix =
2797 nasm_asprintf("[%s::%"PRIu64"] %%%-*s",
2798 ctx->name ? ctx->name : "",
2799 ctx->number, context_depth, "");
2800
2801 context_len = nasm_last_string_len();
2802 memset(context_prefix + context_len - context_depth,
2803 '$', context_depth);
2804 size += context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002805 }
2806
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002807 list_for_each(t, m->expansion)
H. Peter Anvin8571f062019-09-23 16:40:03 -07002808 size += t->len;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002809
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002810 if (m->nparam) {
2811 /*
2812 * Space for ( and either , or ) around each
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002813 * parameter, plus up to 4 flags.
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002814 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002815 int i;
2816
2817 size += 1 + 4 * m->nparam;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002818 for (i = 0; i < m->nparam; i++)
H. Peter Anvin8571f062019-09-23 16:40:03 -07002819 size += m->params[i].name.len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002820 }
2821
2822 def = nasm_malloc(size);
2823 p = def+size;
2824 *--p = '\0';
2825
2826 list_for_each(t, m->expansion) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002827 p -= t->len;
2828 memcpy(p, tok_text(t), t->len);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002829 }
2830
2831 *--p = ' ';
2832
2833 if (m->nparam) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002834 int i;
2835
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002836 *--p = ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002837 for (i = m->nparam-1; i >= 0; i--) {
2838 enum sparmflags flags = m->params[i].flags;
2839 if (flags & SPARM_GREEDY)
2840 *--p = '+';
H. Peter Anvin8571f062019-09-23 16:40:03 -07002841 p -= m->params[i].name.len;
2842 memcpy(p, tok_text(&m->params[i].name), m->params[i].name.len);
2843
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002844 if (flags & SPARM_NOSTRIP)
2845 *--p = '!';
2846 if (flags & SPARM_STR)
2847 *--p = '&';
2848 if (flags & SPARM_EVAL)
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002849 *--p = '=';
2850 *--p = ',';
2851 }
2852 *p = '('; /* First parameter starts with ( not , */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002853 }
2854
2855 p -= namelen;
2856 memcpy(p, m->name, namelen);
2857
H. Peter Anvin6686de22019-08-10 05:33:14 -07002858 if (context_prefix) {
2859 p -= context_len;
2860 memcpy(p, context_prefix, context_len);
2861 nasm_free(context_prefix);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002862 }
2863
2864 nasm_listmsg("%s %s", pp_directives[op], p);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002865 nasm_free(def);
H. Peter Anvin6686de22019-08-10 05:33:14 -07002866}
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002867
2868/*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002869 * Parse smacro arguments, return argument count. If the tmpl argument
2870 * is set, set the nparam, greedy and params field in the template.
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002871 * *tpp is updated to point to the pointer to the first token after the
2872 * prototype.
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002873 *
2874 * The text values from any argument tokens are "stolen" and the
2875 * corresponding text fields set to NULL.
2876 */
2877static int parse_smacro_template(Token ***tpp, SMacro *tmpl)
2878{
2879 int nparam = 0;
2880 enum sparmflags flags;
2881 struct smac_param *params = NULL;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002882 bool err, done;
2883 bool greedy = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002884 Token **tn = *tpp;
2885 Token *t = *tn;
2886 Token *name;
2887
H. Peter Anvin (Intel)d4607842019-08-20 16:19:37 -07002888 /*
2889 * DO NOT skip whitespace here, or we won't be able to distinguish:
2890 *
2891 * %define foo (a,b) ; no arguments, (a,b) is the expansion
2892 * %define bar(a,b) ; two arguments, empty expansion
2893 *
2894 * This ambiguity was inherited from C.
2895 */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002896
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002897 if (!tok_is(t, '('))
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002898 goto finish;
2899
2900 if (tmpl) {
2901 Token *tx = t;
2902 Token **txpp = &tx;
2903 int sparam;
2904
2905 /* Count parameters first */
2906 sparam = parse_smacro_template(&txpp, NULL);
2907 if (!sparam)
2908 goto finish; /* No parameters, we're done */
2909 nasm_newn(params, sparam);
2910 }
2911
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002912 /* Skip leading paren */
2913 tn = &t->next;
2914 t = *tn;
2915
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002916 name = NULL;
2917 flags = 0;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002918 err = done = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002919
2920 while (!done) {
2921 if (!t || !t->type) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002922 if (name || flags)
2923 nasm_nonfatal("`)' expected to terminate macro template");
2924 else
2925 nasm_nonfatal("parameter identifier expected");
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002926 break;
2927 }
2928
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002929 switch (t->type) {
2930 case TOK_ID:
2931 if (name)
2932 goto bad;
2933 name = t;
2934 break;
2935
2936 case TOK_OTHER:
H. Peter Anvin8571f062019-09-23 16:40:03 -07002937 if (t->len != 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002938 goto bad;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002939 switch (t->text.a[0]) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002940 case '=':
2941 flags |= SPARM_EVAL;
2942 break;
2943 case '&':
2944 flags |= SPARM_STR;
2945 break;
2946 case '!':
2947 flags |= SPARM_NOSTRIP;
2948 break;
2949 case '+':
2950 flags |= SPARM_GREEDY;
2951 greedy = true;
2952 break;
2953 case ',':
2954 if (greedy)
2955 nasm_nonfatal("greedy parameter must be last");
2956 /* fall through */
2957 case ')':
2958 if (params) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002959 if (name)
2960 steal_Token(&params[nparam].name, name);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002961 params[nparam].flags = flags;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002962 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002963 nparam++;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002964 name = NULL;
2965 flags = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002966 done = t->text.a[0] == ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002967 break;
2968 default:
2969 goto bad;
2970 }
2971 break;
2972
2973 case TOK_WHITESPACE:
2974 break;
2975
2976 default:
2977 bad:
2978 if (!err) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002979 nasm_nonfatal("garbage `%s' in macro parameter list", tok_text(t));
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002980 err = true;
2981 }
2982 break;
2983 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002984
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002985 tn = &t->next;
2986 t = *tn;
2987 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002988
2989finish:
2990 while (t && t->type == TOK_WHITESPACE) {
2991 tn = &t->next;
2992 t = t->next;
2993 }
2994 *tpp = tn;
2995 if (tmpl) {
2996 tmpl->nparam = nparam;
2997 tmpl->greedy = greedy;
2998 tmpl->params = params;
2999 }
3000 return nparam;
3001}
3002
3003/*
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003004 * Common code for defining an smacro. The tmpl argument, if not NULL,
3005 * contains any macro parameters that aren't explicit arguments;
3006 * those are the more uncommon macro variants.
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003007 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003008static SMacro *define_smacro(const char *mname, bool casesense,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003009 Token *expansion, SMacro *tmpl)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003010{
3011 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003012 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003013 Context *ctx;
3014 bool defining_alias = false;
3015 unsigned int nparam = 0;
H. Peter Anvin70653092007-10-19 14:42:29 -07003016
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003017 if (tmpl) {
3018 defining_alias = tmpl->alias;
3019 nparam = tmpl->nparam;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003020 if (nparam && !defining_alias)
3021 mark_smac_params(expansion, tmpl, 0);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003022 }
3023
3024 while (1) {
3025 ctx = get_ctx(mname, &mname);
3026
H. Peter Anvind2354082019-08-27 16:38:48 -07003027 if (!smacro_defined(ctx, mname, nparam, &smac, casesense, true)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003028 /* Create a new macro */
3029 smtbl = ctx ? &ctx->localmac : &smacros;
3030 smhead = (SMacro **) hash_findi_add(smtbl, mname);
3031 nasm_new(smac);
3032 smac->next = *smhead;
3033 *smhead = smac;
3034 break;
3035 } else if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003036 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003037 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003038 /*
3039 * Some instances of the old code considered this a failure,
3040 * some others didn't. What is the right thing to do here?
3041 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003042 goto fail;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003043 } else if (!smac->alias || ppopt.noaliases || defining_alias) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003044 /*
3045 * We're redefining, so we have to take over an
3046 * existing SMacro structure. This means freeing
H. Peter Anvin8b262472019-02-26 14:00:54 -08003047 * what was already in it, but not the structure itself.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003048 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003049 clear_smacro(smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003050 break;
3051 } else if (smac->in_progress) {
3052 nasm_nonfatal("macro alias loop");
3053 goto fail;
3054 } else {
3055 /* It is an alias macro; follow the alias link */
3056 SMacro *s;
3057
3058 smac->in_progress = true;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003059 s = define_smacro(tok_text(smac->expansion), casesense,
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003060 expansion, tmpl);
3061 smac->in_progress = false;
3062 return s;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003063 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003064 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003065
3066 smac->name = nasm_strdup(mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003067 smac->casesense = casesense;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003068 smac->expansion = expansion;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003069 smac->expand = smacro_expand_default;
3070 if (tmpl) {
3071 smac->nparam = tmpl->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003072 smac->params = tmpl->params;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003073 smac->alias = tmpl->alias;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003074 smac->greedy = tmpl->greedy;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003075 if (tmpl->expand)
3076 smac->expand = tmpl->expand;
3077 }
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07003078 if (list_option('s')) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07003079 list_smacro_def((smac->alias ? PP_DEFALIAS : PP_DEFINE)
3080 + !casesense, ctx, smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003081 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08003082 return smac;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003083
3084fail:
3085 free_tlist(expansion);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003086 if (tmpl)
3087 free_smacro_members(tmpl);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003088 return NULL;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003089}
3090
3091/*
3092 * Undefine an smacro
3093 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003094static void undef_smacro(const char *mname, bool undefalias)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003095{
3096 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003097 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003098 Context *ctx;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003099
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003100 ctx = get_ctx(mname, &mname);
H. Peter Anvin166c2472008-05-28 12:28:58 -07003101 smtbl = ctx ? &ctx->localmac : &smacros;
3102 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07003103
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003104 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003105 /*
3106 * We now have a macro name... go hunt for it.
3107 */
3108 sp = smhead;
3109 while ((s = *sp) != NULL) {
3110 if (!mstrcmp(s->name, mname, s->casesense)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003111 if (s->alias && !undefalias) {
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003112 if (!ppopt.noaliases) {
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003113 if (s->in_progress) {
3114 nasm_nonfatal("macro alias loop");
3115 } else {
3116 s->in_progress = true;
3117 undef_smacro(tok_text(s->expansion), false);
3118 s->in_progress = false;
3119 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003120 }
3121 } else {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07003122 if (list_option('d'))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003123 list_smacro_def(s->alias ? PP_UNDEFALIAS : PP_UNDEF,
3124 ctx, s);
3125 *sp = s->next;
3126 free_smacro(s);
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003127 continue;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003128 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003129 }
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003130 sp = &s->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003131 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003132 }
3133}
3134
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003135/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07003136 * Parse a mmacro specification.
3137 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003138static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07003139{
H. Peter Anvina26433d2008-07-16 14:40:01 -07003140 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003141 tline = skip_white(tline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003142 tline = expand_id(tline);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003143 if (!tok_type(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003144 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003145 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003146 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02003147
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003148#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003149 def->prev = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003150#endif
H. Peter Anvin8571f062019-09-23 16:40:03 -07003151 def->name = dup_text(tline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003152 def->plus = false;
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07003153 def->nolist = 0;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02003154 def->nparam_min = 0;
3155 def->nparam_max = 0;
3156
H. Peter Anvina26433d2008-07-16 14:40:01 -07003157 tline = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003158 tline = skip_white(tline);
3159 if (!tok_type(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003160 nasm_nonfatal("`%s' expects a parameter count", directive);
3161 else
H. Peter Anvin8571f062019-09-23 16:40:03 -07003162 def->nparam_min = def->nparam_max = read_param_count(tok_text(tline));
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003163 if (tline && tok_is(tline->next, '-')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003164 tline = tline->next->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003165 if (tok_is(tline, '*')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003166 def->nparam_max = INT_MAX;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003167 } else if (!tok_type(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003168 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003169 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003170 def->nparam_max = read_param_count(tok_text(tline));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003171 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003172 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03003173 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003174 }
3175 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003176 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003177 if (tline && tok_is(tline->next, '+')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003178 tline = tline->next;
3179 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003180 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003181 if (tline && tok_type(tline->next, TOK_ID) &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07003182 tline->next->len == 7 &&
3183 !nasm_stricmp(tline->next->text.a, ".nolist")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003184 tline = tline->next;
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07003185 if (!list_option('f'))
3186 def->nolist |= NL_LIST|NL_LINE;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003187 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003188
H. Peter Anvina26433d2008-07-16 14:40:01 -07003189 /*
3190 * Handle default parameters.
3191 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07003192 def->ndefs = 0;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003193 if (tline && tline->next) {
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07003194 Token **comma;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003195 def->dlist = tline->next;
3196 tline->next = NULL;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07003197 comma = count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
3198 if (!ppopt.sane_empty_expansion && comma) {
3199 *comma = NULL;
3200 def->ndefs--;
3201 nasm_warn(WARN_MACRO_PARAMS_LEGACY,
3202 "dropping trailing empty default parameter in defintion of multi-line macro `%s'",
3203 def->name);
3204 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003205 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003206 def->dlist = NULL;
3207 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003208 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003209 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003210
H. Peter Anvin89cee572009-07-15 09:16:54 -04003211 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003212 !def->plus) {
3213 /*
3214 *!macro-defaults [on] macros with more default than optional parameters
3215 *! warns when a macro has more default parameters than optional parameters.
3216 *! See \k{mlmacdef} for why might want to disable this warning.
3217 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003218 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003219 "too many default macro parameters in macro `%s'", def->name);
3220 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02003221
H. Peter Anvina26433d2008-07-16 14:40:01 -07003222 return true;
3223}
3224
3225
3226/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003227 * Decode a size directive
3228 */
3229static int parse_size(const char *str) {
3230 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003231 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003232 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003233 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03003234 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003235}
3236
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003237/*
3238 * Process a preprocessor %pragma directive. Currently there are none.
3239 * Gets passed the token list starting with the "preproc" token from
3240 * "%pragma preproc".
3241 */
3242static void do_pragma_preproc(Token *tline)
3243{
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003244 const char *txt;
3245
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003246 /* Skip to the real stuff */
3247 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003248 tline = skip_white(tline);
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003249
3250 if (!tok_type(tline, TOK_ID))
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003251 return;
3252
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003253 txt = tok_text(tline);
3254 if (!nasm_stricmp(txt, "sane_empty_expansion")) {
3255 tline = skip_white(tline->next);
3256 ppopt.sane_empty_expansion =
3257 pp_get_boolean_option(tline, ppopt.sane_empty_expansion);
3258 } else {
3259 /* Unknown pragma, ignore for now */
3260 }
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003261}
3262
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003263static bool is_macro_id(const Token *t)
3264{
H. Peter Anvin8571f062019-09-23 16:40:03 -07003265 return tok_type(t, TOK_ID) || tok_type(t, TOK_LOCAL_MACRO);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003266}
3267
H. Peter Anvin8571f062019-09-23 16:40:03 -07003268static const char *get_id(Token **tp, const char *dname)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003269{
H. Peter Anvin8571f062019-09-23 16:40:03 -07003270 const char *id;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003271 Token *t = *tp;
3272
3273 t = t->next; /* Skip directive */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003274 t = skip_white(t);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003275 t = expand_id(t);
3276
3277 if (!is_macro_id(t)) {
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003278 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003279 return NULL;
3280 }
3281
H. Peter Anvin8571f062019-09-23 16:40:03 -07003282 id = tok_text(t);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003283 t = skip_white(t);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003284 *tp = t;
3285 return id;
3286}
3287
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003288/* Parse a %use package name and find the package. Set *err on syntax error. */
3289static const struct use_package *
H. Peter Anvin8571f062019-09-23 16:40:03 -07003290get_use_pkg(Token *t, const char *dname, const char **name)
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003291{
H. Peter Anvin8571f062019-09-23 16:40:03 -07003292 const char *id;
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003293
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003294 t = skip_white(t);
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003295 t = expand_smacro(t);
3296
H. Peter Anvin8571f062019-09-23 16:40:03 -07003297 *name = NULL;
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003298
H. Peter Anvin8571f062019-09-23 16:40:03 -07003299 if (!t) {
3300 nasm_nonfatal("`%s' expects a package name, got end of line", dname);
3301 return NULL;
3302 } else if (t->type != TOK_ID && t->type != TOK_STRING) {
3303 nasm_nonfatal("`%s' expects a package name, got `%s'",
3304 dname, tok_text(t));
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003305 return NULL;
3306 }
3307
H. Peter Anvin8571f062019-09-23 16:40:03 -07003308 *name = id = unquote_token(t);
3309
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003310 t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003311 t = skip_white(t);
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003312 if (t)
3313 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
3314
3315 return nasm_find_use_package(id);
3316}
3317
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003318/*
3319 * Mark parameter tokens in an smacro definition. If the type argument
3320 * is 0, create smac param tokens, otherwise use the type specified;
3321 * normally this is used for TOK_XDEF_PARAM, which is used to protect
3322 * parameter tokens during expansion during %xdefine.
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003323 *
3324 * tmpl may not be NULL here.
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003325 */
3326static void mark_smac_params(Token *tline, const SMacro *tmpl,
3327 enum pp_token_type type)
3328{
3329 const struct smac_param *params = tmpl->params;
3330 int nparam = tmpl->nparam;
3331 Token *t;
3332 int i;
3333
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003334 list_for_each(t, tline) {
3335 if (t->type != TOK_ID && t->type != TOK_XDEF_PARAM)
3336 continue;
3337
3338 for (i = 0; i < nparam; i++) {
3339 if (tok_text_match(t, &params[i].name))
3340 t->type = type ? type : tok_smac_param(i);
3341 }
3342 }
3343}
3344
Ed Beroset3ab3f412002-06-11 03:31:49 +00003345/**
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003346 * %clear selected macro sets either globally or in contexts
3347 */
3348static void do_clear(enum clear_what what, bool context)
3349{
3350 if (context) {
3351 if (what & CLEAR_ALLDEFINE) {
3352 Context *ctx;
3353 list_for_each(ctx, cstk)
3354 clear_smacro_table(&ctx->localmac, what);
3355 }
3356 /* Nothing else can be context-local */
3357 } else {
3358 if (what & CLEAR_ALLDEFINE)
3359 clear_smacro_table(&smacros, what);
3360 if (what & CLEAR_MMACRO)
3361 free_mmacro_table(&mmacros);
3362 }
3363}
3364
3365/**
Ed Beroset3ab3f412002-06-11 03:31:49 +00003366 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003367 * Find out if a line contains a preprocessor directive, and deal
3368 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07003369 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00003370 * If a directive _is_ found, it is the responsibility of this routine
3371 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003372 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00003373 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003374 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00003375 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07003376 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003377 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07003378static int do_directive(Token *tline, Token **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003379{
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003380 enum preproc_token op;
H. Peter Anvin4169a472007-09-12 01:29:43 +00003381 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07003382 bool err;
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07003383 enum nolist_flags nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003384 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07003385 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003386 int offset;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003387 const char *p;
3388 char *q, *qbuf;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003389 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003390 const char *mname;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003391 struct ppscan pps;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003392 Include *inc;
3393 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003394 Cond *cond;
3395 MMacro *mmac, **mmhead;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003396 Token *t = NULL, *tt, *macro_start, *last, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003397 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003398 struct tokenval tokval;
3399 expr *evalresult;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003400 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003401 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08003402 errflags severity;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003403 const char *dname; /* Name of directive, for messages */
H. Peter Anvin76690a12002-04-30 20:52:49 +00003404
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003405 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00003406 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003407
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003408 tline = skip_white(tline);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003409 if (!tline || !tok_type(tline, TOK_PREPROC_ID))
3410 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003411
H. Peter Anvin8571f062019-09-23 16:40:03 -07003412 dname = tok_text(tline);
3413 if (dname[1] == '%')
3414 return NO_DIRECTIVE_FOUND;
3415
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003416 op = pp_token_hash(dname);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003417
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003418 casesense = true;
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003419 if (PP_HAS_CASE(op) & PP_INSENSITIVE(op)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003420 casesense = false;
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003421 op--;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003422 }
3423
3424 /*
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07003425 * %line directives are always processed immediately and
3426 * unconditionally, as they are intended to reflect position
3427 * in externally preprocessed sources.
3428 */
3429 if (op == PP_LINE) {
3430 /*
3431 * Syntax is `%line nnn[+mmm] [filename]'
3432 */
3433 if (pp_noline || istk->mstk.mstk)
3434 goto done;
3435
3436 tline = tline->next;
3437 tline = skip_white(tline);
3438 if (!tok_type(tline, TOK_NUMBER)) {
3439 nasm_nonfatal("`%s' expects line number", dname);
3440 goto done;
3441 }
3442 k = readnum(tok_text(tline), &err);
3443 m = 1;
3444 tline = tline->next;
3445 if (tok_is(tline, '+') || tok_is(tline, '-')) {
3446 bool minus = tok_is(tline, '-');
3447 tline = tline->next;
3448 if (!tok_type(tline, TOK_NUMBER)) {
3449 nasm_nonfatal("`%s' expects line increment", dname);
3450 goto done;
3451 }
3452 m = readnum(tok_text(tline), &err);
3453 if (minus)
3454 m = -m;
3455 tline = tline->next;
3456 }
3457 tline = skip_white(tline);
3458 if (tline) {
3459 if (tline->type == TOK_STRING) {
3460 src_set_fname(unquote_token(tline));
3461 } else {
3462 char *fname = detoken(tline, false);
3463 src_set_fname(fname);
3464 nasm_free(fname);
3465 }
3466 }
3467 src_set_linnum(k);
3468
3469 istk->where = src_where();
3470 istk->lineinc = m;
3471 goto done;
3472 }
3473
3474 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003475 * If we're in a non-emitting branch of a condition construct,
3476 * or walking to the end of an already terminated %rep block,
3477 * we should ignore all directives except for condition
3478 * directives.
3479 */
3480 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003481 (istk->mstk.mstk && !istk->mstk.mstk->in_progress)) &&
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003482 !is_condition(op)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003483 return NO_DIRECTIVE_FOUND;
3484 }
3485
3486 /*
3487 * If we're defining a macro or reading a %rep block, we should
3488 * ignore all directives except for %macro/%imacro (which nest),
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07003489 * %endm/%endmacro, %line and (only if we're in a %rep block) %endrep.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003490 * If we're in a %rep block, another %rep nests, so should be let through.
3491 */
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003492 if (defining && op != PP_MACRO && op != PP_RMACRO &&
3493 op != PP_ENDMACRO && op != PP_ENDM &&
3494 (defining->name || (op != PP_ENDREP && op != PP_REP))) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003495 return NO_DIRECTIVE_FOUND;
3496 }
3497
3498 if (defining) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003499 if (op == PP_MACRO || op == PP_RMACRO) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003500 nested_mac_count++;
3501 return NO_DIRECTIVE_FOUND;
3502 } else if (nested_mac_count > 0) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003503 if (op == PP_ENDMACRO) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003504 nested_mac_count--;
3505 return NO_DIRECTIVE_FOUND;
3506 }
3507 }
3508 if (!defining->name) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003509 if (op == PP_REP) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003510 nested_rep_count++;
3511 return NO_DIRECTIVE_FOUND;
3512 } else if (nested_rep_count > 0) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003513 if (op == PP_ENDREP) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003514 nested_rep_count--;
3515 return NO_DIRECTIVE_FOUND;
3516 }
3517 }
3518 }
3519 }
3520
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003521 switch (op) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003522 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003523 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003524 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003525
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003526 case PP_PRAGMA:
3527 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003528 * %pragma namespace options...
3529 *
3530 * The namespace "preproc" is reserved for the preprocessor;
3531 * all other namespaces generate a [pragma] assembly directive.
3532 *
3533 * Invalid %pragmas are ignored and may have different
3534 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003535 */
H. Peter Anvinf5d7d902019-08-10 06:21:00 -07003536 t = tline;
3537 tline = tline->next;
3538 t->next = NULL;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003539 tline = zap_white(expand_smacro(tline));
3540 if (tok_type(tline, TOK_ID)) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003541 if (!nasm_stricmp(tok_text(tline), "preproc")) {
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003542 /* Preprocessor pragma */
3543 do_pragma_preproc(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07003544 free_tlist(tline);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003545 } else {
3546 /* Build the assembler directive */
H. Peter Anvin06335872019-08-10 06:42:55 -07003547
3548 /* Append bracket to the end of the output */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003549 for (t = tline; t->next; t = t->next)
3550 ;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003551 t->next = make_tok_char(NULL, ']');
H. Peter Anvin06335872019-08-10 06:42:55 -07003552
3553 /* Prepend "[pragma " */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003554 t = new_White(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07003555 t = new_Token(t, TOK_ID, "pragma", 6);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003556 t = make_tok_char(t, '[');
H. Peter Anvin06335872019-08-10 06:42:55 -07003557 tline = t;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07003558 *output = tline;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003559 }
3560 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003561 break;
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003562
H. Peter Anvine2c80182005-01-15 22:15:51 +00003563 case PP_STACKSIZE:
3564 /* Directive to tell NASM what the default stack size is. The
3565 * default is for a 16-bit stack, and this can be overriden with
3566 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003567 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003568 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003569 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003570 nasm_nonfatal("`%s' missing size parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003571 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07003572 if (nasm_stricmp(tok_text(tline), "flat") == 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003573 /* All subsequent ARG directives are for a 32-bit stack */
3574 StackSize = 4;
3575 StackPointer = "ebp";
3576 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003577 LocalOffset = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003578 } else if (nasm_stricmp(tok_text(tline), "flat64") == 0) {
Charles Crayne7eaf9192007-11-08 22:11:14 -08003579 /* All subsequent ARG directives are for a 64-bit stack */
3580 StackSize = 8;
3581 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03003582 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08003583 LocalOffset = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003584 } else if (nasm_stricmp(tok_text(tline), "large") == 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003585 /* All subsequent ARG directives are for a 16-bit stack,
3586 * far function call.
3587 */
3588 StackSize = 2;
3589 StackPointer = "bp";
3590 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003591 LocalOffset = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003592 } else if (nasm_stricmp(tok_text(tline), "small") == 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003593 /* All subsequent ARG directives are for a 16-bit stack,
3594 * far function call. We don't support near functions.
3595 */
3596 StackSize = 2;
3597 StackPointer = "bp";
3598 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003599 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003600 } else {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003601 nasm_nonfatal("`%s' invalid size type", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003602 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003603 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003604
H. Peter Anvine2c80182005-01-15 22:15:51 +00003605 case PP_ARG:
3606 /* TASM like ARG directive to define arguments to functions, in
3607 * the following form:
3608 *
3609 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
3610 */
3611 offset = ArgOffset;
3612 do {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003613 const char *arg;
3614 char directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003615 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003616
H. Peter Anvine2c80182005-01-15 22:15:51 +00003617 /* Find the argument name */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003618 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003619 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003620 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003621 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003622 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07003623 arg = tok_text(tline);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003624
H. Peter Anvine2c80182005-01-15 22:15:51 +00003625 /* Find the argument size type */
3626 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003627 if (!tok_is(tline, ':')) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003628 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003629 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003630 }
3631 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003632 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003633 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003634 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003635 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003636
H. Peter Anvine2c80182005-01-15 22:15:51 +00003637 /* Allow macro expansion of type parameter */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003638 tt = tokenize(tok_text(tline));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003639 tt = expand_smacro(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003640 size = parse_size(tok_text(tt));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003641 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003642 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003643 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003644 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003645 }
3646 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003647
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003648 /* Round up to even stack slots */
3649 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003650
H. Peter Anvine2c80182005-01-15 22:15:51 +00003651 /* Now define the macro for the argument */
3652 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
3653 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003654 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003655 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003656
H. Peter Anvine2c80182005-01-15 22:15:51 +00003657 /* Move to the next argument in the list */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003658 tline = skip_white(tline->next);
3659 } while (tok_is(tline, ','));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003660 ArgOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003661 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003662
H. Peter Anvine2c80182005-01-15 22:15:51 +00003663 case PP_LOCAL:
3664 /* TASM like LOCAL directive to define local variables for a
3665 * function, in the following form:
3666 *
3667 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
3668 *
3669 * The '= LocalSize' at the end is ignored by NASM, but is
3670 * required by TASM to define the local parameter size (and used
3671 * by the TASM macro package).
3672 */
3673 offset = LocalOffset;
3674 do {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003675 const char *local;
3676 char directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003677 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003678
H. Peter Anvine2c80182005-01-15 22:15:51 +00003679 /* Find the argument name */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003680 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003681 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003682 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003683 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003684 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07003685 local = tok_text(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003686
H. Peter Anvine2c80182005-01-15 22:15:51 +00003687 /* Find the argument size type */
3688 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003689 if (!tok_is(tline, ':')) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003690 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003691 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003692 }
3693 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003694 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003695 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003696 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003697 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003698
H. Peter Anvine2c80182005-01-15 22:15:51 +00003699 /* Allow macro expansion of type parameter */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003700 tt = tokenize(tok_text(tline));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003701 tt = expand_smacro(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003702 size = parse_size(tok_text(tt));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003703 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003704 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003705 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003706 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003707 }
3708 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003709
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003710 /* Round up to even stack slots */
3711 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003712
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003713 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003714
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003715 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003716 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
3717 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003718 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003719
H. Peter Anvine2c80182005-01-15 22:15:51 +00003720 /* Now define the assign to setup the enter_c macro correctly */
3721 snprintf(directive, sizeof(directive),
3722 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003723 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003724
H. Peter Anvine2c80182005-01-15 22:15:51 +00003725 /* Move to the next argument in the list */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003726 tline = skip_white(tline->next);
3727 } while (tok_is(tline, ','));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003728 LocalOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003729 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003730
H. Peter Anvine2c80182005-01-15 22:15:51 +00003731 case PP_CLEAR:
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003732 {
3733 bool context = false;
3734
3735 t = tline->next = expand_smacro(tline->next);
3736 t = skip_white(t);
3737 if (!t) {
3738 /* Emulate legacy behavior */
3739 do_clear(CLEAR_DEFINE|CLEAR_MMACRO, false);
3740 } else {
3741 while ((t = skip_white(t)) && t->type == TOK_ID) {
3742 const char *txt = tok_text(t);
3743 if (!nasm_stricmp(txt, "all")) {
3744 do_clear(CLEAR_ALL, context);
3745 } else if (!nasm_stricmp(txt, "define") ||
3746 !nasm_stricmp(txt, "def") ||
3747 !nasm_stricmp(txt, "smacro")) {
3748 do_clear(CLEAR_DEFINE, context);
3749 } else if (!nasm_stricmp(txt, "defalias") ||
3750 !nasm_stricmp(txt, "alias") ||
3751 !nasm_stricmp(txt, "salias")) {
3752 do_clear(CLEAR_DEFALIAS, context);
3753 } else if (!nasm_stricmp(txt, "alldef") ||
3754 !nasm_stricmp(txt, "alldefine")) {
3755 do_clear(CLEAR_ALLDEFINE, context);
3756 } else if (!nasm_stricmp(txt, "macro") ||
3757 !nasm_stricmp(txt, "mmacro")) {
3758 do_clear(CLEAR_MMACRO, context);
3759 } else if (!nasm_stricmp(txt, "context") ||
3760 !nasm_stricmp(txt, "ctx")) {
3761 context = true;
3762 } else if (!nasm_stricmp(txt, "global")) {
3763 context = false;
3764 } else if (!nasm_stricmp(txt, "nothing") ||
3765 !nasm_stricmp(txt, "none") ||
3766 !nasm_stricmp(txt, "ignore") ||
3767 !nasm_stricmp(txt, "-") ||
3768 !nasm_stricmp(txt, "--")) {
3769 /* Do nothing */
3770 } else {
3771 nasm_nonfatal("invalid option to %s: %s", dname, txt);
3772 t = NULL;
3773 }
3774 }
3775 }
3776
3777 t = skip_white(t);
3778 if (t)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003779 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003780 break;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003781 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003782
H. Peter Anvin418ca702008-05-30 10:42:30 -07003783 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003784 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003785 t = skip_white(t);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003786 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003787 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003788 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003789 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003790 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003791 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003792 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003793
3794 strlist_add(deplist, unquote_token_cstr(t));
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003795 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003796
3797 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003798 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003799 t = skip_white(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07003800
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003801 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003802 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003803 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003804 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003805 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003806 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003807 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003808 p = unquote_token_cstr(t);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003809 nasm_new(inc);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003810 inc->next = istk;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003811 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07003812 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08003813 (pp_mode == PP_DEPS)
3814 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003815 if (!inc->fp) {
3816 /* -MG given but file not found */
3817 nasm_free(inc);
3818 } else {
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07003819 inc->where = src_where();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003820 inc->lineinc = 1;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003821 inc->nolist = istk->nolist;
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07003822 inc->noline = istk->noline;
3823 if (!inc->noline)
3824 src_set(0, found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003825 istk = inc;
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003826 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003827 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003828 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003829
H. Peter Anvind2456592008-06-19 15:04:18 -07003830 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003831 {
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003832 const struct use_package *pkg;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003833 const char *name;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003834
H. Peter Anvin8571f062019-09-23 16:40:03 -07003835 pkg = get_use_pkg(tline->next, dname, &name);
3836 if (!name)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003837 goto done;
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003838 if (!pkg) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003839 nasm_nonfatal("unknown `%s' package: `%s'", dname, name);
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003840 } else if (!use_loaded[pkg->index]) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003841 /*
3842 * Not already included, go ahead and include it.
3843 * Treat it as an include file for the purpose of
3844 * producing a listing.
3845 */
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003846 use_loaded[pkg->index] = true;
3847 stdmacpos = pkg->macros;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003848 nasm_new(inc);
3849 inc->next = istk;
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07003850 inc->nolist = istk->nolist + !list_option('b');
3851 inc->noline = istk->noline;
3852 if (!inc->noline)
3853 src_set(0, NULL);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003854 istk = inc;
3855 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003856 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003857 break;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003858 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003859 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003860 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07003861 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003862 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003863 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003864 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003865 if (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003866 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003867 nasm_nonfatal("`%s' expects a context identifier", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003868 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003869 }
3870 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003871 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003872 dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003873 p = tok_text(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003874 } else {
3875 p = NULL; /* Anonymous */
3876 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07003877
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003878 if (op == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08003879 nasm_new(ctx);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003880 ctx->depth = cstk ? cstk->depth + 1 : 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003881 ctx->next = cstk;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003882 ctx->name = p ? nasm_strdup(p) : NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003883 ctx->number = unique++;
3884 cstk = ctx;
3885 } else {
3886 /* %pop or %repl */
3887 if (!cstk) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003888 nasm_nonfatal("`%s': context stack is empty", dname);
3889 } else if (op == PP_POP) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003890 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003891 nasm_nonfatal("`%s' in wrong context: %s, "
H. Peter Anvin8b262472019-02-26 14:00:54 -08003892 "expected %s",
3893 dname, cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003894 else
3895 ctx_pop();
3896 } else {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003897 /* op == PP_REPL */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003898 nasm_free((char *)cstk->name);
3899 cstk->name = p ? nasm_strdup(p) : NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003900 p = NULL;
3901 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003902 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003903 break;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003904 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003905 severity = ERR_FATAL;
3906 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003907 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003908 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003909 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003910 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003911 /*!
3912 *!user [on] %warning directives
3913 *! controls output of \c{%warning} directives (see \k{pperror}).
3914 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003915 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003916 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003917
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003918issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07003919 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003920 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003921 tline->next = expand_smacro(tline->next);
3922 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003923 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003924 t = tline ? tline->next : NULL;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003925 t = skip_white(t);
3926 if (tok_type(tline, TOK_STRING) && !t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003927 /* The line contains only a quoted string */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003928 p = unquote_token(tline); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08003929 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003930 } else {
3931 /* Not a quoted string, or more than a quoted string */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003932 q = detoken(tline, false);
3933 nasm_error(severity, "%s", q);
3934 nasm_free(q);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003935 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003936 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003937 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003938
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003939 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003940 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003941 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003942 else {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003943 j = if_condition(tline->next, op);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003944 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003945 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003946 cond = nasm_malloc(sizeof(Cond));
3947 cond->next = istk->conds;
3948 cond->state = j;
3949 istk->conds = cond;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003950 if(istk->mstk.mstk)
3951 istk->mstk.mstk->condcnt++;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003952 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003953
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003954 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003955 if (!istk->conds)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003956 nasm_fatal("`%s': no matching `%%if'", dname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003957 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003958 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003959 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003960 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003961
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003962 case COND_DONE:
3963 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003964 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003965
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003966 case COND_ELSE_TRUE:
3967 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003968 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003969 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003970 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003971 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003972
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003973 case COND_IF_FALSE:
3974 /*
3975 * IMPORTANT: In the case of %if, we will already have
3976 * called expand_mmac_params(); however, if we're
3977 * processing an %elif we must have been in a
3978 * non-emitting mode, which would have inhibited
3979 * the normal invocation of expand_mmac_params().
3980 * Therefore, we have to do it explicitly here.
3981 */
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003982 j = if_condition(expand_mmac_params(tline->next), op);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003983 tline->next = NULL; /* it got freed */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003984 istk->conds->state = j;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003985 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003986 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003987 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003988
H. Peter Anvine2c80182005-01-15 22:15:51 +00003989 case PP_ELSE:
3990 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003991 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003992 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003993 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07003994 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003995 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003996 case COND_IF_TRUE:
3997 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003998 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003999 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004000
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004001 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004002 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004003
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004004 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004005 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004006 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004007
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004008 case COND_ELSE_TRUE:
4009 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004010 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004011 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004012 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004013 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004014 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004015 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004016
H. Peter Anvine2c80182005-01-15 22:15:51 +00004017 case PP_ENDIF:
4018 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004019 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004020 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004021 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004022 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004023 cond = istk->conds;
4024 istk->conds = cond->next;
4025 nasm_free(cond);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004026 if(istk->mstk.mstk)
4027 istk->mstk.mstk->condcnt--;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004028 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004029
H. Peter Anvin8b262472019-02-26 14:00:54 -08004030 case PP_RMACRO:
4031 case PP_MACRO:
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07004032 {
4033 MMacro *def;
4034
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004035 nasm_assert(!defining);
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07004036 nasm_new(def);
4037 def->casesense = casesense;
4038 def->dstk.mmac = defining;
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004039 if (op == PP_RMACRO)
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07004040 def->max_depth = nasm_limit[LIMIT_MACRO_LEVELS];
4041 if (!parse_mmacro_spec(tline, def, dname)) {
4042 nasm_free(def);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004043 goto done;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004044 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07004045
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07004046 defining = def;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07004047 defining->where = istk->where;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07004048
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004049 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
4050 while (mmac) {
4051 if (!strcmp(mmac->name, defining->name) &&
4052 (mmac->nparam_min <= defining->nparam_max
4053 || defining->plus)
4054 && (defining->nparam_min <= mmac->nparam_max
4055 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004056 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004057 defining->name);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004058 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004059 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004060 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004061 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004062 break;
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07004063 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004064
H. Peter Anvine2c80182005-01-15 22:15:51 +00004065 case PP_ENDM:
4066 case PP_ENDMACRO:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004067 if (!(defining && defining->name)) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004068 nasm_nonfatal("`%s': not defining a macro", tok_text(tline));
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004069 goto done;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004070 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004071 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
4072 defining->next = *mmhead;
4073 *mmhead = defining;
4074 defining = NULL;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004075 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004076
H. Peter Anvin89cee572009-07-15 09:16:54 -04004077 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004078 /*
4079 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004080 * macro-end marker for a macro with a name. Then we
4081 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004082 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004083 list_for_each(l, istk->expansion)
4084 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004085 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004086
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004087 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004088 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004089 * Remove all conditional entries relative to this
4090 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004091 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004092 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
4093 cond = istk->conds;
4094 istk->conds = cond->next;
4095 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004096 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004097 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004098 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004099 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004100 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004101 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05004102
H. Peter Anvina26433d2008-07-16 14:40:01 -07004103 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08004104 casesense = false;
4105 /* fall through */
4106 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07004107 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004108 MMacro **mmac_p;
4109 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07004110
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004111 nasm_zero(spec);
H. Peter Anvin8b262472019-02-26 14:00:54 -08004112 spec.casesense = casesense;
4113 if (!parse_mmacro_spec(tline, &spec, dname)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004114 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004115 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004116 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
4117 while (mmac_p && *mmac_p) {
4118 mmac = *mmac_p;
4119 if (mmac->casesense == spec.casesense &&
4120 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
4121 mmac->nparam_min == spec.nparam_min &&
4122 mmac->nparam_max == spec.nparam_max &&
4123 mmac->plus == spec.plus) {
4124 *mmac_p = mmac->next;
4125 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004126 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004127 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004128 }
4129 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004130 free_tlist(spec.dlist);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004131 break;
H. Peter Anvina26433d2008-07-16 14:40:01 -07004132 }
4133
H. Peter Anvine2c80182005-01-15 22:15:51 +00004134 case PP_ROTATE:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004135 while (tok_white(tline->next))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004136 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04004137 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004138 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004139 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00004140 return DIRECTIVE_FOUND;
4141 }
4142 t = expand_smacro(tline->next);
4143 tline->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004144 pps.tptr = tline = t;
4145 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004146 tokval.t_type = TOKEN_INVALID;
4147 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004148 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004149 free_tlist(tline);
4150 if (!evalresult)
4151 return DIRECTIVE_FOUND;
4152 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004153 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00004154 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004155 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00004156 return DIRECTIVE_FOUND;
4157 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004158 mmac = istk->mstk.mmac;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004159 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004160 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004161 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004162 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004163 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004164 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004165
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004166 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004167 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004168 rotate += mmac->nparam;
4169
4170 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004171 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004172 break;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004173
H. Peter Anvine2c80182005-01-15 22:15:51 +00004174 case PP_REP:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004175 {
4176 MMacro *tmp_defining;
4177
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07004178 nolist = 0;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004179 tline = skip_white(tline->next);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004180 if (tok_type(tline, TOK_ID) && tline->len == 7 &&
4181 !nasm_memicmp(tline->text.a, ".nolist", 7)) {
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07004182 if (!list_option('f'))
4183 nolist |= NL_LIST; /* ... but update line numbers */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004184 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004185 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004186
H. Peter Anvine2c80182005-01-15 22:15:51 +00004187 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08004188 pps.tptr = expand_smacro(tline);
4189 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004190 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004191 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004192 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004193 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004194 if (!evalresult)
4195 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004196 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004197 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00004198 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004199 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004200 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004201 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04004202 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07004203 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004204 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
4205 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04004206 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07004207 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08004208 /*!
4209 *!negative-rep [on] regative %rep count
4210 *! warns about negative counts given to the \c{%rep}
4211 *! preprocessor directive.
4212 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08004213 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07004214 "negative `%%rep' count: %"PRId64, count);
4215 count = 0;
4216 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04004217 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07004218 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004219 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004220 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07004221 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004222 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004223 tmp_defining = defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07004224 nasm_new(defining);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004225 defining->nolist = nolist;
4226 defining->in_progress = count;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004227 defining->mstk = istk->mstk;
4228 defining->dstk.mstk = tmp_defining;
4229 defining->dstk.mmac = tmp_defining ? tmp_defining->dstk.mmac : NULL;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07004230 defining->where = istk->where;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004231 break;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004232 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004233
H. Peter Anvine2c80182005-01-15 22:15:51 +00004234 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004235 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004236 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004237 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004238 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004239
H. Peter Anvine2c80182005-01-15 22:15:51 +00004240 /*
4241 * Now we have a "macro" defined - although it has no name
4242 * and we won't be entering it in the hash tables - we must
4243 * push a macro-end marker for it on to istk->expansion.
4244 * After that, it will take care of propagating itself (a
4245 * macro-end marker line for a macro which is really a %rep
4246 * block will cause the macro to be re-expanded, complete
4247 * with another macro-end marker to ensure the process
4248 * continues) until the whole expansion is forcibly removed
4249 * from istk->expansion by a %exitrep.
4250 */
H. Peter Anvin6686de22019-08-10 05:33:14 -07004251 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004252 l->next = istk->expansion;
4253 l->finishes = defining;
4254 l->first = NULL;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07004255 l->where = src_where();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004256 istk->expansion = l;
4257
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004258 istk->mstk.mstk = defining;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004259
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07004260 /* A loop does not change istk->noline */
4261 istk->nolist += !!(defining->nolist & NL_LIST);
4262 if (!istk->nolist)
4263 lfmt->uplevel(LIST_MACRO, 0);
4264
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004265 defining = defining->dstk.mstk;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004266 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004267
H. Peter Anvine2c80182005-01-15 22:15:51 +00004268 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004269 /*
4270 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004271 * macro-end marker for a macro with no name. Then we set
4272 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004273 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004274 list_for_each(l, istk->expansion)
4275 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004276 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004277
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004278 if (l)
H. Peter Anvind983b622019-10-07 21:19:32 -07004279 l->finishes->in_progress = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004280 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004281 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004282 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004283
H. Peter Anvin8b262472019-02-26 14:00:54 -08004284 case PP_DEFINE:
4285 case PP_XDEFINE:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004286 case PP_DEFALIAS:
H. Peter Anvin8b262472019-02-26 14:00:54 -08004287 {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004288 SMacro tmpl;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004289 Token **lastp;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07004290 int nparam;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -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;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004294
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004295 nasm_zero(tmpl);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004296 lastp = &tline->next;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07004297 nparam = parse_smacro_template(&lastp, &tmpl);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004298 tline = *lastp;
4299 *lastp = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004300
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004301 if (unlikely(op == PP_DEFALIAS)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004302 macro_start = tline;
4303 if (!is_macro_id(macro_start)) {
4304 nasm_nonfatal("`%s' expects a macro identifier to alias",
4305 dname);
4306 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004307 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004308 tt = macro_start->next;
4309 macro_start->next = NULL;
4310 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004311 tline = skip_white(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004312 if (tline && tline->type) {
4313 nasm_warn(WARN_OTHER,
4314 "trailing garbage after aliasing identifier ignored");
4315 }
4316 free_tlist(tt);
4317 tmpl.alias = true;
4318 } else {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004319 if (op == PP_XDEFINE) {
4320 /* Protect macro parameter tokens */
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07004321 if (nparam)
4322 mark_smac_params(tline, &tmpl, TOK_XDEF_PARAM);
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004323 tline = expand_smacro(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004324 }
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004325 /* NB: Does this still make sense? */
4326 macro_start = reverse_tokens(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004327 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004328
H. Peter Anvine2c80182005-01-15 22:15:51 +00004329 /*
4330 * Good. We now have a macro name, a parameter count, and a
4331 * token list (in reverse order) for an expansion. We ought
4332 * to be OK just to create an SMacro, store it, and let
4333 * free_tlist have the rest of the line (which we have
4334 * carefully re-terminated after chopping off the expansion
4335 * from the end).
4336 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004337 define_smacro(mname, casesense, macro_start, &tmpl);
4338 break;
4339 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004340
H. Peter Anvine2c80182005-01-15 22:15:51 +00004341 case PP_UNDEF:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004342 case PP_UNDEFALIAS:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004343 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004344 goto done;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004345 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004346 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004347
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004348 undef_smacro(mname, op == PP_UNDEFALIAS);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004349 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004350
H. Peter Anvin8b262472019-02-26 14:00:54 -08004351 case PP_DEFSTR:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004352 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004353 goto done;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004354
H. Peter Anvin9e200162008-06-04 17:23:14 -07004355 last = tline;
4356 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004357 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004358
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004359 tline = zap_white(tline);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004360 q = detoken(tline, false);
4361 macro_start = make_tok_qstr(NULL, q);
4362 nasm_free(q);
H. Peter Anvin9e200162008-06-04 17:23:14 -07004363
4364 /*
4365 * We now have a macro name, an implicit parameter count of
4366 * zero, and a string token to use as an expansion. Create
4367 * and store an SMacro.
4368 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004369 define_smacro(mname, casesense, macro_start, NULL);
4370 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004371
H. Peter Anvin8b262472019-02-26 14:00:54 -08004372 case PP_DEFTOK:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004373 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004374 goto done;
4375
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004376 last = tline;
4377 tline = expand_smacro(tline->next);
4378 last->next = NULL;
4379
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004380 t = skip_white(tline);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004381 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004382 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004383 nasm_nonfatal("`%s' requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004384 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004385 goto done;
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004386 }
4387
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04004388 /*
4389 * Convert the string to a token stream. Note that smacros
4390 * are stored with the token stream reversed, so we have to
4391 * reverse the output of tokenize().
4392 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07004393 macro_start = reverse_tokens(tokenize(unquote_token_cstr(t)));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004394
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004395 /*
4396 * We now have a macro name, an implicit parameter count of
4397 * zero, and a numeric token to use as an expansion. Create
4398 * and store an SMacro.
4399 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004400 define_smacro(mname, casesense, macro_start, NULL);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004401 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004402 break;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004403
H. Peter Anvin418ca702008-05-30 10:42:30 -07004404 case PP_PATHSEARCH:
4405 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07004406 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004407
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004408 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004409 goto done;
4410
H. Peter Anvin418ca702008-05-30 10:42:30 -07004411 last = tline;
4412 tline = expand_smacro(tline->next);
4413 last->next = NULL;
4414
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004415 t = skip_white(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004416 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004417 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004418 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004419 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004420 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004421 }
4422 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004423 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004424
4425 p = unquote_token_cstr(t);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004426
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07004427 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07004428 if (!found_path)
4429 found_path = p;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004430 macro_start = make_tok_qstr(NULL, found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004431
4432 /*
4433 * We now have a macro name, an implicit parameter count of
4434 * zero, and a string token to use as an expansion. Create
4435 * and store an SMacro.
4436 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004437 define_smacro(mname, casesense, macro_start, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004438 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004439 break;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004440 }
4441
H. Peter Anvine2c80182005-01-15 22:15:51 +00004442 case PP_STRLEN:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004443 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004444 goto done;
4445
H. Peter Anvine2c80182005-01-15 22:15:51 +00004446 last = tline;
4447 tline = expand_smacro(tline->next);
4448 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004449
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004450 t = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004451 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004452 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004453 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004454 free_tlist(tline);
4455 free_tlist(origline);
4456 return DIRECTIVE_FOUND;
4457 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004458
H. Peter Anvin8571f062019-09-23 16:40:03 -07004459 unquote_token(t);
4460 macro_start = make_tok_num(NULL, t->len);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004461
H. Peter Anvine2c80182005-01-15 22:15:51 +00004462 /*
4463 * We now have a macro name, an implicit parameter count of
4464 * zero, and a numeric token to use as an expansion. Create
4465 * and store an SMacro.
4466 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004467 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004468 free_tlist(tline);
4469 free_tlist(origline);
4470 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004471
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004472 case PP_STRCAT:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004473 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004474 goto done;
4475
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004476 last = tline;
4477 tline = expand_smacro(tline->next);
4478 last->next = NULL;
4479
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004480 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004481 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004482 switch (t->type) {
4483 case TOK_WHITESPACE:
4484 break;
4485 case TOK_STRING:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004486 unquote_token(t);
4487 len += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004488 break;
4489 case TOK_OTHER:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004490 if (tok_is(t, ',')) /* permit comma separators */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004491 break;
4492 /* else fall through */
4493 default:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004494 nasm_nonfatal("non-string passed to `%s': %s", dname,
4495 tok_text(t));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004496 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004497 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004498 }
4499 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004500
H. Peter Anvin (Intel)f770ce82019-10-17 18:22:43 -07004501 q = qbuf = nasm_malloc(len+1);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004502 list_for_each(t, tline) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004503 if (t->type == TOK_INTERNAL_STRING)
4504 q = mempcpy(q, tok_text(t), t->len);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004505 }
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07004506 *q = '\0';
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004507
4508 /*
4509 * We now have a macro name, an implicit parameter count of
4510 * zero, and a numeric token to use as an expansion. Create
4511 * and store an SMacro.
4512 */
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07004513 macro_start = make_tok_qstr_len(NULL, qbuf, len);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004514 nasm_free(qbuf);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004515 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004516 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004517 break;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004518
H. Peter Anvine2c80182005-01-15 22:15:51 +00004519 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004520 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004521 int64_t start, count;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004522 const char *txt;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004523 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07004524
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004525 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004526 goto done;
4527
H. Peter Anvine2c80182005-01-15 22:15:51 +00004528 last = tline;
4529 tline = expand_smacro(tline->next);
4530 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004531
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04004532 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004533 t = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004534
4535 t = skip_white(t);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004536
H. Peter Anvine2c80182005-01-15 22:15:51 +00004537 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004538 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004539 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004540 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004541 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004542 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004543
H. Peter Anvin8b262472019-02-26 14:00:54 -08004544 pps.tptr = t->next;
4545 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004546 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004547 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004548 if (!evalresult) {
4549 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004550 goto done;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004551 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004552 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004553 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004554 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004555 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004556 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004557
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004558 pps.tptr = skip_white(pps.tptr);
H. Peter Anvin8b262472019-02-26 14:00:54 -08004559 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004560 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004561 } else {
4562 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004563 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004564 if (!evalresult) {
4565 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004566 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004567 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004568 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004569 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004570 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004571 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004572 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004573 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004574
H. Peter Anvin8571f062019-09-23 16:40:03 -07004575 unquote_token(t);
4576 len = t->len;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004577
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04004578 /* make start and count being in range */
4579 if (start < 0)
4580 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004581 if (count < 0)
4582 count = len + count + 1 - start;
4583 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04004584 count = len - start;
4585 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004586 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004587
H. Peter Anvin8571f062019-09-23 16:40:03 -07004588 txt = (start < 0) ? "" : tok_text(t) + start;
4589 len = count;
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07004590 macro_start = make_tok_qstr_len(NULL, txt, len);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004591
H. Peter Anvine2c80182005-01-15 22:15:51 +00004592 /*
4593 * We now have a macro name, an implicit parameter count of
4594 * zero, and a numeric token to use as an expansion. Create
4595 * and store an SMacro.
4596 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004597 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004598 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004599 break;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004600 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004601
H. Peter Anvin8b262472019-02-26 14:00:54 -08004602 case PP_ASSIGN:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004603 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004604 goto done;
4605
H. Peter Anvine2c80182005-01-15 22:15:51 +00004606 last = tline;
4607 tline = expand_smacro(tline->next);
4608 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004609
H. Peter Anvin8b262472019-02-26 14:00:54 -08004610 pps.tptr = tline;
4611 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004612 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004613 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004614 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004615 if (!evalresult)
4616 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004617
H. Peter Anvine2c80182005-01-15 22:15:51 +00004618 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004619 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00004620
H. Peter Anvine2c80182005-01-15 22:15:51 +00004621 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004622 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004623 free_tlist(origline);
4624 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004625 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004626
H. Peter Anvin8571f062019-09-23 16:40:03 -07004627 macro_start = make_tok_num(NULL, reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00004628
H. Peter Anvine2c80182005-01-15 22:15:51 +00004629 /*
4630 * We now have a macro name, an implicit parameter count of
4631 * zero, and a numeric token to use as an expansion. Create
4632 * and store an SMacro.
4633 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004634 define_smacro(mname, casesense, macro_start, NULL);
4635 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004636
H. Peter Anvind2354082019-08-27 16:38:48 -07004637 case PP_ALIASES:
4638 tline = tline->next;
4639 tline = expand_smacro(tline);
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07004640 ppopt.noaliases = !pp_get_boolean_option(tline, !ppopt.noaliases);
H. Peter Anvind2354082019-08-27 16:38:48 -07004641 break;
4642
H. Peter Anvine2c80182005-01-15 22:15:51 +00004643 case PP_LINE:
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07004644 nasm_panic("`%s' directive not preprocessed early", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004645 break;
4646 }
4647
4648done:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004649 free_tlist(origline);
4650 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004651}
4652
4653/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00004654 * Ensure that a macro parameter contains a condition code and
4655 * nothing else. Return the condition code index if so, or -1
4656 * otherwise.
4657 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004658static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004659{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004660 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004661
H. Peter Anvin25a99342007-09-22 17:45:45 -07004662 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004663 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07004664
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004665 t = skip_white(t);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004666 if (!tok_type(t, TOK_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004667 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004668 tt = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004669 tt = skip_white(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004670 if (tok_isnt(tt, ','))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004671 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004672
H. Peter Anvin8571f062019-09-23 16:40:03 -07004673 return bsii(tok_text(t), (const char **)conditions,
4674 ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00004675}
4676
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004677static inline bool pp_concat_match(const Token *t, unsigned int mask)
4678{
4679 return t && (PP_CONCAT_MASK(t->type) & mask);
4680}
4681
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004682/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004683 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004684 * pasting, if @handle_explicit passed then explicit pasting
4685 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004686 * The @m array can contain a series of token types which are
4687 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004688 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004689static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004690 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07004691{
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004692 Token *tok, *t, *next, **prev_next, **prev_nonspace;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004693 bool pasted = false;
4694 char *buf, *p;
4695 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07004696
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004697 /*
4698 * The last token before pasting. We need it
4699 * to be able to connect new handled tokens.
4700 * In other words if there were a tokens stream
4701 *
4702 * A -> B -> C -> D
4703 *
4704 * and we've joined tokens B and C, the resulting
4705 * stream should be
4706 *
4707 * A -> BC -> D
4708 */
4709 tok = *head;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004710 prev_next = prev_nonspace = head;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004711
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004712 if (tok_white(tok) || tok_type(tok, TOK_PASTE))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004713 prev_nonspace = NULL;
4714
4715 while (tok && (next = tok->next)) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004716 bool did_paste = false;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004717
4718 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07004719 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004720 /* Zap redundant whitespaces */
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004721 tok->next = next = zap_white(next);
H. Peter Anvind784a082009-04-20 14:01:18 -07004722 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004723
4724 case TOK_PASTE:
4725 /* Explicit pasting */
4726 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004727 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004728
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004729 /* Left pasting token is start of line */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004730 if (!prev_nonspace) {
4731 nasm_nonfatal("No lvalue found on pasting");
4732 tok = delete_Token(tok);
4733 break;
4734 }
4735
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004736 did_paste = true;
4737
4738 prev_next = prev_nonspace;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004739 t = *prev_nonspace;
4740
4741 /* Delete leading whitespace */
4742 next = zap_white(t->next);
4743
4744 /* Delete the %+ token itself */
4745 nasm_assert(next == tok);
4746 next = delete_Token(next);
4747
4748 /* Delete trailing whitespace */
4749 next = zap_white(next);
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004750
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004751 /*
4752 * No ending token, this might happen in two
4753 * cases
4754 *
4755 * 1) There indeed no right token at all
4756 * 2) There is a bare "%define ID" statement,
4757 * and @ID does expand to whitespace.
4758 *
4759 * So technically we need to do a grammar analysis
4760 * in another stage of parsing, but for now lets don't
4761 * change the behaviour people used to. Simply allow
4762 * whitespace after paste token.
4763 */
4764 if (!next) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004765 *prev_nonspace = tok = NULL; /* End of line */
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004766 break;
4767 }
4768
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004769 p = buf = nasm_malloc(t->len + next->len + 1);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004770 p = mempcpy(p, tok_text(t), t->len);
4771 p = mempcpy(p, tok_text(next), next->len);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004772 *p = '\0';
4773 delete_Token(t);
4774 t = tokenize(buf);
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004775 nasm_free(buf);
4776
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004777 if (unlikely(!t)) {
4778 /*
4779 * No output at all? Replace with a single whitespace.
4780 * This should never happen.
4781 */
4782 t = new_White(NULL);
4783 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004784
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004785 *prev_nonspace = tok = t;
4786 while (t->next)
4787 t = t->next; /* Find the last token produced */
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004788
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004789 /* Delete the second token and attach to the end of the list */
4790 t->next = delete_Token(next);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004791
4792 /* We want to restart from the head of the pasted token */
4793 next = tok;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004794 break;
4795
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004796 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004797 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004798 for (i = 0; i < mnum; i++) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004799 if (pp_concat_match(tok, m[i].mask_head))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004800 break;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004801 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004802
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004803 if (i >= mnum)
4804 break;
4805
4806 len = tok->len;
4807 while (pp_concat_match(next, m[i].mask_tail)) {
4808 len += next->len;
4809 next = next->next;
4810 }
4811
4812 /* No match or no text to process */
4813 if (len == tok->len)
4814 break;
4815
4816 p = buf = nasm_malloc(len + 1);
4817 while (tok != next) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004818 p = mempcpy(p, tok_text(tok), tok->len);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004819 tok = delete_Token(tok);
4820 }
4821 *p = '\0';
4822 *prev_next = tok = t = tokenize(buf);
4823 nasm_free(buf);
4824
4825 /*
4826 * Connect pasted into original stream,
4827 * ie A -> new-tokens -> B
4828 */
4829 while (t->next)
4830 t = t->next;
4831 t->next = next;
4832 prev_next = prev_nonspace = &t->next;
4833 did_paste = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004834 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004835 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004836
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004837 if (did_paste) {
4838 pasted = true;
4839 } else {
4840 prev_next = &tok->next;
4841 if (next && next->type != TOK_WHITESPACE && next->type != TOK_PASTE)
4842 prev_nonspace = prev_next;
4843 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004844
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004845 tok = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004846 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004847
4848 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07004849}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004850
4851/*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004852 * Computes the proper rotation of mmacro parameters
4853 */
4854static int mmac_rotate(const MMacro *mac, unsigned int n)
4855{
4856 if (--n < mac->nparam)
4857 n = (n + mac->rotate) % mac->nparam;
4858
4859 return n+1;
4860}
4861
4862/*
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004863 * expands to a list of tokens from %{x:y}
4864 */
H. Peter Anvin (Intel)c0d0f882020-06-30 17:33:39 -07004865static void expand_mmac_params_range(MMacro *mac, Token *tline, Token ***tail)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004866{
H. Peter Anvin (Intel)e99a9462020-06-30 11:51:41 -07004867 Token *t;
4868 const char *arg = tok_text(tline) + 1;
4869 int fst, lst, incr, n;
4870 int parsed;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004871
H. Peter Anvin (Intel)e99a9462020-06-30 11:51:41 -07004872 parsed = sscanf(arg, "%d:%d", &fst, &lst);
4873 nasm_assert(parsed == 2);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004874
4875 /*
4876 * only macros params are accounted so
4877 * if someone passes %0 -- we reject such
4878 * value(s)
4879 */
4880 if (lst == 0 || fst == 0)
4881 goto err;
4882
4883 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004884 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
4885 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004886 goto err;
4887
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004888 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
4889 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004890
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004891 /*
H. Peter Anvin (Intel)e99a9462020-06-30 11:51:41 -07004892 * It will be at least one parameter, as we can loop
4893 * in either direction.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004894 */
H. Peter Anvin (Intel)e99a9462020-06-30 11:51:41 -07004895 incr = (fst < lst) ? 1 : -1;
4896
4897 while (true) {
4898 n = mmac_rotate(mac, fst);
4899 dup_tlistn(mac->params[n], mac->paramlen[n], tail);
4900 if (fst == lst)
4901 break;
4902 t = make_tok_char(NULL, ',');
4903 **tail = t;
4904 *tail = &t->next;
4905 fst += incr;
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004906 }
4907
H. Peter Anvin (Intel)e99a9462020-06-30 11:51:41 -07004908 return;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004909
4910err:
H. Peter Anvin (Intel)e99a9462020-06-30 11:51:41 -07004911 nasm_nonfatal("`%%{%s}': macro parameters out of range", arg);
4912 return;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004913}
4914
H. Peter Anvin76690a12002-04-30 20:52:49 +00004915/*
4916 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004917 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004918 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004919 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004920static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004921{
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004922 Token **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004923 bool changed = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004924 MMacro *mac = istk->mstk.mmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004925
4926 tail = &thead;
4927 thead = NULL;
4928
H. Peter Anvine2c80182005-01-15 22:15:51 +00004929 while (tline) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004930 bool change;
H. Peter Anvin (Intel)a762cd42020-06-01 11:49:08 -07004931 bool err_not_mac = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004932 Token *t = tline;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004933 const char *text = tok_text(t);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004934 int type = t->type;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004935
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004936 tline = tline->next;
4937 t->next = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004938
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004939 switch (type) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004940 case TOK_LOCAL_SYMBOL:
H. Peter Anvin (Intel)a762cd42020-06-01 11:49:08 -07004941 change = true;
4942
4943 if (!mac) {
4944 err_not_mac = true;
4945 break;
4946 }
4947
H. Peter Anvin8571f062019-09-23 16:40:03 -07004948 type = TOK_ID;
4949 text = nasm_asprintf("..@%"PRIu64".%s", mac->unique, text+2);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004950 break;
4951 case TOK_MMACRO_PARAM:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004952 {
4953 Token *tt = NULL;
4954
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004955 change = true;
4956
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004957 if (!mac) {
H. Peter Anvin (Intel)a762cd42020-06-01 11:49:08 -07004958 err_not_mac = true;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004959 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004960 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004961
4962 if (strchr(text, ':')) {
H. Peter Anvin (Intel)e99a9462020-06-30 11:51:41 -07004963 /* It is a range */
4964 expand_mmac_params_range(mac, t, &tail);
4965 text = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004966 break;
4967 }
4968
4969 switch (text[1]) {
4970 /*
4971 * We have to make a substitution of one of the
4972 * forms %1, %-1, %+1, %%foo, %0, %00.
4973 */
4974 case '0':
4975 if (!text[2]) {
4976 type = TOK_NUMBER;
4977 text = nasm_asprintf("%d", mac->nparam);
4978 break;
4979 }
4980 if (text[2] != '0' || text[3])
4981 goto invalid;
4982 /* a possible captured label == mac->params[0] */
4983 /* fall through */
4984 default:
4985 {
4986 unsigned long n;
4987 char *ep;
4988
4989 n = strtoul(text + 1, &ep, 10);
4990 if (unlikely(*ep))
4991 goto invalid;
4992
4993 if (n <= mac->nparam) {
4994 n = mmac_rotate(mac, n);
4995 dup_tlistn(mac->params[n], mac->paramlen[n], &tail);
4996 }
4997 text = NULL;
4998 break;
4999 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005000 case '-':
5001 case '+':
5002 {
5003 int cc;
5004 unsigned long n;
5005 char *ep;
5006
H. Peter Anvin8571f062019-09-23 16:40:03 -07005007 n = strtoul(tok_text(t) + 2, &ep, 10);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005008 if (unlikely(*ep))
5009 goto invalid;
5010
Chang S. Bae057b8322020-04-18 23:11:21 +00005011 if (n && n <= mac->nparam) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005012 n = mmac_rotate(mac, n);
5013 tt = mac->params[n];
5014 }
5015 cc = find_cc(tt);
5016 if (cc == -1) {
5017 nasm_nonfatal("macro parameter `%s' is not a condition code",
H. Peter Anvin8571f062019-09-23 16:40:03 -07005018 tok_text(t));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005019 text = NULL;
5020 break;
5021 }
5022
5023 type = TOK_ID;
5024 if (text[1] == '-') {
5025 int ncc = inverse_ccs[cc];
5026 if (unlikely(ncc == -1)) {
5027 nasm_nonfatal("condition code `%s' is not invertible",
5028 conditions[cc]);
5029 break;
5030 }
5031 cc = ncc;
5032 }
5033 text = nasm_strdup(conditions[cc]);
5034 break;
5035 }
5036
5037 invalid:
5038 nasm_nonfatal("invalid macro parameter: `%s'", text);
5039 text = NULL;
5040 break;
5041 }
5042 break;
5043 }
5044
5045 case TOK_PREPROC_Q:
5046 if (mac) {
5047 type = TOK_ID;
5048 text = nasm_strdup(mac->iname);
5049 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07005050 } else {
5051 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005052 }
5053 break;
5054
5055 case TOK_PREPROC_QQ:
5056 if (mac) {
5057 type = TOK_ID;
5058 text = nasm_strdup(mac->name);
5059 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07005060 } else {
5061 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005062 }
5063 break;
5064
5065 case TOK_INDIRECT:
5066 {
5067 Token *tt;
5068
H. Peter Anvin8571f062019-09-23 16:40:03 -07005069 tt = tokenize(tok_text(t));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005070 tt = expand_mmac_params(tt);
5071 tt = expand_smacro(tt);
5072 /* Why dup_tlist() here? We should own tt... */
5073 dup_tlist(tt, &tail);
5074 text = NULL;
5075 change = true;
5076 break;
5077 }
5078
5079 default:
5080 change = false;
5081 break;
5082 }
5083
H. Peter Anvin (Intel)a762cd42020-06-01 11:49:08 -07005084 if (err_not_mac) {
5085 nasm_nonfatal("`%s': not in a macro call", text);
5086 text = NULL;
5087 change = true;
5088 }
5089
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005090 if (change) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005091 if (!text) {
5092 delete_Token(t);
5093 } else {
5094 *tail = t;
5095 tail = &t->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005096 set_text(t, text, tok_strlen(text));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005097 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005098 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005099 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005100 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005101 *tail = t;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005102 tail = &t->next;
5103 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00005104 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005105
H. Peter Anvineba20a72002-04-30 20:53:55 +00005106 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07005107
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005108 if (changed) {
5109 const struct tokseq_match t[] = {
5110 {
5111 PP_CONCAT_MASK(TOK_ID) |
5112 PP_CONCAT_MASK(TOK_FLOAT), /* head */
5113 PP_CONCAT_MASK(TOK_ID) |
5114 PP_CONCAT_MASK(TOK_NUMBER) |
5115 PP_CONCAT_MASK(TOK_FLOAT) |
5116 PP_CONCAT_MASK(TOK_OTHER) /* tail */
5117 },
5118 {
5119 PP_CONCAT_MASK(TOK_NUMBER), /* head */
5120 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
5121 }
5122 };
5123 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
5124 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07005125
H. Peter Anvin76690a12002-04-30 20:52:49 +00005126 return thead;
5127}
5128
H. Peter Anvin322bee02019-08-10 01:38:06 -07005129static Token *expand_smacro_noreset(Token * tline);
H. Peter Anvin322bee02019-08-10 01:38:06 -07005130
H. Peter Anvin76690a12002-04-30 20:52:49 +00005131/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005132 * Expand *one* single-line macro instance. If the first token is not
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005133 * a macro at all, it is simply copied to the output and the pointer
5134 * advanced. tpp should be a pointer to a pointer (usually the next
5135 * pointer of the previous token) to the first token. **tpp is updated
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005136 * to point to the first token of the expansion, and *tpp updated to
5137 * point to the next pointer of the last token of the expansion.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005138 *
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005139 * If the expansion is empty, *tpp will be unchanged but **tpp will
5140 * be advanced past the macro call.
5141 *
H. Peter Anvin322bee02019-08-10 01:38:06 -07005142 * Return the macro expanded, or NULL if no expansion took place.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005143 */
H. Peter Anvin322bee02019-08-10 01:38:06 -07005144static SMacro *expand_one_smacro(Token ***tpp)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005145{
5146 Token **params = NULL;
5147 const char *mname;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005148 Token *mstart = **tpp;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005149 Token *tline = mstart;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005150 SMacro *head, *m;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005151 int i;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005152 Token *t, *tup, *tafter;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005153 int nparam = 0;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005154 bool cond_comma;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005155
5156 if (!tline)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005157 return false; /* Empty line, nothing to do */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005158
H. Peter Anvin8571f062019-09-23 16:40:03 -07005159 mname = tok_text(mstart);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005160
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005161 smacro_deadman.total--;
H. Peter Anvin322bee02019-08-10 01:38:06 -07005162 smacro_deadman.levels--;
5163
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005164 if (unlikely(smacro_deadman.total < 0 || smacro_deadman.levels < 0)) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07005165 if (unlikely(!smacro_deadman.triggered)) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005166 nasm_nonfatal("interminable macro recursion");
H. Peter Anvin322bee02019-08-10 01:38:06 -07005167 smacro_deadman.triggered = true;
5168 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005169 goto not_a_macro;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005170 } else if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005171 head = (SMacro *)hash_findix(&smacros, mname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005172 } else if (tline->type == TOK_LOCAL_MACRO) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005173 Context *ctx = get_ctx(mname, &mname);
5174 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
5175 } else {
5176 goto not_a_macro;
5177 }
5178
5179 /*
5180 * We've hit an identifier of some sort. First check whether the
5181 * identifier is a single-line macro at all, then think about
5182 * checking for parameters if necessary.
5183 */
5184 list_for_each(m, head) {
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005185 if (unlikely(m->alias && ppopt.noaliases))
H. Peter Anvind2354082019-08-27 16:38:48 -07005186 continue;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005187 if (!mstrcmp(m->name, mname, m->casesense))
5188 break;
5189 }
5190
5191 if (!m) {
5192 goto not_a_macro;
5193 }
5194
5195 /* Parse parameters, if applicable */
5196
5197 params = NULL;
5198 nparam = 0;
5199
5200 if (m->nparam == 0) {
5201 /*
5202 * Simple case: the macro is parameterless.
5203 * Nothing to parse; the expansion code will
5204 * drop the macro name token.
5205 */
5206 } else {
5207 /*
5208 * Complicated case: at least one macro with this name
5209 * exists and takes parameters. We must find the
5210 * parameters in the call, count them, find the SMacro
5211 * that corresponds to that form of the macro call, and
5212 * substitute for the parameters when we expand. What a
5213 * pain.
5214 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005215 Token *t;
5216 int paren, brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005217
5218 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005219 tline = skip_white(tline);
5220 if (!tok_is(tline, '(')) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005221 /*
5222 * This macro wasn't called with parameters: ignore
5223 * the call. (Behaviour borrowed from gnu cpp.)
5224 */
5225 goto not_a_macro;
5226 }
5227
5228 paren = 1;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005229 nparam = 1;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005230 brackets = 0;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005231 t = tline; /* tline points to leading ( */
5232
5233 while (paren) {
5234 t = t->next;
5235
5236 if (!t) {
5237 nasm_nonfatal("macro call expects terminating `)'");
5238 goto not_a_macro;
5239 }
5240
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005241 if (t->type != TOK_OTHER || t->len != 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005242 continue;
5243
H. Peter Anvin8571f062019-09-23 16:40:03 -07005244 switch (t->text.a[0]) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005245 case ',':
H. Peter Anvinbd00f252020-06-04 21:05:01 -07005246 if (!brackets && paren == 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005247 nparam++;
5248 break;
5249
5250 case '{':
5251 brackets++;
5252 break;
5253
5254 case '}':
5255 if (brackets > 0)
5256 brackets--;
5257 break;
5258
5259 case '(':
5260 if (!brackets)
5261 paren++;
5262 break;
5263
5264 case ')':
5265 if (!brackets)
5266 paren--;
5267 break;
5268
5269 default:
5270 break; /* Normal token */
5271 }
5272 }
5273
5274 /*
5275 * Look for a macro matching in both name and parameter count.
5276 * We already know any matches cannot be anywhere before the
5277 * current position of "m", so there is no reason to
5278 * backtrack.
5279 */
5280 while (1) {
5281 if (!m) {
5282 /*!
5283 *!macro-params-single [on] single-line macro calls with wrong parameter count
5284 *! warns about \i{single-line macros} being invoked
5285 *! with the wrong number of parameters.
5286 */
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005287 nasm_warn(WARN_MACRO_PARAMS_SINGLE|ERR_HOLD,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005288 "single-line macro `%s' exists, "
5289 "but not taking %d parameter%s",
5290 mname, nparam, (nparam == 1) ? "" : "s");
5291 goto not_a_macro;
5292 }
5293
5294 if (!mstrcmp(m->name, mname, m->casesense)) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005295 if (nparam == m->nparam)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005296 break; /* It's good */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005297 if (m->greedy && nparam >= m->nparam-1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005298 break; /* Also good */
5299 }
5300 m = m->next;
5301 }
5302 }
5303
5304 if (m->in_progress)
5305 goto not_a_macro;
5306
5307 /* Expand the macro */
5308 m->in_progress = true;
5309
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005310 if (nparam) {
5311 /* Extract parameters */
5312 Token **phead, **pep;
5313 int white = 0;
5314 int brackets = 0;
5315 int paren;
5316 bool bracketed = false;
5317 bool bad_bracket = false;
5318 enum sparmflags flags;
5319
5320 nparam = m->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005321 paren = 1;
5322 nasm_newn(params, nparam);
5323 i = 0;
5324 flags = m->params[i].flags;
5325 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005326 *pep = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005327
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005328 while (paren) {
5329 bool skip;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005330 char ch;
5331
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005332 tline = tline->next;
5333
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005334 if (!tline)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005335 nasm_nonfatal("macro call expects terminating `)'");
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005336
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005337 ch = 0;
5338 skip = false;
5339
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005340
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005341 switch (tline->type) {
5342 case TOK_OTHER:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005343 if (tline->len == 1)
H. Peter Anvin8571f062019-09-23 16:40:03 -07005344 ch = tline->text.a[0];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005345 break;
5346
5347 case TOK_WHITESPACE:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005348 if (!(flags & SPARM_NOSTRIP)) {
5349 if (brackets || *phead)
5350 white++; /* Keep interior whitespace */
5351 skip = true;
5352 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005353 break;
5354
5355 default:
5356 break;
5357 }
5358
5359 switch (ch) {
5360 case ',':
H. Peter Anvinbd00f252020-06-04 21:05:01 -07005361 if (!brackets && paren == 1 && !(flags & SPARM_GREEDY)) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005362 i++;
5363 nasm_assert(i < nparam);
5364 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005365 *pep = NULL;
5366 bracketed = false;
5367 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005368 flags = m->params[i].flags;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005369 }
5370 break;
5371
5372 case '{':
5373 if (!bracketed) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005374 bracketed = !*phead && !(flags & SPARM_NOSTRIP);
5375 skip = bracketed;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005376 }
5377 brackets++;
5378 break;
5379
5380 case '}':
5381 if (brackets > 0) {
5382 if (!--brackets)
5383 skip = bracketed;
5384 }
5385 break;
5386
5387 case '(':
5388 if (!brackets)
5389 paren++;
5390 break;
5391
5392 case ')':
5393 if (!brackets) {
5394 paren--;
5395 if (!paren) {
5396 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005397 i++; /* Found last argument */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005398 }
5399 }
5400 break;
5401
5402 default:
5403 break; /* Normal token */
5404 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005405
5406 if (!skip) {
5407 Token *t;
5408
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005409 bad_bracket |= bracketed && !brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005410
5411 if (white) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005412 *pep = t = new_White(NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005413 pep = &t->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005414 white = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005415 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005416 *pep = t = dup_Token(NULL, tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005417 pep = &t->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005418 }
5419 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005420
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005421 /*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005422 * Possible further processing of parameters. Note that the
5423 * ordering matters here.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005424 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005425 for (i = 0; i < nparam; i++) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005426 enum sparmflags flags = m->params[i].flags;
5427
5428 if (flags & SPARM_EVAL) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005429 /* Evaluate this parameter as a number */
5430 struct ppscan pps;
5431 struct tokenval tokval;
5432 expr *evalresult;
5433 Token *eval_param;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005434
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005435 pps.tptr = eval_param = expand_smacro_noreset(params[i]);
5436 pps.ntokens = -1;
5437 tokval.t_type = TOKEN_INVALID;
5438 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005439
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005440 free_tlist(eval_param);
5441 params[i] = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005442
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005443 if (!evalresult) {
5444 /* Nothing meaningful to do */
5445 } else if (tokval.t_type) {
5446 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
5447 } else if (!is_simple(evalresult)) {
5448 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
5449 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005450 params[i] = make_tok_num(NULL, reloc_value(evalresult));
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005451 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005452 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005453
5454 if (flags & SPARM_STR) {
5455 /* Convert expansion to a quoted string */
5456 char *arg;
5457 Token *qs;
5458
5459 qs = expand_smacro_noreset(params[i]);
5460 arg = detoken(qs, false);
5461 free_tlist(qs);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005462 params[i] = make_tok_qstr(NULL, arg);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005463 nasm_free(arg);
5464 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005465 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005466 }
5467
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005468 /* Note: we own the expansion this returns. */
5469 t = m->expand(m, params, nparam);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005470
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005471 tafter = tline->next; /* Skip past the macro call */
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005472 tline->next = NULL; /* Truncate list at the macro call end */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005473 tline = tafter;
5474
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005475 tup = NULL;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005476 cond_comma = false;
5477
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005478 while (t) {
5479 enum pp_token_type type = t->type;
5480 Token *tnext = t->next;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005481
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005482 switch (type) {
5483 case TOK_PREPROC_Q:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005484 delete_Token(t);
5485 t = dup_Token(tline, mstart);
5486 break;
5487
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005488 case TOK_PREPROC_QQ:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005489 {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005490 size_t mlen = strlen(m->name);
5491 size_t len;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005492 char *p;
5493
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005494 t->type = mstart->type;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005495 if (t->type == TOK_LOCAL_MACRO) {
5496 const char *psp; /* prefix start pointer */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005497 const char *pep; /* prefix end pointer */
H. Peter Anvin8571f062019-09-23 16:40:03 -07005498 size_t plen;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005499
H. Peter Anvin8571f062019-09-23 16:40:03 -07005500 psp = tok_text(mstart);
5501 get_ctx(psp, &pep);
5502 plen = pep - psp;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005503
H. Peter Anvin8571f062019-09-23 16:40:03 -07005504 len = mlen + plen;
5505 p = nasm_malloc(len + 1);
5506 p = mempcpy(p, psp, plen);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005507 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005508 len = mlen;
5509 p = nasm_malloc(len + 1);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005510 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07005511 p = mempcpy(p, m->name, mlen);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005512 *p = '\0';
H. Peter Anvin8571f062019-09-23 16:40:03 -07005513 set_text_free(t, p, len);
5514
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005515 t->next = tline;
5516 break;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005517 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005518
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005519 case TOK_COND_COMMA:
5520 delete_Token(t);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005521 t = cond_comma ? make_tok_char(tline, ',') : NULL;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005522 break;
5523
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005524 case TOK_ID:
5525 case TOK_PREPROC_ID:
H. Peter Anvin8571f062019-09-23 16:40:03 -07005526 case TOK_LOCAL_MACRO:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005527 {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005528 /*
5529 * Chain this into the target line *before* expanding,
5530 * that way we pick up any arguments to the new macro call,
5531 * if applicable.
5532 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005533 Token **tp = &t;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005534 t->next = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005535 expand_one_smacro(&tp);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005536 tline = *tp; /* First token left after any macro call */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005537 break;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005538 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005539 default:
5540 if (is_smac_param(t->type)) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005541 int param = smac_nparam(t->type);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005542 nasm_assert(!tup && param < nparam);
5543 delete_Token(t);
5544 t = NULL;
5545 tup = tnext;
5546 tnext = dup_tlist_reverse(params[param], NULL);
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005547 cond_comma = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005548 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005549 t->next = tline;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005550 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005551 }
5552
5553 if (t) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005554 Token *endt = tline;
5555
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005556 tline = t;
Chang S. Bae95e54a92020-02-06 14:39:22 -08005557 while (!cond_comma && t && t != endt) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005558 cond_comma = t->type != TOK_WHITESPACE;
Chang S. Bae95e54a92020-02-06 14:39:22 -08005559 t = t->next;
5560 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005561 }
5562
5563 if (tnext) {
5564 t = tnext;
5565 } else {
5566 t = tup;
5567 tup = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005568 }
5569 }
5570
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005571 **tpp = tline;
H. Peter Anvin (Intel)6e714962020-06-01 12:21:10 -07005572 for (t = tline; t && t != tafter; t = t->next)
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005573 *tpp = &t->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005574
5575 m->in_progress = false;
5576
5577 /* Don't do this until after expansion or we will clobber mname */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005578 free_tlist(mstart);
H. Peter Anvin322bee02019-08-10 01:38:06 -07005579 goto done;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005580
5581 /*
5582 * No macro expansion needed; roll back to mstart (if necessary)
H. Peter Anvin322bee02019-08-10 01:38:06 -07005583 * and then advance to the next input token. Note that this is
5584 * by far the common case!
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005585 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005586not_a_macro:
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005587 *tpp = &mstart->next;
H. Peter Anvin322bee02019-08-10 01:38:06 -07005588 m = NULL;
5589done:
5590 smacro_deadman.levels++;
5591 if (unlikely(params))
5592 free_tlist_array(params, nparam);
5593 return m;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005594}
5595
5596/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005597 * Expand all single-line macro calls made in the given line.
5598 * Return the expanded version of the line. The original is deemed
5599 * to be destroyed in the process. (In reality we'll just move
5600 * Tokens from input to output a lot of the time, rather than
5601 * actually bothering to destroy and replicate.)
5602 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005603static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005604{
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005605 smacro_deadman.total = nasm_limit[LIMIT_MACRO_TOKENS];
H. Peter Anvin322bee02019-08-10 01:38:06 -07005606 smacro_deadman.levels = nasm_limit[LIMIT_MACRO_LEVELS];
5607 smacro_deadman.triggered = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005608 return expand_smacro_noreset(tline);
5609}
5610
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005611static Token *expand_smacro_noreset(Token *org_tline)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005612{
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005613 Token *tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005614 bool expanded;
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005615 errhold errhold; /* Hold warning/errors during expansion */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005616
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005617 if (!org_tline)
5618 return NULL; /* Empty input */
5619
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005620 /*
5621 * Trick: we should avoid changing the start token pointer since it can
5622 * be contained in "next" field of other token. Because of this
5623 * we allocate a copy of first token and work with it; at the end of
5624 * routine we copy it back
5625 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005626 tline = dup_Token(org_tline->next, org_tline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005627
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005628 /*
5629 * Pretend that we always end up doing expansion on the first pass;
5630 * that way %+ get processed. However, if we process %+ before the
5631 * first pass, we end up with things like MACRO %+ TAIL trying to
5632 * look up the macro "MACROTAIL", which we don't want.
5633 */
5634 expanded = true;
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005635
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005636 while (true) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005637 static const struct tokseq_match tmatch[] = {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005638 {
5639 PP_CONCAT_MASK(TOK_ID) |
H. Peter Anvin8571f062019-09-23 16:40:03 -07005640 PP_CONCAT_MASK(TOK_LOCAL_MACRO) |
5641 PP_CONCAT_MASK(TOK_ENVIRON) |
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005642 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
5643 PP_CONCAT_MASK(TOK_ID) |
H. Peter Anvin8571f062019-09-23 16:40:03 -07005644 PP_CONCAT_MASK(TOK_LOCAL_MACRO) |
5645 PP_CONCAT_MASK(TOK_ENVIRON) |
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005646 PP_CONCAT_MASK(TOK_PREPROC_ID) |
5647 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
5648 }
5649 };
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005650 Token **tail = &tline;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005651
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005652 /*
5653 * We hold warnings/errors until we are done this this loop. It is
5654 * possible for nuisance warnings to appear that disappear on later
5655 * passes.
5656 */
5657 errhold = nasm_error_hold_push();
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005658
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005659 while (*tail) /* main token loop */
H. Peter Anvin322bee02019-08-10 01:38:06 -07005660 expanded |= !!expand_one_smacro(&tail);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005661
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005662 if (!expanded)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005663 break; /* Done! */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005664
5665 /*
5666 * Now scan the entire line and look for successive TOK_IDs
5667 * that resulted after expansion (they can't be produced by
5668 * tokenize()). The successive TOK_IDs should be concatenated.
5669 * Also we look for %+ tokens and concatenate the tokens
5670 * before and after them (without white spaces in between).
5671 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005672 if (!paste_tokens(&tline, tmatch, ARRAY_SIZE(tmatch), true))
5673 break; /* Done again! */
5674
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005675 nasm_error_hold_pop(errhold, false);
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005676 expanded = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005677 }
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005678 nasm_error_hold_pop(errhold, true);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005679
H. Peter Anvin8571f062019-09-23 16:40:03 -07005680 if (!tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005681 /*
5682 * The expression expanded to empty line;
5683 * we can't return NULL because of the "trick" above.
5684 * Just set the line to a single WHITESPACE token.
H. Peter Anvin8571f062019-09-23 16:40:03 -07005685 */
5686
5687 tline = new_White(NULL);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005688 }
5689
H. Peter Anvin8571f062019-09-23 16:40:03 -07005690 steal_Token(org_tline, tline);
5691 org_tline->next = tline->next;
5692 delete_Token(tline);
5693
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005694 return org_tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005695}
5696
5697/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005698 * Similar to expand_smacro but used exclusively with macro identifiers
5699 * right before they are fetched in. The reason is that there can be
5700 * identifiers consisting of several subparts. We consider that if there
5701 * are more than one element forming the name, user wants a expansion,
5702 * otherwise it will be left as-is. Example:
5703 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005704 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005705 *
5706 * the identifier %$abc will be left as-is so that the handler for %define
5707 * will suck it and define the corresponding value. Other case:
5708 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005709 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005710 *
5711 * In this case user wants name to be expanded *before* %define starts
5712 * working, so we'll expand %$abc into something (if it has a value;
5713 * otherwise it will be left as-is) then concatenate all successive
5714 * PP_IDs into one.
5715 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005716static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005717{
5718 Token *cur, *oldnext = NULL;
5719
H. Peter Anvin734b1882002-04-30 21:01:08 +00005720 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005721 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005722
5723 cur = tline;
5724 while (cur->next &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07005725 (cur->next->type == TOK_ID || cur->next->type == TOK_PREPROC_ID ||
5726 cur->next->type == TOK_LOCAL_MACRO || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005727 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005728
5729 /* If identifier consists of just one token, don't expand */
5730 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005731 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005732
H. Peter Anvine2c80182005-01-15 22:15:51 +00005733 if (cur) {
5734 oldnext = cur->next; /* Detach the tail past identifier */
5735 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005736 }
5737
H. Peter Anvin734b1882002-04-30 21:01:08 +00005738 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005739
H. Peter Anvine2c80182005-01-15 22:15:51 +00005740 if (cur) {
5741 /* expand_smacro possibly changhed tline; re-scan for EOL */
5742 cur = tline;
5743 while (cur && cur->next)
5744 cur = cur->next;
5745 if (cur)
5746 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005747 }
5748
5749 return tline;
5750}
5751
5752/*
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005753 * This is called from find_mmacro_in_list() after finding a suitable macro.
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005754 */
5755static MMacro *use_mmacro(MMacro *m, int *nparamp, Token ***paramsp)
5756{
5757 int nparam = *nparamp;
5758 Token **params = *paramsp;
5759
5760 /*
5761 * This one is right. Just check if cycle removal
5762 * prohibits us using it before we actually celebrate...
5763 */
5764 if (m->in_progress > m->max_depth) {
5765 if (m->max_depth > 0) {
5766 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
5767 m->max_depth);
5768 }
5769 nasm_free(params);
5770 *nparamp = 0;
5771 *paramsp = NULL;
5772 return NULL;
5773 }
5774
5775 /*
5776 * It's right, and we can use it. Add its default
5777 * parameters to the end of our list if necessary.
5778 */
5779 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
5780 int newnparam = m->nparam_min + m->ndefs;
5781 params = nasm_realloc(params, sizeof(*params) * (newnparam+2));
5782 memcpy(&params[nparam+1], &m->defaults[nparam+1-m->nparam_min],
5783 (newnparam - nparam) * sizeof(*params));
5784 nparam = newnparam;
5785 }
5786 /*
5787 * If we've gone over the maximum parameter count (and
5788 * we're in Plus mode), ignore parameters beyond
5789 * nparam_max.
5790 */
5791 if (m->plus && nparam > m->nparam_max)
5792 nparam = m->nparam_max;
5793
5794 /*
5795 * If nparam was adjusted above, make sure the list is still
5796 * NULL-terminated.
5797 */
5798 params[nparam+1] = NULL;
5799
5800 /* Done! */
5801 *paramsp = params;
5802 *nparamp = nparam;
5803 return m;
5804}
5805
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005806/*
5807 * Search a macro list and try to find a match. If matching, call
5808 * use_mmacro() to set up the macro call. m points to the list of
5809 * search, which is_mmacro() sets to the first *possible* match.
5810 */
5811static MMacro *
5812find_mmacro_in_list(MMacro *m, const char *finding,
5813 int *nparamp, Token ***paramsp)
5814{
5815 int nparam = *nparamp;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005816
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005817 while (m) {
5818 if (m->nparam_min <= nparam
5819 && (m->plus || nparam <= m->nparam_max)) {
5820 /*
5821 * This one matches, use it.
5822 */
5823 return use_mmacro(m, nparamp, paramsp);
5824 }
5825
5826 /*
5827 * Otherwise search for the next one with a name match.
5828 */
5829 list_for_each(m, m->next) {
5830 if (!mstrcmp(m->name, finding, m->casesense))
5831 break;
5832 }
5833 }
5834
5835 return NULL;
5836}
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005837
5838/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005839 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005840 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005841 * to check for an initial label - that's taken care of in
5842 * expand_mmacro - but must check numbers of parameters. Guaranteed
5843 * to be called with tline->type == TOK_ID, so the putative macro
5844 * name is easy to find.
5845 */
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005846static MMacro *is_mmacro(Token * tline, int *nparamp, Token ***paramsp)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005847{
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005848 MMacro *head, *m, *found;
5849 Token **params, **comma;
5850 int raw_nparam, nparam;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005851 const char *finding = tok_text(tline);
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005852 bool empty_args = !tline->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005853
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005854 *nparamp = 0;
5855 *paramsp = NULL;
5856
H. Peter Anvin8571f062019-09-23 16:40:03 -07005857 head = (MMacro *) hash_findix(&mmacros, finding);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005858
5859 /*
5860 * Efficiency: first we see if any macro exists with the given
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005861 * name which isn't already excluded by macro cycle removal.
5862 * (The cycle removal test here helps optimize the case of wrapping
5863 * instructions, and is cheap to do here.)
5864 *
5865 * If not, we can return NULL immediately. _Then_ we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005866 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005867 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005868 */
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005869 list_for_each(m, head) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005870 if (!mstrcmp(m->name, finding, m->casesense) &&
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005871 (m->in_progress != 1 || m->max_depth > 0))
5872 break; /* Found something that needs consideration */
5873 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005874 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005875 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005876
5877 /*
5878 * OK, we have a potential macro. Count and demarcate the
5879 * parameters.
5880 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005881 comma = count_mmac_params(tline->next, nparamp, paramsp);
5882 raw_nparam = *nparamp;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005883
5884 /*
5885 * Search for an exact match. This cannot come *before* the m
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005886 * found in the list search before, so we can start there.
5887 *
5888 * If found is NULL and *paramsp has been cleared, then we
5889 * encountered an error for which we have already issued a
5890 * diagnostic, so we should not proceed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005891 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005892 found = find_mmacro_in_list(m, finding, nparamp, paramsp);
5893 if (!*paramsp)
5894 return NULL;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005895
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005896 nparam = *nparamp;
5897 params = *paramsp;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005898
5899 /*
5900 * Special weirdness: in NASM < 2.15, an expansion of
5901 * *only* whitespace, as can happen during macro expansion under
5902 * certain circumstances, is counted as zero arguments for the
5903 * purpose of %0, but one argument for the purpose of macro
5904 * matching! In particular, this affects:
5905 *
5906 * foobar %1
5907 *
5908 * ... with %1 being empty; this would call the one-argument
5909 * version of "foobar" with an empty argument, equivalent to
5910 *
5911 * foobar {%1}
5912 *
5913 * ... except that %0 would be set to 0 inside foobar, even if
5914 * foobar is declared with "%macro foobar 1" or equivalent!
5915 *
5916 * The proper way to do that is to define "%macro foobar 0-1".
5917 *
5918 * To be compatible without doing something too stupid, try to
5919 * match a zero-argument macro first, but if that fails, try
5920 * for a one-argument macro with the above behavior.
5921 *
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005922 * Furthermore, NASM < 2.15 will match stripping a tailing empty
5923 * argument, but in that case %0 *does* reflect that this argument
5924 * have been stripped; this is handled in count_mmac_params().
5925 *
5926 * To disable these insane legacy behaviors, use:
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005927 *
5928 * %pragma preproc sane_empty_expansion yes
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005929 *
5930 *!macro-params-legacy [on] improperly calling multi-line macro for legacy support
5931 *! warns about \i{multi-line macros} being invoked
5932 *! with the wrong number of parameters, but for bug-compatibility
5933 *! with NASM versions older than 2.15, NASM tried to fix up the
5934 *! parameters to match the legacy behavior and call the macro anyway.
5935 *! This can happen in certain cases where there are empty arguments
5936 *! without braces, sometimes as a result of macro expansion.
5937 *!-
5938 *! The legacy behavior is quite strange and highly context-dependent,
5939 *! and can be disabled with:
5940 *!-
H. Peter Anvin (Intel)de8817d2020-06-27 22:30:50 -07005941 *! \c %pragma preproc sane_empty_expansion true
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005942 *!-
5943 *! It is highly recommended to use this option in new code.
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005944 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005945 if (!ppopt.sane_empty_expansion) {
5946 if (!found) {
5947 if (raw_nparam == 0 && !empty_args) {
5948 /*
5949 * A single all-whitespace parameter as the only thing?
5950 * Look for a one-argument macro, but don't adjust
5951 * *nparamp.
5952 */
5953 int bogus_nparam = 1;
5954 params[2] = NULL;
5955 found = find_mmacro_in_list(m, finding, &bogus_nparam, paramsp);
5956 } else if (raw_nparam > 1 && comma) {
5957 Token *comma_tail = *comma;
5958
5959 /*
5960 * Drop the terminal argument and try again.
5961 * If we fail, we need to restore the comma to
5962 * preserve tlist.
5963 */
5964 *comma = NULL;
5965 *nparamp = raw_nparam - 1;
5966 found = find_mmacro_in_list(m, finding, nparamp, paramsp);
5967 if (found)
5968 free_tlist(comma_tail);
5969 else
5970 *comma = comma_tail;
5971 }
5972
5973 if (!*paramsp)
5974 return NULL;
5975 } else if (comma) {
5976 free_tlist(*comma);
5977 *comma = NULL;
5978 if (raw_nparam > found->nparam_min &&
5979 raw_nparam <= found->nparam_min + found->ndefs) {
5980 /* Replace empty argument with default parameter */
5981 params[raw_nparam] =
5982 found->defaults[raw_nparam - found->nparam_min];
5983 } else if (raw_nparam > found->nparam_max && found->plus) {
5984 /* Just drop the comma, don't adjust argument count */
5985 } else {
5986 /* Drop argument. This may cause nparam < nparam_min. */
5987 params[raw_nparam] = NULL;
5988 *nparamp = nparam = raw_nparam - 1;
5989 }
5990 }
5991
5992 if (found) {
5993 if (raw_nparam < found->nparam_min ||
5994 (raw_nparam > found->nparam_max && !found->plus)) {
5995 nasm_warn(WARN_MACRO_PARAMS_LEGACY,
5996 "improperly calling multi-line macro `%s' with %d parameters",
5997 found->name, raw_nparam);
5998 } else if (comma) {
5999 nasm_warn(WARN_MACRO_PARAMS_LEGACY,
6000 "dropping trailing empty parameter in call to multi-line macro `%s'", found->name);
6001 }
6002 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006003 }
6004
6005 /*
6006 * After all that, we didn't find one with the right number of
6007 * parameters. Issue a warning, and fail to expand the macro.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006008 *!
6009 *!macro-params-multi [on] multi-line macro calls with wrong parameter count
6010 *! warns about \i{multi-line macros} being invoked
6011 *! with the wrong number of parameters. See \k{mlmacover} for an
6012 *! example of why you might want to disable this warning.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006013 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07006014 if (found)
6015 return found;
6016
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006017 nasm_warn(WARN_MACRO_PARAMS_MULTI,
6018 "multi-line macro `%s' exists, but not taking %d parameter%s",
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07006019 finding, nparam, (nparam == 1) ? "" : "s");
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07006020 nasm_free(*paramsp);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006021 return NULL;
6022}
6023
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006024
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006025#if 0
6026
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006027/*
6028 * Save MMacro invocation specific fields in
6029 * preparation for a recursive macro expansion
6030 */
6031static void push_mmacro(MMacro *m)
6032{
6033 MMacroInvocation *i;
6034
6035 i = nasm_malloc(sizeof(MMacroInvocation));
6036 i->prev = m->prev;
6037 i->params = m->params;
6038 i->iline = m->iline;
6039 i->nparam = m->nparam;
6040 i->rotate = m->rotate;
6041 i->paramlen = m->paramlen;
6042 i->unique = m->unique;
6043 i->condcnt = m->condcnt;
6044 m->prev = i;
6045}
6046
6047
6048/*
6049 * Restore MMacro invocation specific fields that were
6050 * saved during a previous recursive macro expansion
6051 */
6052static void pop_mmacro(MMacro *m)
6053{
6054 MMacroInvocation *i;
6055
6056 if (m->prev) {
6057 i = m->prev;
6058 m->prev = i->prev;
6059 m->params = i->params;
6060 m->iline = i->iline;
6061 m->nparam = i->nparam;
6062 m->rotate = i->rotate;
6063 m->paramlen = i->paramlen;
6064 m->unique = i->unique;
6065 m->condcnt = i->condcnt;
6066 nasm_free(i);
6067 }
6068}
6069
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006070#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006071
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006072/*
H. Peter Anvin (Intel)ffe89dd2019-08-20 16:06:36 -07006073 * List an mmacro call with arguments (-Lm option)
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07006074 */
6075static void list_mmacro_call(const MMacro *m)
6076{
6077 const char prefix[] = " ;;; [macro] ";
6078 size_t namelen, size;
6079 char *buf, *p;
6080 unsigned int i;
6081 const Token *t;
6082
6083 namelen = strlen(m->iname);
6084 size = namelen + sizeof(prefix); /* Includes final null (from prefix) */
6085
6086 for (i = 1; i <= m->nparam; i++) {
6087 int j = 0;
6088 size += 3; /* Braces and space/comma */
6089 list_for_each(t, m->params[i]) {
6090 if (j++ >= m->paramlen[i])
6091 break;
6092 size += (t->type == TOK_WHITESPACE) ? 1 : t->len;
6093 }
6094 }
6095
6096 buf = p = nasm_malloc(size);
6097 p = mempcpy(p, prefix, sizeof(prefix) - 1);
6098 p = mempcpy(p, m->iname, namelen);
6099 *p++ = ' ';
6100
6101 for (i = 1; i <= m->nparam; i++) {
6102 int j = 0;
6103 *p++ = '{';
6104 list_for_each(t, m->params[i]) {
6105 if (j++ >= m->paramlen[i])
6106 break;
H. Peter Anvin8571f062019-09-23 16:40:03 -07006107 p = mempcpy(p, tok_text(t), t->len);
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07006108 }
6109 *p++ = '}';
6110 *p++ = ',';
6111 }
6112
6113 *--p = '\0'; /* Replace last delimeter with null */
6114 lfmt->line(LIST_MACRO, -1, buf);
6115 nasm_free(buf);
6116}
6117
6118/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006119 * Expand the multi-line macro call made by the given line, if
6120 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006121 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006122 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006123static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006124{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006125 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006126 Token *label = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006127 bool dont_prepend = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006128 Token **params, *t, *tt;
6129 MMacro *m;
6130 Line *l, *ll;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07006131 int i, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07006132 const char *mname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006133 int nparam = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006134
6135 t = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006136 t = skip_white(t);
6137 /* if (!tok_type(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07006138 if (!tok_type(t, TOK_ID) && !tok_type(t, TOK_LOCAL_MACRO))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006139 return 0;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006140 m = is_mmacro(t, &nparam, &params);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006141 if (m) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006142 mname = tok_text(t);
H. Peter Anvinc751e862008-06-09 10:18:45 -07006143 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006144 Token *last;
6145 /*
6146 * We have an id which isn't a macro call. We'll assume
6147 * it might be a label; we'll also check to see if a
6148 * colon follows it. Then, if there's another id after
6149 * that lot, we'll check it again for macro-hood.
6150 */
6151 label = last = t;
6152 t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006153 if (tok_white(t))
H. Peter Anvine2c80182005-01-15 22:15:51 +00006154 last = t, t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006155 if (tok_is(t, ':')) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006156 dont_prepend = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006157 last = t, t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006158 if (tok_white(t))
H. Peter Anvine2c80182005-01-15 22:15:51 +00006159 last = t, t = t->next;
6160 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006161 if (!tok_type(t, TOK_ID) || !(m = is_mmacro(t, &nparam, &params)))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006162 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006163 last->next = NULL;
H. Peter Anvin8571f062019-09-23 16:40:03 -07006164 mname = tok_text(t);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006165 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006166 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006167
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07006168 if (unlikely(mmacro_deadman.total >= nasm_limit[LIMIT_MMACROS] ||
6169 mmacro_deadman.levels >= nasm_limit[LIMIT_MACRO_LEVELS])) {
6170 if (!mmacro_deadman.triggered) {
6171 nasm_nonfatal("interminable multiline macro recursion");
6172 mmacro_deadman.triggered = true;
6173 }
6174 return 0;
6175 }
6176
6177 mmacro_deadman.total++;
6178 mmacro_deadman.levels++;
6179
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006180 /*
6181 * Fix up the parameters: this involves stripping leading and
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07006182 * trailing whitespace and stripping braces if they are present.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006183 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006184 nasm_newn(paramlen, nparam+1);
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07006185
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006186 for (i = 1; (t = params[i]); i++) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006187 bool braced = false;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08006188 int brace = 0;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006189 int white = 0;
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07006190 bool comma = !m->plus || i < nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006191
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006192 t = skip_white(t);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006193 if (tok_is(t, '{')) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006194 t = t->next;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006195 brace = 1;
6196 braced = true;
6197 comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006198 }
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006199
6200 params[i] = t;
6201 for (; t; t = t->next) {
6202 if (tok_white(t)) {
6203 white++;
6204 continue;
6205 }
6206
6207 if (t->type == TOK_OTHER && t->len == 1) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006208 switch (t->text.a[0]) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006209 case ',':
6210 if (comma && !brace)
6211 goto endparam;
6212 break;
6213
6214 case '{':
6215 brace++;
6216 break;
6217
6218 case '}':
6219 brace--;
6220 if (braced && !brace) {
6221 paramlen[i] += white;
6222 goto endparam;
6223 }
6224 break;
6225
6226 default:
6227 break;
6228 }
6229 }
6230
6231 paramlen[i] += white + 1;
6232 white = 0;
6233 }
6234 endparam:
6235 ;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006236 }
6237
6238 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006239 * OK, we have a MMacro structure together with a set of
6240 * parameters. We must now go through the expansion and push
6241 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00006242 * parameter tokens and macro-local tokens doesn't get done
6243 * until the single-line macro substitution process; this is
6244 * because delaying them allows us to change the semantics
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006245 * later through %rotate and give the right semantics for
6246 * nested mmacros.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006247 *
6248 * First, push an end marker on to istk->expansion, mark this
6249 * macro as in progress, and set up its invocation-specific
6250 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006251 */
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07006252 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006253 ll->next = istk->expansion;
6254 ll->finishes = m;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006255 ll->where = istk->where;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006256 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006257
6258 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006259 * Save the previous MMacro expansion in the case of
6260 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006261 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006262#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006263 if (m->max_depth && m->in_progress)
6264 push_mmacro(m);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006265#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006266
6267 m->in_progress ++;
6268 m->params = params;
6269 m->iline = tline;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006270 m->iname = nasm_strdup(mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006271 m->nparam = nparam;
6272 m->rotate = 0;
6273 m->paramlen = paramlen;
6274 m->unique = unique++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006275 m->condcnt = 0;
6276
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006277 m->mstk = istk->mstk;
6278 istk->mstk.mstk = istk->mstk.mmac = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006279
6280 list_for_each(l, m->expansion) {
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07006281 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006282 ll->next = istk->expansion;
6283 istk->expansion = ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006284 ll->first = dup_tlist(l->first, NULL);
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006285 ll->where = l->where;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006286 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006287
6288 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006289 * If we had a label, and this macro definition does not include
6290 * a %00, push it on as the first line of, ot
H. Peter Anvineba20a72002-04-30 20:53:55 +00006291 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006292 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006293 if (label) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006294 /*
6295 * We had a label. If this macro contains an %00 parameter,
6296 * save the value as a special parameter (which is what it
6297 * is), otherwise push it as the first line of the macro
6298 * expansion.
6299 */
6300 if (m->capture_label) {
6301 params[0] = dup_Token(NULL, label);
6302 paramlen[0] = 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006303 free_tlist(startline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006304 } else {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006305 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006306 ll->finishes = NULL;
6307 ll->next = istk->expansion;
6308 istk->expansion = ll;
6309 ll->first = startline;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006310 ll->where = istk->where;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006311 if (!dont_prepend) {
6312 while (label->next)
6313 label = label->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07006314 label->next = tt = make_tok_char(NULL, ':');
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006315 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006316 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00006317 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006318
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07006319 istk->nolist += !!(m->nolist & NL_LIST);
6320 istk->noline += !!(m->nolist & NL_LINE);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006321
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07006322 if (!istk->nolist) {
6323 lfmt->uplevel(LIST_MACRO, 0);
6324
6325 if (list_option('m'))
6326 list_mmacro_call(m);
6327 }
6328
6329 if (!istk->noline)
6330 src_macro_push(m, istk->where);
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07006331
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006332 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006333}
6334
H. Peter Anvin130736c2016-02-17 20:27:41 -08006335/*
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006336 * This function decides if an error message should be suppressed.
6337 * It will never be called with a severity level of ERR_FATAL or
6338 * higher.
H. Peter Anvin130736c2016-02-17 20:27:41 -08006339 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006340static bool pp_suppress_error(errflags severity)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02006341{
H. Peter Anvin130736c2016-02-17 20:27:41 -08006342 /*
6343 * If we're in a dead branch of IF or something like it, ignore the error.
6344 * However, because %else etc are evaluated in the state context
6345 * of the previous branch, errors might get lost:
6346 * %if 0 ... %else trailing garbage ... %endif
6347 * So %else etc should set the ERR_PP_PRECOND flag.
6348 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006349 if (istk && istk->conds &&
H. Peter Anvin130736c2016-02-17 20:27:41 -08006350 ((severity & ERR_PP_PRECOND) ?
6351 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07006352 !emitting(istk->conds->state)))
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006353 return true;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02006354
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006355 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00006356}
6357
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006358static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006359stdmac_file(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006360{
6361 (void)s;
6362 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006363 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006364
H. Peter Anvin8571f062019-09-23 16:40:03 -07006365 return make_tok_qstr(NULL, src_get_fname());
H. Peter Anvin8b262472019-02-26 14:00:54 -08006366}
6367
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006368static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006369stdmac_line(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006370{
6371 (void)s;
6372 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006373 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006374
H. Peter Anvin8571f062019-09-23 16:40:03 -07006375 return make_tok_num(NULL, src_get_linnum());
H. Peter Anvin8b262472019-02-26 14:00:54 -08006376}
6377
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006378static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006379stdmac_bits(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006380{
6381 (void)s;
6382 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006383 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006384
H. Peter Anvin8571f062019-09-23 16:40:03 -07006385 return make_tok_num(NULL, globalbits);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006386}
6387
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006388static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006389stdmac_ptr(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006390{
H. Peter Anvin8b262472019-02-26 14:00:54 -08006391 (void)s;
6392 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006393 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006394
6395 switch (globalbits) {
6396 case 16:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006397 return new_Token(NULL, TOK_ID, "word", 4);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006398 case 32:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006399 return new_Token(NULL, TOK_ID, "dword", 5);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006400 case 64:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006401 return new_Token(NULL, TOK_ID, "qword", 5);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006402 default:
6403 panic();
6404 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08006405}
6406
H. Peter Anvin8b262472019-02-26 14:00:54 -08006407/* Add magic standard macros */
6408struct magic_macros {
6409 const char *name;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006410 int nparam;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006411 ExpandSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006412};
6413static const struct magic_macros magic_macros[] =
6414{
H. Peter Anvind2354082019-08-27 16:38:48 -07006415 { "__?FILE?__", 0, stdmac_file },
6416 { "__?LINE?__", 0, stdmac_line },
6417 { "__?BITS?__", 0, stdmac_bits },
6418 { "__?PTR?__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08006419 { NULL, 0, NULL }
6420};
6421
6422static void pp_add_magic_stdmac(void)
6423{
6424 const struct magic_macros *m;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006425 SMacro tmpl;
6426
6427 nasm_zero(tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006428
6429 for (m = magic_macros; m->name; m++) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006430 tmpl.nparam = m->nparam;
6431 tmpl.expand = m->func;
6432 define_smacro(m->name, true, NULL, &tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006433 }
6434}
6435
H. Peter Anvin734b1882002-04-30 21:01:08 +00006436static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006437pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006438{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006439 int apass;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006440 struct Include *inc;
H. Peter Anvin7383b402008-09-24 10:20:40 -07006441
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006442 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006443 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07006444 nested_mac_count = 0;
6445 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07006446 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006447 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07006448 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006449 pp_mode = mode;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07006450
6451 /* Reset options to default */
6452 nasm_zero(ppopt);
H. Peter Anvinf7606612016-07-13 14:23:48 -07006453
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07006454 if (!use_loaded)
6455 use_loaded = nasm_malloc(use_package_count * sizeof(bool));
6456 memset(use_loaded, 0, use_package_count * sizeof(bool));
6457
H. Peter Anvin6686de22019-08-10 05:33:14 -07006458 /* First set up the top level input file */
6459 nasm_new(istk);
6460 istk->fp = nasm_open_read(file, NF_TEXT);
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006461 if (!istk->fp) {
6462 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'%s%s",
6463 file, errno ? " " : "", errno ? strerror(errno) : "");
6464 }
H. Peter Anvin6686de22019-08-10 05:33:14 -07006465 src_set(0, file);
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006466 istk->where = src_where();
H. Peter Anvin6686de22019-08-10 05:33:14 -07006467 istk->lineinc = 1;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006468
6469 strlist_add(deplist, file);
6470
6471 /*
6472 * Set up the stdmac packages as a virtual include file,
6473 * indicated by a null file pointer.
6474 */
6475 nasm_new(inc);
6476 inc->next = istk;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006477 src_set(0, NULL);
6478 inc->where = src_where();
H. Peter Anvin6686de22019-08-10 05:33:14 -07006479 inc->nolist = !list_option('b');
6480 istk = inc;
6481 lfmt->uplevel(LIST_INCLUDE, 0);
6482
H. Peter Anvin8b262472019-02-26 14:00:54 -08006483 pp_add_magic_stdmac();
6484
H. Peter Anvinf7606612016-07-13 14:23:48 -07006485 if (tasm_compatible_mode)
6486 pp_add_stdmac(nasm_stdmac_tasm);
6487
6488 pp_add_stdmac(nasm_stdmac_nasm);
6489 pp_add_stdmac(nasm_stdmac_version);
6490
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006491 if (extrastdmac)
6492 pp_add_stdmac(extrastdmac);
6493
H. Peter Anvinf7606612016-07-13 14:23:48 -07006494 stdmacpos = stdmacros[0];
6495 stdmacnext = &stdmacros[1];
6496
H. Peter Anvind2456592008-06-19 15:04:18 -07006497 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006498
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006499 /*
H. Peter Anvind2354082019-08-27 16:38:48 -07006500 * Define the __?PASS?__ macro. This is defined here unlike all the
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07006501 * other builtins, because it is special -- it varies between
6502 * passes -- but there is really no particular reason to make it
6503 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006504 *
6505 * 0 = dependencies only
6506 * 1 = preparatory passes
6507 * 2 = final pass
6508 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006509 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006510 switch (mode) {
6511 case PP_NORMAL:
6512 apass = pass_final() ? 2 : 1;
6513 break;
6514 case PP_DEPS:
6515 apass = 0;
6516 break;
6517 case PP_PREPROC:
6518 apass = 3;
6519 break;
6520 default:
6521 panic();
6522 }
6523
H. Peter Anvin8571f062019-09-23 16:40:03 -07006524 define_smacro("__?PASS?__", true, make_tok_num(NULL, apass), NULL);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006525}
6526
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006527static void pp_init(void)
6528{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006529}
6530
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006531/*
6532 * Get a line of tokens. If we popped the macro expansion/include stack,
6533 * we return a pointer to the dummy token tok_pop; at that point if
6534 * istk is NULL then we have reached end of input;
6535 */
6536static Token tok_pop; /* Dummy token placeholder */
6537
6538static Token *pp_tokline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006539{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006540 while (true) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006541 Line *l = istk->expansion;
6542 Token *tline = NULL;
6543 Token *dtline;
6544
H. Peter Anvine2c80182005-01-15 22:15:51 +00006545 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006546 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00006547 * buffer or from the input file.
6548 */
6549 tline = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006550 while (l && l->finishes) {
6551 MMacro *fm = l->finishes;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006552
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07006553 nasm_assert(fm == istk->mstk.mstk);
6554
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006555 if (!fm->name && fm->in_progress > 1) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006556 /*
6557 * This is a macro-end marker for a macro with no
6558 * name, which means it's not really a macro at all
6559 * but a %rep block, and the `in_progress' field is
6560 * more than 1, meaning that we still need to
6561 * repeat. (1 means the natural last repetition; 0
6562 * means termination by %exitrep.) We have
6563 * therefore expanded up to the %endrep, and must
6564 * push the whole block on to the expansion buffer
6565 * again. We don't bother to remove the macro-end
6566 * marker: we'd only have to generate another one
6567 * if we did.
6568 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006569 fm->in_progress--;
6570 list_for_each(l, fm->expansion) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006571 Line *ll;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006572
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006573 nasm_new(ll);
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006574 ll->next = istk->expansion;
6575 ll->first = dup_tlist(l->first, NULL);
6576 ll->where = l->where;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006577 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006578 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006579 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006580 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006581 MMacro *m = istk->mstk.mstk;
6582
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006583 /*
6584 * Check whether a `%rep' was started and not ended
6585 * within this macro expansion. This can happen and
6586 * should be detected. It's a fatal error because
6587 * I'm too confused to work out how to recover
6588 * sensibly from it.
6589 */
6590 if (defining) {
6591 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07006592 nasm_panic("defining with name in expansion");
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006593 else if (m->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07006594 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006595 " expansion of macro `%s'", m->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006596 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006597
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006598 /*
6599 * FIXME: investigate the relationship at this point between
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006600 * istk->mstk.mstk and fm
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006601 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006602 istk->mstk = m->mstk;
6603 if (m->name) {
6604 /*
6605 * This was a real macro call, not a %rep, and
6606 * therefore the parameter information needs to
6607 * be freed and the iteration count/nesting
6608 * depth adjusted.
6609 */
6610
6611 if (!--mmacro_deadman.levels) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006612 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006613 * If all mmacro processing done,
6614 * clear all counters and the deadman
6615 * message trigger.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006616 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006617 nasm_zero(mmacro_deadman); /* Clear all counters */
Adam Majer91e72402017-07-25 10:42:01 +02006618 }
6619
Adam Majer91e72402017-07-25 10:42:01 +02006620#if 0
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006621 if (m->prev) {
6622 pop_mmacro(m);
6623 fm->in_progress --;
6624 } else
Adam Majer91e72402017-07-25 10:42:01 +02006625#endif
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006626 {
6627 nasm_free(m->params);
6628 free_tlist(m->iline);
6629 nasm_free(m->paramlen);
6630 fm->in_progress = 0;
6631 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006632 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006633
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07006634 if (fm->nolist & NL_LIST) {
6635 istk->nolist--;
6636 } else if (!istk->nolist) {
6637 lfmt->downlevel(LIST_MACRO);
6638 }
6639
6640 if (fm->nolist & NL_LINE) {
6641 istk->noline--;
6642 } else if (!istk->noline) {
6643 if (fm == src_macro_current())
6644 src_macro_pop();
6645 src_update(l->where);
6646 }
6647
6648 istk->where = l->where;
6649
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006650 /*
6651 * FIXME It is incorrect to always free_mmacro here.
6652 * It leads to usage-after-free.
6653 *
6654 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
6655 */
6656#if 0
6657 else
6658 free_mmacro(m);
6659#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006660 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006661 istk->expansion = l->next;
6662 nasm_free(l);
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07006663
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006664 return &tok_pop;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006665 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006666
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006667 do { /* until we get a line we can use */
6668 char *line;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006669
6670 if (istk->expansion) { /* from a macro expansion */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006671 Line *l = istk->expansion;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006672
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006673 istk->expansion = l->next;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006674 istk->where = l->where;
6675 tline = l->first;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006676 nasm_free(l);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006677
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07006678 if (!istk->noline)
6679 src_update(istk->where);
6680
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006681 if (!istk->nolist) {
6682 line = detoken(tline, false);
6683 lfmt->line(LIST_MACRO, istk->where.lineno, line);
6684 nasm_free(line);
6685 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006686 } else if ((line = read_line())) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006687 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006688 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006689 nasm_free(line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006690 } else {
6691 /*
6692 * The current file has ended; work down the istk
6693 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00006694 Include *i = istk;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006695 Include *is;
6696
H. Peter Anvin6686de22019-08-10 05:33:14 -07006697 if (i->fp)
6698 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006699 if (i->conds) {
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006700 /* nasm_fatal can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07006701 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006702 }
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006703
6704 list_for_each(is, i->next) {
6705 if (is->fp) {
6706 lfmt->downlevel(LIST_INCLUDE);
6707 src_update(is->where);
6708 break;
6709 }
6710 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00006711 istk = i->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006712 nasm_free(i);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006713 return &tok_pop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006714 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006715 } while (0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006716
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006717 /*
6718 * We must expand MMacro parameters and MMacro-local labels
6719 * _before_ we plunge into directive processing, to cope
6720 * with things like `%define something %1' such as STRUC
6721 * uses. Unless we're _defining_ a MMacro, in which case
6722 * those tokens should be left alone to go into the
6723 * definition; and unless we're in a non-emitting
6724 * condition, in which case we don't want to meddle with
6725 * anything.
6726 */
H. Peter Anvin (Intel)bacf04a2020-06-08 13:29:06 -07006727 if (!defining &&
6728 !(istk->conds && !emitting(istk->conds->state)) &&
6729 !(istk->mstk.mmac && !istk->mstk.mmac->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006730 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006731 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006732
H. Peter Anvine2c80182005-01-15 22:15:51 +00006733 /*
6734 * Check the line to see if it's a preprocessor directive.
6735 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006736 if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) {
6737 if (dtline)
6738 return dtline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006739 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006740 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006741 * We're defining a multi-line macro. We emit nothing
6742 * at all, and just
6743 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00006744 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006745 MMacro *mmac = defining->dstk.mmac;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006746 Line *l;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006747
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006748 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006749 l->next = defining->expansion;
6750 l->first = tline;
6751 l->finishes = NULL;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006752 l->where = istk->where;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006753 defining->expansion = l;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006754
6755 /*
6756 * Remember if this mmacro expansion contains %00:
6757 * if it does, we will have to handle leading labels
6758 * specially.
6759 */
6760 if (mmac) {
6761 const Token *t;
6762 list_for_each(t, tline) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006763 if (!memcmp(t->text.a, "%00", 4))
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006764 mmac->capture_label = true;
6765 }
6766 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006767 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006768 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006769 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00006770 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006771 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00006772 * directive so we keep our place correctly.
6773 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006774 free_tlist(tline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006775 } else if (istk->mstk.mstk && !istk->mstk.mstk->in_progress) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006776 /*
6777 * We're in a %rep block which has been terminated, so
6778 * we're walking through to the %endrep without
6779 * emitting anything. Emit nothing at all, not even a
6780 * blank line: when we emerge from the %rep block we'll
6781 * give a line-number directive so we keep our place
6782 * correctly.
6783 */
6784 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006785 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006786 tline = expand_smacro(tline);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006787 if (!expand_mmacro(tline))
6788 return tline;
6789 }
6790 }
6791}
6792
6793static char *pp_getline(void)
6794{
6795 char *line = NULL;
6796 Token *tline;
6797
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006798 while (true) {
6799 tline = pp_tokline();
6800 if (tline == &tok_pop) {
6801 /*
6802 * We popped the macro/include stack. If istk is empty,
6803 * we are at end of input, otherwise just loop back.
6804 */
6805 if (!istk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006806 break;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006807 } else {
6808 /*
6809 * De-tokenize the line and emit it.
6810 */
6811 line = detoken(tline, true);
6812 free_tlist(tline);
6813 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006814 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006815 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006816
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006817 if (list_option('e') && istk && !istk->nolist && line && line[0]) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006818 char *buf = nasm_strcat(" ;;; ", line);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006819 lfmt->line(LIST_MACRO, -1, buf);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006820 nasm_free(buf);
6821 }
6822
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006823 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006824}
6825
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006826static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006827{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006828 if (defining) {
6829 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006830 nasm_nonfatal("end of file while still defining macro `%s'",
6831 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006832 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006833 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006834 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006835
6836 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006837 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006838 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08006839
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006840 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006841 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07006842 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006843 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006844 Include *i = istk;
6845 istk = istk->next;
6846 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03006847 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006848 }
6849 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006850 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07006851 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006852}
6853
6854static void pp_cleanup_session(void)
6855{
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07006856 nasm_free(use_loaded);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006857 free_llist(predef);
6858 predef = NULL;
6859 delete_Blocks();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006860 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006861}
6862
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006863static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006864{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006865 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006866}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00006867
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006868static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006869{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006870 Token *inc, *space, *name;
6871 Line *l;
6872
H. Peter Anvin734b1882002-04-30 21:01:08 +00006873 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006874 space = new_White(name);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006875 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006876
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006877 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006878 l->next = predef;
6879 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006880 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006881 predef = l;
6882}
6883
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006884static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006885{
6886 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006887 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00006888 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006889
6890 equals = strchr(definition, '=');
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006891 space = new_White(NULL);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006892 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006893 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006894 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00006895 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006896 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006897 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006898
H. Peter Anvin8571f062019-09-23 16:40:03 -07006899 /* We can't predefine a TOK_LOCAL_MACRO for obvious reasons... */
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006900 if (space->next->type != TOK_PREPROC_ID &&
6901 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08006902 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006903
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006904 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006905 l->next = predef;
6906 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006907 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006908 predef = l;
6909}
6910
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006911static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00006912{
6913 Token *def, *space;
6914 Line *l;
6915
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006916 space = new_White(NULL);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006917 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006918 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00006919
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006920 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00006921 l->next = predef;
6922 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006923 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00006924 predef = l;
6925}
6926
H. Peter Anvin05990342018-06-11 13:32:42 -07006927/* Insert an early preprocessor command that doesn't need special handling */
6928static void pp_pre_command(const char *what, char *string)
6929{
6930 char *cmd;
6931 Token *def, *space;
6932 Line *l;
6933
6934 def = tokenize(string);
6935 if (what) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006936 space = new_White(def);
H. Peter Anvin8571f062019-09-23 16:40:03 -07006937 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
6938 def = new_Token(space, TOK_PREPROC_ID, cmd, nasm_last_string_len());
6939 nasm_free(cmd);
H. Peter Anvin05990342018-06-11 13:32:42 -07006940 }
6941
6942 l = nasm_malloc(sizeof(Line));
6943 l->next = predef;
6944 l->first = def;
6945 l->finishes = NULL;
6946 predef = l;
6947}
6948
H. Peter Anvinf7606612016-07-13 14:23:48 -07006949static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006950{
H. Peter Anvinf7606612016-07-13 14:23:48 -07006951 macros_t **mp;
6952
6953 /* Find the end of the list and avoid duplicates */
6954 for (mp = stdmacros; *mp; mp++) {
6955 if (*mp == macros)
6956 return; /* Nothing to do */
6957 }
6958
6959 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
6960
6961 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00006962}
6963
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006964static void pp_extra_stdmac(macros_t *macros)
6965{
6966 extrastdmac = macros;
6967}
6968
H. Peter Anvin8571f062019-09-23 16:40:03 -07006969/* Create a numeric token */
6970static Token *make_tok_num(Token *next, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006971{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04006972 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08006973 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvin8571f062019-09-23 16:40:03 -07006974 return new_Token(next, TOK_NUMBER, numbuf, len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006975}
6976
H. Peter Anvin8571f062019-09-23 16:40:03 -07006977/* Create a quoted string token */
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07006978static Token *make_tok_qstr_len(Token *next, const char *str, size_t len)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006979{
H. Peter Anvin8571f062019-09-23 16:40:03 -07006980 char *p = nasm_quote(str, &len);
6981 return new_Token_free(next, TOK_STRING, p, len);
6982}
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07006983static Token *make_tok_qstr(Token *next, const char *str)
6984{
6985 return make_tok_qstr_len(next, str, strlen(str));
6986}
H. Peter Anvin8571f062019-09-23 16:40:03 -07006987
6988/* Create a single-character operator token */
6989static Token *make_tok_char(Token *next, char op)
6990{
6991 Token *t = new_Token(next, TOK_OTHER, NULL, 1);
6992 t->text.a[0] = op;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006993 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006994}
6995
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006996/*
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07006997 * Descent the macro hierarchy and display the expansion after
6998 * encountering an error message.
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006999 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08007000static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07007001{
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07007002 const MMacro *m;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07007003
H. Peter Anvinddb29062018-12-11 00:06:29 -08007004 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07007005
7006 while ((m = src_error_down())) {
7007 nasm_error(severity, "... from macro `%s' defined", m->name);
7008 }
7009
7010 src_error_reset();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07007011}
7012
H. Peter Anvine7469712016-02-18 02:20:59 -08007013const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07007014 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00007015 pp_reset,
7016 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08007017 pp_cleanup_pass,
7018 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03007019 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04007020 pp_pre_define,
7021 pp_pre_undefine,
7022 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07007023 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07007024 pp_include_path,
7025 pp_error_list_macros,
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07007026 pp_suppress_error
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00007027};