blob: 4fcdb3592e6ec608dbe3868bd4538628325d78b5 [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;
H. Peter Anvin (Intel)23abe9f2020-07-10 01:52:49 -07004038 /*
4039 * dstk.mstk points to the previous definition bracket,
4040 * whereas dstk.mmac points to the topmost mmacro, which
4041 * in this case is the one we are just starting to create.
4042 */
4043 def->dstk.mstk = defining;
4044 def->dstk.mmac = def;
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004045 if (op == PP_RMACRO)
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07004046 def->max_depth = nasm_limit[LIMIT_MACRO_LEVELS];
4047 if (!parse_mmacro_spec(tline, def, dname)) {
4048 nasm_free(def);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004049 goto done;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004050 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07004051
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07004052 defining = def;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07004053 defining->where = istk->where;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07004054
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004055 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
4056 while (mmac) {
4057 if (!strcmp(mmac->name, defining->name) &&
4058 (mmac->nparam_min <= defining->nparam_max
4059 || defining->plus)
4060 && (defining->nparam_min <= mmac->nparam_max
4061 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004062 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004063 defining->name);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004064 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004065 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004066 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004067 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004068 break;
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07004069 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004070
H. Peter Anvine2c80182005-01-15 22:15:51 +00004071 case PP_ENDM:
4072 case PP_ENDMACRO:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004073 if (!(defining && defining->name)) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004074 nasm_nonfatal("`%s': not defining a macro", tok_text(tline));
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004075 goto done;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004076 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004077 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
4078 defining->next = *mmhead;
4079 *mmhead = defining;
4080 defining = NULL;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004081 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004082
H. Peter Anvin89cee572009-07-15 09:16:54 -04004083 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004084 /*
4085 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004086 * macro-end marker for a macro with a name. Then we
4087 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004088 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004089 list_for_each(l, istk->expansion)
4090 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004091 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004092
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004093 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004094 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004095 * Remove all conditional entries relative to this
4096 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004097 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004098 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
4099 cond = istk->conds;
4100 istk->conds = cond->next;
4101 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004102 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004103 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004104 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004105 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004106 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004107 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05004108
H. Peter Anvina26433d2008-07-16 14:40:01 -07004109 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08004110 casesense = false;
4111 /* fall through */
4112 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07004113 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004114 MMacro **mmac_p;
4115 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07004116
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004117 nasm_zero(spec);
H. Peter Anvin8b262472019-02-26 14:00:54 -08004118 spec.casesense = casesense;
4119 if (!parse_mmacro_spec(tline, &spec, dname)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004120 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004121 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004122 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
4123 while (mmac_p && *mmac_p) {
4124 mmac = *mmac_p;
4125 if (mmac->casesense == spec.casesense &&
4126 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
4127 mmac->nparam_min == spec.nparam_min &&
4128 mmac->nparam_max == spec.nparam_max &&
4129 mmac->plus == spec.plus) {
4130 *mmac_p = mmac->next;
4131 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004132 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004133 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004134 }
4135 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004136 free_tlist(spec.dlist);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004137 break;
H. Peter Anvina26433d2008-07-16 14:40:01 -07004138 }
4139
H. Peter Anvine2c80182005-01-15 22:15:51 +00004140 case PP_ROTATE:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004141 while (tok_white(tline->next))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004142 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04004143 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004144 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004145 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00004146 return DIRECTIVE_FOUND;
4147 }
4148 t = expand_smacro(tline->next);
4149 tline->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004150 pps.tptr = tline = t;
4151 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004152 tokval.t_type = TOKEN_INVALID;
4153 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004154 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004155 free_tlist(tline);
4156 if (!evalresult)
4157 return DIRECTIVE_FOUND;
4158 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004159 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00004160 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004161 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00004162 return DIRECTIVE_FOUND;
4163 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004164 mmac = istk->mstk.mmac;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004165 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004166 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004167 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004168 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004169 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004170 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004171
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004172 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004173 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004174 rotate += mmac->nparam;
4175
4176 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004177 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004178 break;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004179
H. Peter Anvine2c80182005-01-15 22:15:51 +00004180 case PP_REP:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004181 {
4182 MMacro *tmp_defining;
4183
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07004184 nolist = 0;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004185 tline = skip_white(tline->next);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004186 if (tok_type(tline, TOK_ID) && tline->len == 7 &&
4187 !nasm_memicmp(tline->text.a, ".nolist", 7)) {
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07004188 if (!list_option('f'))
4189 nolist |= NL_LIST; /* ... but update line numbers */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004190 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004191 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004192
H. Peter Anvine2c80182005-01-15 22:15:51 +00004193 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08004194 pps.tptr = expand_smacro(tline);
4195 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004196 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004197 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004198 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004199 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004200 if (!evalresult)
4201 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004202 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004203 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00004204 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004205 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004206 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004207 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04004208 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07004209 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004210 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
4211 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04004212 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07004213 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08004214 /*!
4215 *!negative-rep [on] regative %rep count
4216 *! warns about negative counts given to the \c{%rep}
4217 *! preprocessor directive.
4218 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08004219 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07004220 "negative `%%rep' count: %"PRId64, count);
4221 count = 0;
4222 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04004223 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07004224 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004225 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004226 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07004227 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004228 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004229 tmp_defining = defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07004230 nasm_new(defining);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004231 defining->nolist = nolist;
4232 defining->in_progress = count;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004233 defining->mstk = istk->mstk;
4234 defining->dstk.mstk = tmp_defining;
4235 defining->dstk.mmac = tmp_defining ? tmp_defining->dstk.mmac : NULL;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07004236 defining->where = istk->where;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004237 break;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004238 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004239
H. Peter Anvine2c80182005-01-15 22:15:51 +00004240 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004241 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004242 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004243 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004244 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004245
H. Peter Anvine2c80182005-01-15 22:15:51 +00004246 /*
4247 * Now we have a "macro" defined - although it has no name
4248 * and we won't be entering it in the hash tables - we must
4249 * push a macro-end marker for it on to istk->expansion.
4250 * After that, it will take care of propagating itself (a
4251 * macro-end marker line for a macro which is really a %rep
4252 * block will cause the macro to be re-expanded, complete
4253 * with another macro-end marker to ensure the process
4254 * continues) until the whole expansion is forcibly removed
4255 * from istk->expansion by a %exitrep.
4256 */
H. Peter Anvin6686de22019-08-10 05:33:14 -07004257 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004258 l->next = istk->expansion;
4259 l->finishes = defining;
4260 l->first = NULL;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07004261 l->where = src_where();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004262 istk->expansion = l;
4263
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004264 istk->mstk.mstk = defining;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004265
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07004266 /* A loop does not change istk->noline */
4267 istk->nolist += !!(defining->nolist & NL_LIST);
4268 if (!istk->nolist)
4269 lfmt->uplevel(LIST_MACRO, 0);
4270
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004271 defining = defining->dstk.mstk;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004272 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004273
H. Peter Anvine2c80182005-01-15 22:15:51 +00004274 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004275 /*
4276 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004277 * macro-end marker for a macro with no name. Then we set
4278 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004279 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004280 list_for_each(l, istk->expansion)
4281 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004282 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004283
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004284 if (l)
H. Peter Anvind983b622019-10-07 21:19:32 -07004285 l->finishes->in_progress = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004286 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004287 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004288 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004289
H. Peter Anvin8b262472019-02-26 14:00:54 -08004290 case PP_DEFINE:
4291 case PP_XDEFINE:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004292 case PP_DEFALIAS:
H. Peter Anvin8b262472019-02-26 14:00:54 -08004293 {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004294 SMacro tmpl;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004295 Token **lastp;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07004296 int nparam;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07004297
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004298 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004299 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004300
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004301 nasm_zero(tmpl);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004302 lastp = &tline->next;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07004303 nparam = parse_smacro_template(&lastp, &tmpl);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004304 tline = *lastp;
4305 *lastp = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004306
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004307 if (unlikely(op == PP_DEFALIAS)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004308 macro_start = tline;
4309 if (!is_macro_id(macro_start)) {
4310 nasm_nonfatal("`%s' expects a macro identifier to alias",
4311 dname);
4312 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004313 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004314 tt = macro_start->next;
4315 macro_start->next = NULL;
4316 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004317 tline = skip_white(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004318 if (tline && tline->type) {
4319 nasm_warn(WARN_OTHER,
4320 "trailing garbage after aliasing identifier ignored");
4321 }
4322 free_tlist(tt);
4323 tmpl.alias = true;
4324 } else {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004325 if (op == PP_XDEFINE) {
4326 /* Protect macro parameter tokens */
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07004327 if (nparam)
4328 mark_smac_params(tline, &tmpl, TOK_XDEF_PARAM);
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004329 tline = expand_smacro(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004330 }
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004331 /* NB: Does this still make sense? */
4332 macro_start = reverse_tokens(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004333 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004334
H. Peter Anvine2c80182005-01-15 22:15:51 +00004335 /*
4336 * Good. We now have a macro name, a parameter count, and a
4337 * token list (in reverse order) for an expansion. We ought
4338 * to be OK just to create an SMacro, store it, and let
4339 * free_tlist have the rest of the line (which we have
4340 * carefully re-terminated after chopping off the expansion
4341 * from the end).
4342 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004343 define_smacro(mname, casesense, macro_start, &tmpl);
4344 break;
4345 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004346
H. Peter Anvine2c80182005-01-15 22:15:51 +00004347 case PP_UNDEF:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004348 case PP_UNDEFALIAS:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004349 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004350 goto done;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004351 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004352 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004353
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004354 undef_smacro(mname, op == PP_UNDEFALIAS);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004355 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004356
H. Peter Anvin8b262472019-02-26 14:00:54 -08004357 case PP_DEFSTR:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004358 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004359 goto done;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004360
H. Peter Anvin9e200162008-06-04 17:23:14 -07004361 last = tline;
4362 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004363 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004364
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004365 tline = zap_white(tline);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004366 q = detoken(tline, false);
4367 macro_start = make_tok_qstr(NULL, q);
4368 nasm_free(q);
H. Peter Anvin9e200162008-06-04 17:23:14 -07004369
4370 /*
4371 * We now have a macro name, an implicit parameter count of
4372 * zero, and a string token to use as an expansion. Create
4373 * and store an SMacro.
4374 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004375 define_smacro(mname, casesense, macro_start, NULL);
4376 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004377
H. Peter Anvin8b262472019-02-26 14:00:54 -08004378 case PP_DEFTOK:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004379 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004380 goto done;
4381
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004382 last = tline;
4383 tline = expand_smacro(tline->next);
4384 last->next = NULL;
4385
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004386 t = skip_white(tline);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004387 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004388 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004389 nasm_nonfatal("`%s' requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004390 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004391 goto done;
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004392 }
4393
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04004394 /*
4395 * Convert the string to a token stream. Note that smacros
4396 * are stored with the token stream reversed, so we have to
4397 * reverse the output of tokenize().
4398 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07004399 macro_start = reverse_tokens(tokenize(unquote_token_cstr(t)));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004400
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004401 /*
4402 * We now have a macro name, an implicit parameter count of
4403 * zero, and a numeric token to use as an expansion. Create
4404 * and store an SMacro.
4405 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004406 define_smacro(mname, casesense, macro_start, NULL);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004407 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004408 break;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004409
H. Peter Anvin418ca702008-05-30 10:42:30 -07004410 case PP_PATHSEARCH:
4411 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07004412 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004413
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004414 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004415 goto done;
4416
H. Peter Anvin418ca702008-05-30 10:42:30 -07004417 last = tline;
4418 tline = expand_smacro(tline->next);
4419 last->next = NULL;
4420
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004421 t = skip_white(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004422 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004423 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004424 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004425 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004426 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004427 }
4428 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004429 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004430
4431 p = unquote_token_cstr(t);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004432
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07004433 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07004434 if (!found_path)
4435 found_path = p;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004436 macro_start = make_tok_qstr(NULL, found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004437
4438 /*
4439 * We now have a macro name, an implicit parameter count of
4440 * zero, and a string token to use as an expansion. Create
4441 * and store an SMacro.
4442 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004443 define_smacro(mname, casesense, macro_start, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004444 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004445 break;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004446 }
4447
H. Peter Anvine2c80182005-01-15 22:15:51 +00004448 case PP_STRLEN:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004449 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004450 goto done;
4451
H. Peter Anvine2c80182005-01-15 22:15:51 +00004452 last = tline;
4453 tline = expand_smacro(tline->next);
4454 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004455
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004456 t = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004457 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004458 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004459 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004460 free_tlist(tline);
4461 free_tlist(origline);
4462 return DIRECTIVE_FOUND;
4463 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004464
H. Peter Anvin8571f062019-09-23 16:40:03 -07004465 unquote_token(t);
4466 macro_start = make_tok_num(NULL, t->len);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004467
H. Peter Anvine2c80182005-01-15 22:15:51 +00004468 /*
4469 * We now have a macro name, an implicit parameter count of
4470 * zero, and a numeric token to use as an expansion. Create
4471 * and store an SMacro.
4472 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004473 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004474 free_tlist(tline);
4475 free_tlist(origline);
4476 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004477
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004478 case PP_STRCAT:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004479 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004480 goto done;
4481
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004482 last = tline;
4483 tline = expand_smacro(tline->next);
4484 last->next = NULL;
4485
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004486 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004487 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004488 switch (t->type) {
4489 case TOK_WHITESPACE:
4490 break;
4491 case TOK_STRING:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004492 unquote_token(t);
4493 len += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004494 break;
4495 case TOK_OTHER:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004496 if (tok_is(t, ',')) /* permit comma separators */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004497 break;
4498 /* else fall through */
4499 default:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004500 nasm_nonfatal("non-string passed to `%s': %s", dname,
4501 tok_text(t));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004502 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004503 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004504 }
4505 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004506
H. Peter Anvin (Intel)f770ce82019-10-17 18:22:43 -07004507 q = qbuf = nasm_malloc(len+1);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004508 list_for_each(t, tline) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004509 if (t->type == TOK_INTERNAL_STRING)
4510 q = mempcpy(q, tok_text(t), t->len);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004511 }
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07004512 *q = '\0';
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004513
4514 /*
4515 * We now have a macro name, an implicit parameter count of
4516 * zero, and a numeric token to use as an expansion. Create
4517 * and store an SMacro.
4518 */
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07004519 macro_start = make_tok_qstr_len(NULL, qbuf, len);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004520 nasm_free(qbuf);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004521 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004522 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004523 break;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004524
H. Peter Anvine2c80182005-01-15 22:15:51 +00004525 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004526 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004527 int64_t start, count;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004528 const char *txt;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004529 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07004530
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004531 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004532 goto done;
4533
H. Peter Anvine2c80182005-01-15 22:15:51 +00004534 last = tline;
4535 tline = expand_smacro(tline->next);
4536 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004537
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04004538 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004539 t = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004540
4541 t = skip_white(t);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004542
H. Peter Anvine2c80182005-01-15 22:15:51 +00004543 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004544 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004545 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004546 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004547 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004548 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004549
H. Peter Anvin8b262472019-02-26 14:00:54 -08004550 pps.tptr = t->next;
4551 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004552 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004553 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004554 if (!evalresult) {
4555 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004556 goto done;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004557 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004558 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004559 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004560 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004561 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004562 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004563
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004564 pps.tptr = skip_white(pps.tptr);
H. Peter Anvin8b262472019-02-26 14:00:54 -08004565 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004566 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004567 } else {
4568 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004569 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004570 if (!evalresult) {
4571 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004572 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004573 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004574 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004575 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004576 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004577 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004578 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004579 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004580
H. Peter Anvin8571f062019-09-23 16:40:03 -07004581 unquote_token(t);
4582 len = t->len;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004583
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04004584 /* make start and count being in range */
4585 if (start < 0)
4586 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004587 if (count < 0)
4588 count = len + count + 1 - start;
4589 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04004590 count = len - start;
4591 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004592 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004593
H. Peter Anvin8571f062019-09-23 16:40:03 -07004594 txt = (start < 0) ? "" : tok_text(t) + start;
4595 len = count;
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07004596 macro_start = make_tok_qstr_len(NULL, txt, len);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004597
H. Peter Anvine2c80182005-01-15 22:15:51 +00004598 /*
4599 * We now have a macro name, an implicit parameter count of
4600 * zero, and a numeric token to use as an expansion. Create
4601 * and store an SMacro.
4602 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004603 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004604 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004605 break;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004606 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004607
H. Peter Anvin8b262472019-02-26 14:00:54 -08004608 case PP_ASSIGN:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004609 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004610 goto done;
4611
H. Peter Anvine2c80182005-01-15 22:15:51 +00004612 last = tline;
4613 tline = expand_smacro(tline->next);
4614 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004615
H. Peter Anvin8b262472019-02-26 14:00:54 -08004616 pps.tptr = tline;
4617 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004618 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004619 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004620 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004621 if (!evalresult)
4622 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004623
H. Peter Anvine2c80182005-01-15 22:15:51 +00004624 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004625 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00004626
H. Peter Anvine2c80182005-01-15 22:15:51 +00004627 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004628 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004629 free_tlist(origline);
4630 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004631 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004632
H. Peter Anvin8571f062019-09-23 16:40:03 -07004633 macro_start = make_tok_num(NULL, reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00004634
H. Peter Anvine2c80182005-01-15 22:15:51 +00004635 /*
4636 * We now have a macro name, an implicit parameter count of
4637 * zero, and a numeric token to use as an expansion. Create
4638 * and store an SMacro.
4639 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004640 define_smacro(mname, casesense, macro_start, NULL);
4641 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004642
H. Peter Anvind2354082019-08-27 16:38:48 -07004643 case PP_ALIASES:
4644 tline = tline->next;
4645 tline = expand_smacro(tline);
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07004646 ppopt.noaliases = !pp_get_boolean_option(tline, !ppopt.noaliases);
H. Peter Anvind2354082019-08-27 16:38:48 -07004647 break;
4648
H. Peter Anvine2c80182005-01-15 22:15:51 +00004649 case PP_LINE:
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07004650 nasm_panic("`%s' directive not preprocessed early", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004651 break;
4652 }
4653
4654done:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004655 free_tlist(origline);
4656 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004657}
4658
4659/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00004660 * Ensure that a macro parameter contains a condition code and
4661 * nothing else. Return the condition code index if so, or -1
4662 * otherwise.
4663 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004664static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004665{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004666 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004667
H. Peter Anvin25a99342007-09-22 17:45:45 -07004668 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004669 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07004670
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004671 t = skip_white(t);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004672 if (!tok_type(t, TOK_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004673 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004674 tt = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004675 tt = skip_white(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004676 if (tok_isnt(tt, ','))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004677 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004678
H. Peter Anvin8571f062019-09-23 16:40:03 -07004679 return bsii(tok_text(t), (const char **)conditions,
4680 ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00004681}
4682
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004683static inline bool pp_concat_match(const Token *t, unsigned int mask)
4684{
4685 return t && (PP_CONCAT_MASK(t->type) & mask);
4686}
4687
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004688/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004689 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004690 * pasting, if @handle_explicit passed then explicit pasting
4691 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004692 * The @m array can contain a series of token types which are
4693 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004694 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004695static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004696 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07004697{
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004698 Token *tok, *t, *next, **prev_next, **prev_nonspace;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004699 bool pasted = false;
4700 char *buf, *p;
4701 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07004702
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004703 /*
4704 * The last token before pasting. We need it
4705 * to be able to connect new handled tokens.
4706 * In other words if there were a tokens stream
4707 *
4708 * A -> B -> C -> D
4709 *
4710 * and we've joined tokens B and C, the resulting
4711 * stream should be
4712 *
4713 * A -> BC -> D
4714 */
4715 tok = *head;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004716 prev_next = prev_nonspace = head;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004717
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004718 if (tok_white(tok) || tok_type(tok, TOK_PASTE))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004719 prev_nonspace = NULL;
4720
4721 while (tok && (next = tok->next)) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004722 bool did_paste = false;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004723
4724 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07004725 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004726 /* Zap redundant whitespaces */
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004727 tok->next = next = zap_white(next);
H. Peter Anvind784a082009-04-20 14:01:18 -07004728 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004729
4730 case TOK_PASTE:
4731 /* Explicit pasting */
4732 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004733 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004734
H. Peter Anvin (Intel)122c5fb2020-07-05 03:39:04 -07004735 /* Left pasting token is start of line, just drop %+ */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004736 if (!prev_nonspace) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004737 tok = delete_Token(tok);
4738 break;
4739 }
4740
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004741 did_paste = true;
4742
4743 prev_next = prev_nonspace;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004744 t = *prev_nonspace;
4745
4746 /* Delete leading whitespace */
4747 next = zap_white(t->next);
4748
H. Peter Anvin (Intel)122c5fb2020-07-05 03:39:04 -07004749 /*
4750 * Delete the %+ token itself, followed by any whitespace.
4751 * In a sequence of %+ ... %+ ... %+ pasting sequences where
4752 * some expansions in the middle have ended up empty,
4753 * we can end up having multiple %+ tokens in a row;
4754 * just drop whem in that case.
4755 */
4756 while (next) {
4757 if (next->type == TOK_PASTE || next->type == TOK_WHITESPACE)
4758 next = delete_Token(next);
4759 else
4760 break;
4761 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004762
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004763 /*
H. Peter Anvin (Intel)122c5fb2020-07-05 03:39:04 -07004764 * Nothing after? Just leave the existing token.
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004765 */
4766 if (!next) {
H. Peter Anvin (Intel)122c5fb2020-07-05 03:39:04 -07004767 t->next = tok = NULL; /* End of line */
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004768 break;
4769 }
4770
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004771 p = buf = nasm_malloc(t->len + next->len + 1);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004772 p = mempcpy(p, tok_text(t), t->len);
4773 p = mempcpy(p, tok_text(next), next->len);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004774 *p = '\0';
4775 delete_Token(t);
4776 t = tokenize(buf);
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004777 nasm_free(buf);
4778
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004779 if (unlikely(!t)) {
4780 /*
4781 * No output at all? Replace with a single whitespace.
4782 * This should never happen.
4783 */
4784 t = new_White(NULL);
4785 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004786
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004787 *prev_nonspace = tok = t;
4788 while (t->next)
4789 t = t->next; /* Find the last token produced */
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004790
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004791 /* Delete the second token and attach to the end of the list */
4792 t->next = delete_Token(next);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004793
4794 /* We want to restart from the head of the pasted token */
4795 next = tok;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004796 break;
4797
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004798 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004799 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004800 for (i = 0; i < mnum; i++) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004801 if (pp_concat_match(tok, m[i].mask_head))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004802 break;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004803 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004804
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004805 if (i >= mnum)
4806 break;
4807
4808 len = tok->len;
4809 while (pp_concat_match(next, m[i].mask_tail)) {
4810 len += next->len;
4811 next = next->next;
4812 }
4813
4814 /* No match or no text to process */
4815 if (len == tok->len)
4816 break;
4817
4818 p = buf = nasm_malloc(len + 1);
4819 while (tok != next) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004820 p = mempcpy(p, tok_text(tok), tok->len);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004821 tok = delete_Token(tok);
4822 }
4823 *p = '\0';
4824 *prev_next = tok = t = tokenize(buf);
4825 nasm_free(buf);
4826
4827 /*
4828 * Connect pasted into original stream,
4829 * ie A -> new-tokens -> B
4830 */
4831 while (t->next)
4832 t = t->next;
4833 t->next = next;
4834 prev_next = prev_nonspace = &t->next;
4835 did_paste = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004836 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004837 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004838
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004839 if (did_paste) {
4840 pasted = true;
4841 } else {
4842 prev_next = &tok->next;
4843 if (next && next->type != TOK_WHITESPACE && next->type != TOK_PASTE)
4844 prev_nonspace = prev_next;
4845 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004846
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004847 tok = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004848 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004849
4850 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07004851}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004852
4853/*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004854 * Computes the proper rotation of mmacro parameters
4855 */
4856static int mmac_rotate(const MMacro *mac, unsigned int n)
4857{
4858 if (--n < mac->nparam)
4859 n = (n + mac->rotate) % mac->nparam;
4860
4861 return n+1;
4862}
4863
4864/*
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004865 * expands to a list of tokens from %{x:y}
4866 */
H. Peter Anvin (Intel)c0d0f882020-06-30 17:33:39 -07004867static void expand_mmac_params_range(MMacro *mac, Token *tline, Token ***tail)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004868{
H. Peter Anvin (Intel)e99a9462020-06-30 11:51:41 -07004869 Token *t;
4870 const char *arg = tok_text(tline) + 1;
4871 int fst, lst, incr, n;
4872 int parsed;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004873
H. Peter Anvin (Intel)e99a9462020-06-30 11:51:41 -07004874 parsed = sscanf(arg, "%d:%d", &fst, &lst);
4875 nasm_assert(parsed == 2);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004876
4877 /*
4878 * only macros params are accounted so
4879 * if someone passes %0 -- we reject such
4880 * value(s)
4881 */
4882 if (lst == 0 || fst == 0)
4883 goto err;
4884
4885 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004886 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
4887 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004888 goto err;
4889
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004890 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
4891 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004892
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004893 /*
H. Peter Anvin (Intel)e99a9462020-06-30 11:51:41 -07004894 * It will be at least one parameter, as we can loop
4895 * in either direction.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004896 */
H. Peter Anvin (Intel)e99a9462020-06-30 11:51:41 -07004897 incr = (fst < lst) ? 1 : -1;
4898
4899 while (true) {
4900 n = mmac_rotate(mac, fst);
4901 dup_tlistn(mac->params[n], mac->paramlen[n], tail);
4902 if (fst == lst)
4903 break;
4904 t = make_tok_char(NULL, ',');
4905 **tail = t;
4906 *tail = &t->next;
4907 fst += incr;
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004908 }
4909
H. Peter Anvin (Intel)e99a9462020-06-30 11:51:41 -07004910 return;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004911
4912err:
H. Peter Anvin (Intel)e99a9462020-06-30 11:51:41 -07004913 nasm_nonfatal("`%%{%s}': macro parameters out of range", arg);
4914 return;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004915}
4916
H. Peter Anvin76690a12002-04-30 20:52:49 +00004917/*
4918 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004919 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004920 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004921 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004922static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004923{
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004924 Token **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004925 bool changed = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004926 MMacro *mac = istk->mstk.mmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004927
4928 tail = &thead;
4929 thead = NULL;
4930
H. Peter Anvine2c80182005-01-15 22:15:51 +00004931 while (tline) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004932 bool change;
H. Peter Anvin (Intel)a762cd42020-06-01 11:49:08 -07004933 bool err_not_mac = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004934 Token *t = tline;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004935 const char *text = tok_text(t);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004936 int type = t->type;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004937
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004938 tline = tline->next;
4939 t->next = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004940
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004941 switch (type) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004942 case TOK_LOCAL_SYMBOL:
H. Peter Anvin (Intel)a762cd42020-06-01 11:49:08 -07004943 change = true;
4944
4945 if (!mac) {
4946 err_not_mac = true;
4947 break;
4948 }
4949
H. Peter Anvin8571f062019-09-23 16:40:03 -07004950 type = TOK_ID;
4951 text = nasm_asprintf("..@%"PRIu64".%s", mac->unique, text+2);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004952 break;
4953 case TOK_MMACRO_PARAM:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004954 {
4955 Token *tt = NULL;
4956
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004957 change = true;
4958
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004959 if (!mac) {
H. Peter Anvin (Intel)a762cd42020-06-01 11:49:08 -07004960 err_not_mac = true;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004961 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004962 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004963
4964 if (strchr(text, ':')) {
H. Peter Anvin (Intel)e99a9462020-06-30 11:51:41 -07004965 /* It is a range */
4966 expand_mmac_params_range(mac, t, &tail);
4967 text = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004968 break;
4969 }
4970
4971 switch (text[1]) {
4972 /*
4973 * We have to make a substitution of one of the
4974 * forms %1, %-1, %+1, %%foo, %0, %00.
4975 */
4976 case '0':
4977 if (!text[2]) {
4978 type = TOK_NUMBER;
4979 text = nasm_asprintf("%d", mac->nparam);
4980 break;
4981 }
4982 if (text[2] != '0' || text[3])
4983 goto invalid;
4984 /* a possible captured label == mac->params[0] */
4985 /* fall through */
4986 default:
4987 {
4988 unsigned long n;
4989 char *ep;
4990
4991 n = strtoul(text + 1, &ep, 10);
4992 if (unlikely(*ep))
4993 goto invalid;
4994
4995 if (n <= mac->nparam) {
4996 n = mmac_rotate(mac, n);
4997 dup_tlistn(mac->params[n], mac->paramlen[n], &tail);
4998 }
4999 text = NULL;
5000 break;
5001 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005002 case '-':
5003 case '+':
5004 {
5005 int cc;
5006 unsigned long n;
5007 char *ep;
5008
H. Peter Anvin8571f062019-09-23 16:40:03 -07005009 n = strtoul(tok_text(t) + 2, &ep, 10);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005010 if (unlikely(*ep))
5011 goto invalid;
5012
Chang S. Bae057b8322020-04-18 23:11:21 +00005013 if (n && n <= mac->nparam) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005014 n = mmac_rotate(mac, n);
5015 tt = mac->params[n];
5016 }
5017 cc = find_cc(tt);
5018 if (cc == -1) {
5019 nasm_nonfatal("macro parameter `%s' is not a condition code",
H. Peter Anvin8571f062019-09-23 16:40:03 -07005020 tok_text(t));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005021 text = NULL;
5022 break;
5023 }
5024
5025 type = TOK_ID;
5026 if (text[1] == '-') {
5027 int ncc = inverse_ccs[cc];
5028 if (unlikely(ncc == -1)) {
5029 nasm_nonfatal("condition code `%s' is not invertible",
5030 conditions[cc]);
5031 break;
5032 }
5033 cc = ncc;
5034 }
5035 text = nasm_strdup(conditions[cc]);
5036 break;
5037 }
5038
5039 invalid:
5040 nasm_nonfatal("invalid macro parameter: `%s'", text);
5041 text = NULL;
5042 break;
5043 }
5044 break;
5045 }
5046
5047 case TOK_PREPROC_Q:
5048 if (mac) {
5049 type = TOK_ID;
5050 text = nasm_strdup(mac->iname);
5051 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07005052 } else {
5053 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005054 }
5055 break;
5056
5057 case TOK_PREPROC_QQ:
5058 if (mac) {
5059 type = TOK_ID;
5060 text = nasm_strdup(mac->name);
5061 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07005062 } else {
5063 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005064 }
5065 break;
5066
5067 case TOK_INDIRECT:
5068 {
5069 Token *tt;
5070
H. Peter Anvin8571f062019-09-23 16:40:03 -07005071 tt = tokenize(tok_text(t));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005072 tt = expand_mmac_params(tt);
5073 tt = expand_smacro(tt);
5074 /* Why dup_tlist() here? We should own tt... */
5075 dup_tlist(tt, &tail);
5076 text = NULL;
5077 change = true;
5078 break;
5079 }
5080
5081 default:
5082 change = false;
5083 break;
5084 }
5085
H. Peter Anvin (Intel)a762cd42020-06-01 11:49:08 -07005086 if (err_not_mac) {
5087 nasm_nonfatal("`%s': not in a macro call", text);
5088 text = NULL;
5089 change = true;
5090 }
5091
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005092 if (change) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005093 if (!text) {
5094 delete_Token(t);
5095 } else {
5096 *tail = t;
5097 tail = &t->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005098 set_text(t, text, tok_strlen(text));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005099 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005100 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005101 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005102 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005103 *tail = t;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005104 tail = &t->next;
5105 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00005106 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005107
H. Peter Anvineba20a72002-04-30 20:53:55 +00005108 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07005109
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005110 if (changed) {
5111 const struct tokseq_match t[] = {
5112 {
5113 PP_CONCAT_MASK(TOK_ID) |
5114 PP_CONCAT_MASK(TOK_FLOAT), /* head */
5115 PP_CONCAT_MASK(TOK_ID) |
5116 PP_CONCAT_MASK(TOK_NUMBER) |
5117 PP_CONCAT_MASK(TOK_FLOAT) |
5118 PP_CONCAT_MASK(TOK_OTHER) /* tail */
5119 },
5120 {
5121 PP_CONCAT_MASK(TOK_NUMBER), /* head */
5122 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
5123 }
5124 };
5125 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
5126 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07005127
H. Peter Anvin76690a12002-04-30 20:52:49 +00005128 return thead;
5129}
5130
H. Peter Anvin322bee02019-08-10 01:38:06 -07005131static Token *expand_smacro_noreset(Token * tline);
H. Peter Anvin322bee02019-08-10 01:38:06 -07005132
H. Peter Anvin76690a12002-04-30 20:52:49 +00005133/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005134 * Expand *one* single-line macro instance. If the first token is not
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005135 * a macro at all, it is simply copied to the output and the pointer
5136 * advanced. tpp should be a pointer to a pointer (usually the next
5137 * pointer of the previous token) to the first token. **tpp is updated
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005138 * to point to the first token of the expansion, and *tpp updated to
5139 * point to the next pointer of the last token of the expansion.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005140 *
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005141 * If the expansion is empty, *tpp will be unchanged but **tpp will
5142 * be advanced past the macro call.
5143 *
H. Peter Anvin322bee02019-08-10 01:38:06 -07005144 * Return the macro expanded, or NULL if no expansion took place.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005145 */
H. Peter Anvin322bee02019-08-10 01:38:06 -07005146static SMacro *expand_one_smacro(Token ***tpp)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005147{
5148 Token **params = NULL;
5149 const char *mname;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005150 Token *mstart = **tpp;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005151 Token *tline = mstart;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005152 SMacro *head, *m;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005153 int i;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005154 Token *t, *tup, *tafter;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005155 int nparam = 0;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005156 bool cond_comma;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005157
5158 if (!tline)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005159 return false; /* Empty line, nothing to do */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005160
H. Peter Anvin8571f062019-09-23 16:40:03 -07005161 mname = tok_text(mstart);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005162
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005163 smacro_deadman.total--;
H. Peter Anvin322bee02019-08-10 01:38:06 -07005164 smacro_deadman.levels--;
5165
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005166 if (unlikely(smacro_deadman.total < 0 || smacro_deadman.levels < 0)) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07005167 if (unlikely(!smacro_deadman.triggered)) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005168 nasm_nonfatal("interminable macro recursion");
H. Peter Anvin322bee02019-08-10 01:38:06 -07005169 smacro_deadman.triggered = true;
5170 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005171 goto not_a_macro;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005172 } else if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005173 head = (SMacro *)hash_findix(&smacros, mname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005174 } else if (tline->type == TOK_LOCAL_MACRO) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005175 Context *ctx = get_ctx(mname, &mname);
5176 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
5177 } else {
5178 goto not_a_macro;
5179 }
5180
5181 /*
5182 * We've hit an identifier of some sort. First check whether the
5183 * identifier is a single-line macro at all, then think about
5184 * checking for parameters if necessary.
5185 */
5186 list_for_each(m, head) {
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005187 if (unlikely(m->alias && ppopt.noaliases))
H. Peter Anvind2354082019-08-27 16:38:48 -07005188 continue;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005189 if (!mstrcmp(m->name, mname, m->casesense))
5190 break;
5191 }
5192
5193 if (!m) {
5194 goto not_a_macro;
5195 }
5196
5197 /* Parse parameters, if applicable */
5198
5199 params = NULL;
5200 nparam = 0;
5201
5202 if (m->nparam == 0) {
5203 /*
5204 * Simple case: the macro is parameterless.
5205 * Nothing to parse; the expansion code will
5206 * drop the macro name token.
5207 */
5208 } else {
5209 /*
5210 * Complicated case: at least one macro with this name
5211 * exists and takes parameters. We must find the
5212 * parameters in the call, count them, find the SMacro
5213 * that corresponds to that form of the macro call, and
5214 * substitute for the parameters when we expand. What a
5215 * pain.
5216 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005217 Token *t;
5218 int paren, brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005219
5220 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005221 tline = skip_white(tline);
5222 if (!tok_is(tline, '(')) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005223 /*
5224 * This macro wasn't called with parameters: ignore
5225 * the call. (Behaviour borrowed from gnu cpp.)
5226 */
5227 goto not_a_macro;
5228 }
5229
5230 paren = 1;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005231 nparam = 1;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005232 brackets = 0;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005233 t = tline; /* tline points to leading ( */
5234
5235 while (paren) {
5236 t = t->next;
5237
5238 if (!t) {
5239 nasm_nonfatal("macro call expects terminating `)'");
5240 goto not_a_macro;
5241 }
5242
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005243 if (t->type != TOK_OTHER || t->len != 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005244 continue;
5245
H. Peter Anvin8571f062019-09-23 16:40:03 -07005246 switch (t->text.a[0]) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005247 case ',':
H. Peter Anvinbd00f252020-06-04 21:05:01 -07005248 if (!brackets && paren == 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005249 nparam++;
5250 break;
5251
5252 case '{':
5253 brackets++;
5254 break;
5255
5256 case '}':
5257 if (brackets > 0)
5258 brackets--;
5259 break;
5260
5261 case '(':
5262 if (!brackets)
5263 paren++;
5264 break;
5265
5266 case ')':
5267 if (!brackets)
5268 paren--;
5269 break;
5270
5271 default:
5272 break; /* Normal token */
5273 }
5274 }
5275
5276 /*
5277 * Look for a macro matching in both name and parameter count.
5278 * We already know any matches cannot be anywhere before the
5279 * current position of "m", so there is no reason to
5280 * backtrack.
5281 */
5282 while (1) {
5283 if (!m) {
5284 /*!
5285 *!macro-params-single [on] single-line macro calls with wrong parameter count
5286 *! warns about \i{single-line macros} being invoked
5287 *! with the wrong number of parameters.
5288 */
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005289 nasm_warn(WARN_MACRO_PARAMS_SINGLE|ERR_HOLD,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005290 "single-line macro `%s' exists, "
5291 "but not taking %d parameter%s",
5292 mname, nparam, (nparam == 1) ? "" : "s");
5293 goto not_a_macro;
5294 }
5295
5296 if (!mstrcmp(m->name, mname, m->casesense)) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005297 if (nparam == m->nparam)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005298 break; /* It's good */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005299 if (m->greedy && nparam >= m->nparam-1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005300 break; /* Also good */
5301 }
5302 m = m->next;
5303 }
5304 }
5305
5306 if (m->in_progress)
5307 goto not_a_macro;
5308
5309 /* Expand the macro */
5310 m->in_progress = true;
5311
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005312 if (nparam) {
5313 /* Extract parameters */
5314 Token **phead, **pep;
5315 int white = 0;
5316 int brackets = 0;
5317 int paren;
5318 bool bracketed = false;
5319 bool bad_bracket = false;
5320 enum sparmflags flags;
5321
5322 nparam = m->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005323 paren = 1;
5324 nasm_newn(params, nparam);
5325 i = 0;
5326 flags = m->params[i].flags;
5327 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005328 *pep = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005329
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005330 while (paren) {
5331 bool skip;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005332 char ch;
5333
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005334 tline = tline->next;
5335
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005336 if (!tline)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005337 nasm_nonfatal("macro call expects terminating `)'");
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005338
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005339 ch = 0;
5340 skip = false;
5341
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005342
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005343 switch (tline->type) {
5344 case TOK_OTHER:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005345 if (tline->len == 1)
H. Peter Anvin8571f062019-09-23 16:40:03 -07005346 ch = tline->text.a[0];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005347 break;
5348
5349 case TOK_WHITESPACE:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005350 if (!(flags & SPARM_NOSTRIP)) {
5351 if (brackets || *phead)
5352 white++; /* Keep interior whitespace */
5353 skip = true;
5354 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005355 break;
5356
5357 default:
5358 break;
5359 }
5360
5361 switch (ch) {
5362 case ',':
H. Peter Anvinbd00f252020-06-04 21:05:01 -07005363 if (!brackets && paren == 1 && !(flags & SPARM_GREEDY)) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005364 i++;
5365 nasm_assert(i < nparam);
5366 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005367 *pep = NULL;
5368 bracketed = false;
5369 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005370 flags = m->params[i].flags;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005371 }
5372 break;
5373
5374 case '{':
5375 if (!bracketed) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005376 bracketed = !*phead && !(flags & SPARM_NOSTRIP);
5377 skip = bracketed;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005378 }
5379 brackets++;
5380 break;
5381
5382 case '}':
5383 if (brackets > 0) {
5384 if (!--brackets)
5385 skip = bracketed;
5386 }
5387 break;
5388
5389 case '(':
5390 if (!brackets)
5391 paren++;
5392 break;
5393
5394 case ')':
5395 if (!brackets) {
5396 paren--;
5397 if (!paren) {
5398 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005399 i++; /* Found last argument */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005400 }
5401 }
5402 break;
5403
5404 default:
5405 break; /* Normal token */
5406 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005407
5408 if (!skip) {
5409 Token *t;
5410
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005411 bad_bracket |= bracketed && !brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005412
5413 if (white) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005414 *pep = t = new_White(NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005415 pep = &t->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005416 white = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005417 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005418 *pep = t = dup_Token(NULL, tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005419 pep = &t->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005420 }
5421 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005422
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005423 /*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005424 * Possible further processing of parameters. Note that the
5425 * ordering matters here.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005426 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005427 for (i = 0; i < nparam; i++) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005428 enum sparmflags flags = m->params[i].flags;
5429
5430 if (flags & SPARM_EVAL) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005431 /* Evaluate this parameter as a number */
5432 struct ppscan pps;
5433 struct tokenval tokval;
5434 expr *evalresult;
5435 Token *eval_param;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005436
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005437 pps.tptr = eval_param = expand_smacro_noreset(params[i]);
5438 pps.ntokens = -1;
5439 tokval.t_type = TOKEN_INVALID;
5440 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005441
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005442 free_tlist(eval_param);
5443 params[i] = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005444
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005445 if (!evalresult) {
5446 /* Nothing meaningful to do */
5447 } else if (tokval.t_type) {
5448 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
5449 } else if (!is_simple(evalresult)) {
5450 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
5451 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005452 params[i] = make_tok_num(NULL, reloc_value(evalresult));
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005453 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005454 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005455
5456 if (flags & SPARM_STR) {
5457 /* Convert expansion to a quoted string */
5458 char *arg;
5459 Token *qs;
5460
5461 qs = expand_smacro_noreset(params[i]);
5462 arg = detoken(qs, false);
5463 free_tlist(qs);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005464 params[i] = make_tok_qstr(NULL, arg);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005465 nasm_free(arg);
5466 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005467 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005468 }
5469
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005470 /* Note: we own the expansion this returns. */
5471 t = m->expand(m, params, nparam);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005472
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005473 tafter = tline->next; /* Skip past the macro call */
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005474 tline->next = NULL; /* Truncate list at the macro call end */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005475 tline = tafter;
5476
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005477 tup = NULL;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005478 cond_comma = false;
5479
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005480 while (t) {
5481 enum pp_token_type type = t->type;
5482 Token *tnext = t->next;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005483
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005484 switch (type) {
5485 case TOK_PREPROC_Q:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005486 delete_Token(t);
5487 t = dup_Token(tline, mstart);
5488 break;
5489
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005490 case TOK_PREPROC_QQ:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005491 {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005492 size_t mlen = strlen(m->name);
5493 size_t len;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005494 char *p;
5495
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005496 t->type = mstart->type;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005497 if (t->type == TOK_LOCAL_MACRO) {
5498 const char *psp; /* prefix start pointer */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005499 const char *pep; /* prefix end pointer */
H. Peter Anvin8571f062019-09-23 16:40:03 -07005500 size_t plen;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005501
H. Peter Anvin8571f062019-09-23 16:40:03 -07005502 psp = tok_text(mstart);
5503 get_ctx(psp, &pep);
5504 plen = pep - psp;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005505
H. Peter Anvin8571f062019-09-23 16:40:03 -07005506 len = mlen + plen;
5507 p = nasm_malloc(len + 1);
5508 p = mempcpy(p, psp, plen);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005509 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005510 len = mlen;
5511 p = nasm_malloc(len + 1);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005512 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07005513 p = mempcpy(p, m->name, mlen);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005514 *p = '\0';
H. Peter Anvin8571f062019-09-23 16:40:03 -07005515 set_text_free(t, p, len);
5516
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005517 t->next = tline;
5518 break;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005519 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005520
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005521 case TOK_COND_COMMA:
5522 delete_Token(t);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005523 t = cond_comma ? make_tok_char(tline, ',') : NULL;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005524 break;
5525
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005526 case TOK_ID:
5527 case TOK_PREPROC_ID:
H. Peter Anvin8571f062019-09-23 16:40:03 -07005528 case TOK_LOCAL_MACRO:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005529 {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005530 /*
5531 * Chain this into the target line *before* expanding,
5532 * that way we pick up any arguments to the new macro call,
5533 * if applicable.
5534 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005535 Token **tp = &t;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005536 t->next = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005537 expand_one_smacro(&tp);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005538 tline = *tp; /* First token left after any macro call */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005539 break;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005540 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005541 default:
5542 if (is_smac_param(t->type)) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005543 int param = smac_nparam(t->type);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005544 nasm_assert(!tup && param < nparam);
5545 delete_Token(t);
5546 t = NULL;
5547 tup = tnext;
5548 tnext = dup_tlist_reverse(params[param], NULL);
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005549 cond_comma = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005550 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005551 t->next = tline;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005552 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005553 }
5554
5555 if (t) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005556 Token *endt = tline;
5557
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005558 tline = t;
Chang S. Bae95e54a92020-02-06 14:39:22 -08005559 while (!cond_comma && t && t != endt) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005560 cond_comma = t->type != TOK_WHITESPACE;
Chang S. Bae95e54a92020-02-06 14:39:22 -08005561 t = t->next;
5562 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005563 }
5564
5565 if (tnext) {
5566 t = tnext;
5567 } else {
5568 t = tup;
5569 tup = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005570 }
5571 }
5572
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005573 **tpp = tline;
H. Peter Anvin (Intel)6e714962020-06-01 12:21:10 -07005574 for (t = tline; t && t != tafter; t = t->next)
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005575 *tpp = &t->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005576
5577 m->in_progress = false;
5578
5579 /* Don't do this until after expansion or we will clobber mname */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005580 free_tlist(mstart);
H. Peter Anvin322bee02019-08-10 01:38:06 -07005581 goto done;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005582
5583 /*
5584 * No macro expansion needed; roll back to mstart (if necessary)
H. Peter Anvin322bee02019-08-10 01:38:06 -07005585 * and then advance to the next input token. Note that this is
5586 * by far the common case!
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005587 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005588not_a_macro:
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005589 *tpp = &mstart->next;
H. Peter Anvin322bee02019-08-10 01:38:06 -07005590 m = NULL;
5591done:
5592 smacro_deadman.levels++;
5593 if (unlikely(params))
5594 free_tlist_array(params, nparam);
5595 return m;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005596}
5597
5598/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005599 * Expand all single-line macro calls made in the given line.
5600 * Return the expanded version of the line. The original is deemed
5601 * to be destroyed in the process. (In reality we'll just move
5602 * Tokens from input to output a lot of the time, rather than
5603 * actually bothering to destroy and replicate.)
5604 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005605static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005606{
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005607 smacro_deadman.total = nasm_limit[LIMIT_MACRO_TOKENS];
H. Peter Anvin322bee02019-08-10 01:38:06 -07005608 smacro_deadman.levels = nasm_limit[LIMIT_MACRO_LEVELS];
5609 smacro_deadman.triggered = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005610 return expand_smacro_noreset(tline);
5611}
5612
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005613static Token *expand_smacro_noreset(Token *org_tline)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005614{
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005615 Token *tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005616 bool expanded;
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005617 errhold errhold; /* Hold warning/errors during expansion */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005618
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005619 if (!org_tline)
5620 return NULL; /* Empty input */
5621
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005622 /*
5623 * Trick: we should avoid changing the start token pointer since it can
5624 * be contained in "next" field of other token. Because of this
5625 * we allocate a copy of first token and work with it; at the end of
5626 * routine we copy it back
5627 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005628 tline = dup_Token(org_tline->next, org_tline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005629
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005630 /*
5631 * Pretend that we always end up doing expansion on the first pass;
5632 * that way %+ get processed. However, if we process %+ before the
5633 * first pass, we end up with things like MACRO %+ TAIL trying to
5634 * look up the macro "MACROTAIL", which we don't want.
5635 */
5636 expanded = true;
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005637
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005638 while (true) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005639 static const struct tokseq_match tmatch[] = {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005640 {
5641 PP_CONCAT_MASK(TOK_ID) |
H. Peter Anvin8571f062019-09-23 16:40:03 -07005642 PP_CONCAT_MASK(TOK_LOCAL_MACRO) |
5643 PP_CONCAT_MASK(TOK_ENVIRON) |
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005644 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
5645 PP_CONCAT_MASK(TOK_ID) |
H. Peter Anvin8571f062019-09-23 16:40:03 -07005646 PP_CONCAT_MASK(TOK_LOCAL_MACRO) |
5647 PP_CONCAT_MASK(TOK_ENVIRON) |
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005648 PP_CONCAT_MASK(TOK_PREPROC_ID) |
5649 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
5650 }
5651 };
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005652 Token **tail = &tline;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005653
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005654 /*
5655 * We hold warnings/errors until we are done this this loop. It is
5656 * possible for nuisance warnings to appear that disappear on later
5657 * passes.
5658 */
5659 errhold = nasm_error_hold_push();
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005660
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005661 while (*tail) /* main token loop */
H. Peter Anvin322bee02019-08-10 01:38:06 -07005662 expanded |= !!expand_one_smacro(&tail);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005663
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005664 if (!expanded)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005665 break; /* Done! */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005666
5667 /*
5668 * Now scan the entire line and look for successive TOK_IDs
5669 * that resulted after expansion (they can't be produced by
5670 * tokenize()). The successive TOK_IDs should be concatenated.
5671 * Also we look for %+ tokens and concatenate the tokens
5672 * before and after them (without white spaces in between).
5673 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005674 if (!paste_tokens(&tline, tmatch, ARRAY_SIZE(tmatch), true))
5675 break; /* Done again! */
5676
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005677 nasm_error_hold_pop(errhold, false);
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005678 expanded = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005679 }
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005680 nasm_error_hold_pop(errhold, true);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005681
H. Peter Anvin8571f062019-09-23 16:40:03 -07005682 if (!tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005683 /*
5684 * The expression expanded to empty line;
5685 * we can't return NULL because of the "trick" above.
5686 * Just set the line to a single WHITESPACE token.
H. Peter Anvin8571f062019-09-23 16:40:03 -07005687 */
5688
5689 tline = new_White(NULL);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005690 }
5691
H. Peter Anvin8571f062019-09-23 16:40:03 -07005692 steal_Token(org_tline, tline);
5693 org_tline->next = tline->next;
5694 delete_Token(tline);
5695
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005696 return org_tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005697}
5698
5699/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005700 * Similar to expand_smacro but used exclusively with macro identifiers
5701 * right before they are fetched in. The reason is that there can be
5702 * identifiers consisting of several subparts. We consider that if there
5703 * are more than one element forming the name, user wants a expansion,
5704 * otherwise it will be left as-is. Example:
5705 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005706 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005707 *
5708 * the identifier %$abc will be left as-is so that the handler for %define
5709 * will suck it and define the corresponding value. Other case:
5710 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005711 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005712 *
5713 * In this case user wants name to be expanded *before* %define starts
5714 * working, so we'll expand %$abc into something (if it has a value;
5715 * otherwise it will be left as-is) then concatenate all successive
5716 * PP_IDs into one.
5717 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005718static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005719{
5720 Token *cur, *oldnext = NULL;
5721
H. Peter Anvin734b1882002-04-30 21:01:08 +00005722 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005723 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005724
5725 cur = tline;
5726 while (cur->next &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07005727 (cur->next->type == TOK_ID || cur->next->type == TOK_PREPROC_ID ||
5728 cur->next->type == TOK_LOCAL_MACRO || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005729 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005730
5731 /* If identifier consists of just one token, don't expand */
5732 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005733 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005734
H. Peter Anvine2c80182005-01-15 22:15:51 +00005735 if (cur) {
5736 oldnext = cur->next; /* Detach the tail past identifier */
5737 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005738 }
5739
H. Peter Anvin734b1882002-04-30 21:01:08 +00005740 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005741
H. Peter Anvine2c80182005-01-15 22:15:51 +00005742 if (cur) {
5743 /* expand_smacro possibly changhed tline; re-scan for EOL */
5744 cur = tline;
5745 while (cur && cur->next)
5746 cur = cur->next;
5747 if (cur)
5748 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005749 }
5750
5751 return tline;
5752}
5753
5754/*
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005755 * This is called from find_mmacro_in_list() after finding a suitable macro.
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005756 */
5757static MMacro *use_mmacro(MMacro *m, int *nparamp, Token ***paramsp)
5758{
5759 int nparam = *nparamp;
5760 Token **params = *paramsp;
5761
5762 /*
5763 * This one is right. Just check if cycle removal
5764 * prohibits us using it before we actually celebrate...
5765 */
5766 if (m->in_progress > m->max_depth) {
5767 if (m->max_depth > 0) {
5768 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
5769 m->max_depth);
5770 }
5771 nasm_free(params);
5772 *nparamp = 0;
5773 *paramsp = NULL;
5774 return NULL;
5775 }
5776
5777 /*
5778 * It's right, and we can use it. Add its default
5779 * parameters to the end of our list if necessary.
5780 */
5781 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
5782 int newnparam = m->nparam_min + m->ndefs;
5783 params = nasm_realloc(params, sizeof(*params) * (newnparam+2));
5784 memcpy(&params[nparam+1], &m->defaults[nparam+1-m->nparam_min],
5785 (newnparam - nparam) * sizeof(*params));
5786 nparam = newnparam;
5787 }
5788 /*
5789 * If we've gone over the maximum parameter count (and
5790 * we're in Plus mode), ignore parameters beyond
5791 * nparam_max.
5792 */
5793 if (m->plus && nparam > m->nparam_max)
5794 nparam = m->nparam_max;
5795
5796 /*
5797 * If nparam was adjusted above, make sure the list is still
5798 * NULL-terminated.
5799 */
5800 params[nparam+1] = NULL;
5801
5802 /* Done! */
5803 *paramsp = params;
5804 *nparamp = nparam;
5805 return m;
5806}
5807
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005808/*
5809 * Search a macro list and try to find a match. If matching, call
5810 * use_mmacro() to set up the macro call. m points to the list of
5811 * search, which is_mmacro() sets to the first *possible* match.
5812 */
5813static MMacro *
5814find_mmacro_in_list(MMacro *m, const char *finding,
5815 int *nparamp, Token ***paramsp)
5816{
5817 int nparam = *nparamp;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005818
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005819 while (m) {
5820 if (m->nparam_min <= nparam
5821 && (m->plus || nparam <= m->nparam_max)) {
5822 /*
5823 * This one matches, use it.
5824 */
5825 return use_mmacro(m, nparamp, paramsp);
5826 }
5827
5828 /*
5829 * Otherwise search for the next one with a name match.
5830 */
5831 list_for_each(m, m->next) {
5832 if (!mstrcmp(m->name, finding, m->casesense))
5833 break;
5834 }
5835 }
5836
5837 return NULL;
5838}
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005839
5840/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005841 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005842 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005843 * to check for an initial label - that's taken care of in
5844 * expand_mmacro - but must check numbers of parameters. Guaranteed
5845 * to be called with tline->type == TOK_ID, so the putative macro
5846 * name is easy to find.
5847 */
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005848static MMacro *is_mmacro(Token * tline, int *nparamp, Token ***paramsp)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005849{
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005850 MMacro *head, *m, *found;
5851 Token **params, **comma;
5852 int raw_nparam, nparam;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005853 const char *finding = tok_text(tline);
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005854 bool empty_args = !tline->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005855
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005856 *nparamp = 0;
5857 *paramsp = NULL;
5858
H. Peter Anvin8571f062019-09-23 16:40:03 -07005859 head = (MMacro *) hash_findix(&mmacros, finding);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005860
5861 /*
5862 * Efficiency: first we see if any macro exists with the given
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005863 * name which isn't already excluded by macro cycle removal.
5864 * (The cycle removal test here helps optimize the case of wrapping
5865 * instructions, and is cheap to do here.)
5866 *
5867 * If not, we can return NULL immediately. _Then_ we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005868 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005869 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005870 */
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005871 list_for_each(m, head) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005872 if (!mstrcmp(m->name, finding, m->casesense) &&
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005873 (m->in_progress != 1 || m->max_depth > 0))
5874 break; /* Found something that needs consideration */
5875 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005876 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005877 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005878
5879 /*
5880 * OK, we have a potential macro. Count and demarcate the
5881 * parameters.
5882 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005883 comma = count_mmac_params(tline->next, nparamp, paramsp);
5884 raw_nparam = *nparamp;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005885
5886 /*
5887 * Search for an exact match. This cannot come *before* the m
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005888 * found in the list search before, so we can start there.
5889 *
5890 * If found is NULL and *paramsp has been cleared, then we
5891 * encountered an error for which we have already issued a
5892 * diagnostic, so we should not proceed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005893 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005894 found = find_mmacro_in_list(m, finding, nparamp, paramsp);
5895 if (!*paramsp)
5896 return NULL;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005897
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005898 nparam = *nparamp;
5899 params = *paramsp;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005900
5901 /*
5902 * Special weirdness: in NASM < 2.15, an expansion of
5903 * *only* whitespace, as can happen during macro expansion under
5904 * certain circumstances, is counted as zero arguments for the
5905 * purpose of %0, but one argument for the purpose of macro
5906 * matching! In particular, this affects:
5907 *
5908 * foobar %1
5909 *
5910 * ... with %1 being empty; this would call the one-argument
5911 * version of "foobar" with an empty argument, equivalent to
5912 *
5913 * foobar {%1}
5914 *
5915 * ... except that %0 would be set to 0 inside foobar, even if
5916 * foobar is declared with "%macro foobar 1" or equivalent!
5917 *
5918 * The proper way to do that is to define "%macro foobar 0-1".
5919 *
5920 * To be compatible without doing something too stupid, try to
5921 * match a zero-argument macro first, but if that fails, try
5922 * for a one-argument macro with the above behavior.
5923 *
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005924 * Furthermore, NASM < 2.15 will match stripping a tailing empty
5925 * argument, but in that case %0 *does* reflect that this argument
5926 * have been stripped; this is handled in count_mmac_params().
5927 *
5928 * To disable these insane legacy behaviors, use:
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005929 *
5930 * %pragma preproc sane_empty_expansion yes
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005931 *
5932 *!macro-params-legacy [on] improperly calling multi-line macro for legacy support
5933 *! warns about \i{multi-line macros} being invoked
5934 *! with the wrong number of parameters, but for bug-compatibility
5935 *! with NASM versions older than 2.15, NASM tried to fix up the
5936 *! parameters to match the legacy behavior and call the macro anyway.
5937 *! This can happen in certain cases where there are empty arguments
5938 *! without braces, sometimes as a result of macro expansion.
5939 *!-
5940 *! The legacy behavior is quite strange and highly context-dependent,
5941 *! and can be disabled with:
5942 *!-
H. Peter Anvin (Intel)de8817d2020-06-27 22:30:50 -07005943 *! \c %pragma preproc sane_empty_expansion true
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005944 *!-
5945 *! It is highly recommended to use this option in new code.
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005946 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005947 if (!ppopt.sane_empty_expansion) {
5948 if (!found) {
5949 if (raw_nparam == 0 && !empty_args) {
5950 /*
5951 * A single all-whitespace parameter as the only thing?
5952 * Look for a one-argument macro, but don't adjust
5953 * *nparamp.
5954 */
5955 int bogus_nparam = 1;
5956 params[2] = NULL;
5957 found = find_mmacro_in_list(m, finding, &bogus_nparam, paramsp);
5958 } else if (raw_nparam > 1 && comma) {
5959 Token *comma_tail = *comma;
5960
5961 /*
5962 * Drop the terminal argument and try again.
5963 * If we fail, we need to restore the comma to
5964 * preserve tlist.
5965 */
5966 *comma = NULL;
5967 *nparamp = raw_nparam - 1;
5968 found = find_mmacro_in_list(m, finding, nparamp, paramsp);
5969 if (found)
5970 free_tlist(comma_tail);
5971 else
5972 *comma = comma_tail;
5973 }
5974
5975 if (!*paramsp)
5976 return NULL;
5977 } else if (comma) {
5978 free_tlist(*comma);
5979 *comma = NULL;
5980 if (raw_nparam > found->nparam_min &&
5981 raw_nparam <= found->nparam_min + found->ndefs) {
5982 /* Replace empty argument with default parameter */
5983 params[raw_nparam] =
5984 found->defaults[raw_nparam - found->nparam_min];
5985 } else if (raw_nparam > found->nparam_max && found->plus) {
5986 /* Just drop the comma, don't adjust argument count */
5987 } else {
5988 /* Drop argument. This may cause nparam < nparam_min. */
5989 params[raw_nparam] = NULL;
5990 *nparamp = nparam = raw_nparam - 1;
5991 }
5992 }
5993
5994 if (found) {
5995 if (raw_nparam < found->nparam_min ||
5996 (raw_nparam > found->nparam_max && !found->plus)) {
5997 nasm_warn(WARN_MACRO_PARAMS_LEGACY,
5998 "improperly calling multi-line macro `%s' with %d parameters",
5999 found->name, raw_nparam);
6000 } else if (comma) {
6001 nasm_warn(WARN_MACRO_PARAMS_LEGACY,
6002 "dropping trailing empty parameter in call to multi-line macro `%s'", found->name);
6003 }
6004 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006005 }
6006
6007 /*
6008 * After all that, we didn't find one with the right number of
6009 * parameters. Issue a warning, and fail to expand the macro.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006010 *!
6011 *!macro-params-multi [on] multi-line macro calls with wrong parameter count
6012 *! warns about \i{multi-line macros} being invoked
6013 *! with the wrong number of parameters. See \k{mlmacover} for an
6014 *! example of why you might want to disable this warning.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006015 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07006016 if (found)
6017 return found;
6018
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006019 nasm_warn(WARN_MACRO_PARAMS_MULTI,
6020 "multi-line macro `%s' exists, but not taking %d parameter%s",
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07006021 finding, nparam, (nparam == 1) ? "" : "s");
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07006022 nasm_free(*paramsp);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006023 return NULL;
6024}
6025
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006026
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006027#if 0
6028
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006029/*
6030 * Save MMacro invocation specific fields in
6031 * preparation for a recursive macro expansion
6032 */
6033static void push_mmacro(MMacro *m)
6034{
6035 MMacroInvocation *i;
6036
6037 i = nasm_malloc(sizeof(MMacroInvocation));
6038 i->prev = m->prev;
6039 i->params = m->params;
6040 i->iline = m->iline;
6041 i->nparam = m->nparam;
6042 i->rotate = m->rotate;
6043 i->paramlen = m->paramlen;
6044 i->unique = m->unique;
6045 i->condcnt = m->condcnt;
6046 m->prev = i;
6047}
6048
6049
6050/*
6051 * Restore MMacro invocation specific fields that were
6052 * saved during a previous recursive macro expansion
6053 */
6054static void pop_mmacro(MMacro *m)
6055{
6056 MMacroInvocation *i;
6057
6058 if (m->prev) {
6059 i = m->prev;
6060 m->prev = i->prev;
6061 m->params = i->params;
6062 m->iline = i->iline;
6063 m->nparam = i->nparam;
6064 m->rotate = i->rotate;
6065 m->paramlen = i->paramlen;
6066 m->unique = i->unique;
6067 m->condcnt = i->condcnt;
6068 nasm_free(i);
6069 }
6070}
6071
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006072#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006073
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006074/*
H. Peter Anvin (Intel)ffe89dd2019-08-20 16:06:36 -07006075 * List an mmacro call with arguments (-Lm option)
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07006076 */
6077static void list_mmacro_call(const MMacro *m)
6078{
6079 const char prefix[] = " ;;; [macro] ";
6080 size_t namelen, size;
6081 char *buf, *p;
6082 unsigned int i;
6083 const Token *t;
6084
6085 namelen = strlen(m->iname);
6086 size = namelen + sizeof(prefix); /* Includes final null (from prefix) */
6087
6088 for (i = 1; i <= m->nparam; i++) {
6089 int j = 0;
6090 size += 3; /* Braces and space/comma */
6091 list_for_each(t, m->params[i]) {
6092 if (j++ >= m->paramlen[i])
6093 break;
6094 size += (t->type == TOK_WHITESPACE) ? 1 : t->len;
6095 }
6096 }
6097
6098 buf = p = nasm_malloc(size);
6099 p = mempcpy(p, prefix, sizeof(prefix) - 1);
6100 p = mempcpy(p, m->iname, namelen);
6101 *p++ = ' ';
6102
6103 for (i = 1; i <= m->nparam; i++) {
6104 int j = 0;
6105 *p++ = '{';
6106 list_for_each(t, m->params[i]) {
6107 if (j++ >= m->paramlen[i])
6108 break;
H. Peter Anvin8571f062019-09-23 16:40:03 -07006109 p = mempcpy(p, tok_text(t), t->len);
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07006110 }
6111 *p++ = '}';
6112 *p++ = ',';
6113 }
6114
6115 *--p = '\0'; /* Replace last delimeter with null */
6116 lfmt->line(LIST_MACRO, -1, buf);
6117 nasm_free(buf);
6118}
6119
6120/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006121 * Expand the multi-line macro call made by the given line, if
6122 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006123 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006124 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006125static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006126{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006127 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006128 Token *label = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006129 bool dont_prepend = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006130 Token **params, *t, *tt;
6131 MMacro *m;
6132 Line *l, *ll;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07006133 int i, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07006134 const char *mname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006135 int nparam = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006136
6137 t = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006138 t = skip_white(t);
6139 /* if (!tok_type(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07006140 if (!tok_type(t, TOK_ID) && !tok_type(t, TOK_LOCAL_MACRO))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006141 return 0;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006142 m = is_mmacro(t, &nparam, &params);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006143 if (m) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006144 mname = tok_text(t);
H. Peter Anvinc751e862008-06-09 10:18:45 -07006145 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006146 Token *last;
6147 /*
6148 * We have an id which isn't a macro call. We'll assume
6149 * it might be a label; we'll also check to see if a
6150 * colon follows it. Then, if there's another id after
6151 * that lot, we'll check it again for macro-hood.
6152 */
6153 label = last = t;
6154 t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006155 if (tok_white(t))
H. Peter Anvine2c80182005-01-15 22:15:51 +00006156 last = t, t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006157 if (tok_is(t, ':')) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006158 dont_prepend = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006159 last = t, t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006160 if (tok_white(t))
H. Peter Anvine2c80182005-01-15 22:15:51 +00006161 last = t, t = t->next;
6162 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006163 if (!tok_type(t, TOK_ID) || !(m = is_mmacro(t, &nparam, &params)))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006164 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006165 last->next = NULL;
H. Peter Anvin8571f062019-09-23 16:40:03 -07006166 mname = tok_text(t);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006167 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006168 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006169
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07006170 if (unlikely(mmacro_deadman.total >= nasm_limit[LIMIT_MMACROS] ||
6171 mmacro_deadman.levels >= nasm_limit[LIMIT_MACRO_LEVELS])) {
6172 if (!mmacro_deadman.triggered) {
6173 nasm_nonfatal("interminable multiline macro recursion");
6174 mmacro_deadman.triggered = true;
6175 }
6176 return 0;
6177 }
6178
6179 mmacro_deadman.total++;
6180 mmacro_deadman.levels++;
6181
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006182 /*
6183 * Fix up the parameters: this involves stripping leading and
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07006184 * trailing whitespace and stripping braces if they are present.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006185 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006186 nasm_newn(paramlen, nparam+1);
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07006187
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006188 for (i = 1; (t = params[i]); i++) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006189 bool braced = false;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08006190 int brace = 0;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006191 int white = 0;
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07006192 bool comma = !m->plus || i < nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006193
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006194 t = skip_white(t);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006195 if (tok_is(t, '{')) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006196 t = t->next;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006197 brace = 1;
6198 braced = true;
6199 comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006200 }
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006201
6202 params[i] = t;
6203 for (; t; t = t->next) {
6204 if (tok_white(t)) {
6205 white++;
6206 continue;
6207 }
6208
6209 if (t->type == TOK_OTHER && t->len == 1) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006210 switch (t->text.a[0]) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006211 case ',':
6212 if (comma && !brace)
6213 goto endparam;
6214 break;
6215
6216 case '{':
6217 brace++;
6218 break;
6219
6220 case '}':
6221 brace--;
6222 if (braced && !brace) {
6223 paramlen[i] += white;
6224 goto endparam;
6225 }
6226 break;
6227
6228 default:
6229 break;
6230 }
6231 }
6232
6233 paramlen[i] += white + 1;
6234 white = 0;
6235 }
6236 endparam:
6237 ;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006238 }
6239
6240 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006241 * OK, we have a MMacro structure together with a set of
6242 * parameters. We must now go through the expansion and push
6243 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00006244 * parameter tokens and macro-local tokens doesn't get done
6245 * until the single-line macro substitution process; this is
6246 * because delaying them allows us to change the semantics
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006247 * later through %rotate and give the right semantics for
6248 * nested mmacros.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006249 *
6250 * First, push an end marker on to istk->expansion, mark this
6251 * macro as in progress, and set up its invocation-specific
6252 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006253 */
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07006254 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006255 ll->next = istk->expansion;
6256 ll->finishes = m;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006257 ll->where = istk->where;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006258 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006259
6260 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006261 * Save the previous MMacro expansion in the case of
6262 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006263 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006264#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006265 if (m->max_depth && m->in_progress)
6266 push_mmacro(m);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006267#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006268
6269 m->in_progress ++;
6270 m->params = params;
6271 m->iline = tline;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006272 m->iname = nasm_strdup(mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006273 m->nparam = nparam;
6274 m->rotate = 0;
6275 m->paramlen = paramlen;
6276 m->unique = unique++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006277 m->condcnt = 0;
6278
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006279 m->mstk = istk->mstk;
6280 istk->mstk.mstk = istk->mstk.mmac = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006281
6282 list_for_each(l, m->expansion) {
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07006283 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006284 ll->next = istk->expansion;
6285 istk->expansion = ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006286 ll->first = dup_tlist(l->first, NULL);
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006287 ll->where = l->where;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006288 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006289
6290 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006291 * If we had a label, and this macro definition does not include
6292 * a %00, push it on as the first line of, ot
H. Peter Anvineba20a72002-04-30 20:53:55 +00006293 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006294 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006295 if (label) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006296 /*
6297 * We had a label. If this macro contains an %00 parameter,
6298 * save the value as a special parameter (which is what it
6299 * is), otherwise push it as the first line of the macro
6300 * expansion.
6301 */
6302 if (m->capture_label) {
6303 params[0] = dup_Token(NULL, label);
6304 paramlen[0] = 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006305 free_tlist(startline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006306 } else {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006307 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006308 ll->finishes = NULL;
6309 ll->next = istk->expansion;
6310 istk->expansion = ll;
6311 ll->first = startline;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006312 ll->where = istk->where;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006313 if (!dont_prepend) {
6314 while (label->next)
6315 label = label->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07006316 label->next = tt = make_tok_char(NULL, ':');
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006317 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006318 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00006319 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006320
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07006321 istk->nolist += !!(m->nolist & NL_LIST);
6322 istk->noline += !!(m->nolist & NL_LINE);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006323
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07006324 if (!istk->nolist) {
6325 lfmt->uplevel(LIST_MACRO, 0);
6326
6327 if (list_option('m'))
6328 list_mmacro_call(m);
6329 }
6330
6331 if (!istk->noline)
6332 src_macro_push(m, istk->where);
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07006333
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006334 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006335}
6336
H. Peter Anvin130736c2016-02-17 20:27:41 -08006337/*
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006338 * This function decides if an error message should be suppressed.
6339 * It will never be called with a severity level of ERR_FATAL or
6340 * higher.
H. Peter Anvin130736c2016-02-17 20:27:41 -08006341 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006342static bool pp_suppress_error(errflags severity)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02006343{
H. Peter Anvin130736c2016-02-17 20:27:41 -08006344 /*
6345 * If we're in a dead branch of IF or something like it, ignore the error.
6346 * However, because %else etc are evaluated in the state context
6347 * of the previous branch, errors might get lost:
6348 * %if 0 ... %else trailing garbage ... %endif
6349 * So %else etc should set the ERR_PP_PRECOND flag.
6350 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006351 if (istk && istk->conds &&
H. Peter Anvin130736c2016-02-17 20:27:41 -08006352 ((severity & ERR_PP_PRECOND) ?
6353 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07006354 !emitting(istk->conds->state)))
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006355 return true;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02006356
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006357 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00006358}
6359
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006360static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006361stdmac_file(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006362{
6363 (void)s;
6364 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006365 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006366
H. Peter Anvin8571f062019-09-23 16:40:03 -07006367 return make_tok_qstr(NULL, src_get_fname());
H. Peter Anvin8b262472019-02-26 14:00:54 -08006368}
6369
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006370static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006371stdmac_line(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006372{
6373 (void)s;
6374 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006375 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006376
H. Peter Anvin8571f062019-09-23 16:40:03 -07006377 return make_tok_num(NULL, src_get_linnum());
H. Peter Anvin8b262472019-02-26 14:00:54 -08006378}
6379
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006380static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006381stdmac_bits(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006382{
6383 (void)s;
6384 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006385 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006386
H. Peter Anvin8571f062019-09-23 16:40:03 -07006387 return make_tok_num(NULL, globalbits);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006388}
6389
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006390static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006391stdmac_ptr(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006392{
H. Peter Anvin8b262472019-02-26 14:00:54 -08006393 (void)s;
6394 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006395 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006396
6397 switch (globalbits) {
6398 case 16:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006399 return new_Token(NULL, TOK_ID, "word", 4);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006400 case 32:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006401 return new_Token(NULL, TOK_ID, "dword", 5);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006402 case 64:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006403 return new_Token(NULL, TOK_ID, "qword", 5);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006404 default:
6405 panic();
6406 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08006407}
6408
H. Peter Anvin8b262472019-02-26 14:00:54 -08006409/* Add magic standard macros */
6410struct magic_macros {
6411 const char *name;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006412 int nparam;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006413 ExpandSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006414};
6415static const struct magic_macros magic_macros[] =
6416{
H. Peter Anvind2354082019-08-27 16:38:48 -07006417 { "__?FILE?__", 0, stdmac_file },
6418 { "__?LINE?__", 0, stdmac_line },
6419 { "__?BITS?__", 0, stdmac_bits },
6420 { "__?PTR?__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08006421 { NULL, 0, NULL }
6422};
6423
6424static void pp_add_magic_stdmac(void)
6425{
6426 const struct magic_macros *m;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006427 SMacro tmpl;
6428
6429 nasm_zero(tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006430
6431 for (m = magic_macros; m->name; m++) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006432 tmpl.nparam = m->nparam;
6433 tmpl.expand = m->func;
6434 define_smacro(m->name, true, NULL, &tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006435 }
6436}
6437
H. Peter Anvin734b1882002-04-30 21:01:08 +00006438static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006439pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006440{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006441 int apass;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006442 struct Include *inc;
H. Peter Anvin7383b402008-09-24 10:20:40 -07006443
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006444 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006445 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07006446 nested_mac_count = 0;
6447 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07006448 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006449 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07006450 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006451 pp_mode = mode;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07006452
6453 /* Reset options to default */
6454 nasm_zero(ppopt);
H. Peter Anvinf7606612016-07-13 14:23:48 -07006455
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07006456 if (!use_loaded)
6457 use_loaded = nasm_malloc(use_package_count * sizeof(bool));
6458 memset(use_loaded, 0, use_package_count * sizeof(bool));
6459
H. Peter Anvin6686de22019-08-10 05:33:14 -07006460 /* First set up the top level input file */
6461 nasm_new(istk);
6462 istk->fp = nasm_open_read(file, NF_TEXT);
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006463 if (!istk->fp) {
6464 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'%s%s",
6465 file, errno ? " " : "", errno ? strerror(errno) : "");
6466 }
H. Peter Anvin6686de22019-08-10 05:33:14 -07006467 src_set(0, file);
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006468 istk->where = src_where();
H. Peter Anvin6686de22019-08-10 05:33:14 -07006469 istk->lineinc = 1;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006470
6471 strlist_add(deplist, file);
6472
6473 /*
6474 * Set up the stdmac packages as a virtual include file,
6475 * indicated by a null file pointer.
6476 */
6477 nasm_new(inc);
6478 inc->next = istk;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006479 src_set(0, NULL);
6480 inc->where = src_where();
H. Peter Anvin6686de22019-08-10 05:33:14 -07006481 inc->nolist = !list_option('b');
6482 istk = inc;
6483 lfmt->uplevel(LIST_INCLUDE, 0);
6484
H. Peter Anvin8b262472019-02-26 14:00:54 -08006485 pp_add_magic_stdmac();
6486
H. Peter Anvinf7606612016-07-13 14:23:48 -07006487 if (tasm_compatible_mode)
6488 pp_add_stdmac(nasm_stdmac_tasm);
6489
6490 pp_add_stdmac(nasm_stdmac_nasm);
6491 pp_add_stdmac(nasm_stdmac_version);
6492
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006493 if (extrastdmac)
6494 pp_add_stdmac(extrastdmac);
6495
H. Peter Anvinf7606612016-07-13 14:23:48 -07006496 stdmacpos = stdmacros[0];
6497 stdmacnext = &stdmacros[1];
6498
H. Peter Anvind2456592008-06-19 15:04:18 -07006499 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006500
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006501 /*
H. Peter Anvind2354082019-08-27 16:38:48 -07006502 * Define the __?PASS?__ macro. This is defined here unlike all the
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07006503 * other builtins, because it is special -- it varies between
6504 * passes -- but there is really no particular reason to make it
6505 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006506 *
6507 * 0 = dependencies only
6508 * 1 = preparatory passes
6509 * 2 = final pass
6510 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006511 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006512 switch (mode) {
6513 case PP_NORMAL:
6514 apass = pass_final() ? 2 : 1;
6515 break;
6516 case PP_DEPS:
6517 apass = 0;
6518 break;
6519 case PP_PREPROC:
6520 apass = 3;
6521 break;
6522 default:
6523 panic();
6524 }
6525
H. Peter Anvin8571f062019-09-23 16:40:03 -07006526 define_smacro("__?PASS?__", true, make_tok_num(NULL, apass), NULL);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006527}
6528
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006529static void pp_init(void)
6530{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006531}
6532
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006533/*
6534 * Get a line of tokens. If we popped the macro expansion/include stack,
6535 * we return a pointer to the dummy token tok_pop; at that point if
6536 * istk is NULL then we have reached end of input;
6537 */
6538static Token tok_pop; /* Dummy token placeholder */
6539
6540static Token *pp_tokline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006541{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006542 while (true) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006543 Line *l = istk->expansion;
6544 Token *tline = NULL;
6545 Token *dtline;
6546
H. Peter Anvine2c80182005-01-15 22:15:51 +00006547 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006548 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00006549 * buffer or from the input file.
6550 */
6551 tline = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006552 while (l && l->finishes) {
6553 MMacro *fm = l->finishes;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006554
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07006555 nasm_assert(fm == istk->mstk.mstk);
6556
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006557 if (!fm->name && fm->in_progress > 1) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006558 /*
6559 * This is a macro-end marker for a macro with no
6560 * name, which means it's not really a macro at all
6561 * but a %rep block, and the `in_progress' field is
6562 * more than 1, meaning that we still need to
6563 * repeat. (1 means the natural last repetition; 0
6564 * means termination by %exitrep.) We have
6565 * therefore expanded up to the %endrep, and must
6566 * push the whole block on to the expansion buffer
6567 * again. We don't bother to remove the macro-end
6568 * marker: we'd only have to generate another one
6569 * if we did.
6570 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006571 fm->in_progress--;
6572 list_for_each(l, fm->expansion) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006573 Line *ll;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006574
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006575 nasm_new(ll);
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006576 ll->next = istk->expansion;
6577 ll->first = dup_tlist(l->first, NULL);
6578 ll->where = l->where;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006579 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006580 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006581 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006582 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006583 MMacro *m = istk->mstk.mstk;
6584
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006585 /*
6586 * Check whether a `%rep' was started and not ended
6587 * within this macro expansion. This can happen and
6588 * should be detected. It's a fatal error because
6589 * I'm too confused to work out how to recover
6590 * sensibly from it.
6591 */
6592 if (defining) {
6593 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07006594 nasm_panic("defining with name in expansion");
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006595 else if (m->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07006596 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006597 " expansion of macro `%s'", m->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006598 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006599
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006600 /*
6601 * FIXME: investigate the relationship at this point between
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006602 * istk->mstk.mstk and fm
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006603 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006604 istk->mstk = m->mstk;
6605 if (m->name) {
6606 /*
6607 * This was a real macro call, not a %rep, and
6608 * therefore the parameter information needs to
6609 * be freed and the iteration count/nesting
6610 * depth adjusted.
6611 */
6612
6613 if (!--mmacro_deadman.levels) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006614 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006615 * If all mmacro processing done,
6616 * clear all counters and the deadman
6617 * message trigger.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006618 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006619 nasm_zero(mmacro_deadman); /* Clear all counters */
Adam Majer91e72402017-07-25 10:42:01 +02006620 }
6621
Adam Majer91e72402017-07-25 10:42:01 +02006622#if 0
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006623 if (m->prev) {
6624 pop_mmacro(m);
6625 fm->in_progress --;
6626 } else
Adam Majer91e72402017-07-25 10:42:01 +02006627#endif
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006628 {
6629 nasm_free(m->params);
6630 free_tlist(m->iline);
6631 nasm_free(m->paramlen);
6632 fm->in_progress = 0;
6633 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006634 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006635
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07006636 if (fm->nolist & NL_LIST) {
6637 istk->nolist--;
6638 } else if (!istk->nolist) {
6639 lfmt->downlevel(LIST_MACRO);
6640 }
6641
6642 if (fm->nolist & NL_LINE) {
6643 istk->noline--;
6644 } else if (!istk->noline) {
6645 if (fm == src_macro_current())
6646 src_macro_pop();
6647 src_update(l->where);
6648 }
6649
6650 istk->where = l->where;
6651
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006652 /*
6653 * FIXME It is incorrect to always free_mmacro here.
6654 * It leads to usage-after-free.
6655 *
6656 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
6657 */
6658#if 0
6659 else
6660 free_mmacro(m);
6661#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006662 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006663 istk->expansion = l->next;
6664 nasm_free(l);
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07006665
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006666 return &tok_pop;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006667 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006668
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006669 do { /* until we get a line we can use */
6670 char *line;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006671
6672 if (istk->expansion) { /* from a macro expansion */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006673 Line *l = istk->expansion;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006674
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006675 istk->expansion = l->next;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006676 istk->where = l->where;
6677 tline = l->first;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006678 nasm_free(l);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006679
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07006680 if (!istk->noline)
6681 src_update(istk->where);
6682
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006683 if (!istk->nolist) {
6684 line = detoken(tline, false);
6685 lfmt->line(LIST_MACRO, istk->where.lineno, line);
6686 nasm_free(line);
6687 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006688 } else if ((line = read_line())) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006689 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006690 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006691 nasm_free(line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006692 } else {
6693 /*
6694 * The current file has ended; work down the istk
6695 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00006696 Include *i = istk;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006697 Include *is;
6698
H. Peter Anvin6686de22019-08-10 05:33:14 -07006699 if (i->fp)
6700 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006701 if (i->conds) {
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006702 /* nasm_fatal can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07006703 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006704 }
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006705
6706 list_for_each(is, i->next) {
6707 if (is->fp) {
6708 lfmt->downlevel(LIST_INCLUDE);
6709 src_update(is->where);
6710 break;
6711 }
6712 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00006713 istk = i->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006714 nasm_free(i);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006715 return &tok_pop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006716 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006717 } while (0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006718
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006719 /*
6720 * We must expand MMacro parameters and MMacro-local labels
6721 * _before_ we plunge into directive processing, to cope
6722 * with things like `%define something %1' such as STRUC
6723 * uses. Unless we're _defining_ a MMacro, in which case
6724 * those tokens should be left alone to go into the
6725 * definition; and unless we're in a non-emitting
6726 * condition, in which case we don't want to meddle with
6727 * anything.
6728 */
H. Peter Anvin (Intel)bacf04a2020-06-08 13:29:06 -07006729 if (!defining &&
6730 !(istk->conds && !emitting(istk->conds->state)) &&
6731 !(istk->mstk.mmac && !istk->mstk.mmac->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006732 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006733 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006734
H. Peter Anvine2c80182005-01-15 22:15:51 +00006735 /*
6736 * Check the line to see if it's a preprocessor directive.
6737 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006738 if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) {
6739 if (dtline)
6740 return dtline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006741 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006742 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006743 * We're defining a multi-line macro. We emit nothing
6744 * at all, and just
6745 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00006746 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006747 MMacro *mmac = defining->dstk.mmac;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006748 Line *l;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006749
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006750 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006751 l->next = defining->expansion;
6752 l->first = tline;
6753 l->finishes = NULL;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006754 l->where = istk->where;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006755 defining->expansion = l;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006756
6757 /*
6758 * Remember if this mmacro expansion contains %00:
6759 * if it does, we will have to handle leading labels
6760 * specially.
6761 */
6762 if (mmac) {
6763 const Token *t;
6764 list_for_each(t, tline) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006765 if (!memcmp(t->text.a, "%00", 4))
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006766 mmac->capture_label = true;
6767 }
6768 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006769 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006770 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006771 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00006772 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006773 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00006774 * directive so we keep our place correctly.
6775 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006776 free_tlist(tline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006777 } else if (istk->mstk.mstk && !istk->mstk.mstk->in_progress) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006778 /*
6779 * We're in a %rep block which has been terminated, so
6780 * we're walking through to the %endrep without
6781 * emitting anything. Emit nothing at all, not even a
6782 * blank line: when we emerge from the %rep block we'll
6783 * give a line-number directive so we keep our place
6784 * correctly.
6785 */
6786 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006787 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006788 tline = expand_smacro(tline);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006789 if (!expand_mmacro(tline))
6790 return tline;
6791 }
6792 }
6793}
6794
6795static char *pp_getline(void)
6796{
6797 char *line = NULL;
6798 Token *tline;
6799
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006800 while (true) {
6801 tline = pp_tokline();
6802 if (tline == &tok_pop) {
6803 /*
6804 * We popped the macro/include stack. If istk is empty,
6805 * we are at end of input, otherwise just loop back.
6806 */
6807 if (!istk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006808 break;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006809 } else {
6810 /*
6811 * De-tokenize the line and emit it.
6812 */
6813 line = detoken(tline, true);
6814 free_tlist(tline);
6815 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006816 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006817 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006818
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006819 if (list_option('e') && istk && !istk->nolist && line && line[0]) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006820 char *buf = nasm_strcat(" ;;; ", line);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006821 lfmt->line(LIST_MACRO, -1, buf);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006822 nasm_free(buf);
6823 }
6824
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006825 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006826}
6827
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006828static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006829{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006830 if (defining) {
6831 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006832 nasm_nonfatal("end of file while still defining macro `%s'",
6833 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006834 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006835 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006836 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006837
6838 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006839 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006840 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08006841
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006842 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006843 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07006844 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006845 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006846 Include *i = istk;
6847 istk = istk->next;
6848 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03006849 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006850 }
6851 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006852 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07006853 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006854}
6855
6856static void pp_cleanup_session(void)
6857{
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07006858 nasm_free(use_loaded);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006859 free_llist(predef);
6860 predef = NULL;
6861 delete_Blocks();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006862 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006863}
6864
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006865static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006866{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006867 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006868}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00006869
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006870static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006871{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006872 Token *inc, *space, *name;
6873 Line *l;
6874
H. Peter Anvin734b1882002-04-30 21:01:08 +00006875 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006876 space = new_White(name);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006877 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006878
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006879 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006880 l->next = predef;
6881 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006882 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006883 predef = l;
6884}
6885
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006886static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006887{
6888 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006889 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00006890 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006891
6892 equals = strchr(definition, '=');
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006893 space = new_White(NULL);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006894 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006895 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006896 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00006897 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006898 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006899 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006900
H. Peter Anvin8571f062019-09-23 16:40:03 -07006901 /* We can't predefine a TOK_LOCAL_MACRO for obvious reasons... */
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006902 if (space->next->type != TOK_PREPROC_ID &&
6903 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08006904 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006905
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006906 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006907 l->next = predef;
6908 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006909 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006910 predef = l;
6911}
6912
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006913static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00006914{
6915 Token *def, *space;
6916 Line *l;
6917
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006918 space = new_White(NULL);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006919 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006920 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00006921
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006922 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00006923 l->next = predef;
6924 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006925 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00006926 predef = l;
6927}
6928
H. Peter Anvin05990342018-06-11 13:32:42 -07006929/* Insert an early preprocessor command that doesn't need special handling */
6930static void pp_pre_command(const char *what, char *string)
6931{
6932 char *cmd;
6933 Token *def, *space;
6934 Line *l;
6935
6936 def = tokenize(string);
6937 if (what) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006938 space = new_White(def);
H. Peter Anvin8571f062019-09-23 16:40:03 -07006939 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
6940 def = new_Token(space, TOK_PREPROC_ID, cmd, nasm_last_string_len());
6941 nasm_free(cmd);
H. Peter Anvin05990342018-06-11 13:32:42 -07006942 }
6943
6944 l = nasm_malloc(sizeof(Line));
6945 l->next = predef;
6946 l->first = def;
6947 l->finishes = NULL;
6948 predef = l;
6949}
6950
H. Peter Anvinf7606612016-07-13 14:23:48 -07006951static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006952{
H. Peter Anvinf7606612016-07-13 14:23:48 -07006953 macros_t **mp;
6954
6955 /* Find the end of the list and avoid duplicates */
6956 for (mp = stdmacros; *mp; mp++) {
6957 if (*mp == macros)
6958 return; /* Nothing to do */
6959 }
6960
6961 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
6962
6963 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00006964}
6965
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006966static void pp_extra_stdmac(macros_t *macros)
6967{
6968 extrastdmac = macros;
6969}
6970
H. Peter Anvin8571f062019-09-23 16:40:03 -07006971/* Create a numeric token */
6972static Token *make_tok_num(Token *next, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006973{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04006974 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08006975 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvin8571f062019-09-23 16:40:03 -07006976 return new_Token(next, TOK_NUMBER, numbuf, len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006977}
6978
H. Peter Anvin8571f062019-09-23 16:40:03 -07006979/* Create a quoted string token */
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07006980static Token *make_tok_qstr_len(Token *next, const char *str, size_t len)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006981{
H. Peter Anvin8571f062019-09-23 16:40:03 -07006982 char *p = nasm_quote(str, &len);
6983 return new_Token_free(next, TOK_STRING, p, len);
6984}
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07006985static Token *make_tok_qstr(Token *next, const char *str)
6986{
6987 return make_tok_qstr_len(next, str, strlen(str));
6988}
H. Peter Anvin8571f062019-09-23 16:40:03 -07006989
6990/* Create a single-character operator token */
6991static Token *make_tok_char(Token *next, char op)
6992{
6993 Token *t = new_Token(next, TOK_OTHER, NULL, 1);
6994 t->text.a[0] = op;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006995 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006996}
6997
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006998/*
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07006999 * Descent the macro hierarchy and display the expansion after
7000 * encountering an error message.
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07007001 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08007002static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07007003{
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07007004 const MMacro *m;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07007005
H. Peter Anvinddb29062018-12-11 00:06:29 -08007006 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07007007
7008 while ((m = src_error_down())) {
7009 nasm_error(severity, "... from macro `%s' defined", m->name);
7010 }
7011
7012 src_error_reset();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07007013}
7014
H. Peter Anvine7469712016-02-18 02:20:59 -08007015const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07007016 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00007017 pp_reset,
7018 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08007019 pp_cleanup_pass,
7020 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03007021 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04007022 pp_pre_define,
7023 pp_pre_undefine,
7024 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07007025 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07007026 pp_include_path,
7027 pp_error_list_macros,
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07007028 pp_suppress_error
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00007029};